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 51b4d14..5f79f52 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -95,7 +95,7 @@ namespace SAPFOR } return inserted; - }; + } bool BasicBlock::removeLive(SAPFOR::Argument* to_remove, bool in) { @@ -126,7 +126,7 @@ namespace SAPFOR } return removed; - }; + } map> BasicBlock::getLive(bool in) const { auto& current_set = in ? live_in : live_out; @@ -231,22 +231,22 @@ public: map> getIn() { return getBlock()->getLiveOut(); - }; + } map> getOut() { return getBlock()->getLiveIn(); - }; + } bool addIn(const map>& data) { return getBlock()->addLiveOut(data); - }; + } bool addOut(const map>& data) { return getBlock()->addLiveIn(data); - }; + } bool forwardData(const map>& data) { @@ -257,7 +257,7 @@ public: inserted |= getBlock()->addLiveIn({ byArg }); return inserted; - }; + } LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, vector& fcalls, const map& funcByName) @@ -279,11 +279,11 @@ protected: LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName); - }; + } public: LiveVarAnalysis(vector& formal_parameters, vector& fcalls, const map& funcByName) : formal_parameters(formal_parameters), fcalls(fcalls), funcByName(funcByName) - { }; + { } }; void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h index d4a2c6a..7e6cd73 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h @@ -37,7 +37,7 @@ void insertIfVar(IT begin, IT end, DEST& to) { for (auto it = begin; it != end; it++) if (*it && (*it)->getType() == SAPFOR::CFG_ARG_TYPE::VAR) to.insert(*it); -}; +} void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, std::set& use, std::set& def, diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 8cb4b18..01c76f8 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -10,6 +10,8 @@ using std::string; using std::vector; using std::set; +#define PRINT_USELESS_STATEMENTS 1 + static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, set& use, set& def, vector& formal_parameters, @@ -190,11 +192,11 @@ public: map> getIn() { return getBlock()->getLiveOut(); - }; + } map> getOut() { return getBlock()->getLiveIn(); - }; + } bool addIn(const map>& data) { bool inserted = getBlock()->addLiveOut(data); @@ -205,11 +207,11 @@ public: inserted |= updateJumpStatus(); return inserted; - }; + } bool addOut(const map>& data) { return getBlock()->addLiveIn(data); - }; + } bool forwardData(const map>& data) { bool inserted = false; @@ -279,7 +281,7 @@ public: } return inserted; - }; + } DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters) : formal_parameters(formal_parameters) @@ -295,7 +297,7 @@ public: getBlock()->addLiveIn({ { arg, { getBlock() } } }); } - const vector& getResult() { return useful; }; + const vector& getResult() { return useful; } }; class DeadCodeAnalysis : public BackwardDataFlowAnalysis { @@ -304,11 +306,11 @@ protected: DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { return new DeadCodeAnalysisNode(block, formal_parameters); - }; + } public: DeadCodeAnalysis(vector& formal_parameters) : formal_parameters(formal_parameters) - { }; + { } }; @@ -318,12 +320,6 @@ void removeDeadCode(SgStatement* func, { SgProgHedrStmt* prog = isSgProgHedrStmt(func); - if (prog) - __spf_print(1, "[analyze %s]\n", prog->name().identifier()); - else - __spf_print(1, "[cannot resolve name of function]\n"); - - auto cfg = buildCFGforCurrentFunc(func, SAPFOR::CFG_Settings(true, false, false, false, false, false, false), commonBlocks, allFuncs); if(cfg.size() != 1) @@ -333,14 +329,14 @@ void removeDeadCode(SgStatement* func, vector func_parameters(cfg_pair.first->funcParams.countOfPars, NULL); - DeadCodeAnalysis* analysis_object = new DeadCodeAnalysis(func_parameters); + DeadCodeAnalysis analysis_object(func_parameters); - analysis_object->fit(cfg_pair.second); - analysis_object->analyze(); + analysis_object.fit(cfg_pair.second); + analysis_object.analyze(); set useful; - for (DeadCodeAnalysisNode* byNode : analysis_object->getNodes()) + for (DeadCodeAnalysisNode* byNode : analysis_object.getNodes()) { const auto& instructions = byNode->getBlock()->getInstructions(); const auto& statuses = byNode->getResult(); @@ -357,10 +353,7 @@ void removeDeadCode(SgStatement* func, SgStatement* stmt = instructions[i]->getInstruction()->getOperator(); while(stmt && useful.insert(stmt).second) - { - __spf_print(1, "[Useful statement '%s' on line %d]\n", stmt->unparse(), stmt->lineNumber()); stmt = stmt->controlParent(); - } } } } @@ -372,65 +365,50 @@ void removeDeadCode(SgStatement* func, ASSIGN_STAT }; - SgStatement* enclosing = st->controlParent(); - SgStatement* encl_end = enclosing ? enclosing->lastNodeOfStmt() : NULL; - while (st != end) { SgStatement* next = st->lexNext(); - SgStatement* parent = NULL; if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end()) { - __spf_print(1, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); + __spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); - parent = st->controlParent(); - - st->extractStmt()->deleteStmt(); - st = NULL; + st->deleteStmt(); } else { - if (st == encl_end) + if (isSgControlEndStmt(st)) { - if (enclosing) + SgStatement* parent = st->controlParent(); + SgStatement* parent_end; + + if (parent && (parent_end = parent->lastNodeOfStmt()) && parent_end == st) { bool empty_parent = false; - switch (enclosing->variant()) + switch (parent->variant()) { - case IF_NODE: - empty_parent = - enclosing->lexNext() == encl_end || // IF THEN ENDIF - enclosing->lexNext()->variant() == CONTROL_END && - enclosing->lexNext()->lexNext() == encl_end; // IF THEN ELSE ENDIF - break; - default: - empty_parent = enclosing->lexNext() == encl_end; - break; + case IF_NODE: + empty_parent = + parent->lexNext() == parent_end || // IF THEN ENDIF + isSgControlEndStmt(parent->lexNext()) && + parent->lexNext()->lexNext() == parent_end; // IF THEN ELSE ENDIF + break; + default: + empty_parent = parent->lexNext() == parent_end; // DO, WHILE + break; } - if(empty_parent) - { - parent = enclosing->controlParent(); - enclosing->extractStmt()->deleteStmt(); - st->extractStmt()->deleteStmt(); - st = NULL; - } + if (empty_parent) + parent->deleteStmt(); + else if(isSgIfStmt(parent) && isSgControlEndStmt(parent_end->lexPrev())) // IF with empty ELSE branch + parent_end->deleteStmt(); } - } } - if(!parent) - parent = st->controlParent(); - - if (parent != enclosing) - { - enclosing = parent; - encl_end = enclosing ? enclosing->lastNodeOfStmt() : NULL; - } - st = next; } + + deleteCFG(cfg); } \ No newline at end of file