[llvm] [ConstraintElimination] Allow pointer types in tightenBoundUsingNe (PR #223215)
Alina Sbirlea via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 12 23:40:29 PDT 2026
https://github.com/alinas created https://github.com/llvm/llvm-project/pull/223215
Allow pointer types in ConstraintInfo::tightenBoundUsingNe() for the unsigned system. When A != B is known and the system already implies A u>= B, tighten the bound to strict inequality A u> B (symmetrically, A u<= B becomes A u< B).
Added lit tests in llvm/test/Transforms/ConstraintElimination/pointer-tighten-ne.ll.
>From fedcf4c660357c7214048b5121e3b472f5cddc57 Mon Sep 17 00:00:00 2001
From: Alina Sbirlea <asbirlea at google.com>
Date: Sun, 13 Sep 2026 04:00:27 +0000
Subject: [PATCH] [ConstraintElimination] Allow pointer types in
tightenBoundUsingNe
Allow pointer types in ConstraintInfo::tightenBoundUsingNe() for the unsigned
system. When A != B is known and the system already implies A u>= B, tighten
the bound to strict inequality A u> B (symmetrically, A u<= B becomes A u< B).
Added lit tests in llvm/test/Transforms/ConstraintElimination/pointer-tighten-ne.ll.
---
.../Scalar/ConstraintElimination.cpp | 4 +-
.../pointer-tighten-ne.ll | 40 +++++++++++++++++++
2 files changed, 43 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/Transforms/ConstraintElimination/pointer-tighten-ne.ll
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index ed95686e9e5d9..c94ecfdbe2f05 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -2121,10 +2121,12 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
void ConstraintInfo::tightenBoundUsingNe(
Value *A, Value *B, unsigned NumIn, unsigned NumOut,
SmallVectorImpl<StackEntry> &DFSInStack) {
- if (!A->getType()->isIntegerTy())
+ if (!A->getType()->isIntegerTy() && !A->getType()->isPointerTy())
return;
for (bool IsSigned : {false, true}) {
+ if (IsSigned && A->getType()->isPointerTy())
+ continue;
// In the unsigned system `A u>= 0` holds for every A, so getConstraint
// already turned `A != 0` into `A u> 0`.
if (!IsSigned && match(B, m_Zero()))
diff --git a/llvm/test/Transforms/ConstraintElimination/pointer-tighten-ne.ll b/llvm/test/Transforms/ConstraintElimination/pointer-tighten-ne.ll
new file mode 100644
index 0000000000000..da177135bf592
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/pointer-tighten-ne.ll
@@ -0,0 +1,40 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+declare void @llvm.assume(i1 noundef)
+
+; Pointer inequality tightening: a u>= b and a != b implies strict a u> b.
+define i1 @test_ptr_tighten_uge_ne(ptr %a, ptr %b) {
+; CHECK-LABEL: define i1 @test_ptr_tighten_uge_ne(
+; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
+; CHECK-NEXT: [[C1:%.*]] = icmp uge ptr [[A]], [[B]]
+; CHECK-NEXT: call void @llvm.assume(i1 [[C1]])
+; CHECK-NEXT: [[C2:%.*]] = icmp ne ptr [[A]], [[B]]
+; CHECK-NEXT: call void @llvm.assume(i1 [[C2]])
+; CHECK-NEXT: ret i1 true
+;
+ %c1 = icmp uge ptr %a, %b
+ call void @llvm.assume(i1 %c1)
+ %c2 = icmp ne ptr %a, %b
+ call void @llvm.assume(i1 %c2)
+ %check = icmp ugt ptr %a, %b
+ ret i1 %check
+}
+
+; Pointer inequality tightening: a u<= b and a != b implies strict a u< b.
+define i1 @test_ptr_tighten_ule_ne(ptr %a, ptr %b) {
+; CHECK-LABEL: define i1 @test_ptr_tighten_ule_ne(
+; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
+; CHECK-NEXT: [[C1:%.*]] = icmp ule ptr [[A]], [[B]]
+; CHECK-NEXT: call void @llvm.assume(i1 [[C1]])
+; CHECK-NEXT: [[C2:%.*]] = icmp ne ptr [[A]], [[B]]
+; CHECK-NEXT: call void @llvm.assume(i1 [[C2]])
+; CHECK-NEXT: ret i1 true
+;
+ %c1 = icmp ule ptr %a, %b
+ call void @llvm.assume(i1 %c1)
+ %c2 = icmp ne ptr %a, %b
+ call void @llvm.assume(i1 %c2)
+ %check = icmp ult ptr %a, %b
+ ret i1 %check
+}
More information about the llvm-commits
mailing list