Skip to content

Commit b989211

Browse files
committed
[BasicAA] Move more extension logic into ExtendedValue (NFC)
Add methods to appropriately extend KnownBits/ConstantRange there, same as with APInt. Also clean up the known bits handling by actually doing that extension rather than checking ZExtBits. This doesn't matter now, but becomes relevant once truncation is involved.
1 parent 2189548 commit b989211

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@ struct ExtendedValue {
314314
return N;
315315
}
316316

317+
KnownBits evaluateWith(KnownBits N) const {
318+
assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
319+
"Incompatible bit width");
320+
if (SExtBits) N = N.sext(N.getBitWidth() + SExtBits);
321+
if (ZExtBits) N = N.zext(N.getBitWidth() + ZExtBits);
322+
return N;
323+
}
324+
325+
ConstantRange evaluateWith(ConstantRange N) const {
326+
assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
327+
"Incompatible bit width");
328+
if (SExtBits) N = N.signExtend(N.getBitWidth() + SExtBits);
329+
if (ZExtBits) N = N.zeroExtend(N.getBitWidth() + ZExtBits);
330+
return N;
331+
}
332+
317333
bool canDistributeOver(bool NUW, bool NSW) const {
318334
// zext(x op<nuw> y) == zext(x) op<nuw> zext(y)
319335
// sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
@@ -1247,9 +1263,10 @@ AliasResult BasicAAResult::aliasGEP(
12471263
bool AllNonNegative = DecompGEP1.Offset.isNonNegative();
12481264
bool AllNonPositive = DecompGEP1.Offset.isNonPositive();
12491265
for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1250-
APInt Scale = DecompGEP1.VarIndices[i].Scale;
1251-
APInt ScaleForGCD = DecompGEP1.VarIndices[i].Scale;
1252-
if (!DecompGEP1.VarIndices[i].IsNSW)
1266+
const VariableGEPIndex &Index = DecompGEP1.VarIndices[i];
1267+
const APInt &Scale = Index.Scale;
1268+
APInt ScaleForGCD = Scale;
1269+
if (!Index.IsNSW)
12531270
ScaleForGCD = APInt::getOneBitSet(Scale.getBitWidth(),
12541271
Scale.countTrailingZeros());
12551272

@@ -1259,23 +1276,11 @@ AliasResult BasicAAResult::aliasGEP(
12591276
GCD = APIntOps::GreatestCommonDivisor(GCD, ScaleForGCD.abs());
12601277

12611278
if (AllNonNegative || AllNonPositive) {
1262-
// If the Value could change between cycles, then any reasoning about
1263-
// the Value this cycle may not hold in the next cycle. We'll just
1264-
// give up if we can't determine conditions that hold for every cycle:
1265-
const Value *V = DecompGEP1.VarIndices[i].Val.V;
1266-
const Instruction *CxtI = DecompGEP1.VarIndices[i].CxtI;
1267-
1268-
KnownBits Known = computeKnownBits(V, DL, 0, &AC, CxtI, DT);
1279+
KnownBits Known = Index.Val.evaluateWith(
1280+
computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT));
1281+
// TODO: Account for implicit trunc.
12691282
bool SignKnownZero = Known.isNonNegative();
12701283
bool SignKnownOne = Known.isNegative();
1271-
1272-
// Zero-extension widens the variable, and so forces the sign
1273-
// bit to zero.
1274-
bool IsZExt =
1275-
DecompGEP1.VarIndices[i].Val.ZExtBits > 0 || isa<ZExtInst>(V);
1276-
SignKnownZero |= IsZExt;
1277-
SignKnownOne &= !IsZExt;
1278-
12791284
AllNonNegative &= (SignKnownZero && Scale.isNonNegative()) ||
12801285
(SignKnownOne && Scale.isNonPositive());
12811286
AllNonPositive &= (SignKnownZero && Scale.isNonPositive()) ||
@@ -1326,15 +1331,11 @@ AliasResult BasicAAResult::aliasGEP(
13261331
// If V != 0 then abs(VarIndex) >= abs(Scale).
13271332
MinAbsVarIndex = Var.Scale.abs();
13281333
}
1329-
ConstantRange R = computeConstantRange(Var.Val.V, true, &AC, Var.CxtI);
1330-
if (!R.isFullSet() && !R.isEmptySet()) {
1331-
if (Var.Val.SExtBits)
1332-
R = R.signExtend(R.getBitWidth() + Var.Val.SExtBits);
1333-
if (Var.Val.ZExtBits)
1334-
R = R.zeroExtend(R.getBitWidth() + Var.Val.ZExtBits);
1334+
ConstantRange R = Var.Val.evaluateWith(
1335+
computeConstantRange(Var.Val.V, true, &AC, Var.CxtI));
1336+
if (!R.isFullSet() && !R.isEmptySet())
13351337
VarIndexRange = R.sextOrTrunc(Var.Scale.getBitWidth())
13361338
.multiply(ConstantRange(Var.Scale));
1337-
}
13381339
} else if (DecompGEP1.VarIndices.size() == 2) {
13391340
// VarIndex = Scale*V0 + (-Scale)*V1.
13401341
// If V0 != V1 then abs(VarIndex) >= abs(Scale).

0 commit comments

Comments
 (0)