[BasicAA] Pass whole DecomposedGEP to subtraction API (NFC)

Rather than separately handling subtraction of offset and variable
indices, make this one operation. Also rewrite the implementation
to use range-based for loops.
This commit is contained in:
Nikita Popov 2021-09-29 22:58:30 +02:00
parent 940755515d
commit 45288edb65
2 changed files with 23 additions and 31 deletions

View File

@ -202,8 +202,8 @@ private:
bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
const SmallVectorImpl<VariableGEPIndex> &Src);
void subtractDecomposedGEPs(DecomposedGEP &DestGEP,
const DecomposedGEP &SrcGEP);
AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
const Value *V2, LocationSize V2Size,

View File

@ -1096,8 +1096,7 @@ AliasResult BasicAAResult::aliasGEP(
// Subtract the GEP2 pointer from the GEP1 pointer to find out their
// symbolic difference.
DecompGEP1.Offset -= DecompGEP2.Offset;
GetIndexDifference(DecompGEP1.VarIndices, DecompGEP2.VarIndices);
subtractDecomposedGEPs(DecompGEP1, DecompGEP2);
// If an inbounds GEP would have to start from an out of bounds address
// for the two to alias, then we can assume noalias.
@ -1729,43 +1728,36 @@ bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
}
/// Computes the symbolic difference between two de-composed GEPs.
///
/// Dest and Src are the variable indices from two decomposed GetElementPtr
/// instructions GEP1 and GEP2 which have common base pointers.
void BasicAAResult::GetIndexDifference(
SmallVectorImpl<VariableGEPIndex> &Dest,
const SmallVectorImpl<VariableGEPIndex> &Src) {
if (Src.empty())
return;
for (unsigned i = 0, e = Src.size(); i != e; ++i) {
const Value *V = Src[i].V;
unsigned ZExtBits = Src[i].ZExtBits, SExtBits = Src[i].SExtBits;
APInt Scale = Src[i].Scale;
void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
const DecomposedGEP &SrcGEP) {
DestGEP.Offset -= SrcGEP.Offset;
for (const VariableGEPIndex &Src : SrcGEP.VarIndices) {
// Find V in Dest. This is N^2, but pointer indices almost never have more
// than a few variable indexes.
for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
if (!isValueEqualInPotentialCycles(Dest[j].V, V) ||
Dest[j].ZExtBits != ZExtBits || Dest[j].SExtBits != SExtBits)
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)
continue;
// If we found it, subtract off Scale V's from the entry in Dest. If it
// goes to zero, remove the entry.
if (Dest[j].Scale != Scale) {
Dest[j].Scale -= Scale;
Dest[j].IsNSW = false;
} else
Dest.erase(Dest.begin() + j);
Scale = 0;
if (Dest.Scale != Src.Scale) {
Dest.Scale -= Src.Scale;
Dest.IsNSW = false;
} else {
DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() + I.index());
}
Found = true;
break;
}
// If we didn't consume this entry, add it to the end of the Dest list.
if (!!Scale) {
VariableGEPIndex Entry = {V, ZExtBits, SExtBits,
-Scale, Src[i].CxtI, Src[i].IsNSW};
Dest.push_back(Entry);
if (!Found) {
VariableGEPIndex Entry = {Src.V, Src.ZExtBits, Src.SExtBits,
-Src.Scale, Src.CxtI, Src.IsNSW};
DestGEP.VarIndices.push_back(Entry);
}
}
}