private_removing: add dead code removing #31
@@ -1106,11 +1106,13 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
|
||||
{
|
||||
auto itFound = loopGraph.find(file->filename());
|
||||
if (itFound != loopGraph.end())
|
||||
removePrivatesAnalysis(itFound->second, getObjectForFileFromMap(file_name, SPF_messages), usersDirectives, commonBlocks, allFuncInfo);
|
||||
removePrivatesAnalysis(file_name, itFound->second, getObjectForFileFromMap(file_name, SPF_messages),
|
||||
usersDirectives, commonBlocks, allFuncInfo);
|
||||
}
|
||||
else if (curr_regime == PRIVATE_REMOVING)
|
||||
{
|
||||
removePrivates(file, getObjectForFileFromMap(file_name, SPF_messages), commonBlocks, allFuncInfo, countOfTransform);
|
||||
removePrivates(file_name, getObjectForFileFromMap(file_name, SPF_messages),
|
||||
commonBlocks, allFuncInfo, countOfTransform);
|
||||
}
|
||||
else if (curr_regime == CREATE_INTER_TREE)
|
||||
{
|
||||
@@ -2498,15 +2500,8 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam
|
||||
case REMOVE_OMP_DIRS_TRANSFORM:
|
||||
case REMOVE_COMMENTS:
|
||||
case INSERT_NO_DISTR_FLAGS_FROM_GUI:
|
||||
runAnalysis(*project, curr_regime, true, "", folderName);
|
||||
break;
|
||||
case PRIVATE_REMOVING:
|
||||
runAnalysis(*project, curr_regime, false, "", folderName);
|
||||
runPass(REVERT_SUBST_EXPR_RD, proj_name, folderName);
|
||||
if (folderName)
|
||||
runAnalysis(*project, UNPARSE_FILE, true, "", folderName);
|
||||
else
|
||||
__spf_print(1, "can not run UNPARSE_FILE - folder name is null\n");
|
||||
runAnalysis(*project, curr_regime, true, "", folderName);
|
||||
break;
|
||||
case INLINE_PROCEDURES:
|
||||
runAnalysis(*project, curr_regime, false);
|
||||
|
||||
@@ -599,38 +599,39 @@ static bool isVarChangedBetween(string var, SgStatement* first, SgStatement* sec
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: remove if needless
|
||||
// removeDeadCodeFromLoop removes assign statements to private scalar vars which are not read in loop
|
||||
static void removeDeadCodeFromLoop(LoopGraph* loop)
|
||||
{
|
||||
SgForStmt* loopStmt = (SgForStmt*) loop->loop->GetOriginal();
|
||||
set<Symbol*> privateVars;
|
||||
for (auto data : getAttributes<SgStatement*, SgStatement*>(loopStmt, set<int>{ SPF_ANALYSIS_DIR }))
|
||||
fillPrivatesFromComment(new Statement(data), privateVars);
|
||||
|
||||
set<string> privates;
|
||||
for (Symbol* symbol : privateVars)
|
||||
privates.insert(OriginalSymbol((SgSymbol*)symbol)->identifier());
|
||||
|
||||
vector<SgStatement*> stmtsToDelete;
|
||||
for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext())
|
||||
{
|
||||
if (st->variant() != ASSIGN_STAT)
|
||||
continue;
|
||||
|
||||
SgSymbol* var = st->expr(0)->symbol();
|
||||
if (var == nullptr || var->variant() != VARIABLE_NAME)
|
||||
continue;
|
||||
|
||||
if (privates.find(var->identifier()) != privates.end() && !isVarReadInLoop(var, loopStmt))
|
||||
stmtsToDelete.push_back(st);
|
||||
}
|
||||
|
||||
for (auto stmt : stmtsToDelete)
|
||||
stmt->deleteStmt();
|
||||
|
||||
for (auto childLoop : loop->children)
|
||||
removeDeadCodeFromLoop(childLoop);
|
||||
}
|
||||
//static void removeDeadCodeFromLoop(LoopGraph* loop)
|
||||
//{
|
||||
// SgForStmt* loopStmt = (SgForStmt*) loop->loop->GetOriginal();
|
||||
// set<Symbol*> privateVars;
|
||||
// for (auto data : getAttributes<SgStatement*, SgStatement*>(loopStmt, set<int>{ SPF_ANALYSIS_DIR }))
|
||||
// fillPrivatesFromComment(new Statement(data), privateVars);
|
||||
//
|
||||
// set<string> privates;
|
||||
// for (Symbol* symbol : privateVars)
|
||||
// privates.insert(OriginalSymbol((SgSymbol*)symbol)->identifier());
|
||||
//
|
||||
// vector<SgStatement*> stmtsToDelete;
|
||||
// for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext())
|
||||
// {
|
||||
// if (st->variant() != ASSIGN_STAT)
|
||||
// continue;
|
||||
//
|
||||
// SgSymbol* var = st->expr(0)->symbol();
|
||||
// if (var == nullptr || var->variant() != VARIABLE_NAME)
|
||||
// continue;
|
||||
//
|
||||
// if (privates.find(var->identifier()) != privates.end() && !isVarReadInLoop(var, loopStmt))
|
||||
// stmtsToDelete.push_back(st);
|
||||
// }
|
||||
//
|
||||
// for (auto stmt : stmtsToDelete)
|
||||
// stmt->deleteStmt();
|
||||
//
|
||||
// for (auto childLoop : loop->children)
|
||||
// removeDeadCodeFromLoop(childLoop);
|
||||
//}
|
||||
|
||||
// fillReadShortFixedSumscripts fills all short fixed subscripts vectors of array var,
|
||||
// which are used for reading from array var in exp
|
||||
@@ -697,30 +698,31 @@ static void removeExcessiveDefs(const PrivateToRemove& var)
|
||||
st->deleteStmt();
|
||||
}
|
||||
|
||||
// TODO: remove is needless
|
||||
// removeEmptyLoops removes loops with empty body and create messages
|
||||
static void removeEmptyLoops(LoopGraph* loop, vector<Messages>& messages)
|
||||
{
|
||||
vector<LoopGraph*> loopsToDelete;
|
||||
vector<LoopGraph*> newChildrenVector;
|
||||
for (auto childLoop : loop->children)
|
||||
{
|
||||
SgStatement* loopStmt = childLoop->loop->GetOriginal();
|
||||
if (loopStmt->lastNodeOfStmt() == loopStmt->lexNext())
|
||||
loopsToDelete.push_back(childLoop);
|
||||
else
|
||||
newChildrenVector.push_back(childLoop);
|
||||
}
|
||||
|
||||
for (auto loopToDelete : loopsToDelete)
|
||||
{
|
||||
addMessageRemoveLoop(messages, loopToDelete->lineNum);
|
||||
loopToDelete->loop->extractStmt();
|
||||
}
|
||||
|
||||
loop->children.swap(newChildrenVector);
|
||||
for (auto childLoop : loop->children)
|
||||
removeEmptyLoops(childLoop, messages);
|
||||
}
|
||||
//static void removeEmptyLoops(LoopGraph* loop, vector<Messages>& messages)
|
||||
//{
|
||||
// vector<LoopGraph*> loopsToDelete;
|
||||
// vector<LoopGraph*> newChildrenVector;
|
||||
// for (auto childLoop : loop->children)
|
||||
// {
|
||||
// SgStatement* loopStmt = childLoop->loop->GetOriginal();
|
||||
// if (loopStmt->lastNodeOfStmt() == loopStmt->lexNext())
|
||||
// loopsToDelete.push_back(childLoop);
|
||||
// else
|
||||
// newChildrenVector.push_back(childLoop);
|
||||
// }
|
||||
//
|
||||
// for (auto loopToDelete : loopsToDelete)
|
||||
// {
|
||||
// addMessageRemoveLoop(messages, loopToDelete->lineNum);
|
||||
// loopToDelete->loop->extractStmt();
|
||||
// }
|
||||
//
|
||||
// loop->children.swap(newChildrenVector);
|
||||
// for (auto childLoop : loop->children)
|
||||
// removeEmptyLoops(childLoop, messages);
|
||||
//}
|
||||
|
||||
// removeVarFromPrivateAttributes removes var from SPF ANALYSIS PRIVATE attributes of loop
|
||||
static void removeVarFromPrivateAttributes(SgSymbol* var, LoopGraph* loop)
|
||||
@@ -820,39 +822,35 @@ static set<vector<int>> removeArray(string filename, const PrivateToRemove& arra
|
||||
|
||||
SgExpression* substExp = substituteExpressions(useStmt->expr(i), refToExpMap);
|
||||
useStmt->setExpression(i, *substExp);
|
||||
|
||||
// don't revert substitutions in useStmt:
|
||||
cancelRevertionForStatement(filename, useStmt, i);
|
||||
}
|
||||
}
|
||||
|
||||
return removedFixedSubscripts;
|
||||
}
|
||||
|
||||
void removePrivates(SgFile* file, vector<Messages>& messages,
|
||||
void removePrivates(string filename, vector<Messages>& messages,
|
||||
const map<string, CommonBlock*>& commonBlocks,
|
||||
const map<string, vector<FuncInfo*>>& allFuncInfo,
|
||||
int& countOfTransform)
|
||||
{
|
||||
for (auto& varToRemove : privatesToRemoveGlobal)
|
||||
{
|
||||
if (string(file->filename()) != varToRemove.loop->fileName)
|
||||
if (filename != varToRemove.loop->fileName)
|
||||
continue;
|
||||
|
||||
auto removedDimensions = removeArray(file->filename(), varToRemove);
|
||||
auto removedDimensions = removeArray(filename, varToRemove);
|
||||
countOfTransform++;
|
||||
|
||||
//removeDeadCodeFromLoop(varToRemove.loop); // TODO: problem with reverting substitution
|
||||
//removeDeadCodeFromLoop(varToRemove.loop);
|
||||
removeExcessiveDefs(varToRemove);
|
||||
removeEmptyLoops(varToRemove.loop, messages); // removing is made by REMOVE_DEAD_CODE pass
|
||||
//removeEmptyLoops(varToRemove.loop, messages);
|
||||
|
||||
SgForStmt* loopStmt = (SgForStmt*)varToRemove.loop->loop->GetOriginal();
|
||||
FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo);
|
||||
if (currFunc == nullptr)
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
|
||||
// TODO: problem with removing dead code before reverting substitution
|
||||
//removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks);
|
||||
removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks);
|
||||
|
||||
vector<SgArrayRefExp*> varRefs = getDirectArrayRefs(loopStmt, varToRemove.varSymbol);
|
||||
int loopLineNum = varToRemove.loop->lineNum;
|
||||
@@ -904,6 +902,7 @@ struct Context {
|
||||
const map<string, vector<FuncInfo*>>& allFuncInfo;
|
||||
const map<string, CommonBlock*>& commonBlocks;
|
||||
vector<Messages>& messages;
|
||||
string filename;
|
||||
|
||||
Regime regime;
|
||||
LoopGraph* loop;
|
||||
@@ -2083,9 +2082,19 @@ void removePrivateAnalyze(Context *ctx)
|
||||
{
|
||||
vector<DefUseStmtsPair> resultDefUsePairs;
|
||||
for (auto& pair : defUseStmtsPairs)
|
||||
{
|
||||
if (checkDefUsePair(ctx, pair, CFG_ForFunc))
|
||||
{
|
||||
resultDefUsePairs.push_back(pair);
|
||||
|
||||
// don't revert substitutions in use and def stmts:
|
||||
cancelRevertionForStatement(ctx->filename, pair.first, 1);
|
||||
cancelRevertionForStatement(ctx->filename, pair.second, 0);
|
||||
cancelRevertionForStatement(ctx->filename, pair.second, 1);
|
||||
cancelRevertionForStatement(ctx->filename, pair.second, 2);
|
||||
}
|
||||
}
|
||||
|
||||
PrivateToRemove newPrivateToRemove;
|
||||
newPrivateToRemove.loop = ctx->loop;
|
||||
newPrivateToRemove.varSymbol = ctx->arraySymbol;
|
||||
@@ -2104,7 +2113,8 @@ void removePrivateAnalyze(Context *ctx)
|
||||
deleteCFG(CFG_ForFunc);
|
||||
}
|
||||
|
||||
void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs,
|
||||
void removePrivatesAnalysis(string filename,
|
||||
vector<LoopGraph*>& loopGraphs,
|
||||
vector<Messages>& messages,
|
||||
const UsersDirectives& usersDirectives,
|
||||
const map<string, CommonBlock*>& commonBlocks,
|
||||
@@ -2151,7 +2161,7 @@ void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs,
|
||||
if (arrayToRemove == nullptr) // no array to remove
|
||||
break;
|
||||
|
||||
Context context = Context{allFuncInfo, commonBlocks, messages};
|
||||
Context context = Context{allFuncInfo, commonBlocks, messages, filename};
|
||||
context.regime = Regime::DEFLT;
|
||||
context.loop = loop;
|
||||
context.loopStmt = loopStmt;
|
||||
@@ -2173,9 +2183,7 @@ void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs,
|
||||
{
|
||||
removePrivateAnalyze(&context);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (checkRegularIndexRefs(&context))
|
||||
else if (checkRegularIndexRefs(&context))
|
||||
{
|
||||
context.regime = Regime::REGULAR_INDEXES;
|
||||
context.fixedDimensionsMask = vector<bool>{};
|
||||
@@ -2185,8 +2193,7 @@ void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs,
|
||||
addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(), context.loop->lineNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (LoopGraph* loop : loopGraphs)
|
||||
removePrivatesAnalysis(loop->children, messages, usersDirectives, commonBlocks, allFuncInfo);
|
||||
removePrivatesAnalysis(filename, loop->children, messages, usersDirectives, commonBlocks, allFuncInfo);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ struct PrivateToRemove {
|
||||
|
||||
// removePrivates removes all privates from vector privatesToRemoveGloval
|
||||
// and add info messages
|
||||
void removePrivates(SgFile* file, std::vector<Messages>& messages,
|
||||
void removePrivates(std::string filename, std::vector<Messages>& messages,
|
||||
const std::map<std::string, CommonBlock*>& commonBlocks,
|
||||
const std::map<std::string, std::vector<FuncInfo*>>& allFuncInfo,
|
||||
int& countOfTransform);
|
||||
@@ -30,7 +30,8 @@ void removePrivates(SgFile* file, std::vector<Messages>& messages,
|
||||
// removePrivatesAnalysis checks all private variables in loopGraphs specified by usersDirectives
|
||||
// if they can be removed, and adds those that can to vector privatesToRemoveGloval.
|
||||
// if private var cannot be removed, the error message is returned with vector messages
|
||||
void removePrivatesAnalysis(std::vector<LoopGraph*>& loopGraphs,
|
||||
void removePrivatesAnalysis(std::string filename,
|
||||
std::vector<LoopGraph*>& loopGraphs,
|
||||
std::vector<Messages>& messages,
|
||||
const std::map<std::pair<std::string, int>, std::set<SgStatement*>>& usersDirectives,
|
||||
const std::map<std::string, CommonBlock*>& commonBlocks,
|
||||
|
||||
@@ -212,7 +212,7 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
|
||||
|
||||
Pass(BUILD_IR) <= Pass(SUBST_EXPR_RD) <= Pass(SUBST_EXPR_RD_AND_UNPARSE);
|
||||
|
||||
list({ LOOP_ANALYZER_DATA_DIST_S1, SUBST_EXPR_RD } ) <= Pass(PRIVATE_REMOVING_ANALYSIS) <= Pass(PRIVATE_REMOVING);
|
||||
list({ LOOP_ANALYZER_DATA_DIST_S1, SUBST_EXPR_RD } ) <= Pass(PRIVATE_REMOVING_ANALYSIS) <= Pass(REVERT_SUBST_EXPR_RD) <= Pass(PRIVATE_REMOVING);
|
||||
|
||||
Pass(RESTORE_LOOP_FROM_ASSIGN) <= list({ SUBST_EXPR_AND_UNPARSE, SUBST_EXPR_RD_AND_UNPARSE });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user