improved CFG functions style

This commit is contained in:
ALEXks
2025-05-30 17:06:21 +03:00
parent f7c66f537d
commit 06dd8848be
3 changed files with 15 additions and 15 deletions

View File

@@ -50,27 +50,28 @@ BBlock::BasicBlock(const BBlock& copyFrom)
prev = copyFrom.prev; prev = copyFrom.prev;
} }
void BBlock::addInstruction(IR_Block* item) void BBlock::addInstruction(IR_Block* item, bool pushFront)
{ {
instructions.push_back(item); if (pushFront)
instructions.insert(instructions.begin(), item);
else
instructions.push_back(item);
item->setBasicBlock(this); item->setBasicBlock(this);
} }
void BBlock::addInstructionInFront(IR_Block* item) void BBlock::addInstructionBefore(IR_Block* item, Instruction* before)
{ {
instructions.insert(instructions.begin(), item); checkNull(before, convertFileName(__FILE__).c_str(), __LINE__);
item->setBasicBlock(this); checkNull(item, convertFileName(__FILE__).c_str(), __LINE__);
}
void BBlock::addInstructionBeforeInstruction(IR_Block* item, Instruction* instruction)
{
for (auto it = instructions.begin(); it != instructions.end(); ++it) { for (auto it = instructions.begin(); it != instructions.end(); ++it) {
if ((*it)->getInstruction() == instruction) { if ((*it)->getInstruction() == before) {
instructions.insert(it, item); instructions.insert(it, item);
item->setBasicBlock(this); item->setBasicBlock(this);
return; return;
} }
} }
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
} }
int BBlock::removePrev(BBlock* removed) int BBlock::removePrev(BBlock* removed)
@@ -529,7 +530,7 @@ static int buildReachingDefs(const vector<BBlock*>& CFG, const FuncInfo* currF,
return iter; return iter;
} }
//Kosaraju<EFBFBD>Sharir algorithm //Kosaraju-Sharir algorithm
static vector<int> getStronglyConnectedComps(vector<vector<int>>& g) { static vector<int> getStronglyConnectedComps(vector<vector<int>>& g) {
// 1. For each vertex u of the graph, mark u as unvisited. Let l be empty. // 1. For each vertex u of the graph, mark u as unvisited. Let l be empty.
auto size = g.size(); auto size = g.size();

View File

@@ -39,9 +39,8 @@ namespace SAPFOR
BasicBlock(IR_Block* item); BasicBlock(IR_Block* item);
BasicBlock(const BasicBlock& copyFrom); BasicBlock(const BasicBlock& copyFrom);
void addInstructionBeforeInstruction(IR_Block* item, Instruction* istruction); void addInstructionBefore(IR_Block* item, Instruction* istruction);
void addInstructionInFront(IR_Block* item); void addInstruction(IR_Block* item, bool pushFront = false);
void addInstruction(IR_Block* item);
void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } void addPrev(BasicBlock* prev_) { prev.push_back(prev_); }
void addNext(BasicBlock* next_) { next.push_back(next_); } void addNext(BasicBlock* next_) { next.push_back(next_); }

View File

@@ -354,7 +354,7 @@ static void getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArgument*> glo
Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, new BArgument(*fiFunc), new BArgument(*paramCount), var, dfBlock->getInstructions()[0]->getInstruction()->getOperator()); Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, new BArgument(*fiFunc), new BArgument(*paramCount), var, dfBlock->getInstructions()[0]->getInstruction()->getOperator());
IR_Block* phiBlock = new IR_Block(phiInstruction); IR_Block* phiBlock = new IR_Block(phiInstruction);
dfBlock->addInstructionInFront(phiBlock); dfBlock->addInstruction(phiBlock, true);
//blocksWithFiFunctions.push_back(dfBlock); //blocksWithFiFunctions.push_back(dfBlock);
} }
@@ -477,7 +477,7 @@ void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stac
} }
paramInstruction->setOperator(block->getInstructions()[0]->getInstruction()->getOperator()); paramInstruction->setOperator(block->getInstructions()[0]->getInstruction()->getOperator());
block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction); block->addInstructionBefore(new IR_Block(paramInstruction), instruction);
instruction->getArg2()->setValue(to_string(stoi(instruction->getArg2()->getValue()) + 1)); instruction->getArg2()->setValue(to_string(stoi(instruction->getArg2()->getValue()) + 1));
i++; i++;