This commit is contained in:
2025-05-01 18:52:29 +03:00
committed by Alexander
parent 6742932862
commit 4e16638c36

View File

@@ -75,7 +75,6 @@ void processArgument(set<SAPFOR::Argument*>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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
@@ -193,7 +186,6 @@ void lookup_for_vars(std::set<std::tuple<SgStatement*, std::string, MODE>>& wher
{
if (arg->isMemGlobal())
{
std::cout << "global: " << arg->getValue() << " : " << RD.count(arg) << '\n';
set<int> found_rd;
if (RD.count(arg))
found_rd = RD.at(arg);
@@ -203,19 +195,16 @@ void lookup_for_vars(std::set<std::tuple<SgStatement*, std::string, MODE>>& 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";
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();
@@ -246,7 +235,6 @@ void lookup_for_vars(std::set<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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);
}
}
@@ -287,7 +273,6 @@ void handle_single_allocate(std::set<tuple<SgStatement*, std::string, MODE>>& wh
auto arrays_num = stoi((*alloc_instr)->getInstruction()->getArg2()->getValue());
std::cout << "arrays_num: " << arrays_num << std::endl; //DEBUG PRINT
set<SAPFOR::Argument*> worklist;
for (int i = 0; i < arrays_num; i++)
@@ -311,11 +296,8 @@ void handle_single_allocate(std::set<tuple<SgStatement*, std::string, MODE>>& wh
(*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
{
auto arg = (*ref_instr)->getInstruction()->getArg1();
@@ -332,7 +314,6 @@ void handle_single_loop(std::set<tuple<SgStatement*, std::string, MODE>>& where_
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& 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<SgStatement*> 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<SgStatement*> for_statements;
// Find all FOR statements in the program
for (const auto& [func, bblocks] : fullIR)
@@ -408,35 +376,33 @@ findParameters(ResultSet& foundParameters,
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);
auto assgn_op = new SgExpression(ASSGN_OP, var, zero);
ex->setLhs(assgn_op);
std::cout << "len: " << ex->length() << '\n';
ex->setLhs(new SgExpression(ASSGN_OP, var, zero));
std::cout << "stmt_before: " << stmt_before->unparse();
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