new changes

This commit is contained in:
2026-04-09 21:56:03 +03:00
committed by ALEXks
parent 8632dfbf31
commit 7bca67b75c
4 changed files with 134 additions and 53 deletions

View File

@@ -3,16 +3,35 @@
#include "../Utils/SgUtils.h" #include "../Utils/SgUtils.h"
#include <iostream> #include <iostream>
#include <functional>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
using namespace std; using namespace std;
namespace {
struct MyHash {
size_t operator()(const SgSymbol* s) const {
return std::hash<std::string_view>{}(s->identifier());
}
};
struct MyEq {
bool operator()(const SgSymbol* a, const SgSymbol* b) const {
return std::strcmp(a->identifier(), b->identifier()) == 0;
}
};
}
SgStatement* declPlace = NULL; SgStatement* declPlace = NULL;
unordered_set<SgStatement*> changed; unordered_set<SgStatement*> changed;
unordered_set<SgSymbol*> variablesToAdd; unordered_set<SgSymbol*, MyHash, MyEq> variablesToAdd;
unordered_set<SgStatement*> positionsToAdd; unordered_set<SgStatement*> positionsToAdd;
unordered_set<SgStatement*> statementsToRemove;
unordered_map<string, vector<pair<SgStatement*, SgStatement*>>> expToChange;
static bool CheckConstIndexes(SgExpression* exp) static bool CheckConstIndexes(SgExpression* exp)
{ {
@@ -76,7 +95,7 @@ static SgStatement* FindLastDeclStatement(SgStatement* funcStart)
return lastDecl; return lastDecl;
} }
static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unordered_set<SgSymbol*>& symbols) static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unordered_set<SgSymbol*, MyHash, MyEq>& symbols)
{ {
if (symbols.empty()) if (symbols.empty())
return; return;
@@ -113,30 +132,15 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unorde
vector<SgExpression*> varRefs; vector<SgExpression*> varRefs;
for (SgSymbol* sym : symbols) 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); SgExpression* varList = makeExprList(varRefs, false);
if (!commonList) SgStatement* insertAfter = FindLastDeclStatement(funcStart);
{
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;
for (SgSymbol* sym : symbols) for (SgSymbol* sym : symbols)
{ {
SgStatement* declStmt = sym->makeVarDeclStmt(); SgStatement* declStmt = sym->makeVarDeclStmt();
@@ -153,28 +157,53 @@ static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unorde
insertAfter->insertStmtAfter(*declStmt, *funcStart); insertAfter->insertStmtAfter(*declStmt, *funcStart);
insertAfter = declStmt; 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<string, SgExpression*>& arrayToVariable, int& variableNumber) static void TransformRightPart(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{ {
if (!exp) if (!exp)
{
return; return;
}
vector<SgExpression*> subnodes = { exp->lhs(), exp->rhs() }; vector<SgExpression*> subnodes = { exp->lhs(), exp->rhs() };
string expUnparsed; string expUnparsed;
SgExpression* toAdd = NULL; SgExpression* toAdd = NULL;
if (exp->variant() == ARRAY_REF && CheckConstIndexes(exp->lhs())) 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()) if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType())
{ {
arrayToVariable[expUnparsed] = CreateVar(variableNumber, 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; return;
} }
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
@@ -183,26 +212,29 @@ static void TransformRightPart(SgStatement* st, SgExpression* exp, unordered_map
{ {
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(variableNumber, subnodes[i]->symbol()->type()->baseType());;
} positionsToAdd.insert(declPlace);
toAdd = arrayToVariable[expUnparsed]->copyPtr(); SgSymbol* builder = arrayToVariable[expUnparsed]->symbol();
auto* sym = new SgSymbol(builder->variant(), builder->identifier(), builder->type(), st->controlParent());
toAdd = new SgVarRefExp(sym);
if (toAdd) if (toAdd)
{ {
if (i == 0) if (i == 0)
{ {
expToChange[st->fileName()].push_back({ st , st->copyPtr() });;
exp->setLhs(toAdd); exp->setLhs(toAdd);
} }
else else
{ {
expToChange[st->fileName()].push_back({ st , st->copyPtr() });;
exp->setRhs(toAdd); exp->setRhs(toAdd);
} }
} }
} }
else else
{
TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber); TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber);
}
} }
} }
@@ -217,6 +249,7 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, unordered_map<
{ {
arrayToVariable[expUnparsed] = CreateVar(variableNumber, 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); SgStatement* newStatement = new SgStatement(ASSIGN_STAT, NULL, NULL, arrayToVariable[expUnparsed]->copyPtr(), st->expr(1)->copyPtr(), NULL);
newStatement->setFileId(st->getFileId()); newStatement->setFileId(st->getFileId());
@@ -226,6 +259,7 @@ static void TransformLeftPart(SgStatement* st, SgExpression* exp, unordered_map<
newStatement->setLocalLineNumber(st->lineNumber()); newStatement->setLocalLineNumber(st->lineNumber());
st->insertStmtBefore(*newStatement, *st->controlParent()); st->insertStmtBefore(*newStatement, *st->controlParent());
changed.insert(st); changed.insert(st);
statementsToRemove.insert(newStatement);
} }
static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber) static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber)
@@ -233,6 +267,7 @@ static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_map<st
SgStatement* firstStatement = declPlace->lexPrev(); SgStatement* firstStatement = declPlace->lexPrev();
st = st->lexPrev(); st = st->lexPrev();
string array = exp->unparse(); string array = exp->unparse();
if (arrayToVariable.find(array) == arrayToVariable.end())
arrayToVariable[array] = CreateVar(variableNumber, exp->symbol()->type()->baseType()); arrayToVariable[array] = CreateVar(variableNumber, exp->symbol()->type()->baseType());
while (st != firstStatement) while (st != firstStatement)
{ {
@@ -243,10 +278,8 @@ static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_map<st
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber); TransformRightPart(st, st->expr(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()) 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); TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber);
} }
}
st = st->lexPrev(); st = st->lexPrev();
} }
} }
@@ -261,16 +294,19 @@ static void CheckVariable(SgStatement* st, SgExpression* exp, unordered_map<stri
if (st->variant() == ASSIGN_STAT && st->expr(0)->symbol() == exp->symbol()) if (st->variant() == ASSIGN_STAT && st->expr(0)->symbol() == exp->symbol())
{ {
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber); 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->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{ {
if (st->expr(1)) if (st->expr(1))
{ {
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber); 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()) 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); TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber);
positionsToAdd.insert(declPlace);
} }
} }
st = st->lexPrev(); st = st->lexPrev();
@@ -287,6 +323,7 @@ void ArrayConstantPropagation(SgProject& project)
if (!file) if (!file)
continue; continue;
SgFile::switchToFile(file->filename());
const int funcNum = file->numberOfFunctions(); const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i) for (int i = 0; i < funcNum; ++i)
{ {
@@ -300,28 +337,37 @@ void ArrayConstantPropagation(SgProject& project)
{ {
SgExpression* lowerBound = st->expr(0)->lhs(); SgExpression* lowerBound = st->expr(0)->lhs();
SgExpression* upperBound = st->expr(0)->rhs(); SgExpression* upperBound = st->expr(0)->rhs();
SgStatement* boundCopy = NULL;
string lowerBoundUnparsed = lowerBound->unparse(), upperBoundUnparsed = upperBound->unparse(); string lowerBoundUnparsed = lowerBound->unparse(), upperBoundUnparsed = upperBound->unparse();
if (upperBound->variant() == ARRAY_REF && upperBound->symbol()->type()->baseType() && CheckConstIndexes(upperBound->lhs())) if (upperBound->variant() == ARRAY_REF && upperBound->symbol()->type()->baseType() && CheckConstIndexes(upperBound->lhs()))
{ {
boundCopy = st->copyPtr();
TransformBorder(st, upperBound, arrayToVariable, variableNumber); TransformBorder(st, upperBound, arrayToVariable, variableNumber);
st->expr(0)->setRhs(arrayToVariable[upperBoundUnparsed]->copyPtr()); st->expr(0)->setRhs(arrayToVariable[upperBoundUnparsed]->copyPtr());
expToChange[st->fileName()].push_back({ st ,boundCopy });;
positionsToAdd.insert(declPlace);
} }
else if (upperBound->variant() == VAR_REF) else if (upperBound->variant() == VAR_REF)
CheckVariable(st, upperBound, arrayToVariable, variableNumber); CheckVariable(st, upperBound, arrayToVariable, variableNumber);
if (lowerBound->variant() == ARRAY_REF && lowerBound->symbol()->type()->baseType() && CheckConstIndexes(lowerBound->lhs())) if (lowerBound->variant() == ARRAY_REF && lowerBound->symbol()->type()->baseType() && CheckConstIndexes(lowerBound->lhs()))
{ {
boundCopy = st->copyPtr();
TransformBorder(st, lowerBound, arrayToVariable, variableNumber); TransformBorder(st, lowerBound, arrayToVariable, variableNumber);
st->expr(0)->setLhs(arrayToVariable[lowerBoundUnparsed]->copyPtr()); st->expr(0)->setLhs(arrayToVariable[lowerBoundUnparsed]->copyPtr());
expToChange[st->fileName()].push_back({ st , boundCopy });;
positionsToAdd.insert(declPlace);
} }
else if (lowerBound->variant() == VAR_REF) else if (lowerBound->variant() == VAR_REF)
CheckVariable(st, lowerBound, arrayToVariable, variableNumber); CheckVariable(st, lowerBound, arrayToVariable, variableNumber);
} }
} }
} }
}
unordered_set<SgStatement*> funcStarts; unordered_set<SgStatement*> funcStarts;
for (SgStatement* st : positionsToAdd) for (SgStatement* st : positionsToAdd)
{ {
SgFile::switchToFile(st->fileName());
SgStatement* scope = st->controlParent(); SgStatement* scope = st->controlParent();
if (scope) if (scope)
funcStarts.insert(scope); funcStarts.insert(scope);
@@ -331,5 +377,4 @@ void ArrayConstantPropagation(SgProject& project)
SgFile::switchToFile(st->fileName()); SgFile::switchToFile(st->fileName());
InsertCommonAndDeclsForFunction(st, variablesToAdd); InsertCommonAndDeclsForFunction(st, variablesToAdd);
} }
}
} }

View File

@@ -1,4 +1,20 @@
#pragma once #pragma once
#include "../Utils/SgUtils.h" #include "../Utils/SgUtils.h"
#include <string>
#include <vector>
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); void ArrayConstantPropagation(SgProject& project);

View File

@@ -7,6 +7,7 @@
#include <numeric> #include <numeric>
#include <iostream> #include <iostream>
#include "ArrayConstantPropagation/propagation.h"
#include "CFGraph/CFGraph.h" #include "CFGraph/CFGraph.h"
#include "Distribution/Array.h" #include "Distribution/Array.h"
#include "graph_loops.h" #include "graph_loops.h"
@@ -21,6 +22,9 @@ using namespace std;
extern std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> declaredArrays; extern std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> declaredArrays;
extern unordered_set<SgStatement*> statementsToRemove;
extern unordered_map<string, vector<pair<SgStatement*, SgStatement*>>> expToChange;
static unordered_set<Region*> collapsed; static unordered_set<Region*> collapsed;
static void RemoveEmptyPoints(ArrayAccessingIndexes& container) static void RemoveEmptyPoints(ArrayAccessingIndexes& container)
@@ -458,4 +462,20 @@ void FindPrivateArrays(map<string, vector<LoopGraph*>>& loopGraph, map<FuncInfo*
AddPrivateArraysToLoop(loop, result[loop], insertedPrivates); AddPrivateArraysToLoop(loop, result[loop], insertedPrivates);
} }
} }
for (SgStatement* st : statementsToRemove)
{
SgFile::switchToFile(st->fileName());
st->deleteStmt();
}
for (auto& [filename, statements] : expToChange)
{
SgFile::switchToFile(filename);
for (auto& [statement, statementCopy] : statements)
{
statement->insertStmtBefore(*statementCopy, *statement->controlParent());
statement->deleteStmt();
}
}
} }

View File

@@ -318,7 +318,7 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); 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({ 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); list({ BUILD_IR, CALL_GRAPH2, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(MOVE_OPERATORS);