dead code: fixed memory leaks, simplified code for empty do/while/if removing, removing of 'else' branch
This commit is contained in:
@@ -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<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
|
||||
vector<SAPFOR::Argument*>& 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<SAPFOR::Argument*> 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<SgStatement*> 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);
|
||||
}
|
||||
Reference in New Issue
Block a user