[llvm] 8c3281d - [ConstraintElimination] Use AddOverflow for offset summation.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 25 11:08:44 PDT 2022


Author: Florian Hahn
Date: 2022-03-25T18:08:24Z
New Revision: 8c3281db492efa2d9b3cbe032731f3c3faa3c072

URL: https://github.com/llvm/llvm-project/commit/8c3281db492efa2d9b3cbe032731f3c3faa3c072
DIFF: https://github.com/llvm/llvm-project/commit/8c3281db492efa2d9b3cbe032731f3c3faa3c072.diff

LOG: [ConstraintElimination] Use AddOverflow for offset summation.

Fixes an incorrect transformation due to values overflowing
https://alive2.llvm.org/ce/z/uizoea

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
    llvm/test/Transforms/ConstraintElimination/sub-nuw.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index acf2d4da360dc..dbc7a534467b1 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -27,6 +27,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/DebugCounter.h"
+#include "llvm/Support/MathExtras.h"
 #include "llvm/Transforms/Scalar.h"
 
 #include <string>
@@ -320,8 +321,13 @@ getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
   for (const auto &KV : VariablesB)
     R[GetOrAddIndex(KV.second)] -= KV.first;
 
-  R[0] = Offset1 + Offset2 +
-         (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT) ? -1 : 0);
+  int64_t OffsetSum;
+  if (AddOverflow(Offset1, Offset2, OffsetSum))
+    return {};
+  if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT))
+    if (AddOverflow(OffsetSum, int64_t(-1), OffsetSum))
+      return {};
+  R[0] = OffsetSum;
   Res.Preconditions = std::move(Preconditions);
   return Res;
 }

diff  --git a/llvm/test/Transforms/ConstraintElimination/sub-nuw.ll b/llvm/test/Transforms/ConstraintElimination/sub-nuw.ll
index f2d4990ef6b2e..0af257e3cb5cc 100644
--- a/llvm/test/Transforms/ConstraintElimination/sub-nuw.ll
+++ b/llvm/test/Transforms/ConstraintElimination/sub-nuw.ll
@@ -362,7 +362,7 @@ define i1 @wrapping_offset_sum(i64 %x) {
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[NON_ZERO]])
 ; CHECK-NEXT:    [[ADD:%.*]] = sub nuw i64 [[X]], 9223372036854775802
 ; CHECK-NEXT:    [[ULT:%.*]] = icmp ugt i64 200, [[ADD]]
-; CHECK-NEXT:    ret i1 false
+; CHECK-NEXT:    ret i1 [[ULT]]
 ;
   %non.zero = icmp ugt i64 %x, 0
   call void @llvm.assume(i1 %non.zero)


        


More information about the llvm-commits mailing list