[llvm] 367b306 - [ConstraintElimination] Fix trivially true compares with no variables. (#216392)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 15 03:22:03 PDT 2026


Author: Florian Hahn
Date: 2026-08-15T10:21:58Z
New Revision: 367b306e9c12e16621b27e606bfbb765166a6b96

URL: https://github.com/llvm/llvm-project/commit/367b306e9c12e16621b27e606bfbb765166a6b96
DIFF: https://github.com/llvm/llvm-project/commit/367b306e9c12e16621b27e606bfbb765166a6b96.diff

LOG: [ConstraintElimination] Fix trivially true compares with no variables. (#216392)

getConstraintForSolving handles 'X uge 0' and '0 ule X' directly,
returning a constraint that is trivially true. The returned row was sized
Value2Index.size(), which is 0 if the unsigned system does not have any
variables yet, e.g. in a function without arguments. An empty row means
the condition could not be decomposed, so the fast path was discarded and
the compare not simplified.

Size the row to include the entry for the constant part, so it is never
empty.

PR: https://github.com/llvm/llvm-project/pull/216392

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
    llvm/test/Transforms/ConstraintElimination/shl.ll
    llvm/test/Transforms/ConstraintElimination/uge.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index f77426f2b6322..638c7e276d311 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -823,8 +823,8 @@ ConstraintTy ConstraintInfo::getConstraintForSolving(CmpInst::Predicate Pred,
       (Pred == CmpInst::ICMP_UGE && Op1 == NullC)) {
     auto &Value2Index = getValue2Index(false);
     // Return constraint that's trivially true.
-    return ConstraintTy(SmallVector<int64_t, 8>(Value2Index.size(), 0), false,
-                        false, false);
+    return ConstraintTy(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
+                        false, false, false);
   }
 
   // If both operands are known to be non-negative, change signed predicates to

diff  --git a/llvm/test/Transforms/ConstraintElimination/shl.ll b/llvm/test/Transforms/ConstraintElimination/shl.ll
index fe053120ac871..3affdd2a5291f 100644
--- a/llvm/test/Transforms/ConstraintElimination/shl.ll
+++ b/llvm/test/Transforms/ConstraintElimination/shl.ll
@@ -1240,8 +1240,7 @@ define i1 @shl_overflow_2() {
 ; CHECK-LABEL: @shl_overflow_2(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[SHL_UB:%.*]] = shl nuw nsw i256 0, 64
-; CHECK-NEXT:    [[SHL_CMP:%.*]] = icmp uge i256 [[SHL_UB]], 0
-; CHECK-NEXT:    ret i1 [[SHL_CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
 entry:
   %shl.ub = shl nuw nsw i256 0, 64
@@ -1253,8 +1252,7 @@ define i1 @shl_overflow_3() {
 ; CHECK-LABEL: @shl_overflow_3(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[SHL_UB:%.*]] = shl nuw nsw i256 0, 65
-; CHECK-NEXT:    [[SHL_CMP:%.*]] = icmp uge i256 [[SHL_UB]], 0
-; CHECK-NEXT:    ret i1 [[SHL_CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
 entry:
   %shl.ub = shl nuw nsw i256 0, 65

diff  --git a/llvm/test/Transforms/ConstraintElimination/uge.ll b/llvm/test/Transforms/ConstraintElimination/uge.ll
index 2ac078eefe14b..b69286df379f3 100644
--- a/llvm/test/Transforms/ConstraintElimination/uge.ll
+++ b/llvm/test/Transforms/ConstraintElimination/uge.ll
@@ -241,3 +241,29 @@ bb2:
 exit:
   ret i8 20
 }
+
+declare i8 @get()
+
+define i1 @uge_zero_no_function_args() {
+; CHECK-LABEL: @uge_zero_no_function_args(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[X:%.*]] = call i8 @get()
+; CHECK-NEXT:    ret i1 true
+;
+entry:
+  %x = call i8 @get()
+  %c = icmp uge i8 %x, 0
+  ret i1 %c
+}
+
+define i1 @ule_zero_no_function_args() {
+; CHECK-LABEL: @ule_zero_no_function_args(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[X:%.*]] = call i8 @get()
+; CHECK-NEXT:    ret i1 true
+;
+entry:
+  %x = call i8 @get()
+  %c = icmp ule i8 0, %x
+  ret i1 %c
+}


        


More information about the llvm-commits mailing list