[llvm] [ValueTracking] Fix bit width handling in computeKnownBits() for GEPs (PR #125532)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 3 14:26:30 PST 2025


================
@@ -1426,7 +1426,22 @@ static void computeKnownBitsFromOperator(const Operator *I,
     computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
     // Accumulate the constant indices in a separate variable
     // to minimize the number of calls to computeForAddSub.
-    APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true);
+    unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
+    APInt AccConstIndices(IndexWidth, 0);
+
+    auto AddIndex = [&](KnownBits IndexBits) {
+      if (IndexWidth == BitWidth) {
+        // Note that inbounds does *not* guarantee nsw for the addition, as only
+        // the offset is signed, while the base address is unsigned.
+        Known = KnownBits::add(Known, IndexBits);
----------------
goldsteinn wrote:

Beyond the scope of this PR, but would it be slightly more optimal to start `Known` as zero so we could include `nsw` here (and `nuw` on the final addition) if we have `inbounds`?

https://github.com/llvm/llvm-project/pull/125532


More information about the llvm-commits mailing list