[PATCH] D112848: [BasicAA] Use saturating multiply if nsw

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 29 14:53:44 PDT 2021


nikic created this revision.
nikic added reviewers: reames, courbet, fhahn, jdoerfert.
Herald added subscribers: jeroen.dobbelaere, arphaman, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

If we know that the var * scale multiplication is nsw, we can use a saturating multiplication (as a good approximation of an nsw multiply). This recovers some cases where the fix from D112611 <https://reviews.llvm.org/D112611> is unnecessarily strict. (This can be further strengthened by using a saturating add, but we currently don't track all the necessary information for that.)

This exposes an issue in our NSW tracking for multiplies. The code was assuming that `(X +nsw Y) *nsw Z` results in `(X *nsw Z) +nsw (Y *nsw Z)` -- however, it is possible that the distributed multiplications overflow, even if the non-distributed one does not. We should discard the nsw flag if the the offset is non-zero. If we just have `(X *nsw Y) *nsw Z` then concluding `X *nsw (Y *nsw Z)` is fine. I'm happy to land this part separately -- I've bundled these because I don't think it's possible to actually cause a miscompile otherwise, due to the specific way the NSW flag was previously used.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112848

Files:
  llvm/lib/Analysis/BasicAliasAnalysis.cpp
  llvm/test/Analysis/BasicAA/assume-index-positive.ll


Index: llvm/test/Analysis/BasicAA/assume-index-positive.ll
===================================================================
--- llvm/test/Analysis/BasicAA/assume-index-positive.ll
+++ llvm/test/Analysis/BasicAA/assume-index-positive.ll
@@ -145,12 +145,12 @@
   ret void
 }
 
-; TODO: Unlike the previous case, %ptr.neg and %ptr.shl can't alias, because
+; Unlike the previous case, %ptr.neg and %ptr.shl can't alias, because
 ; shl nsw of non-negative is non-negative.
 define void @shl_nsw_of_non_negative(i8* %ptr, i64 %a) {
 ; CHECK-LABEL: Function: shl_nsw_of_non_negative
 ; CHECK: NoAlias: i8* %ptr.a, i8* %ptr.neg
-; CHECK: MayAlias: i8* %ptr.neg, i8* %ptr.shl
+; CHECK: NoAlias: i8* %ptr.neg, i8* %ptr.shl
   %a.cmp = icmp sge i64 %a, 0
   call void @llvm.assume(i1 %a.cmp)
   %ptr.neg = getelementptr i8, i8* %ptr, i64 -2
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -360,8 +360,10 @@
   }
 
   LinearExpression mul(const APInt &Other, bool MulIsNSW) const {
-    return LinearExpression(Val, Scale * Other, Offset * Other,
-                            IsNSW && (Other.isOne() || MulIsNSW));
+    // The check for zero offset is necessary, because generally
+    // (X +nsw Y) *nsw Z does not imply (X *nsw Z) +nsw (Y *nsw Z).
+    bool NSW = IsNSW && (Other.isOne() || (MulIsNSW && Offset.isZero()));
+    return LinearExpression(Val, Scale * Other, Offset * Other, NSW);
   }
 };
 }
@@ -1249,12 +1251,14 @@
       CR = CR.intersectWith(
           ConstantRange::fromKnownBits(Known, /* Signed */ true),
           ConstantRange::Signed);
+      CR = Index.Val.evaluateWith(CR).sextOrTrunc(OffsetRange.getBitWidth());
 
       assert(OffsetRange.getBitWidth() == Scale.getBitWidth() &&
              "Bit widths are normalized to MaxPointerSize");
-      OffsetRange = OffsetRange.add(
-          Index.Val.evaluateWith(CR).sextOrTrunc(OffsetRange.getBitWidth())
-                                    .smul_fast(ConstantRange(Scale)));
+      if (Index.IsNSW)
+        OffsetRange = OffsetRange.add(CR.smul_sat(ConstantRange(Scale)));
+      else
+        OffsetRange = OffsetRange.add(CR.smul_fast(ConstantRange(Scale)));
     }
 
     // We now have accesses at two offsets from the same base:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112848.383505.patch
Type: text/x-patch
Size: 2373 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211029/9a5ef728/attachment.bin>


More information about the llvm-commits mailing list