Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1715,12 +1715,19 @@ } // (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a - // result greater than C1. - unsigned NumTZ = C2->countTrailingZeros(); - if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && NumTZ < C2->getBitWidth() && - NumTZ >= C1->getActiveBits()) { - Constant *Zero = Constant::getNullValue(And->getType()); - return new ICmpInst(ICmpInst::ICMP_NE, And, Zero); + // result greater than C1. Also handle (X & C2) < C1 --> (X & C2) == 0. + if (!C2->isNullValue()) { + unsigned NumTZ = C2->countTrailingZeros(); + if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && + NumTZ >= C1->getActiveBits()) { + Constant *Zero = Constant::getNullValue(And->getType()); + return new ICmpInst(ICmpInst::ICMP_NE, And, Zero); + } + if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && + NumTZ >= C1->ceilLogBase2()) { + Constant *Zero = Constant::getNullValue(And->getType()); + return new ICmpInst(ICmpInst::ICMP_EQ, And, Zero); + } } return nullptr; Index: llvm/trunk/test/Transforms/InstCombine/icmp.ll =================================================================== --- llvm/trunk/test/Transforms/InstCombine/icmp.ll +++ llvm/trunk/test/Transforms/InstCombine/icmp.ll @@ -1141,7 +1141,7 @@ define i1 @test67inverse(i32 %x) { ; CHECK-LABEL: @test67inverse( ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 96 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[AND]], 32 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %and = and i32 %x, 127 @@ -1178,7 +1178,7 @@ define <2 x i1> @test67vecinverse(<2 x i32> %x) { ; CHECK-LABEL: @test67vecinverse( ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> [[AND]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[AND]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %and = and <2 x i32> %x,