Skip to content

Commit a646436

Browse files
committed
[InstCombine] Folding (icmp eq/ne (and X, -P2), INT_MIN)
Folds to `(icmp slt/sge X, (INT_MIN + P2))` Proofs: https://alive2.llvm.org/ce/z/vpNFY5 Closes #110880
1 parent cd6858c commit a646436

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,6 +5015,18 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
50155015
}
50165016
}
50175017

5018+
// (icmp eq/ne (X, -P2), INT_MIN)
5019+
// -> (icmp slt/sge X, INT_MIN + P2)
5020+
if (ICmpInst::isEquality(Pred) && BO0 &&
5021+
match(I.getOperand(1), m_SignMask()) &&
5022+
match(BO0, m_And(m_Value(), m_NegatedPower2OrZero()))) {
5023+
// Will Constant fold.
5024+
Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
5025+
return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
5026+
: ICmpInst::ICMP_SGE,
5027+
BO0->getOperand(0), NewC);
5028+
}
5029+
50185030
{
50195031
// Similar to above: an unsigned overflow comparison may use offset + mask:
50205032
// ((Op1 + C) & C) u< Op1 --> Op1 != 0

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
define i1 @cmp_x_and_negp2_with_eq(i8 %x) {
55
; CHECK-LABEL: @cmp_x_and_negp2_with_eq(
6-
; CHECK-NEXT: [[ANDX:%.*]] = and i8 [[X:%.*]], -2
7-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ANDX]], -128
6+
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], -126
87
; CHECK-NEXT: ret i1 [[R]]
98
;
109
%andx = and i8 %x, -2
@@ -25,8 +24,7 @@ define i1 @cmp_x_and_negp2_with_eq_fail_not_signmask(i8 %x) {
2524

2625
define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) {
2726
; CHECK-LABEL: @cmp_x_and_negp2_with_ne(
28-
; CHECK-NEXT: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], <i8 -8, i8 -16>
29-
; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], <i8 -128, i8 -128>
27+
; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 -121, i8 -113>
3028
; CHECK-NEXT: ret <2 x i1> [[R]]
3129
;
3230
%andx = and <2 x i8> %x, <i8 -8, i8 -16>
@@ -36,8 +34,7 @@ define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) {
3634

3735
define <2 x i1> @cmp_x_and_negp2_with_ne_or_z(<2 x i8> %x) {
3836
; CHECK-LABEL: @cmp_x_and_negp2_with_ne_or_z(
39-
; CHECK-NEXT: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], <i8 0, i8 -16>
40-
; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], <i8 -128, i8 -128>
37+
; CHECK-NEXT: [[R:%.*]] = icmp sge <2 x i8> [[X:%.*]], <i8 -128, i8 -112>
4138
; CHECK-NEXT: ret <2 x i1> [[R]]
4239
;
4340
%andx = and <2 x i8> %x, <i8 0, i8 -16>

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,7 @@ define i1 @test53(i32 %a, i32 %b) {
11161116

11171117
define i1 @test54(i8 %a) {
11181118
; CHECK-LABEL: @test54(
1119-
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], -64
1120-
; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], -128
1119+
; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[A:%.*]], -64
11211120
; CHECK-NEXT: ret i1 [[RET]]
11221121
;
11231122
%ext = zext i8 %a to i32

0 commit comments

Comments
 (0)