From: Nikita Popov Date: Thu, 30 Sep 2021 17:09:11 +0000 (+0200) Subject: [BasicAA] Move more extension logic into ExtendedValue (NFC) X-Git-Tag: upstream/15.0.7~30009 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b989211d7db6bcc64bb9148bfb80bab4ebb6887e;p=platform%2Fupstream%2Fllvm.git [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. --- diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp index cb62654..5d9a5f6 100644 --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -314,6 +314,22 @@ struct ExtendedValue { return N; } + KnownBits evaluateWith(KnownBits N) const { + assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() && + "Incompatible bit width"); + if (SExtBits) N = N.sext(N.getBitWidth() + SExtBits); + if (ZExtBits) N = N.zext(N.getBitWidth() + ZExtBits); + return N; + } + + ConstantRange evaluateWith(ConstantRange N) const { + assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() && + "Incompatible bit width"); + if (SExtBits) N = N.signExtend(N.getBitWidth() + SExtBits); + if (ZExtBits) N = N.zeroExtend(N.getBitWidth() + ZExtBits); + return N; + } + bool canDistributeOver(bool NUW, bool NSW) const { // zext(x op y) == zext(x) op zext(y) // sext(x op y) == sext(x) op sext(y) @@ -1247,9 +1263,10 @@ AliasResult BasicAAResult::aliasGEP( bool AllNonNegative = DecompGEP1.Offset.isNonNegative(); bool AllNonPositive = DecompGEP1.Offset.isNonPositive(); for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) { - APInt Scale = DecompGEP1.VarIndices[i].Scale; - APInt ScaleForGCD = DecompGEP1.VarIndices[i].Scale; - if (!DecompGEP1.VarIndices[i].IsNSW) + const VariableGEPIndex &Index = DecompGEP1.VarIndices[i]; + const APInt &Scale = Index.Scale; + APInt ScaleForGCD = Scale; + if (!Index.IsNSW) ScaleForGCD = APInt::getOneBitSet(Scale.getBitWidth(), Scale.countTrailingZeros()); @@ -1259,23 +1276,11 @@ AliasResult BasicAAResult::aliasGEP( GCD = APIntOps::GreatestCommonDivisor(GCD, ScaleForGCD.abs()); if (AllNonNegative || AllNonPositive) { - // 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].Val.V; - const Instruction *CxtI = DecompGEP1.VarIndices[i].CxtI; - - KnownBits Known = computeKnownBits(V, DL, 0, &AC, CxtI, DT); + KnownBits Known = Index.Val.evaluateWith( + computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT)); + // TODO: Account for implicit trunc. bool SignKnownZero = Known.isNonNegative(); bool SignKnownOne = Known.isNegative(); - - // Zero-extension widens the variable, and so forces the sign - // bit to zero. - bool IsZExt = - DecompGEP1.VarIndices[i].Val.ZExtBits > 0 || isa(V); - SignKnownZero |= IsZExt; - SignKnownOne &= !IsZExt; - AllNonNegative &= (SignKnownZero && Scale.isNonNegative()) || (SignKnownOne && Scale.isNonPositive()); AllNonPositive &= (SignKnownZero && Scale.isNonPositive()) || @@ -1326,15 +1331,11 @@ AliasResult BasicAAResult::aliasGEP( // If V != 0 then abs(VarIndex) >= abs(Scale). MinAbsVarIndex = Var.Scale.abs(); } - ConstantRange R = computeConstantRange(Var.Val.V, true, &AC, Var.CxtI); - if (!R.isFullSet() && !R.isEmptySet()) { - if (Var.Val.SExtBits) - R = R.signExtend(R.getBitWidth() + Var.Val.SExtBits); - if (Var.Val.ZExtBits) - R = R.zeroExtend(R.getBitWidth() + Var.Val.ZExtBits); + ConstantRange R = Var.Val.evaluateWith( + computeConstantRange(Var.Val.V, true, &AC, Var.CxtI)); + if (!R.isFullSet() && !R.isEmptySet()) VarIndexRange = R.sextOrTrunc(Var.Scale.getBitWidth()) .multiply(ConstantRange(Var.Scale)); - } } else if (DecompGEP1.VarIndices.size() == 2) { // VarIndex = Scale*V0 + (-Scale)*V1. // If V0 != V1 then abs(VarIndex) >= abs(Scale).