From 392ad97738fd05d19a544df08402f935b9ccf092 Mon Sep 17 00:00:00 2001 From: DenisDudarenko Date: Sun, 26 May 2024 12:32:53 +0300 Subject: [PATCH] WIP add analizing IR loop --- .../experts/Sapfor_2017/_src/CFGraph/IR.cpp | 115 ++++++++++++++++++ sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h | 8 +- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 2 + sapfor/experts/Sapfor_2017/_src/Sapfor.h | 2 + .../Sapfor_2017/_src/Utils/PassManager.h | 2 + 5 files changed, 126 insertions(+), 3 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp index 5c8bd0c..4a7ae6b 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp @@ -1613,3 +1613,118 @@ vector buildIR(SgStatement* function, const FuncInfo* func, const vec return blocks; } + +void dfs(SAPFOR::BasicBlock* block, vector& visit, vector>& startAndEnd, SAPFOR::BasicBlock* prev) { + if (visit[block->getNumber()] == 2) { + cout << "error"; + return; + } + + if (visit[block->getNumber()] == 1) { + visit[block->getNumber()] = 2; + startAndEnd.push_back(make_pair(prev, block)); + return; + } + + visit[block->getNumber()] = 1; + for (auto i : block->getNext()) { + dfs(i, visit, startAndEnd, block); + } +} + +void printBlock(SAPFOR::BasicBlock* block) { + cout << "block - " << block->getNumber() << endl; + cout << "next -"; + for (auto i : block->getNext()) + { + cout << " " << i->getNumber(); + } + cout << endl << "prev -"; + for (auto i : block->getPrev()) + { + cout << " " << i->getNumber(); + } + cout << endl; + + for (auto i : block->getInstructions()) + { + cout << i->getNumber() << " " << i->getInstruction()->dump() << endl; + } + + cout << endl; +} + +void testIR(map> fullIR) { + for (auto& i : fullIR) + { + for (auto j : i.second) + printBlock(j); + + vector visited(i.second.size(), 0); + vector> startAndEnd; + dfs(i.second[0], visited, startAndEnd, NULL); + + + vector loops; + for (auto j : startAndEnd) + { + auto instruction = j.second->getInstructions()[0]->getInstruction(); + if (instruction->getOperator()->variant() == FOR_NODE) { + SgForStmt* stmt = isSgForStmt(instruction->getOperator()); + + auto tmpLoop = new LoopGraph(); + tmpLoop->isFor = true; + tmpLoop->lineNum = instruction->getOperator()->lineNumber(); + + cout << "for" << endl << stmt->sunparse() << endl; + cout << "loop start line " << tmpLoop->lineNum << endl << endl; + + loops.push_back(tmpLoop); + } else if (instruction->getOperator()->variant() == WHILE_NODE) { + SgWhileStmt* stmt = isSgWhileStmt(instruction->getOperator()); + + auto tmpLoop = new LoopGraph(); + tmpLoop->lineNum = instruction->getOperator()->lineNumber(); + + if (stmt->conditional() == NULL) + { + //infinit loop + cout << "infinit loop " << endl << stmt->sunparse() << endl; + } + else + { + //while + cout << "while " << endl << stmt->sunparse(); + } + + cout << "loop start line " << tmpLoop->lineNum << endl << endl; + + loops.push_back(tmpLoop); + } else if (instruction->getOperator()->variant() == LOOP_NODE) { + cout << "not known loop" << endl << instruction->getOperator()->sunparse() << endl; + } + else { + cout << "goto loop - " << instruction->getOperator()->sunparse() << endl; + } + } + + + /*for (auto j : i.second) { + cout << j->getNumber() << endl << "in" << endl; + for (auto k : j->getRD_In()) { + cout << k.first->getMemTypeStr() << " - "; + for (auto h : k.second) { + cout << h << " "; + } + cout << endl; + } + cout << "out" << endl; + for (auto k : j->getRD_Out()) { + cout << k.first->getMemTypeStr() << " - "; + for (auto h : k.second) { + cout << h << " "; + } + cout << endl; + }*/ + } +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h index 505a428..48900e3 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h @@ -130,7 +130,7 @@ namespace SAPFOR { if (arg == NULL) return ""; - return arg->getValue(); + return arg->getValue(); } public: @@ -169,7 +169,7 @@ namespace SAPFOR { std::string res = ""; - std::string resultVal = getArgValue(result); + std::string resultVal = getArgValue(result); std::string arg1Val = getArgValue(arg1); std::string arg2Val = getArgValue(arg2); @@ -312,4 +312,6 @@ std::vector buildIR(SgStatement* function, const FuncInfo* fu SAPFOR::Instruction* getInstructionByNumber(const std::vector& blocks, int num); std::pair getInstructionAndBlockByNumber(const std::map>& CFGraph, int num); std::pair getInstructionAndBlockByStatement(const std::map>& CFGraph, SgStatement* stmt); -int getParamIndex(SAPFOR::Argument* func_param, int max_index); \ No newline at end of file +int getParamIndex(SAPFOR::Argument* func_param, int max_index); + +void testIR(std::map> fullIR); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 7759c53..680a44b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1036,6 +1036,8 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne if(func->funcPointer->variant() != ENTRY_STAT) countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks); } + else if (curr_regime == EXPLORE_IR_LOOPS) + testIR(fullIR); else if (curr_regime == TEST_PASS) { //test pass diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index 1337941..f56d124 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -178,6 +178,7 @@ enum passes { INSERT_NO_DISTR_FLAGS_FROM_GUI, SET_IMPLICIT_NONE, + EXPLORE_IR_LOOPS, TEST_PASS, EMPTY_PASS @@ -360,6 +361,7 @@ static void setPassValues() passNames[CONVERT_TO_C] = "CONVERT_TO_C"; passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE"; passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI"; + passNames[EXPLORE_IR_LOOPS] = "EXPLORE_IR_LOOPS"; passNames[TEST_PASS] = "TEST_PASS"; } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 50f571b..75f1787 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -312,6 +312,8 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(CORRECT_VAR_DECL) <= Pass(SET_IMPLICIT_NONE); + list({ CALL_GRAPH, LOOP_GRAPH, CALL_GRAPH2 }) <= Pass(EXPLORE_IR_LOOPS); + passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS, EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW, REVERSE_CREATED_NESTED_LOOPS, PREDICT_SCHEME, CALCULATE_STATS_SCHEME, REVERT_SPF_DIRS, CLEAR_SPF_DIRS, TRANSFORM_SHADOW_IF_FULL,