Skip to content

Commit 670778c

Browse files
committedJun 19, 2019
[InstCombine] Fold icmp eq/ne (and %x, signbit), 0 -> %x s>=/s< 0 earlier
Summary: To generate simplified IR, make sure fold ``` (X & signbit) ==/!= 0) -> X s>=/s< 0; ``` is scheduled before fold ``` ((X << Y) & C) == 0 -> (X & (C >> Y)) == 0. ``` https://rise4fun.com/Alive/fbdh Reviewers: lebedev.ri, efriedma, spatel, craig.topper Reviewed By: lebedev.ri Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63026 llvm-svn: 363845
1 parent 482269b commit 670778c

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed
 

Diff for: ‎llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+17-10
Original file line numberDiff line numberDiff line change
@@ -1626,20 +1626,34 @@ Instruction *InstCombiner::foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
16261626
Instruction *InstCombiner::foldICmpAndConstConst(ICmpInst &Cmp,
16271627
BinaryOperator *And,
16281628
const APInt &C1) {
1629+
bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
1630+
16291631
// For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
16301632
// TODO: We canonicalize to the longer form for scalars because we have
16311633
// better analysis/folds for icmp, and codegen may be better with icmp.
1632-
if (Cmp.getPredicate() == CmpInst::ICMP_NE && Cmp.getType()->isVectorTy() &&
1633-
C1.isNullValue() && match(And->getOperand(1), m_One()))
1634+
if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isNullValue() &&
1635+
match(And->getOperand(1), m_One()))
16341636
return new TruncInst(And->getOperand(0), Cmp.getType());
16351637

16361638
const APInt *C2;
1637-
if (!match(And->getOperand(1), m_APInt(C2)))
1639+
Value *X;
1640+
if (!match(And, m_And(m_Value(X), m_APInt(C2))))
16381641
return nullptr;
16391642

1643+
// Don't perform the following transforms if the AND has multiple uses
16401644
if (!And->hasOneUse())
16411645
return nullptr;
16421646

1647+
if (Cmp.isEquality() && C1.isNullValue()) {
1648+
// Restrict this fold to single-use 'and' (PR10267).
1649+
// Replace (and X, (1 << size(X)-1) != 0) with X s< 0
1650+
if (C2->isSignMask()) {
1651+
Constant *Zero = Constant::getNullValue(X->getType());
1652+
auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1653+
return new ICmpInst(NewPred, X, Zero);
1654+
}
1655+
}
1656+
16431657
// If the LHS is an 'and' of a truncate and we can widen the and/compare to
16441658
// the input width without changing the value produced, eliminate the cast:
16451659
//
@@ -2788,13 +2802,6 @@ Instruction *InstCombiner::foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
27882802
if (!BO->hasOneUse())
27892803
break;
27902804

2791-
// Replace (and X, (1 << size(X)-1) != 0) with x s< 0
2792-
if (BOC->isSignMask()) {
2793-
Constant *Zero = Constant::getNullValue(BOp0->getType());
2794-
auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
2795-
return new ICmpInst(NewPred, BOp0, Zero);
2796-
}
2797-
27982805
// ((X & ~7) == 0) --> X < 8
27992806
if (C.isNullValue() && (~(*BOC) + 1).isPowerOf2()) {
28002807
Constant *NegBOC = ConstantExpr::getNeg(cast<Constant>(BOp1));

Diff for: ‎llvm/test/Transforms/InstCombine/lshr-and-signbit-icmpeq-zero.ll

+12-18
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
define i1 @scalar_i8_lshr_and_signbit_eq(i8 %x, i8 %y) {
1111
; CHECK-LABEL: @scalar_i8_lshr_and_signbit_eq(
12-
; CHECK-NEXT: [[TMP1:%.*]] = shl i8 -128, [[Y:%.*]]
13-
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], [[X:%.*]]
14-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP2]], 0
12+
; CHECK-NEXT: [[LSHR:%.*]] = lshr i8 [[X:%.*]], [[Y:%.*]]
13+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[LSHR]], -1
1514
; CHECK-NEXT: ret i1 [[R]]
1615
;
1716
%lshr = lshr i8 %x, %y
@@ -22,9 +21,8 @@ define i1 @scalar_i8_lshr_and_signbit_eq(i8 %x, i8 %y) {
2221

2322
define i1 @scalar_i16_lshr_and_signbit_eq(i16 %x, i16 %y) {
2423
; CHECK-LABEL: @scalar_i16_lshr_and_signbit_eq(
25-
; CHECK-NEXT: [[TMP1:%.*]] = shl i16 -32768, [[Y:%.*]]
26-
; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[TMP1]], [[X:%.*]]
27-
; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[TMP2]], 0
24+
; CHECK-NEXT: [[LSHR:%.*]] = lshr i16 [[X:%.*]], [[Y:%.*]]
25+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i16 [[LSHR]], -1
2826
; CHECK-NEXT: ret i1 [[R]]
2927
;
3028
%lshr = lshr i16 %x, %y
@@ -35,9 +33,8 @@ define i1 @scalar_i16_lshr_and_signbit_eq(i16 %x, i16 %y) {
3533

3634
define i1 @scalar_i32_lshr_and_signbit_eq(i32 %x, i32 %y) {
3735
; CHECK-LABEL: @scalar_i32_lshr_and_signbit_eq(
38-
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 -2147483648, [[Y:%.*]]
39-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X:%.*]]
40-
; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[TMP2]], 0
36+
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
37+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i32 [[LSHR]], -1
4138
; CHECK-NEXT: ret i1 [[R]]
4239
;
4340
%lshr = lshr i32 %x, %y
@@ -48,9 +45,8 @@ define i1 @scalar_i32_lshr_and_signbit_eq(i32 %x, i32 %y) {
4845

4946
define i1 @scalar_i64_lshr_and_signbit_eq(i64 %x, i64 %y) {
5047
; CHECK-LABEL: @scalar_i64_lshr_and_signbit_eq(
51-
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 -9223372036854775808, [[Y:%.*]]
52-
; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], [[X:%.*]]
53-
; CHECK-NEXT: [[R:%.*]] = icmp eq i64 [[TMP2]], 0
48+
; CHECK-NEXT: [[LSHR:%.*]] = lshr i64 [[X:%.*]], [[Y:%.*]]
49+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i64 [[LSHR]], -1
5450
; CHECK-NEXT: ret i1 [[R]]
5551
;
5652
%lshr = lshr i64 %x, %y
@@ -61,9 +57,8 @@ define i1 @scalar_i64_lshr_and_signbit_eq(i64 %x, i64 %y) {
6157

6258
define i1 @scalar_i32_lshr_and_signbit_ne(i32 %x, i32 %y) {
6359
; CHECK-LABEL: @scalar_i32_lshr_and_signbit_ne(
64-
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 -2147483648, [[Y:%.*]]
65-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X:%.*]]
66-
; CHECK-NEXT: [[R:%.*]] = icmp ne i32 [[TMP2]], 0
60+
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
61+
; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[LSHR]], 0
6762
; CHECK-NEXT: ret i1 [[R]]
6863
;
6964
%lshr = lshr i32 %x, %y
@@ -76,9 +71,8 @@ define i1 @scalar_i32_lshr_and_signbit_ne(i32 %x, i32 %y) {
7671

7772
define <4 x i1> @vec_4xi32_lshr_and_signbit_eq(<4 x i32> %x, <4 x i32> %y) {
7873
; CHECK-LABEL: @vec_4xi32_lshr_and_signbit_eq(
79-
; CHECK-NEXT: [[TMP1:%.*]] = shl <4 x i32> <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>, [[Y:%.*]]
80-
; CHECK-NEXT: [[TMP2:%.*]] = and <4 x i32> [[TMP1]], [[X:%.*]]
81-
; CHECK-NEXT: [[R:%.*]] = icmp eq <4 x i32> [[TMP2]], zeroinitializer
74+
; CHECK-NEXT: [[LSHR:%.*]] = lshr <4 x i32> [[X:%.*]], [[Y:%.*]]
75+
; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[LSHR]], <i32 -1, i32 -1, i32 -1, i32 -1>
8276
; CHECK-NEXT: ret <4 x i1> [[R]]
8377
;
8478
%lshr = lshr <4 x i32> %x, %y

Diff for: ‎llvm/test/Transforms/InstCombine/shl-and-signbit-icmpeq-zero.ll

+12-18
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
define i1 @scalar_i8_shl_and_signbit_eq(i8 %x, i8 %y) {
1111
; CHECK-LABEL: @scalar_i8_shl_and_signbit_eq(
12-
; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -128, [[Y:%.*]]
13-
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], [[X:%.*]]
14-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP2]], 0
12+
; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], [[Y:%.*]]
13+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[SHL]], -1
1514
; CHECK-NEXT: ret i1 [[R]]
1615
;
1716
%shl = shl i8 %x, %y
@@ -22,9 +21,8 @@ define i1 @scalar_i8_shl_and_signbit_eq(i8 %x, i8 %y) {
2221

2322
define i1 @scalar_i16_shl_and_signbit_eq(i16 %x, i16 %y) {
2423
; CHECK-LABEL: @scalar_i16_shl_and_signbit_eq(
25-
; CHECK-NEXT: [[TMP1:%.*]] = lshr i16 -32768, [[Y:%.*]]
26-
; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[TMP1]], [[X:%.*]]
27-
; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[TMP2]], 0
24+
; CHECK-NEXT: [[SHL:%.*]] = shl i16 [[X:%.*]], [[Y:%.*]]
25+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i16 [[SHL]], -1
2826
; CHECK-NEXT: ret i1 [[R]]
2927
;
3028
%shl = shl i16 %x, %y
@@ -35,9 +33,8 @@ define i1 @scalar_i16_shl_and_signbit_eq(i16 %x, i16 %y) {
3533

3634
define i1 @scalar_i32_shl_and_signbit_eq(i32 %x, i32 %y) {
3735
; CHECK-LABEL: @scalar_i32_shl_and_signbit_eq(
38-
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 -2147483648, [[Y:%.*]]
39-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X:%.*]]
40-
; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[TMP2]], 0
36+
; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
37+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i32 [[SHL]], -1
4138
; CHECK-NEXT: ret i1 [[R]]
4239
;
4340
%shl = shl i32 %x, %y
@@ -48,9 +45,8 @@ define i1 @scalar_i32_shl_and_signbit_eq(i32 %x, i32 %y) {
4845

4946
define i1 @scalar_i64_shl_and_signbit_eq(i64 %x, i64 %y) {
5047
; CHECK-LABEL: @scalar_i64_shl_and_signbit_eq(
51-
; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 -9223372036854775808, [[Y:%.*]]
52-
; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], [[X:%.*]]
53-
; CHECK-NEXT: [[R:%.*]] = icmp eq i64 [[TMP2]], 0
48+
; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[X:%.*]], [[Y:%.*]]
49+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i64 [[SHL]], -1
5450
; CHECK-NEXT: ret i1 [[R]]
5551
;
5652
%shl = shl i64 %x, %y
@@ -61,9 +57,8 @@ define i1 @scalar_i64_shl_and_signbit_eq(i64 %x, i64 %y) {
6157

6258
define i1 @scalar_i32_shl_and_signbit_ne(i32 %x, i32 %y) {
6359
; CHECK-LABEL: @scalar_i32_shl_and_signbit_ne(
64-
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 -2147483648, [[Y:%.*]]
65-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], [[X:%.*]]
66-
; CHECK-NEXT: [[R:%.*]] = icmp ne i32 [[TMP2]], 0
60+
; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
61+
; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[SHL]], 0
6762
; CHECK-NEXT: ret i1 [[R]]
6863
;
6964
%shl = shl i32 %x, %y
@@ -76,9 +71,8 @@ define i1 @scalar_i32_shl_and_signbit_ne(i32 %x, i32 %y) {
7671

7772
define <4 x i1> @vec_4xi32_shl_and_signbit_eq(<4 x i32> %x, <4 x i32> %y) {
7873
; CHECK-LABEL: @vec_4xi32_shl_and_signbit_eq(
79-
; CHECK-NEXT: [[TMP1:%.*]] = lshr <4 x i32> <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>, [[Y:%.*]]
80-
; CHECK-NEXT: [[TMP2:%.*]] = and <4 x i32> [[TMP1]], [[X:%.*]]
81-
; CHECK-NEXT: [[R:%.*]] = icmp eq <4 x i32> [[TMP2]], zeroinitializer
74+
; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], [[Y:%.*]]
75+
; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[SHL]], <i32 -1, i32 -1, i32 -1, i32 -1>
8276
; CHECK-NEXT: ret <4 x i1> [[R]]
8377
;
8478
%shl = shl <4 x i32> %x, %y

0 commit comments

Comments
 (0)
Please sign in to comment.