Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2765,6 +2765,7 @@ // Handle icmp {eq|ne} , Constant. Type *Ty = II->getType(); + unsigned BitWidth = C.getBitWidth(); switch (II->getIntrinsicID()) { case Intrinsic::bswap: Worklist.Add(II); @@ -2773,21 +2774,38 @@ return &Cmp; case Intrinsic::ctlz: - case Intrinsic::cttz: + case Intrinsic::cttz: { // ctz(A) == bitwidth(A) -> A == 0 and likewise for != - if (C == C.getBitWidth()) { + if (C == BitWidth) { Worklist.Add(II); Cmp.setOperand(0, II->getArgOperand(0)); Cmp.setOperand(1, ConstantInt::getNullValue(Ty)); return &Cmp; } + + // ctz(A) == C -> A & Mask1 == Mask2, where Mask2 only has bit C set + // and Mask1 has bits 0..C+1 set. Similar for ctl, but for high bits. + unsigned Num = C.getLimitedValue(BitWidth); + if (Num != BitWidth) { + bool IsTrailing = II->getIntrinsicID() == Intrinsic::cttz; + APInt Mask1 = IsTrailing ? APInt::getLowBitsSet(BitWidth, Num + 1) + : APInt::getHighBitsSet(BitWidth, Num + 1); + APInt Mask2 = IsTrailing + ? APInt::getOneBitSet(BitWidth, Num) + : APInt::getOneBitSet(BitWidth, BitWidth - Num - 1); + Cmp.setOperand(0, Builder.CreateAnd(II->getArgOperand(0), Mask1)); + Cmp.setOperand(1, ConstantInt::get(Ty, Mask2)); + Worklist.Add(II); + return &Cmp; + } break; + } case Intrinsic::ctpop: { // popcount(A) == 0 -> A == 0 and likewise for != // popcount(A) == bitwidth(A) -> A == -1 and likewise for != bool IsZero = C.isNullValue(); - if (IsZero || C == C.getBitWidth()) { + if (IsZero || C == BitWidth) { Worklist.Add(II); Cmp.setOperand(0, II->getArgOperand(0)); auto *NewOp = Index: test/Transforms/InstCombine/cmp-intrinsic.ll =================================================================== --- test/Transforms/InstCombine/cmp-intrinsic.ll +++ test/Transforms/InstCombine/cmp-intrinsic.ll @@ -54,8 +54,7 @@ define i1 @ctlz_eq_zero_i32(i32 %x) { ; CHECK-LABEL: @ctlz_eq_zero_i32( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range !0 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[LZ]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) @@ -65,8 +64,7 @@ define <2 x i1> @ctlz_ne_zero_v2i32(<2 x i32> %a) { ; CHECK-LABEL: @ctlz_ne_zero_v2i32( -; CHECK-NEXT: [[X:%.*]] = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> [[A:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[X]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) @@ -76,8 +74,7 @@ define i1 @ctlz_eq_bw_minus_1_i32(i32 %x) { ; CHECK-LABEL: @ctlz_eq_bw_minus_1_i32( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range !0 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[LZ]], 31 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 1 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) @@ -87,8 +84,7 @@ define <2 x i1> @ctlz_ne_bw_minus_1_v2i32(<2 x i32> %a) { ; CHECK-LABEL: @ctlz_ne_bw_minus_1_v2i32( -; CHECK-NEXT: [[X:%.*]] = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> [[A:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[X]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) @@ -98,8 +94,8 @@ define i1 @ctlz_eq_other_i32(i32 %x) { ; CHECK-LABEL: @ctlz_eq_other_i32( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range !0 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[LZ]], 24 +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], -128 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 128 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) @@ -109,8 +105,8 @@ define <2 x i1> @ctlz_ne_other_v2i32(<2 x i32> %a) { ; CHECK-LABEL: @ctlz_ne_other_v2i32( -; CHECK-NEXT: [[X:%.*]] = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> [[A:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[X]], +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[TMP1]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) @@ -150,8 +146,8 @@ define i1 @cttz_eq_zero_i33(i33 %x) { ; CHECK-LABEL: @cttz_eq_zero_i33( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range !1 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[TZ]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 1 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i33 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 false) @@ -161,8 +157,8 @@ define <2 x i1> @cttz_ne_zero_v2i32(<2 x i32> %a) { ; CHECK-LABEL: @cttz_ne_zero_v2i32( -; CHECK-NEXT: [[X:%.*]] = tail call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[A:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[X]], zeroinitializer +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = tail call <2 x i32> @llvm.cttz.v2i32(<2 x i32> %a, i1 false) @@ -172,8 +168,7 @@ define i1 @cttz_eq_bw_minus_1_i33(i33 %x) { ; CHECK-LABEL: @cttz_eq_bw_minus_1_i33( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range !1 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[TZ]], 32 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[X:%.*]], -4294967296 ; CHECK-NEXT: ret i1 [[CMP]] ; %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 false) @@ -183,8 +178,7 @@ define <2 x i1> @cttz_ne_bw_minus_1_v2i32(<2 x i32> %a) { ; CHECK-LABEL: @cttz_ne_bw_minus_1_v2i32( -; CHECK-NEXT: [[X:%.*]] = tail call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[A:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[X]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = tail call <2 x i32> @llvm.cttz.v2i32(<2 x i32> %a, i1 false) @@ -194,8 +188,8 @@ define i1 @cttz_eq_other_i33(i33 %x) { ; CHECK-LABEL: @cttz_eq_other_i33( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range !1 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[TZ]], 4 +; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 31 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[TMP1]], 16 ; CHECK-NEXT: ret i1 [[CMP]] ; %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 false) @@ -205,8 +199,8 @@ define <2 x i1> @cttz_ne_other_v2i32(<2 x i32> %a) { ; CHECK-LABEL: @cttz_ne_other_v2i32( -; CHECK-NEXT: [[X:%.*]] = tail call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[A:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[X]], +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[TMP1]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = tail call <2 x i32> @llvm.cttz.v2i32(<2 x i32> %a, i1 false) Index: test/Transforms/InstCombine/intrinsics.ll =================================================================== --- test/Transforms/InstCombine/intrinsics.ll +++ test/Transforms/InstCombine/intrinsics.ll @@ -354,13 +354,9 @@ ret i1 %res } -; TODO: The icmp is unnecessary given the known bits of the input. define <2 x i1> @cttz_knownbits3_vec(<2 x i32> %arg) { ; CHECK-LABEL: @cttz_knownbits3_vec( -; CHECK-NEXT: [[OR:%.*]] = or <2 x i32> [[ARG:%.*]], -; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[OR]], i1 true) -; CHECK-NEXT: [[RES:%.*]] = icmp eq <2 x i32> [[CNT]], -; CHECK-NEXT: ret <2 x i1> [[RES]] +; CHECK-NEXT: ret <2 x i1> zeroinitializer ; %or = or <2 x i32> %arg, %cnt = call <2 x i32> @llvm.cttz.v2i32(<2 x i32> %or, i1 true) nounwind readnone @@ -450,13 +446,9 @@ ret i1 %res } -; TODO: The icmp is unnecessary given the known bits of the input. define <2 x i1> @ctlz_knownbits3_vec(<2 x i8> %arg) { ; CHECK-LABEL: @ctlz_knownbits3_vec( -; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[ARG:%.*]], -; CHECK-NEXT: [[CNT:%.*]] = call <2 x i8> @llvm.ctlz.v2i8(<2 x i8> [[OR]], i1 true) -; CHECK-NEXT: [[RES:%.*]] = icmp eq <2 x i8> [[CNT]], -; CHECK-NEXT: ret <2 x i1> [[RES]] +; CHECK-NEXT: ret <2 x i1> zeroinitializer ; %or = or <2 x i8> %arg, %cnt = call <2 x i8> @llvm.ctlz.v2i8(<2 x i8> %or, i1 true) nounwind readnone