From 357f961d68fc598af8fd1e5188d530981be057e3 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 1 May 2026 12:03:41 +0300 Subject: [PATCH] refactored arrays propagation pass --- src/Sapfor.cpp | 2 +- .../ArrayConstantPropagation/propagation.cpp | 161 +++++++++--------- .../ArrayConstantPropagation/propagation.h | 2 +- src/Utils/version.h | 2 +- 4 files changed, 88 insertions(+), 79 deletions(-) diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index eb60e0b..fcb4294 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1911,7 +1911,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne else if (curr_regime == ARRAY_PROPAGATION) arrayConstantPropagation(project); else if (curr_regime == ARRAY_PROPAGATION_RESTORE) - restoreArrays(); + arrayConstantPropagationRrestore(); const float elapsed = duration_cast(high_resolution_clock::now() - timeForPass).count() / 1000.; const float elapsedGlobal = duration_cast(high_resolution_clock::now() - globalTime).count() / 1000.; diff --git a/src/Transformations/ArrayConstantPropagation/propagation.cpp b/src/Transformations/ArrayConstantPropagation/propagation.cpp index 356bd59..81c48cc 100644 --- a/src/Transformations/ArrayConstantPropagation/propagation.cpp +++ b/src/Transformations/ArrayConstantPropagation/propagation.cpp @@ -15,10 +15,11 @@ static set changed; static map variablesToAdd; static map> positionsToAdd; static map arrayToName; -static set statementsToRemove; +static map> statementsToRemove; static map> expToChange; +static int variableNumber = 0; -static bool CheckConstIndexes(SgExpression* exp) +static bool checkConstIndexes(SgExpression* exp) { if (!exp) { @@ -41,7 +42,7 @@ static bool CheckConstIndexes(SgExpression* exp) return true; } -static SgExpression* CreateVar(int& variableNumber, SgType* type) +static SgExpression* createVar(SgType* type) { string varName = "tmp_prop_var"; string name = varName + std::to_string(variableNumber) + "__"; @@ -56,10 +57,11 @@ static SgExpression* CreateVar(int& variableNumber, SgType* type) return new SgExpression(VAR_REF, NULL, NULL, varSymbol, type->copyPtr()); } -static SgStatement* FindLastDeclStatement(SgStatement* funcStart) +static SgStatement* findLastDeclStatement(SgStatement* funcStart) { if (!funcStart) return NULL; + SgStatement* endSt = funcStart->lastNodeOfStmt(); SgStatement* cur = funcStart->lexNext(); SgStatement* lastDecl = funcStart; @@ -82,7 +84,7 @@ static SgStatement* FindLastDeclStatement(SgStatement* funcStart) return lastDecl; } -static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map& symbols) +static void insertCommonAndDeclsForFunction(SgStatement* funcStart, const map& symbols) { if (symbols.empty()) return; @@ -90,7 +92,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const maplastNodeOfStmt(); SgStatement* commonStat = NULL; @@ -130,7 +132,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const mapinsertStmtAfter(*declStmt, *funcStart); insertAfter = declStmt; - statementsToRemove.insert(declStmt); + statementsToRemove[declStmt->fileName()].insert(declStmt); } if (!commonList) @@ -164,14 +166,12 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const mapsetlineNumber(getNextNegativeLineNumber()); commonStat->setExpression(0, commonList); - SgStatement* lastDecl = FindLastDeclStatement(funcStart); + SgStatement* lastDecl = findLastDeclStatement(funcStart); lastDecl->insertStmtAfter(*commonStat, *funcStart); - statementsToRemove.insert(commonStat); + statementsToRemove[commonStat->fileName()].insert(commonStat); } else - { commonList->setLhs(varList); - } } @@ -179,6 +179,7 @@ static void copyStatement(SgStatement* st) { if (!st) return; + if (expToChange[st->fileName()].find(st) == expToChange[st->fileName()].end()) { SgStatement* boundCopy = st->copyPtr(); @@ -195,7 +196,7 @@ static void copyStatement(SgStatement* st) } } -static bool TransformRightPart(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) +static bool transformRightPart(SgStatement* st, SgExpression* exp, map& arrayToVariable) { if (!exp) return false; @@ -204,13 +205,13 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, maplhs())) + if (isArrayRef(exp) && checkConstIndexes(exp->lhs())) { expUnparsed = exp->unparse(); if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp && exp->symbol() && exp->symbol()->type() && exp->symbol()->type()->baseType()) { - arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); + arrayToVariable[expUnparsed] = createVar(exp->symbol()->type()->baseType()); arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse(); } positionsToAdd[string(declPlace->fileName())].insert(declPlace); @@ -220,16 +221,17 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, mapsetExpression(1, newVarExp); return true; } + for (int i = 0; i < 2; i++) { if (subnodes[i] && isArrayRef(subnodes[i]) && subnodes[i]->symbol() && subnodes[i]->symbol()->type() && - subnodes[i]->symbol()->type()->baseType() && CheckConstIndexes(subnodes[i]->lhs())) + subnodes[i]->symbol()->type()->baseType() && checkConstIndexes(subnodes[i]->lhs())) { isChanged = true; expUnparsed = subnodes[i]->unparse(); if (arrayToVariable.find(expUnparsed) == arrayToVariable.end()) { - arrayToVariable[expUnparsed] = CreateVar(variableNumber, subnodes[i]->symbol()->type()->baseType()); + arrayToVariable[expUnparsed] = createVar(subnodes[i]->symbol()->type()->baseType()); arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse(); } positionsToAdd[string(declPlace->fileName())].insert(declPlace); @@ -246,13 +248,13 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) +static void transformLeftPart(SgStatement* st, SgExpression* exp, map& arrayToVariable) { if (!st || !st->expr(1)) return; @@ -262,10 +264,11 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, mapunparse(); if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType()) { - arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); + arrayToVariable[expUnparsed] = createVar(exp->symbol()->type()->baseType()); arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse(); } positionsToAdd[string(declPlace->fileName())].insert(declPlace); @@ -282,16 +285,16 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, mapsetLocalLineNumber(st->lineNumber()); changed.insert(st); - statementsToRemove.insert(newStatement); + statementsToRemove[newStatement->fileName()].insert(newStatement); } -static void TransformBorder(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) +static void transformBorder(SgStatement* st, SgExpression* exp, map& arrayToVariable) { if (!st || !exp) return; SgStatement* firstStatement = declPlace->lexPrev(); positionsToAdd[string(declPlace->fileName())].insert(declPlace); - TransformRightPart(st, exp, arrayToVariable, variableNumber); + transformRightPart(st, exp, arrayToVariable); st = st->lexPrev(); while (st &&st != firstStatement) { @@ -299,16 +302,16 @@ static void TransformBorder(SgStatement* st, SgExpression* exp, mapexpr(1)) { - TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber); + transformRightPart(st, st->expr(1), arrayToVariable); } - if (st->expr(0) && isArrayRef(st->expr(0)) && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) - TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber); + if (st->expr(0) && isArrayRef(st->expr(0)) && checkConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) + transformLeftPart(st, st->expr(0), arrayToVariable); } st = st->lexPrev(); } } -static void CheckVariable(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) +static void checkVariable(SgStatement* st, SgExpression* exp, map& arrayToVariable) { SgStatement* firstStatement = declPlace->lexPrev(); st = st->lexPrev(); @@ -316,23 +319,24 @@ static void CheckVariable(SgStatement* st, SgExpression* exp, mapvariant() == ASSIGN_STAT && st->expr(0)->symbol() == exp->symbol()) { - if (TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber)) + if (transformRightPart(st, st->expr(1), arrayToVariable)) { positionsToAdd[string(declPlace->fileName())].insert(declPlace); } } + if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) { if (st->expr(1)) { - if(TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber)) + if(transformRightPart(st, st->expr(1), arrayToVariable)) { positionsToAdd[string(declPlace->fileName())].insert(declPlace); } } - if (st->expr(0) && isArrayRef(st->expr(0)) && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) + if (st->expr(0) && isArrayRef(st->expr(0)) && checkConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) { - TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber); + transformLeftPart(st, st->expr(0), arrayToVariable); positionsToAdd[string(declPlace->fileName())].insert(declPlace); } } @@ -340,12 +344,11 @@ static void CheckVariable(SgStatement* st, SgExpression* exp, map>& borderVars, - const map& arrayToVariable, - map& hitCount, - map>>>& result) +static void findConstValues(SgProject& project, + const map>& borderVars, + const map& arrayToVariable, + map& hitCount, + map>>>& result) { for (int i = 0; i < project.numberOfFiles(); i++) { @@ -422,14 +425,14 @@ static void insertDefinition(mapcontrolParent()) { insertBefore->insertStmtBefore(*asg, *insertBefore->controlParent()); - statementsToRemove.insert(asg); + statementsToRemove[asg->fileName()].insert(asg); } } } } } -static void applyLeftPartForUnchangedAssignments(SgProject& project, map& arrayToVariable, int& variableNumber) +static void applyLeftPartForUnchangedAssignments(SgProject& project, map& arrayToVariable) { for (int fi = 0; fi < project.numberOfFiles(); ++fi) { @@ -468,28 +471,28 @@ static void applyLeftPartForUnchangedAssignments(SgProject& project, mapsymbol()->type()->baseType()) continue; - if (!CheckConstIndexes(lhs->lhs())) + if (!checkConstIndexes(lhs->lhs())) continue; const string lhsUnparsed = lhs->unparse(); if (arrayToVariable.find(lhsUnparsed) == arrayToVariable.end()) continue; - TransformLeftPart(st, lhs, arrayToVariable, variableNumber); + transformLeftPart(st, lhs, arrayToVariable); } } } } -static bool ContainsArrayRefRecursive(SgExpression* exp) +static bool containsArrayRefRecursive(SgExpression* exp) { if (!exp) return false; - if (isArrayRef(exp) && CheckConstIndexes(exp->lhs())) + if (isArrayRef(exp) && checkConstIndexes(exp->lhs())) return true; - return ContainsArrayRefRecursive(exp->lhs()) || ContainsArrayRefRecursive(exp->rhs()); + return containsArrayRefRecursive(exp->lhs()) || containsArrayRefRecursive(exp->rhs()); } static void getBorderVars(SgExpression* exp, const string& filename, map>& borderVars) @@ -497,21 +500,19 @@ static void getBorderVars(SgExpression* exp, const string& filename, maplhs())) || exp->variant() == VAR_REF) + if ((isArrayRef(exp) && checkConstIndexes(exp->lhs())) || exp->variant() == VAR_REF) borderVars[filename][string(exp->unparse())] = declPlace; getBorderVars(exp->lhs(), filename, borderVars); getBorderVars(exp->rhs(), filename, borderVars); } -static void processLoopBound( - SgStatement* st, - SgExpression* bound, - const string& boundUnparsed, - bool isUpperBound, - map& arrayToVariable, - map>& borderVars, - int& variableNumber) +static void processLoopBound(SgStatement* st, + SgExpression* bound, + const string& boundUnparsed, + bool isUpperBound, + map& arrayToVariable, + map>& borderVars) { if (!bound || !st) return; @@ -520,36 +521,40 @@ static void processLoopBound( getBorderVars(exp, st->fileName(), borderVars); - if (ContainsArrayRefRecursive(exp), borderVars, st->fileName()) + if (containsArrayRefRecursive(exp), borderVars, st->fileName()) { copyStatement(st); - TransformBorder(st, bound, arrayToVariable, variableNumber); + transformBorder(st, bound, arrayToVariable); positionsToAdd[string(declPlace->fileName())].insert(declPlace); } else if (bound->variant() == VAR_REF) - CheckVariable(st, bound, arrayToVariable, variableNumber); + checkVariable(st, bound, arrayToVariable); } void arrayConstantPropagation(SgProject& project) { map arrayToVariable; map> borderVars; - int variableNumber = 0; + for (int i = 0; i < project.numberOfFiles(); i++) { - SgFile* file = &(project.file(i)); + SgFile *file = &(project.file(i)); if (!file) - continue; - SgFile::switchToFile(file->filename()); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + if (SgFile::switchToFile(file->filename()) == -1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + const int funcNum = file->numberOfFunctions(); for (int i = 0; i < funcNum; ++i) { SgStatement* st = file->functions(i); if (!st) - continue; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + declPlace = st; SgStatement* lastNode = st->lastNodeOfStmt(); @@ -568,20 +573,22 @@ void arrayConstantPropagation(SgProject& project) string lowerBoundUnparsed = lowerBound->unparse(); string upperBoundUnparsed = upperBound->unparse(); - processLoopBound(st, st->expr(0), upperBoundUnparsed, true, arrayToVariable, borderVars, variableNumber); - processLoopBound(st, st->expr(0), lowerBoundUnparsed, false, arrayToVariable, borderVars, variableNumber); + processLoopBound(st, st->expr(0), upperBoundUnparsed, true, arrayToVariable, borderVars); + processLoopBound(st, st->expr(0), lowerBoundUnparsed, false, arrayToVariable, borderVars); } } } } - applyLeftPartForUnchangedAssignments(project, arrayToVariable, variableNumber); + + applyLeftPartForUnchangedAssignments(project, arrayToVariable); map> funcStarts; for (const auto& [fileName, statements] : positionsToAdd) { int res = SgFile::switchToFile(fileName); if (res == -1) - continue; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + for (SgStatement* st : statements) { SgStatement* scope = isSgProgHedrStmt(st) ? st : st->controlParent(); @@ -589,42 +596,44 @@ void arrayConstantPropagation(SgProject& project) funcStarts[fileName].insert(scope); } } + for (const auto& [fileName, statements] : funcStarts) { SgFile::switchToFile(fileName); for (SgStatement* st : statements) { - InsertCommonAndDeclsForFunction(st, variablesToAdd); + insertCommonAndDeclsForFunction(st, variablesToAdd); } } + map>>> result; map hitCount; + findConstValues(project, borderVars, arrayToVariable, hitCount, result); insertDefinition(result, hitCount); } -void restoreArrays() +void arrayConstantPropagationRrestore() { for (auto& [filename, statements] : expToChange) { if (SgFile::switchToFile(filename) == -1) - continue; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + for (auto& [statement, statementCopy] : statements) { if (statement && statementCopy) - { for (int i = 0; i < 3; i++) - { statement->setExpression(i, statementCopy->expr(i)); - } - - } } } - for (SgStatement* st : statementsToRemove) + for (auto& [filename, statements] : statementsToRemove) { - SgFile::switchToFile(st->fileName()); - st->deleteStmt(); + if (SgFile::switchToFile(filename) == -1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + for (auto& st : statements) + st->deleteStmt(); } } diff --git a/src/Transformations/ArrayConstantPropagation/propagation.h b/src/Transformations/ArrayConstantPropagation/propagation.h index 190bd03..547d96c 100644 --- a/src/Transformations/ArrayConstantPropagation/propagation.h +++ b/src/Transformations/ArrayConstantPropagation/propagation.h @@ -8,4 +8,4 @@ using namespace std; void arrayConstantPropagation(SgProject& project); -void restoreArrays(); +void arrayConstantPropagationRrestore(); diff --git a/src/Utils/version.h b/src/Utils/version.h index e649f40..89bea68 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2483" +#define VERSION_SPF "2484"