[llvm] 626f6ee - [ConstraintElim] Fix integer overflow in getConstraint.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 20 08:14:25 PDT 2023


Author: Florian Hahn
Date: 2023-04-20T16:14:06+01:00
New Revision: 626f6ee7f8f15391de7c4f259e82e701aa1ec20b

URL: https://github.com/llvm/llvm-project/commit/626f6ee7f8f15391de7c4f259e82e701aa1ec20b
DIFF: https://github.com/llvm/llvm-project/commit/626f6ee7f8f15391de7c4f259e82e701aa1ec20b.diff

LOG: [ConstraintElim] Fix integer overflow in getConstraint.

Use SubOverflow to avoid signed integer overflow when combining
coefficients.

Fixes #62226.

Added: 
    llvm/test/Transforms/ConstraintElimination/overflows.ll

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index f87839e15a586..3ce0114903ee0 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -487,7 +487,9 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
   }
 
   for (const auto &KV : VariablesB) {
-    R[GetOrAddIndex(KV.Variable)] -= KV.Coefficient;
+    if (SubOverflow(R[GetOrAddIndex(KV.Variable)], KV.Coefficient,
+                    R[GetOrAddIndex(KV.Variable)]))
+      return {};
     auto I =
         KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
     I.first->second &= KV.IsKnownNonNegative;

diff  --git a/llvm/test/Transforms/ConstraintElimination/overflows.ll b/llvm/test/Transforms/ConstraintElimination/overflows.ll
new file mode 100644
index 0000000000000..9ec05bb7cf03e
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/overflows.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+define i1 @test_overflow_sub_coefficients(i8 %x, i64 %y) {
+; CHECK-LABEL: define i1 @test_overflow_sub_coefficients
+; CHECK-SAME: (i8 [[X:%.*]], i64 [[Y:%.*]]) {
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    [[ZEXT:%.*]] = zext i8 [[X]] to i64
+; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw i64 [[ZEXT]], 63
+; CHECK-NEXT:    [[ICMP:%.*]] = icmp uge i64 [[SHL]], [[Y]]
+; CHECK-NEXT:    ret i1 [[ICMP]]
+;
+bb:
+  %zext = zext i8 %x to i64
+  %shl = shl nuw nsw i64 %zext, 63
+  %icmp = icmp uge i64 %shl, %y
+  ret i1 %icmp
+}
+
+


        


More information about the llvm-commits mailing list