[BasicAA] Use ExtendedValue in VariableGEPIndex (NFC)

Use the ExtendedValue structure which is used for LinearExpression
in VariableGEPIndex as well.
This commit is contained in:
Nikita Popov 2021-09-30 18:18:15 +02:00
parent fc7604a2c0
commit ea02f9caff
1 changed files with 34 additions and 41 deletions

View File

@ -319,6 +319,10 @@ struct ExtendedValue {
// sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
return (!ZExtBits || NUW) && (!SExtBits || NSW);
}
bool hasSameExtensionsAs(const ExtendedValue &Other) const {
return ZExtBits == Other.ZExtBits && SExtBits == Other.SExtBits;
}
};
/// Represents zext(sext(V)) * Scale + Offset.
@ -457,19 +461,10 @@ static unsigned getMaxPointerSize(const DataLayout &DL) {
}
namespace {
// A linear transformation of a Value; this class represents ZExt(SExt(V,
// SExtBits), ZExtBits) * Scale + Offset.
// A linear transformation of a Value; this class represents
// ZExt(SExt(V, SExtBits), ZExtBits) * Scale.
struct VariableGEPIndex {
// An opaque Value - we can't decompose this further.
const Value *V;
// We need to track what extensions we've done as we consider the same Value
// with different extensions as different variables in a GEP's linear
// expression;
// e.g.: if V == -1, then sext(x) != zext(x).
unsigned ZExtBits;
unsigned SExtBits;
ExtendedValue Val;
APInt Scale;
// Context instruction to use when querying information about this index.
@ -483,9 +478,9 @@ struct VariableGEPIndex {
dbgs() << "\n";
}
void print(raw_ostream &OS) const {
OS << "(V=" << V->getName()
<< ", zextbits=" << ZExtBits
<< ", sextbits=" << SExtBits
OS << "(V=" << Val.V->getName()
<< ", zextbits=" << Val.ZExtBits
<< ", sextbits=" << Val.SExtBits
<< ", scale=" << Scale << ")";
}
};
@ -681,9 +676,8 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
// A[x][x] -> x*16 + x*4 -> x*20
// This also ensures that 'x' only appears in the index list once.
for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
if (Decomposed.VarIndices[i].V == LE.Val.V &&
Decomposed.VarIndices[i].ZExtBits == LE.Val.ZExtBits &&
Decomposed.VarIndices[i].SExtBits == LE.Val.SExtBits) {
if (Decomposed.VarIndices[i].Val.V == LE.Val.V &&
Decomposed.VarIndices[i].Val.hasSameExtensionsAs(LE.Val)) {
Scale += Decomposed.VarIndices[i].Scale;
Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
break;
@ -695,8 +689,7 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
Scale = adjustToPointerSize(Scale, PointerSize);
if (!!Scale) {
VariableGEPIndex Entry = {
LE.Val.V, LE.Val.ZExtBits, LE.Val.SExtBits, Scale, CxtI, LE.IsNSW};
VariableGEPIndex Entry = {LE.Val, Scale, CxtI, LE.IsNSW};
Decomposed.VarIndices.push_back(Entry);
}
}
@ -1269,7 +1262,7 @@ AliasResult BasicAAResult::aliasGEP(
// If the Value could change between cycles, then any reasoning about
// the Value this cycle may not hold in the next cycle. We'll just
// give up if we can't determine conditions that hold for every cycle:
const Value *V = DecompGEP1.VarIndices[i].V;
const Value *V = DecompGEP1.VarIndices[i].Val.V;
const Instruction *CxtI = DecompGEP1.VarIndices[i].CxtI;
KnownBits Known = computeKnownBits(V, DL, 0, &AC, CxtI, DT);
@ -1278,7 +1271,8 @@ AliasResult BasicAAResult::aliasGEP(
// Zero-extension widens the variable, and so forces the sign
// bit to zero.
bool IsZExt = DecompGEP1.VarIndices[i].ZExtBits > 0 || isa<ZExtInst>(V);
bool IsZExt =
DecompGEP1.VarIndices[i].Val.ZExtBits > 0 || isa<ZExtInst>(V);
SignKnownZero |= IsZExt;
SignKnownOne &= !IsZExt;
@ -1328,16 +1322,16 @@ AliasResult BasicAAResult::aliasGEP(
if (DecompGEP1.VarIndices.size() == 1) {
// VarIndex = Scale*V.
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
if (isKnownNonZero(Var.V, DL, 0, &AC, Var.CxtI, DT)) {
if (isKnownNonZero(Var.Val.V, DL, 0, &AC, Var.CxtI, DT)) {
// If V != 0 then abs(VarIndex) >= abs(Scale).
MinAbsVarIndex = Var.Scale.abs();
}
ConstantRange R = computeConstantRange(Var.V, true, &AC, Var.CxtI);
ConstantRange R = computeConstantRange(Var.Val.V, true, &AC, Var.CxtI);
if (!R.isFullSet() && !R.isEmptySet()) {
if (Var.SExtBits)
R = R.signExtend(R.getBitWidth() + Var.SExtBits);
if (Var.ZExtBits)
R = R.zeroExtend(R.getBitWidth() + Var.ZExtBits);
if (Var.Val.SExtBits)
R = R.signExtend(R.getBitWidth() + Var.Val.SExtBits);
if (Var.Val.ZExtBits)
R = R.zeroExtend(R.getBitWidth() + Var.Val.ZExtBits);
VarIndexRange = R.sextOrTrunc(Var.Scale.getBitWidth())
.multiply(ConstantRange(Var.Scale));
}
@ -1348,9 +1342,10 @@ AliasResult BasicAAResult::aliasGEP(
// inequality of values across loop iterations.
const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
if (Var0.Scale == -Var1.Scale && Var0.ZExtBits == Var1.ZExtBits &&
Var0.SExtBits == Var1.SExtBits && VisitedPhiBBs.empty() &&
isKnownNonEqual(Var0.V, Var1.V, DL, &AC, /* CxtI */ nullptr, DT))
if (Var0.Scale == -Var1.Scale &&
Var0.Val.hasSameExtensionsAs(Var1.Val) && VisitedPhiBBs.empty() &&
isKnownNonEqual(Var0.Val.V, Var1.Val.V, DL, &AC, /* CxtI */ nullptr,
DT))
MinAbsVarIndex = Var0.Scale.abs();
}
@ -1836,8 +1831,8 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
bool Found = false;
for (auto I : enumerate(DestGEP.VarIndices)) {
VariableGEPIndex &Dest = I.value();
if (!isValueEqualInPotentialCycles(Dest.V, Src.V) ||
Dest.ZExtBits != Src.ZExtBits || Dest.SExtBits != Src.SExtBits)
if (!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V) ||
!Dest.Val.hasSameExtensionsAs(Src.Val))
continue;
// If we found it, subtract off Scale V's from the entry in Dest. If it
@ -1854,8 +1849,7 @@ void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
// If we didn't consume this entry, add it to the end of the Dest list.
if (!Found) {
VariableGEPIndex Entry = {Src.V, Src.ZExtBits, Src.SExtBits,
-Src.Scale, Src.CxtI, Src.IsNSW};
VariableGEPIndex Entry = {Src.Val, -Src.Scale, Src.CxtI, Src.IsNSW};
DestGEP.VarIndices.push_back(Entry);
}
}
@ -1873,8 +1867,8 @@ bool BasicAAResult::constantOffsetHeuristic(
const VariableGEPIndex &Var0 = GEP.VarIndices[0], &Var1 = GEP.VarIndices[1];
if (Var0.ZExtBits != Var1.ZExtBits || Var0.SExtBits != Var1.SExtBits ||
Var0.Scale != -Var1.Scale || Var0.V->getType() != Var1.V->getType())
if (!Var0.Val.hasSameExtensionsAs(Var1.Val) || Var0.Scale != -Var1.Scale ||
Var0.Val.V->getType() != Var1.Val.V->getType())
return false;
// We'll strip off the Extensions of Var0 and Var1 and do another round
@ -1882,11 +1876,10 @@ bool BasicAAResult::constantOffsetHeuristic(
// is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
LinearExpression E0 =
GetLinearExpression(ExtendedValue(Var0.V), DL, 0, AC, DT);
GetLinearExpression(ExtendedValue(Var0.Val.V), DL, 0, AC, DT);
LinearExpression E1 =
GetLinearExpression(ExtendedValue(Var1.V), DL, 0, AC, DT);
if (E0.Scale != E1.Scale || E0.Val.ZExtBits != E1.Val.ZExtBits ||
E0.Val.SExtBits != E1.Val.SExtBits ||
GetLinearExpression(ExtendedValue(Var1.Val.V), DL, 0, AC, DT);
if (E0.Scale != E1.Scale || !E0.Val.hasSameExtensionsAs(E1.Val) ||
!isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V))
return false;