refactored arrays propagation pass

This commit is contained in:
ALEXks
2026-05-01 12:03:41 +03:00
parent 8b282ebc21
commit 357f961d68
4 changed files with 88 additions and 79 deletions

View File

@@ -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<milliseconds>(high_resolution_clock::now() - timeForPass).count() / 1000.;
const float elapsedGlobal = duration_cast<milliseconds>(high_resolution_clock::now() - globalTime).count() / 1000.;

View File

@@ -15,10 +15,11 @@ static set<SgStatement*> changed;
static map<string, SgSymbol*> variablesToAdd;
static map<string, set<SgStatement*>> positionsToAdd;
static map<string, string> arrayToName;
static set<SgStatement*> statementsToRemove;
static map<string, set<SgStatement*>> statementsToRemove;
static map<string, map<SgStatement*, SgStatement*>> 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<string, SgSymbol*>& symbols)
static void insertCommonAndDeclsForFunction(SgStatement* funcStart, const map<string, SgSymbol*>& symbols)
{
if (symbols.empty())
return;
@@ -90,7 +92,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
if (!funcStart)
return;
const string commonBlockName = "propagation_common__";
const string commonBlockName = "__propagation_common__";
SgStatement* funcEnd = funcStart->lastNodeOfStmt();
SgStatement* commonStat = NULL;
@@ -130,7 +132,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
}
SgExpression* varList = makeExprList(varRefs, false);
SgStatement* insertAfter = FindLastDeclStatement(funcStart);
SgStatement* insertAfter = findLastDeclStatement(funcStart);
for (const auto& [name, sym] : symbols)
{
if (!sym)
@@ -149,7 +151,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
insertAfter->insertStmtAfter(*declStmt, *funcStart);
insertAfter = declStmt;
statementsToRemove.insert(declStmt);
statementsToRemove[declStmt->fileName()].insert(declStmt);
}
if (!commonList)
@@ -164,14 +166,12 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
commonStat->setlineNumber(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<string, SgExpression*>& arrayToVariable, int& variableNumber)
static bool transformRightPart(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable)
{
if (!exp)
return false;
@@ -204,13 +205,13 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, map<string, S
string expUnparsed;
SgExpression* toAdd = NULL;
if (isArrayRef(exp) && CheckConstIndexes(exp->lhs()))
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, map<string, S
st->setExpression(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<string, S
}
}
else
isChanged = isChanged || TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber);
isChanged = isChanged || transformRightPart(st, subnodes[i], arrayToVariable);
}
return isChanged;
}
static void TransformLeftPart(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
static void transformLeftPart(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable)
{
if (!st || !st->expr(1))
return;
@@ -262,10 +264,11 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, map<string, Sg
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());
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, map<string, Sg
newStatement->setLocalLineNumber(st->lineNumber());
changed.insert(st);
statementsToRemove.insert(newStatement);
statementsToRemove[newStatement->fileName()].insert(newStatement);
}
static void TransformBorder(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
static void transformBorder(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& 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, map<string, SgEx
{
if (st->expr(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<string, SgExpression*>& arrayToVariable, int& variableNumber)
static void checkVariable(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable)
{
SgStatement* firstStatement = declPlace->lexPrev();
st = st->lexPrev();
@@ -316,23 +319,24 @@ static void CheckVariable(SgStatement* st, SgExpression* exp, map<string, SgExpr
{
if (st->variant() == 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<string, SgExpr
}
}
static void findConstValues(
SgProject& project,
const map<string, map<string, SgStatement*>>& borderVars,
const map<string, SgExpression*>& arrayToVariable,
map<string, int>& hitCount,
map<string, map<SgStatement*, vector<pair<string, string>>>>& result)
static void findConstValues(SgProject& project,
const map<string, map<string, SgStatement*>>& borderVars,
const map<string, SgExpression*>& arrayToVariable,
map<string, int>& hitCount,
map<string, map<SgStatement*, vector<pair<string, string>>>>& result)
{
for (int i = 0; i < project.numberOfFiles(); i++)
{
@@ -422,14 +425,14 @@ static void insertDefinition(map<string, map<SgStatement*, vector<pair<string, s
if (insertBefore && insertBefore->controlParent())
{
insertBefore->insertStmtBefore(*asg, *insertBefore->controlParent());
statementsToRemove.insert(asg);
statementsToRemove[asg->fileName()].insert(asg);
}
}
}
}
}
static void applyLeftPartForUnchangedAssignments(SgProject& project, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
static void applyLeftPartForUnchangedAssignments(SgProject& project, map<string, SgExpression*>& arrayToVariable)
{
for (int fi = 0; fi < project.numberOfFiles(); ++fi)
{
@@ -468,28 +471,28 @@ static void applyLeftPartForUnchangedAssignments(SgProject& project, map<string,
continue;
if (!lhs->symbol()->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<string, map<string, SgStatement*>>& borderVars)
@@ -497,21 +500,19 @@ static void getBorderVars(SgExpression* exp, const string& filename, map<string,
if (!exp)
return;
if ((isArrayRef(exp) && CheckConstIndexes(exp->lhs())) || 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<string, SgExpression*>& arrayToVariable,
map<string, map<string, SgStatement*>>& borderVars,
int& variableNumber)
static void processLoopBound(SgStatement* st,
SgExpression* bound,
const string& boundUnparsed,
bool isUpperBound,
map<string, SgExpression*>& arrayToVariable,
map<string, map<string, SgStatement*>>& 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<string, SgExpression*> arrayToVariable;
map<string, map<string, SgStatement*>> 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<string, set<SgStatement*>> 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<string, map<SgStatement*, vector<pair<string, string>>>> result;
map<string, int> 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();
}
}

View File

@@ -8,4 +8,4 @@ using namespace std;
void arrayConstantPropagation(SgProject& project);
void restoreArrays();
void arrayConstantPropagationRrestore();

View File

@@ -1,3 +1,3 @@
#pragma once
#define VERSION_SPF "2483"
#define VERSION_SPF "2484"