From 4eb057731f6419da9cec067573e92989437a7b7f Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Wed, 5 Mar 2025 16:34:32 +0300 Subject: [PATCH 01/12] trivial case for local variables only --- src/ProjectParameters/projectParameters.cpp | 203 ++++++++++++++++-- src/ProjectParameters/projectParameters.h | 23 +- src/Sapfor.cpp | 7 +- src/SapforData.h | 2 +- .../parameter/dynamic_array_maximum.f90 | 52 +++++ 5 files changed, 271 insertions(+), 16 deletions(-) create mode 100644 tests/sapfor/parameter/dynamic_array_maximum.f90 diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index fd395dc..7ac33d0 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -1,8 +1,6 @@ -#include "../Utils/leak_detector.h" - +#include #include #include -#include #include #include #include @@ -11,23 +9,202 @@ #include #include +#include "../Utils/SgUtils.h" +#include "../CFGraph/CFGraph.h" +#include "CFGraph/IR.h" +#include "Distribution/Array.h" #include "dvm.h" #include "../Utils/errors.h" -#include "../Utils/SgUtils.h" #include "../GraphCall/graph_calls.h" #include "../GraphCall/graph_calls_func.h" +#include "libSage++.h" #include "projectParameters.h" using namespace std; -map< pair, set> - findParameters(const map> &defUseByFunctions, - const map &commonBlocks, - const map> &allFuncInfo) +template +static void processArgument(set& worklist, SAPFOR::Argument* arg, Iterator instr, Iterator first_instr) { + if (arg == NULL) + return; + if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) + extract_vars_from_reg(worklist, arg, instr, first_instr); + else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::LOCAL_) + { + std::cout << "worklist add: " << arg->getValue() << std::endl; //DEBUG PRINT + worklist.insert(arg); + } +} + +template +void extract_vars_from_reg(set& worklist, SAPFOR::Argument* reg, Iterator instr, Iterator first_instr) { + for (; instr >= first_instr; instr--) { + if ((*instr)->getInstruction()->getResult() == reg) + { + processArgument(worklist, (*instr)->getInstruction()->getArg1(), instr, first_instr); + processArgument(worklist, (*instr)->getInstruction()->getArg2(), instr, first_instr); + return; + } + } +} + +static void lookup_for_vars(ResultSet& result_set, + set& worklist, + SAPFOR::Instruction* instr, + SAPFOR::BasicBlock* bblock, + const std::map>& fullIR) { - map< pair, set> foundParameters; - - - return foundParameters; -} \ No newline at end of file + std::cout << "Lookup in bblock no." << bblock->getNumber() << std::endl; //DEBUG PRINT + + auto first_instr = bblock->getInstructions().begin(); + auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { + return i->getInstruction() == instr; + }); + + for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--) + { + auto instr = (*cur_instr)->getInstruction(); + auto result_arg = instr->getResult(); + auto arg1 = instr->getArg1(); + auto arg2 = instr->getArg2(); + + if (worklist.count(result_arg)) + { + processArgument(worklist, arg1, cur_instr, first_instr); + processArgument(worklist, arg2, cur_instr, first_instr); + std::cout << "worklist erase: " << result_arg->getValue() << std::endl; //DEBUG PRINT + worklist.erase(result_arg); + } + if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) + { + // skip to F_CALL + auto f_call_instr = cur_instr; + while ((*f_call_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL) + f_call_instr++; + + if ((*f_call_instr)->getInstruction()->getArg1()->getValue() == "_READ") + { + auto filename = (*f_call_instr)->getInstruction()->getOperator()->fileName(); + auto line = (*f_call_instr)->getInstruction()->getOperator()->lineNumber(); + __spf_print(1,"Please specify value of variable %s on line %d of file %s", arg1->getValue().c_str(), line, filename); + result_set.insert(make_tuple(filename, line, arg1->getValue())); + + std::cout << "worklist erase: " << arg1->getValue() << std::endl; //DEBUG PRINT + worklist.erase(arg1); + } else + { + //check if variable is modified in called function + } + } + } + + + const auto& RD = bblock->getRD_In(); + map group_by_block; + for (auto& arg : worklist) + { + if (RD.count(arg)) + { + if (RD.at(arg).size() == 0) + __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); + else if (RD.at(arg).size() > 1) + __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible", arg->getValue().c_str()); + else + { + for (const auto& instr_num : RD.at(arg)) + { + auto [instr, bblock] = getInstructionAndBlockByNumber(fullIR, instr_num); + if (group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num) + group_by_block[bblock] = instr; + } + } + } + } + + for (const auto& [bblock, instr] : group_by_block) + { + lookup_for_vars(result_set, worklist, instr, bblock, fullIR); + } +} + +static void handle_single_allocate(ResultSet& result_set, + SAPFOR::Instruction* instr, + SAPFOR::BasicBlock* bblock, + const std::map>& fullIR) +{ + auto first_instr = bblock->getInstructions().begin(); + auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { + return i->getInstruction() == instr; + }); + auto alloc_instr = cur_instr; + + // skip to F_CALL _ALLOC n + while ((*alloc_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL || + (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") + { + alloc_instr++; + } + + auto arrays_num = stoi((*alloc_instr)->getInstruction()->getArg2()->getValue()); + std::cout << "arrays_num: " << arrays_num << std::endl; //DEBUG PRINT + + set worklist; + for (int i = 0; i < arrays_num; i++) + { + auto param_instr = --alloc_instr; + auto param_reg = (*param_instr)->getInstruction()->getArg1(); + + while ((*param_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::LOAD || + (*param_instr)->getInstruction()->getResult() != param_reg) + { + param_instr--; + } + + auto dimensions_num = stoi((*param_instr)->getInstruction()->getArg2()->getValue()); + + for (int j = 0; j < dimensions_num; j++) + { + auto ref_instr = --param_instr; + + auto arg = (*ref_instr)->getInstruction()->getArg1(); + if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) + { + extract_vars_from_reg(worklist, arg, ref_instr, first_instr); + } + else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::LOCAL_) + { + std::cout << "worklist add: " << arg->getValue() << std::endl; //DEBUG PRINT + worklist.insert(arg); + } + } + } + lookup_for_vars(result_set,worklist, instr, bblock, fullIR); +} + +ResultSet +findParameters(const std::map>& fullIR, + const std::map, std::pair>& declaredArrays) +{ + ResultSet foundParameters; + std::set alloc_statements; + for (const auto& elem : declaredArrays) + { + const auto& array = elem.second.first; + assert(array->GetLocation().first == Distribution::arrayLocation::l_LOCAL); // v0.1 + + SgSymbol* arraySymb = array->GetDeclSymbol()->GetOriginal(); + SgStatement* decl = declaratedInStmt(arraySymb); + for (auto &stmt : getAttributes(decl, set{ ALLOCATE_STMT })) + { + alloc_statements.insert(stmt); + } + } + + for (const auto& alloc_statement : alloc_statements) + { + auto [instr, bblock] = getInstructionAndBlockByStatement(fullIR, alloc_statement); + ResultSet result_set; + handle_single_allocate(result_set, instr, bblock, fullIR); + } + return foundParameters; +} diff --git a/src/ProjectParameters/projectParameters.h b/src/ProjectParameters/projectParameters.h index 5a240b0..25e32e4 100644 --- a/src/ProjectParameters/projectParameters.h +++ b/src/ProjectParameters/projectParameters.h @@ -1,3 +1,24 @@ #pragma once -std::map< std::pair, std::set> findParameters(const std::map> &defUseByFunctions, const std::map &commonBlocks, const std::map> &allFuncInfo); \ No newline at end of file +#include +#include +#include + +using ResultSet = std::set>; + +template +void extract_vars_from_reg(std::set& worklist, SAPFOR::Argument* reg, Iterator instr, Iterator first_instr); + + +template +static void processArgument(std::set& worklist, SAPFOR::Argument* arg, Iterator instr, Iterator first_instr); + +static void lookup_for_vars(ResultSet& result_set, + std::set& worklist, + SAPFOR::Instruction* instr, + SAPFOR::BasicBlock* bblock, + const std::map>& fullIR); + +ResultSet +findParameters(const std::map>& fullIR, + const std::map, std::pair>& declaredArrays); diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 5f5f93d..cbdaa32 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1894,7 +1894,11 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne else if (curr_regime == RENAME_SYMBOLS) runRenameSymbols(&project, commonBlocks); else if (curr_regime == FIND_PARAMETERS) - parametersOfProject = findParameters(defUseByFunctions, commonBlocks, allFuncInfo); + { + performRDSubst(fullIR, commonBlocks, &project); + parametersOfProject = findParameters(fullIR, declaredArrays); + performRDSubst(fullIR, commonBlocks, &project); + } else if (curr_regime == BUILD_IR) { auto CFG_forFile = buildCFG(commonBlocks, allFuncInfo_IR, SAPFOR::CFG_Settings(0)); @@ -2373,6 +2377,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case FIX_COMMON_BLOCKS: case TEST_PASS: case SET_IMPLICIT_NONE: + case FIND_PARAMETERS: runAnalysis(*project, curr_regime, false); case SUBST_EXPR_RD_AND_UNPARSE: case SUBST_EXPR_AND_UNPARSE: diff --git a/src/SapforData.h b/src/SapforData.h index cfd4f42..54ebc85 100644 --- a/src/SapforData.h +++ b/src/SapforData.h @@ -168,7 +168,7 @@ std::map filesInfo; // information about open,close,write and re // //for FIND_PARAMETERS -std::map< std::pair, std::set> parametersOfProject; // [file, line] -> set[vars] +std::set> parametersOfProject; // [file, line, varname] // //for GET_MIN_MAX_BLOCK_DIST diff --git a/tests/sapfor/parameter/dynamic_array_maximum.f90 b/tests/sapfor/parameter/dynamic_array_maximum.f90 new file mode 100644 index 0000000..f49e5e3 --- /dev/null +++ b/tests/sapfor/parameter/dynamic_array_maximum.f90 @@ -0,0 +1,52 @@ +program dynamic_array_maximum_3d + implicit none + integer :: n1, n2, n3, n4 , k, i, j, l, a + integer :: sum3 + real :: max_element + real, allocatable :: array(:,:,:), array2(:,:,:), array3(:,:,:) + + write(*, *) "Enter 3 integers" + read(*, *) n, m, k + m = 100 + + if (1 .eq. 1) then + a = 3 + else if (2 .eq. 1) then + a = 4 + endif + + m = m + 1 + k = m * 1000 + n * 10 + + allocate(array(n, m + n, k + m + n), & + &array2(k, m + n, k), & + &array3(k, m, k + n)) + + call random_seed() + do i = 1, n1 + do j = 1, m * n1 + do l = 1, k * m * n1 + call random_number(array(i,j,l)) + array(i,j,l) = int(array(i,j,l) * 100) + end do + end do + end do + + max_element = array(1,1,1) + do i = 1, n1 + do j = 1, m + do l = 1, k + max_element = MAX(array(i,j,l), max_element) + end do + end do + end do + deallocate(array, array2, array3) + write(*, *) max_element +end program dynamic_array_maximum_3d + +! function sum3(x, y, z) +! implicit none +! integer :: x, y, z +! integer :: sum3 +! sum3 = x + y + z +! end function sum3 From 537d60222f1b8ddee0761c551b5a8226cd0f6694 Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Tue, 15 Apr 2025 12:23:32 +0300 Subject: [PATCH 02/12] Add Dominator tree builder and interprocedural analysis --- src/CFGraph/CFGraph.h | 8 +- src/ProjectParameters/domTree.h | 111 +++++++++++ src/ProjectParameters/projectParameters.cpp | 203 +++++++++++++++----- src/ProjectParameters/projectParameters.h | 18 +- src/Sapfor.cpp | 4 +- 5 files changed, 289 insertions(+), 55 deletions(-) create mode 100644 src/ProjectParameters/domTree.h diff --git a/src/CFGraph/CFGraph.h b/src/CFGraph/CFGraph.h index 319b420..cdb190b 100644 --- a/src/CFGraph/CFGraph.h +++ b/src/CFGraph/CFGraph.h @@ -24,7 +24,7 @@ namespace SAPFOR std::vector next; std::vector prev; - + BasicBlock* idom{}; //reaching definition std::map> RD_in, RD_out; @@ -42,6 +42,7 @@ namespace SAPFOR void addInstruction(IR_Block* item); void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } void addNext(BasicBlock* next_) { next.push_back(next_); } + void setIdom(BasicBlock* idom_) { idom = idom_; } int removePrev(BasicBlock* removed); int removeNext(BasicBlock* removed); @@ -69,7 +70,8 @@ namespace SAPFOR const std::vector& getInstructions() const { return instructions; } const std::vector& getNext() const { return next; } const std::vector& getPrev() const { return prev; } - + BasicBlock* getIdom() const { return idom; } + /* * FOR LIVE ANALYSIS */ @@ -146,4 +148,4 @@ static inline void deleteCFG(std::map + +using namespace std; + +namespace SAPFOR { +class DominatorFinder { +private: + BasicBlock* entry; + std::vector vertices; + std::unordered_map dfs_num; + std::vector parent, semi, vertex, ancestor, label; + std::vector> bucket; + int n; + + void DFS(BasicBlock* v, int parent_num) { + dfs_num[v] = n; + vertex[n] = n; + semi[n] = n; + label[n] = n; + ancestor[n] = -1; + parent[n] = parent_num; + vertices[n++] = v; + + for (const auto& w : v->getNext()) { + if (dfs_num[w] == -1) { + DFS(w, dfs_num[v]); + } + } + } + + void Compress(int v) { + if (ancestor[ancestor[v]] != -1) { + Compress(ancestor[v]); + if (semi[label[ancestor[v]]] < semi[label[v]]) + label[v] = label[ancestor[v]]; + ancestor[v] = ancestor[ancestor[v]]; + } + } + + int Eval(int v) { + if (ancestor[v] == -1) return v; + Compress(v); + return label[v]; + } + + void Link(int v, int w) { + ancestor[w] = v; + } + +public: + DominatorFinder(std::vector& blocks) { + if (blocks.empty()) return; + entry = blocks[0]; + n = 0; + + for (auto block : blocks) dfs_num[block] = -1; + + int max_size = blocks.size(); + vertices.resize(max_size); + parent.assign(max_size, -1); + semi.assign(max_size, -1); + vertex.assign(max_size, -1); + ancestor.assign(max_size, -1); + label.assign(max_size, -1); + bucket.resize(max_size); + + DFS(entry, -1); + + for (int i = n - 1; i > 0; --i) { + int w = vertex[i]; + + for (BasicBlock* v : vertices[w]->getPrev()) { + int u = Eval(dfs_num[v]); + if (semi[u] < semi[w]) + semi[w] = semi[u]; + } + bucket[vertex[semi[w]]].push_back(w); + Link(parent[w], w); + + for (int v : bucket[parent[w]]) + { + int u = Eval(v); + if (semi[u] < semi[v]) + vertices[v]->setIdom(vertices[u]); + else + vertices[v]->setIdom(vertices[parent[w]]); + } + bucket[parent[w]].clear(); + } + + for (int i = 1; i < n; ++i) { + int w = vertex[i]; + if (vertices[w]->getIdom() != vertices[vertex[semi[w]]]) + vertices[w]->setIdom(vertices[w]->getIdom()->getIdom()); + } + + entry->setIdom(nullptr); + } +}; + +void buildDominatorTreeLT(std::vector& blocks) { + DominatorFinder finder(blocks); +} + +} diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index 7ac33d0..4a5f5c6 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -1,10 +1,10 @@ -#include #include #include #include #include #include #include +#include #include #include #include @@ -20,14 +20,43 @@ #include "libSage++.h" #include "projectParameters.h" +#include "domTree.h" using namespace std; +tuple stmtToIR(const map>& CFGraph, SgStatement* stmt) +{ + SgStatement* cur = stmt; + while (cur->variant() != PROC_HEDR && cur->variant() != PROG_HEDR && cur->variant() != FUNC_HEDR) + cur = cur->controlParent(); + + string funcName = ((SgProcHedrStmt*)cur)->nameWithContains(); + + int stmtID = stmt->id(); + for (const auto& [func, bblocks] : CFGraph) + { + if (func->funcName != funcName) + continue; + + for (auto basicBlock : bblocks) + for (auto ins : basicBlock->getInstructions()) + if (stmtID == ins->getInstruction()->getOperator()->id()) + return make_tuple(func, ins->getInstruction(), basicBlock); + } + + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + return { NULL, NULL, NULL }; +} + template -static void processArgument(set& worklist, SAPFOR::Argument* arg, Iterator instr, Iterator first_instr) { +static void processArgument(set& worklist, + SAPFOR::Argument* arg, + Iterator instr, + Iterator first_instr) +{ if (arg == NULL) return; - if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) + if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) extract_vars_from_reg(worklist, arg, instr, first_instr); else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::LOCAL_) { @@ -37,9 +66,14 @@ static void processArgument(set& worklist, SAPFOR::Argument* } template -void extract_vars_from_reg(set& worklist, SAPFOR::Argument* reg, Iterator instr, Iterator first_instr) { - for (; instr >= first_instr; instr--) { - if ((*instr)->getInstruction()->getResult() == reg) +static void extract_vars_from_reg(set& worklist, + SAPFOR::Argument* reg, + Iterator instr, + Iterator first_instr) +{ + for (; instr >= first_instr; instr--) + { + if ((*instr)->getInstruction()->getResult() == reg) { processArgument(worklist, (*instr)->getInstruction()->getArg1(), instr, first_instr); processArgument(worklist, (*instr)->getInstruction()->getArg2(), instr, first_instr); @@ -48,14 +82,14 @@ void extract_vars_from_reg(set& worklist, SAPFOR::Argument* r } } -static void lookup_for_vars(ResultSet& result_set, +static void lookup_for_vars(std::map& where_to_add, set& worklist, SAPFOR::Instruction* instr, SAPFOR::BasicBlock* bblock, const std::map>& fullIR) { std::cout << "Lookup in bblock no." << bblock->getNumber() << std::endl; //DEBUG PRINT - + auto first_instr = bblock->getInstructions().begin(); auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { return i->getInstruction() == instr; @@ -63,7 +97,7 @@ static void lookup_for_vars(ResultSet& result_set, for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--) { - auto instr = (*cur_instr)->getInstruction(); + auto instr = (*cur_instr)->getInstruction(); auto result_arg = instr->getResult(); auto arg1 = instr->getArg1(); auto arg2 = instr->getArg2(); @@ -75,37 +109,37 @@ static void lookup_for_vars(ResultSet& result_set, std::cout << "worklist erase: " << result_arg->getValue() << std::endl; //DEBUG PRINT worklist.erase(result_arg); } - if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) + if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) { // skip to F_CALL auto f_call_instr = cur_instr; while ((*f_call_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL) f_call_instr++; - if ((*f_call_instr)->getInstruction()->getArg1()->getValue() == "_READ") + if ((*f_call_instr)->getInstruction()->getArg1()->getValue() == "_READ") { - auto filename = (*f_call_instr)->getInstruction()->getOperator()->fileName(); - auto line = (*f_call_instr)->getInstruction()->getOperator()->lineNumber(); + auto stmt_before = (*f_call_instr)->getInstruction()->getOperator(); + auto filename = stmt_before->fileName(); + auto line = stmt_before->lineNumber(); + auto var_name = arg1->getValue().substr(arg1->getValue().find('%') + 1); __spf_print(1,"Please specify value of variable %s on line %d of file %s", arg1->getValue().c_str(), line, filename); - result_set.insert(make_tuple(filename, line, arg1->getValue())); - + where_to_add[stmt_before] = var_name; std::cout << "worklist erase: " << arg1->getValue() << std::endl; //DEBUG PRINT worklist.erase(arg1); - } else + } else { //check if variable is modified in called function } } } - const auto& RD = bblock->getRD_In(); map group_by_block; - for (auto& arg : worklist) + for (auto& arg : worklist) { if (RD.count(arg)) { - if (RD.at(arg).size() == 0) + if (RD.at(arg).size() == 1 && *RD.at(arg).begin() == SAPFOR::CFG_VAL::UNINIT) __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); else if (RD.at(arg).size() > 1) __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible", arg->getValue().c_str()); @@ -114,24 +148,31 @@ static void lookup_for_vars(ResultSet& result_set, for (const auto& instr_num : RD.at(arg)) { auto [instr, bblock] = getInstructionAndBlockByNumber(fullIR, instr_num); - if (group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num) + if (group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num) group_by_block[bblock] = instr; } } } } - for (const auto& [bblock, instr] : group_by_block) + auto idom = bblock->getIdom(); + while (idom != NULL) { - lookup_for_vars(result_set, worklist, instr, bblock, fullIR); + if (group_by_block.count(idom)) + { + lookup_for_vars(where_to_add, worklist, group_by_block[idom], idom, fullIR); + return; + } + idom = idom->getIdom(); } } -static void handle_single_allocate(ResultSet& result_set, - SAPFOR::Instruction* instr, - SAPFOR::BasicBlock* bblock, +static void handle_single_allocate(std::map& where_to_add, + SgStatement* alloc_statement, const std::map>& fullIR) -{ +{ + auto [func, instr, bblock] = stmtToIR(fullIR, alloc_statement); + auto first_instr = bblock->getInstructions().begin(); auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { return i->getInstruction() == instr; @@ -140,7 +181,7 @@ static void handle_single_allocate(ResultSet& result_set, // skip to F_CALL _ALLOC n while ((*alloc_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL || - (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") + (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") { alloc_instr++; } @@ -161,11 +202,11 @@ static void handle_single_allocate(ResultSet& result_set, } auto dimensions_num = stoi((*param_instr)->getInstruction()->getArg2()->getValue()); - + for (int j = 0; j < dimensions_num; j++) { auto ref_instr = --param_instr; - + auto arg = (*ref_instr)->getInstruction()->getArg1(); if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) { @@ -178,33 +219,107 @@ static void handle_single_allocate(ResultSet& result_set, } } } - lookup_for_vars(result_set,worklist, instr, bblock, fullIR); + lookup_for_vars(where_to_add,worklist, instr, bblock, fullIR); } -ResultSet -findParameters(const std::map>& fullIR, +static void handle_single_loop(std::map& where_to_add, + SgStatement* loop_stmt, + const std::map>& fullIR) +{ + auto [func_name, instr, bblock] = stmtToIR(fullIR, loop_stmt); + std::cout << "bblock: " << bblock->getNumber() << " instr: " << instr->getNumber() << std::endl; + + auto cur_instr = bblock->getInstructions().end() - 1; + + set worklist; + extract_vars_from_reg(worklist, (*cur_instr)->getInstruction()->getResult(), cur_instr, bblock->getInstructions().begin()); + + lookup_for_vars(where_to_add, worklist, (*cur_instr)->getInstruction(), bblock, fullIR); +} + +void +findParameters(ResultSet& foundParameters, + std::map>& fullIR, const std::map, std::pair>& declaredArrays) { - ResultSet foundParameters; + map where_to_add; + + map name_to_func; + for (const auto& [func, _] : fullIR) + name_to_func[func->funcName] = func; + + map> call_sites; + for (auto& [func, bblocks] : fullIR) + { + for (const auto& block : bblocks) + { + for (const auto& ir_block : block->getInstructions()) + { + auto instr = ir_block->getInstruction(); + if (instr->getOperation() == SAPFOR::CFG_OP::F_CALL) + { + auto func_name = instr->getArg1()->getValue(); + auto func_info = name_to_func.find(func_name); + + if (func_info != name_to_func.end()) + call_sites[func_info->second].push_back(instr); + } + } + } + + SAPFOR::buildDominatorTreeLT(bblocks); + for (auto block : bblocks) + { + if (block->getIdom() != NULL) + std::cout << "BB: " << block->getNumber() << " IDOM: " << block->getIdom()->getNumber() << std::endl; + } + std::cout << "+++++++++++\n"; + } + std::set alloc_statements; - for (const auto& elem : declaredArrays) + for (const auto& elem : declaredArrays) { const auto& array = elem.second.first; - assert(array->GetLocation().first == Distribution::arrayLocation::l_LOCAL); // v0.1 - SgSymbol* arraySymb = array->GetDeclSymbol()->GetOriginal(); SgStatement* decl = declaratedInStmt(arraySymb); - for (auto &stmt : getAttributes(decl, set{ ALLOCATE_STMT })) - { + + for (auto& stmt : getAttributes(decl, set{ ALLOCATE_STMT })) alloc_statements.insert(stmt); - } } - + for (const auto& alloc_statement : alloc_statements) { - auto [instr, bblock] = getInstructionAndBlockByStatement(fullIR, alloc_statement); - ResultSet result_set; - handle_single_allocate(result_set, instr, bblock, fullIR); + handle_single_allocate(where_to_add, alloc_statement, fullIR); + } + + set for_statements; + // Find all FOR statements in the program + for (const auto& [func, bblocks] : fullIR) + for (const auto& block : bblocks) + for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr) + { + auto op = (*instr)->getInstruction()->getOperator(); + if (op && op->variant() == FOR_NODE) { + std::cout << block->getNumber() << std::endl; + for_statements.insert(op); + } + } + + for (const auto& stmt : for_statements) + { + handle_single_loop(where_to_add, stmt, fullIR); + } + + for (const auto& [stmt_before, var_name] : where_to_add) + { + // SgVariableSymb* var_symb = new SgVariableSymb(var_name.c_str()); + // SgVarRefExp* var = new SgVarRefExp(var_symb); + // SgValueExp* zero = new SgValueExp(0); + // SgExprListExp* ex = new SgExprListExp(); + // ex->setLhs(new SgExpression(ASSGN_OP, var, zero)); + // SgStatement* toAdd = new SgStatement(SPF_PARAMETER_OP, NULL, NULL, ex, NULL, NULL); + // stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); + + foundParameters.insert(make_tuple(stmt_before->fileName(), stmt_before->lineNumber(), var_name)); } - return foundParameters; } diff --git a/src/ProjectParameters/projectParameters.h b/src/ProjectParameters/projectParameters.h index 25e32e4..36547b7 100644 --- a/src/ProjectParameters/projectParameters.h +++ b/src/ProjectParameters/projectParameters.h @@ -1,5 +1,6 @@ #pragma once +#include "libSage++.h" #include #include #include @@ -7,18 +8,25 @@ using ResultSet = std::set>; template -void extract_vars_from_reg(std::set& worklist, SAPFOR::Argument* reg, Iterator instr, Iterator first_instr); +static void extract_vars_from_reg(std::set& worklist, + SAPFOR::Argument* reg, + Iterator instr, + Iterator first_instr); template -static void processArgument(std::set& worklist, SAPFOR::Argument* arg, Iterator instr, Iterator first_instr); +static void processArgument(std::set& worklist, + SAPFOR::Argument* arg, + Iterator instr, + Iterator first_instr); -static void lookup_for_vars(ResultSet& result_set, +static void lookup_for_vars(std::map& where_to_add, std::set& worklist, SAPFOR::Instruction* instr, SAPFOR::BasicBlock* bblock, const std::map>& fullIR); -ResultSet -findParameters(const std::map>& fullIR, +void +findParameters(ResultSet& foundParameters, + std::map>& fullIR, const std::map, std::pair>& declaredArrays); diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index cbdaa32..c5a4cf2 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1895,9 +1895,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne runRenameSymbols(&project, commonBlocks); else if (curr_regime == FIND_PARAMETERS) { - performRDSubst(fullIR, commonBlocks, &project); - parametersOfProject = findParameters(fullIR, declaredArrays); - performRDSubst(fullIR, commonBlocks, &project); + findParameters(parametersOfProject, fullIR, declaredArrays); } else if (curr_regime == BUILD_IR) { From f9d52c0c3ee343147c3f452fea9f929f0c690e95 Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Tue, 15 Apr 2025 18:45:04 +0300 Subject: [PATCH 03/12] unparse after --- src/Sapfor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index c5a4cf2..bb4e093 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -2340,6 +2340,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case INSERT_NO_DISTR_FLAGS_FROM_GUI: case PRIVATE_REMOVING: case RENAME_INLCUDES: + case FIND_PARAMETERS: runAnalysis(*project, curr_regime, true, "", folderName); break; case INLINE_PROCEDURES: @@ -2375,7 +2376,6 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case FIX_COMMON_BLOCKS: case TEST_PASS: case SET_IMPLICIT_NONE: - case FIND_PARAMETERS: runAnalysis(*project, curr_regime, false); case SUBST_EXPR_RD_AND_UNPARSE: case SUBST_EXPR_AND_UNPARSE: From 8b2e59356aa24d6f4003a09b736ddebaf4201252 Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Thu, 1 May 2025 11:43:35 +0300 Subject: [PATCH 04/12] Extend interprocedural analysis --- src/ProjectParameters/projectParameters.cpp | 339 +++++++++++++------- src/ProjectParameters/projectParameters.h | 36 ++- 2 files changed, 249 insertions(+), 126 deletions(-) diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index 4a5f5c6..9919a51 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -27,6 +27,7 @@ using namespace std; tuple stmtToIR(const map>& CFGraph, SgStatement* stmt) { SgStatement* cur = stmt; + cur->switchToFile(); while (cur->variant() != PROC_HEDR && cur->variant() != PROG_HEDR && cur->variant() != FUNC_HEDR) cur = cur->controlParent(); @@ -48,17 +49,31 @@ tuple stmtToIR(const map IRByNumber(const map>& CFGraph, int num) +{ + if (num < 0) + return { NULL, NULL, NULL }; + + for (const auto& [func, bblocks] : CFGraph) + for (auto byBB : bblocks) + if (byBB->getInstructions().front()->getNumber() <= num && byBB->getInstructions().back()->getNumber() >= num) + return make_tuple(func, getInstructionByNumber(byBB->getInstructions(), num), byBB); + + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + return { NULL, NULL, NULL}; +} + template -static void processArgument(set& worklist, - SAPFOR::Argument* arg, - Iterator instr, - Iterator first_instr) +void processArgument(set& worklist, + SAPFOR::Argument* arg, + Iterator instr, + Iterator first_instr) { if (arg == NULL) return; if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) extract_vars_from_reg(worklist, arg, instr, first_instr); - else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::LOCAL_) + else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR) { std::cout << "worklist add: " << arg->getValue() << std::endl; //DEBUG PRINT worklist.insert(arg); @@ -66,10 +81,10 @@ static void processArgument(set& worklist, } template -static void extract_vars_from_reg(set& worklist, - SAPFOR::Argument* reg, - Iterator instr, - Iterator first_instr) +void extract_vars_from_reg(set& worklist, + SAPFOR::Argument* reg, + Iterator instr, + Iterator first_instr) { for (; instr >= first_instr; instr--) { @@ -82,94 +97,180 @@ static void extract_vars_from_reg(set& worklist, } } -static void lookup_for_vars(std::map& where_to_add, - set& worklist, - SAPFOR::Instruction* instr, - SAPFOR::BasicBlock* bblock, - const std::map>& fullIR) +void lookup_for_vars(std::set>& where_to_add, + set& worklist, + SAPFOR::Instruction* instr, + SAPFOR::BasicBlock* bblock, + FuncInfo* cur_func, + const std::map>& fullIR) { - std::cout << "Lookup in bblock no." << bblock->getNumber() << std::endl; //DEBUG PRINT - - auto first_instr = bblock->getInstructions().begin(); - auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { - return i->getInstruction() == instr; - }); - - for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--) + while (bblock) { - auto instr = (*cur_instr)->getInstruction(); - auto result_arg = instr->getResult(); - auto arg1 = instr->getArg1(); - auto arg2 = instr->getArg2(); + std::cout << "Lookup in bblock no." << bblock->getNumber() << std::endl; //DEBUG PRINT - if (worklist.count(result_arg)) - { - processArgument(worklist, arg1, cur_instr, first_instr); - processArgument(worklist, arg2, cur_instr, first_instr); - std::cout << "worklist erase: " << result_arg->getValue() << std::endl; //DEBUG PRINT - worklist.erase(result_arg); - } - if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) - { - // skip to F_CALL - auto f_call_instr = cur_instr; - while ((*f_call_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL) - f_call_instr++; + auto first_instr = bblock->getInstructions().begin(); + auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { + return i->getInstruction() == instr; + }); - if ((*f_call_instr)->getInstruction()->getArg1()->getValue() == "_READ") + for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--) + { + auto instr = (*cur_instr)->getInstruction(); + // std::cout << instr->getNumber() << '\n'; + auto result_arg = instr->getResult(); + auto arg1 = instr->getArg1(); + auto arg2 = instr->getArg2(); + + if (worklist.count(result_arg)) { - auto stmt_before = (*f_call_instr)->getInstruction()->getOperator(); - auto filename = stmt_before->fileName(); - auto line = stmt_before->lineNumber(); - auto var_name = arg1->getValue().substr(arg1->getValue().find('%') + 1); - __spf_print(1,"Please specify value of variable %s on line %d of file %s", arg1->getValue().c_str(), line, filename); - where_to_add[stmt_before] = var_name; - std::cout << "worklist erase: " << arg1->getValue() << std::endl; //DEBUG PRINT - worklist.erase(arg1); - } else + processArgument(worklist, arg1, cur_instr, first_instr); + processArgument(worklist, arg2, cur_instr, first_instr); + std::cout << "worklist erase: " << result_arg->getValue() << std::endl; //DEBUG PRINT + worklist.erase(result_arg); + } + if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) { - //check if variable is modified in called function + // skip to F_CALL + auto f_call_instr = cur_instr; + while ((*f_call_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL) + f_call_instr++; + + if ((*f_call_instr)->getInstruction()->getArg1()->getValue() == "_READ") + { + auto stmt_before = (*f_call_instr)->getInstruction()->getOperator(); + auto filename = stmt_before->fileName(); + auto line = stmt_before->lineNumber(); + auto var_name = arg1->getValue().substr(arg1->getValue().find('%') + 1); + __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg1->getValue().c_str(), line, filename); + auto toAdd = make_tuple(stmt_before, var_name, MODE::AFTER); + where_to_add.insert(toAdd); + std::cout << "worklist erase: " << arg1->getValue() << std::endl; //DEBUG PRINT + worklist.erase(arg1); + } } } - } - const auto& RD = bblock->getRD_In(); - map group_by_block; - for (auto& arg : worklist) - { - if (RD.count(arg)) + const auto& RD = bblock->getRD_In(); + map group_by_block; + for (auto& arg : worklist) { - if (RD.at(arg).size() == 1 && *RD.at(arg).begin() == SAPFOR::CFG_VAL::UNINIT) - __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); - else if (RD.at(arg).size() > 1) - __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible", arg->getValue().c_str()); - else + if (RD.count(arg)) { - for (const auto& instr_num : RD.at(arg)) + if (RD.at(arg).size() == 1 && *RD.at(arg).begin() == SAPFOR::CFG_VAL::UNINIT) + __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); + else if (RD.at(arg).size() > 1) { - auto [instr, bblock] = getInstructionAndBlockByNumber(fullIR, instr_num); - if (group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num) + auto stmt_after = (*first_instr)->getInstruction()->getOperator(); + auto filename = stmt_after->fileName(); + auto line = stmt_after->lineNumber(); + auto var_name = arg->getValue().substr(arg->getValue().find('%') + 1); + __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible\n", arg->getValue().c_str()); + __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); + auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE); + where_to_add.insert(toAdd); + std::cout << "worklist erase: " << arg->getValue() << std::endl; //DEBUG PRINT + worklist.erase(arg); + } + else + { + auto instr_num = *RD.at(arg).begin(); + auto [func, instr, bblock] = IRByNumber(fullIR, instr_num); + if (cur_func == func && group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num) group_by_block[bblock] = instr; } } } + + while (bblock && group_by_block.find(bblock) == group_by_block.end()) + bblock = bblock->getIdom(); + if (bblock) + instr = group_by_block[bblock]; } - auto idom = bblock->getIdom(); - while (idom != NULL) + // other variables are from global scope + const auto& RD = fullIR.at(cur_func).front()->getRD_In(); + for (auto& arg : worklist) { - if (group_by_block.count(idom)) + if (arg->isMemGlobal()) { - lookup_for_vars(where_to_add, worklist, group_by_block[idom], idom, fullIR); - return; + std::cout << "global: " << arg->getValue() << " : " << RD.count(arg) << '\n'; + set found_rd; + if (RD.count(arg)) + found_rd = RD.at(arg); + if (found_rd.size() == 0) + { + auto call_instr = call_sites[cur_func].size() ? call_sites[cur_func].front() : NULL; + while (call_instr && found_rd.size() == 0) + { + auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber()); + std::cout << "caller: " << call_func->funcName << '\n'; + if (call_bblock->getRD_Out().count(arg)) + { + std::cout << *call_bblock->getRD_Out().at(arg).begin() << "\n"; + found_rd = call_bblock->getRD_Out().at(arg); + } + + call_instr = call_sites[call_func].size() ? call_sites[call_func].front() : NULL; + } + } + if (found_rd.size() == 1 && *found_rd.begin() == SAPFOR::CFG_VAL::UNINIT) + { + __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); + } else if (found_rd.size() > 1) + { + auto first_instr = fullIR.at(cur_func).front()->getInstructions().begin(); + auto stmt_after = (*first_instr)->getInstruction()->getOperator(); + auto filename = stmt_after->fileName(); + auto line = stmt_after->lineNumber(); + auto var_name = arg->getValue().substr(arg->getValue().find('%') + 1); + __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible\n", arg->getValue().c_str()); + __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); + auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE); + where_to_add.insert(toAdd); + } + else + { + auto instr_num = *found_rd.begin(); + auto [func, instr, bblock] = IRByNumber(fullIR, instr_num); + set new_worklist = {arg}; + + lookup_for_vars(where_to_add, new_worklist, instr, bblock, func, fullIR); + } } - idom = idom->getIdom(); } + + + for (const auto& call_instr : call_sites[cur_func]) + { + set new_worklist; + auto params_num = cur_func->funcParams.countOfPars; + + auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber()); + std::cout << "caller: " << call_func->funcName << '\n'; + auto first_instr = call_bblock->getInstructions().begin(); + auto cur_instr = std::find_if(first_instr, call_bblock->getInstructions().end(), [call_instr](SAPFOR::IR_Block* i) { + return i->getInstruction() == call_instr; + }); + for (auto& arg : worklist) + { + if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_) + { + auto param_num= stoi(arg->getValue().substr(arg->getValue().find('%', arg->getValue().find('%') + 1) + 1)); + // std::cout << params_num << '\n'; + auto param_instr = (cur_instr - (params_num - param_num)); + auto param_arg = (*param_instr)->getInstruction()->getArg1(); + std::cout << "param_val: " << param_arg->getValue() << " : " << param_num << '\n'; + processArgument(new_worklist, param_arg, param_instr, first_instr); + } + } + lookup_for_vars(where_to_add, new_worklist, call_instr, call_bblock, call_func, fullIR); + } + } -static void handle_single_allocate(std::map& where_to_add, - SgStatement* alloc_statement, - const std::map>& fullIR) +void handle_single_allocate(std::set>& where_to_add, + SgStatement* alloc_statement, + const std::map>& fullIR) { auto [func, instr, bblock] = stmtToIR(fullIR, alloc_statement); @@ -182,9 +283,8 @@ static void handle_single_allocate(std::map& where_to // skip to F_CALL _ALLOC n while ((*alloc_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL || (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") - { alloc_instr++; - } + auto arrays_num = stoi((*alloc_instr)->getInstruction()->getArg2()->getValue()); std::cout << "arrays_num: " << arrays_num << std::endl; //DEBUG PRINT @@ -197,36 +297,41 @@ static void handle_single_allocate(std::map& where_to while ((*param_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::LOAD || (*param_instr)->getInstruction()->getResult() != param_reg) - { param_instr--; - } + auto dimensions_num = stoi((*param_instr)->getInstruction()->getArg2()->getValue()); for (int j = 0; j < dimensions_num; j++) { auto ref_instr = --param_instr; - - auto arg = (*ref_instr)->getInstruction()->getArg1(); - if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) + if ((*ref_instr)->getInstruction()->getOperation() == SAPFOR::CFG_OP::RANGE) { - extract_vars_from_reg(worklist, arg, ref_instr, first_instr); - } - else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::LOCAL_) + vector range_args = {(*ref_instr)->getInstruction()->getArg1(), + (*ref_instr)->getInstruction()->getArg2(), + (*ref_instr)->getInstruction()->getResult()}; + for (auto& arg : range_args) + { + if (arg) + std::cout << "range-arg: " << arg->getValue() << '\n'; + processArgument(worklist, arg, ref_instr, first_instr); + } + } else { - std::cout << "worklist add: " << arg->getValue() << std::endl; //DEBUG PRINT - worklist.insert(arg); + auto arg = (*ref_instr)->getInstruction()->getArg1(); + processArgument(worklist, arg, ref_instr, first_instr); } } } - lookup_for_vars(where_to_add,worklist, instr, bblock, fullIR); + lookup_for_vars(where_to_add,worklist, instr, bblock, func, fullIR); } -static void handle_single_loop(std::map& where_to_add, - SgStatement* loop_stmt, - const std::map>& fullIR) + +void handle_single_loop(std::set>& where_to_add, + SgStatement* loop_stmt, + const std::map>& fullIR) { - auto [func_name, instr, bblock] = stmtToIR(fullIR, loop_stmt); + auto [func, instr, bblock] = stmtToIR(fullIR, loop_stmt); std::cout << "bblock: " << bblock->getNumber() << " instr: " << instr->getNumber() << std::endl; auto cur_instr = bblock->getInstructions().end() - 1; @@ -234,7 +339,7 @@ static void handle_single_loop(std::map& where_to_add set worklist; extract_vars_from_reg(worklist, (*cur_instr)->getInstruction()->getResult(), cur_instr, bblock->getInstructions().begin()); - lookup_for_vars(where_to_add, worklist, (*cur_instr)->getInstruction(), bblock, fullIR); + lookup_for_vars(where_to_add, worklist, (*cur_instr)->getInstruction(), bblock, func, fullIR); } void @@ -242,13 +347,12 @@ findParameters(ResultSet& foundParameters, std::map>& fullIR, const std::map, std::pair>& declaredArrays) { - map where_to_add; + set> where_to_add; map name_to_func; for (const auto& [func, _] : fullIR) name_to_func[func->funcName] = func; - map> call_sites; for (auto& [func, bblocks] : fullIR) { for (const auto& block : bblocks) @@ -277,18 +381,19 @@ findParameters(ResultSet& foundParameters, } std::set alloc_statements; - for (const auto& elem : declaredArrays) - { - const auto& array = elem.second.first; - SgSymbol* arraySymb = array->GetDeclSymbol()->GetOriginal(); - SgStatement* decl = declaratedInStmt(arraySymb); - - for (auto& stmt : getAttributes(decl, set{ ALLOCATE_STMT })) - alloc_statements.insert(stmt); - } + for (const auto& [func, bblocks] : fullIR) + for (const auto& block : bblocks) + for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr) + { + auto op = (*instr)->getInstruction()->getOperator(); + if (op && op->variant() == ALLOCATE_STMT) { + alloc_statements.insert(op); + } + } for (const auto& alloc_statement : alloc_statements) { + alloc_statement->unparsestdout(); handle_single_allocate(where_to_add, alloc_statement, fullIR); } @@ -299,26 +404,34 @@ findParameters(ResultSet& foundParameters, for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr) { auto op = (*instr)->getInstruction()->getOperator(); - if (op && op->variant() == FOR_NODE) { - std::cout << block->getNumber() << std::endl; + if (op && op->variant() == FOR_NODE) for_statements.insert(op); - } } for (const auto& stmt : for_statements) { + std::cout << string(stmt->fileName()) << ":" << stmt->lineNumber() << '\n'; handle_single_loop(where_to_add, stmt, fullIR); } - for (const auto& [stmt_before, var_name] : where_to_add) + for (const auto& [stmt_before, var_name, mode] : where_to_add) { - // SgVariableSymb* var_symb = new SgVariableSymb(var_name.c_str()); - // SgVarRefExp* var = new SgVarRefExp(var_symb); - // SgValueExp* zero = new SgValueExp(0); - // SgExprListExp* ex = new SgExprListExp(); - // ex->setLhs(new SgExpression(ASSGN_OP, var, zero)); - // SgStatement* toAdd = new SgStatement(SPF_PARAMETER_OP, NULL, NULL, ex, NULL, NULL); - // stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); + stmt_before->switchToFile(); + SgVariableSymb* var_symb = new SgVariableSymb(var_name.c_str()); + SgVarRefExp* var = new SgVarRefExp(var_symb); + SgValueExp* zero = new SgValueExp(1337); + SgExprListExp* ex = new SgExprListExp(); + ex->setLhs(new SgExpression(ASSGN_OP, var, zero)); + std::cout << "stmt_before: " << stmt_before->unparse(); + SgStatement* toAdd = new SgStatement(ASSIGN_STAT, NULL, NULL, var, zero, NULL); + toAdd->unparsestdout(); + // toAdd->lineNumber() = stmt_before->lineNumber(); + toAdd->setlineNumber(stmt_before->lineNumber()); + toAdd->setLocalLineNumber(stmt_before->lineNumber()); + toAdd->setFileId(stmt_before->getFileId()); + toAdd->setProject(stmt_before->getProject()); + stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); + stmt_before->controlParent()->unparsestdout(); foundParameters.insert(make_tuple(stmt_before->fileName(), stmt_before->lineNumber(), var_name)); } diff --git a/src/ProjectParameters/projectParameters.h b/src/ProjectParameters/projectParameters.h index 36547b7..1181560 100644 --- a/src/ProjectParameters/projectParameters.h +++ b/src/ProjectParameters/projectParameters.h @@ -4,27 +4,37 @@ #include #include #include +#include using ResultSet = std::set>; +enum class MODE +{ + BEFORE, + AFTER +}; + +static std::map> call_sites; + template -static void extract_vars_from_reg(std::set& worklist, - SAPFOR::Argument* reg, - Iterator instr, - Iterator first_instr); +void extract_vars_from_reg(std::set& worklist, + SAPFOR::Argument* reg, + Iterator instr, + Iterator first_instr); template -static void processArgument(std::set& worklist, - SAPFOR::Argument* arg, - Iterator instr, - Iterator first_instr); +void processArgument(std::set& worklist, + SAPFOR::Argument* arg, + Iterator instr, + Iterator first_instr); -static void lookup_for_vars(std::map& where_to_add, - std::set& worklist, - SAPFOR::Instruction* instr, - SAPFOR::BasicBlock* bblock, - const std::map>& fullIR); +void lookup_for_vars(std::set>& where_to_add, + std::set& worklist, + SAPFOR::Instruction* instr, + SAPFOR::BasicBlock* bblock, + FuncInfo* cur_func, + const std::map>& fullIR); void findParameters(ResultSet& foundParameters, From 6742932862a27227dd8257c5cf9cdaa2d8447b6b Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Thu, 1 May 2025 18:43:51 +0300 Subject: [PATCH 05/12] Add SPF directive insertion --- src/ProjectParameters/projectParameters.cpp | 15 ++++++++++++--- src/Sapfor.cpp | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index 9919a51..cad6738 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -175,7 +175,7 @@ void lookup_for_vars(std::set>& wher { auto instr_num = *RD.at(arg).begin(); auto [func, instr, bblock] = IRByNumber(fullIR, instr_num); - if (cur_func == func && group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num) + if (cur_func == func && (group_by_block[bblock] == NULL || group_by_block[bblock]->getNumber() < instr_num)) group_by_block[bblock] = instr; } } @@ -421,17 +421,26 @@ findParameters(ResultSet& foundParameters, SgVarRefExp* var = new SgVarRefExp(var_symb); SgValueExp* zero = new SgValueExp(1337); SgExprListExp* ex = new SgExprListExp(); + auto op = new SgExpression(ASSGN_OP, var, zero); + + std::cout << "len: " << ex->length() << '\n'; ex->setLhs(new SgExpression(ASSGN_OP, var, zero)); std::cout << "stmt_before: " << stmt_before->unparse(); - SgStatement* toAdd = new SgStatement(ASSIGN_STAT, NULL, NULL, var, zero, NULL); + SgExpression* parameter_op = new SgExpression(SPF_PARAMETER_OP, ex); + auto ex2 = new SgExprListExp(); + ex2->setLhs(parameter_op); + SgStatement* toAdd = new SgStatement(SPF_ANALYSIS_DIR, NULL, NULL, ex2, NULL, NULL); toAdd->unparsestdout(); // toAdd->lineNumber() = stmt_before->lineNumber(); toAdd->setlineNumber(stmt_before->lineNumber()); toAdd->setLocalLineNumber(stmt_before->lineNumber()); toAdd->setFileId(stmt_before->getFileId()); toAdd->setProject(stmt_before->getProject()); - stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); stmt_before->controlParent()->unparsestdout(); + if (mode == MODE::AFTER) + stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); + else + stmt_before->insertStmtBefore(*toAdd, *stmt_before->controlParent()); foundParameters.insert(make_tuple(stmt_before->fileName(), stmt_before->lineNumber(), var_name)); } diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index bb4e093..2bee6a5 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -2340,7 +2340,6 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case INSERT_NO_DISTR_FLAGS_FROM_GUI: case PRIVATE_REMOVING: case RENAME_INLCUDES: - case FIND_PARAMETERS: runAnalysis(*project, curr_regime, true, "", folderName); break; case INLINE_PROCEDURES: @@ -2375,6 +2374,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case LOOPS_COMBINER: case FIX_COMMON_BLOCKS: case TEST_PASS: + case FIND_PARAMETERS: case SET_IMPLICIT_NONE: runAnalysis(*project, curr_regime, false); case SUBST_EXPR_RD_AND_UNPARSE: From 4e16638c363ee33d7119ec0e785d48aec2d75d62 Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Thu, 1 May 2025 18:52:29 +0300 Subject: [PATCH 06/12] Clean up --- src/ProjectParameters/projectParameters.cpp | 92 +++++++-------------- 1 file changed, 29 insertions(+), 63 deletions(-) diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index cad6738..4bf0fec 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -53,12 +53,12 @@ tuple IRByNumber(const map { if (num < 0) return { NULL, NULL, NULL }; - + for (const auto& [func, bblocks] : CFGraph) for (auto byBB : bblocks) if (byBB->getInstructions().front()->getNumber() <= num && byBB->getInstructions().back()->getNumber() >= num) return make_tuple(func, getInstructionByNumber(byBB->getInstructions(), num), byBB); - + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); return { NULL, NULL, NULL}; } @@ -75,7 +75,6 @@ void processArgument(set& worklist, extract_vars_from_reg(worklist, arg, instr, first_instr); else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR) { - std::cout << "worklist add: " << arg->getValue() << std::endl; //DEBUG PRINT worklist.insert(arg); } } @@ -106,8 +105,6 @@ void lookup_for_vars(std::set>& wher { while (bblock) { - std::cout << "Lookup in bblock no." << bblock->getNumber() << std::endl; //DEBUG PRINT - auto first_instr = bblock->getInstructions().begin(); auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { return i->getInstruction() == instr; @@ -116,7 +113,6 @@ void lookup_for_vars(std::set>& wher for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--) { auto instr = (*cur_instr)->getInstruction(); - // std::cout << instr->getNumber() << '\n'; auto result_arg = instr->getResult(); auto arg1 = instr->getArg1(); auto arg2 = instr->getArg2(); @@ -125,7 +121,6 @@ void lookup_for_vars(std::set>& wher { processArgument(worklist, arg1, cur_instr, first_instr); processArgument(worklist, arg2, cur_instr, first_instr); - std::cout << "worklist erase: " << result_arg->getValue() << std::endl; //DEBUG PRINT worklist.erase(result_arg); } if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) @@ -144,7 +139,6 @@ void lookup_for_vars(std::set>& wher __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg1->getValue().c_str(), line, filename); auto toAdd = make_tuple(stmt_before, var_name, MODE::AFTER); where_to_add.insert(toAdd); - std::cout << "worklist erase: " << arg1->getValue() << std::endl; //DEBUG PRINT worklist.erase(arg1); } } @@ -157,7 +151,7 @@ void lookup_for_vars(std::set>& wher if (RD.count(arg)) { if (RD.at(arg).size() == 1 && *RD.at(arg).begin() == SAPFOR::CFG_VAL::UNINIT) - __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); + __spf_print(1, "variable %s has no definition\n", arg->getValue().c_str()); else if (RD.at(arg).size() > 1) { auto stmt_after = (*first_instr)->getInstruction()->getOperator(); @@ -168,7 +162,6 @@ void lookup_for_vars(std::set>& wher __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE); where_to_add.insert(toAdd); - std::cout << "worklist erase: " << arg->getValue() << std::endl; //DEBUG PRINT worklist.erase(arg); } else @@ -191,9 +184,8 @@ void lookup_for_vars(std::set>& wher const auto& RD = fullIR.at(cur_func).front()->getRD_In(); for (auto& arg : worklist) { - if (arg->isMemGlobal()) + if (arg->isMemGlobal()) { - std::cout << "global: " << arg->getValue() << " : " << RD.count(arg) << '\n'; set found_rd; if (RD.count(arg)) found_rd = RD.at(arg); @@ -203,19 +195,16 @@ void lookup_for_vars(std::set>& wher while (call_instr && found_rd.size() == 0) { auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber()); - std::cout << "caller: " << call_func->funcName << '\n'; - if (call_bblock->getRD_Out().count(arg)) - { - std::cout << *call_bblock->getRD_Out().at(arg).begin() << "\n"; + if (call_bblock->getRD_Out().count(arg)) found_rd = call_bblock->getRD_Out().at(arg); - } - + + call_instr = call_sites[call_func].size() ? call_sites[call_func].front() : NULL; } } if (found_rd.size() == 1 && *found_rd.begin() == SAPFOR::CFG_VAL::UNINIT) { - __spf_print(1, "variable %s has no definition", arg->getValue().c_str()); + __spf_print(1, "variable %s has no definition\n", arg->getValue().c_str()); } else if (found_rd.size() > 1) { auto first_instr = fullIR.at(cur_func).front()->getInstructions().begin(); @@ -233,8 +222,8 @@ void lookup_for_vars(std::set>& wher auto instr_num = *found_rd.begin(); auto [func, instr, bblock] = IRByNumber(fullIR, instr_num); set new_worklist = {arg}; - - lookup_for_vars(where_to_add, new_worklist, instr, bblock, func, fullIR); + + lookup_for_vars(where_to_add, new_worklist, instr, bblock, func, fullIR); } } } @@ -246,7 +235,6 @@ void lookup_for_vars(std::set>& wher auto params_num = cur_func->funcParams.countOfPars; auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber()); - std::cout << "caller: " << call_func->funcName << '\n'; auto first_instr = call_bblock->getInstructions().begin(); auto cur_instr = std::find_if(first_instr, call_bblock->getInstructions().end(), [call_instr](SAPFOR::IR_Block* i) { return i->getInstruction() == call_instr; @@ -256,10 +244,8 @@ void lookup_for_vars(std::set>& wher if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_) { auto param_num= stoi(arg->getValue().substr(arg->getValue().find('%', arg->getValue().find('%') + 1) + 1)); - // std::cout << params_num << '\n'; auto param_instr = (cur_instr - (params_num - param_num)); auto param_arg = (*param_instr)->getInstruction()->getArg1(); - std::cout << "param_val: " << param_arg->getValue() << " : " << param_num << '\n'; processArgument(new_worklist, param_arg, param_instr, first_instr); } } @@ -284,10 +270,9 @@ void handle_single_allocate(std::set>& wh while ((*alloc_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL || (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") alloc_instr++; - + auto arrays_num = stoi((*alloc_instr)->getInstruction()->getArg2()->getValue()); - std::cout << "arrays_num: " << arrays_num << std::endl; //DEBUG PRINT set worklist; for (int i = 0; i < arrays_num; i++) @@ -298,7 +283,7 @@ void handle_single_allocate(std::set>& wh while ((*param_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::LOAD || (*param_instr)->getInstruction()->getResult() != param_reg) param_instr--; - + auto dimensions_num = stoi((*param_instr)->getInstruction()->getArg2()->getValue()); @@ -310,16 +295,13 @@ void handle_single_allocate(std::set>& wh vector range_args = {(*ref_instr)->getInstruction()->getArg1(), (*ref_instr)->getInstruction()->getArg2(), (*ref_instr)->getInstruction()->getResult()}; - for (auto& arg : range_args) - { - if (arg) - std::cout << "range-arg: " << arg->getValue() << '\n'; + for (auto& arg : range_args) processArgument(worklist, arg, ref_instr, first_instr); - } + } else { auto arg = (*ref_instr)->getInstruction()->getArg1(); - processArgument(worklist, arg, ref_instr, first_instr); + processArgument(worklist, arg, ref_instr, first_instr); } } } @@ -332,7 +314,6 @@ void handle_single_loop(std::set>& where_ const std::map>& fullIR) { auto [func, instr, bblock] = stmtToIR(fullIR, loop_stmt); - std::cout << "bblock: " << bblock->getNumber() << " instr: " << instr->getNumber() << std::endl; auto cur_instr = bblock->getInstructions().end() - 1; @@ -356,7 +337,6 @@ findParameters(ResultSet& foundParameters, for (auto& [func, bblocks] : fullIR) { for (const auto& block : bblocks) - { for (const auto& ir_block : block->getInstructions()) { auto instr = ir_block->getInstruction(); @@ -369,15 +349,9 @@ findParameters(ResultSet& foundParameters, call_sites[func_info->second].push_back(instr); } } - } + SAPFOR::buildDominatorTreeLT(bblocks); - for (auto block : bblocks) - { - if (block->getIdom() != NULL) - std::cout << "BB: " << block->getNumber() << " IDOM: " << block->getIdom()->getNumber() << std::endl; - } - std::cout << "+++++++++++\n"; } std::set alloc_statements; @@ -391,12 +365,6 @@ findParameters(ResultSet& foundParameters, } } - for (const auto& alloc_statement : alloc_statements) - { - alloc_statement->unparsestdout(); - handle_single_allocate(where_to_add, alloc_statement, fullIR); - } - set for_statements; // Find all FOR statements in the program for (const auto& [func, bblocks] : fullIR) @@ -404,39 +372,37 @@ findParameters(ResultSet& foundParameters, for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr) { auto op = (*instr)->getInstruction()->getOperator(); - if (op && op->variant() == FOR_NODE) + if (op && op->variant() == FOR_NODE) for_statements.insert(op); } + for (const auto& alloc_statement : alloc_statements) + handle_single_allocate(where_to_add, alloc_statement, fullIR); + for (const auto& stmt : for_statements) - { - std::cout << string(stmt->fileName()) << ":" << stmt->lineNumber() << '\n'; handle_single_loop(where_to_add, stmt, fullIR); - } for (const auto& [stmt_before, var_name, mode] : where_to_add) { stmt_before->switchToFile(); + SgVariableSymb* var_symb = new SgVariableSymb(var_name.c_str()); SgVarRefExp* var = new SgVarRefExp(var_symb); SgValueExp* zero = new SgValueExp(1337); SgExprListExp* ex = new SgExprListExp(); - auto op = new SgExpression(ASSGN_OP, var, zero); - - std::cout << "len: " << ex->length() << '\n'; - ex->setLhs(new SgExpression(ASSGN_OP, var, zero)); - std::cout << "stmt_before: " << stmt_before->unparse(); + auto assgn_op = new SgExpression(ASSGN_OP, var, zero); + ex->setLhs(assgn_op); + SgExpression* parameter_op = new SgExpression(SPF_PARAMETER_OP, ex); - auto ex2 = new SgExprListExp(); - ex2->setLhs(parameter_op); - SgStatement* toAdd = new SgStatement(SPF_ANALYSIS_DIR, NULL, NULL, ex2, NULL, NULL); - toAdd->unparsestdout(); - // toAdd->lineNumber() = stmt_before->lineNumber(); + auto dir_list = new SgExprListExp(); + dir_list->setLhs(parameter_op); + SgStatement* toAdd = new SgStatement(SPF_ANALYSIS_DIR, NULL, NULL, dir_list, NULL, NULL); + toAdd->setlineNumber(stmt_before->lineNumber()); toAdd->setLocalLineNumber(stmt_before->lineNumber()); toAdd->setFileId(stmt_before->getFileId()); toAdd->setProject(stmt_before->getProject()); - stmt_before->controlParent()->unparsestdout(); + if (mode == MODE::AFTER) stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); else From d3e8c481d2da802fd587c40d06557b28525a19b3 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 11:34:32 +0300 Subject: [PATCH 07/12] fixed code style, moved dom tree building to IR --- CMakeLists.txt | 2 + src/CFGraph/CFGraph.cpp | 10 ++ src/CFGraph/CFGraph.h | 23 +++- src/CFGraph/IR.h | 1 + src/CFGraph/IR_domTree.h | 35 +++++ src/ProjectParameters/domTree.h | 111 --------------- src/ProjectParameters/projectParameters.cpp | 142 +++++++++++--------- src/ProjectParameters/projectParameters.h | 38 +----- src/Sapfor.cpp | 2 - src/Utils/CommonBlock.h | 2 + src/Utils/version.h | 2 +- 11 files changed, 155 insertions(+), 213 deletions(-) create mode 100644 src/CFGraph/IR_domTree.h delete mode 100644 src/ProjectParameters/domTree.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 87eb86c..1ac1c0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,6 +205,8 @@ set(TRANSFORMS set(CFG src/CFGraph/IR.cpp src/CFGraph/IR.h + src/CFGraph/IR_domTree.cpp + src/CFGraph/IR_domTree.h src/CFGraph/CFGraph.cpp src/CFGraph/CFGraph.h src/CFGraph/RD_subst.cpp diff --git a/src/CFGraph/CFGraph.cpp b/src/CFGraph/CFGraph.cpp index 4c414d4..283e0a4 100644 --- a/src/CFGraph/CFGraph.cpp +++ b/src/CFGraph/CFGraph.cpp @@ -1149,6 +1149,16 @@ map> buildCFG(const map& common if (settings.withRD) buildReachingDefs(result, settings); + if (settings.withDominators) + { + auto t = high_resolution_clock::now(); + for (auto& [func, bblocks] : result) + SAPFOR::buildDominatorTree(bblocks); + + auto msec = duration_cast(high_resolution_clock::now() - t).count(); + __spf_print(1, "dominator build time is %.3f sec\n", msec / 1000.); + } + if (SgFile::switchToFile(oldFile) == -1) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); diff --git a/src/CFGraph/CFGraph.h b/src/CFGraph/CFGraph.h index cdb190b..f0308fd 100644 --- a/src/CFGraph/CFGraph.h +++ b/src/CFGraph/CFGraph.h @@ -6,6 +6,7 @@ #include #include "IR.h" +#include "IR_domTree.h" namespace SAPFOR { @@ -24,7 +25,7 @@ namespace SAPFOR std::vector next; std::vector prev; - BasicBlock* idom{}; + BasicBlock* directDominator = NULL; //reaching definition std::map> RD_in, RD_out; @@ -34,6 +35,7 @@ namespace SAPFOR bool addLive(const std::map>& to_add, bool in); std::map> getLive(bool in) const; bool removeLive(SAPFOR::Argument* to_remove, bool in); + public: BasicBlock() { num = lastNumBlock++; } BasicBlock(IR_Block* item); @@ -42,7 +44,7 @@ namespace SAPFOR void addInstruction(IR_Block* item); void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } void addNext(BasicBlock* next_) { next.push_back(next_); } - void setIdom(BasicBlock* idom_) { idom = idom_; } + void setDom(BasicBlock* dom) { directDominator = dom; } int removePrev(BasicBlock* removed); int removeNext(BasicBlock* removed); @@ -70,7 +72,16 @@ namespace SAPFOR const std::vector& getInstructions() const { return instructions; } const std::vector& getNext() const { return next; } const std::vector& getPrev() const { return prev; } - BasicBlock* getIdom() const { return idom; } + BasicBlock* getDom() const + { + if (!directDominator) + { + __spf_print(1, "%s\n", "the dominator tree was built with an error or was not built at all"); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + + return directDominator; + } /* * FOR LIVE ANALYSIS @@ -107,13 +118,15 @@ namespace SAPFOR bool withDVM = false; bool withCallsInBlocks = false; // separate each F_CALL to own BasicBlock bool withCallFrom = true; + bool withDominators = true; explicit CFG_Settings(int) { } explicit CFG_Settings(bool atLeastOneIterInLoop = false, bool withRD = true, bool withRegisters = false, - bool withDVM = false, bool withSPF = false, bool withCallsInBlocks = false, bool withCallFrom = true) : + bool withDVM = false, bool withSPF = false, bool withCallsInBlocks = false, + bool withCallFrom = true, bool withDominators = true) : atLeastOneIterInLoop(atLeastOneIterInLoop), withRD(withRD), withRegisters(withRegisters), withDVM(withDVM), withSPF(withSPF), - withCallsInBlocks(withCallsInBlocks), withCallFrom(withCallFrom) + withCallsInBlocks(withCallsInBlocks), withCallFrom(withCallFrom), withDominators(withDominators) { } }; } diff --git a/src/CFGraph/IR.h b/src/CFGraph/IR.h index a887a39..d61deed 100644 --- a/src/CFGraph/IR.h +++ b/src/CFGraph/IR.h @@ -7,6 +7,7 @@ #include "CFGraph.h" #include "../Utils/CommonBlock.h" +#include "../GraphCall/graph_calls.h" namespace SAPFOR { diff --git a/src/CFGraph/IR_domTree.h b/src/CFGraph/IR_domTree.h new file mode 100644 index 0000000..78a2c8b --- /dev/null +++ b/src/CFGraph/IR_domTree.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +#include "CFGraph.h" + +// Lengauer, Thomas. A fast algorithm for finding dominators in a flowgraph / Thomas Lengauer, Robert Endre Tarjan +// ACM Transactions on Programming Languages and Systems (TOPLAS). — 1979. — Vol. 1, no. 1. — Pp. 121–141. + +namespace SAPFOR { + +class BasicBlock; + +class DominatorFinder { +private: + BasicBlock* entry; + std::vector vertices; + std::unordered_map dfs_num; + std::vector parent, semi, vertex, ancestor, label; + std::vector> bucket; + int n; + + void DFS(BasicBlock* v, int parent_num); + void Compress(int v); + int Eval(int v); + void Link(int v, int w); + +public: + DominatorFinder(std::vector& blocks); +}; + +void buildDominatorTree(std::vector& blocks); +} diff --git a/src/ProjectParameters/domTree.h b/src/ProjectParameters/domTree.h deleted file mode 100644 index 2fe268f..0000000 --- a/src/ProjectParameters/domTree.h +++ /dev/null @@ -1,111 +0,0 @@ -#pragma once - -#include "vector" -#include "map" - -#include "../CFGraph/CFGraph.h" -#include - -using namespace std; - -namespace SAPFOR { -class DominatorFinder { -private: - BasicBlock* entry; - std::vector vertices; - std::unordered_map dfs_num; - std::vector parent, semi, vertex, ancestor, label; - std::vector> bucket; - int n; - - void DFS(BasicBlock* v, int parent_num) { - dfs_num[v] = n; - vertex[n] = n; - semi[n] = n; - label[n] = n; - ancestor[n] = -1; - parent[n] = parent_num; - vertices[n++] = v; - - for (const auto& w : v->getNext()) { - if (dfs_num[w] == -1) { - DFS(w, dfs_num[v]); - } - } - } - - void Compress(int v) { - if (ancestor[ancestor[v]] != -1) { - Compress(ancestor[v]); - if (semi[label[ancestor[v]]] < semi[label[v]]) - label[v] = label[ancestor[v]]; - ancestor[v] = ancestor[ancestor[v]]; - } - } - - int Eval(int v) { - if (ancestor[v] == -1) return v; - Compress(v); - return label[v]; - } - - void Link(int v, int w) { - ancestor[w] = v; - } - -public: - DominatorFinder(std::vector& blocks) { - if (blocks.empty()) return; - entry = blocks[0]; - n = 0; - - for (auto block : blocks) dfs_num[block] = -1; - - int max_size = blocks.size(); - vertices.resize(max_size); - parent.assign(max_size, -1); - semi.assign(max_size, -1); - vertex.assign(max_size, -1); - ancestor.assign(max_size, -1); - label.assign(max_size, -1); - bucket.resize(max_size); - - DFS(entry, -1); - - for (int i = n - 1; i > 0; --i) { - int w = vertex[i]; - - for (BasicBlock* v : vertices[w]->getPrev()) { - int u = Eval(dfs_num[v]); - if (semi[u] < semi[w]) - semi[w] = semi[u]; - } - bucket[vertex[semi[w]]].push_back(w); - Link(parent[w], w); - - for (int v : bucket[parent[w]]) - { - int u = Eval(v); - if (semi[u] < semi[v]) - vertices[v]->setIdom(vertices[u]); - else - vertices[v]->setIdom(vertices[parent[w]]); - } - bucket[parent[w]].clear(); - } - - for (int i = 1; i < n; ++i) { - int w = vertex[i]; - if (vertices[w]->getIdom() != vertices[vertex[semi[w]]]) - vertices[w]->setIdom(vertices[w]->getIdom()->getIdom()); - } - - entry->setIdom(nullptr); - } -}; - -void buildDominatorTreeLT(std::vector& blocks) { - DominatorFinder finder(blocks); -} - -} diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index 4bf0fec..5060983 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -18,13 +18,27 @@ #include "../GraphCall/graph_calls.h" #include "../GraphCall/graph_calls_func.h" -#include "libSage++.h" #include "projectParameters.h" -#include "domTree.h" -using namespace std; +using std::set; +using std::map; +using std::string; +using std::vector; +using std::tuple; +using std::pair; +using std::make_tuple; +using std::find_if; -tuple stmtToIR(const map>& CFGraph, SgStatement* stmt) +static map> call_sites; + +enum class MODE +{ + BEFORE, + AFTER +}; + +static tuple +stmtToIR(const map>& CFGraph, SgStatement* stmt) { SgStatement* cur = stmt; cur->switchToFile(); @@ -49,7 +63,8 @@ tuple stmtToIR(const map IRByNumber(const map>& CFGraph, int num) +static tuple +IRByNumber(const map>& CFGraph, int num) { if (num < 0) return { NULL, NULL, NULL }; @@ -60,30 +75,27 @@ tuple IRByNumber(const map return make_tuple(func, getInstructionByNumber(byBB->getInstructions(), num), byBB); printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - return { NULL, NULL, NULL}; + return { NULL, NULL, NULL }; } template -void processArgument(set& worklist, - SAPFOR::Argument* arg, - Iterator instr, - Iterator first_instr) +static void processArgument(set& worklist, + SAPFOR::Argument* arg, + Iterator instr, Iterator first_instr) { if (arg == NULL) return; + if (arg->getType() == SAPFOR::CFG_ARG_TYPE::REG) extract_vars_from_reg(worklist, arg, instr, first_instr); else if (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR) - { worklist.insert(arg); - } } template -void extract_vars_from_reg(set& worklist, - SAPFOR::Argument* reg, - Iterator instr, - Iterator first_instr) +static void extract_vars_from_reg(set& worklist, + SAPFOR::Argument* reg, + Iterator instr, Iterator first_instr) { for (; instr >= first_instr; instr--) { @@ -96,21 +108,21 @@ void extract_vars_from_reg(set& worklist, } } -void lookup_for_vars(std::set>& where_to_add, - set& worklist, - SAPFOR::Instruction* instr, - SAPFOR::BasicBlock* bblock, - FuncInfo* cur_func, - const std::map>& fullIR) +static void lookup_for_vars(set>& where_to_add, + set& worklist, + SAPFOR::Instruction* instr, + SAPFOR::BasicBlock* bblock, + FuncInfo* cur_func, + const map>& fullIR) { while (bblock) { auto first_instr = bblock->getInstructions().begin(); - auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { + auto cur_instr = find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { return i->getInstruction() == instr; - }); + }); - for (; cur_instr >= bblock->getInstructions().begin(); cur_instr--) + for (; cur_instr >= bblock->getInstructions().begin(); --cur_instr) { auto instr = (*cur_instr)->getInstruction(); auto result_arg = instr->getResult(); @@ -123,6 +135,7 @@ void lookup_for_vars(std::set>& wher processArgument(worklist, arg2, cur_instr, first_instr); worklist.erase(result_arg); } + if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) { // skip to F_CALL @@ -136,7 +149,7 @@ void lookup_for_vars(std::set>& wher auto filename = stmt_before->fileName(); auto line = stmt_before->lineNumber(); auto var_name = arg1->getValue().substr(arg1->getValue().find('%') + 1); - __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg1->getValue().c_str(), line, filename); + __spf_print(1, "Please specify value of variable %s on line %d of file %s\n", arg1->getValue().c_str(), line, filename); auto toAdd = make_tuple(stmt_before, var_name, MODE::AFTER); where_to_add.insert(toAdd); worklist.erase(arg1); @@ -159,7 +172,7 @@ void lookup_for_vars(std::set>& wher auto line = stmt_after->lineNumber(); auto var_name = arg->getValue().substr(arg->getValue().find('%') + 1); __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible\n", arg->getValue().c_str()); - __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); + __spf_print(1, "Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE); where_to_add.insert(toAdd); worklist.erase(arg); @@ -175,7 +188,8 @@ void lookup_for_vars(std::set>& wher } while (bblock && group_by_block.find(bblock) == group_by_block.end()) - bblock = bblock->getIdom(); + bblock = bblock->getDom(); + if (bblock) instr = group_by_block[bblock]; } @@ -189,6 +203,7 @@ void lookup_for_vars(std::set>& wher set found_rd; if (RD.count(arg)) found_rd = RD.at(arg); + if (found_rd.size() == 0) { auto call_instr = call_sites[cur_func].size() ? call_sites[cur_func].front() : NULL; @@ -202,10 +217,10 @@ void lookup_for_vars(std::set>& wher call_instr = call_sites[call_func].size() ? call_sites[call_func].front() : NULL; } } + if (found_rd.size() == 1 && *found_rd.begin() == SAPFOR::CFG_VAL::UNINIT) - { __spf_print(1, "variable %s has no definition\n", arg->getValue().c_str()); - } else if (found_rd.size() > 1) + else if (found_rd.size() > 1) { auto first_instr = fullIR.at(cur_func).front()->getInstructions().begin(); auto stmt_after = (*first_instr)->getInstruction()->getOperator(); @@ -213,7 +228,7 @@ void lookup_for_vars(std::set>& wher auto line = stmt_after->lineNumber(); auto var_name = arg->getValue().substr(arg->getValue().find('%') + 1); __spf_print(1, "variable %s has multiple reaching definitions, further analysis is impossible\n", arg->getValue().c_str()); - __spf_print(1,"Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); + __spf_print(1, "Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE); where_to_add.insert(toAdd); } @@ -221,7 +236,7 @@ void lookup_for_vars(std::set>& wher { auto instr_num = *found_rd.begin(); auto [func, instr, bblock] = IRByNumber(fullIR, instr_num); - set new_worklist = {arg}; + set new_worklist = { arg }; lookup_for_vars(where_to_add, new_worklist, instr, bblock, func, fullIR); } @@ -236,14 +251,15 @@ void lookup_for_vars(std::set>& wher auto [call_func, _, call_bblock] = IRByNumber(fullIR, call_instr->getNumber()); auto first_instr = call_bblock->getInstructions().begin(); - auto cur_instr = std::find_if(first_instr, call_bblock->getInstructions().end(), [call_instr](SAPFOR::IR_Block* i) { + auto cur_instr = find_if(first_instr, call_bblock->getInstructions().end(), [call_instr](SAPFOR::IR_Block* i) { return i->getInstruction() == call_instr; - }); + }); + for (auto& arg : worklist) { if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_) { - auto param_num= stoi(arg->getValue().substr(arg->getValue().find('%', arg->getValue().find('%') + 1) + 1)); + auto param_num = stoi(arg->getValue().substr(arg->getValue().find('%', arg->getValue().find('%') + 1) + 1)); auto param_instr = (cur_instr - (params_num - param_num)); auto param_arg = (*param_instr)->getInstruction()->getArg1(); processArgument(new_worklist, param_arg, param_instr, first_instr); @@ -254,24 +270,23 @@ void lookup_for_vars(std::set>& wher } -void handle_single_allocate(std::set>& where_to_add, - SgStatement* alloc_statement, - const std::map>& fullIR) +static void handle_single_allocate(set>& where_to_add, + SgStatement* alloc_statement, + const map>& fullIR) { auto [func, instr, bblock] = stmtToIR(fullIR, alloc_statement); auto first_instr = bblock->getInstructions().begin(); - auto cur_instr = std::find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { + auto cur_instr = find_if(first_instr, bblock->getInstructions().end(), [instr](SAPFOR::IR_Block* i) { return i->getInstruction() == instr; - }); + }); auto alloc_instr = cur_instr; // skip to F_CALL _ALLOC n while ((*alloc_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::F_CALL || - (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") + (*alloc_instr)->getInstruction()->getArg1()->getValue() != "_ALLOC") alloc_instr++; - auto arrays_num = stoi((*alloc_instr)->getInstruction()->getArg2()->getValue()); set worklist; @@ -281,7 +296,7 @@ void handle_single_allocate(std::set>& wh auto param_reg = (*param_instr)->getInstruction()->getArg1(); while ((*param_instr)->getInstruction()->getOperation() != SAPFOR::CFG_OP::LOAD || - (*param_instr)->getInstruction()->getResult() != param_reg) + (*param_instr)->getInstruction()->getResult() != param_reg) param_instr--; @@ -292,29 +307,29 @@ void handle_single_allocate(std::set>& wh auto ref_instr = --param_instr; if ((*ref_instr)->getInstruction()->getOperation() == SAPFOR::CFG_OP::RANGE) { - vector range_args = {(*ref_instr)->getInstruction()->getArg1(), + vector range_args = { (*ref_instr)->getInstruction()->getArg1(), (*ref_instr)->getInstruction()->getArg2(), - (*ref_instr)->getInstruction()->getResult()}; + (*ref_instr)->getInstruction()->getResult() }; for (auto& arg : range_args) processArgument(worklist, arg, ref_instr, first_instr); - } else + } + else { auto arg = (*ref_instr)->getInstruction()->getArg1(); processArgument(worklist, arg, ref_instr, first_instr); } } } - lookup_for_vars(where_to_add,worklist, instr, bblock, func, fullIR); + lookup_for_vars(where_to_add, worklist, instr, bblock, func, fullIR); } -void handle_single_loop(std::set>& where_to_add, - SgStatement* loop_stmt, - const std::map>& fullIR) +static void handle_single_loop(set>& where_to_add, + SgStatement* loop_stmt, + const map>& fullIR) { auto [func, instr, bblock] = stmtToIR(fullIR, loop_stmt); - auto cur_instr = bblock->getInstructions().end() - 1; set worklist; @@ -323,10 +338,9 @@ void handle_single_loop(std::set>& where_ lookup_for_vars(where_to_add, worklist, (*cur_instr)->getInstruction(), bblock, func, fullIR); } -void -findParameters(ResultSet& foundParameters, - std::map>& fullIR, - const std::map, std::pair>& declaredArrays) +void findParameters(ResultSet& foundParameters, + map>& fullIR, + const map, pair>& declaredArrays) { set> where_to_add; @@ -337,6 +351,7 @@ findParameters(ResultSet& foundParameters, for (auto& [func, bblocks] : fullIR) { for (const auto& block : bblocks) + { for (const auto& ir_block : block->getInstructions()) { auto instr = ir_block->getInstruction(); @@ -349,32 +364,37 @@ findParameters(ResultSet& foundParameters, call_sites[func_info->second].push_back(instr); } } - - - SAPFOR::buildDominatorTreeLT(bblocks); + } } - std::set alloc_statements; + set alloc_statements; for (const auto& [func, bblocks] : fullIR) + { for (const auto& block : bblocks) + { for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr) { auto op = (*instr)->getInstruction()->getOperator(); - if (op && op->variant() == ALLOCATE_STMT) { + if (op && op->variant() == ALLOCATE_STMT) alloc_statements.insert(op); - } } + } + } set for_statements; // Find all FOR statements in the program for (const auto& [func, bblocks] : fullIR) + { for (const auto& block : bblocks) + { for (auto instr = block->getInstructions().begin(); instr != block->getInstructions().end(); ++instr) { auto op = (*instr)->getInstruction()->getOperator(); if (op && op->variant() == FOR_NODE) for_statements.insert(op); } + } + } for (const auto& alloc_statement : alloc_statements) handle_single_allocate(where_to_add, alloc_statement, fullIR); diff --git a/src/ProjectParameters/projectParameters.h b/src/ProjectParameters/projectParameters.h index 1181560..83a69e3 100644 --- a/src/ProjectParameters/projectParameters.h +++ b/src/ProjectParameters/projectParameters.h @@ -1,42 +1,14 @@ #pragma once -#include "libSage++.h" #include #include #include #include +#include "..\GraphCall\graph_calls.h" + using ResultSet = std::set>; -enum class MODE -{ - BEFORE, - AFTER -}; - -static std::map> call_sites; - -template -void extract_vars_from_reg(std::set& worklist, - SAPFOR::Argument* reg, - Iterator instr, - Iterator first_instr); - - -template -void processArgument(std::set& worklist, - SAPFOR::Argument* arg, - Iterator instr, - Iterator first_instr); - -void lookup_for_vars(std::set>& where_to_add, - std::set& worklist, - SAPFOR::Instruction* instr, - SAPFOR::BasicBlock* bblock, - FuncInfo* cur_func, - const std::map>& fullIR); - -void -findParameters(ResultSet& foundParameters, - std::map>& fullIR, - const std::map, std::pair>& declaredArrays); +void findParameters(ResultSet& foundParameters, + std::map>& fullIR, + const std::map, std::pair>& declaredArrays); diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 2bee6a5..e604e39 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1894,9 +1894,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne else if (curr_regime == RENAME_SYMBOLS) runRenameSymbols(&project, commonBlocks); else if (curr_regime == FIND_PARAMETERS) - { findParameters(parametersOfProject, fullIR, declaredArrays); - } else if (curr_regime == BUILD_IR) { auto CFG_forFile = buildCFG(commonBlocks, allFuncInfo_IR, SAPFOR::CFG_Settings(0)); diff --git a/src/Utils/CommonBlock.h b/src/Utils/CommonBlock.h index 196f3b9..7b89792 100644 --- a/src/Utils/CommonBlock.h +++ b/src/Utils/CommonBlock.h @@ -1,5 +1,7 @@ #pragma once +#include "../Distribution/Array.h" + enum varType { SCALAR, ARRAY, CONST, ANOTHER }; struct CommonVariableUse diff --git a/src/Utils/version.h b/src/Utils/version.h index 43758a9..6f49767 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2422" +#define VERSION_SPF "2423" From ad99446b129d60641d8af81815cd7eb269bf7778 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 11:57:39 +0300 Subject: [PATCH 08/12] added missing --- src/CFGraph/IR_domTree.cpp | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/CFGraph/IR_domTree.cpp diff --git a/src/CFGraph/IR_domTree.cpp b/src/CFGraph/IR_domTree.cpp new file mode 100644 index 0000000..cfab670 --- /dev/null +++ b/src/CFGraph/IR_domTree.cpp @@ -0,0 +1,100 @@ +#include "dvm.h" +#include "IR_domTree.h" + +namespace SAPFOR { + void DominatorFinder::DFS(BasicBlock* v, int parent_num) { + dfs_num[v] = n; + vertex[n] = n; + semi[n] = n; + label[n] = n; + ancestor[n] = -1; + parent[n] = parent_num; + vertices[n++] = v; + + for (const auto& w : v->getNext()) { + if (dfs_num[w] == -1) + DFS(w, dfs_num[v]); + } + } + + void DominatorFinder::Compress(int v) { + if (ancestor[ancestor[v]] != -1) { + Compress(ancestor[v]); + + if (semi[label[ancestor[v]]] < semi[label[v]]) + label[v] = label[ancestor[v]]; + ancestor[v] = ancestor[ancestor[v]]; + } + } + + int DominatorFinder::Eval(int v) { + if (ancestor[v] == -1) + return v; + + Compress(v); + return label[v]; + } + + void DominatorFinder::Link(int v, int w) { + ancestor[w] = v; + } + + DominatorFinder::DominatorFinder(std::vector& blocks) { + if (blocks.empty()) + return; + + entry = blocks[0]; + n = 0; + + for (auto block : blocks) + dfs_num[block] = -1; + + int max_size = blocks.size(); + vertices.resize(max_size); + parent.assign(max_size, -1); + semi.assign(max_size, -1); + vertex.assign(max_size, -1); + ancestor.assign(max_size, -1); + label.assign(max_size, -1); + bucket.resize(max_size); + + DFS(entry, -1); + + for (int i = n - 1; i > 0; --i) { + int w = vertex[i]; + + for (BasicBlock* v : vertices[w]->getPrev()) { + int u = Eval(dfs_num[v]); + + if (semi[u] < semi[w]) + semi[w] = semi[u]; + } + + bucket[vertex[semi[w]]].push_back(w); + Link(parent[w], w); + + for (int v : bucket[parent[w]]) + { + int u = Eval(v); + if (semi[u] < semi[v]) + vertices[v]->setDom(vertices[u]); + else + vertices[v]->setDom(vertices[parent[w]]); + } + bucket[parent[w]].clear(); + } + + for (int i = 1; i < n; ++i) { + int w = vertex[i]; + + if (vertices[w]->getDom() != vertices[vertex[semi[w]]]) + vertices[w]->setDom(vertices[w]->getDom()->getDom()); + } + + entry->setDom(nullptr); + } + + void buildDominatorTree(std::vector& blocks) { + DominatorFinder finder(blocks); + } +} \ No newline at end of file From b454858647a9f6f8b90cbe06dc424dfc22b64dec Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Thu, 1 May 2025 19:48:47 +0300 Subject: [PATCH 09/12] delete zero from directive --- src/ProjectParameters/projectParameters.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index 5060983..af7c038 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -408,9 +408,8 @@ void findParameters(ResultSet& foundParameters, SgVariableSymb* var_symb = new SgVariableSymb(var_name.c_str()); SgVarRefExp* var = new SgVarRefExp(var_symb); - SgValueExp* zero = new SgValueExp(1337); SgExprListExp* ex = new SgExprListExp(); - auto assgn_op = new SgExpression(ASSGN_OP, var, zero); + auto assgn_op = new SgExpression(ASSGN_OP, var, NULL); ex->setLhs(assgn_op); SgExpression* parameter_op = new SgExpression(SPF_PARAMETER_OP, ex); From c6a0c7328721caa7ad253dd7a93d21df1ad466e2 Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Wed, 4 Jun 2025 09:18:24 +0300 Subject: [PATCH 10/12] fix UB in dom tree builder --- src/CFGraph/CFGraph.h | 8 +------- src/CFGraph/IR_domTree.cpp | 4 +++- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/CFGraph/CFGraph.h b/src/CFGraph/CFGraph.h index f0308fd..8dfeb6a 100644 --- a/src/CFGraph/CFGraph.h +++ b/src/CFGraph/CFGraph.h @@ -73,13 +73,7 @@ namespace SAPFOR const std::vector& getNext() const { return next; } const std::vector& getPrev() const { return prev; } BasicBlock* getDom() const - { - if (!directDominator) - { - __spf_print(1, "%s\n", "the dominator tree was built with an error or was not built at all"); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - + { return directDominator; } diff --git a/src/CFGraph/IR_domTree.cpp b/src/CFGraph/IR_domTree.cpp index cfab670..067cc7c 100644 --- a/src/CFGraph/IR_domTree.cpp +++ b/src/CFGraph/IR_domTree.cpp @@ -64,6 +64,8 @@ namespace SAPFOR { int w = vertex[i]; for (BasicBlock* v : vertices[w]->getPrev()) { + if (dfs_num[v] == -1) + continue; int u = Eval(dfs_num[v]); if (semi[u] < semi[w]) @@ -97,4 +99,4 @@ namespace SAPFOR { void buildDominatorTree(std::vector& blocks) { DominatorFinder finder(blocks); } -} \ No newline at end of file +} From ae9cc2bf3b8e82235f9ffc58ff0e9b515ffa03d3 Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Wed, 4 Jun 2025 09:19:10 +0300 Subject: [PATCH 11/12] Fix set iterators invalidation in find_parameters --- src/ProjectParameters/projectParameters.cpp | 8 ++++++-- src/ProjectParameters/projectParameters.h | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index af7c038..997f57c 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -131,9 +131,9 @@ static void lookup_for_vars(set>& where_to_add if (worklist.count(result_arg)) { + worklist.erase(result_arg); processArgument(worklist, arg1, cur_instr, first_instr); processArgument(worklist, arg2, cur_instr, first_instr); - worklist.erase(result_arg); } if (instr->getOperation() == SAPFOR::CFG_OP::PARAM && worklist.count(arg1)) @@ -159,6 +159,8 @@ static void lookup_for_vars(set>& where_to_add const auto& RD = bblock->getRD_In(); map group_by_block; + + set to_erase; for (auto& arg : worklist) { if (RD.count(arg)) @@ -175,7 +177,7 @@ static void lookup_for_vars(set>& where_to_add __spf_print(1, "Please specify value of variable %s on line %d of file %s\n", arg->getValue().c_str(), line, filename); auto toAdd = make_tuple(stmt_after, var_name, MODE::BEFORE); where_to_add.insert(toAdd); - worklist.erase(arg); + to_erase.insert(arg); } else { @@ -186,6 +188,8 @@ static void lookup_for_vars(set>& where_to_add } } } + for (const auto& arg : to_erase) + worklist.erase(arg); while (bblock && group_by_block.find(bblock) == group_by_block.end()) bblock = bblock->getDom(); diff --git a/src/ProjectParameters/projectParameters.h b/src/ProjectParameters/projectParameters.h index 83a69e3..5be0d3c 100644 --- a/src/ProjectParameters/projectParameters.h +++ b/src/ProjectParameters/projectParameters.h @@ -5,7 +5,7 @@ #include #include -#include "..\GraphCall\graph_calls.h" +#include "../GraphCall/graph_calls.h" using ResultSet = std::set>; From 622159cba6b235ab52a460b9f044d35cb2465105 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 11 Jun 2025 15:07:06 +0300 Subject: [PATCH 12/12] improved --- src/CFGraph/CFGraph.cpp | 2 +- src/ProjectParameters/projectParameters.cpp | 5 +++-- src/Sapfor.cpp | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CFGraph/CFGraph.cpp b/src/CFGraph/CFGraph.cpp index 749eac3..6fc4682 100644 --- a/src/CFGraph/CFGraph.cpp +++ b/src/CFGraph/CFGraph.cpp @@ -1156,7 +1156,7 @@ map> buildCFG(const map& common SAPFOR::buildDominatorTree(bblocks); auto msec = duration_cast(high_resolution_clock::now() - t).count(); - __spf_print(1, "dominator build time is %.3f sec\n", msec / 1000.); + __spf_print(1, " dominator build time is %.3f sec\n", msec / 1000.); } if (SgFile::switchToFile(oldFile) == -1) diff --git a/src/ProjectParameters/projectParameters.cpp b/src/ProjectParameters/projectParameters.cpp index 30e21f2..6de6861 100644 --- a/src/ProjectParameters/projectParameters.cpp +++ b/src/ProjectParameters/projectParameters.cpp @@ -429,10 +429,11 @@ void findParameters(ResultSet& foundParameters, toAdd->setFileId(stmt_before->getFileId()); toAdd->setProject(stmt_before->getProject()); - if (mode == MODE::AFTER) + //NOTE: only for debbuging, results will be transferred to the visualizer + /*if (mode == MODE::AFTER) stmt_before->insertStmtAfter(*toAdd, *stmt_before->controlParent()); else - stmt_before->insertStmtBefore(*toAdd, *stmt_before->controlParent()); + stmt_before->insertStmtBefore(*toAdd, *stmt_before->controlParent());*/ foundParameters.insert(make_tuple(stmt_before->fileName(), stmt_before->lineNumber(), var_name)); } diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 8e2ed3e..961964f 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -2373,7 +2373,6 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case LOOPS_COMBINER: case FIX_COMMON_BLOCKS: case TEST_PASS: - case FIND_PARAMETERS: case SET_IMPLICIT_NONE: runAnalysis(*project, curr_regime, false); case SUBST_EXPR_RD_AND_UNPARSE: