Associativity for MULT_OP and ADD_OP for array indices

This commit is contained in:
Egor Mayorov
2026-05-06 00:45:37 +03:00
parent 4d2ac93f36
commit fba78c7184

View File

@@ -1,7 +1,6 @@
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
@@ -192,6 +191,94 @@ static map<SgStatement*, set<SgStatement*>> analyzeBasicBlockIntraDependencies(c
return normalizeSageString(raw ? string(raw) : string());
};
function<string(SgExpression*)> canonicalExprKey;
canonicalExprKey = [&](SgExpression* expr) -> string
{
if (!expr)
return string();
const int v = expr->variant();
if (v == ADD_OP)
{
vector<string> terms;
function<void(SgExpression*)> flattenAdd = [&](SgExpression* e)
{
if (!e)
return;
if (e->variant() == ADD_OP)
{
flattenAdd(e->lhs());
flattenAdd(e->rhs());
}
else
terms.push_back(canonicalExprKey(e));
};
flattenAdd(expr);
sort(terms.begin(), terms.end());
string joined;
for (size_t i = 0; i < terms.size(); ++i)
{
if (i)
joined += " + ";
joined += terms[i];
}
return joined;
}
if (v == MULT_OP)
{
vector<string> factors;
function<void(SgExpression*)> flattenMult = [&](SgExpression* e)
{
if (!e)
return;
if (e->variant() == MULT_OP)
{
flattenMult(e->lhs());
flattenMult(e->rhs());
}
else
factors.push_back(canonicalExprKey(e));
};
flattenMult(expr);
sort(factors.begin(), factors.end());
string joined;
for (size_t i = 0; i < factors.size(); ++i)
{
if (i)
joined += " * ";
joined += factors[i];
}
return joined;
}
SgExpression *l = expr->lhs();
SgExpression *r = expr->rhs();
if (l && r)
{
if (v == SUBT_OP || v == SUB_OP)
return canonicalExprKey(l) + string(" - ") + canonicalExprKey(r);
if (v == DIV_OP || v == INTEGER_DIV_OP)
return canonicalExprKey(l) + string(" / ") + canonicalExprKey(r);
if (v == EXP_OP)
return canonicalExprKey(l) + string(" ** ") + canonicalExprKey(r);
}
if (l && !r)
{
if (v == MINUS_OP)
return string("-") + canonicalExprKey(l);
if (v == UNARY_ADD_OP)
return canonicalExprKey(l);
if (v == CAST_OP)
return canonicalExprKey(l);
}
return sageExprToString(expr);
};
auto arrayElementKey = [&](SgArrayRefExp* arrayRef) -> string
{
if (!arrayRef)
@@ -212,7 +299,7 @@ static map<SgStatement*, set<SgStatement*>> analyzeBasicBlockIntraDependencies(c
{
if (i)
key += ", ";
key += sageExprToString(arrayRef->subscript(i));
key += canonicalExprKey(arrayRef->subscript(i));
}
key += ")";