Skip to content

Commit 294726d

Browse files
committed
Reapply "[InstCombine] Folding (icmp eq/ne (and X, -P2), INT_MIN)" (#111236)
The underlying issue with msan was fixed by #113200
1 parent 7c72199 commit 294726d

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

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

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

llvm/test/Transforms/InstCombine/and-or-icmps.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,8 +3335,7 @@ define i1 @icmp_eq_or_z_or_pow2orz_fail_bad_pred2(i8 %x, i8 %y) {
33353335

33363336
define i1 @and_slt_to_mask(i8 %x) {
33373337
; CHECK-LABEL: @and_slt_to_mask(
3338-
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -2
3339-
; CHECK-NEXT: [[AND2:%.*]] = icmp eq i8 [[TMP1]], -128
3338+
; CHECK-NEXT: [[AND2:%.*]] = icmp slt i8 [[X:%.*]], -126
33403339
; CHECK-NEXT: ret i1 [[AND2]]
33413340
;
33423341
%cmp = icmp slt i8 %x, -124

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)