From c72198079df60e73948fe4744b1c0ef7231803fd Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 23 Oct 2020 08:32:58 -0400 Subject: [PATCH] [ValueTracking] add range limits for cttz As discussed in D89952, instcombine can sometimes find a way to reduce similar patterns, but it is incomplete. InstSimplify uses the computeConstantRange() ValueTracking analysis via simplifyICmpWithConstant(), so we just need to fill in the max value of cttz to process any "icmp pred cttz(X), C" pattern (the min value is initialized to zero automatically). https://alive2.llvm.org/ce/z/Z_SLWZ Follow-up to D89976. --- llvm/lib/Analysis/ValueTracking.cpp | 1 + .../test/Transforms/InstCombine/ctpop-cttz.ll | 2 +- llvm/test/Transforms/InstSimplify/compare.ll | 28 +++++-------------- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index e277b02720ea..23642084cecd 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -6462,6 +6462,7 @@ static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower, switch (II.getIntrinsicID()) { case Intrinsic::ctpop: case Intrinsic::ctlz: + case Intrinsic::cttz: // Maximum of set/clear bits is the bit width. assert(Lower == 0 && "Expected lower bound to be zero"); Upper = Width + 1; diff --git a/llvm/test/Transforms/InstCombine/ctpop-cttz.ll b/llvm/test/Transforms/InstCombine/ctpop-cttz.ll index 6ac3cfe36bbc..59a45f354a37 100644 --- a/llvm/test/Transforms/InstCombine/ctpop-cttz.ll +++ b/llvm/test/Transforms/InstCombine/ctpop-cttz.ll @@ -21,7 +21,7 @@ define i32 @ctpop1(i32 %0) { define <2 x i32> @ctpop1v(<2 x i32> %0) { ; CHECK-LABEL: @ctpop1v( ; CHECK-NEXT: [[TMP2:%.*]] = call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[TMP0:%.*]], i1 false) -; CHECK-NEXT: [[TMP3:%.*]] = sub nsw <2 x i32> , [[TMP2]] +; CHECK-NEXT: [[TMP3:%.*]] = sub nuw nsw <2 x i32> , [[TMP2]] ; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %2 = sub <2 x i32> zeroinitializer, %0 diff --git a/llvm/test/Transforms/InstSimplify/compare.ll b/llvm/test/Transforms/InstSimplify/compare.ll index d024d7096272..5afa05b8a06e 100644 --- a/llvm/test/Transforms/InstSimplify/compare.ll +++ b/llvm/test/Transforms/InstSimplify/compare.ll @@ -2259,9 +2259,7 @@ declare <2 x i13> @llvm.cttz.v2i13(<2 x i13>) define i1 @cttz_sgt_bitwidth(i11 %x) { ; CHECK-LABEL: @cttz_sgt_bitwidth( -; CHECK-NEXT: [[POP:%.*]] = call i11 @llvm.cttz.i11(i11 [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i11 [[POP]], 11 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %pop = call i11 @llvm.cttz.i11(i11 %x) %cmp = icmp sgt i11 %pop, 11 @@ -2270,9 +2268,7 @@ define i1 @cttz_sgt_bitwidth(i11 %x) { define i1 @cttz_sle_minus1(i11 %x) { ; CHECK-LABEL: @cttz_sle_minus1( -; CHECK-NEXT: [[POP:%.*]] = call i11 @llvm.cttz.i11(i11 [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp sle i11 [[POP]], -1 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %pop = call i11 @llvm.cttz.i11(i11 %x) %cmp = icmp sle i11 %pop, -1 @@ -2281,9 +2277,7 @@ define i1 @cttz_sle_minus1(i11 %x) { define i1 @cttz_ugt_bitwidth(i73 %x) { ; CHECK-LABEL: @cttz_ugt_bitwidth( -; CHECK-NEXT: [[POP:%.*]] = call i73 @llvm.cttz.i73(i73 [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i73 [[POP]], 73 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %pop = call i73 @llvm.cttz.i73(i73 %x) %cmp = icmp ugt i73 %pop, 73 @@ -2305,9 +2299,7 @@ define i1 @cttz_ugt_bitwidth_minus1(i73 %x) { define <2 x i1> @cttz_sgt_bitwidth_splat(<2 x i13> %x) { ; CHECK-LABEL: @cttz_sgt_bitwidth_splat( -; CHECK-NEXT: [[POP:%.*]] = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i13> [[POP]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> zeroinitializer ; %pop = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> %x) %cmp = icmp sgt <2 x i13> %pop, @@ -2316,9 +2308,7 @@ define <2 x i1> @cttz_sgt_bitwidth_splat(<2 x i13> %x) { define i1 @cttz_ult_plus1_bitwidth(i11 %x) { ; CHECK-LABEL: @cttz_ult_plus1_bitwidth( -; CHECK-NEXT: [[POP:%.*]] = call i11 @llvm.cttz.i11(i11 [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i11 [[POP]], 12 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %pop = call i11 @llvm.cttz.i11(i11 %x) %cmp = icmp ult i11 %pop, 12 @@ -2327,9 +2317,7 @@ define i1 @cttz_ult_plus1_bitwidth(i11 %x) { define i1 @cttz_ne_big_bitwidth(i73 %x) { ; CHECK-LABEL: @cttz_ne_big_bitwidth( -; CHECK-NEXT: [[POP:%.*]] = call i73 @llvm.cttz.i73(i73 [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i73 [[POP]], 75 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %pop = call i73 @llvm.cttz.i73(i73 %x) %cmp = icmp ne i73 %pop, 75 @@ -2338,9 +2326,7 @@ define i1 @cttz_ne_big_bitwidth(i73 %x) { define <2 x i1> @cttz_slt_bitwidth_plus1_splat(<2 x i13> %x) { ; CHECK-LABEL: @cttz_slt_bitwidth_plus1_splat( -; CHECK-NEXT: [[POP:%.*]] = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> [[X:%.*]], i1 false) -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i13> [[POP]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> ; %pop = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> %x) %cmp = icmp slt <2 x i13> %pop,