From 7bca67b75c7ec919fe1c35424832479800f9b562 Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Thu, 9 Apr 2026 21:56:03 +0300 Subject: [PATCH] new changes --- src/ArrayConstantPropagation/propagation.cpp | 149 ++++++++++++------ src/ArrayConstantPropagation/propagation.h | 16 ++ src/PrivateAnalyzer/private_arrays_search.cpp | 20 +++ src/Utils/PassManager.h | 2 +- 4 files changed, 134 insertions(+), 53 deletions(-) diff --git a/src/ArrayConstantPropagation/propagation.cpp b/src/ArrayConstantPropagation/propagation.cpp index dddf265..a9400fc 100644 --- a/src/ArrayConstantPropagation/propagation.cpp +++ b/src/ArrayConstantPropagation/propagation.cpp @@ -3,16 +3,35 @@ #include "../Utils/SgUtils.h" #include +#include #include #include #include using namespace std; +namespace { + + struct MyHash { + size_t operator()(const SgSymbol* s) const { + return std::hash{}(s->identifier()); + } + }; + + struct MyEq { + bool operator()(const SgSymbol* a, const SgSymbol* b) const { + return std::strcmp(a->identifier(), b->identifier()) == 0; + } + }; + +} + SgStatement* declPlace = NULL; unordered_set changed; -unordered_set variablesToAdd; +unordered_set variablesToAdd; unordered_set positionsToAdd; +unordered_set statementsToRemove; +unordered_map>> expToChange; static bool CheckConstIndexes(SgExpression* exp) { @@ -76,7 +95,7 @@ static SgStatement* FindLastDeclStatement(SgStatement* funcStart) return lastDecl; } -static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unordered_set& symbols) +static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unordered_set& symbols) { if (symbols.empty()) return; @@ -113,30 +132,15 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unorde vector varRefs; for (SgSymbol* sym : symbols) - varRefs.push_back(new SgVarRefExp(sym)); + { + 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); - 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); - } - else - { - commonList->setLhs(varList); - } - - SgStatement* insertAfter = commonStat; + SgStatement* insertAfter = FindLastDeclStatement(funcStart); for (SgSymbol* sym : symbols) { SgStatement* declStmt = sym->makeVarDeclStmt(); @@ -153,28 +157,53 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unorde 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, unordered_map& arrayToVariable, int& variableNumber) { if (!exp) - { return; - } - vector subnodes = { exp->lhs(), exp->rhs() }; string expUnparsed; SgExpression* toAdd = NULL; if (exp->variant() == ARRAY_REF && CheckConstIndexes(exp->lhs())) { - cout << st->unparse() << endl; + expUnparsed = exp->unparse(); if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType()) { arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); } - st->setExpression(1, arrayToVariable[expUnparsed]->copyPtr()); + 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++) @@ -182,27 +211,30 @@ static void TransformRightPart(SgStatement* st, SgExpression* exp, unordered_map if (subnodes[i] && subnodes[i]->variant() == ARRAY_REF && 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());; - } - toAdd = arrayToVariable[expUnparsed]->copyPtr(); + 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); - } + } } @@ -217,15 +249,17 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, unordered_map< { 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->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, unordered_map& arrayToVariable, int& variableNumber) @@ -233,7 +267,8 @@ static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_maplexPrev(); st = st->lexPrev(); string array = exp->unparse(); - arrayToVariable[array] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); + 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()) @@ -243,9 +278,7 @@ static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_mapexpr(1), arrayToVariable, variableNumber); } if (st->expr(0) && st->expr(0)->variant() == ARRAY_REF && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) - { TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber); - } } st = st->lexPrev(); } @@ -261,16 +294,19 @@ static void CheckVariable(SgStatement* st, SgExpression* exp, unordered_mapvariant() == 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) && st->expr(0)->variant() == ARRAY_REF && 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(); @@ -287,6 +323,7 @@ void ArrayConstantPropagation(SgProject& project) if (!file) continue; + SgFile::switchToFile(file->filename()); const int funcNum = file->numberOfFunctions(); for (int i = 0; i < funcNum; ++i) { @@ -300,36 +337,44 @@ void ArrayConstantPropagation(SgProject& project) { SgExpression* lowerBound = st->expr(0)->lhs(); SgExpression* upperBound = st->expr(0)->rhs(); + SgStatement* boundCopy = NULL; string lowerBoundUnparsed = lowerBound->unparse(), upperBoundUnparsed = upperBound->unparse(); if (upperBound->variant() == ARRAY_REF && 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 (lowerBound->variant() == ARRAY_REF && 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); } } } - unordered_set funcStarts; - for (SgStatement* st : positionsToAdd) - { - SgStatement* scope = st->controlParent(); - if (scope) - funcStarts.insert(scope); - } - for (const auto& st : funcStarts) - { - SgFile::switchToFile(st->fileName()); - InsertCommonAndDeclsForFunction(st, variablesToAdd); - } + } + unordered_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 index e2e76cd..f33523c 100644 --- a/src/ArrayConstantPropagation/propagation.h +++ b/src/ArrayConstantPropagation/propagation.h @@ -1,4 +1,20 @@ #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/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index 95cb090..041c2bc 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -7,6 +7,7 @@ #include #include +#include "ArrayConstantPropagation/propagation.h" #include "CFGraph/CFGraph.h" #include "Distribution/Array.h" #include "graph_loops.h" @@ -21,6 +22,9 @@ using namespace std; extern std::map, std::pair> declaredArrays; +extern unordered_set statementsToRemove; +extern unordered_map>> expToChange; + static unordered_set collapsed; static void RemoveEmptyPoints(ArrayAccessingIndexes& container) @@ -458,4 +462,20 @@ void FindPrivateArrays(map>& loopGraph, mapfileName()); + st->deleteStmt(); + } + + for (auto& [filename, statements] : expToChange) + { + SgFile::switchToFile(filename); + for (auto& [statement, statementCopy] : statements) + { + statement->insertStmtBefore(*statementCopy, *statement->controlParent()); + statement->deleteStmt(); + } + } } \ No newline at end of file diff --git a/src/Utils/PassManager.h b/src/Utils/PassManager.h index 95b0e61..cd69c2f 100644 --- a/src/Utils/PassManager.h +++ b/src/Utils/PassManager.h @@ -318,7 +318,7 @@ void InitPassesDependencies(map> &passDepsIn, set list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); - list({ CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH, LOOP_ANALYZER_DATA_DIST_S2 }) <= Pass(FIND_PRIVATE_ARRAYS_ANALYSIS); + 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({ BUILD_IR, CALL_GRAPH2, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(MOVE_OPERATORS);