WIP: finishing SSA renaming

This commit is contained in:
2024-12-26 01:58:15 +03:00
parent d267dc047a
commit 7df02737c7
3 changed files with 59 additions and 12 deletions

View File

@@ -62,6 +62,17 @@ void BBlock::addInstructionInFront(IR_Block* item)
item->setBasicBlock(this);
}
void BBlock::addInstructionBeforeInstruction(IR_Block* item, Instruction* instruction)
{
for (auto it = instructions.begin(); it != instructions.end(); ++it) {
if ((*it)->getInstruction() == instruction) {
instructions.insert(instructions.begin(), item);
item->setBasicBlock(this);
return;
}
}
}
int BBlock::removePrev(BBlock* removed)
{
auto it = std::remove(prev.begin(), prev.end(), removed);

View File

@@ -39,6 +39,7 @@ namespace SAPFOR
BasicBlock(IR_Block* item);
BasicBlock(const BasicBlock& copyFrom);
void addInstructionBeforeInstruction(IR_Block* item, Instruction* istruction);
void addInstructionInFront(IR_Block* item);
void addInstruction(IR_Block* item);
void addPrev(BasicBlock* prev_) { prev.push_back(prev_); }

View File

@@ -34,16 +34,42 @@ static void printBlock(BBlock* block) {
for (auto i : block->getInstructions())
{
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;
if (i->getInstruction()->getResult() != nullptr) {
cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl;
string resValue = "";
string arg1Value = "";
string arg2Value = "";
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR) {
resValue = i->getInstruction()->getResult()->getValue();
i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber()));
}
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR) {
arg1Value = i->getInstruction()->getArg1()->getValue();
i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber()));
}
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR) {
arg2Value = i->getInstruction()->getArg2()->getValue();
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
}
cout << i->getNumber() << " " << i->getInstruction()->dump() << (i->getInstruction()->getResult() != nullptr ? " 1 " : " 0 ") << (i->getInstruction()->getArg1() != nullptr ? "1 " : "0 ") << (i->getInstruction()->getArg2() != nullptr ? "1 " : "0 ") << endl;
/*if (i->getInstruction()->getResult() != nullptr) {
cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl;
})*/
/*if (i->getInstruction()->getOperation() == CFG_OP::F_CALL) {
cout << endl;
cout << "arg1 - " << (i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::FUNC ? "1" : "0") << "arg2 - " << (i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::CONST ? "1" : "0") << "res - " << i->getInstruction()->getResult() << endl;
}*/
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR) {
i->getInstruction()->getResult()->setValue(resValue);
}
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR) {
i->getInstruction()->getArg1()->setValue(arg1Value);
}
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR) {
i->getInstruction()->getArg2()->setValue(arg2Value);
}
}
cout << endl;
@@ -290,7 +316,7 @@ static vector<BBlock*> getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArg
for (auto dfBlock : dominatorBorders[block]) {
if (hasFiFunction.find(dfBlock) == hasFiFunction.end()) {
hasFiFunction.insert(dfBlock);
hasFiFunction.insert(dfBlock);
Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, fiFunc, paramCount, var);
@@ -394,6 +420,21 @@ void RenameInstructionVars(BBlock* block, map<string, int>& counter, map<string,
}
}
void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stack) {
auto size = block->getInstructions().size();
for (auto i = 0; i < size; i++) {
auto irBlock = block->getInstructions()[i];
auto instruction = irBlock->getInstruction();
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) {
auto paramInstruction = new Instruction(CFG_OP::PARAM, stack[instruction->getResult()->getValue()].top());
block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction);
i++;
}
}
}
void RenameIR(BBlock* block, map<BBlock*, vector<BBlock*>>& dominatorTree, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
@@ -402,14 +443,13 @@ void RenameIR(BBlock* block, map<BBlock*, vector<BBlock*>>& dominatorTree, map<s
RenameInstructionVars(block, counter, stack);
for (auto* successor : block->getNext()) {
RenameFiFunctionArgsVar(successor, stack);
}
for (auto* child : dominatorTree.at(block)) {
RenameIR(child, dominatorTree, counter, stack);
}
// Очистка стека после обработки инструкций
for (auto& irBlock : block->getInstructions()) {
auto instruction = irBlock->getInstruction();
if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) {
@@ -418,7 +458,6 @@ void RenameIR(BBlock* block, map<BBlock*, vector<BBlock*>>& dominatorTree, map<s
}
}
// Очистка стека после φ-функций
for (auto& irBlock : block->getInstructions()) {
auto instruction = irBlock->getInstruction();
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) {
@@ -428,10 +467,6 @@ void RenameIR(BBlock* block, map<BBlock*, vector<BBlock*>>& dominatorTree, map<s
}
}
map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR) {
map<FuncInfo*, vector<BBlock*>> result;