From 5750a482e4a1980200855fcd1df42bc2f8ff591d Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 5 Sep 2020 12:25:41 +0200 Subject: [PATCH] [InstCombine] Fold cttz of abs intrinsic Same as the existing fold for SPF_ABS. We don't need to explicitly handle the NABS variant, as we'll first fold away the neg in that case. --- lib/Transforms/InstCombine/InstCombineCalls.cpp | 3 +++ test/Transforms/InstCombine/cttz-abs.ll | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineCalls.cpp b/lib/Transforms/InstCombine/InstCombineCalls.cpp index dd4e48170f0..311a18c7f58 100644 --- a/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -428,6 +428,9 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) { SelectPatternFlavor SPF = matchSelectPattern(Op0, X, Y).Flavor; if (SPF == SPF_ABS || SPF == SPF_NABS) return IC.replaceOperand(II, 0, X); + + if (match(Op0, m_Intrinsic(m_Value(X)))) + return IC.replaceOperand(II, 0, X); } KnownBits Known = IC.computeKnownBits(Op0, 0, &II); diff --git a/test/Transforms/InstCombine/cttz-abs.ll b/test/Transforms/InstCombine/cttz-abs.ll index 8da3668997f..b89a55c8f5b 100644 --- a/test/Transforms/InstCombine/cttz-abs.ll +++ b/test/Transforms/InstCombine/cttz-abs.ll @@ -218,8 +218,7 @@ define <2 x i64> @no_cttz_nabs_vec(<2 x i64> %x) { define i32 @cttz_abs_intrin(i32 %x) { ; CHECK-LABEL: @cttz_abs_intrin( -; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false) -; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.cttz.i32(i32 [[A]], i1 false), [[RNG0]] +; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false), [[RNG0]] ; CHECK-NEXT: ret i32 [[R]] ; %a = call i32 @llvm.abs.i32(i32 %x, i1 false) @@ -229,8 +228,7 @@ define i32 @cttz_abs_intrin(i32 %x) { define i32 @cttz_nabs_intrin(i32 %x) { ; CHECK-LABEL: @cttz_nabs_intrin( -; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false) -; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.cttz.i32(i32 [[A]], i1 false), [[RNG0]] +; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false), [[RNG0]] ; CHECK-NEXT: ret i32 [[R]] ; %a = call i32 @llvm.abs.i32(i32 %x, i1 false)