dead_code_removing (Новый проход) #21

Merged
Alexander_KS merged 6 commits from dead_code_removing into master 2024-01-12 18:45:36 +00:00
3 changed files with 36 additions and 28 deletions
Showing only changes of commit bd8690d54a - Show all commits

View File

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

View File

@@ -21,15 +21,15 @@ 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*, 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*>>& current_set = (in ? live_in : live_out);
std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& opposite_set = (!in ? live_in : live_out); std::map<SAPFOR::Argument*, std::vector<SAPFOR::BasicBlock*>>& opposite_set = (!in ? live_in : live_out);
bool inserted = false; bool inserted = false;
for (const auto& byNew : to_add) for (const auto& byNew : to_add)
{ {
const set<SAPFOR::BasicBlock*>& add_in_live = byNew.second; const vector<SAPFOR::BasicBlock*>& add_in_live = byNew.second;
set<SAPFOR::BasicBlock*> new_in_live = {}; vector<SAPFOR::BasicBlock*> new_in_live = {};
auto current_set_iter = current_set.find(byNew.first); auto current_set_iter = current_set.find(byNew.first);
@@ -128,14 +128,19 @@ namespace SAPFOR
return removed; 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; 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_source : { current_set, live_inout })
{
for (auto& by_pair : by_source) 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; return res;
} }
@@ -219,31 +224,31 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use,
vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls, vector<SAPFOR::Argument*>& formal_parameters, vector<fcall>& fcalls,
const map<string, FuncInfo*>& funcByName, bool interprocedural); 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: private:
set<SAPFOR::Argument*> live, dead; set<SAPFOR::Argument*> live, dead;
public: public:
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getIn() map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn()
{ {
return getBlock()->getLiveOut(); return getBlock()->getLiveOut();
}; };
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getOut() map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut()
{ {
return getBlock()->getLiveIn(); 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); 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); 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; 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: protected:
vector<SAPFOR::Argument*>& formal_parameters; vector<SAPFOR::Argument*>& formal_parameters;
vector<fcall>& fcalls; vector<fcall>& fcalls;
@@ -694,10 +699,13 @@ void runLiveVariableAnalysis(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>&
{ {
for (const auto& byArg : live_after) 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); exit->setInCnt(max_cnt);
if (exit->forwardData({ byArg })) if (exit->forwardData(converted))
exit->setOutCnt(max_cnt); 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: private:
vector<bool> useful; vector<bool> useful;
bool useful_block = false; bool useful_block = false;
@@ -188,15 +188,15 @@ public:
return updated; return updated;
} }
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getIn() { map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn() {
return getBlock()->getLiveOut(); return getBlock()->getLiveOut();
}; };
map<SAPFOR::Argument*, set<SAPFOR::BasicBlock*>> getOut() { map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut() {
return getBlock()->getLiveIn(); 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); bool inserted = getBlock()->addLiveOut(data);
if (!useful_block) if (!useful_block)
@@ -207,11 +207,11 @@ public:
return inserted; 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); 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; bool inserted = false;
SAPFOR::BasicBlock* bb= getBlock(); SAPFOR::BasicBlock* bb= getBlock();
@@ -298,7 +298,7 @@ public:
const vector<bool>& getResult() { return useful; }; 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: protected:
vector<SAPFOR::Argument*>& formal_parameters; vector<SAPFOR::Argument*>& formal_parameters;