Skip to content

Commit f2c5700

Browse files
committed
[InstCombine] Generalize to handle non-rem-zero cases
1 parent 52bf8d6 commit f2c5700

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,11 +2239,11 @@ static Instruction *foldICmpShlLHSC(ICmpInst &Cmp, Instruction *Shl,
22392239
unsigned TypeBits = C.getBitWidth();
22402240
ICmpInst::Predicate Pred = Cmp.getPredicate();
22412241
if (Cmp.isUnsigned()) {
2242+
assert(!C2->isZero() && C2->ult(C) &&
2243+
"Should be simplified by InstSimplify");
22422244
APInt Div, Rem;
22432245
APInt::udivrem(C, *C2, Div, Rem);
2244-
if (!Rem.isZero())
2245-
return nullptr;
2246-
bool CIsPowerOf2 = Div.isPowerOf2();
2246+
bool CIsPowerOf2 = Rem.isZero() && Div.isPowerOf2();
22472247

22482248
// (1 << Y) pred C -> Y pred Log2(C)
22492249
if (!CIsPowerOf2) {

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ define i1 @fold_icmp_shl_nuw_c2_div_non_pow2(i32 %x) {
138138

139139
define i1 @fold_icmp_shl_nuw_c2_indivisible(i32 %x) {
140140
; CHECK-LABEL: @fold_icmp_shl_nuw_c2_indivisible(
141-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 16, [[X:%.*]]
142-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[SHL]], 63
141+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 2
143142
; CHECK-NEXT: ret i1 [[CMP]]
144143
;
145144
%shl = shl nuw i32 16, %x
@@ -157,3 +156,23 @@ define i1 @fold_icmp_shl_c2_without_nuw(i32 %x) {
157156
%cmp = icmp ult i32 %shl, 64
158157
ret i1 %cmp
159158
}
159+
160+
; Make sure this trivial case is folded by InstSimplify.
161+
define i1 @fold_icmp_shl_nuw_c2_precondition1(i32 %x) {
162+
; CHECK-LABEL: @fold_icmp_shl_nuw_c2_precondition1(
163+
; CHECK-NEXT: ret i1 true
164+
;
165+
%shl = shl nuw i32 0, %x
166+
%cmp = icmp ult i32 %shl, 63
167+
ret i1 %cmp
168+
}
169+
170+
; Make sure this trivial case is folded by InstSimplify.
171+
define i1 @fold_icmp_shl_nuw_c2_precondition2(i32 %x) {
172+
; CHECK-LABEL: @fold_icmp_shl_nuw_c2_precondition2(
173+
; CHECK-NEXT: ret i1 false
174+
;
175+
%shl = shl nuw i32 127, %x
176+
%cmp = icmp ult i32 %shl, 63
177+
ret i1 %cmp
178+
}

0 commit comments

Comments
 (0)