dead code removing: unreachable code

This commit is contained in:
Михаил Кочармин
2024-01-30 19:40:14 +03:00
parent 00c539eff4
commit 3b4af66720
3 changed files with 71 additions and 1 deletions

View File

@@ -56,6 +56,22 @@ void BBlock::addInstruction(IR_Block* item)
item->setBasicBlock(this); 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() BBlock::~BasicBlock()
{ {
for (auto& instr : instructions) for (auto& instr : instructions)

View File

@@ -42,6 +42,9 @@ namespace SAPFOR
void addInstruction(IR_Block* item); void addInstruction(IR_Block* item);
void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } void addPrev(BasicBlock* prev_) { prev.push_back(prev_); }
void addNext(BasicBlock* next_) { next.push_back(next_); } void addNext(BasicBlock* next_) { next.push_back(next_); }
int removePrev(BasicBlock* removed);
int removeNext(BasicBlock* removed);
void replacePrevNext(const std::map<BasicBlock*, BasicBlock*>& oldToNew) void replacePrevNext(const std::map<BasicBlock*, BasicBlock*>& oldToNew)
{ {

View File

@@ -4,12 +4,15 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <set> #include <set>
#include <algorithm>
using std::map; using std::map;
using std::string; using std::string;
using std::vector; using std::vector;
using std::set; using std::set;
using std::remove_if;
#define PRINT_USELESS_STATEMENTS 0 #define PRINT_USELESS_STATEMENTS 0
static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
@@ -325,7 +328,51 @@ void removeDeadCode(SgStatement* func,
if(cfg.size() != 1) if(cfg.size() != 1)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); 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); 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.fit(cfg_pair.second);
analysis_object.analyze(); analysis_object.analyze();
// detect dead statements
set<SgStatement*> useful; set<SgStatement*> useful;
for (DeadCodeAnalysisNode* byNode : analysis_object.getNodes()) for (DeadCodeAnalysisNode* byNode : analysis_object.getNodes())
@@ -358,6 +407,8 @@ void removeDeadCode(SgStatement* func,
} }
} }
// remove dead statements
SgStatement* end = func->lastNodeOfStmt(), *st = func; SgStatement* end = func->lastNodeOfStmt(), *st = func;
set<int> removable = set<int> removable =