[llvm] [ValueTracking] Make Depth last default arg (NFC) (PR #142384)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 2 06:05:37 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Ramkumar Ramachandra (artagnon)
<details>
<summary>Changes</summary>
Having a finite Depth (or recursion limit) for computeKnownBits is very limiting, but is currently a load-bearing necessity, as all KnownBits are recomputed on each call and there is no caching. As a prerequisite for an effort to remove the recursion limit altogether, either using a clever caching technique, or writing a easily-invalidable KnownBits analysis, make the Depth argument in APIs in ValueTracking uniformly the last argument with a default value. This would aid in removing the argument when the time comes, as many callers that currently pass 0 explicitly are now updated to omit the argument altogether.
---
Patch is 272.00 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/142384.diff
40 Files Affected:
- (modified) llvm/include/llvm/Analysis/ValueTracking.h (+50-45)
- (modified) llvm/include/llvm/Analysis/WithCache.h (+3-3)
- (modified) llvm/include/llvm/Transforms/InstCombine/InstCombiner.h (+24-20)
- (modified) llvm/lib/Analysis/BasicAliasAnalysis.cpp (+1-2)
- (modified) llvm/lib/Analysis/DemandedBits.cpp (+2-2)
- (modified) llvm/lib/Analysis/IVDescriptors.cpp (+1-1)
- (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+33-39)
- (modified) llvm/lib/Analysis/Lint.cpp (+1-2)
- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+7-8)
- (modified) llvm/lib/Analysis/ValueTracking.cpp (+441-436)
- (modified) llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp (+10-12)
- (modified) llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp (+1-2)
- (modified) llvm/lib/Target/AMDGPU/AMDGPULateCodeGenPrepare.cpp (+1-1)
- (modified) llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp (+3-3)
- (modified) llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp (+1-1)
- (modified) llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp (+2-2)
- (modified) llvm/lib/Target/X86/X86PartialReduction.cpp (+2-3)
- (modified) llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp (+1-2)
- (modified) llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h (+2-3)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+5-5)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+16-17)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+10-11)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (+19-21)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+25-28)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineInternal.h (+12-10)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (+1-1)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (+8-10)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+7-9)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp (+10-10)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (+88-87)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (+1-1)
- (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+2-2)
- (modified) llvm/lib/Transforms/Scalar/InferAlignment.cpp (+1-1)
- (modified) llvm/lib/Transforms/Utils/Local.cpp (+1-1)
- (modified) llvm/lib/Transforms/Utils/LowerSwitch.cpp (+1-1)
- (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+2-2)
- (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+5-8)
- (modified) llvm/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp (+2-2)
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+13-13)
- (modified) llvm/unittests/Analysis/ValueTrackingTest.cpp (+44-47)
``````````diff
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index feb7a1fa2cb35..b05b8f349b8d5 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -54,36 +54,37 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6;
/// same width as the vector element, and the bit is set only if it is true
/// for all of the elements in the vector.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known,
- const DataLayout &DL, unsigned Depth = 0,
+ const DataLayout &DL,
AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr,
const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+ bool UseInstrInfo = true, unsigned Depth = 0);
/// Returns the known bits rather than passing by reference.
LLVM_ABI KnownBits computeKnownBits(const Value *V, const DataLayout &DL,
- unsigned Depth = 0,
AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr,
const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+ bool UseInstrInfo = true,
+ unsigned Depth = 0);
/// Returns the known bits rather than passing by reference.
LLVM_ABI KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
- const DataLayout &DL, unsigned Depth = 0,
+ const DataLayout &DL,
AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr,
const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+ bool UseInstrInfo = true,
+ unsigned Depth = 0);
LLVM_ABI KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
- unsigned Depth, const SimplifyQuery &Q);
+ const SimplifyQuery &Q, unsigned Depth = 0);
-LLVM_ABI KnownBits computeKnownBits(const Value *V, unsigned Depth,
- const SimplifyQuery &Q);
+LLVM_ABI KnownBits computeKnownBits(const Value *V, const SimplifyQuery &Q,
+ unsigned Depth = 0);
-LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
- const SimplifyQuery &Q);
+LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known,
+ const SimplifyQuery &Q, unsigned Depth = 0);
/// Compute known bits from the range metadata.
/// \p KnownZero the set of bits that are known to be zero
@@ -93,22 +94,22 @@ LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
/// Merge bits known from context-dependent facts into Known.
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known,
- unsigned Depth,
- const SimplifyQuery &Q);
+ const SimplifyQuery &Q,
+ unsigned Depth = 0);
/// Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I,
const KnownBits &KnownLHS,
const KnownBits &KnownRHS,
- unsigned Depth,
- const SimplifyQuery &SQ);
+ const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Adjust \p Known for the given select \p Arm to include information from the
/// select \p Cond.
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond,
Value *Arm, bool Invert,
- unsigned Depth,
- const SimplifyQuery &Q);
+ const SimplifyQuery &Q,
+ unsigned Depth = 0);
/// Return true if LHS and RHS have no common bits set.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
@@ -121,14 +122,16 @@ LLVM_ABI bool haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
/// vectors of integers. If 'OrZero' is set, then return true if the given
/// value is either a power of two or zero.
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
- bool OrZero = false, unsigned Depth = 0,
+ bool OrZero = false,
AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr,
const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+ bool UseInstrInfo = true,
+ unsigned Depth = 0);
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
- unsigned Depth, const SimplifyQuery &Q);
+ const SimplifyQuery &Q,
+ unsigned Depth = 0);
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI);
@@ -196,21 +199,21 @@ LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask,
/// sign bits for the vector element with the mininum number of known sign
/// bits.
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL,
- unsigned Depth = 0,
AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr,
const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+ bool UseInstrInfo = true,
+ unsigned Depth = 0);
/// Get the upper bound on bit size for this Value \p Op as a signed integer.
/// i.e. x == sext(trunc(x to MaxSignificantBits) to bitwidth(x)).
/// Similar to the APInt::getSignificantBits function.
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op,
const DataLayout &DL,
- unsigned Depth = 0,
AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr,
- const DominatorTree *DT = nullptr);
+ const DominatorTree *DT = nullptr,
+ unsigned Depth = 0);
/// Map a call instruction to an intrinsic ID. Libcalls which have equivalent
/// intrinsics are treated as-if they were intrinsics.
@@ -236,36 +239,36 @@ LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V,
const APInt &DemandedElts,
FPClassTest InterestedClasses,
- unsigned Depth,
- const SimplifyQuery &SQ);
+ const SimplifyQuery &SQ,
+ unsigned Depth = 0);
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V,
FPClassTest InterestedClasses,
- unsigned Depth,
- const SimplifyQuery &SQ);
+ const SimplifyQuery &SQ,
+ unsigned Depth = 0);
LLVM_ABI KnownFPClass computeKnownFPClass(
const Value *V, const DataLayout &DL,
- FPClassTest InterestedClasses = fcAllFlags, unsigned Depth = 0,
+ FPClassTest InterestedClasses = fcAllFlags,
const TargetLibraryInfo *TLI = nullptr, AssumptionCache *AC = nullptr,
const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+ bool UseInstrInfo = true, unsigned Depth = 0);
/// Wrapper to account for known fast math flags at the use instruction.
LLVM_ABI KnownFPClass computeKnownFPClass(
const Value *V, const APInt &DemandedElts, FastMathFlags FMF,
- FPClassTest InterestedClasses, unsigned Depth, const SimplifyQuery &SQ);
+ FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth = 0);
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, FastMathFlags FMF,
FPClassTest InterestedClasses,
- unsigned Depth,
- const SimplifyQuery &SQ);
+ const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return true if we can prove that the specified FP value is never equal to
/// -0.0. Users should use caution when considering PreserveSign
/// denormal-fp-math.
-LLVM_ABI bool cannotBeNegativeZero(const Value *V, unsigned Depth,
- const SimplifyQuery &SQ);
+LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return true if we can prove that the specified FP value is either NaN or
/// never less than -0.0.
@@ -275,30 +278,32 @@ LLVM_ABI bool cannotBeNegativeZero(const Value *V, unsigned Depth,
/// -0 --> true
/// x > +0 --> true
/// x < -0 --> false
-LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, unsigned Depth,
- const SimplifyQuery &SQ);
+LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V,
+ const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return true if the floating-point scalar value is not an infinity or if
/// the floating-point vector value has no infinities. Return false if a value
/// could ever be infinity.
-LLVM_ABI bool isKnownNeverInfinity(const Value *V, unsigned Depth,
- const SimplifyQuery &SQ);
+LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return true if the floating-point value can never contain a NaN or infinity.
-LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, unsigned Depth,
- const SimplifyQuery &SQ);
+LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return true if the floating-point scalar value is not a NaN or if the
/// floating-point vector value has no NaN elements. Return false if a value
/// could ever be NaN.
-LLVM_ABI bool isKnownNeverNaN(const Value *V, unsigned Depth,
- const SimplifyQuery &SQ);
+LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return false if we can prove that the specified FP value's sign bit is 0.
/// Return true if we can prove that the specified FP value's sign bit is 1.
/// Otherwise return std::nullopt.
-LLVM_ABI std::optional<bool>
-computeKnownFPSignBit(const Value *V, unsigned Depth, const SimplifyQuery &SQ);
+LLVM_ABI std::optional<bool> computeKnownFPSignBit(const Value *V,
+ const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Return true if the sign bit of the FP value can be ignored by the user when
/// the value is zero.
diff --git a/llvm/include/llvm/Analysis/WithCache.h b/llvm/include/llvm/Analysis/WithCache.h
index 82c230a32297c..3bf35a889bbf2 100644
--- a/llvm/include/llvm/Analysis/WithCache.h
+++ b/llvm/include/llvm/Analysis/WithCache.h
@@ -22,8 +22,8 @@
namespace llvm {
struct SimplifyQuery;
-LLVM_ABI KnownBits computeKnownBits(const Value *V, unsigned Depth,
- const SimplifyQuery &Q);
+LLVM_ABI KnownBits computeKnownBits(const Value *V, const SimplifyQuery &Q,
+ unsigned Depth);
template <typename Arg> class WithCache {
static_assert(std::is_pointer_v<Arg>, "WithCache requires a pointer type!");
@@ -45,7 +45,7 @@ template <typename Arg> class WithCache {
mutable KnownBits Known;
void calculateKnownBits(const SimplifyQuery &Q) const {
- Known = computeKnownBits(Pointer.getPointer(), 0, Q);
+ Known = computeKnownBits(Pointer.getPointer(), Q, 0);
Pointer.setInt(true);
}
diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
index fa6b60cba15aa..fa313f5290773 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
@@ -430,36 +430,39 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
/// methods should return the value returned by this function.
virtual Instruction *eraseInstFromFunction(Instruction &I) = 0;
- void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
- const Instruction *CxtI) const {
- llvm::computeKnownBits(V, Known, Depth, SQ.getWithInstruction(CxtI));
+ void computeKnownBits(const Value *V, KnownBits &Known,
+ const Instruction *CxtI, unsigned Depth = 0) const {
+ llvm::computeKnownBits(V, Known, SQ.getWithInstruction(CxtI), Depth);
}
- KnownBits computeKnownBits(const Value *V, unsigned Depth,
- const Instruction *CxtI) const {
- return llvm::computeKnownBits(V, Depth, SQ.getWithInstruction(CxtI));
+ KnownBits computeKnownBits(const Value *V, const Instruction *CxtI,
+ unsigned Depth = 0) const {
+ return llvm::computeKnownBits(V, SQ.getWithInstruction(CxtI), Depth);
}
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
- unsigned Depth = 0,
- const Instruction *CxtI = nullptr) {
- return llvm::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
- SQ.getWithInstruction(CxtI));
+ const Instruction *CxtI = nullptr,
+ unsigned Depth = 0) {
+ return llvm::isKnownToBeAPowerOfTwo(V, OrZero, SQ.getWithInstruction(CxtI),
+ Depth);
}
- bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
- const Instruction *CxtI = nullptr) const {
+ bool MaskedValueIsZero(const Value *V, const APInt &Mask,
+ const Instruction *CxtI = nullptr,
+ unsigned Depth = 0) const {
return llvm::MaskedValueIsZero(V, Mask, SQ.getWithInstruction(CxtI), Depth);
}
- unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
- const Instruction *CxtI = nullptr) const {
- return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
+ unsigned ComputeNumSignBits(const Value *Op,
+ const Instruction *CxtI = nullptr,
+ unsigned Depth = 0) const {
+ return llvm::ComputeNumSignBits(Op, DL, &AC, CxtI, &DT, Depth);
}
- unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth = 0,
- const Instruction *CxtI = nullptr) const {
- return llvm::ComputeMaxSignificantBits(Op, DL, Depth, &AC, CxtI, &DT);
+ unsigned ComputeMaxSignificantBits(const Value *Op,
+ const Instruction *CxtI = nullptr,
+ unsigned Depth = 0) const {
+ return llvm::ComputeMaxSignificantBits(Op, DL, &AC, CxtI, &DT, Depth);
}
OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
@@ -507,12 +510,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,
const APInt &DemandedMask, KnownBits &Known,
- unsigned Depth, const SimplifyQuery &Q) = 0;
+ const SimplifyQuery &Q,
+ unsigned Depth = 0) = 0;
bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,
const APInt &DemandedMask, KnownBits &Known) {
return SimplifyDemandedBits(I, OpNo, DemandedMask, Known,
- /*Depth=*/0, SQ.getWithInstruction(I));
+ SQ.getWithInstruction(I));
}
virtual Value *
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index bcc9a71917aaf..b110c2017b9eb 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1261,8 +1261,7 @@ AliasResult BasicAAResult::aliasGEP(
ConstantRange CR = computeConstantRange(Index.Val.V, /* ForSigned */ false,
true, &AC, Index.CxtI);
- KnownBits Known =
- computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT);
+ KnownBits Known = computeKnownBits(Index.Val.V, DL, &AC, Index.CxtI, DT);
CR = CR.intersectWith(
ConstantRange::fromKnownBits(Known, /* Signed */ true),
ConstantRange::Signed);
diff --git a/llvm/lib/Analysis/DemandedBits.cpp b/llvm/lib/Analysis/DemandedBits.cpp
index b538e16f25859..d7e2a3fa4fc59 100644
--- a/llvm/lib/Analysis/DemandedBits.cpp
+++ b/llvm/lib/Analysis/DemandedBits.cpp
@@ -70,11 +70,11 @@ void DemandedBits::determineLiveOperandBits(
const DataLayout &DL = UserI->getDataLayout();
Known = KnownBits(BitWidth);
- computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
+ computeKnownBits(V1, Known, DL, &AC, UserI, &DT);
if (V2) {
Known2 = KnownBits(BitWidth);
- computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
+ computeKnownBits(V2, Known2, DL, &AC, UserI, &DT);
}
};
diff --git a/llvm/lib/Analysis/IVDescriptors.cpp b/llvm/lib/Analysis/IVDescriptors.cpp
index b7c7bcab168cc..d13f2e139ee4a 100644
--- a/llvm/lib/Analysis/IVDescriptors.cpp
+++ b/llvm/lib/Analysis/IVDescriptors.cpp
@@ -111,7 +111,7 @@ static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
// If demanded bits wasn't able to limit the bit width, we can try to use
// value tracking instead. This can be the case, for example, if the value
// may be negative.
- auto NumSignBits = ComputeNumSignBits(Exit, DL, 0, AC, nullptr, DT);
+ auto NumSignBits = ComputeNumSignBits(Exit, DL, AC, nullptr, DT);
auto NumTypeBits = DL.getTypeSizeInBits(Exit->getType());
MaxBitWidth = NumTypeBits - NumSignBits;
KnownBits Bits = computeKnownBits(Exit, DL);
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 12c84c718d4fd..e397a228afee0 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -770,7 +770,7 @@ static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
if (IsNUW)
return Constant::getNullValue(Op0->getType());
- KnownBit...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/142384
More information about the llvm-commits
mailing list