reorganize live variable analysis to use it's functions further

This commit is contained in:
mkoch
2024-01-09 18:13:56 +03:00
parent 3cd16c9d0f
commit 5f8940f34d
2 changed files with 227 additions and 159 deletions

View File

@@ -17,6 +17,8 @@ using std::set;
using std::unordered_map; using std::unordered_map;
using std::list; using std::list;
using LIVE_VARIABLES::fcall;
namespace SAPFOR namespace SAPFOR
{ {
bool BasicBlock::addLive(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& to_add, bool in) { bool BasicBlock::addLive(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& to_add, bool in) {
@@ -28,7 +30,7 @@ namespace SAPFOR
{ {
const set<SAPFOR::BasicBlock*>& add_in_live = byNew.second; const set<SAPFOR::BasicBlock*>& add_in_live = byNew.second;
set<SAPFOR::BasicBlock*> new_in_live = {}; set<SAPFOR::BasicBlock*> new_in_live = {};
auto current_set_iter = current_set.find(byNew.first); auto current_set_iter = current_set.find(byNew.first);
if (current_set_iter == current_set.end()) if (current_set_iter == current_set.end())
@@ -106,7 +108,7 @@ namespace SAPFOR
return res; return res;
} }
void BasicBlock::compressLives() { void BasicBlock::compressLives() {
for (auto& bySrc : { &live_in, &live_out, &live_inout }) for (auto& bySrc : { &live_in, &live_out, &live_inout })
for (auto& byArg : *bySrc) for (auto& byArg : *bySrc)
@@ -114,89 +116,77 @@ namespace SAPFOR
} }
} }
struct fcall bool fcall::tryInsert(set<SAPFOR::BasicBlock*>& dest, SAPFOR::BasicBlock* b)
{ {
private: if (b == block || dest.find(block) == dest.end())
bool tryInsert(set<SAPFOR::BasicBlock*>& dest, SAPFOR::BasicBlock* b)
{ {
if (b == block || dest.find(block) == dest.end()) dest.insert(b);
{ return true;
dest.insert(b);
return true;
}
return false;
} }
public: return false;
FuncInfo* func; }
map<int, set<SAPFOR::BasicBlock*>> live_after;
set<int> dead_after;
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> commons_live_after; fcall::fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector<SAPFOR::Argument*>& p)
set<SAPFOR::Argument*> commons_dead_after; {
block = b;
func = f;
vector<SAPFOR::Argument*> params; int param_size = p.size();
SAPFOR::BasicBlock* block; params = vector<SAPFOR::Argument*>(param_size, NULL);
fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector<SAPFOR::Argument*>& p) for (int i = 0; i < param_size; i++)
{ if (f->funcParams.isArgOut(i))
block = b; params[i] = p[i];
func = f; }
int param_size = p.size();
params = vector<SAPFOR::Argument*>(param_size, NULL);
for (int i = 0; i < param_size; i++) void fcall::make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b)
if (f->funcParams.isArgOut(i)) {
params[i] = p[i]; if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_)
{
if (commons_dead_after.find(arg) == commons_dead_after.end())
tryInsert(commons_live_after[arg], b);
} }
void make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b) auto it = find(params.begin(), params.end(), arg);
if (it != params.end())
{ {
if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) int idx = it - params.begin();
{ if (dead_after.find(idx) == dead_after.end())
if (commons_dead_after.find(arg) == commons_dead_after.end()) tryInsert(live_after[idx], b);
tryInsert(commons_live_after[arg], b);
}
auto it = find(params.begin(), params.end(), arg);
if (it != params.end())
{
int idx = it - params.begin();
if (dead_after.find(idx) == dead_after.end())
tryInsert(live_after[idx], b);
}
} }
}
void make_dead(SAPFOR::Argument* arg) void fcall::make_dead(SAPFOR::Argument* arg)
{
if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_)
{ {
if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) if (commons_live_after.find(arg) == commons_live_after.end())
commons_dead_after.insert(arg); commons_dead_after.insert(arg);
auto it = find(params.begin(), params.end(), arg);
if (it != params.end())
{
int idx = it - params.begin();
dead_after.insert(idx);
}
} }
void updateFromOut() auto it = find(params.begin(), params.end(), arg);
if (it != params.end())
{ {
for (const auto& p : block->getLiveOut()) int idx = it - params.begin();
for (auto b : p.second) if (live_after.find(idx) == live_after.end())
make_live(p.first, b); dead_after.insert(idx);
} }
}; }
void fcall::updateFromOut()
{
for (const auto& p : block->getLiveOut())
for (auto b : p.second)
make_live(p.first, b);
}
static bool getLiveDead(const vector<SAPFOR::Argument*>& params, const string& func_name, static bool getLiveDead(const vector<SAPFOR::Argument*>& params, const string& func_name,
set<SAPFOR::Argument*>& live, set<SAPFOR::Argument*>& dead); set<SAPFOR::Argument*>& live, set<SAPFOR::Argument*>& dead);
static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def, static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls, vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls,
const map<string, FuncInfo*>& funcByName); const map<string, FuncInfo*>& funcByName, bool interprocedural);
class LiveVarAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>> { class LiveVarAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>> {
private: private:
@@ -234,11 +224,11 @@ public:
}; };
LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector<SAPFOR::Argument*>& formal_parameters, LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector<SAPFOR::Argument*>& formal_parameters,
vector<fcall>& fcalls, const map<string, FuncInfo*>& funcByName) vector<fcall>& fcalls, const map<string, FuncInfo*>& funcByName)
{ {
setBlock(block); setBlock(block);
buildUseDef(getBlock(), live, dead, formal_parameters, fcalls, funcByName); buildUseDef(getBlock(), live, dead, formal_parameters, fcalls, funcByName, true);
for (SAPFOR::Argument* arg : live) for (SAPFOR::Argument* arg : live)
getBlock()->addLiveIn({ { arg, { getBlock() } } }); getBlock()->addLiveIn({ { arg, { getBlock() } } });
@@ -251,8 +241,7 @@ protected:
vector<fcall>& fcalls; vector<fcall>& fcalls;
const map<string, FuncInfo*>& funcByName; const map<string, FuncInfo*>& funcByName;
LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override {
{
return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName); return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName);
}; };
public: public:
@@ -261,107 +250,151 @@ public:
{ }; { };
}; };
//Build use and def sets of block. Result are stored in use and def void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls, vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls,
const map<string, FuncInfo*>& funcByName) vector<SAPFOR::Argument*>& lastParamRef, int& last_param_ref_index, int& last_param_ref_size,
string& fName, const map<string, FuncInfo*>& funcByName, bool interprocedural)
{ {
set<SAPFOR::Argument*> tmp_use, tmp_def; for (auto arg : { instr->getArg1(), instr->getArg2(), instr->getResult() })
if (arg && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_)
formal_parameters[getParamIndex(arg, formal_parameters.size())] = arg;
vector<SAPFOR::Argument*> lastParamRef; SAPFOR::Argument* res_arg = NULL;
bool fcall_added;
for (auto ir_block : block->getInstructions()) static const set<SAPFOR::CFG_OP> skip = { SAPFOR::CFG_OP::ENTRY };
SAPFOR::CFG_OP instr_operation = instr->getOperation();
if (hasStoreStructure(instr_operation))
{ {
fcall_added = false; res_arg = instr->getArg1();
SAPFOR::Instruction* instr = ir_block->getInstruction(); set<SAPFOR::Argument*> instr_args = { instr->getResult(), instr->getArg2() };
for (auto arg : { instr->getArg1(), instr->getArg2(), instr->getResult() }) use.insert(instr_args.begin(), instr_args.end());
if (arg && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_) }
formal_parameters[getParamIndex(arg, formal_parameters.size())] = arg; else if (instr_operation == SAPFOR::CFG_OP::PARAM)
{
lastParamRef[last_param_ref_index--] = instr->getArg1();
}
else if (instr_operation == SAPFOR::CFG_OP::F_CALL)
{
res_arg = instr->getResult();
set<SAPFOR::Argument*> res = {}, args = {}; last_param_ref_size = stoi(instr->getArg2()->getValue());
SAPFOR::Argument* res_arg = NULL;
static const set<SAPFOR::CFG_OP> skip = { SAPFOR::CFG_OP::ENTRY }; lastParamRef.clear();
SAPFOR::CFG_OP instr_operation = instr->getOperation(); lastParamRef.resize(last_param_ref_size);
if (hasStoreStructure(instr_operation)) {
res_arg = instr->getArg1(); last_param_ref_index = last_param_ref_size - 1;
std::set<SAPFOR::Argument*> instr_args = { instr->getResult(), instr->getArg2()};
insertIfVar(instr_args.begin(), instr_args.end(), args); fName = instr->getArg1()->getValue();
} }
else if (instr_operation == SAPFOR::CFG_OP::PARAM) else if (skip.find(instr_operation) == skip.end())
lastParamRef.push_back(instr->getArg1()); {
else if (instr_operation == SAPFOR::CFG_OP::F_CALL) //default
res_arg = instr->getResult();
std::set<SAPFOR::Argument*> intr_args = { instr->getArg1(), instr->getArg2() };
use.insert(intr_args.begin(), intr_args.end());
}
else
{
//skip
return;
}
if ((instr_operation == SAPFOR::CFG_OP::F_CALL || instr_operation == SAPFOR::CFG_OP::PARAM) && last_param_ref_index < 0) {
auto func_it = funcByName.find(fName);
if (func_it != funcByName.end())
{ {
res_arg = instr->getResult(); fcalls.push_back(fcall(func_it->second, block, lastParamRef));
int count = stoi(instr->getArg2()->getValue()); auto r_it = fcalls.rbegin();
if (lastParamRef.size() != count) auto r_end = fcalls.rend();
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
const string& fName = instr->getArg1()->getValue(); for (auto e : def)
auto func_it = funcByName.find(fName);
if (func_it != funcByName.end())
{
fcalls.push_back(fcall(func_it->second, block, lastParamRef));
fcall_added = true;
}
set<SAPFOR::Argument*> make_live, make_dead;
if (fName == "_READ")
res.insert(lastParamRef.begin(), lastParamRef.end());
else if (getLiveDead(lastParamRef, fName, make_live, make_dead))
{
insertIfVar(make_live.begin(), make_live.end(), args);
insertIfVar(make_dead.begin(), make_dead.end(), res);
}
else
insertIfVar(lastParamRef.begin(), lastParamRef.end(), args);
lastParamRef.clear();
}
else if(skip.find(instr_operation) == skip.end())
{
//default
res_arg = instr->getResult();
std::set<SAPFOR::Argument*> intr_args = { instr->getArg1(), instr->getArg2() };
insertIfVar(intr_args.begin(), intr_args.end(), args);
}
else {
//skip
continue;
}
if (res_arg && res_arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
res.insert(res_arg);
for (auto e : args)
if (!tmp_def.count(e))
tmp_use.insert(e);
for(auto e : res)
if (!tmp_use.count(e))
tmp_def.insert(e);
auto r_it = fcalls.rbegin();
auto r_end = fcalls.rend();
if (fcall_added && r_it != r_end)
r_it++;
while (r_it != r_end && r_it->block == block)
{
for (auto e : args)
r_it->make_live(e, block);
for (auto e : res)
r_it->make_dead(e); r_it->make_dead(e);
r_it++; for (auto e : use)
r_it->make_live(e, block);
}
set<SAPFOR::Argument*> make_live, make_dead;
if (fName == "_READ")
def.insert(lastParamRef.begin(), lastParamRef.end());
else if (interprocedural && getLiveDead(lastParamRef, fName, make_live, make_dead))
{
use.insert(make_live.begin(), make_live.end());
def.insert(make_dead.begin(), make_dead.end());
}
else
use.insert(lastParamRef.begin(), lastParamRef.end());
last_param_ref_index = 0;
last_param_ref_size = 0;
lastParamRef.clear();
fName = "";
}
if (res_arg)
def.insert(res_arg);
}
static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls,
vector<SAPFOR::Argument*>& lastParamRef, int& last_param_ref_index, int& last_param_ref_size,
string& fName, const map<string, FuncInfo*>& funcByName, bool interprocedural)
{
set<SAPFOR::Argument*> res, args;
getUseDefForInstruction(block, instr,
args, res,
formal_parameters, fcalls,
lastParamRef, last_param_ref_index, last_param_ref_size,
fName, funcByName,
interprocedural
);
for (auto e : res)
{
if (e && e->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
{
def.insert(e);
use.erase(e);
} }
} }
use = tmp_use; for (auto e : args)
def = tmp_def; {
if (e && e->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
{
use.insert(e);
def.erase(e);
}
}
}
//Build use and def sets of block. Result are stored in use and def
static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls,
const map<string, FuncInfo*>& funcByName, bool interprocedural)
{
vector<SAPFOR::Argument*> lastParamRef;
int last_param_ref_index = 0, last_param_ref_size = 0;
string fName;
const auto& instructions = block->getInstructions();
auto ir_block_it = instructions.rbegin(), ir_block_end = instructions.rend();
for (; ir_block_it != ir_block_end; ir_block_it++)
{
updateUseDefForInstruction(block, (*ir_block_it)->getInstruction(),
use, def,
formal_parameters, fcalls,
lastParamRef, last_param_ref_index, last_param_ref_size,
fName, funcByName,
interprocedural
);
}
} }
// prints info about live variables // prints info about live variables
@@ -379,7 +412,7 @@ void doDumpLive(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& CFGraph_for_p
for (const auto& live : byBB->getLiveIn()) for (const auto& live : byBB->getLiveIn())
{ {
__spf_print(1, " %s:", live.first->getValue().c_str()); __spf_print(1, " %s:", live.first->getValue().c_str());
for(auto use : live.second) for (auto use : live.second)
__spf_print(1, " %d", use->getNumber()); __spf_print(1, " %d", use->getNumber());
__spf_print(1, "\n"); __spf_print(1, "\n");
} }
@@ -482,8 +515,8 @@ static void fillLiveDeadArgs(const FuncInfo* func, const vector<SAPFOR::BasicBlo
// unite global arguments and actual parameters with given indexes for function // unite global arguments and actual parameters with given indexes for function
// stores the result in the last argument // stores the result in the last argument
bool joinGlobalsWithParameters(const vector<SAPFOR::Argument*>& params, bool joinGlobalsWithParameters(const vector<SAPFOR::Argument*>& params,
const map<string, pair<set<int>, set<SAPFOR::Argument*>>>& params_and_globals, const map<string, pair<set<int>, set<SAPFOR::Argument*>>>& params_and_globals,
const string& func_name, set<SAPFOR::Argument*>& result) const string& func_name, set<SAPFOR::Argument*>& result)
{ {
auto globals_it = params_and_globals.find(func_name); auto globals_it = params_and_globals.find(func_name);
@@ -498,7 +531,7 @@ bool joinGlobalsWithParameters(const vector<SAPFOR::Argument*>& params,
for (int idx : param_indexes) for (int idx : param_indexes)
{ {
if (idx < params_size) { if (idx < params_size) {
if(params[idx] && params[idx]->getType() == SAPFOR::CFG_ARG_TYPE::VAR) if (params[idx] && params[idx]->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
result.insert(params[idx]); result.insert(params[idx]);
} }
else else
@@ -510,11 +543,11 @@ bool joinGlobalsWithParameters(const vector<SAPFOR::Argument*>& params,
} }
// fill sets of arguments wich becomes live or dead after call with parameters params // fill sets of arguments wich becomes live or dead after call with parameters params
static bool getLiveDead(const vector<SAPFOR::Argument*>& params, const string& func_name, static bool getLiveDead(const vector<SAPFOR::Argument*>& params, const string& func_name,
set<SAPFOR::Argument*>& live, set<SAPFOR::Argument*>& dead) set<SAPFOR::Argument*>& live, set<SAPFOR::Argument*>& dead)
{ {
return joinGlobalsWithParameters(params, live_by_func, func_name, live) && return joinGlobalsWithParameters(params, live_by_func, func_name, live) &&
joinGlobalsWithParameters(params, dead_by_func, func_name, dead); joinGlobalsWithParameters(params, dead_by_func, func_name, dead);
} }
// entrypoint for live variable analysis pass // entrypoint for live variable analysis pass

View File

@@ -1,6 +1,35 @@
#pragma once
#include "../Utils/SgUtils.h" #include "../Utils/SgUtils.h"
#include "CFGraph.h" #include "CFGraph.h"
namespace LIVE_VARIABLES
{
struct fcall
{
private:
bool tryInsert(std::set<SAPFOR::BasicBlock*>& dest, SAPFOR::BasicBlock* b);
public:
FuncInfo* func;
std::map<int, std::set<SAPFOR::BasicBlock*>> live_after;
std::set<int> dead_after;
std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>> commons_live_after;
std::set<SAPFOR::Argument*> commons_dead_after;
std::vector<SAPFOR::Argument*> params;
SAPFOR::BasicBlock* block;
fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const std::vector<SAPFOR::Argument*>& p);
void make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b);
void make_dead(SAPFOR::Argument* arg);
void updateFromOut();
};
}
template <class IT, class DEST> template <class IT, class DEST>
void insertIfVar(IT begin, IT end, DEST& to) { void insertIfVar(IT begin, IT end, DEST& to) {
for (auto it = begin; it != end; it++) for (auto it = begin; it != end; it++)
@@ -8,6 +37,12 @@ void insertIfVar(IT begin, IT end, DEST& to) {
to.insert(*it); to.insert(*it);
}; };
void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
std::set<SAPFOR::Argument*>& use, std::set<SAPFOR::Argument*>& def,
std::vector<SAPFOR::Argument*>& formal_parameters, std::vector<LIVE_VARIABLES::fcall>& fcalls,
std::vector<SAPFOR::Argument*>& lastParamRef, int& last_param_ref_index, int& last_param_ref_size,
std::string& fName, const std::map<std::string, FuncInfo*>& funcByName, bool interprocedural);
void runLiveVariableAnalysis(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph_for_project); void runLiveVariableAnalysis(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph_for_project);
void doDumpLive(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph_for_project); void doDumpLive(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph_for_project);