diff --git a/src/CFGraph/IR.cpp b/src/CFGraph/IR.cpp index b4a61fe..76de087 100644 --- a/src/CFGraph/IR.cpp +++ b/src/CFGraph/IR.cpp @@ -1945,11 +1945,18 @@ Instruction* findInstructionAfterLoop(const std::vector& lo return nullptr; // не нашли } -void exploreLoops(map>* fullIR) { +bool isEqual(const char* cstr, const std::string& str) { + return str == cstr; +} + +void exploreLoops(map>* fullIR, const char* fileName) { for (auto& i : *fullIR) { //for (auto j : i.second) - // printBlock(j); + // printblock(j); + + if (!isEqual(fileName, i.first->fileName)) + continue; map visited; for (auto i : i.second) @@ -2005,24 +2012,24 @@ void exploreLoops(map>* fullIR) { if (firstInstruction->getOperator()->variant() == FOR_NODE) { SgForStmt* stmt = isSgForStmt(firstInstruction->getOperator()); - cout << "for loop" << endl << stmt->sunparse() << endl; + cout << "for loop" << endl;// << stmt->sunparse() << endl; } else if (firstInstruction->getOperator()->variant() == WHILE_NODE) { SgWhileStmt* stmt = isSgWhileStmt(firstInstruction->getOperator()); - cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl << stmt->sunparse() << endl; + cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl;//<< stmt->sunparse() << endl; } else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) { SgWhileStmt* stmt = isSgDoWhileStmt(firstInstruction->getOperator()); - cout << "do while loop" << endl << stmt->sunparse() << endl; + cout << "do while loop" << endl;// << stmt->sunparse() << endl; } else if (firstInstruction->getOperator()->variant() == LOOP_NODE) { - cout << "not known loop" << endl << firstInstruction->getOperator()->sunparse() << endl; + cout << "not known loop" << endl;// << firstInstruction->getOperator()->sunparse() << endl; } else { - cout << "goto loop" << firstInstruction->getOperator()->sunparse() << endl; + cout << "goto loop" << endl;// firstInstruction->getOperator()->sunparse() << endl; } cout << "loop start line " << tmpLoop->lineNum << endl; diff --git a/src/CFGraph/IR.h b/src/CFGraph/IR.h index 0449638..4c31182 100644 --- a/src/CFGraph/IR.h +++ b/src/CFGraph/IR.h @@ -352,4 +352,4 @@ std::pair getInstructionAndBlockByNum std::pair getInstructionAndBlockByStatement(const std::map>& CFGraph, SgStatement* stmt); int getParamIndex(SAPFOR::Argument* func_param, int max_index); -void exploreLoops(std::map>* fullIR); \ No newline at end of file +void exploreLoops(std::map>* fullIR, const char* fileName); \ No newline at end of file diff --git a/src/CFGraph/IRSSAForm.cpp b/src/CFGraph/IRSSAForm.cpp index b9fd897..cfa7371 100644 --- a/src/CFGraph/IRSSAForm.cpp +++ b/src/CFGraph/IRSSAForm.cpp @@ -181,6 +181,8 @@ static map> findDominatorBorders(vector& block if (block->getPrev().size() > 1) { for (auto prev : block->getPrev()) { auto tmpBlock = prev; + auto test = iDominators[block]; + auto test2 = iDominators[prev]; while (tmpBlock != iDominators[block]) { result[tmpBlock].push_back(block); @@ -219,6 +221,45 @@ static BBlock* findImmediateDominatorsDfsHelper(BBlock* block, BBlock* currentBl return nullptr; } +static map findImmediateDominators1(const map>& dominators, BBlock* entry) { + map iDom; + + for (const auto& pair : dominators) { + BBlock* b = pair.first; + + if (b == entry) continue; + + const auto& doms = pair.second; + + BBlock* candidate = nullptr; + for (auto d : doms) { + if (d == b) continue; + bool isImmediate = true; + + for (auto other : doms) { + if (other == b || other == d) continue; + const auto& domsOfOther = dominators.at(other); + if (std::find(domsOfOther.begin(), domsOfOther.end(), d) != domsOfOther.end()) { + isImmediate = false; + break; + } + } + + if (isImmediate) { + candidate = d; + break; + } + } + + if (candidate) { + iDom[b] = candidate; + } + } + + return iDom; +} + + static map findImmediateDominators(map>& dominators, BBlock* fistBlock) { map iDominators; @@ -320,7 +361,7 @@ static void getBlocksWithFiFunctions(vector blocks, set glo } } } - + //return blocksWithFiFunctions; } @@ -356,10 +397,14 @@ void restoreConnections(const vector& originalBlocks, vector& BBlock* originalBlock = originalBlocks[i]; BBlock* copiedBlock = copiedBlocks[i]; - for (auto j : copiedBlock->getPrev()) { + auto prevCopy = copiedBlock->getPrev(); + for (auto j : prevCopy) { copiedBlock->removePrev(j); } - for (auto j : copiedBlock->getNext()) { + + // Копируем, затем удаляем next связи + auto nextCopy = copiedBlock->getNext(); + for (auto j : nextCopy) { copiedBlock->removeNext(j); } @@ -488,9 +533,14 @@ void RenameIR(BBlock* block, map& iDominators, map> fullIR, map>* result) { for (auto item : fullIR) { + auto funcinfo = item.first; auto funcIRConst = item.second; + cout << "Testing " << item.first->funcName << endl; + + + vector funcIR; for (auto i : funcIRConst) { @@ -501,7 +551,6 @@ void buildIRSSAForm(map> fullIR, map> fullIR, mapgetNumber() << endl; @@ -564,14 +613,16 @@ void buildIRSSAForm(map> fullIR, mapgetValue()] = 0; stack tmp; + BArgument* tmpArgument = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, "-1"); + tmp.push(tmpArgument); varStack[var->getValue()] = tmp; } RenameIR(funcIR[0], iDominators, count, varStack); - //for (auto i : funcIR) { - // printBlock(i); - //} + for (auto i : funcIR) { + //printBlock(i); + } //cout << endl << endl << endl << endl << endl; //for (auto i : funcIRConst) { // printBlock(i); diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index bd31612..8f390a2 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1020,9 +1020,16 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks); } else if (curr_regime == BUILD_IR_SSA_FORM) - buildIRSSAForm(fullIR, &ssaFormIR); + { + if (ssaFormIR.size() == 0) { + buildIRSSAForm(fullIR, &ssaFormIR); + } + } else if (curr_regime == EXPLORE_IR_LOOPS) - exploreLoops(&ssaFormIR); + { + exploreLoops(&ssaFormIR, file_name); + } + else if (curr_regime == TEST_PASS) { //test pass diff --git a/src/SapforData.h b/src/SapforData.h index 57ac68a..a15fcce 100644 --- a/src/SapforData.h +++ b/src/SapforData.h @@ -183,4 +183,6 @@ std::map> sgExprs; //for EXPLORE_IR_LOOPS and BUILD_IR_SSA_FORM map> ssaFormIR; -// \ No newline at end of file +// + +int test = 0; \ No newline at end of file