32#define DEBUG_TYPE "riscv-codegenprepare"
33#define PASS_NAME "RISC-V CodeGenPrepare"
36class RISCVCodeGenPrepare :
public InstVisitor<RISCVCodeGenPrepare, bool> {
45 :
F(
F),
DL(&
F.getDataLayout()), DT(DT), ST(ST) {}
56class RISCVCodeGenPrepareLegacyPass :
public FunctionPass {
60 RISCVCodeGenPrepareLegacyPass() : FunctionPass(ID) {}
63 StringRef getPassName()
const override {
return PASS_NAME; }
65 void getAnalysisUsage(AnalysisUsage &AU)
const override {
83 using namespace PatternMatch;
99 uint64_t
C = CI->getZExtValue();
143bool RISCVCodeGenPrepare::widenVPMerge(IntrinsicInst &
II) {
144 if (!
II.getType()->getScalarType()->isIntegerTy(1))
148 using namespace PatternMatch;
155 if (!Phi || !
Phi->hasOneUse() ||
Phi->getNumIncomingValues() != 2 ||
157 Phi->getIncomingValue(1) != &
II)
161 VectorType::get(IntegerType::getInt8Ty(
II.getContext()),
165 PHINode *WidePhi = Builder.CreatePHI(WideTy, 2);
167 Phi->getIncomingBlock(0));
168 Builder.SetInsertPoint(&
II);
169 Value *WideTrue = Builder.CreateZExt(True, WideTy);
170 Value *WideMerge = Builder.CreateIntrinsic(Intrinsic::vp_merge, {WideTy},
171 {
Mask, WideTrue, WidePhi, EVL});
173 Value *Trunc = Builder.CreateTrunc(WideMerge,
II.getType());
175 II.replaceAllUsesWith(Trunc);
178 Phi->setIncomingValue(1,
Phi->getIncomingValue(0));
215bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &
I) {
216 if (expandVPStrideLoad(
I))
222 if (
I.getIntrinsicID() != Intrinsic::vector_reduce_fadd &&
227 if (!
PHI || !
PHI->hasOneUse() ||
231 Type *VecTy =
I.getOperand(1)->getType();
233 auto *VecPHI = Builder.CreatePHI(VecTy,
PHI->getNumIncomingValues());
235 for (
auto *BB :
PHI->blocks()) {
236 Builder.SetInsertPoint(BB->getTerminator());
237 Value *InsertElt = Builder.CreateInsertElement(
238 VecTy,
PHI->getIncomingValueForBlock(BB), (uint64_t)0);
239 VecPHI->addIncoming(InsertElt, BB);
242 Builder.SetInsertPoint(&
I);
243 I.setOperand(0, Builder.CreateExtractElement(VecPHI, (uint64_t)0));
245 PHI->eraseFromParent();
253bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &
II) {
256 using namespace PatternMatch;
263 if (
II.getType()->getScalarSizeInBits() > ST->
getXLen())
272 Type *STy = VTy->getElementType();
273 Value *Val = Builder.CreateLoad(STy, BasePtr);
274 Value *Res = Builder.CreateIntrinsic(Intrinsic::experimental_vp_splat, {VTy},
275 {Val,
II.getOperand(2), VL});
277 II.replaceAllUsesWith(Res);
278 II.eraseFromParent();
282bool RISCVCodeGenPrepare::run() {
283 bool MadeChange =
false;
291bool RISCVCodeGenPrepareLegacyPass::runOnFunction(Function &
F) {
295 auto &TPC = getAnalysis<TargetPassConfig>();
296 auto &TM = TPC.getTM<RISCVTargetMachine>();
297 auto ST = &TM.getSubtarget<RISCVSubtarget>(
F);
298 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
300 RISCVCodeGenPrepare RVCGP(
F, DT, ST);
310char RISCVCodeGenPrepareLegacyPass::
ID = 0;
313 return new RISCVCodeGenPrepareLegacyPass();
320 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)
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> 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.
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.
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)
Target-Independent Code Generator Pass Configuration Options.
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.
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.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
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.
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 isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
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 >
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.