[PATCH] D138416: [ConstraintElim] Support add nsw with negative constants.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 21 04:03:27 PST 2022


fhahn created this revision.
fhahn added reviewers: nikic, fcloutier, zjaffal.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.

`add nsw` with negative constants can be used for reasoning, if the
first operand is larger or equal to the subtracted constant. Then the
value won't cross 0.

Add a precondition to ensure that.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138416

Files:
  llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
  llvm/test/Transforms/ConstraintElimination/add-nsw.ll


Index: llvm/test/Transforms/ConstraintElimination/add-nsw.ll
===================================================================
--- llvm/test/Transforms/ConstraintElimination/add-nsw.ll
+++ llvm/test/Transforms/ConstraintElimination/add-nsw.ll
@@ -775,7 +775,7 @@
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[A_SGE]])
 ; CHECK-NEXT:    [[SUB:%.*]] = add nsw i32 [[A]], -1
 ; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[SUB]], [[A]]
-; CHECK-NEXT:    ret i1 [[C]]
+; CHECK-NEXT:    ret i1 true
 ;
 entry:
   %a.sge = icmp sge i32 %a, 1
@@ -792,7 +792,7 @@
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[A_SGE]])
 ; CHECK-NEXT:    [[SUB:%.*]] = add nsw i32 [[A]], -1
 ; CHECK-NEXT:    [[C:%.*]] = icmp uge i32 [[SUB]], 0
-; CHECK-NEXT:    ret i1 [[C]]
+; CHECK-NEXT:    ret i1 true
 ;
 entry:
   %a.sge = icmp sge i32 %a, 1
@@ -843,7 +843,7 @@
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[A_SGE]])
 ; CHECK-NEXT:    [[SUB:%.*]] = add nsw i32 [[A]], -3
 ; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[SUB]], [[A]]
-; CHECK-NEXT:    ret i1 [[C]]
+; CHECK-NEXT:    ret i1 true
 ;
 entry:
   %a.sge = icmp sge i32 %a, 3
@@ -860,7 +860,7 @@
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[A_SGE]])
 ; CHECK-NEXT:    [[SUB:%.*]] = add nsw i32 [[A]], -3
 ; CHECK-NEXT:    [[C:%.*]] = icmp uge i32 [[SUB]], 0
-; CHECK-NEXT:    ret i1 [[C]]
+; CHECK-NEXT:    ret i1 true
 ;
 entry:
   %a.sge = icmp sge i32 %a, 4
Index: llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -364,7 +364,15 @@
     if (!isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
       Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
                                  ConstantInt::get(Op0->getType(), 0));
-    if (!isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
+    if (auto *C = dyn_cast<ConstantInt>(Op1)) {
+      if (C->isNegative()) {
+        Preconditions.emplace_back(
+            CmpInst::ICMP_SGE, Op0,
+            ConstantInt::get(Op1->getType(), C->getValue() * -1));
+        IsSigned = true;
+      }
+    } else if (!isKnownNonNegative(Op1, DL,
+                                   /*Depth=*/MaxAnalysisRecursionDepth - 1))
       Preconditions.emplace_back(CmpInst::ICMP_SGE, Op1,
                                  ConstantInt::get(Op1->getType(), 0));
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138416.476854.patch
Type: text/x-patch
Size: 2461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221121/993be070/attachment.bin>


More information about the llvm-commits mailing list