xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1fcaf7f86SDimitry Andric //===----- RISCVCodeGenPrepare.cpp ----------------------------------------===//
2fcaf7f86SDimitry Andric //
3fcaf7f86SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fcaf7f86SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fcaf7f86SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fcaf7f86SDimitry Andric //
7fcaf7f86SDimitry Andric //===----------------------------------------------------------------------===//
8fcaf7f86SDimitry Andric //
906c3fb27SDimitry Andric // This is a RISC-V specific version of CodeGenPrepare.
10fcaf7f86SDimitry Andric // It munges the code in the input function to better prepare it for
11fcaf7f86SDimitry Andric // SelectionDAG-based code generation. This works around limitations in it's
12fcaf7f86SDimitry Andric // basic-block-at-a-time approach.
13fcaf7f86SDimitry Andric //
14fcaf7f86SDimitry Andric //===----------------------------------------------------------------------===//
15fcaf7f86SDimitry Andric 
16fcaf7f86SDimitry Andric #include "RISCV.h"
17fcaf7f86SDimitry Andric #include "RISCVTargetMachine.h"
18fcaf7f86SDimitry Andric #include "llvm/ADT/Statistic.h"
19fcaf7f86SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
20fcaf7f86SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
21*7a6dacacSDimitry Andric #include "llvm/IR/IRBuilder.h"
22bdd1243dSDimitry Andric #include "llvm/IR/InstVisitor.h"
23*7a6dacacSDimitry Andric #include "llvm/IR/Intrinsics.h"
24972a253aSDimitry Andric #include "llvm/IR/PatternMatch.h"
25fcaf7f86SDimitry Andric #include "llvm/InitializePasses.h"
26fcaf7f86SDimitry Andric #include "llvm/Pass.h"
27fcaf7f86SDimitry Andric 
28fcaf7f86SDimitry Andric using namespace llvm;
29fcaf7f86SDimitry Andric 
30fcaf7f86SDimitry Andric #define DEBUG_TYPE "riscv-codegenprepare"
3106c3fb27SDimitry Andric #define PASS_NAME "RISC-V CodeGenPrepare"
32fcaf7f86SDimitry Andric 
33fcaf7f86SDimitry Andric namespace {
34fcaf7f86SDimitry Andric 
35bdd1243dSDimitry Andric class RISCVCodeGenPrepare : public FunctionPass,
36bdd1243dSDimitry Andric                             public InstVisitor<RISCVCodeGenPrepare, bool> {
37fcaf7f86SDimitry Andric   const DataLayout *DL;
38fcaf7f86SDimitry Andric   const RISCVSubtarget *ST;
39fcaf7f86SDimitry Andric 
40fcaf7f86SDimitry Andric public:
41fcaf7f86SDimitry Andric   static char ID;
42fcaf7f86SDimitry Andric 
43fcaf7f86SDimitry Andric   RISCVCodeGenPrepare() : FunctionPass(ID) {}
44fcaf7f86SDimitry Andric 
45fcaf7f86SDimitry Andric   bool runOnFunction(Function &F) override;
46fcaf7f86SDimitry Andric 
47fcaf7f86SDimitry Andric   StringRef getPassName() const override { return PASS_NAME; }
48fcaf7f86SDimitry Andric 
49fcaf7f86SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
50fcaf7f86SDimitry Andric     AU.setPreservesCFG();
51fcaf7f86SDimitry Andric     AU.addRequired<TargetPassConfig>();
52fcaf7f86SDimitry Andric   }
53fcaf7f86SDimitry Andric 
54bdd1243dSDimitry Andric   bool visitInstruction(Instruction &I) { return false; }
55bdd1243dSDimitry Andric   bool visitAnd(BinaryOperator &BO);
56*7a6dacacSDimitry Andric   bool visitIntrinsicInst(IntrinsicInst &I);
57fcaf7f86SDimitry Andric };
58fcaf7f86SDimitry Andric 
59fcaf7f86SDimitry Andric } // end anonymous namespace
60fcaf7f86SDimitry Andric 
61fcaf7f86SDimitry Andric // Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set,
625f757f3fSDimitry Andric // but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill
635f757f3fSDimitry Andric // the upper 32 bits with ones.
64bdd1243dSDimitry Andric bool RISCVCodeGenPrepare::visitAnd(BinaryOperator &BO) {
65fcaf7f86SDimitry Andric   if (!ST->is64Bit())
66fcaf7f86SDimitry Andric     return false;
67fcaf7f86SDimitry Andric 
68bdd1243dSDimitry Andric   if (!BO.getType()->isIntegerTy(64))
69fcaf7f86SDimitry Andric     return false;
70fcaf7f86SDimitry Andric 
71*7a6dacacSDimitry Andric   using namespace PatternMatch;
725f757f3fSDimitry Andric 
73*7a6dacacSDimitry Andric   // Left hand side should be a zext nneg.
74*7a6dacacSDimitry Andric   Value *LHSSrc;
75*7a6dacacSDimitry Andric   if (!match(BO.getOperand(0), m_NNegZExt(m_Value(LHSSrc))))
76fcaf7f86SDimitry Andric     return false;
77fcaf7f86SDimitry Andric 
78fcaf7f86SDimitry Andric   if (!LHSSrc->getType()->isIntegerTy(32))
79fcaf7f86SDimitry Andric     return false;
80fcaf7f86SDimitry Andric 
81fcaf7f86SDimitry Andric   // Right hand side should be a constant.
82bdd1243dSDimitry Andric   Value *RHS = BO.getOperand(1);
83fcaf7f86SDimitry Andric 
84fcaf7f86SDimitry Andric   auto *CI = dyn_cast<ConstantInt>(RHS);
85fcaf7f86SDimitry Andric   if (!CI)
86fcaf7f86SDimitry Andric     return false;
87fcaf7f86SDimitry Andric   uint64_t C = CI->getZExtValue();
88fcaf7f86SDimitry Andric 
89fcaf7f86SDimitry Andric   // Look for constants that fit in 32 bits but not simm12, and can be made
90fcaf7f86SDimitry Andric   // into simm12 by sign extending bit 31. This will allow use of ANDI.
91fcaf7f86SDimitry Andric   // TODO: Is worth making simm32?
92fcaf7f86SDimitry Andric   if (!isUInt<32>(C) || isInt<12>(C) || !isInt<12>(SignExtend64<32>(C)))
93fcaf7f86SDimitry Andric     return false;
94fcaf7f86SDimitry Andric 
95fcaf7f86SDimitry Andric   // Sign extend the constant and replace the And operand.
96fcaf7f86SDimitry Andric   C = SignExtend64<32>(C);
97*7a6dacacSDimitry Andric   BO.setOperand(1, ConstantInt::get(RHS->getType(), C));
98*7a6dacacSDimitry Andric 
99*7a6dacacSDimitry Andric   return true;
100*7a6dacacSDimitry Andric }
101*7a6dacacSDimitry Andric 
102*7a6dacacSDimitry Andric // LLVM vector reduction intrinsics return a scalar result, but on RISC-V vector
103*7a6dacacSDimitry Andric // reduction instructions write the result in the first element of a vector
104*7a6dacacSDimitry Andric // register. So when a reduction in a loop uses a scalar phi, we end up with
105*7a6dacacSDimitry Andric // unnecessary scalar moves:
106*7a6dacacSDimitry Andric //
107*7a6dacacSDimitry Andric // loop:
108*7a6dacacSDimitry Andric // vfmv.s.f v10, fa0
109*7a6dacacSDimitry Andric // vfredosum.vs v8, v8, v10
110*7a6dacacSDimitry Andric // vfmv.f.s fa0, v8
111*7a6dacacSDimitry Andric //
112*7a6dacacSDimitry Andric // This mainly affects ordered fadd reductions, since other types of reduction
113*7a6dacacSDimitry Andric // typically use element-wise vectorisation in the loop body. This tries to
114*7a6dacacSDimitry Andric // vectorize any scalar phis that feed into a fadd reduction:
115*7a6dacacSDimitry Andric //
116*7a6dacacSDimitry Andric // loop:
117*7a6dacacSDimitry Andric // %phi = phi <float> [ ..., %entry ], [ %acc, %loop ]
118*7a6dacacSDimitry Andric // %acc = call float @llvm.vector.reduce.fadd.nxv4f32(float %phi, <vscale x 2 x float> %vec)
119*7a6dacacSDimitry Andric //
120*7a6dacacSDimitry Andric // ->
121*7a6dacacSDimitry Andric //
122*7a6dacacSDimitry Andric // loop:
123*7a6dacacSDimitry Andric // %phi = phi <vscale x 2 x float> [ ..., %entry ], [ %acc.vec, %loop ]
124*7a6dacacSDimitry Andric // %phi.scalar = extractelement <vscale x 2 x float> %phi, i64 0
125*7a6dacacSDimitry Andric // %acc = call float @llvm.vector.reduce.fadd.nxv4f32(float %x, <vscale x 2 x float> %vec)
126*7a6dacacSDimitry Andric // %acc.vec = insertelement <vscale x 2 x float> poison, float %acc.next, i64 0
127*7a6dacacSDimitry Andric //
128*7a6dacacSDimitry Andric // Which eliminates the scalar -> vector -> scalar crossing during instruction
129*7a6dacacSDimitry Andric // selection.
130*7a6dacacSDimitry Andric bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
131*7a6dacacSDimitry Andric   if (I.getIntrinsicID() != Intrinsic::vector_reduce_fadd)
132*7a6dacacSDimitry Andric     return false;
133*7a6dacacSDimitry Andric 
134*7a6dacacSDimitry Andric   auto *PHI = dyn_cast<PHINode>(I.getOperand(0));
135*7a6dacacSDimitry Andric   if (!PHI || !PHI->hasOneUse() ||
136*7a6dacacSDimitry Andric       !llvm::is_contained(PHI->incoming_values(), &I))
137*7a6dacacSDimitry Andric     return false;
138*7a6dacacSDimitry Andric 
139*7a6dacacSDimitry Andric   Type *VecTy = I.getOperand(1)->getType();
140*7a6dacacSDimitry Andric   IRBuilder<> Builder(PHI);
141*7a6dacacSDimitry Andric   auto *VecPHI = Builder.CreatePHI(VecTy, PHI->getNumIncomingValues());
142*7a6dacacSDimitry Andric 
143*7a6dacacSDimitry Andric   for (auto *BB : PHI->blocks()) {
144*7a6dacacSDimitry Andric     Builder.SetInsertPoint(BB->getTerminator());
145*7a6dacacSDimitry Andric     Value *InsertElt = Builder.CreateInsertElement(
146*7a6dacacSDimitry Andric         VecTy, PHI->getIncomingValueForBlock(BB), (uint64_t)0);
147*7a6dacacSDimitry Andric     VecPHI->addIncoming(InsertElt, BB);
148*7a6dacacSDimitry Andric   }
149*7a6dacacSDimitry Andric 
150*7a6dacacSDimitry Andric   Builder.SetInsertPoint(&I);
151*7a6dacacSDimitry Andric   I.setOperand(0, Builder.CreateExtractElement(VecPHI, (uint64_t)0));
152*7a6dacacSDimitry Andric 
153*7a6dacacSDimitry Andric   PHI->eraseFromParent();
154fcaf7f86SDimitry Andric 
155fcaf7f86SDimitry Andric   return true;
156fcaf7f86SDimitry Andric }
157fcaf7f86SDimitry Andric 
158fcaf7f86SDimitry Andric bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
159fcaf7f86SDimitry Andric   if (skipFunction(F))
160fcaf7f86SDimitry Andric     return false;
161fcaf7f86SDimitry Andric 
162fcaf7f86SDimitry Andric   auto &TPC = getAnalysis<TargetPassConfig>();
163fcaf7f86SDimitry Andric   auto &TM = TPC.getTM<RISCVTargetMachine>();
164fcaf7f86SDimitry Andric   ST = &TM.getSubtarget<RISCVSubtarget>(F);
165fcaf7f86SDimitry Andric 
166fcaf7f86SDimitry Andric   DL = &F.getParent()->getDataLayout();
167fcaf7f86SDimitry Andric 
168fcaf7f86SDimitry Andric   bool MadeChange = false;
169bdd1243dSDimitry Andric   for (auto &BB : F)
170bdd1243dSDimitry Andric     for (Instruction &I : llvm::make_early_inc_range(BB))
171bdd1243dSDimitry Andric       MadeChange |= visit(I);
172fcaf7f86SDimitry Andric 
173fcaf7f86SDimitry Andric   return MadeChange;
174fcaf7f86SDimitry Andric }
175fcaf7f86SDimitry Andric 
176fcaf7f86SDimitry Andric INITIALIZE_PASS_BEGIN(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false)
177fcaf7f86SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
178fcaf7f86SDimitry Andric INITIALIZE_PASS_END(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false)
179fcaf7f86SDimitry Andric 
180fcaf7f86SDimitry Andric char RISCVCodeGenPrepare::ID = 0;
181fcaf7f86SDimitry Andric 
182fcaf7f86SDimitry Andric FunctionPass *llvm::createRISCVCodeGenPreparePass() {
183fcaf7f86SDimitry Andric   return new RISCVCodeGenPrepare();
184fcaf7f86SDimitry Andric }
185