dead code removing: unreachable code
This commit is contained in:
@@ -56,6 +56,22 @@ void BBlock::addInstruction(IR_Block* item)
|
||||
item->setBasicBlock(this);
|
||||
}
|
||||
|
||||
int BBlock::removePrev(BBlock* removed)
|
||||
{
|
||||
auto it = std::remove(prev.begin(), prev.end(), removed);
|
||||
auto r = prev.end() - it;
|
||||
prev.erase(it, prev.end());
|
||||
return r;
|
||||
}
|
||||
|
||||
int BBlock::removeNext(BBlock* removed)
|
||||
{
|
||||
auto it = std::remove(next.begin(), next.end(), removed);
|
||||
auto r = next.end() - it;
|
||||
next.erase(it, next.end());
|
||||
return r;
|
||||
}
|
||||
|
||||
BBlock::~BasicBlock()
|
||||
{
|
||||
for (auto& instr : instructions)
|
||||
|
||||
@@ -43,6 +43,9 @@ namespace SAPFOR
|
||||
void addPrev(BasicBlock* prev_) { prev.push_back(prev_); }
|
||||
void addNext(BasicBlock* next_) { next.push_back(next_); }
|
||||
|
||||
int removePrev(BasicBlock* removed);
|
||||
int removeNext(BasicBlock* removed);
|
||||
|
||||
void replacePrevNext(const std::map<BasicBlock*, BasicBlock*>& oldToNew)
|
||||
{
|
||||
for (int z = 0; z < next.size(); ++z)
|
||||
|
||||
@@ -4,12 +4,15 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using std::map;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::set;
|
||||
|
||||
using std::remove_if;
|
||||
|
||||
#define PRINT_USELESS_STATEMENTS 0
|
||||
|
||||
static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
|
||||
@@ -325,7 +328,51 @@ void removeDeadCode(SgStatement* func,
|
||||
if(cfg.size() != 1)
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
|
||||
const auto& cfg_pair = *(cfg.begin());
|
||||
auto& cfg_pair = *(cfg.begin());
|
||||
|
||||
// delete unreachable blocks
|
||||
|
||||
set<SAPFOR::BasicBlock*> reachable;
|
||||
|
||||
for (auto b : cfg_pair.second)
|
||||
if(b->getInstructions().front()->isHeader())
|
||||
reachable.insert(b);
|
||||
|
||||
set<SAPFOR::BasicBlock*> worklist = reachable;
|
||||
while (worklist.size() != 0)
|
||||
{
|
||||
set<SAPFOR::BasicBlock*> 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());
|
||||
|
||||
// detect useless code
|
||||
|
||||
vector<SAPFOR::Argument*> func_parameters(cfg_pair.first->funcParams.countOfPars, NULL);
|
||||
|
||||
@@ -334,6 +381,8 @@ void removeDeadCode(SgStatement* func,
|
||||
analysis_object.fit(cfg_pair.second);
|
||||
analysis_object.analyze();
|
||||
|
||||
// detect dead statements
|
||||
|
||||
set<SgStatement*> useful;
|
||||
|
||||
for (DeadCodeAnalysisNode* byNode : analysis_object.getNodes())
|
||||
@@ -358,6 +407,8 @@ void removeDeadCode(SgStatement* func,
|
||||
}
|
||||
}
|
||||
|
||||
// remove dead statements
|
||||
|
||||
SgStatement* end = func->lastNodeOfStmt(), *st = func;
|
||||
|
||||
set<int> removable =
|
||||
|
||||
Reference in New Issue
Block a user