[llvm] r357521 - [InstCombine] Simplify ctlz/cttz with bitreverse
David Bolvansky via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 2 13:13:28 PDT 2019
Author: xbolva00
Date: Tue Apr 2 13:13:28 2019
New Revision: 357521
URL: http://llvm.org/viewvc/llvm-project?rev=357521&view=rev
Log:
[InstCombine] Simplify ctlz/cttz with bitreverse
Summary: Fixes PR41273
Reviewers: spatel
Reviewed By: spatel
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60096
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/trunk/test/Transforms/InstCombine/ctlz-cttz-bitreverse.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=357521&r1=357520&r2=357521&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Tue Apr 2 13:13:28 2019
@@ -1324,12 +1324,20 @@ static Instruction *foldCttzCtlz(Intrins
assert((II.getIntrinsicID() == Intrinsic::cttz ||
II.getIntrinsicID() == Intrinsic::ctlz) &&
"Expected cttz or ctlz intrinsic");
+ bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
Value *Op0 = II.getArgOperand(0);
+ Value *X;
+ // ctlz(bitreverse(x)) -> cttz(x)
+ // cttz(bitreverse(x)) -> ctlz(x)
+ if (match(Op0, m_BitReverse(m_Value(X)))) {
+ Intrinsic::ID ID = IsTZ ? Intrinsic::ctlz : Intrinsic::cttz;
+ Function *F = Intrinsic::getDeclaration(II.getModule(), ID, II.getType());
+ return CallInst::Create(F, {X, II.getArgOperand(1)});
+ }
KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
// Create a mask for bits above (ctlz) or below (cttz) the first known one.
- bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
unsigned PossibleZeros = IsTZ ? Known.countMaxTrailingZeros()
: Known.countMaxLeadingZeros();
unsigned DefiniteZeros = IsTZ ? Known.countMinTrailingZeros()
Modified: llvm/trunk/test/Transforms/InstCombine/ctlz-cttz-bitreverse.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/ctlz-cttz-bitreverse.ll?rev=357521&r1=357520&r2=357521&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/ctlz-cttz-bitreverse.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/ctlz-cttz-bitreverse.ll Tue Apr 2 13:13:28 2019
@@ -3,9 +3,8 @@
define i32 @ctlz_true_bitreverse(i32 %x) {
; CHECK-LABEL: @ctlz_true_bitreverse(
-; CHECK-NEXT: [[A:%.*]] = tail call i32 @llvm.bitreverse.i32(i32 [[X:%.*]])
-; CHECK-NEXT: [[B:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[A]], i1 true), !range !0
-; CHECK-NEXT: ret i32 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true), !range !0
+; CHECK-NEXT: ret i32 [[TMP1]]
;
%a = tail call i32 @llvm.bitreverse.i32(i32 %x)
%b = tail call i32 @llvm.ctlz.i32(i32 %a, i1 true)
@@ -14,9 +13,8 @@ define i32 @ctlz_true_bitreverse(i32 %x)
define <2 x i64> @ctlz_true_bitreverse_vec(<2 x i64> %x) {
; CHECK-LABEL: @ctlz_true_bitreverse_vec(
-; CHECK-NEXT: [[A:%.*]] = tail call <2 x i64> @llvm.bitreverse.v2i64(<2 x i64> [[X:%.*]])
-; CHECK-NEXT: [[B:%.*]] = tail call <2 x i64> @llvm.ctlz.v2i64(<2 x i64> [[A]], i1 true)
-; CHECK-NEXT: ret <2 x i64> [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[X:%.*]], i1 true)
+; CHECK-NEXT: ret <2 x i64> [[TMP1]]
;
%a = tail call <2 x i64> @llvm.bitreverse.v2i64(<2 x i64> %x)
%b = tail call <2 x i64> @llvm.ctlz.v2i64(<2 x i64> %a, i1 true)
@@ -25,9 +23,8 @@ define <2 x i64> @ctlz_true_bitreverse_v
define i32 @ctlz_false_bitreverse(i32 %x) {
; CHECK-LABEL: @ctlz_false_bitreverse(
-; CHECK-NEXT: [[A:%.*]] = tail call i32 @llvm.bitreverse.i32(i32 [[X:%.*]])
-; CHECK-NEXT: [[B:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[A]], i1 false), !range !0
-; CHECK-NEXT: ret i32 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false), !range !0
+; CHECK-NEXT: ret i32 [[TMP1]]
;
%a = tail call i32 @llvm.bitreverse.i32(i32 %x)
%b = tail call i32 @llvm.ctlz.i32(i32 %a, i1 false)
@@ -36,9 +33,8 @@ define i32 @ctlz_false_bitreverse(i32 %x
define i32 @cttz_true_bitreverse(i32 %x) {
; CHECK-LABEL: @cttz_true_bitreverse(
-; CHECK-NEXT: [[A:%.*]] = tail call i32 @llvm.bitreverse.i32(i32 [[X:%.*]])
-; CHECK-NEXT: [[B:%.*]] = tail call i32 @llvm.cttz.i32(i32 [[A]], i1 true), !range !0
-; CHECK-NEXT: ret i32 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 true), !range !0
+; CHECK-NEXT: ret i32 [[TMP1]]
;
%a = tail call i32 @llvm.bitreverse.i32(i32 %x)
%b = tail call i32 @llvm.cttz.i32(i32 %a, i1 true)
@@ -47,9 +43,8 @@ define i32 @cttz_true_bitreverse(i32 %x)
define <2 x i64> @cttz_true_bitreverse_vec(<2 x i64> %x) {
; CHECK-LABEL: @cttz_true_bitreverse_vec(
-; CHECK-NEXT: [[A:%.*]] = tail call <2 x i64> @llvm.bitreverse.v2i64(<2 x i64> [[X:%.*]])
-; CHECK-NEXT: [[B:%.*]] = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[A]], i1 true)
-; CHECK-NEXT: ret <2 x i64> [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i64> @llvm.ctlz.v2i64(<2 x i64> [[X:%.*]], i1 true)
+; CHECK-NEXT: ret <2 x i64> [[TMP1]]
;
%a = tail call <2 x i64> @llvm.bitreverse.v2i64(<2 x i64> %x)
%b = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %a, i1 true)
@@ -58,9 +53,8 @@ define <2 x i64> @cttz_true_bitreverse_v
define i32 @cttz_false_bitreverse(i32 %x) {
; CHECK-LABEL: @cttz_false_bitreverse(
-; CHECK-NEXT: [[A:%.*]] = tail call i32 @llvm.bitreverse.i32(i32 [[X:%.*]])
-; CHECK-NEXT: [[B:%.*]] = tail call i32 @llvm.cttz.i32(i32 [[A]], i1 false), !range !0
-; CHECK-NEXT: ret i32 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range !0
+; CHECK-NEXT: ret i32 [[TMP1]]
;
%a = tail call i32 @llvm.bitreverse.i32(i32 %x)
%b = tail call i32 @llvm.cttz.i32(i32 %a, i1 false)
More information about the llvm-commits
mailing list