Skip to content

Commit 3b73945

Browse files
committed
A more concise implementation of the original swap code;
looser equality constraints, such as nsw/nuw combinations, can be folded
1 parent 23585d3 commit 3b73945

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7728,9 +7728,13 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
77287728
}
77297729
}
77307730

7731-
// In case of a comparison with add/sub instructions having the same operands,
7732-
// check whether cmp operands have same signed no wrap. If so, just compare
7733-
// the sub's second operand and zero.
7731+
// If comparing to add and sub instructions with the same operands, check if
7732+
// the cmp type and add/sub instruction overflow satisfy the following
7733+
// constraints:
7734+
// Signed cmp equals nsw;
7735+
// Unsigned cmp equals nuw;
7736+
// Equal cmp equals nsw or nuw.
7737+
// If yes, only the second operand of sub is compared to zero.
77347738
// For example:
77357739
// %tmp1 = sub nsw i8 %x, %y
77367740
// %tmp2 = add nsw i8 %x, %y
@@ -7742,22 +7746,21 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
77427746
Value *A, *B;
77437747
auto *I0 = dyn_cast<OverflowingBinaryOperator>(Op0);
77447748
auto *I1 = dyn_cast<OverflowingBinaryOperator>(Op1);
7745-
bool UnsignedCmp = ICmpInst::isUnsigned(Pred);
7746-
bool SignedCmp = ICmpInst::isSigned(Pred);
7747-
bool EqualityCmp = ICmpInst::isEquality(Pred);
7748-
77497749
if (I0 && I1) {
77507750
bool I0NUW = I0->hasNoUnsignedWrap();
77517751
bool I1NUW = I1->hasNoUnsignedWrap();
77527752
bool I0NSW = I0->hasNoSignedWrap();
77537753
bool I1NSW = I1->hasNoSignedWrap();
7754+
bool UnsignedCmp = ICmpInst::isUnsigned(Pred);
7755+
bool SignedCmp = ICmpInst::isSigned(Pred);
7756+
bool EqualityCmp = ICmpInst::isEquality(Pred);
7757+
CmpPredicate CmpPred;
77547758
if ((UnsignedCmp && I0NUW && I1NUW) || (SignedCmp && I0NSW && I1NSW) ||
7755-
(EqualityCmp && ((I0NUW && I1NUW) || (I0NSW && I1NSW)))) {
7756-
if (match(I0, m_Sub(m_Value(A), m_Value(B))) &&
7757-
match(I1, m_Add(m_Specific(A), m_Specific(B)))) {
7758-
return new ICmpInst(I.getSwappedPredicate(), B,
7759+
(EqualityCmp && ((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
7760+
if (match(&I, m_c_ICmp(CmpPred, m_Sub(m_Value(A), m_Value(B)),
7761+
m_c_Add(m_Deferred(A), m_Deferred(B)))))
7762+
return new ICmpInst(CmpPredicate::getSwapped(CmpPred), B,
77597763
ConstantInt::get(Op0->getType(), 0));
7760-
}
77617764
}
77627765
}
77637766
}

llvm/test/Transforms/InstCombine/icmp-subadd.ll

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ define i1 @test-same-operands-sub-add-nsw-nuw-icmp-eq(i8 %a, i8 %b) {
4343
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[B]], 0
4444
; CHECK-NEXT: ret i1 [[CMP]]
4545
;
46-
%sub = sub nuw nsw i8 %a, %b
47-
%add = add nuw nsw i8 %a, %b
46+
%sub = sub nsw i8 %a, %b
47+
%add = add nuw i8 %a, %b
4848
%cmp = icmp eq i8 %sub, %add
4949
ret i1 %cmp
5050
}
@@ -61,13 +61,10 @@ define i1 @test-same-operands-sub-add-nsw-icmp-eq(i8 %a, i8 %b) {
6161
ret i1 %cmp
6262
}
6363

64-
; Should floded by foldICmpCommutative in the future
6564
define i1 @test-add-sub-nsw-icmp-sgt(i8 %a, i8 %b) {
6665
; CHECK-LABEL: define i1 @test-add-sub-nsw-icmp-sgt(
6766
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
68-
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[A]], [[B]]
69-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[A]], [[B]]
70-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[ADD]], [[SUB]]
67+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 0
7168
; CHECK-NEXT: ret i1 [[CMP]]
7269
;
7370
%sub = sub nsw i8 %a, %b
@@ -76,14 +73,10 @@ define i1 @test-add-sub-nsw-icmp-sgt(i8 %a, i8 %b) {
7673
ret i1 %cmp
7774
}
7875

79-
; Should floded by foldICmpCommutative in the future
8076
define i1 @test-add-sub-nuw-icmp-uge(i8 %a, i8 %b) {
8177
; CHECK-LABEL: define i1 @test-add-sub-nuw-icmp-uge(
8278
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
83-
; CHECK-NEXT: [[SUB:%.*]] = sub nuw i8 [[A]], [[B]]
84-
; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[A]], [[B]]
85-
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i8 [[ADD]], [[SUB]]
86-
; CHECK-NEXT: ret i1 [[CMP]]
79+
; CHECK-NEXT: ret i1 true
8780
;
8881
%sub = sub nuw i8 %a, %b
8982
%add = add nuw i8 %a, %b
@@ -106,14 +99,10 @@ define i1 @test-add-sub-nuw-icmp-sge(i8 %a, i8 %b) {
10699
ret i1 %cmp
107100
}
108101

109-
; Should floded by foldICmpCommutative in the future
110102
define i1 @test-add-swap-sub-nuw-icmp-uge(i8 %a, i8 %b) {
111103
; CHECK-LABEL: define i1 @test-add-swap-sub-nuw-icmp-uge(
112104
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
113-
; CHECK-NEXT: [[SUB:%.*]] = sub nuw i8 [[A]], [[B]]
114-
; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[B]], [[A]]
115-
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i8 [[ADD]], [[SUB]]
116-
; CHECK-NEXT: ret i1 [[CMP]]
105+
; CHECK-NEXT: ret i1 true
117106
;
118107
%sub = sub nuw i8 %a, %b
119108
%add = add nuw i8 %b, %a

0 commit comments

Comments
 (0)