Skip to content

Commit 27b651c

Browse files
[InstCombine] cttz(zext(x)) -> zext(cttz(x)) if the 'ZeroIsUndef' parameter is 'true' (PR50172)
Zext doesn't change the number of trailing zeros, so narrow cttz(zext(x)) -> zext(cttz(x)) if the 'ZeroIsUndef' parameter is 'true'. Proofs: https://alive2.llvm.org/ce/z/o2dnjY Solves https://bugs.llvm.org/show_bug.cgi?id=50172 Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D101582
1 parent aad3113 commit 27b651c

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {
424424
"Expected cttz or ctlz intrinsic");
425425
bool IsTZ = II.getIntrinsicID() == Intrinsic::cttz;
426426
Value *Op0 = II.getArgOperand(0);
427+
Value *Op1 = II.getArgOperand(1);
427428
Value *X;
428429
// ctlz(bitreverse(x)) -> cttz(x)
429430
// cttz(bitreverse(x)) -> ctlz(x)
@@ -438,6 +439,15 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {
438439
if (match(Op0, m_Neg(m_Value(X))))
439440
return IC.replaceOperand(II, 0, X);
440441

442+
// Zext doesn't change the number of trailing zeros, so narrow:
443+
// cttz(zext(x)) -> zext(cttz(x)) if the 'ZeroIsUndef' parameter is 'true'.
444+
if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) && match(Op1, m_One())) {
445+
auto *Cttz = IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, X,
446+
IC.Builder.getTrue());
447+
auto *ZextCttz = IC.Builder.CreateZExt(Cttz, II.getType());
448+
return IC.replaceInstUsesWith(II, ZextCttz);
449+
}
450+
441451
// cttz(abs(x)) -> cttz(x)
442452
// cttz(nabs(x)) -> cttz(x)
443453
Value *Y;

llvm/test/Transforms/InstCombine/cttz.ll

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ declare void @use(i32)
77

88
define i32 @cttz_zext_zero_undef(i16 %x) {
99
; CHECK-LABEL: @cttz_zext_zero_undef(
10-
; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
11-
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[Z]], i1 true), !range [[RNG0:![0-9]+]]
12-
; CHECK-NEXT: ret i32 [[TZ]]
10+
; CHECK-NEXT: [[TMP1:%.*]] = call i16 @llvm.cttz.i16(i16 [[X:%.*]], i1 true), !range [[RNG0:![0-9]+]]
11+
; CHECK-NEXT: [[TMP2:%.*]] = zext i16 [[TMP1]] to i32
12+
; CHECK-NEXT: ret i32 [[TMP2]]
1313
;
1414
%z = zext i16 %x to i32
1515
%tz = call i32 @llvm.cttz.i32(i32 %z, i1 true)
@@ -19,7 +19,7 @@ define i32 @cttz_zext_zero_undef(i16 %x) {
1919
define i32 @cttz_zext_zero_def(i16 %x) {
2020
; CHECK-LABEL: @cttz_zext_zero_def(
2121
; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
22-
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[Z]], i1 false), !range [[RNG0]]
22+
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[Z]], i1 false), !range [[RNG1:![0-9]+]]
2323
; CHECK-NEXT: ret i32 [[TZ]]
2424
;
2525
%z = zext i16 %x to i32
@@ -31,7 +31,7 @@ define i32 @cttz_zext_zero_undef_extra_use(i16 %x) {
3131
; CHECK-LABEL: @cttz_zext_zero_undef_extra_use(
3232
; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
3333
; CHECK-NEXT: call void @use(i32 [[Z]])
34-
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[Z]], i1 true), !range [[RNG0]]
34+
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[Z]], i1 true), !range [[RNG1]]
3535
; CHECK-NEXT: ret i32 [[TZ]]
3636
;
3737
%z = zext i16 %x to i32
@@ -42,9 +42,9 @@ define i32 @cttz_zext_zero_undef_extra_use(i16 %x) {
4242

4343
define <2 x i64> @cttz_zext_zero_undef_vec(<2 x i32> %x) {
4444
; CHECK-LABEL: @cttz_zext_zero_undef_vec(
45-
; CHECK-NEXT: [[Z:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64>
46-
; CHECK-NEXT: [[TZ:%.*]] = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[Z]], i1 true)
47-
; CHECK-NEXT: ret <2 x i64> [[TZ]]
45+
; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[X:%.*]], i1 true)
46+
; CHECK-NEXT: [[TMP2:%.*]] = zext <2 x i32> [[TMP1]] to <2 x i64>
47+
; CHECK-NEXT: ret <2 x i64> [[TMP2]]
4848
;
4949
%z = zext <2 x i32> %x to <2 x i64>
5050
%tz = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %z, i1 true)
@@ -65,7 +65,7 @@ define <2 x i64> @cttz_zext_zero_def_vec(<2 x i32> %x) {
6565
define i32 @cttz_sext_zero_undef(i16 %x) {
6666
; CHECK-LABEL: @cttz_sext_zero_undef(
6767
; CHECK-NEXT: [[S:%.*]] = sext i16 [[X:%.*]] to i32
68-
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[S]], i1 true), !range [[RNG0]]
68+
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[S]], i1 true), !range [[RNG1]]
6969
; CHECK-NEXT: ret i32 [[TZ]]
7070
;
7171
%s = sext i16 %x to i32
@@ -76,19 +76,19 @@ define i32 @cttz_sext_zero_undef(i16 %x) {
7676
define i32 @cttz_sext_zero_def(i16 %x) {
7777
; CHECK-LABEL: @cttz_sext_zero_def(
7878
; CHECK-NEXT: [[S:%.*]] = zext i16 [[X:%.*]] to i32
79-
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[S]], i1 false), !range [[RNG0]]
79+
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[S]], i1 false), !range [[RNG1]]
8080
; CHECK-NEXT: ret i32 [[TZ]]
8181
;
8282
%s = zext i16 %x to i32
8383
%tz = call i32 @llvm.cttz.i32(i32 %s, i1 false)
8484
ret i32 %tz
8585
}
8686

87-
define i32 @cttz_zext_sero_undef_extra_use(i16 %x) {
88-
; CHECK-LABEL: @cttz_zext_sero_undef_extra_use(
87+
define i32 @cttz_sext_zero_undef_extra_use(i16 %x) {
88+
; CHECK-LABEL: @cttz_sext_zero_undef_extra_use(
8989
; CHECK-NEXT: [[S:%.*]] = sext i16 [[X:%.*]] to i32
9090
; CHECK-NEXT: call void @use(i32 [[S]])
91-
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[S]], i1 true), !range [[RNG0]]
91+
; CHECK-NEXT: [[TZ:%.*]] = call i32 @llvm.cttz.i32(i32 [[S]], i1 true), !range [[RNG1]]
9292
; CHECK-NEXT: ret i32 [[TZ]]
9393
;
9494
%s = sext i16 %x to i32

0 commit comments

Comments
 (0)