From fd18e95fa2cee40a0a38c19c23e917c7a4efbdfc Mon Sep 17 00:00:00 2001 From: Vladislav Abrosimov Date: Thu, 1 May 2025 18:52:29 +0300 Subject: [PATCH] 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