Skip to content

Commit 52bf8d6

Browse files
committed
[InstCombine] Generalize icmp (shl nuw C2, Y), C -> icmp Y, C3
1 parent a12a05b commit 52bf8d6

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,18 +2227,24 @@ Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
22272227
return NewC ? new ICmpInst(Pred, X, NewC) : nullptr;
22282228
}
22292229

2230-
/// Fold icmp (shl 1, Y), C.
2231-
static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
2232-
const APInt &C) {
2230+
/// Fold icmp (shl nuw C2, Y), C.
2231+
static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl,
2232+
const APInt &C) {
22332233
Value *Y;
2234-
if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
2234+
const APInt *C2;
2235+
if (!match(Shl, m_NUWShl(m_APInt(C2), m_Value(Y))))
22352236
return nullptr;
22362237

22372238
Type *ShiftType = Shl->getType();
22382239
unsigned TypeBits = C.getBitWidth();
2239-
bool CIsPowerOf2 = C.isPowerOf2();
22402240
ICmpInst::Predicate Pred = Cmp.getPredicate();
22412241
if (Cmp.isUnsigned()) {
2242+
APInt Div, Rem;
2243+
APInt::udivrem(C, *C2, Div, Rem);
2244+
if (!Rem.isZero())
2245+
return nullptr;
2246+
bool CIsPowerOf2 = Div.isPowerOf2();
2247+
22422248
// (1 << Y) pred C -> Y pred Log2(C)
22432249
if (!CIsPowerOf2) {
22442250
// (1 << Y) < 30 -> Y <= 4
@@ -2251,9 +2257,9 @@ static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
22512257
Pred = ICmpInst::ICMP_UGT;
22522258
}
22532259

2254-
unsigned CLog2 = C.logBase2();
2260+
unsigned CLog2 = Div.logBase2();
22552261
return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
2256-
} else if (Cmp.isSigned()) {
2262+
} else if (Cmp.isSigned() && C2->isOne()) {
22572263
Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
22582264
// (1 << Y) > 0 -> Y != 31
22592265
// (1 << Y) > C -> Y != 31 if C is negative.
@@ -2307,7 +2313,7 @@ Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,
23072313

23082314
const APInt *ShiftAmt;
23092315
if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
2310-
return foldICmpShlOne(Cmp, Shl, C);
2316+
return foldICmpShlLHSC(Cmp, Shl, C);
23112317

23122318
// Check that the shift amount is in range. If not, don't perform undefined
23132319
// shifts. When the shift is visited, it will be simplified.

llvm/test/Transforms/InstCombine/icmp-shl-nuw.ll

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,8 @@ define <2 x i1> @icmp_ugt_16x2(<2 x i32>) {
9393

9494
define i1 @fold_icmp_shl_nuw_c1(i32 %x) {
9595
; CHECK-LABEL: @fold_icmp_shl_nuw_c1(
96-
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], 12
97-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[LSHR]], 15
98-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i32 2, [[AND]]
99-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[SHL]], 4
96+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 61440
97+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 0
10098
; CHECK-NEXT: ret i1 [[CMP]]
10199
;
102100
%lshr = lshr i32 %x, 12
@@ -108,8 +106,7 @@ define i1 @fold_icmp_shl_nuw_c1(i32 %x) {
108106

109107
define i1 @fold_icmp_shl_nuw_c2(i32 %x) {
110108
; CHECK-LABEL: @fold_icmp_shl_nuw_c2(
111-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 16, [[X:%.*]]
112-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[SHL]], 64
109+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 2
113110
; CHECK-NEXT: ret i1 [[CMP]]
114111
;
115112
%shl = shl nuw i32 16, %x
@@ -119,8 +116,7 @@ define i1 @fold_icmp_shl_nuw_c2(i32 %x) {
119116

120117
define i1 @fold_icmp_shl_nuw_c2_non_pow2(i32 %x) {
121118
; CHECK-LABEL: @fold_icmp_shl_nuw_c2_non_pow2(
122-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 48, [[X:%.*]]
123-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[SHL]], 192
119+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 2
124120
; CHECK-NEXT: ret i1 [[CMP]]
125121
;
126122
%shl = shl nuw i32 48, %x
@@ -130,8 +126,7 @@ define i1 @fold_icmp_shl_nuw_c2_non_pow2(i32 %x) {
130126

131127
define i1 @fold_icmp_shl_nuw_c2_div_non_pow2(i32 %x) {
132128
; CHECK-LABEL: @fold_icmp_shl_nuw_c2_div_non_pow2(
133-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 2, [[X:%.*]]
134-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[SHL]], 60
129+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 5
135130
; CHECK-NEXT: ret i1 [[CMP]]
136131
;
137132
%shl = shl nuw i32 2, %x

0 commit comments

Comments
 (0)