Skip to content

Commit 66badf2

Browse files
authored
VT: teach a special-case optz about samesign (#122590)
There is a narrow special-case in isImpliedCondICmps that can benefit from being taught about samesign. Since it costs us nothing to implement it, teach it about samesign, for completeness. This patch marks the completion of the effort to teach ValueTracking about samesign.
1 parent 26b4a0a commit 66badf2

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9495,7 +9495,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
94959495
// must be positive if X >= Y and no overflow".
94969496
// Take SGT as an example: L0:x > L1:y and C >= 0
94979497
// ==> R0:(x -nsw y) < R1:(-C) is false
9498-
if ((LPred == ICmpInst::ICMP_SGT || LPred == ICmpInst::ICMP_SGE) &&
9498+
if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGT) ||
9499+
CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGE)) &&
94999500
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
95009501
if (match(R1, m_NonPositive()) &&
95019502
isImpliedCondMatchingOperands(LPred, RPred) == false)
@@ -9504,7 +9505,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
95049505

95059506
// Take SLT as an example: L0:x < L1:y and C <= 0
95069507
// ==> R0:(x -nsw y) < R1:(-C) is true
9507-
if ((LPred == ICmpInst::ICMP_SLT || LPred == ICmpInst::ICMP_SLE) &&
9508+
if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLT) ||
9509+
CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLE)) &&
95089510
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
95099511
if (match(R1, m_NonNegative()) &&
95109512
isImpliedCondMatchingOperands(LPred, RPred) == true)

llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,7 @@ define i32 @gt_sub_nsw(i32 %x, i32 %y) {
207207
; CHECK: [[TAKEN]]:
208208
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
209209
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
210-
; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
211-
; CHECK-NEXT: [[ABSCOND:%.*]] = icmp samesign ult i32 [[SUB]], -1
212-
; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
213-
; CHECK-NEXT: ret i32 [[ABS]]
210+
; CHECK-NEXT: ret i32 [[ADD]]
214211
; CHECK: [[END]]:
215212
; CHECK-NEXT: ret i32 0
216213
;
@@ -239,10 +236,7 @@ define i32 @ge_sub_nsw(i32 %x, i32 %y) {
239236
; CHECK: [[TAKEN]]:
240237
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
241238
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
242-
; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
243-
; CHECK-NEXT: [[ABSCOND:%.*]] = icmp samesign ult i32 [[SUB]], -1
244-
; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
245-
; CHECK-NEXT: ret i32 [[ABS]]
239+
; CHECK-NEXT: ret i32 [[ADD]]
246240
; CHECK: [[END]]:
247241
; CHECK-NEXT: ret i32 0
248242
;

0 commit comments

Comments
 (0)