Skip to content

Commit e2d8443

Browse files
committed
[InstCombine] Fix regressions from canonicalizing (icmp eq/ne (X, Y), Y -> (icmp eq/ne (~X, Y), 0)
This issue is the canonicalization can consume `not` instruction which are a limitted resource and are use to enable multiple transforms. In this case `foldICmpWithLowBitMaskedVal` is a "better" user of `not` instructions, so just check if that has a result first.
1 parent c63dbd2 commit e2d8443

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4719,6 +4719,11 @@ static Instruction *foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q,
47194719
return new ICmpInst(Pred, IC.Builder.CreateOr(A, NotOp1),
47204720
Constant::getAllOnesValue(Op1->getType()));
47214721
// icmp (X & Y) eq/ne Y --> (~X & Y) eq/ne 0 if X is freely invertible.
4722+
// Since we may be consuming a `not` here, first check if we match
4723+
// `foldICmpWithLowBitMaskedVal` as it is a "better" user of `not`
4724+
// instructions.
4725+
if (Value *R = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, IC))
4726+
return IC.replaceInstUsesWith(I, R);
47224727
if (auto *NotA = IC.getFreelyInverted(A, A->hasOneUse(), &IC.Builder))
47234728
return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, NotA),
47244729
Constant::getNullValue(Op1->getType()));

llvm/test/Transforms/InstCombine/canonicalize-low-bit-mask-v2-and-icmp-eq-to-icmp-ule.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ define i1 @oneuse0(i8 %x, i8 %y) {
144144
; CHECK-LABEL: @oneuse0(
145145
; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
146146
; CHECK-NEXT: call void @use8(i8 [[T0]])
147-
; CHECK-NEXT: [[X_HIGHBITS:%.*]] = and i8 [[T0]], [[X:%.*]]
147+
; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr i8 [[X:%.*]], [[Y]]
148148
; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[X_HIGHBITS]], 0
149149
; CHECK-NEXT: ret i1 [[RET]]
150150
;
@@ -161,8 +161,7 @@ define i1 @oneuse1(i8 %x, i8 %y) {
161161
; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
162162
; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1
163163
; CHECK-NEXT: call void @use8(i8 [[T1]])
164-
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[T0]], [[X:%.*]]
165-
; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0
164+
; CHECK-NEXT: [[RET:%.*]] = icmp uge i8 [[T1]], [[X:%.*]]
166165
; CHECK-NEXT: ret i1 [[RET]]
167166
;
168167
%t0 = shl i8 -1, %y
@@ -196,8 +195,7 @@ define i1 @oneuse3(i8 %x, i8 %y) {
196195
; CHECK-NEXT: call void @use8(i8 [[T0]])
197196
; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1
198197
; CHECK-NEXT: call void @use8(i8 [[T1]])
199-
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[T0]], [[X:%.*]]
200-
; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0
198+
; CHECK-NEXT: [[RET:%.*]] = icmp uge i8 [[T1]], [[X:%.*]]
201199
; CHECK-NEXT: ret i1 [[RET]]
202200
;
203201
%t0 = shl i8 -1, %y

llvm/test/Transforms/InstCombine/canonicalize-low-bit-mask-v2-and-icmp-ne-to-icmp-ugt.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ define i1 @oneuse0(i8 %x, i8 %y) {
144144
; CHECK-LABEL: @oneuse0(
145145
; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
146146
; CHECK-NEXT: call void @use8(i8 [[T0]])
147-
; CHECK-NEXT: [[X_HIGHBITS:%.*]] = and i8 [[T0]], [[X:%.*]]
147+
; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr i8 [[X:%.*]], [[Y]]
148148
; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X_HIGHBITS]], 0
149149
; CHECK-NEXT: ret i1 [[RET]]
150150
;
@@ -161,8 +161,7 @@ define i1 @oneuse1(i8 %x, i8 %y) {
161161
; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
162162
; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1
163163
; CHECK-NEXT: call void @use8(i8 [[T1]])
164-
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[T0]], [[X:%.*]]
165-
; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[TMP1]], 0
164+
; CHECK-NEXT: [[RET:%.*]] = icmp ult i8 [[T1]], [[X:%.*]]
166165
; CHECK-NEXT: ret i1 [[RET]]
167166
;
168167
%t0 = shl i8 -1, %y
@@ -196,8 +195,7 @@ define i1 @oneuse3(i8 %x, i8 %y) {
196195
; CHECK-NEXT: call void @use8(i8 [[T0]])
197196
; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1
198197
; CHECK-NEXT: call void @use8(i8 [[T1]])
199-
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[T0]], [[X:%.*]]
200-
; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[TMP1]], 0
198+
; CHECK-NEXT: [[RET:%.*]] = icmp ult i8 [[T1]], [[X:%.*]]
201199
; CHECK-NEXT: ret i1 [[RET]]
202200
;
203201
%t0 = shl i8 -1, %y

0 commit comments

Comments
 (0)