Live variable analysis: use vectors of blocks instead of sets

This commit is contained in:
mkoch
2024-01-10 16:20:48 +03:00
parent 14219cdfd5
commit bd8690d54a
3 changed files with 36 additions and 28 deletions

View File

@@ -31,8 +31,8 @@ namespace SAPFOR
//live variables [arg -> blocks with usages]
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>> live_in, live_out, live_inout;
bool addLive(const std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>>& to_add, bool in);
std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>> getLive(bool in) const;
bool addLive(const std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& to_add, bool in);
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>> getLive(bool in) const;
bool removeLive(SAPFOR::Argument* to_remove, bool in);
public:
BasicBlock() { num = lastNumBlock++; }
@@ -70,14 +70,14 @@ namespace SAPFOR
/*
* FOR LIVE ANALYSIS
*/
bool addLiveIn(const std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>>& to_add) { return addLive(to_add, true); };
bool addLiveOut(const std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>>& to_add) { return addLive(to_add, false); };
bool addLiveIn(const std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& to_add) { return addLive(to_add, true); };
bool addLiveOut(const std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& to_add) { return addLive(to_add, false); };
bool removeLiveIn(SAPFOR::Argument* to_remove) { return removeLive(to_remove, true); };
bool removeLiveOut(SAPFOR::Argument* to_remove) { return removeLive(to_remove, false); };
std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>> getLiveIn() const { return getLive(true); };
std::map<SAPFOR::Argument*, std::set<SAPFOR::BasicBlock*>> getLiveOut() const { return getLive(false); };
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>> getLiveIn() const { return getLive(true); };
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>> getLiveOut() const { return getLive(false); };
void compressLives();
/*

View File

@@ -21,15 +21,15 @@ using LIVE_VARIABLES::fcall;
namespace SAPFOR
{
bool BasicBlock::addLive(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& to_add, bool in) {
bool BasicBlock::addLive(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& to_add, bool in) {
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& current_set = (in ? live_in : live_out);
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& opposite_set = (!in ? live_in : live_out);
bool inserted = false;
for (const auto& byNew : to_add)
{
const set<SAPFOR::BasicBlock*>& add_in_live = byNew.second;
set<SAPFOR::BasicBlock*> new_in_live = {};
const vector<SAPFOR::BasicBlock*>& add_in_live = byNew.second;
vector<SAPFOR::BasicBlock*> new_in_live = {};
auto current_set_iter = current_set.find(byNew.first);
@@ -128,14 +128,19 @@ namespace SAPFOR
return removed;
};
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> BasicBlock::getLive(bool in) const {
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> BasicBlock::getLive(bool in) const {
auto& current_set = in ? live_in : live_out;
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> res;
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> res;
for (auto& by_source : { current_set, live_inout })
{
for (auto& by_pair : by_source)
res[by_pair.first].insert(by_pair.second.begin(), by_pair.second.end());
{
auto& dest = res[by_pair.first];
dest.insert(dest.end(), by_pair.second.begin(), by_pair.second.end());
}
}
return res;
}
@@ -219,31 +224,31 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use,
vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls,
const map<string, FuncInfo*>& funcByName, bool interprocedural);
class LiveVarAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>> {
class LiveVarAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>> {
private:
set<SAPFOR::Argument*> live, dead;
public:
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getIn()
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn()
{
return getBlock()->getLiveOut();
};
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getOut()
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut()
{
return getBlock()->getLiveIn();
};
bool addIn(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& data)
bool addIn(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
return getBlock()->addLiveOut(data);
};
bool addOut(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& data)
bool addOut(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
return getBlock()->addLiveIn(data);
};
bool forwardData(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& data)
bool forwardData(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
bool inserted = false;
@@ -266,7 +271,7 @@ public:
}
};
class LiveVarAnalysis : public BackwardDataFlowAnalysis<map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>, LiveVarAnalysisNode> {
class LiveVarAnalysis : public BackwardDataFlowAnalysis<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>, LiveVarAnalysisNode> {
protected:
vector<SAPFOR::Argument*>& formal_parameters;
vector<fcall>& fcalls;
@@ -694,10 +699,13 @@ void runLiveVariableAnalysis(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>&
{
for (const auto& byArg : live_after)
{
if (exit->addIn({ byArg }))
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> converted;
converted[byArg.first] = vector<SAPFOR::BasicBlock*>(byArg.second.begin(), byArg.second.end());
if (exit->addIn(converted))
{
exit->setInCnt(max_cnt);
if (exit->forwardData({ byArg }))
if (exit->forwardData(converted))
exit->setOutCnt(max_cnt);
}
}

View File

@@ -126,7 +126,7 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use,
}
class DeadCodeAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>> {
class DeadCodeAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>> {
private:
vector<bool> useful;
bool useful_block = false;
@@ -188,15 +188,15 @@ public:
return updated;
}
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getIn() {
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn() {
return getBlock()->getLiveOut();
};
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getOut() {
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut() {
return getBlock()->getLiveIn();
};
bool addIn(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& data) {
bool addIn(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) {
bool inserted = getBlock()->addLiveOut(data);
if (!useful_block)
@@ -207,11 +207,11 @@ public:
return inserted;
};
bool addOut(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& data) {
bool addOut(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) {
return getBlock()->addLiveIn(data);
};
bool forwardData(const map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>& data) {
bool forwardData(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) {
bool inserted = false;
SAPFOR::BasicBlock* bb= getBlock();
@@ -298,7 +298,7 @@ public:
const vector<bool>& getResult() { return useful; };
};
class DeadCodeAnalysis : public BackwardDataFlowAnalysis<map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>>, DeadCodeAnalysisNode> {
class DeadCodeAnalysis : public BackwardDataFlowAnalysis<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>, DeadCodeAnalysisNode> {
protected:
vector<SAPFOR::Argument*>& formal_parameters;