diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1779,7 +1779,7 @@ // Restrict this fold only for single-use 'and' (PR10267). // ((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two. - if ((~(*C2) + 1).isPowerOf2()) { + if (C2->isNegatedPowerOf2()) { Constant *NegBOC = ConstantExpr::getNeg(cast(And->getOperand(1))); auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; diff --git a/llvm/test/Transforms/InstCombine/lshr-and-negC-icmpeq-zero.ll b/llvm/test/Transforms/InstCombine/lshr-and-negC-icmpeq-zero.ll --- a/llvm/test/Transforms/InstCombine/lshr-and-negC-icmpeq-zero.ll +++ b/llvm/test/Transforms/InstCombine/lshr-and-negC-icmpeq-zero.ll @@ -234,3 +234,18 @@ %r = icmp eq i32 %and, 1 ; should be comparing with 0 ret i1 %r } + +; Not NegatedPowerOf2 + +define i1 @scalar_i8_lshr_and_negC_eq_not_negatedPowerOf2(i8 %x, i8 %y) { +; CHECK-LABEL: @scalar_i8_lshr_and_negC_eq_not_negatedPowerOf2( +; CHECK-NEXT: [[TMP1:%.*]] = shl i8 -3, [[Y:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP2]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %lshr = lshr i8 %x, %y + %and = and i8 %lshr, 253 ; -3 + %r = icmp eq i8 %and, 0 + ret i1 %r +}