From 1ac7fcca2affd230d1c4ca9d7d1d91b39edbd8be Mon Sep 17 00:00:00 2001 From: Mikhail Kocharmin Date: Wed, 3 Apr 2024 21:13:56 +0300 Subject: [PATCH] fixes for dead code removing pass --- .../_src/CFGraph/DataFlow/data_flow.h | 1 + .../_src/CFGraph/DataFlow/data_flow_impl.h | 6 ++-- .../_src/Transformations/dead_code.cpp | 33 +++++++++++-------- .../Sapfor_2017/_src/Utils/PassManager.h | 4 +-- 4 files changed, 26 insertions(+), 18 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 8067bd7..561d1b3 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h @@ -29,6 +29,7 @@ public: virtual bool addIn(const DataType& data) = 0; virtual bool addOut(const DataType& data) = 0; + virtual bool updateState() { return false; } virtual bool forwardData(const DataType& data) = 0; bool newerThan(const DataFlowAnalysisNode* block) const { return out_cnt > block->in_cnt; } 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 5e74e29..f45b2d7 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 @@ -27,6 +27,8 @@ template void DataFlowAnalysisNode::doStep() { int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT; + + bool uniq_change = updateState(); for (auto next : prev_blocks) { if (in_cnt < next->out_cnt) @@ -49,7 +51,7 @@ void DataFlowAnalysisNode::doStep() } } - bool was_notinit = (out_cnt == CNT_NOTINIT); + uniq_change |= (out_cnt == CNT_NOTINIT); if (out_max_cnt != CNT_NOTINIT) out_cnt = out_max_cnt; @@ -58,7 +60,7 @@ void DataFlowAnalysisNode::doStep() in_cnt = in_max_cnt; // TODO: fix counter overflow - if (was_notinit) + if (uniq_change) { out_cnt++; in_cnt++; diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index f7493d2..089ca88 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -39,7 +39,9 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru { for (SAPFOR::Argument* r : res) { - if (use.find(r) != use.end() || r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_) + if (use.find(r) != use.end() || + r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_ || + r->getType() != SAPFOR::CFG_ARG_TYPE::VAR) { useful = true; break; @@ -88,7 +90,7 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru use.insert(e); def.erase(e); } - + insertIfVar(args.begin(), args.end(), usedByThisBlock); } @@ -207,11 +209,6 @@ public: bool addIn(const map>& data) { bool inserted = getBlock()->addLiveOut(data); - if (!useful_block) - inserted |= updateNextNotEmpty(); - - inserted |= updateJumpStatus(); - return inserted; } @@ -219,6 +216,21 @@ public: return getBlock()->addLiveIn(data); } + bool updateState() { + if(getBlock()->getInstructions()[0]->getInstruction()->getOperator()->lineNumber() == 58) + int b = 51; + + bool updated = false; + + if (!useful_block) + updated |= updateNextNotEmpty(); + + updated |= updateJumpStatus(); + updated |= this->forwardData({ }); + + return updated; + } + bool forwardData(const map>& data) { bool inserted = false; SAPFOR::BasicBlock* bb= getBlock(); @@ -298,13 +310,6 @@ public: { setBlock(block); useful.resize(block->getInstructions().size(), false); - set use, def; - set usedByThisBlock; - - buildUseDef(getBlock(), use, def, this->formal_parameters, useful, usedByThisBlock, funcByName); - - for (SAPFOR::Argument* arg : use) - getBlock()->addLiveIn({ { arg, { getBlock() } } }); } const vector& getResult() { return useful; } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 0e07092..0f2e0fe 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -307,8 +307,8 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM); - Pass(CALL_GRAPH2) <= Pass(REMOVE_DEAD_CODE); - list({ REMOVE_DEAD_CODE, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); + list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD }) <= Pass(REMOVE_DEAD_CODE); + list({ REMOVE_DEAD_CODE, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS, EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW,