From 02baed087ff32df0c12e020b7744f29ed5a8e229 Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Sun, 7 Apr 2024 12:05:39 +0300 Subject: [PATCH] private_removing: update interprocedure analysis --- .../_src/Transformations/private_removing.cpp | 61 +++++++++++-------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index 37c64d2..ed08c73 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -290,20 +290,17 @@ static bool isSymbolInExpression(SgSymbol* symbol, SgExpression* exp) isSymbolInExpression(symbol, exp->rhs()); } -static FuncInfo* findFunc(string fileName, string funcName, const map>& allFuncInfo) +static FuncInfo* findFuncByName(string funcName, const map>& 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>& allFuncInfo) +static FuncInfo* getCurrentFunc(SgStatement* stmt, const map>& allFuncInfo) { auto fileInfo = allFuncInfo.find(stmt->fileName()); if (fileInfo == allFuncInfo.end()) @@ -855,7 +852,7 @@ void removePrivates(string filename, vector& 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 getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0, SgStatement* stmt = nullptr) { - if (arrayRef->numberOfSubscripts() == 0) + if (arrayRef == nullptr || arrayRef->numberOfSubscripts() == 0) return vector(dimensionsNum); vector 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> 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 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 commonBlockGroupedVar, +static void checkIndirectUsage(Context* ctx, FuncInfo* calledFunc, vector commonBlockGroupedVar, set& visitedFuncs, vector>& 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 directArrayRefs = getDirectArrayRefs(varUse.getFunction(), varUse.getUseS()); + SgStatement* calledFuncStmt = varUse.getFunction(); + calledFuncStmt->switchToFile(); + vector 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> checkIndirectUsage(Context* ctx) { vector> 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> checkIndirectUsage(Context* ctx) if (commonBlockGroupedVar.empty()) return indirectUsageMasks; - set 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;