From 6a063d2f8fec5f689a655fb3d4ef5183e30570c3 Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Wed, 20 Mar 2024 01:24:17 +0300 Subject: [PATCH] private_removing: some small fixes --- .../_src/Transformations/private_removing.cpp | 134 ++++++++++-------- 1 file changed, 76 insertions(+), 58 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index 6e97d06..457a2a1 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -44,7 +44,7 @@ static bool operator<(const RegularExpr& left, const RegularExpr& right) } // FixedSubscript represents subscript of array. Subscript is fixed if it is INT_VAL value -struct FixedSubscript { +struct ArraySubscript { bool isFixed; int value; @@ -52,7 +52,7 @@ struct FixedSubscript { RegularExpr regExprStart; RegularExpr regExprEnd; - FixedSubscript() { + ArraySubscript() { isFixed = false; value = 0; isRegIndex = false; @@ -240,6 +240,7 @@ static vector getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, const if (vars != varToRemove.arrayRefToIterationVarsMap.end()) iterationVars = vars->second; } + return getShortFixedSubscriptsVector(arrayRef, varToRemove.fixedDimensions, varToRemove.regime, iterationVars); } @@ -312,6 +313,20 @@ static FuncInfo* getCurrectFunc(SgStatement* stmt, const map& vars) +{ + if (stmt == nullptr) + return; + + if (stmt->variant() == FOR_NODE) + vars.push_back(((SgForStmt*)stmt)->doName()); + + if (stmt->id() != outerLoopStmt->id()) + fillIterationVars(stmt->controlParent(), outerLoopStmt, vars); +} + /* ************************************** * * End of block of common used functions: * * ************************************** */ @@ -369,18 +384,19 @@ static void addMessageCannotFindRD(vector& messages, string varName, i messages.push_back(Messages(typeMessage::WARR, loopLineNum, messageR, messageE, 2021)); } -static void addMessageMoreThanOneRD(vector& messages, string varName, int loopLineNum) -{ - __spf_print(1, "WARR: cannot remove private var '%s' - more than one definition reaches the statement in line %d\n", - varName.c_str(), loopLineNum); - - wstring messageE, messageR; - __spf_printToLongBuf(messageE, L"Cannot remove private var '%s' - more than one definition reaches the statement", - to_wstring(varName).c_str()); - __spf_printToLongBuf(messageR, R193, to_wstring(varName).c_str()); - - messages.push_back(Messages(typeMessage::WARR, loopLineNum, messageR, messageE, 2020)); -} +// TODO: unused: +//static void addMessageMoreThanOneRD(vector& messages, string varName, int loopLineNum) +//{ +// __spf_print(1, "WARR: cannot remove private var '%s' - more than one definition reaches the statement in line %d\n", +// varName.c_str(), loopLineNum); +// +// wstring messageE, messageR; +// __spf_printToLongBuf(messageE, L"Cannot remove private var '%s' - more than one definition reaches the statement", +// to_wstring(varName).c_str()); +// __spf_printToLongBuf(messageR, R193, to_wstring(varName).c_str()); +// +// messages.push_back(Messages(typeMessage::WARR, loopLineNum, messageR, messageE, 2020)); +//} static void addMessageRecursiveDependency(vector& messages, string varName, int lineNum) { @@ -499,7 +515,7 @@ static string getDimensionVarName(SgSymbol* var, const vector& subscripts, } // getDimensionVarName returns var name in style A(1, 2, *) -static string getDimensionVarName(SgSymbol* var, const vector& fixedSubscripts) +static string getDimensionVarName(SgSymbol* var, const vector& fixedSubscripts) { string result = var->identifier(); @@ -718,10 +734,23 @@ static map getVarToExpMap(SgArrayRefExp* defRef, SgArr // removeArray removes array by substituting it in DEF-USE pairs. // Returns set of removed fixed subscripts -static set> removeArray(string filename, const PrivateToRemove& arrayToRemove) +static set> removeArray(string filename, PrivateToRemove& arrayToRemove) { set> removedFixedSubscripts; + // again fill itaration vars: + arrayToRemove.arrayRefToIterationVarsMap.clear(); + SgStatement* loopStmt = arrayToRemove.loop->loop->GetOriginal(); + for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext()) + { + vector iterationVars; + fillIterationVars(st, loopStmt, iterationVars); + + vector arrayRefs = getDirectArrayRefsFromSingleStmt(st, arrayToRemove.varSymbol); + for (SgArrayRefExp* arrayRef : arrayRefs) + arrayToRemove.arrayRefToIterationVarsMap.insert(make_pair(arrayRef, iterationVars)); + } + auto& fixedDimensions = arrayToRemove.fixedDimensions; for (auto& defUsePair : arrayToRemove.defUseStmtsPairs) { @@ -934,19 +963,6 @@ static vector getShortFixedSubscriptsVector(Context* ctx, SgArrayRefExp* ar return getShortFixedSubscriptsVector(arrayRef, ctx->fixedDimensionsMask, ctx->regime, iterationVars); } -// fillIterationVariables fill vars set with iteration variables of all loops -// from stmt to outerLoopStmt -static void fillIterationVars(SgStatement* stmt, SgStatement* outerLoopStmt, vector& vars) -{ - if (stmt == nullptr) - return; - - if (stmt->variant() == FOR_NODE) - vars.push_back(((SgForStmt*)stmt)->doName()); - - if (stmt->id() != outerLoopStmt->id()) - fillIterationVars(stmt->controlParent(), outerLoopStmt, vars); -} // matchesFixedDimensionsMask checks if all array references have INT_VAL value in fixed dimension static bool checkFixedDimensionsMaskMatching(Context* ctx) @@ -1058,6 +1074,7 @@ static bool checkRegularIndexRefs(Context* ctx) return false; } + // TODO: possibly can be removed: if (st->variant() == ASSIGN_STAT && isEqSymbols(st->expr(0)->symbol(), ctx->arraySymbol)) for (auto iterationVar : iterationVars) if (isSymbolInExpression(iterationVar, st->expr(1))) @@ -1117,17 +1134,17 @@ static SgForStmt* getLoopStmtForVar(SgStatement* stmt, string loopVar) // getFixedSubscriptsVector returns vector of fixed INT_VAL subscripts of arrayRef // true - subscript is fixed, false - it isn't -static vector getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0, +static vector getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0, SgStatement* stmt = nullptr) { if (arrayRef->numberOfSubscripts() == 0) - return vector(dimensionsNum); + return vector(dimensionsNum); - vector subscriptsVector; + vector subscriptsVector; for (int i = 0; i < arrayRef->numberOfSubscripts(); ++i) { SgExpression* subscriptExpr = arrayRef->subscript(i); - FixedSubscript sub; + ArraySubscript sub; if (subscriptExpr->variant() == INT_VAL) { @@ -1173,7 +1190,7 @@ static vector getFixedSubscriptsVector(SgArrayRefExp* arrayRef, // checkImplicitDirectUsage returns masks of array implicit usage (as out argument) // in any function call in exp and writes message about each usage static void checkImplicitDirectUsage(Context* ctx, SgExpression* exp, int stmtLineNum, - vector>& fixedSubscripts) + vector>& fixedSubscripts) { if (exp == nullptr) return; @@ -1208,9 +1225,9 @@ static void checkImplicitDirectUsage(Context* ctx, SgExpression* exp, int stmtLi // checkImplicitDirectUsage returns masks of array implicit usage (as out argument) // and reference to whole array (like fcall(A, 1)) in any function call in loop // and writes message about each usage -static vector> checkImplicitDirectUsage(Context* ctx) +static vector> checkImplicitDirectUsage(Context* ctx) { - vector> fixedSubscripts; + vector> fixedSubscripts; for (SgStatement* st = ctx->loopStmt->lexNext(); st != ctx->loopStmt->lastNodeOfStmt(); st = st->lexNext()) { if (st->variant() != PROC_STAT) @@ -1276,7 +1293,7 @@ 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, - set& visitedFuncs, vector>& indirectUsageMasks) + set& visitedFuncs, vector>& indirectUsageMasks) { if (visitedFuncs.find(curFunc->funcName) != visitedFuncs.end()) return; @@ -1307,9 +1324,9 @@ static void checkIndirectUsage(Context* ctx, FuncInfo* curFunc, vector> checkIndirectUsage(Context* ctx) +static vector> checkIndirectUsage(Context* ctx) { - vector> indirectUsageMasks; + vector> indirectUsageMasks; FuncInfo* currentFunc = getCurrectFunc(ctx->loopStmt, ctx->allFuncInfo); if (currentFunc == nullptr) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); @@ -1327,10 +1344,10 @@ static vector> checkIndirectUsage(Context* ctx) } // checkImplicitAndIndirectUsage returns masks of implicit or indirect array usage in loop -static vector> checkImplicitAndIndirectUsage(Context* ctx) +static vector> checkImplicitAndIndirectUsage(Context* ctx) { - vector> implicitMasks = checkImplicitDirectUsage(ctx); - vector> indirectMasks = checkIndirectUsage(ctx); + vector> implicitMasks = checkImplicitDirectUsage(ctx); + vector> indirectMasks = checkIndirectUsage(ctx); implicitMasks.insert(implicitMasks.end(), indirectMasks.begin(), indirectMasks.end()); return implicitMasks; @@ -1338,7 +1355,7 @@ static vector> checkImplicitAndIndirectUsage(Context* ctx // filterArrayRefs removes from arrayRefs all refs that are not different from fixedVectors static void filterArrayRefs(Context* ctx, vector& arrayRefs, - const vector>& masks) + const vector>& masks) { if (masks.empty()) return; @@ -1346,7 +1363,7 @@ static void filterArrayRefs(Context* ctx, vector& arrayRefs, vector filteredArrayRefs; for (auto arrayRef : arrayRefs) { - vector arrayRefVec = getFixedSubscriptsVector(arrayRef, ctx->dimensionsNum); + vector arrayRefVec = getFixedSubscriptsVector(arrayRef, ctx->dimensionsNum); bool isDifferent = false; for (auto& mask : masks) @@ -1609,17 +1626,18 @@ static vector buildDefUsePairs(Context* ctx, const CFG_Type& CF if (!defIsFound) { - // try to find definition not from RD_defArgs: + // try to find definition not from RD_defArgs, by search in the block instructions: string defVarName = useInsertedStmt.insertedStmt->expr(1)->symbol()->identifier(); - SgStatement* blockStart = useInsAndBlock.second->getInstructions().front()->getInstruction()->getOperator(); - SgStatement* blockEnd = useInsAndBlock.second->getInstructions().back()->getInstruction()->getOperator(); - for (SgStatement* st = blockStart; st != blockEnd; st = st->lexNext()) + const auto& blockInstructionsVector = useInsAndBlock.second->getInstructions(); + for (auto& instruction : blockInstructionsVector) { - if (st->variant() == ASSIGN_STAT && st->expr(0)->symbol()->identifier() == defVarName && - !isVarChangedBetween(defVarName, st, useInsertedStmt.insertedStmt)) + SgStatement* stmt = instruction->getInstruction()->getOperator(); + if (stmt->variant() == ASSIGN_STAT + && stmt->expr(0)->symbol()->identifier() == defVarName + && !isVarChangedBetween(defVarName, stmt, useInsertedStmt.insertedStmt)) { defIsFound = true; - defStmt = st; + defStmt = stmt; break; } } @@ -1735,7 +1753,7 @@ static LoopGraph* leastCommonAncestor(LoopGraph* a, LoopGraph* b, LoopGraph* par // fillFullFixedSubscriptsVectorsOfAllVars return vector of pairs (name of var, its fixed subscripts vector) // of all VAR_REF and ARRAY_REF vars in exp static void fillFixedSubscriptsVectorsOfAllVars(SgExpression* exp, - vector>>& vec, + vector>>& vec, SgStatement* stmt = nullptr) { if (exp == nullptr) @@ -1749,7 +1767,7 @@ static void fillFixedSubscriptsVectorsOfAllVars(SgExpression* exp, if (elem.first == exp->symbol()->identifier()) return; - vec.push_back(make_pair(exp->symbol()->identifier(), vector{})); + vec.push_back(make_pair(exp->symbol()->identifier(), vector{})); } else if (exp->variant() == ARRAY_REF) { @@ -1768,7 +1786,7 @@ static void fillFixedSubscriptsVectorsOfAllVars(SgExpression* exp, } // fixedSubscriptLess checks if left FixedSubscript is less than right -static bool fixedSubscriptLess(const FixedSubscript& left, const FixedSubscript& right) +static bool fixedSubscriptLess(const ArraySubscript& left, const ArraySubscript& right) { if (left.isFixed && right.isFixed && left.value < right.value) return true; @@ -1789,7 +1807,7 @@ static bool fixedSubscriptLess(const FixedSubscript& left, const FixedSubscript& // fixedSubscriptLess checks if left and right FixedSubscripts are different, // using empirical methods -static bool possibleDifferent(FixedSubscript left, FixedSubscript right) +static bool possibleDifferent(ArraySubscript left, ArraySubscript right) { // TODO: add warning? if (left.isFixed && right.isRegIndex && right.regExprStart == right.regExprEnd) { @@ -1801,7 +1819,7 @@ static bool possibleDifferent(FixedSubscript left, FixedSubscript right) // isDifferentRefs checks if exp (var reference) is different from var. Refs are different // if they has at least one different fixed subscript: arr(i, 1) is different from arr(j, 2) -static bool isDifferentRefs(SgExpression* exp, const pair>& var, SgStatement* stmt) +static bool isDifferentRefs(SgExpression* exp, const pair>& var, SgStatement* stmt) { if (exp->symbol()->identifier() != var.first) return true; @@ -1809,7 +1827,7 @@ static bool isDifferentRefs(SgExpression* exp, const pairvariant() == VAR_REF) return false; - vector leftVec = getFixedSubscriptsVector((SgArrayRefExp*)exp, 0, stmt); + vector leftVec = getFixedSubscriptsVector((SgArrayRefExp*)exp, 0, stmt); if (leftVec.size() != var.second.size()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); @@ -1852,7 +1870,7 @@ static bool checkDefUsePair(Context* ctx, const DefUseStmtsPair& defUse, const C string arrayName = ctx->arraySymbol->identifier(); - vector>> dependOnVars; + vector>> dependOnVars; SgArrayRefExp* defRef = (SgArrayRefExp*)defUse.first->expr(0); vector arrayUseRefs = getDirectArrayRefsFromSingleStmt(defUse.second, ctx->arraySymbol); for (auto useRef : arrayUseRefs)