From 01016a7b9ff02077b3325c90b29946045c289085 Mon Sep 17 00:00:00 2001 From: Mikhail Kocharmin Date: Wed, 22 May 2024 10:33:58 +0300 Subject: [PATCH 1/2] improve dead code analysis --- .../_src/CFGraph/DataFlow/data_flow.h | 10 ++++-- .../_src/CFGraph/DataFlow/data_flow_impl.h | 20 +++++------ .../_src/CFGraph/live_variable_analysis.cpp | 28 +++++++-------- .../_src/Transformations/dead_code.cpp | 34 +++++++++++-------- 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h index 561d1b3..d43c4af 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h @@ -6,6 +6,12 @@ #include "../CFGraph.h" #include "../IR.h" +enum DATA_FLOW_UPD_STATUS { + NO_CHANGE = 0, + FORWARD, + NEW +}; + template class DataFlowAnalysisNode { static const int CNT_NOTINIT = 0; @@ -13,7 +19,6 @@ class DataFlowAnalysisNode { int in_cnt = CNT_NOTINIT, out_cnt = CNT_NOTINIT; std::set rollback; - std::set ignore_rollback; std::set*> prev_blocks; @@ -30,7 +35,7 @@ public: virtual bool addOut(const DataType& data) = 0; virtual bool updateState() { return false; } - virtual bool forwardData(const DataType& data) = 0; + virtual DATA_FLOW_UPD_STATUS forwardData(const DataType& data) = 0; bool newerThan(const DataFlowAnalysisNode* block) const { return out_cnt > block->in_cnt; } @@ -42,7 +47,6 @@ public: static int getStartCounter() { return CNT_NOTINIT; } std::set& getRollback() { return rollback; } - std::set& getIgnoreRollback() { return ignore_rollback; } std::set*>& getPrevBlocks() { return prev_blocks; } diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h index cb8ae41..b1dd26a 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h @@ -19,7 +19,6 @@ template DataFlowAnalysisNode::DataFlowAnalysisNode() { getRollback() = {}; - getIgnoreRollback() = {}; prev_blocks = {}; } @@ -33,18 +32,21 @@ void DataFlowAnalysisNode::doStep() { if (in_cnt < next->out_cnt) { + if (next->out_cnt > in_max_cnt) + in_max_cnt = next->out_cnt; + const auto& byOut = next->getOut(); bool inserted = addIn( byOut); if (inserted) { - if (next->out_cnt > in_max_cnt) - in_max_cnt = next->out_cnt; - - inserted = forwardData(byOut); + auto status = forwardData(byOut); + inserted = status != DATA_FLOW_UPD_STATUS::NO_CHANGE; if (inserted && next->out_cnt > out_max_cnt) out_max_cnt = next->out_cnt; + + uniq_change |= status == DATA_FLOW_UPD_STATUS::NEW; } } } @@ -81,12 +83,10 @@ void DataFlowAnalysis::analyze() const auto& jumps = curr_bb->getRollback(); if (jumps.size() != 0) { - auto& ignored_jumps = curr_bb->getIgnoreRollback(); - bool jump = false; for (const auto& jump_to : jumps) { - if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(nodes[jump_to])) + if (curr_bb->newerThan(nodes[jump_to])) { jump = true; curr = jump_to; @@ -94,9 +94,7 @@ void DataFlowAnalysis::analyze() } } - if (!jump) - curr_bb->getIgnoreRollback().clear(); - else + if (jump) continue; } diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 841882e..54b5d74 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -107,23 +107,21 @@ namespace SAPFOR removed |= (current_set.erase(to_remove) != 0); - if (!removed) + auto it = live_inout.find(to_remove); + + if (it != live_inout.end()) { - auto it = live_inout.find(to_remove); - - if (it != live_inout.end()) + auto& dest = opposite_set[to_remove]; + for (SAPFOR::BasicBlock* bb : it->second) { - auto& dest = opposite_set[to_remove]; - for (SAPFOR::BasicBlock* bb : it->second) - { - auto find_bb_from_dest = std::lower_bound(dest.begin(), dest.end(), bb); + auto find_bb_from_dest = std::lower_bound(dest.begin(), dest.end(), bb); - if (find_bb_from_dest == dest.end() || *find_bb_from_dest != bb) - dest.insert(find_bb_from_dest, bb); - } - - removed = true; + if (find_bb_from_dest == dest.end() || *find_bb_from_dest != bb) + dest.insert(find_bb_from_dest, bb); } + + live_inout.erase(to_remove); + removed = true; } return removed; @@ -253,7 +251,7 @@ public: return getBlock()->addLiveIn(data); } - bool forwardData(const map>& data) + DATA_FLOW_UPD_STATUS forwardData(const map>& data) { bool inserted = false; @@ -261,7 +259,7 @@ public: if (live.find(byArg.first) == live.end() && dead.find(byArg.first) == dead.end()) inserted |= getBlock()->addLiveIn({ byArg }); - return inserted; + return inserted ? DATA_FLOW_UPD_STATUS::FORWARD : DATA_FLOW_UPD_STATUS::NO_CHANGE; } LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index e80aab6..1e27602 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -235,6 +235,15 @@ public: if (!useful_block) updated |= updateNextNotEmpty(); + if (!useful_block) + { + if (next_notempty_in != next_notempty_out) + { + updated = true; + next_notempty_out = next_notempty_in; + } + } + updated |= updateJumpStatus(); if(updated) @@ -243,9 +252,10 @@ public: return updated; } - bool forwardData(const map>& data) + DATA_FLOW_UPD_STATUS forwardData(const map>& data) { - bool inserted = false; + bool inserted_forward = false, inserted_new = false; + SAPFOR::BasicBlock* bb = getBlock(); set use, def; @@ -271,7 +281,7 @@ public: if (data_it == data.end()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - inserted |= bb->addLiveIn({ *data_it }); + inserted_forward |= bb->addLiveIn({ *data_it }); } else { @@ -285,7 +295,7 @@ public: bb->removeLiveIn(arg); } if(!skip) - inserted |= bb->addLiveIn({ { arg, { bb } } }); + inserted_new |= bb->addLiveIn({ { arg, { bb } } }); } } } @@ -298,23 +308,19 @@ public: { useful_block = true; - inserted = true; + inserted_new = true; next_notempty_out = { this }; break; } } } - if (!useful_block) - { - if (next_notempty_in != next_notempty_out) - { - inserted = true; - next_notempty_out = next_notempty_in; - } - } + if(inserted_new) + return DATA_FLOW_UPD_STATUS::NEW; + else if(inserted_forward) + return DATA_FLOW_UPD_STATUS::FORWARD; - return inserted; + return DATA_FLOW_UPD_STATUS::NO_CHANGE; } DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, From 17de9e271a963d2e2b9de660739c94a40b6e15f8 Mon Sep 17 00:00:00 2001 From: Mikhail Kocharmin Date: Wed, 22 May 2024 20:28:13 +0300 Subject: [PATCH 2/2] rename DATA_FLOW_UPD_STATUS constants --- .../_src/CFGraph/DataFlow/data_flow.h | 6 +----- .../_src/CFGraph/DataFlow/data_flow_impl.h | 2 +- .../_src/CFGraph/live_variable_analysis.cpp | 4 ++-- .../_src/Transformations/dead_code.cpp | 16 ++++++++-------- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h index d43c4af..d58755f 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h @@ -6,11 +6,7 @@ #include "../CFGraph.h" #include "../IR.h" -enum DATA_FLOW_UPD_STATUS { - NO_CHANGE = 0, - FORWARD, - NEW -}; +enum class DATA_FLOW_UPD_STATUS { NO_CHANGE = 0, PROPAGATED, GENERATED }; template class DataFlowAnalysisNode { diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h index b1dd26a..c6a88f6 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h @@ -46,7 +46,7 @@ void DataFlowAnalysisNode::doStep() if (inserted && next->out_cnt > out_max_cnt) out_max_cnt = next->out_cnt; - uniq_change |= status == DATA_FLOW_UPD_STATUS::NEW; + uniq_change |= status == DATA_FLOW_UPD_STATUS::GENERATED; } } } diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 54b5d74..a9d9333 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -259,7 +259,7 @@ public: if (live.find(byArg.first) == live.end() && dead.find(byArg.first) == dead.end()) inserted |= getBlock()->addLiveIn({ byArg }); - return inserted ? DATA_FLOW_UPD_STATUS::FORWARD : DATA_FLOW_UPD_STATUS::NO_CHANGE; + return inserted ? DATA_FLOW_UPD_STATUS::PROPAGATED : DATA_FLOW_UPD_STATUS::NO_CHANGE; } LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, @@ -738,7 +738,7 @@ void runLiveVariableAnalysis(const map>& if (exit->addIn(converted)) { exit->setInCnt(max_cnt); - if (exit->forwardData(converted)) + if (exit->forwardData(converted) != DATA_FLOW_UPD_STATUS::NO_CHANGE) exit->setOutCnt(max_cnt); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 1e27602..c2b8901 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -254,7 +254,7 @@ public: DATA_FLOW_UPD_STATUS forwardData(const map>& data) { - bool inserted_forward = false, inserted_new = false; + bool inserted_prop = false, inserted_gen = false; SAPFOR::BasicBlock* bb = getBlock(); @@ -281,7 +281,7 @@ public: if (data_it == data.end()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - inserted_forward |= bb->addLiveIn({ *data_it }); + inserted_prop |= bb->addLiveIn({ *data_it }); } else { @@ -295,7 +295,7 @@ public: bb->removeLiveIn(arg); } if(!skip) - inserted_new |= bb->addLiveIn({ { arg, { bb } } }); + inserted_gen |= bb->addLiveIn({ { arg, { bb } } }); } } } @@ -308,17 +308,17 @@ public: { useful_block = true; - inserted_new = true; + inserted_gen = true; next_notempty_out = { this }; break; } } } - if(inserted_new) - return DATA_FLOW_UPD_STATUS::NEW; - else if(inserted_forward) - return DATA_FLOW_UPD_STATUS::FORWARD; + if(inserted_gen) + return DATA_FLOW_UPD_STATUS::GENERATED; + else if(inserted_prop) + return DATA_FLOW_UPD_STATUS::PROPAGATED; return DATA_FLOW_UPD_STATUS::NO_CHANGE; }