diff --git a/src/CFGraph/CFGraph.cpp b/src/CFGraph/CFGraph.cpp index e27590e..a67c4b4 100644 --- a/src/CFGraph/CFGraph.cpp +++ b/src/CFGraph/CFGraph.cpp @@ -1254,4 +1254,49 @@ map> fileFuncInfoMap[callFrom->fileName].push_back(callFrom); return buildCFG(commonBlocks, fileFuncInfoMap, settings); +} + + +void removedUnreachableBlocks(vector& blocks) +{ + set reachable; + + for (auto& b : blocks) + if (b->getInstructions().front()->isHeader() || + b->getInstructions().front()->getInstruction()->getOperation() == SAPFOR::CFG_OP::ENTRY) + reachable.insert(b); + + set worklist = reachable; + while (worklist.size() != 0) + { + set to_insert; + + for (auto& b : worklist) + for (auto& next : b->getNext()) + if (reachable.insert(next).second) + to_insert.insert(next); + + worklist = to_insert; + } + + auto remove_unreachable_it = remove_if(blocks.begin(), blocks.end(), + [&reachable](SAPFOR::BasicBlock* b) + { + if (reachable.find(b) == reachable.end()) + { + for (auto& next : b->getNext()) + if (reachable.find(next) != reachable.end()) + next->removePrev(b); + + delete b; + return true; + } + + return false; + } + ); + + reachable.clear(); + + blocks.erase(remove_unreachable_it, blocks.end()); } \ No newline at end of file diff --git a/src/CFGraph/CFGraph.h b/src/CFGraph/CFGraph.h index c828c12..3c7df16 100644 --- a/src/CFGraph/CFGraph.h +++ b/src/CFGraph/CFGraph.h @@ -161,3 +161,5 @@ static inline void deleteCFG(std::map& blocks); diff --git a/src/Transformations/DeadCodeRemoving/dead_code.cpp b/src/Transformations/DeadCodeRemoving/dead_code.cpp index c2b8901..75e218c 100644 --- a/src/Transformations/DeadCodeRemoving/dead_code.cpp +++ b/src/Transformations/DeadCodeRemoving/dead_code.cpp @@ -6,6 +6,8 @@ #include #include +#include "../../CFGraph/CFGraph.h" + using std::map; using std::string; using std::vector; @@ -88,13 +90,13 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru instr_operation == SAPFOR::CFG_OP::STORE) last_stack_op_useful = true; - for (auto e : res) + for (auto& e : res) { def.insert(e); use.erase(e); } - for (auto e : args) + for (auto& e : args) { use.insert(e); def.erase(e); @@ -164,7 +166,7 @@ public: { const auto& example = dynamic_cast(*prev.begin())->next_notempty_out; - for (auto p : prev) + for (auto& p : prev) { DeadCodeAnalysisNode* next = dynamic_cast(p); @@ -188,7 +190,7 @@ public: { set current = {}; - for (auto nextP : getPrevBlocks()) + for (auto& nextP : getPrevBlocks()) { DeadCodeAnalysisNode* next = dynamic_cast(nextP); @@ -428,49 +430,7 @@ int removeDeadCode(SgStatement* func, printInternalError(convertFileName(__FILE__).c_str(), __LINE__); auto& cfg_pair = *(cfg.begin()); - - // delete unreachable blocks - - set reachable; - - for (auto b : cfg_pair.second) - if(b->getInstructions().front()->isHeader() || - b->getInstructions().front()->getInstruction()->getOperation() == SAPFOR::CFG_OP::ENTRY) - reachable.insert(b); - - set worklist = reachable; - while (worklist.size() != 0) - { - set to_insert; - - for(auto b : worklist) - for(auto next: b->getNext()) - if(reachable.insert(next).second) - to_insert.insert(next); - - worklist = to_insert; - } - - auto remove_unreachable_it = remove_if(cfg_pair.second.begin(), cfg_pair.second.end(), - [&reachable](SAPFOR::BasicBlock* b) - { - if (reachable.find(b) == reachable.end()) - { - for(auto next: b->getNext()) - if(reachable.find(next) != reachable.end()) - next->removePrev(b); - - delete b; - return true; - } - - return false; - } - ); - - reachable.clear(); - - cfg_pair.second.erase(remove_unreachable_it, cfg_pair.second.end()); + removedUnreachableBlocks(cfg_pair.second); #if DEBUG_IR dumpCFG({ cfg_pair }, false); @@ -481,7 +441,7 @@ int removeDeadCode(SgStatement* func, map funcByName; for (auto& byFile : allFuncs) - for (auto byFunc : byFile.second) + for (auto& byFunc : byFile.second) funcByName[byFunc->funcName] = byFunc; DeadCodeAnalysis analysis_object(func_parameters, funcByName); diff --git a/src/Utils/version.h b/src/Utils/version.h index 7465e56..092925c 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2429" +#define VERSION_SPF "2430"