[llvm] 45ff287 - [ConstraintSystem] Fix signed overflow in negate.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 3 13:07:05 PST 2024


Author: Florian Hahn
Date: 2024-12-03T21:06:36Z
New Revision: 45ff28746f5f6350a95d8d9a3e3b3a62b932bce9

URL: https://github.com/llvm/llvm-project/commit/45ff28746f5f6350a95d8d9a3e3b3a62b932bce9
DIFF: https://github.com/llvm/llvm-project/commit/45ff28746f5f6350a95d8d9a3e3b3a62b932bce9.diff

LOG: [ConstraintSystem] Fix signed overflow in negate.

Use AddOverflow for potentially overflowing addition to fixed signed
integer overflow.

Compile-time impact is in the noise
https://llvm-compile-time-tracker.com/compare.php?from=bfb26202e05ee2932b4368b5fca607df01e8247f&to=195b0707148b567c674235e59712458e7ce1bb0e&stat=instructions:u

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/ConstraintSystem.h
    llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/ConstraintSystem.h b/llvm/include/llvm/Analysis/ConstraintSystem.h
index 449852343964ca..01eeadb17db9f0 100644
--- a/llvm/include/llvm/Analysis/ConstraintSystem.h
+++ b/llvm/include/llvm/Analysis/ConstraintSystem.h
@@ -113,7 +113,9 @@ class ConstraintSystem {
   static SmallVector<int64_t, 8> negate(SmallVector<int64_t, 8> R) {
     // The negated constraint R is obtained by multiplying by -1 and adding 1 to
     // the constant.
-    R[0] += 1;
+    if (AddOverflow(R[0], int64_t(1), R[0]))
+      return {};
+
     return negateOrEqual(R);
   }
 

diff  --git a/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll b/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
index 88f87f4afab286..57b7b11be0cf1f 100644
--- a/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
+++ b/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
@@ -38,3 +38,17 @@ exit:
 }
 
 declare void @llvm.assume(i1)
+
+define i1 @negate_overflow_add_1(i64 %x) {
+; CHECK-LABEL: define i1 @negate_overflow_add_1(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[SUB:%.*]] = add nsw i64 [[X]], -9223372036854775807
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 0, [[SUB]]
+; CHECK-NEXT:    ret i1 [[C]]
+;
+entry:
+  %sub = add nsw i64 %x, -9223372036854775807
+  %c = icmp slt i64 0, %sub
+  ret i1 %c
+}


        


More information about the llvm-commits mailing list