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) else if (curr_regime == ARRAY_PROPAGATION)
arrayConstantPropagation(project); arrayConstantPropagation(project);
else if (curr_regime == ARRAY_PROPAGATION_RESTORE) else if (curr_regime == ARRAY_PROPAGATION_RESTORE)
restoreArrays(); arrayConstantPropagationRrestore();
const float elapsed = duration_cast<milliseconds>(high_resolution_clock::now() - timeForPass).count() / 1000.; 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.; 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, SgSymbol*> variablesToAdd;
static map<string, set<SgStatement*>> positionsToAdd; static map<string, set<SgStatement*>> positionsToAdd;
static map<string, string> arrayToName; static map<string, string> arrayToName;
static set<SgStatement*> statementsToRemove; static map<string, set<SgStatement*>> statementsToRemove;
static map<string, map<SgStatement*, SgStatement*>> expToChange; static map<string, map<SgStatement*, SgStatement*>> expToChange;
static int variableNumber = 0;
static bool CheckConstIndexes(SgExpression* exp) static bool checkConstIndexes(SgExpression* exp)
{ {
if (!exp) if (!exp)
{ {
@@ -41,7 +42,7 @@ static bool CheckConstIndexes(SgExpression* exp)
return true; return true;
} }
static SgExpression* CreateVar(int& variableNumber, SgType* type) static SgExpression* createVar(SgType* type)
{ {
string varName = "tmp_prop_var"; string varName = "tmp_prop_var";
string name = varName + std::to_string(variableNumber) + "__"; 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()); return new SgExpression(VAR_REF, NULL, NULL, varSymbol, type->copyPtr());
} }
static SgStatement* FindLastDeclStatement(SgStatement* funcStart) static SgStatement* findLastDeclStatement(SgStatement* funcStart)
{ {
if (!funcStart) if (!funcStart)
return NULL; return NULL;
SgStatement* endSt = funcStart->lastNodeOfStmt(); SgStatement* endSt = funcStart->lastNodeOfStmt();
SgStatement* cur = funcStart->lexNext(); SgStatement* cur = funcStart->lexNext();
SgStatement* lastDecl = funcStart; SgStatement* lastDecl = funcStart;
@@ -82,7 +84,7 @@ static SgStatement* FindLastDeclStatement(SgStatement* funcStart)
return lastDecl; 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()) if (symbols.empty())
return; return;
@@ -90,7 +92,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
if (!funcStart) if (!funcStart)
return; return;
const string commonBlockName = "propagation_common__"; const string commonBlockName = "__propagation_common__";
SgStatement* funcEnd = funcStart->lastNodeOfStmt(); SgStatement* funcEnd = funcStart->lastNodeOfStmt();
SgStatement* commonStat = NULL; SgStatement* commonStat = NULL;
@@ -130,7 +132,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
} }
SgExpression* varList = makeExprList(varRefs, false); SgExpression* varList = makeExprList(varRefs, false);
SgStatement* insertAfter = FindLastDeclStatement(funcStart); SgStatement* insertAfter = findLastDeclStatement(funcStart);
for (const auto& [name, sym] : symbols) for (const auto& [name, sym] : symbols)
{ {
if (!sym) if (!sym)
@@ -149,7 +151,7 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
insertAfter->insertStmtAfter(*declStmt, *funcStart); insertAfter->insertStmtAfter(*declStmt, *funcStart);
insertAfter = declStmt; insertAfter = declStmt;
statementsToRemove.insert(declStmt); statementsToRemove[declStmt->fileName()].insert(declStmt);
} }
if (!commonList) if (!commonList)
@@ -164,14 +166,12 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<st
commonStat->setlineNumber(getNextNegativeLineNumber()); commonStat->setlineNumber(getNextNegativeLineNumber());
commonStat->setExpression(0, commonList); commonStat->setExpression(0, commonList);
SgStatement* lastDecl = FindLastDeclStatement(funcStart); SgStatement* lastDecl = findLastDeclStatement(funcStart);
lastDecl->insertStmtAfter(*commonStat, *funcStart); lastDecl->insertStmtAfter(*commonStat, *funcStart);
statementsToRemove.insert(commonStat); statementsToRemove[commonStat->fileName()].insert(commonStat);
} }
else else
{
commonList->setLhs(varList); commonList->setLhs(varList);
}
} }
@@ -179,6 +179,7 @@ static void copyStatement(SgStatement* st)
{ {
if (!st) if (!st)
return; return;
if (expToChange[st->fileName()].find(st) == expToChange[st->fileName()].end()) if (expToChange[st->fileName()].find(st) == expToChange[st->fileName()].end())
{ {
SgStatement* boundCopy = st->copyPtr(); 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) if (!exp)
return false; return false;
@@ -204,13 +205,13 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, map<string, S
string expUnparsed; string expUnparsed;
SgExpression* toAdd = NULL; SgExpression* toAdd = NULL;
if (isArrayRef(exp) && CheckConstIndexes(exp->lhs())) if (isArrayRef(exp) && checkConstIndexes(exp->lhs()))
{ {
expUnparsed = exp->unparse(); expUnparsed = exp->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp && exp->symbol() && if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp && exp->symbol() &&
exp->symbol()->type() && exp->symbol()->type()->baseType()) 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(); arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse();
} }
positionsToAdd[string(declPlace->fileName())].insert(declPlace); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
@@ -220,16 +221,17 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, map<string, S
st->setExpression(1, newVarExp); st->setExpression(1, newVarExp);
return true; return true;
} }
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
if (subnodes[i] && isArrayRef(subnodes[i]) && subnodes[i]->symbol() && subnodes[i]->symbol()->type() && 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; isChanged = true;
expUnparsed = subnodes[i]->unparse(); expUnparsed = subnodes[i]->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end()) 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(); arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse();
} }
positionsToAdd[string(declPlace->fileName())].insert(declPlace); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
@@ -246,13 +248,13 @@ static bool TransformRightPart(SgStatement* st, SgExpression* exp, map<string, S
} }
} }
else else
isChanged = isChanged || TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber); isChanged = isChanged || transformRightPart(st, subnodes[i], arrayToVariable);
} }
return isChanged; 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)) if (!st || !st->expr(1))
return; return;
@@ -262,10 +264,11 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, map<string, Sg
return; return;
if (changed.find(st) != changed.end()) if (changed.find(st) != changed.end())
return; return;
string expUnparsed = exp->unparse(); string expUnparsed = exp->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType()) 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(); arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse();
} }
positionsToAdd[string(declPlace->fileName())].insert(declPlace); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
@@ -282,16 +285,16 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, map<string, Sg
newStatement->setLocalLineNumber(st->lineNumber()); newStatement->setLocalLineNumber(st->lineNumber());
changed.insert(st); 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) if (!st || !exp)
return; return;
SgStatement* firstStatement = declPlace->lexPrev(); SgStatement* firstStatement = declPlace->lexPrev();
positionsToAdd[string(declPlace->fileName())].insert(declPlace); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
TransformRightPart(st, exp, arrayToVariable, variableNumber); transformRightPart(st, exp, arrayToVariable);
st = st->lexPrev(); st = st->lexPrev();
while (st &&st != firstStatement) while (st &&st != firstStatement)
{ {
@@ -299,16 +302,16 @@ static void TransformBorder(SgStatement* st, SgExpression* exp, map<string, SgEx
{ {
if (st->expr(1)) 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()) 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);
} }
st = st->lexPrev(); 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(); SgStatement* firstStatement = declPlace->lexPrev();
st = st->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 (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); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
} }
} }
if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end()) if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{ {
if (st->expr(1)) 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); 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); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
} }
} }
@@ -340,12 +344,11 @@ static void CheckVariable(SgStatement* st, SgExpression* exp, map<string, SgExpr
} }
} }
static void findConstValues( static void findConstValues(SgProject& project,
SgProject& project, const map<string, map<string, SgStatement*>>& borderVars,
const map<string, map<string, SgStatement*>>& borderVars, const map<string, SgExpression*>& arrayToVariable,
const map<string, SgExpression*>& arrayToVariable, map<string, int>& hitCount,
map<string, int>& hitCount, map<string, map<SgStatement*, vector<pair<string, string>>>>& result)
map<string, map<SgStatement*, vector<pair<string, string>>>>& result)
{ {
for (int i = 0; i < project.numberOfFiles(); i++) 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()) if (insertBefore && insertBefore->controlParent())
{ {
insertBefore->insertStmtBefore(*asg, *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) for (int fi = 0; fi < project.numberOfFiles(); ++fi)
{ {
@@ -468,28 +471,28 @@ static void applyLeftPartForUnchangedAssignments(SgProject& project, map<string,
continue; continue;
if (!lhs->symbol()->type()->baseType()) if (!lhs->symbol()->type()->baseType())
continue; continue;
if (!CheckConstIndexes(lhs->lhs())) if (!checkConstIndexes(lhs->lhs()))
continue; continue;
const string lhsUnparsed = lhs->unparse(); const string lhsUnparsed = lhs->unparse();
if (arrayToVariable.find(lhsUnparsed) == arrayToVariable.end()) if (arrayToVariable.find(lhsUnparsed) == arrayToVariable.end())
continue; continue;
TransformLeftPart(st, lhs, arrayToVariable, variableNumber); transformLeftPart(st, lhs, arrayToVariable);
} }
} }
} }
} }
static bool ContainsArrayRefRecursive(SgExpression* exp) static bool containsArrayRefRecursive(SgExpression* exp)
{ {
if (!exp) if (!exp)
return false; return false;
if (isArrayRef(exp) && CheckConstIndexes(exp->lhs())) if (isArrayRef(exp) && checkConstIndexes(exp->lhs()))
return true; 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) 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) if (!exp)
return; 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; borderVars[filename][string(exp->unparse())] = declPlace;
getBorderVars(exp->lhs(), filename, borderVars); getBorderVars(exp->lhs(), filename, borderVars);
getBorderVars(exp->rhs(), filename, borderVars); getBorderVars(exp->rhs(), filename, borderVars);
} }
static void processLoopBound( static void processLoopBound(SgStatement* st,
SgStatement* st, SgExpression* bound,
SgExpression* bound, const string& boundUnparsed,
const string& boundUnparsed, bool isUpperBound,
bool isUpperBound, map<string, SgExpression*>& arrayToVariable,
map<string, SgExpression*>& arrayToVariable, map<string, map<string, SgStatement*>>& borderVars)
map<string, map<string, SgStatement*>>& borderVars,
int& variableNumber)
{ {
if (!bound || !st) if (!bound || !st)
return; return;
@@ -520,36 +521,40 @@ static void processLoopBound(
getBorderVars(exp, st->fileName(), borderVars); getBorderVars(exp, st->fileName(), borderVars);
if (ContainsArrayRefRecursive(exp), borderVars, st->fileName()) if (containsArrayRefRecursive(exp), borderVars, st->fileName())
{ {
copyStatement(st); copyStatement(st);
TransformBorder(st, bound, arrayToVariable, variableNumber); transformBorder(st, bound, arrayToVariable);
positionsToAdd[string(declPlace->fileName())].insert(declPlace); positionsToAdd[string(declPlace->fileName())].insert(declPlace);
} }
else if (bound->variant() == VAR_REF) else if (bound->variant() == VAR_REF)
CheckVariable(st, bound, arrayToVariable, variableNumber); checkVariable(st, bound, arrayToVariable);
} }
void arrayConstantPropagation(SgProject& project) void arrayConstantPropagation(SgProject& project)
{ {
map<string, SgExpression*> arrayToVariable; map<string, SgExpression*> arrayToVariable;
map<string, map<string, SgStatement*>> borderVars; map<string, map<string, SgStatement*>> borderVars;
int variableNumber = 0;
for (int i = 0; i < project.numberOfFiles(); i++) for (int i = 0; i < project.numberOfFiles(); i++)
{ {
SgFile* file = &(project.file(i)); SgFile *file = &(project.file(i));
if (!file) if (!file)
continue; printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
SgFile::switchToFile(file->filename());
if (SgFile::switchToFile(file->filename()) == -1)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
const int funcNum = file->numberOfFunctions(); const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i) for (int i = 0; i < funcNum; ++i)
{ {
SgStatement* st = file->functions(i); SgStatement* st = file->functions(i);
if (!st) if (!st)
continue; printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
declPlace = st; declPlace = st;
SgStatement* lastNode = st->lastNodeOfStmt(); SgStatement* lastNode = st->lastNodeOfStmt();
@@ -568,20 +573,22 @@ void arrayConstantPropagation(SgProject& project)
string lowerBoundUnparsed = lowerBound->unparse(); string lowerBoundUnparsed = lowerBound->unparse();
string upperBoundUnparsed = upperBound->unparse(); string upperBoundUnparsed = upperBound->unparse();
processLoopBound(st, st->expr(0), upperBoundUnparsed, true, arrayToVariable, borderVars, variableNumber); processLoopBound(st, st->expr(0), upperBoundUnparsed, true, arrayToVariable, borderVars);
processLoopBound(st, st->expr(0), lowerBoundUnparsed, false, arrayToVariable, borderVars, variableNumber); processLoopBound(st, st->expr(0), lowerBoundUnparsed, false, arrayToVariable, borderVars);
} }
} }
} }
} }
applyLeftPartForUnchangedAssignments(project, arrayToVariable, variableNumber);
applyLeftPartForUnchangedAssignments(project, arrayToVariable);
map<string, set<SgStatement*>> funcStarts; map<string, set<SgStatement*>> funcStarts;
for (const auto& [fileName, statements] : positionsToAdd) for (const auto& [fileName, statements] : positionsToAdd)
{ {
int res = SgFile::switchToFile(fileName); int res = SgFile::switchToFile(fileName);
if (res == -1) if (res == -1)
continue; printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
for (SgStatement* st : statements) for (SgStatement* st : statements)
{ {
SgStatement* scope = isSgProgHedrStmt(st) ? st : st->controlParent(); SgStatement* scope = isSgProgHedrStmt(st) ? st : st->controlParent();
@@ -589,42 +596,44 @@ void arrayConstantPropagation(SgProject& project)
funcStarts[fileName].insert(scope); funcStarts[fileName].insert(scope);
} }
} }
for (const auto& [fileName, statements] : funcStarts) for (const auto& [fileName, statements] : funcStarts)
{ {
SgFile::switchToFile(fileName); SgFile::switchToFile(fileName);
for (SgStatement* st : statements) for (SgStatement* st : statements)
{ {
InsertCommonAndDeclsForFunction(st, variablesToAdd); insertCommonAndDeclsForFunction(st, variablesToAdd);
} }
} }
map<string, map<SgStatement*, vector<pair<string, string>>>> result; map<string, map<SgStatement*, vector<pair<string, string>>>> result;
map<string, int> hitCount; map<string, int> hitCount;
findConstValues(project, borderVars, arrayToVariable, hitCount, result); findConstValues(project, borderVars, arrayToVariable, hitCount, result);
insertDefinition(result, hitCount); insertDefinition(result, hitCount);
} }
void restoreArrays() void arrayConstantPropagationRrestore()
{ {
for (auto& [filename, statements] : expToChange) for (auto& [filename, statements] : expToChange)
{ {
if (SgFile::switchToFile(filename) == -1) if (SgFile::switchToFile(filename) == -1)
continue; printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
for (auto& [statement, statementCopy] : statements) for (auto& [statement, statementCopy] : statements)
{ {
if (statement && statementCopy) if (statement && statementCopy)
{
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{
statement->setExpression(i, statementCopy->expr(i)); statement->setExpression(i, statementCopy->expr(i));
}
}
} }
} }
for (SgStatement* st : statementsToRemove) for (auto& [filename, statements] : statementsToRemove)
{ {
SgFile::switchToFile(st->fileName()); if (SgFile::switchToFile(filename) == -1)
st->deleteStmt(); 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 arrayConstantPropagation(SgProject& project);
void restoreArrays(); void arrayConstantPropagationRrestore();

View File

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