Skip to content

Commit abb02ec

Browse files
committed
address more comments
1 parent 2856b58 commit abb02ec

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,17 @@ static Value *foldSelectICmpMinMax(const ICmpInst *Cmp, Value *TVal,
573573
const SimplifyQuery &SQ) {
574574
const Value *CmpLHS = Cmp->getOperand(0);
575575
const Value *CmpRHS = Cmp->getOperand(1);
576-
const ICmpInst::Predicate Pred = Cmp->getPredicate();
576+
ICmpInst::Predicate Pred = Cmp->getPredicate();
577577

578578
// (X > Y) ? X : (Y - 1) ==> MIN(X, Y - 1)
579579
// (X < Y) ? X : (Y + 1) ==> MAX(X, Y + 1)
580580
// This transformation is valid when overflow corresponding to the sign of
581581
// the comparison is poison and we must drop the non-matching overflow flag.
582+
if (CmpRHS == TVal) {
583+
std::swap(CmpLHS, CmpRHS);
584+
Pred = CmpInst::getSwappedPredicate(Pred);
585+
}
586+
582587
if (CmpLHS == TVal) {
583588
if (Pred == CmpInst::ICMP_SGT &&
584589
match(FVal, m_NSWAddLike(m_Specific(CmpRHS), m_One()))) {

llvm/test/Transforms/InstCombine/minmax-fold.ll

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,54 @@ define i32 @test_umin_sub1_nuw(i32 %x, i32 range(i32 1, 0) %w) {
16451645
ret i32 %r
16461646
}
16471647

1648+
define i32 @test_smin_sub1_nsw_swapped(i32 %x, i32 %w) {
1649+
; CHECK-LABEL: @test_smin_sub1_nsw_swapped(
1650+
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[W:%.*]], -1
1651+
; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 [[SUB]])
1652+
; CHECK-NEXT: ret i32 [[R]]
1653+
;
1654+
%cmp = icmp sgt i32 %w, %x
1655+
%sub = add nsw i32 %w, -1
1656+
%r = select i1 %cmp, i32 %x, i32 %sub
1657+
ret i32 %r
1658+
}
1659+
1660+
define i32 @test_smax_add1_nsw_swapped(i32 %x, i32 %w) {
1661+
; CHECK-LABEL: @test_smax_add1_nsw_swapped(
1662+
; CHECK-NEXT: [[X2:%.*]] = add nsw i32 [[W:%.*]], 1
1663+
; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 [[X2]])
1664+
; CHECK-NEXT: ret i32 [[R]]
1665+
;
1666+
%cmp = icmp slt i32 %w, %x
1667+
%add = add nsw i32 %w, 1
1668+
%r = select i1 %cmp, i32 %x, i32 %add
1669+
ret i32 %r
1670+
}
1671+
1672+
define i32 @test_umax_add1_nuw_swapped(i32 %x, i32 %w) {
1673+
; CHECK-LABEL: @test_umax_add1_nuw_swapped(
1674+
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[W:%.*]], 1
1675+
; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 [[ADD]])
1676+
; CHECK-NEXT: ret i32 [[R]]
1677+
;
1678+
%cmp = icmp ult i32 %w, %x
1679+
%add = add nuw i32 %w, 1
1680+
%r = select i1 %cmp, i32 %x, i32 %add
1681+
ret i32 %r
1682+
}
1683+
1684+
define i32 @test_umin_sub1_nuw_swapped(i32 %x, i32 range(i32 1, 0) %w) {
1685+
; CHECK-LABEL: @test_umin_sub1_nuw_swapped(
1686+
; CHECK-NEXT: [[SUB:%.*]] = add i32 [[W:%.*]], -1
1687+
; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 [[SUB]])
1688+
; CHECK-NEXT: ret i32 [[R]]
1689+
;
1690+
%cmp = icmp ugt i32 %w, %x
1691+
%sub = add i32 %w, -1
1692+
%r = select i1 %cmp, i32 %x, i32 %sub
1693+
ret i32 %r
1694+
}
1695+
16481696
define <2 x i16> @test_smin_sub1_nsw_vec(<2 x i16> %x, <2 x i16> %w) {
16491697
; CHECK-LABEL: @test_smin_sub1_nsw_vec(
16501698
; CHECK-NEXT: [[SUB:%.*]] = add nsw <2 x i16> [[W:%.*]], splat (i16 -1)

0 commit comments

Comments
 (0)