Skip to content

Commit ea86fb8

Browse files
authored
[InstCombine] Fold zext-of-icmp with no shift (#68503)
This regression triggers after commit f400daa to fix infinite loop issue. In this case, we can known the shift count is 0, so it will not be triggered by the form of (iN (~X) u>> (N - 1)) in commit 21d3871, of which N indicates the data type bitwidth of X. Fixes #68465.
1 parent e44cfe0 commit ea86fb8

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -904,19 +904,18 @@ Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp,
904904
// zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
905905
// zext (X != 0) to i32 --> X iff X has only the low bit set.
906906
// zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
907-
if (Op1CV->isZero() && Cmp->isEquality() &&
908-
(Cmp->getOperand(0)->getType() == Zext.getType() ||
909-
Cmp->getPredicate() == ICmpInst::ICMP_NE)) {
910-
// If Op1C some other power of two, convert:
911-
KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext);
912907

908+
if (Op1CV->isZero() && Cmp->isEquality()) {
913909
// Exactly 1 possible 1? But not the high-bit because that is
914910
// canonicalized to this form.
911+
KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext);
915912
APInt KnownZeroMask(~Known.Zero);
916-
if (KnownZeroMask.isPowerOf2() &&
917-
(Zext.getType()->getScalarSizeInBits() !=
918-
KnownZeroMask.logBase2() + 1)) {
919-
uint32_t ShAmt = KnownZeroMask.logBase2();
913+
uint32_t ShAmt = KnownZeroMask.logBase2();
914+
bool IsExpectShAmt = KnownZeroMask.isPowerOf2() &&
915+
(Zext.getType()->getScalarSizeInBits() != ShAmt + 1);
916+
if (IsExpectShAmt &&
917+
(Cmp->getOperand(0)->getType() == Zext.getType() ||
918+
Cmp->getPredicate() == ICmpInst::ICMP_NE || ShAmt == 0)) {
920919
Value *In = Cmp->getOperand(0);
921920
if (ShAmt) {
922921
// Perform a logical shr by shiftamt.

llvm/test/Transforms/InstCombine/zext.ll

+2-1
Original file line numberDiff line numberDiff line change
@@ -749,10 +749,11 @@ define i64 @zext_icmp_ne_bool_1(ptr %ptr) {
749749
ret i64 %len
750750
}
751751

752+
; https://alive2.llvm.org/ce/z/k7qosS
752753
define i32 @zext_icmp_eq0_no_shift(ptr %ptr ) {
753754
; CHECK-LABEL: @zext_icmp_eq0_no_shift(
754755
; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[PTR:%.*]], align 1, !range [[RNG1:![0-9]+]]
755-
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X]], 0
756+
; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], 1
756757
; CHECK-NEXT: [[RES:%.*]] = zext i8 [[TMP1]] to i32
757758
; CHECK-NEXT: ret i32 [[RES]]
758759
;

0 commit comments

Comments
 (0)