diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp --- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp @@ -470,6 +470,23 @@ return IC.replaceInstUsesWith(II, Extract); } +static Optional instCombineRDFFR(InstCombiner &IC, + IntrinsicInst &II) { + LLVMContext &Ctx = II.getContext(); + IRBuilder<> Builder(Ctx); + Builder.SetInsertPoint(&II); + // Replace rdffr with predicated rdffr.z intrinsic, so that optimizePTestInstr + // can work with RDFFR_PP for ptest elimination. + auto *AllPat = + ConstantInt::get(Type::getInt32Ty(Ctx), AArch64SVEPredPattern::all); + auto *PTrue = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_ptrue, + {II.getType()}, {AllPat}); + auto *RDFFR = + Builder.CreateIntrinsic(Intrinsic::aarch64_sve_rdffr_z, {}, {PTrue}); + RDFFR->takeName(&II); + return IC.replaceInstUsesWith(II, RDFFR); +} + Optional AArch64TTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const { @@ -481,6 +498,8 @@ return instCombineConvertFromSVBool(IC, II); case Intrinsic::aarch64_sve_dup: return instCombineSVEDup(IC, II); + case Intrinsic::aarch64_sve_rdffr: + return instCombineRDFFR(IC, II); case Intrinsic::aarch64_sve_lasta: case Intrinsic::aarch64_sve_lastb: return instCombineSVELast(IC, II); diff --git a/llvm/test/CodeGen/AArch64/sve-intrinsics-ffr-manipulation.ll b/llvm/test/CodeGen/AArch64/sve-intrinsics-ffr-manipulation.ll --- a/llvm/test/CodeGen/AArch64/sve-intrinsics-ffr-manipulation.ll +++ b/llvm/test/CodeGen/AArch64/sve-intrinsics-ffr-manipulation.ll @@ -1,33 +1,51 @@ -; RUN: llc -mtriple=aarch64-linux-gnu -mattr=+sve < %s | FileCheck %s +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc < %s | FileCheck %s + +target triple = "aarch64-unknown-linux-gnu" ; ; RDFFR ; -define @rdffr() { +define @rdffr() #0 { ; CHECK-LABEL: rdffr: -; CHECK: rdffr p0.b -; CHECK-NEXT: ret +; CHECK: // %bb.0: +; CHECK-NEXT: rdffr p0.b +; CHECK-NEXT: ret %out = call @llvm.aarch64.sve.rdffr() ret %out } -define @rdffr_z( %pg) { +define @rdffr_z( %pg) #0 { ; CHECK-LABEL: rdffr_z: -; CHECK: rdffr p0.b, p0/z -; CHECK-NEXT: ret +; CHECK: // %bb.0: +; CHECK-NEXT: rdffr p0.b, p0/z +; CHECK-NEXT: ret %out = call @llvm.aarch64.sve.rdffr.z( %pg) ret %out } +; Test that rdffr.z followed by ptest optimizes to flags-setting rdffrs. +define i1 @rdffr_z_ptest( %pg) #0 { +; CHECK-LABEL: rdffr_z_ptest: +; CHECK: // %bb.0: +; CHECK-NEXT: rdffrs p0.b, p0/z +; CHECK-NEXT: cset w0, ne +; CHECK-NEXT: ret + %rdffr = call @llvm.aarch64.sve.rdffr.z( %pg) + %out = call i1 @llvm.aarch64.sve.ptest.any.nxv16i1( %pg, %rdffr) + ret i1 %out +} + ; ; SETFFR ; -define void @set_ffr() { +define void @set_ffr() #0 { ; CHECK-LABEL: set_ffr: -; CHECK: setffr -; CHECK-NEXT: ret +; CHECK: // %bb.0: +; CHECK-NEXT: setffr +; CHECK-NEXT: ret call void @llvm.aarch64.sve.setffr() ret void } @@ -36,10 +54,11 @@ ; WRFFR ; -define void @wrffr( %a) { +define void @wrffr( %a) #0 { ; CHECK-LABEL: wrffr: -; CHECK: wrffr p0.b -; CHECK-NEXT: ret +; CHECK: // %bb.0: +; CHECK-NEXT: wrffr p0.b +; CHECK-NEXT: ret call void @llvm.aarch64.sve.wrffr( %a) ret void } @@ -48,3 +67,7 @@ declare @llvm.aarch64.sve.rdffr.z() declare void @llvm.aarch64.sve.setffr() declare void @llvm.aarch64.sve.wrffr() + +declare i1 @llvm.aarch64.sve.ptest.any.nxv16i1(, ) + +attributes #0 = { "target-features"="+sve" } diff --git a/llvm/test/CodeGen/AArch64/sve-intrinsics-rdffr-predication.ll b/llvm/test/CodeGen/AArch64/sve-intrinsics-rdffr-predication.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/sve-intrinsics-rdffr-predication.ll @@ -0,0 +1,19 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -instcombine < %s | FileCheck %s + +target triple = "aarch64-unknown-linux-gnu" + +; Test that rdffr is substituted with predicated form which enables ptest optimization later. +define @predicate_rdffr() #0 { +; CHECK-LABEL: @predicate_rdffr( +; CHECK-NEXT: [[TMP1:%.*]] = call @llvm.aarch64.sve.ptrue.nxv16i1(i32 31) +; CHECK-NEXT: [[OUT:%.*]] = call @llvm.aarch64.sve.rdffr.z( [[TMP1]]) +; CHECK-NEXT: ret [[OUT]] +; + %out = call @llvm.aarch64.sve.rdffr() + ret %out +} + +declare @llvm.aarch64.sve.rdffr() + +attributes #0 = { "target-features"="+sve" }