Skip to content

Commit 2bd568f

Browse files
committed
[ValueTracking] Infer relationship for the select with SLT
1 parent 3023713 commit 2bd568f

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9119,6 +9119,15 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
91199119
return false;
91209120
}
91219121

9122+
// Take SLT as an example: L0:x < L1:y and C <= 0
9123+
// ==> R0:(x -nsw y) < R1:(-C) is true
9124+
if ((LPred == ICmpInst::ICMP_SLT || LPred == ICmpInst::ICMP_SLE) &&
9125+
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9126+
if (match(R1, m_NonNegative()) &&
9127+
isImpliedCondMatchingOperands(LPred, RPred) == true)
9128+
return true;
9129+
}
9130+
91229131
// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
91239132
if (L0 == R0 &&
91249133
(LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&

llvm/test/Transforms/InstSimplify/select-icmp.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,8 @@ define i32 @pr54735_slt_neg(i32 %x, i32 %y) {
162162
; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
163163
; CHECK: cond.true:
164164
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
165-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], -12
166165
; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], 12
167-
; CHECK-NEXT: [[ABSCOND:%.*]] = icmp sle i32 [[SUB]], 12
168-
; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
169-
; CHECK-NEXT: ret i32 [[ABS]]
166+
; CHECK-NEXT: ret i32 [[NEG]]
170167
; CHECK: cond.end:
171168
; CHECK-NEXT: ret i32 0
172169
;
@@ -194,11 +191,8 @@ define i32 @pr54735_sle_neg(i32 %x, i32 %y) {
194191
; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
195192
; CHECK: cond.true:
196193
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
197-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], -12
198194
; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], 12
199-
; CHECK-NEXT: [[ABSCOND:%.*]] = icmp sle i32 [[SUB]], 12
200-
; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
201-
; CHECK-NEXT: ret i32 [[ABS]]
195+
; CHECK-NEXT: ret i32 [[NEG]]
202196
; CHECK: cond.end:
203197
; CHECK-NEXT: ret i32 0
204198
;

0 commit comments

Comments
 (0)