diff --git a/src/ArrayConstantPropagation/propagation.cpp b/src/ArrayConstantPropagation/propagation.cpp deleted file mode 100644 index 526dd1f..0000000 --- a/src/ArrayConstantPropagation/propagation.cpp +++ /dev/null @@ -1,374 +0,0 @@ -#include "propagation.h" - -#include "../Utils/SgUtils.h" - -#include -#include -#include -#include -#include - -using namespace std; - -namespace { - - struct MyCmp { - bool operator()(const SgSymbol* a, const SgSymbol* b) const { - return std::strcmp(a->identifier(), b->identifier()) < 0; - } - }; - -} - -SgStatement* declPlace = NULL; -set changed; -set variablesToAdd; -set positionsToAdd; -set statementsToRemove; -map>> expToChange; - -static bool CheckConstIndexes(SgExpression* exp) -{ - if (!exp) - { - return true; - } - SgExpression* lhs = exp->lhs(); - SgExpression* rhs = exp->rhs(); - do - { - if (lhs->variant() != INT_VAL) - { - return false; - } - if (rhs) - { - lhs = rhs->lhs(); - rhs = rhs->rhs(); - } - } while (rhs); - return true; -} - -static SgExpression* CreateVar(int& variableNumber, SgType* type) -{ - string varName = "__tmp_prop_var"; - string name = varName + std::to_string(variableNumber) + "__"; - variableNumber++; - - SgStatement* funcStart = declPlace->controlParent(); - SgSymbol* varSymbol = new SgSymbol(VARIABLE_NAME, name.c_str(), *type, *funcStart); - - variablesToAdd.insert(varSymbol); - positionsToAdd.insert(declPlace); - - return new SgExpression(VAR_REF, NULL, NULL, varSymbol, type->copyPtr()); -} - -static SgStatement* FindLastDeclStatement(SgStatement* funcStart) -{ - SgStatement* endSt = funcStart->lastNodeOfStmt(); - SgStatement* cur = funcStart->lexNext(); - SgStatement* lastDecl = funcStart; - const set declVariants = { VAR_DECL, VAR_DECL_90, ALLOCATABLE_STMT, DIM_STAT, - EXTERN_STAT, COMM_STAT, HPF_TEMPLATE_STAT, DVM_VAR_DECL, STRUCT_DECL }; - - while (cur && cur != endSt) - { - if (cur->variant() == INTERFACE_STMT) - cur = cur->lastNodeOfStmt(); - - if (declVariants.find(cur->variant()) != declVariants.end()) - lastDecl = cur; - else if (isSgExecutableStatement(cur)) - break; - - cur = cur->lexNext(); - } - - return lastDecl; -} - -static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const set& symbols) -{ - if (symbols.empty()) - return; - - const string commonBlockName = "__propagation_common__"; - - SgStatement* funcEnd = funcStart->lastNodeOfStmt(); - SgStatement* commonStat = NULL; - SgExpression* commonList = NULL; - - for (SgStatement* cur = funcStart->lexNext(); - cur && cur != funcEnd; cur = cur->lexNext()) - { - if (cur->variant() != COMM_STAT) - continue; - - for (SgExpression* exp = cur->expr(0); exp; exp = exp->rhs()) - { - if (exp->variant() != COMM_LIST) - continue; - - const char* id = exp->symbol() ? exp->symbol()->identifier() : NULL; - string existingName = id ? string(id) : string("spf_unnamed"); - if (existingName == commonBlockName) - { - commonStat = cur; - commonList = exp; - break; - } - } - if (commonStat) - break; - } - - vector varRefs; - for (SgSymbol* sym : symbols) - { - if (!sym || sym->variant() != VARIABLE_NAME || string(sym->identifier()) == commonBlockName) - continue; - SgSymbol* symToAdd = new SgSymbol(VARIABLE_NAME, sym->identifier(), *sym->type(), *funcStart); - varRefs.push_back(new SgVarRefExp(symToAdd)); - } - SgExpression* varList = makeExprList(varRefs, false); - - SgStatement* insertAfter = FindLastDeclStatement(funcStart); - for (SgSymbol* sym : symbols) - { - SgStatement* declStmt = sym->makeVarDeclStmt(); - if (!declStmt) - continue; - - if (SgVarDeclStmt* vds = isSgVarDeclStmt(declStmt)) - vds->setVariant(VAR_DECL_90); - - declStmt->setFileName(funcStart->fileName()); - declStmt->setFileId(funcStart->getFileId()); - declStmt->setProject(funcStart->getProject()); - declStmt->setlineNumber(getNextNegativeLineNumber()); - - insertAfter->insertStmtAfter(*declStmt, *funcStart); - insertAfter = declStmt; - statementsToRemove.insert(declStmt); - } - - if (!commonList) - { - SgSymbol* commonSymbol = new SgSymbol(COMMON_NAME, commonBlockName.c_str()); - commonList = new SgExpression(COMM_LIST, varList, NULL, commonSymbol); - - commonStat = new SgStatement(COMM_STAT); - commonStat->setFileName(funcStart->fileName()); - commonStat->setFileId(funcStart->getFileId()); - commonStat->setProject(funcStart->getProject()); - commonStat->setlineNumber(getNextNegativeLineNumber()); - commonStat->setExpression(0, commonList); - - SgStatement* lastDecl = FindLastDeclStatement(funcStart); - lastDecl->insertStmtAfter(*commonStat, *funcStart); - statementsToRemove.insert(commonStat); - } - else - { - commonList->setLhs(varList); - } - -} - -static void TransformRightPart(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) -{ - if (!exp) - return; - vector subnodes = { exp->lhs(), exp->rhs() }; - - string expUnparsed; - SgExpression* toAdd = NULL; - if (isArrayRef(exp) && CheckConstIndexes(exp->lhs())) - { - expUnparsed = exp->unparse(); - if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType()) - { - arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); - } - positionsToAdd.insert(declPlace); - SgSymbol* builder = arrayToVariable[expUnparsed]->symbol(); - auto* sym = new SgSymbol(builder->variant(), builder->identifier(), builder->type(), st->controlParent()); - auto* newVarExp = new SgVarRefExp(sym); - expToChange[st->fileName()].push_back({ st , st->copyPtr() }); - st->setExpression(1, newVarExp); - return; - } - for (int i = 0; i < 2; i++) - { - if (subnodes[i] && isArrayRef(subnodes[i]) && subnodes[i]->symbol()->type()->baseType() && CheckConstIndexes(subnodes[i]->lhs())) - { - expUnparsed = subnodes[i]->unparse(); - if (arrayToVariable.find(expUnparsed) == arrayToVariable.end()) - arrayToVariable[expUnparsed] = CreateVar(variableNumber, subnodes[i]->symbol()->type()->baseType()); - - positionsToAdd.insert(declPlace); - SgSymbol* builder = arrayToVariable[expUnparsed]->symbol(); - auto* sym = new SgSymbol(builder->variant(), builder->identifier(), builder->type(), st->controlParent()); - toAdd = new SgVarRefExp(sym); - if (toAdd) - { - if (i == 0) - { - expToChange[st->fileName()].push_back({ st , st->copyPtr() });; - exp->setLhs(toAdd); - } - else - { - expToChange[st->fileName()].push_back({ st , st->copyPtr() });; - exp->setRhs(toAdd); - } - } - } - else - TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber); - - } -} - -static void TransformLeftPart(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) -{ - if (exp->symbol()->type()->variant() == T_STRING) - return; - if (changed.find(st) != changed.end()) - return; - string expUnparsed = exp->unparse(); - if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType()) - { - arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); - } - positionsToAdd.insert(declPlace); - SgStatement* newStatement = new SgStatement(ASSIGN_STAT, NULL, NULL, arrayToVariable[expUnparsed]->copyPtr(), st->expr(1)->copyPtr(), NULL); - - newStatement->setFileId(st->getFileId()); - newStatement->setProject(st->getProject()); - - newStatement->setlineNumber(getNextNegativeLineNumber()); - newStatement->setLocalLineNumber(st->lineNumber()); - st->insertStmtBefore(*newStatement, *st->controlParent()); - changed.insert(st); - statementsToRemove.insert(newStatement); -} - -static void TransformBorder(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) -{ - SgStatement* firstStatement = declPlace->lexPrev(); - st = st->lexPrev(); - string array = exp->unparse(); - if (arrayToVariable.find(array) == arrayToVariable.end()) - arrayToVariable[array] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); - while (st != firstStatement) - { - if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) - { - if (st->expr(1)) - { - TransformRightPart(st, st->expr(1), 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, variableNumber); - } - st = st->lexPrev(); - } -} - -static void CheckVariable(SgStatement* st, SgExpression* exp, map& arrayToVariable, int& variableNumber) -{ - SgStatement* firstStatement = declPlace->lexPrev(); - st = st->lexPrev(); - string varName = exp->unparse(); - while (st != firstStatement) - { - if (st->variant() == ASSIGN_STAT && st->expr(0)->symbol() == exp->symbol()) - { - TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber); - positionsToAdd.insert(declPlace); - } - if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) - { - if (st->expr(1)) - { - TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber); - positionsToAdd.insert(declPlace); - } - 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); - positionsToAdd.insert(declPlace); - } - } - st = st->lexPrev(); - } -} - -void arrayConstantPropagation(SgProject& project) -{ - map arrayToVariable; - int variableNumber = 0; - for (int i = 0; i < project.numberOfFiles(); i++) - { - SgFile* file = &(project.file(i)); - - if (!file) - continue; - SgFile::switchToFile(file->filename()); - const int funcNum = file->numberOfFunctions(); - for (int i = 0; i < funcNum; ++i) - { - SgStatement* st = file->functions(i); - declPlace = st->lexNext(); - SgStatement* lastNode = st->lastNodeOfStmt(); - - for (; st != lastNode; st = st->lexNext()) - { - if (st->variant() == FOR_NODE) - { - SgExpression* lowerBound = st->expr(0)->lhs(); - SgExpression* upperBound = st->expr(0)->rhs(); - SgStatement* boundCopy = NULL; - string lowerBoundUnparsed = lowerBound->unparse(), upperBoundUnparsed = upperBound->unparse(); - if (isArrayRef(upperBound) && upperBound->symbol()->type()->baseType() && CheckConstIndexes(upperBound->lhs())) - { - boundCopy = st->copyPtr(); - TransformBorder(st, upperBound, arrayToVariable, variableNumber); - st->expr(0)->setRhs(arrayToVariable[upperBoundUnparsed]->copyPtr()); - expToChange[st->fileName()].push_back({ st ,boundCopy });; - positionsToAdd.insert(declPlace); - } - else if (upperBound->variant() == VAR_REF) - CheckVariable(st, upperBound, arrayToVariable, variableNumber); - - if (isArrayRef(lowerBound) && lowerBound->symbol()->type()->baseType() && CheckConstIndexes(lowerBound->lhs())) - { - boundCopy = st->copyPtr(); - TransformBorder(st, lowerBound, arrayToVariable, variableNumber); - st->expr(0)->setLhs(arrayToVariable[lowerBoundUnparsed]->copyPtr()); - expToChange[st->fileName()].push_back({ st , boundCopy });; - positionsToAdd.insert(declPlace); - } - else if (lowerBound->variant() == VAR_REF) - CheckVariable(st, lowerBound, arrayToVariable, variableNumber); - } - } - } - } - set funcStarts; - for (SgStatement* st : positionsToAdd) - { - SgFile::switchToFile(st->fileName()); - SgStatement* scope = st->controlParent(); - if (scope) - funcStarts.insert(scope); - } - for (const auto& st : funcStarts) - { - SgFile::switchToFile(st->fileName()); - InsertCommonAndDeclsForFunction(st, variablesToAdd); - } -} \ No newline at end of file diff --git a/src/ArrayConstantPropagation/propagation.h b/src/ArrayConstantPropagation/propagation.h deleted file mode 100644 index 65fe4ed..0000000 --- a/src/ArrayConstantPropagation/propagation.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "../Utils/SgUtils.h" - -#include -#include - -using namespace std; - -struct ExprRestoreEntry -{ - enum Kind { kStatementExpr, kExprChild }; - Kind kind; - SgStatement* stmt; - int stmtExprIndex; - SgExpression* parent; - bool childIsRhs; - SgExpression* savedCopy; -}; - -void arrayConstantPropagation(SgProject& project); \ No newline at end of file diff --git a/src/CFGraph/RD_subst.cpp b/src/CFGraph/RD_subst.cpp index 250ff3a..ac6f00a 100644 --- a/src/CFGraph/RD_subst.cpp +++ b/src/CFGraph/RD_subst.cpp @@ -56,7 +56,7 @@ static void setSubstitutionsApplied(bool new_state) substitutions_applied = new_state; } -map>> oldExpressionsInFile; +static map>> oldExpressionsInFile; static map>> replacementsInFiles; static int substitution_counter = 0; diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index 8f9d79e..06d4d94 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -20,10 +20,6 @@ using namespace std; -extern map> expToChange; -extern map>> oldExpressionsInFile; -extern map> SPF_messages; -extern set statementsToRemove; extern std::map, std::pair> declaredArrays; static set collapsed; @@ -424,28 +420,4 @@ void findPrivateArrays(map>& loopGraph, mapexpr(i); - } - - } - } - } - - for (SgStatement* st : statementsToRemove) - { - SgFile::switchToFile(st->fileName()); - st->deleteStmt(); - } } \ No newline at end of file diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 3471045..eb60e0b 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1910,6 +1910,8 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne mergeRegions(parallelRegions, allFuncInfo); else if (curr_regime == ARRAY_PROPAGATION) arrayConstantPropagation(project); + else if (curr_regime == ARRAY_PROPAGATION_RESTORE) + restoreArrays(); const float elapsed = duration_cast(high_resolution_clock::now() - timeForPass).count() / 1000.; const float elapsedGlobal = duration_cast(high_resolution_clock::now() - globalTime).count() / 1000.; @@ -2369,19 +2371,9 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case SUBST_EXPR_RD_AND_UNPARSE: case SUBST_EXPR_AND_UNPARSE: case REMOVE_DEAD_CODE_AND_UNPARSE: - if (folderName) - runAnalysis(*project, UNPARSE_FILE, true, "", folderName); - else - __spf_print(1, "can not run UNPARSE_FILE - folder name is null\n"); - break; case FIND_PRIVATE_ARRAYS: if (folderName) - { - //if (!insertedPrivates.empty()) - runAnalysis(*project, UNPARSE_FILE, true, "", folderName); - /* else - __spf_print(1, "skip UNPARSE_FILE after FIND_PRIVATE_ARRAYS: no inserted directives\n");*/ - } + runAnalysis(*project, UNPARSE_FILE, true, "", folderName); else __spf_print(1, "can not run UNPARSE_FILE - folder name is null\n"); break; diff --git a/src/Sapfor.h b/src/Sapfor.h index 08541cc..a10a2d0 100644 --- a/src/Sapfor.h +++ b/src/Sapfor.h @@ -192,6 +192,7 @@ enum passes { TRANSFORM_ASSUMED_SIZE_PARAMETERS, ARRAY_PROPAGATION, + ARRAY_PROPAGATION_RESTORE, TEST_PASS, EMPTY_PASS @@ -384,9 +385,7 @@ static void setPassValues() passNames[TRANSFORM_ASSUMED_SIZE_PARAMETERS] = "TRANSFORM_ASSUMED_SIZE_PARAMETERS"; passNames[ARRAY_PROPAGATION] = "ARRAY_PROPAGATION"; - - passNames[ARRAY_PROPAGATION] = "ARRAY_PROPAGATION"; - + passNames[ARRAY_PROPAGATION_RESTORE] = "ARRAY_PROPAGATION_RESTORE"; passNames[TEST_PASS] = "TEST_PASS"; } diff --git a/src/Transformations/ArrayConstantPropagation/propagation.cpp b/src/Transformations/ArrayConstantPropagation/propagation.cpp index 9e4c376..058e5b2 100644 --- a/src/Transformations/ArrayConstantPropagation/propagation.cpp +++ b/src/Transformations/ArrayConstantPropagation/propagation.cpp @@ -15,8 +15,8 @@ static set changed; static map variablesToAdd; static map> positionsToAdd; static map arrayToName; -set statementsToRemove; -map> expToChange; +static set statementsToRemove; +static map> expToChange; static bool CheckConstIndexes(SgExpression* exp) { @@ -601,4 +601,31 @@ void arrayConstantPropagation(SgProject& project) map hitCount; findConstValues(project, borderVars, arrayToVariable, hitCount, result); insertDefinition(result, hitCount); -} \ No newline at end of file +} + +void restoreArrays() +{ + cout << "ARRAY_PROPAGATION_RESTORE" << endl; + for (auto& [filename, statements] : expToChange) + { + if (SgFile::switchToFile(filename) == -1) + continue; + 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) + { + SgFile::switchToFile(st->fileName()); + st->deleteStmt(); + } +} diff --git a/src/Transformations/ArrayConstantPropagation/propagation.h b/src/Transformations/ArrayConstantPropagation/propagation.h index d75287a..190bd03 100644 --- a/src/Transformations/ArrayConstantPropagation/propagation.h +++ b/src/Transformations/ArrayConstantPropagation/propagation.h @@ -7,4 +7,5 @@ using namespace std; -void arrayConstantPropagation(SgProject& project); \ No newline at end of file +void arrayConstantPropagation(SgProject& project); +void restoreArrays(); diff --git a/src/Utils/PassManager.h b/src/Utils/PassManager.h index 68a6f6b..cca9f91 100644 --- a/src/Utils/PassManager.h +++ b/src/Utils/PassManager.h @@ -319,7 +319,7 @@ void InitPassesDependencies(map> &passDepsIn, set list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); list({ ARRAY_PROPAGATION, CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH, LOOP_ANALYZER_DATA_DIST_S2 }) <= Pass(FIND_PRIVATE_ARRAYS_ANALYSIS); - list({ FIND_PRIVATE_ARRAYS_ANALYSIS, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(FIND_PRIVATE_ARRAYS); + list({ FIND_PRIVATE_ARRAYS_ANALYSIS, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD, ARRAY_PROPAGATION_RESTORE }) <= Pass(FIND_PRIVATE_ARRAYS); list({ BUILD_IR, CALL_GRAPH2, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(MOVE_OPERATORS); Pass(CREATE_TEMPLATE_LINKS) <= Pass(MERGE_REGIONS);