LLVM 23.0.0git
RISCVCodeGenPrepare.cpp
Go to the documentation of this file.
1//===----- RISCVCodeGenPrepare.cpp ----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is a RISC-V specific version of CodeGenPrepare.
10// It munges the code in the input function to better prepare it for
11// SelectionDAG-based code generation. This works around limitations in it's
12// basic-block-at-a-time approach.
13//
14//===----------------------------------------------------------------------===//
15
16#include "RISCV.h"
17#include "RISCVTargetMachine.h"
18#include "llvm/ADT/Statistic.h"
21#include "llvm/IR/Dominators.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/InstVisitor.h"
24#include "llvm/IR/Intrinsics.h"
27#include "llvm/Pass.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "riscv-codegenprepare"
33#define PASS_NAME "RISC-V CodeGenPrepare"
34
35namespace {
36class RISCVCodeGenPrepare : public InstVisitor<RISCVCodeGenPrepare, bool> {
37 Function &F;
38 const DataLayout *DL;
39 const DominatorTree *DT;
40 const RISCVSubtarget *ST;
41
42public:
43 RISCVCodeGenPrepare(Function &F, const DominatorTree *DT,
44 const RISCVSubtarget *ST)
45 : F(F), DL(&F.getDataLayout()), DT(DT), ST(ST) {}
46 bool run();
47 bool visitInstruction(Instruction &I) { return false; }
48 bool visitAnd(BinaryOperator &BO);
49 bool visitIntrinsicInst(IntrinsicInst &I);
50 bool expandVPStrideLoad(IntrinsicInst &I);
51 bool widenVPMerge(Instruction *I);
52 bool visitFreezeInst(FreezeInst &BO);
53};
54} // namespace
55
56namespace {
57class RISCVCodeGenPrepareLegacyPass : public FunctionPass {
58public:
59 static char ID;
60
61 RISCVCodeGenPrepareLegacyPass() : FunctionPass(ID) {}
62
63 bool runOnFunction(Function &F) override;
64 StringRef getPassName() const override { return PASS_NAME; }
65
66 void getAnalysisUsage(AnalysisUsage &AU) const override {
67 AU.setPreservesCFG();
68 AU.addRequired<DominatorTreeWrapperPass>();
69 AU.addRequired<TargetPassConfig>();
70 }
71};
72} // namespace
73
74// Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set,
75// but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill
76// the upper 32 bits with ones.
77bool RISCVCodeGenPrepare::visitAnd(BinaryOperator &BO) {
78 if (!ST->is64Bit())
79 return false;
80
81 if (!BO.getType()->isIntegerTy(64))
82 return false;
83
84 using namespace PatternMatch;
85
86 // Left hand side should be a zext nneg.
87 Value *LHSSrc;
88 if (!match(BO.getOperand(0), m_NNegZExt(m_Value(LHSSrc))))
89 return false;
90
91 if (!LHSSrc->getType()->isIntegerTy(32))
92 return false;
93
94 // Right hand side should be a constant.
95 Value *RHS = BO.getOperand(1);
96
97 auto *CI = dyn_cast<ConstantInt>(RHS);
98 if (!CI)
99 return false;
100 uint64_t C = CI->getZExtValue();
101
102 // Look for constants that fit in 32 bits but not simm12, and can be made
103 // into simm12 by sign extending bit 31. This will allow use of ANDI.
104 // TODO: Is worth making simm32?
106 return false;
107
108 // Sign extend the constant and replace the And operand.
110 BO.setOperand(1, ConstantInt::get(RHS->getType(), C));
111
112 return true;
113}
114
115// With EVL tail folding, an AnyOf reduction will generate an i1 vp.merge like
116// follows:
117//
118// loop:
119// %phi = phi <vscale x 4 x i1> [zeroinitializer, %entry], [%freeze, %loop]
120// %cmp = icmp ...
121// %rec = call <vscale x 4 x i1> @llvm.vp.merge(%cmp, i1 true, %phi, %evl)
122// %freeze = freeze <vscale x 4 x i1> %rec [optional]
123// ...
124// middle:
125// %res = call i1 @llvm.vector.reduce.or(<vscale x 4 x i1> %freeze)
126//
127// However RVV doesn't have any tail undisturbed mask instructions and so we
128// need a convoluted sequence of mask instructions to lower the i1 vp.merge: see
129// llvm/test/CodeGen/RISCV/rvv/vpmerge-sdnode.ll.
130//
131// To avoid that this widens the i1 vp.merge to an i8 vp.merge, which will
132// generate a single vmerge.vim:
133//
134// loop:
135// %phi = phi <vscale x 4 x i8> [zeroinitializer, %entry], [%freeze, %loop]
136// %cmp = icmp ...
137// %rec = call <vscale x 4 x i8> @llvm.vp.merge(%cmp, i8 true, %phi, %evl)
138// %freeze = freeze <vscale x 4 x i8> %rec
139// %trunc = trunc <vscale x 4 x i8> %freeze to <vscale x 4 x i1>
140// ...
141// middle:
142// %res = call i1 @llvm.vector.reduce.or(<vscale x 4 x i1> %trunc)
143//
144// The trunc will normally be sunk outside of the loop, but even if there are
145// users inside the loop it is still profitable.
146bool RISCVCodeGenPrepare::widenVPMerge(Instruction *Root) {
147 if (!Root->getType()->getScalarType()->isIntegerTy(1))
148 return false;
149
150 Value *Mask, *True, *PhiV, *EVL;
151 using namespace PatternMatch;
152 auto m_VPMerge = m_Intrinsic<Intrinsic::vp_merge>(
153 m_Value(Mask), m_Value(True), m_Value(PhiV), m_Value(EVL));
154 if (!match(Root, m_CombineOr(m_VPMerge, m_Freeze(m_VPMerge))))
155 return false;
156
157 auto *Phi = dyn_cast<PHINode>(PhiV);
158 if (!Phi || !Phi->hasOneUse() || Phi->getNumIncomingValues() != 2 ||
159 !match(Phi->getIncomingValue(0), m_Zero()) ||
160 Phi->getIncomingValue(1) != Root)
161 return false;
162
163 Type *WideTy =
164 VectorType::get(IntegerType::getInt8Ty(Root->getContext()),
165 cast<VectorType>(Root->getType())->getElementCount());
166
167 IRBuilder<> Builder(Phi);
168 PHINode *WidePhi = Builder.CreatePHI(WideTy, 2);
170 Phi->getIncomingBlock(0));
171 Builder.SetInsertPoint(Root);
172 Value *WideTrue = Builder.CreateZExt(True, WideTy);
173 Value *WideMerge = Builder.CreateIntrinsic(Intrinsic::vp_merge, {WideTy},
174 {Mask, WideTrue, WidePhi, EVL});
175 if (isa<FreezeInst>(Root))
176 WideMerge = Builder.CreateFreeze(WideMerge);
177 WidePhi->addIncoming(WideMerge, Phi->getIncomingBlock(1));
178 Value *Trunc = Builder.CreateTrunc(WideMerge, Root->getType());
179
180 Root->replaceAllUsesWith(Trunc);
181
182 // Break the cycle and delete the old chain.
183 Phi->setIncomingValue(1, Phi->getIncomingValue(0));
185
186 return true;
187}
188
189bool RISCVCodeGenPrepare::visitFreezeInst(FreezeInst &I) {
190 if (auto *II = dyn_cast<IntrinsicInst>(I.getOperand(0)))
191 if (II->getIntrinsicID() == Intrinsic::vp_merge)
192 return widenVPMerge(&I);
193 return false;
194}
195
196// LLVM vector reduction intrinsics return a scalar result, but on RISC-V vector
197// reduction instructions write the result in the first element of a vector
198// register. So when a reduction in a loop uses a scalar phi, we end up with
199// unnecessary scalar moves:
200//
201// loop:
202// vfmv.s.f v10, fa0
203// vfredosum.vs v8, v8, v10
204// vfmv.f.s fa0, v8
205//
206// This mainly affects ordered fadd reductions and VP reductions that have a
207// scalar start value, since other types of reduction typically use element-wise
208// vectorisation in the loop body. This tries to vectorize any scalar phis that
209// feed into these reductions:
210//
211// loop:
212// %phi = phi <float> [ ..., %entry ], [ %acc, %loop ]
213// %acc = call float @llvm.vector.reduce.fadd.nxv2f32(float %phi,
214// <vscale x 2 x float> %vec)
215//
216// ->
217//
218// loop:
219// %phi = phi <vscale x 2 x float> [ ..., %entry ], [ %acc.vec, %loop ]
220// %phi.scalar = extractelement <vscale x 2 x float> %phi, i64 0
221// %acc = call float @llvm.vector.reduce.fadd.nxv2f32(float %x,
222// <vscale x 2 x float> %vec)
223// %acc.vec = insertelement <vscale x 2 x float> poison, float %acc.next, i64 0
224//
225// Which eliminates the scalar -> vector -> scalar crossing during instruction
226// selection.
227bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
228 if (expandVPStrideLoad(I))
229 return true;
230
231 if (widenVPMerge(&I))
232 return true;
233
234 if (I.getIntrinsicID() != Intrinsic::vector_reduce_fadd &&
236 return false;
237
238 auto *PHI = dyn_cast<PHINode>(I.getOperand(0));
239 if (!PHI || !PHI->hasOneUse() ||
240 !llvm::is_contained(PHI->incoming_values(), &I))
241 return false;
242
243 Type *VecTy = I.getOperand(1)->getType();
244 IRBuilder<> Builder(PHI);
245 auto *VecPHI = Builder.CreatePHI(VecTy, PHI->getNumIncomingValues());
246
247 for (auto *BB : PHI->blocks()) {
248 Builder.SetInsertPoint(BB->getTerminator());
249 Value *InsertElt = Builder.CreateInsertElement(
250 VecTy, PHI->getIncomingValueForBlock(BB), (uint64_t)0);
251 VecPHI->addIncoming(InsertElt, BB);
252 }
253
254 Builder.SetInsertPoint(&I);
255 I.setOperand(0, Builder.CreateExtractElement(VecPHI, (uint64_t)0));
256
257 PHI->eraseFromParent();
258
259 return true;
260}
261
262// Always expand zero strided loads so we match more .vx splat patterns, even if
263// we have +optimized-zero-stride-loads. RISCVDAGToDAGISel::Select will convert
264// it back to a strided load if it's optimized.
265bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) {
266 Value *BasePtr, *VL;
267
268 using namespace PatternMatch;
270 m_Value(BasePtr), m_Zero(), m_AllOnes(), m_Value(VL))))
271 return false;
272
273 // If SEW>XLEN then a splat will get lowered as a zero strided load anyway, so
274 // avoid expanding here.
275 if (II.getType()->getScalarSizeInBits() > ST->getXLen())
276 return false;
277
278 if (!isKnownNonZero(VL, {*DL, DT, nullptr, &II}))
279 return false;
280
281 auto *VTy = cast<VectorType>(II.getType());
282
283 IRBuilder<> Builder(&II);
284 Type *STy = VTy->getElementType();
285 Value *Val = Builder.CreateLoad(STy, BasePtr);
286 Value *Res = Builder.CreateIntrinsic(
287 Intrinsic::vp_merge, VTy,
288 {II.getOperand(2), Builder.CreateVectorSplat(VTy->getElementCount(), Val),
289 PoisonValue::get(VTy), VL});
290
291 II.replaceAllUsesWith(Res);
292 II.eraseFromParent();
293 return true;
294}
295
296bool RISCVCodeGenPrepare::run() {
297 bool MadeChange = false;
298 for (auto &BB : F)
299 for (Instruction &I : llvm::make_early_inc_range(BB))
300 MadeChange |= visit(I);
301
302 return MadeChange;
303}
304
305bool RISCVCodeGenPrepareLegacyPass::runOnFunction(Function &F) {
306 if (skipFunction(F))
307 return false;
308
309 auto &TPC = getAnalysis<TargetPassConfig>();
310 auto &TM = TPC.getTM<RISCVTargetMachine>();
311 auto ST = &TM.getSubtarget<RISCVSubtarget>(F);
312 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
313
314 RISCVCodeGenPrepare RVCGP(F, DT, ST);
315 return RVCGP.run();
316}
317
318INITIALIZE_PASS_BEGIN(RISCVCodeGenPrepareLegacyPass, DEBUG_TYPE, PASS_NAME,
319 false, false)
321INITIALIZE_PASS_END(RISCVCodeGenPrepareLegacyPass, DEBUG_TYPE, PASS_NAME, false,
322 false)
323
324char RISCVCodeGenPrepareLegacyPass::ID = 0;
325
327 return new RISCVCodeGenPrepareLegacyPass();
328}
329
332 DominatorTree *DT = &FAM.getResult<DominatorTreeAnalysis>(F);
333 auto ST = &TM->getSubtarget<RISCVSubtarget>(F);
334 bool Changed = RISCVCodeGenPrepare(F, DT, ST).run();
335 if (!Changed)
336 return PreservedAnalyses::all();
337
340 return PA;
341}
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define PASS_NAME
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Target-Independent Code Generator Pass Configuration Options pass.
#define PASS_NAME
Value * RHS
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
static LLVM_ABI ConstantAggregateZero * get(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Analysis pass which computes a DominatorTree.
Definition Dominators.h:278
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Base class for instruction visitors.
Definition InstVisitor.h:78
A wrapper class for inspecting calls to intrinsic functions.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
unsigned getXLen() const
Target-Independent Code Generator Pass Configuration Options.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
void setOperand(unsigned i, Value *Val)
Definition User.h:212
Value * getOperand(unsigned i) const
Definition User.h:207
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:259
Changed
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
bool match(Val *V, const Pattern &P)
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:535
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
FunctionPass * createRISCVCodeGenPrepareLegacyPass()
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.