Skip to content

Commit 4bbee17

Browse files
committed
[ConstraintElimination] Use ZExtValue for unsigned decomposition.
When decomposing constraints for unsigned conditions, we can use negative values by zero-extending them, as long as they are less than the maximum constraint value. Fixes #54224
1 parent 873e630 commit 4bbee17

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
153153
}
154154

155155
if (auto *CI = dyn_cast<ConstantInt>(V)) {
156-
if (CI->isNegative() || CI->uge(MaxConstraintValue))
156+
if (CI->uge(MaxConstraintValue))
157157
return {};
158-
return {{CI->getSExtValue(), nullptr}};
158+
return {{CI->getZExtValue(), nullptr}};
159159
}
160160
auto *GEP = dyn_cast<GetElementPtrInst>(V);
161161
if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
@@ -208,8 +208,9 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
208208

209209
Value *Op1;
210210
ConstantInt *CI;
211-
if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))))
212-
return {{CI->getSExtValue(), nullptr}, {1, Op0}};
211+
if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
212+
!CI->uge(MaxConstraintValue))
213+
return {{CI->getZExtValue(), nullptr}, {1, Op0}};
213214
if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative()) {
214215
Preconditions.emplace_back(
215216
CmpInst::ICMP_UGE, Op0,

llvm/test/Transforms/ConstraintElimination/add-nuw.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,10 @@ define i1 @add_nuw_neg_pr54224_i16(i16 %a) {
463463
; CHECK-NEXT: br i1 [[C_1]], label [[EXIT_1:%.*]], label [[EXIT_2:%.*]]
464464
; CHECK: exit.1:
465465
; CHECK-NEXT: [[C_2:%.*]] = icmp ugt i16 [[A]], 0
466-
; CHECK-NEXT: ret i1 [[C_2]]
466+
; CHECK-NEXT: ret i1 false
467467
; CHECK: exit.2:
468468
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i16 [[A]], 0
469-
; CHECK-NEXT: ret i1 true
469+
; CHECK-NEXT: ret i1 [[C_3]]
470470
;
471471
entry:
472472
%neg2 = add nuw i16 %a, -305
@@ -493,7 +493,7 @@ define i1 @add_nuw_neg_pr54224_i64(i64 %a) {
493493
; CHECK-NEXT: ret i1 [[C_2]]
494494
; CHECK: exit.2:
495495
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i64 [[A]], 0
496-
; CHECK-NEXT: ret i1 true
496+
; CHECK-NEXT: ret i1 [[C_3]]
497497
;
498498
entry:
499499
%neg2 = add nuw i64 %a, -305
@@ -518,12 +518,12 @@ define i1 @add_nuw_neg2_i8(i8 %a) {
518518
; CHECK: exit.1:
519519
; CHECK-NEXT: [[T_1:%.*]] = icmp ult i8 [[A]], 2
520520
; CHECK-NEXT: [[C_2:%.*]] = icmp ult i8 [[A]], 1
521-
; CHECK-NEXT: [[RES_1:%.*]] = xor i1 [[T_1]], [[C_2]]
521+
; CHECK-NEXT: [[RES_1:%.*]] = xor i1 true, [[C_2]]
522522
; CHECK-NEXT: ret i1 [[RES_1]]
523523
; CHECK: exit.2:
524524
; CHECK-NEXT: [[C_3:%.*]] = icmp ult i8 [[A]], 3
525525
; CHECK-NEXT: [[F_1:%.*]] = icmp ult i8 [[A]], 2
526-
; CHECK-NEXT: [[RES_2:%.*]] = xor i1 [[C_3]], [[F_1]]
526+
; CHECK-NEXT: [[RES_2:%.*]] = xor i1 [[C_3]], false
527527
; CHECK-NEXT: ret i1 [[RES_2]]
528528
;
529529
entry:

0 commit comments

Comments
 (0)