//===----- RISCVCodeGenPrepare.cpp ----------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This is a RISC-V specific version of CodeGenPrepare. // It munges the code in the input function to better prepare it for // SelectionDAG-based code generation. This works around limitations in it's // basic-block-at-a-time approach. // //===----------------------------------------------------------------------===// #include "RISCV.h" #include "RISCVTargetMachine.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstVisitor.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/IntrinsicsRISCV.h" #include "llvm/IR/PatternMatch.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" usingnamespacellvm; #define DEBUG_TYPE … #define PASS_NAME … namespace { class RISCVCodeGenPrepare : public FunctionPass, public InstVisitor<RISCVCodeGenPrepare, bool> { … }; } // end anonymous namespace // Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set, // but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill // the upper 32 bits with ones. bool RISCVCodeGenPrepare::visitAnd(BinaryOperator &BO) { … } // LLVM vector reduction intrinsics return a scalar result, but on RISC-V vector // reduction instructions write the result in the first element of a vector // register. So when a reduction in a loop uses a scalar phi, we end up with // unnecessary scalar moves: // // loop: // vfmv.s.f v10, fa0 // vfredosum.vs v8, v8, v10 // vfmv.f.s fa0, v8 // // This mainly affects ordered fadd reductions, since other types of reduction // typically use element-wise vectorisation in the loop body. This tries to // vectorize any scalar phis that feed into a fadd reduction: // // loop: // %phi = phi <float> [ ..., %entry ], [ %acc, %loop ] // %acc = call float @llvm.vector.reduce.fadd.nxv2f32(float %phi, // <vscale x 2 x float> %vec) // // -> // // loop: // %phi = phi <vscale x 2 x float> [ ..., %entry ], [ %acc.vec, %loop ] // %phi.scalar = extractelement <vscale x 2 x float> %phi, i64 0 // %acc = call float @llvm.vector.reduce.fadd.nxv2f32(float %x, // <vscale x 2 x float> %vec) // %acc.vec = insertelement <vscale x 2 x float> poison, float %acc.next, i64 0 // // Which eliminates the scalar -> vector -> scalar crossing during instruction // selection. bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) { … } // Always expand zero strided loads so we match more .vx splat patterns, even if // we have +optimized-zero-stride-loads. RISCVDAGToDAGISel::Select will convert // it back to a strided load if it's optimized. bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) { … } bool RISCVCodeGenPrepare::runOnFunction(Function &F) { … } INITIALIZE_PASS_BEGIN(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false) INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) INITIALIZE_PASS_END(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false) char RISCVCodeGenPrepare::ID = …; FunctionPass *llvm::createRISCVCodeGenPreparePass() { … }