WIP try to fix renaming

This commit is contained in:
2025-03-04 23:24:20 +03:00
parent dec1a853db
commit b4038b532f

View File

@@ -50,7 +50,7 @@ static void printBlock(BBlock* block) {
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber())); 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; cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;// << (i->getInstruction()->getResult() != nullptr ? " 1 " : " 0 ") << (i->getInstruction()->getArg1() != nullptr ? "1 " : "0 ") << (i->getInstruction()->getArg2() != nullptr ? "1 " : "0 ") << endl;
/*if (i->getInstruction()->getResult() != nullptr) { /*if (i->getInstruction()->getResult() != nullptr) {
cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl; cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl;
@@ -320,6 +320,8 @@ static vector<BBlock*> getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArg
Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, fiFunc, paramCount, var); Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, fiFunc, paramCount, var);
//cout << "insert fi functio9n in " << dfBlock->getNumber() << " with var " << var->getValue() << endl;
IR_Block* phiBlock = new IR_Block(phiInstruction); IR_Block* phiBlock = new IR_Block(phiInstruction);
dfBlock->addInstructionInFront(phiBlock); dfBlock->addInstructionInFront(phiBlock);
@@ -421,11 +423,13 @@ void RenameInstructionVars(BBlock* block, map<string, int>& counter, map<string,
} }
void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stack) { void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stack) {
cout << "test" << endl;
auto size = block->getInstructions().size(); auto size = block->getInstructions().size();
for (auto i = 0; i < size; i++) { for (auto i = 0; i < size; i++) {
auto irBlock = block->getInstructions()[i]; auto irBlock = block->getInstructions()[i];
auto instruction = irBlock->getInstruction(); auto instruction = irBlock->getInstruction();
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) { if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) {
//cout << "test" << endl;
auto paramInstruction = new Instruction(CFG_OP::PARAM, stack[instruction->getResult()->getValue()].top()); auto paramInstruction = new Instruction(CFG_OP::PARAM, stack[instruction->getResult()->getValue()].top());
block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction); block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction);
@@ -435,8 +439,21 @@ void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stac
} }
} }
vector<BBlock*> findBlocksWithValue(map<BBlock*, BBlock*>& iDominators, BBlock* x) {
vector<BBlock*> result;
void RenameIR(BBlock* block, map<BBlock*, vector<BBlock*>>& dominatorTree, map<string, int>& counter, map<string, stack<BArgument*>>& stack) { // Проходим по всем элементам map
for (auto& pair : iDominators) {
// Если значение равно x, добавляем ключ в результат
if (pair.second == x) {
result.push_back(pair.first);
}
}
return result;
}
void RenameIR(BBlock* block, map<BBlock*, BBlock*>& iDominators, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
RenameFiFunctionResultVar(block, counter, stack); RenameFiFunctionResultVar(block, counter, stack);
@@ -446,8 +463,9 @@ void RenameIR(BBlock* block, map<BBlock*, vector<BBlock*>>& dominatorTree, map<s
RenameFiFunctionArgsVar(successor, stack); RenameFiFunctionArgsVar(successor, stack);
} }
for (auto* child : dominatorTree.at(block)) { for (auto* child : findBlocksWithValue(iDominators, block)) {
RenameIR(child, dominatorTree, counter, stack); cout << "sec" << endl;
RenameIR(child, iDominators, counter, stack);
} }
for (auto& irBlock : block->getInstructions()) { for (auto& irBlock : block->getInstructions()) {
@@ -484,12 +502,12 @@ map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> f
restoreConnections(funcIRConst, funcIR); restoreConnections(funcIRConst, funcIR);
for (auto i : funcIR) { for (auto i : funcIR) {
printBlock(i); //printBlock(i);
} }
auto dominators = findDominators(funcIR); auto dominators = findDominators(funcIR);
cout << endl << endl << endl << "DOMINATORS" << endl << endl << endl; /*cout << endl << endl << endl << "DOMINATORS" << endl << endl << endl;
for (auto i : dominators) { for (auto i : dominators) {
cout << "block - " << i.first->getNumber() << endl; cout << "block - " << i.first->getNumber() << endl;
@@ -498,33 +516,34 @@ map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> f
} }
cout << endl; cout << endl;
} }*/
auto iDominators = findImmediateDominators(dominators, funcIR[0]); auto iDominators = findImmediateDominators(dominators, funcIR[0]);
for (auto i : iDominators) { /*for (auto i : iDominators) {
cout << "block - " << i.first->getNumber() << endl; cout << "block - " << i.first->getNumber() << endl;
cout << "Idominators - " << i.second->getNumber() << endl; cout << "Idominators - " << i.second->getNumber() << endl;
cout << endl; cout << endl;
} }
*/
auto dominatorBorders = findDominatorBorders(funcIR, iDominators); auto dominatorBorders = findDominatorBorders(funcIR, iDominators);
/*for (auto i : dominatorBorders) {
for (auto i : dominatorBorders) {
cout << "block - " << i.first->getNumber() << endl; cout << "block - " << i.first->getNumber() << endl;
for (auto j : i.second) { for (auto j : i.second) {
cout << "border - " << j->getNumber() << endl; cout << "border - " << j->getNumber() << endl;
} }
cout << endl; cout << endl;
} }*/
auto globalsAndVarBlocks = getGlobalsAndVarBlocks(funcIR); auto globalsAndVarBlocks = getGlobalsAndVarBlocks(funcIR);
auto globals = globalsAndVarBlocks.first; auto globals = globalsAndVarBlocks.first;
auto varBlocks = globalsAndVarBlocks.second; auto varBlocks = globalsAndVarBlocks.second;
for (auto i : globals) { /*for (auto i : globals) {
cout << i->getValue() << " " << ToString(i->getType()) << " " << i->getNumber() << endl; cout << i->getValue() << " " << ToString(i->getType()) << " " << i->getNumber() << endl;
} }
@@ -537,6 +556,7 @@ map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> f
cout << endl; cout << endl;
} }
cout << endl; cout << endl;
*/
funcResult = getBlocksWithFiFunctions(funcIR, globals, varBlocks, dominatorBorders); funcResult = getBlocksWithFiFunctions(funcIR, globals, varBlocks, dominatorBorders);
@@ -550,8 +570,7 @@ map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> f
varStack[var->getValue()] = tmp; varStack[var->getValue()] = tmp;
} }
RenameIR(funcIR[0], dominatorBorders, count, varStack); RenameIR(funcIR[0], iDominators, count, varStack);
for (auto i : funcIR) { for (auto i : funcIR) {
printBlock(i); printBlock(i);