diff --git a/src/LoopAnalyzer/implicit_loops_analyzer.cpp b/src/LoopAnalyzer/implicit_loops_analyzer.cpp index c89c658..4bf5b8e 100644 --- a/src/LoopAnalyzer/implicit_loops_analyzer.cpp +++ b/src/LoopAnalyzer/implicit_loops_analyzer.cpp @@ -43,7 +43,8 @@ static void dfs(SAPFOR::BasicBlock* block, map& visit, vectorgetNumber() << endl; cout << "next -"; for (auto i : block->getNext()) @@ -59,17 +60,20 @@ static void printBlock(SAPFOR::BasicBlock* block) { string resValue = ""; string arg1Value = ""; string arg2Value = ""; - if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) { + if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == SAPFOR::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() != NULL && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) { + if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == SAPFOR::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() != NULL && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) { + if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + { arg2Value = i->getInstruction()->getArg2()->getValue(); i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber())); } @@ -105,7 +109,7 @@ static void getLoopBody(SAPFOR::BasicBlock* loopHeader, const setgetNext()) + for (auto& succ : block->getNext()) { if (loopExits.count(succ)) continue; @@ -128,12 +132,12 @@ static void getLoopBody(SAPFOR::BasicBlock* loopHeader, const setgetPrev()) + for (auto& pred : block->getPrev()) if (visited.count(pred) && !backReachable.count(pred)) reverseStack.push(pred); } - for (auto block : visited) + for (auto& block : visited) if (backReachable.count(block)) loopBody.push_back(block); } @@ -171,9 +175,9 @@ static set findRegisterSourceVariables(const std::vectorgetInstructions()) + for (auto& instrWrapper : block->getInstructions()) { auto instr = instrWrapper->getInstruction(); if (!instr || instr->getResult() != variable) @@ -279,9 +283,9 @@ static void findInductiveVars(const std::vector& blocks, set inductiveVars; set relevantBlocks = { loopHeader }; - for (auto block : relevantBlocks) + for (auto& block : relevantBlocks) { - for (auto instrWrapper : block->getInstructions()) + for (auto& instrWrapper : block->getInstructions()) { auto instr = instrWrapper->getInstruction(); if (!instr) @@ -300,7 +304,7 @@ static void findInductiveVars(const std::vector& blocks, if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG) { auto foundVariables = findRegisterSourceVariables(blocks, arg1); - for (auto var : foundVariables) + for (auto& var : foundVariables) inductiveVars.insert(var->getValue()); } } @@ -309,7 +313,7 @@ static void findInductiveVars(const std::vector& blocks, set finalInductiveVars; - for (auto instrWrapper : loopHeader->getInstructions()) + for (auto& instrWrapper : loopHeader->getInstructions()) { auto instr = instrWrapper->getInstruction(); if (!instr || instr->getOperation() != SAPFOR::CFG_OP::F_CALL || !instr->getArg1() || instr->getArg1()->getValue() != "FI_FUNCTION") @@ -347,7 +351,7 @@ static void findInductiveVars(const std::vector& blocks, finalInductiveVars.insert(phiRes->getValue()); } - for (auto i : finalInductiveVars) + for (auto& i : finalInductiveVars) cout << "Confirmed inductive variable: " << i << endl; if (finalInductiveVars.empty()) @@ -358,15 +362,15 @@ static SAPFOR::Instruction* findInstructionAfterLoop(const std::vector loopSet(loopBody.begin(), loopBody.end()); - for (auto block : loopBody) + for (auto& block : loopBody) { - for (auto succ : block->getNext()) + for (auto& succ : block->getNext()) { if (!loopSet.count(succ)) { // Нашли выход из цикла — возьмём первую инструкцию auto instructions = succ->getInstructions(); - for (auto wrapper : instructions) + for (auto& wrapper : instructions) if (auto instr = wrapper->getInstruction()) return instr; } @@ -380,14 +384,14 @@ void findImplicitLoops(const map>& fullIR { for (auto& i : fullIR_SSA) { - //for (auto j : i.second) + //for (auto& j : i.second) // printblock(j); if (fileName != i.first->fileName) continue; map visited; - for (auto i : i.second) + for (auto& i : i.second) visited[i->getNumber()] = UNVISITED; //vector visited(i.second.size(), UNVISITED); @@ -399,11 +403,9 @@ void findImplicitLoops(const map>& fullIR for (auto& [tail, header] : startAndEnd) { set loopExits; - for (auto succ : tail->getNext()) { - if (succ != header) { + for (auto& succ : tail->getNext()) + if (succ != header) loopExits.insert(succ); - } - } vector loopBody; getLoopBody(header, loopExits, loopBody); @@ -412,16 +414,15 @@ void findImplicitLoops(const map>& fullIR cout << " Header: " << header->getNumber() << endl; cout << " Tail: " << tail->getNumber() << endl; cout << " Body blocks: "; - for (auto block : loopBody) { + for (auto& block : loopBody) cout << block->getNumber() << " "; - } cout << endl; findInductiveVars(i.second, loopBody, header, loopExits); - SAPFOR::Instruction* instructionAfterLoop = findInstructionAfterLoop(loopBody); - if (instructionAfterLoop == NULL) { + if (instructionAfterLoop == NULL) + { cout << "Warning: instruction after loop not found!" << endl; continue; } @@ -437,24 +438,25 @@ void findImplicitLoops(const map>& fullIR tmpLoop->lineNum = firstInstruction->getOperator()->lineNumber(); tmpLoop->lineNumAfterLoop = instructionAfterLoop->getOperator()->lineNumber(); - if (firstInstruction->getOperator()->variant() == FOR_NODE) { + if (firstInstruction->getOperator()->variant() == FOR_NODE) + { SgForStmt* stmt = isSgForStmt(firstInstruction->getOperator()); cout << "for loop" << endl;// << stmt->sunparse() << endl; } - else if (firstInstruction->getOperator()->variant() == WHILE_NODE) { + else if (firstInstruction->getOperator()->variant() == WHILE_NODE) + { SgWhileStmt* stmt = isSgWhileStmt(firstInstruction->getOperator()); cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl;//<< stmt->sunparse() << endl; } - else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) { + else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) + { SgWhileStmt* stmt = isSgDoWhileStmt(firstInstruction->getOperator()); cout << "do while loop" << endl;// << stmt->sunparse() << endl; } - else if (firstInstruction->getOperator()->variant() == LOOP_NODE) { + else if (firstInstruction->getOperator()->variant() == LOOP_NODE) cout << "not known loop" << endl;// << firstInstruction->getOperator()->sunparse() << endl; - } - else { + else cout << "goto loop" << endl;// firstInstruction->getOperator()->sunparse() << endl; - } cout << "loop start line " << tmpLoop->lineNum << endl; cout << "after loop line " << tmpLoop->lineNumAfterLoop << endl << endl;