33#define DEBUG_TYPE "riscv-codegenprepare"
34#define PASS_NAME "RISC-V CodeGenPrepare"
37class RISCVCodeGenPrepare :
public InstVisitor<RISCVCodeGenPrepare, bool> {
46 :
F(
F),
DL(&
F.getDataLayout()), DT(DT), ST(ST) {}
59class RISCVCodeGenPrepareLegacyPass :
public FunctionPass {
63 RISCVCodeGenPrepareLegacyPass() : FunctionPass(ID) {}
66 StringRef getPassName()
const override {
return PASS_NAME; }
68 void getAnalysisUsage(AnalysisUsage &AU)
const override {
86 using namespace PatternMatch;
102 uint64_t
C = CI->getZExtValue();
148bool RISCVCodeGenPrepare::widenVPMerge(Instruction *Root) {
153 using namespace PatternMatch;
160 if (!Phi || !
Phi->hasOneUse() ||
Phi->getNumIncomingValues() != 2 ||
162 Phi->getIncomingValue(1) != Root)
166 VectorType::get(IntegerType::getInt8Ty(Root->
getContext()),
170 PHINode *WidePhi = Builder.CreatePHI(WideTy, 2);
172 Phi->getIncomingBlock(0));
173 Builder.SetInsertPoint(Root);
174 Value *WideTrue = Builder.CreateZExt(True, WideTy);
175 Value *WideMerge = Builder.CreateIntrinsic(Intrinsic::vp_merge, {WideTy},
176 {
Mask, WideTrue, WidePhi, EVL});
178 WideMerge = Builder.CreateFreeze(WideMerge);
180 Value *Trunc = Builder.CreateTrunc(WideMerge, Root->
getType());
185 Phi->setIncomingValue(1,
Phi->getIncomingValue(0));
191bool RISCVCodeGenPrepare::visitFreezeInst(FreezeInst &
I) {
193 if (
II->getIntrinsicID() == Intrinsic::vp_merge)
194 return widenVPMerge(&
I);
229bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &
I) {
230 if (expandVPStrideLoad(
I))
233 if (expandMulReduction(
I))
236 if (widenVPMerge(&
I))
239 if (
I.getIntrinsicID() != Intrinsic::vector_reduce_fadd &&
244 if (!
PHI || !
PHI->hasOneUse() ||
248 Type *VecTy =
I.getOperand(1)->getType();
250 auto *VecPHI = Builder.CreatePHI(VecTy,
PHI->getNumIncomingValues());
252 for (
auto *BB :
PHI->blocks()) {
253 Builder.SetInsertPoint(BB->getTerminator());
254 Value *InsertElt = Builder.CreateInsertElement(
255 VecTy,
PHI->getIncomingValueForBlock(BB), (uint64_t)0);
256 VecPHI->addIncoming(InsertElt, BB);
259 Builder.SetInsertPoint(&
I);
260 I.setOperand(0, Builder.CreateExtractElement(VecPHI, (uint64_t)0));
262 PHI->eraseFromParent();
270bool RISCVCodeGenPrepare::expandMulReduction(IntrinsicInst &
II) {
271 if (
II.getIntrinsicID() != Intrinsic::vector_reduce_mul)
277 Value *TmpVec =
II.getArgOperand(0);
283 unsigned VF = VecTy->getNumElements();
285 unsigned M1VF = MinVLen / EltSize;
295 for (
unsigned LiveElts = VF; LiveElts > M1VF; LiveElts /= 2) {
296 unsigned Half = LiveElts / 2;
297 std::iota(ShuffleMask.begin(), ShuffleMask.begin() + Half, Half);
298 std::fill(ShuffleMask.begin() + Half, ShuffleMask.end(), -1);
299 Value *Shuf = Builder.CreateShuffleVector(TmpVec, ShuffleMask,
"rdx.shuf");
300 TmpVec = Builder.CreateMul(TmpVec, Shuf,
"bin.rdx");
307 Builder.CreateExtractVector(M1Ty, TmpVec, (uint64_t)0,
"rdx.sub");
309 Builder.CreateIntrinsic(Intrinsic::vector_reduce_mul, {M1Ty}, {
Sub});
310 II.replaceAllUsesWith(Rdx);
311 II.eraseFromParent();
318bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &
II) {
321 using namespace PatternMatch;
328 if (
II.getType()->getScalarSizeInBits() > ST->
getXLen())
337 Type *STy = VTy->getElementType();
338 Value *Val = Builder.CreateLoad(STy, BasePtr);
339 Value *Res = Builder.CreateIntrinsic(
340 Intrinsic::vp_merge, VTy,
341 {
II.getOperand(2), Builder.CreateVectorSplat(VTy->getElementCount(), Val),
344 II.replaceAllUsesWith(Res);
345 II.eraseFromParent();
349bool RISCVCodeGenPrepare::run() {
350 bool MadeChange =
false;
358bool RISCVCodeGenPrepareLegacyPass::runOnFunction(Function &
F) {
362 auto &TPC = getAnalysis<TargetPassConfig>();
363 auto &TM = TPC.getTM<RISCVTargetMachine>();
364 auto ST = &TM.getSubtarget<RISCVSubtarget>(
F);
365 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
367 RISCVCodeGenPrepare RVCGP(
F, DT, ST);
377char RISCVCodeGenPrepareLegacyPass::
ID = 0;
380 return new RISCVCodeGenPrepareLegacyPass();
387 bool Changed = RISCVCodeGenPrepare(
F, DT, ST).run();
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool runOnFunction(Function &F, bool PostInlining)
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
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.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
static LLVM_ABI ConstantAggregateZero * get(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
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.
Base class for instruction visitors.
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.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
unsigned getRealMinVLen() const
bool hasVInstructions() const
Target-Independent Code Generator Pass Configuration Options.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
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.
@ C
The default llvm calling convention, compatible with C.
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
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))
auto m_Value()
Match an arbitrary value and ignore it.
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
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.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
FunctionPass * createRISCVCodeGenPrepareLegacyPass()
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
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...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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...
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 >
@ Sub
Subtraction of integers.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.