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

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 13:05:32 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/216392.diff


3 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+2-2) 
- (modified) llvm/test/Transforms/ConstraintElimination/shl.ll (+2-4) 
- (modified) llvm/test/Transforms/ConstraintElimination/uge.ll (+26) 


``````````diff
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
+}

``````````

</details>


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


More information about the llvm-commits mailing list