fixed code style, moved dom tree building to IR
This commit is contained in:
@@ -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<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*> stmtToIR(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph, SgStatement* stmt)
|
||||
static map<FuncInfo*, vector<SAPFOR::Instruction*>> call_sites;
|
||||
|
||||
enum class MODE
|
||||
{
|
||||
BEFORE,
|
||||
AFTER
|
||||
};
|
||||
|
||||
static tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*>
|
||||
stmtToIR(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph, SgStatement* stmt)
|
||||
{
|
||||
SgStatement* cur = stmt;
|
||||
cur->switchToFile();
|
||||
@@ -49,7 +63,8 @@ tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*> stmtToIR(const map<F
|
||||
return { NULL, NULL, NULL };
|
||||
}
|
||||
|
||||
tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*> IRByNumber(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph, int num)
|
||||
static tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*>
|
||||
IRByNumber(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph, int num)
|
||||
{
|
||||
if (num < 0)
|
||||
return { NULL, NULL, NULL };
|
||||
@@ -60,30 +75,27 @@ tuple<FuncInfo*, SAPFOR::Instruction*, SAPFOR::BasicBlock*> 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<typename Iterator>
|
||||
void processArgument(set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* arg,
|
||||
Iterator instr,
|
||||
Iterator first_instr)
|
||||
static void processArgument(set<SAPFOR::Argument*>& 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<typename Iterator>
|
||||
void extract_vars_from_reg(set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* reg,
|
||||
Iterator instr,
|
||||
Iterator first_instr)
|
||||
static void extract_vars_from_reg(set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Argument* reg,
|
||||
Iterator instr, Iterator first_instr)
|
||||
{
|
||||
for (; instr >= first_instr; instr--)
|
||||
{
|
||||
@@ -96,21 +108,21 @@ void extract_vars_from_reg(set<SAPFOR::Argument*>& worklist,
|
||||
}
|
||||
}
|
||||
|
||||
void lookup_for_vars(std::set<std::tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Instruction* instr,
|
||||
SAPFOR::BasicBlock* bblock,
|
||||
FuncInfo* cur_func,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
static void lookup_for_vars(set<tuple<SgStatement*, string, MODE>>& where_to_add,
|
||||
set<SAPFOR::Argument*>& worklist,
|
||||
SAPFOR::Instruction* instr,
|
||||
SAPFOR::BasicBlock* bblock,
|
||||
FuncInfo* cur_func,
|
||||
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& wher
|
||||
set<int> 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& wher
|
||||
{
|
||||
auto instr_num = *found_rd.begin();
|
||||
auto [func, instr, bblock] = IRByNumber(fullIR, instr_num);
|
||||
set<SAPFOR::Argument*> new_worklist = {arg};
|
||||
set<SAPFOR::Argument*> 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<std::tuple<SgStatement*, std::string, MODE>>& 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<std::tuple<SgStatement*, std::string, MODE>>& wher
|
||||
|
||||
}
|
||||
|
||||
void handle_single_allocate(std::set<tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
SgStatement* alloc_statement,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
static void handle_single_allocate(set<tuple<SgStatement*, string, MODE>>& where_to_add,
|
||||
SgStatement* alloc_statement,
|
||||
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& 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<SAPFOR::Argument*> worklist;
|
||||
@@ -281,7 +296,7 @@ void handle_single_allocate(std::set<tuple<SgStatement*, std::string, MODE>>& 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<tuple<SgStatement*, std::string, MODE>>& wh
|
||||
auto ref_instr = --param_instr;
|
||||
if ((*ref_instr)->getInstruction()->getOperation() == SAPFOR::CFG_OP::RANGE)
|
||||
{
|
||||
vector<SAPFOR::Argument*> range_args = {(*ref_instr)->getInstruction()->getArg1(),
|
||||
vector<SAPFOR::Argument*> 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<tuple<SgStatement*, std::string, MODE>>& where_to_add,
|
||||
SgStatement* loop_stmt,
|
||||
const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
static void handle_single_loop(set<tuple<SgStatement*, string, MODE>>& where_to_add,
|
||||
SgStatement* loop_stmt,
|
||||
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR)
|
||||
{
|
||||
auto [func, instr, bblock] = stmtToIR(fullIR, loop_stmt);
|
||||
|
||||
auto cur_instr = bblock->getInstructions().end() - 1;
|
||||
|
||||
set<SAPFOR::Argument*> worklist;
|
||||
@@ -323,10 +338,9 @@ void handle_single_loop(std::set<tuple<SgStatement*, std::string, MODE>>& where_
|
||||
lookup_for_vars(where_to_add, worklist, (*cur_instr)->getInstruction(), bblock, func, fullIR);
|
||||
}
|
||||
|
||||
void
|
||||
findParameters(ResultSet& foundParameters,
|
||||
std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR,
|
||||
const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
|
||||
void findParameters(ResultSet& foundParameters,
|
||||
map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR,
|
||||
const map<tuple<int, string, string>, pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
|
||||
{
|
||||
set<tuple<SgStatement*, string, MODE>> 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<SgStatement*> alloc_statements;
|
||||
set<SgStatement*> 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<SgStatement*> 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);
|
||||
|
||||
Reference in New Issue
Block a user