Merge pull request 'private_removing: update interprocedure analysis' (#38) from private_removing into master

This commit was merged in pull request #38.
This commit is contained in:
2024-04-07 11:12:04 +00:00

View File

@@ -290,20 +290,17 @@ static bool isSymbolInExpression(SgSymbol* symbol, SgExpression* exp)
isSymbolInExpression(symbol, exp->rhs());
}
static FuncInfo* findFunc(string fileName, string funcName, const map<string, vector<FuncInfo*>>& allFuncInfo)
static FuncInfo* findFuncByName(string funcName, const map<string, vector<FuncInfo*>>& allFuncInfo)
{
auto fileInfo = allFuncInfo.find(fileName);
if (fileInfo == allFuncInfo.end())
return nullptr;
for (auto funcInfo : fileInfo->second)
if (funcInfo->funcName == funcName)
return funcInfo;
for (const auto& fileFuncs : allFuncInfo)
for (auto funcInfo : fileFuncs.second)
if (funcInfo->funcName == funcName)
return funcInfo;
return nullptr;
}
static FuncInfo* getCurrectFunc(SgStatement* stmt, const map<string, vector<FuncInfo*>>& allFuncInfo)
static FuncInfo* getCurrentFunc(SgStatement* stmt, const map<string, vector<FuncInfo*>>& allFuncInfo)
{
auto fileInfo = allFuncInfo.find(stmt->fileName());
if (fileInfo == allFuncInfo.end())
@@ -855,7 +852,7 @@ void removePrivates(string filename, vector<Messages>& messages,
for (auto& dcLoopRem : removeDC)
{
auto loopStmt = dcLoopRem->loop->GetOriginal();
FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo);
FuncInfo* currFunc = getCurrentFunc(loopStmt, allFuncInfo);
if (currFunc == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
@@ -1140,7 +1137,7 @@ static SgForStmt* getLoopStmtForVar(SgStatement* stmt, string loopVar)
static vector<ArraySubscript> getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0,
SgStatement* stmt = nullptr)
{
if (arrayRef->numberOfSubscripts() == 0)
if (arrayRef == nullptr || arrayRef->numberOfSubscripts() == 0)
return vector<ArraySubscript>(dimensionsNum);
vector<ArraySubscript> subscriptsVector;
@@ -1202,7 +1199,7 @@ static void checkImplicitDirectUsage(Context* ctx, SgExpression* exp, int stmtLi
{
SgFunctionCallExp* funcCallExp = (SgFunctionCallExp*)exp;
string funcName = funcCallExp->funName()->identifier();
FuncInfo* funcInfo = findFunc(ctx->loopStmt->fileName(), funcName, ctx->allFuncInfo);
FuncInfo* funcInfo = findFuncByName(funcName, ctx->allFuncInfo);
if (funcInfo != nullptr)
{
for (int i = 0; i < funcCallExp->numberOfArgs(); ++i)
@@ -1244,8 +1241,8 @@ static vector<vector<ArraySubscript>> checkImplicitDirectUsage(Context* ctx)
// st->variant() == PROC_STAT:
SgCallStmt* callStmt = (SgCallStmt*)st;
string procName = callStmt->name()->identifier();
FuncInfo* funcInfo = findFunc(callStmt->fileName(), procName, ctx->allFuncInfo);
FuncInfo* funcInfo = findFuncByName(procName, ctx->allFuncInfo);
for (int i = 0; i < callStmt->numberOfArgs(); ++i)
{
SgExpression* callArg = callStmt->arg(i);
@@ -1295,34 +1292,37 @@ static vector<Variable*> getCommonBlockGroupedVar(FuncInfo* curFunc, SgSymbol* v
// checkIndirectUsage returns masks of array indirect usage in function
// (indirect usage is usage through common blocks) and writes messages about it
static void checkIndirectUsage(Context* ctx, FuncInfo* curFunc, vector<Variable*> commonBlockGroupedVar,
static void checkIndirectUsage(Context* ctx, FuncInfo* calledFunc, vector<Variable*> commonBlockGroupedVar,
set<string>& visitedFuncs, vector<vector<ArraySubscript>>& indirectUsageMasks)
{
if (visitedFuncs.find(curFunc->funcName) != visitedFuncs.end())
if (visitedFuncs.find(calledFunc->funcName) != visitedFuncs.end())
return;
visitedFuncs.insert(curFunc->funcName);
visitedFuncs.insert(calledFunc->funcName);
for (Variable* commonBlockVar : commonBlockGroupedVar)
{
for (const CommonVariableUse& varUse : commonBlockVar->getAllUse())
{
if (varUse.getFileName() != curFunc->fileName || varUse.getFunctionName() != curFunc->funcName)
if (varUse.getFileName() != calledFunc->fileName || varUse.getFunctionName() != calledFunc->funcName)
continue;
vector<SgArrayRefExp*> directArrayRefs = getDirectArrayRefs(varUse.getFunction(), varUse.getUseS());
SgStatement* calledFuncStmt = varUse.getFunction();
calledFuncStmt->switchToFile();
vector<SgArrayRefExp*> directArrayRefs = getDirectArrayRefs(calledFuncStmt, varUse.getUseS());
for (auto arrayRef : directArrayRefs)
{
auto mask = getFixedSubscriptsVector(arrayRef, ctx->dimensionsNum);
indirectUsageMasks.push_back(mask);
addMessageUsageInFunctionCall(ctx->messages, getDimensionVarName(ctx->arraySymbol, mask),
curFunc->funcName, ctx->loop->lineNum, ctx->loop->lineNum);
calledFunc->funcName, ctx->loop->lineNum, ctx->loop->lineNum);
}
ctx->loopStmt->switchToFile();
}
}
for (FuncInfo* calledFunc : curFunc->callsFromV)
checkIndirectUsage(ctx, calledFunc, commonBlockGroupedVar, visitedFuncs, indirectUsageMasks);
for (FuncInfo* subCalledFunc : calledFunc->callsFromV)
checkIndirectUsage(ctx, subCalledFunc, commonBlockGroupedVar, visitedFuncs, indirectUsageMasks);
}
// checkIndirectUsage returns masks of array indirect usage in any function call in loop
@@ -1330,7 +1330,7 @@ static void checkIndirectUsage(Context* ctx, FuncInfo* curFunc, vector<Variable*
static vector<vector<ArraySubscript>> checkIndirectUsage(Context* ctx)
{
vector<vector<ArraySubscript>> indirectUsageMasks;
FuncInfo* currentFunc = getCurrectFunc(ctx->loopStmt, ctx->allFuncInfo);
FuncInfo* currentFunc = getCurrentFunc(ctx->loopStmt, ctx->allFuncInfo);
if (currentFunc == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
@@ -1338,10 +1338,17 @@ static vector<vector<ArraySubscript>> checkIndirectUsage(Context* ctx)
if (commonBlockGroupedVar.empty())
return indirectUsageMasks;
set<string> visitedFunctions = { currentFunc->funcName };
for (FuncInfo* calledFunc : currentFunc->callsFromV)
checkIndirectUsage(ctx, calledFunc, commonBlockGroupedVar, visitedFunctions, indirectUsageMasks);
for (SgStatement* st = ctx->loopStmt->lexNext(); st != ctx->loopStmt->lastNodeOfStmt(); st = st->lexNext())
{
if (st->variant() == PROC_STAT)
{
SgCallStmt* callStmt = (SgCallStmt*)st;
string procName = callStmt->name()->identifier();
FuncInfo* calledFunc = findFuncByName(procName, ctx->allFuncInfo);
checkIndirectUsage(ctx, calledFunc, commonBlockGroupedVar, visitedFunctions, indirectUsageMasks);
}
}
return indirectUsageMasks;
}
@@ -2136,8 +2143,8 @@ void removePrivatesAnalysis(string filename,
auto filterMasks = checkImplicitAndIndirectUsage(&context);
filterArrayRefs(&context, arrayRefs, filterMasks);
context.explicitArrayRefs.swap(arrayRefs);
context.explicitArrayRefs.swap(arrayRefs);
if (context.explicitArrayRefs.empty())
continue;