Skip to content

Conversation

@artagnon
Copy link
Contributor

@artagnon artagnon commented Jun 2, 2025

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.

@artagnon artagnon requested a review from nikic as a code owner June 2, 2025 13:04
@llvmbot llvmbot added backend:AMDGPU backend:Hexagon backend:X86 vectorizers llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Jun 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-vectorizers
@llvm/pr-subscribers-backend-amdgpu
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: Ramkumar Ramachandra (artagnon)

Changes

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 --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]

@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-backend-x86

Author: Ramkumar Ramachandra (artagnon)

Changes

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 --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]

@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-backend-hexagon

Author: Ramkumar Ramachandra (artagnon)

Changes

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 --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]

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are build failures in CI.

I think the original idea was that making Depth required avoids mistakes during recursive calls inside ValueTracking. But I agree that from the perspective of an external user, having to always specify Depth=0 doesn't make a lot of sense.

@nikic nikic requested a review from dtcxzyw June 2, 2025 15:08
@artagnon
Copy link
Contributor Author

artagnon commented Jun 2, 2025

There are build failures in CI.

Not sure what's going on with the CI: I just re-verified locally, and the line it points to in ValueTracking.cpp seems to be outdated? I'll try a dummy force-push to re-kick the CI.

EDIT: The buildkite Windows CI seems to have passed, and there's a Flang test failure in the buildkite Linux CI (which is spurious, I assume?). The GitHub CI is the one that seems to be broken.

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.
@artagnon artagnon force-pushed the vt-depth-lastarg branch from f2d3329 to 6e002cd Compare June 3, 2025 13:35
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please wait on a second opinion.

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never made sense

@artagnon artagnon merged commit b40e4ce into llvm:main Jun 3, 2025
11 checks passed
@artagnon artagnon deleted the vt-depth-lastarg branch June 3, 2025 16:12
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 3, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/23770

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/skipped_status_interactive_crashlog.test (2978 of 2989)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/io.test (2979 of 2989)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/json.test (2980 of 2989)
PASS: lldb-unit :: Utility/./UtilityTests/8/129 (2981 of 2989)
UNSUPPORTED: lldb-shell :: Heap/heap-cstr.test (2982 of 2989)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/watchpoint_callback.test (2983 of 2989)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/fail_breakpoint_oneline.test (2984 of 2989)
UNSUPPORTED: lldb-shell :: BuildScript/toolchain-msvc.test (2985 of 2989)
PASS: lldb-api :: terminal/TestEditlineCompletions.py (2986 of 2989)
UNRESOLVED: lldb-api :: tools/lldb-dap/launch/TestDAP_launch.py (2987 of 2989)
******************** TEST 'lldb-api :: tools/lldb-dap/launch/TestDAP_launch.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib --cmake-build-type Release -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/launch -p TestDAP_launch.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision b40e4ceaa61c5f14ca261e2952e7f85a066403e2)
  clang revision b40e4ceaa61c5f14ca261e2952e7f85a066403e2
  llvm revision b40e4ceaa61c5f14ca261e2952e7f85a066403e2
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/launch
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:AMDGPU backend:Hexagon backend:X86 llvm:analysis Includes value tracking, cost tables and constant folding llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms vectorizers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants