Add dependency between reading and writing var value

This commit is contained in:
Egor Mayorov
2026-05-06 03:45:25 +03:00
parent fba78c7184
commit 583fe7775e

View File

@@ -354,22 +354,6 @@ static map<SgStatement*, set<SgStatement*>> analyzeBasicBlockIntraDependencies(c
collectUsedKeysFromExpression(arrayRef->subscript(i), usedKeys);
};
auto addOperatorDependencies = [&](SgStatement* stmt,
const set<string>& usedKeys,
const map<string, SgStatement*>& varDeclarations,
map<SgStatement*, set<SgStatement*>>& operatorsDependencies)
{
if (!stmt)
return;
for (const string& key : usedKeys)
{
auto it = varDeclarations.find(key);
if (it != varDeclarations.end() && it->second && it->second != stmt)
operatorsDependencies[stmt].insert(it->second);
}
};
auto declarationKeyFromLeftPart = [&](SgStatement* stmt) -> string
{
if (!stmt || stmt->variant() != ASSIGN_STAT)
@@ -392,6 +376,31 @@ static map<SgStatement*, set<SgStatement*>> analyzeBasicBlockIntraDependencies(c
return string();
};
auto addOperatorDependencies = [&](SgStatement* stmt,
const set<string>& usedKeys,
const map<string, SgStatement*>& varDeclarations,
const map<string, SgStatement*>& varUsages,
map<SgStatement*, set<SgStatement*>>& operatorsDependencies)
{
if (!stmt)
return;
for (const string& key : usedKeys)
{
auto it = varDeclarations.find(key);
if (it != varDeclarations.end() && it->second && it->second != stmt)
operatorsDependencies[stmt].insert(it->second);
}
const string defKey = declarationKeyFromLeftPart(stmt);
if (!defKey.empty())
{
auto itu = varUsages.find(defKey);
if (itu != varUsages.end() && itu->second && itu->second != stmt)
operatorsDependencies[stmt].insert(itu->second);
}
};
vector<SgStatement*> operatorsOrder;
set<SgStatement*> seenOperators;
@@ -410,6 +419,7 @@ static map<SgStatement*, set<SgStatement*>> analyzeBasicBlockIntraDependencies(c
}
map<string, SgStatement*> varDeclarations;
map<string, SgStatement*> varUsages;
map<SgStatement*, set<SgStatement*>> operatorsDependencies;
for (SgStatement* stmt : operatorsOrder)
@@ -420,11 +430,14 @@ static map<SgStatement*, set<SgStatement*>> analyzeBasicBlockIntraDependencies(c
collectUsedKeysFromExpression(stmt->expr(1), usedKeys);
collectUsedKeysFromArraySubscripts(isSgArrayRefExp(stmt->expr(0)), usedKeys);
}
addOperatorDependencies(stmt, usedKeys, varDeclarations, operatorsDependencies);
addOperatorDependencies(stmt, usedKeys, varDeclarations, varUsages, operatorsDependencies);
const string declarationKey = declarationKeyFromLeftPart(stmt);
if (!declarationKey.empty())
varDeclarations[declarationKey] = stmt;
for (const string& key : usedKeys)
varUsages[key] = stmt;
}
return operatorsDependencies;