24#include "llvm/IR/IntrinsicsRISCV.h"
30using namespace PatternMatch;
32#define DEBUG_TYPE "riscv-gather-scatter-lowering"
63 return "RISC-V gather/scatter lowering";
70 std::pair<Value *, Value *> determineBaseAndStride(
Instruction *
Ptr,
80char RISCVGatherScatterLowering::ID = 0;
83 "RISC-V gather/scatter lowering pass",
false,
false)
86 return new RISCVGatherScatterLowering();
91 if (!isa<FixedVectorType>(StartC->
getType()))
92 return std::make_pair(
nullptr,
nullptr);
94 unsigned NumElts = cast<FixedVectorType>(StartC->
getType())->getNumElements();
100 return std::make_pair(
nullptr,
nullptr);
101 APInt StrideVal(StartVal->getValue().getBitWidth(), 0);
103 for (
unsigned i = 1; i != NumElts; ++i) {
106 return std::make_pair(
nullptr,
nullptr);
110 StrideVal = LocalStride;
111 else if (StrideVal != LocalStride)
112 return std::make_pair(
nullptr,
nullptr);
117 Value *Stride = ConstantInt::get(StartVal->getType(), StrideVal);
119 return std::make_pair(StartVal, Stride);
125 auto *StartC = dyn_cast<Constant>(Start);
130 if (
match(Start, m_Intrinsic<Intrinsic::experimental_stepvector>())) {
131 auto *Ty = Start->getType()->getScalarType();
132 return std::make_pair(ConstantInt::get(Ty, 0), ConstantInt::get(Ty, 1));
137 auto *BO = dyn_cast<BinaryOperator>(Start);
138 if (!BO || (BO->getOpcode() != Instruction::Add &&
139 BO->getOpcode() != Instruction::Or &&
140 BO->getOpcode() != Instruction::Shl &&
141 BO->getOpcode() != Instruction::Mul))
142 return std::make_pair(
nullptr,
nullptr);
144 if (BO->getOpcode() == Instruction::Or &&
145 !cast<PossiblyDisjointInst>(BO)->isDisjoint())
146 return std::make_pair(
nullptr,
nullptr);
149 unsigned OtherIndex = 0;
156 return std::make_pair(
nullptr,
nullptr);
162 return std::make_pair(
nullptr,
nullptr);
168 switch (BO->getOpcode()) {
171 case Instruction::Or:
175 case Instruction::Add:
178 case Instruction::Mul:
182 case Instruction::Shl:
188 return std::make_pair(Start, Stride);
195bool RISCVGatherScatterLowering::matchStridedRecurrence(
Value *
Index,
Loop *L,
201 if (
auto *Phi = dyn_cast<PHINode>(
Index)) {
204 if (
Phi->getParent() !=
L->getHeader())
211 assert(
Phi->getNumIncomingValues() == 2 &&
"Expected 2 operand phi.");
212 unsigned IncrementingBlock =
Phi->getIncomingValue(0) == Inc ? 0 : 1;
213 assert(
Phi->getIncomingValue(IncrementingBlock) == Inc &&
214 "Expected one operand of phi to be Inc");
217 if (!
L->isLoopInvariant(Step))
228 assert(Stride !=
nullptr);
233 Inc = BinaryOperator::CreateAdd(BasePtr, Step, Inc->
getName() +
".scalar",
235 BasePtr->addIncoming(Start,
Phi->getIncomingBlock(1 - IncrementingBlock));
236 BasePtr->addIncoming(Inc,
Phi->getIncomingBlock(IncrementingBlock));
239 MaybeDeadPHIs.push_back(Phi);
244 auto *BO = dyn_cast<BinaryOperator>(
Index);
248 switch (BO->getOpcode()) {
251 case Instruction::Or:
253 if (!cast<PossiblyDisjointInst>(BO)->isDisjoint())
256 case Instruction::Add:
258 case Instruction::Shl:
260 case Instruction::Mul:
266 if (isa<Instruction>(BO->getOperand(0)) &&
267 L->contains(cast<Instruction>(BO->getOperand(0)))) {
268 Index = cast<Instruction>(BO->getOperand(0));
269 OtherOp = BO->getOperand(1);
270 }
else if (isa<Instruction>(BO->getOperand(1)) &&
271 L->contains(cast<Instruction>(BO->getOperand(1))) &&
273 Index = cast<Instruction>(BO->getOperand(1));
274 OtherOp = BO->getOperand(0);
280 if (!
L->isLoopInvariant(OtherOp))
289 if (!matchStridedRecurrence(
Index, L, Stride, BasePtr, Inc, Builder))
294 unsigned StartBlock =
BasePtr->getOperand(0) == Inc ? 1 : 0;
300 BasePtr->getIncomingBlock(StartBlock)->getTerminator());
303 switch (BO->getOpcode()) {
306 case Instruction::Add:
307 case Instruction::Or: {
310 Start = Builder.
CreateAdd(Start, SplatOp,
"start");
313 case Instruction::Mul: {
314 Start = Builder.
CreateMul(Start, SplatOp,
"start");
315 Step = Builder.
CreateMul(Step, SplatOp,
"step");
316 Stride = Builder.
CreateMul(Stride, SplatOp,
"stride");
319 case Instruction::Shl: {
320 Start = Builder.
CreateShl(Start, SplatOp,
"start");
321 Step = Builder.
CreateShl(Step, SplatOp,
"step");
322 Stride = Builder.
CreateShl(Stride, SplatOp,
"stride");
328 BasePtr->setIncomingValue(StartBlock, Start);
332std::pair<Value *, Value *>
333RISCVGatherScatterLowering::determineBaseAndStride(
Instruction *
Ptr,
339 return std::make_pair(BasePtr, ConstantInt::get(IntPtrTy, 0));
342 auto *
GEP = dyn_cast<GetElementPtrInst>(
Ptr);
344 return std::make_pair(
nullptr,
nullptr);
346 auto I = StridedAddrs.find(
GEP);
347 if (
I != StridedAddrs.end())
354 if (
auto *BaseInst = dyn_cast<Instruction>(
Base);
355 BaseInst && BaseInst->getType()->isVectorTy()) {
357 auto IsScalar = [](
Value *
Idx) {
return !
Idx->getType()->isVectorTy(); };
359 auto [BaseBase, Stride] = determineBaseAndStride(BaseInst, Builder);
364 Builder.
CreateGEP(
GEP->getSourceElementType(), BaseBase, Indices,
365 GEP->getName() +
"offset",
GEP->isInBounds());
366 return {OffsetBase, Stride};
376 return std::make_pair(
nullptr,
nullptr);
379 std::optional<unsigned> VecOperand;
380 unsigned TypeScale = 0;
384 for (
unsigned i = 1, e =
GEP->getNumOperands(); i != e; ++i, ++GTI) {
385 if (!Ops[i]->
getType()->isVectorTy())
389 return std::make_pair(
nullptr,
nullptr);
395 return std::make_pair(
nullptr,
nullptr);
402 return std::make_pair(
nullptr,
nullptr);
409 Value *VecIndex = Ops[*VecOperand];
410 Type *VecIntPtrTy =
DL->getIntPtrType(
GEP->getType());
411 if (VecIndex->
getType() != VecIntPtrTy) {
412 auto *VecIndexC = dyn_cast<Constant>(VecIndex);
414 return std::make_pair(
nullptr,
nullptr);
429 Ops[*VecOperand] = Start;
430 Type *SourceTy =
GEP->getSourceElementType();
440 Stride = Builder.
CreateMul(Stride, ConstantInt::get(IntPtrTy, TypeScale));
442 auto P = std::make_pair(BasePtr, Stride);
443 StridedAddrs[
GEP] =
P;
448 Loop *
L = LI->getLoopFor(
GEP->getParent());
449 if (!L || !
L->getLoopPreheader() || !
L->getLoopLatch())
450 return std::make_pair(
nullptr,
nullptr);
454 if (!matchStridedRecurrence(VecIndex, L, Stride, BasePhi, Inc, Builder))
455 return std::make_pair(
nullptr,
nullptr);
458 unsigned IncrementingBlock = BasePhi->
getOperand(0) == Inc ? 0 : 1;
460 "Expected one operand of phi to be Inc");
465 Ops[*VecOperand] = BasePhi;
466 Type *SourceTy =
GEP->getSourceElementType();
480 Stride = Builder.
CreateMul(Stride, ConstantInt::get(IntPtrTy, TypeScale));
482 auto P = std::make_pair(BasePtr, Stride);
483 StridedAddrs[
GEP] =
P;
487bool RISCVGatherScatterLowering::tryCreateStridedLoadStore(
IntrinsicInst *
II,
492 MaybeAlign MA = cast<ConstantInt>(AlignOp)->getMaybeAlignValue();
493 EVT DataTypeVT = TLI->getValueType(*
DL, DataType);
494 if (!MA || !TLI->isLegalStridedLoadStore(DataTypeVT, *MA))
498 if (!TLI->isTypeLegal(DataTypeVT))
502 auto *PtrI = dyn_cast<Instruction>(
Ptr);
511 std::tie(BasePtr, Stride) = determineBaseAndStride(PtrI, Builder);
514 assert(Stride !=
nullptr);
522 if (
II->getIntrinsicID() == Intrinsic::masked_gather) {
524 Intrinsic::experimental_vp_strided_load,
526 {
BasePtr, Stride,
II->getArgOperand(2), EVL});
528 Intrinsic::vp_select, {DataType},
529 {
II->getOperand(2), Call,
II->getArgOperand(3), EVL});
532 Intrinsic::experimental_vp_strided_store,
533 {DataType,
BasePtr->getType(), Stride->getType()},
534 {
II->getArgOperand(0), BasePtr, Stride,
II->getArgOperand(3), EVL});
537 II->replaceAllUsesWith(Call);
538 II->eraseFromParent();
540 if (PtrI->use_empty())
546bool RISCVGatherScatterLowering::runOnFunction(
Function &
F) {
550 auto &TPC = getAnalysis<TargetPassConfig>();
553 if (!
ST->hasVInstructions() || !
ST->useRVVForFixedLengthVectors())
556 TLI =
ST->getTargetLowering();
557 DL = &
F.getDataLayout();
558 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
560 StridedAddrs.clear();
565 bool Changed =
false;
570 if (
II &&
II->getIntrinsicID() == Intrinsic::masked_gather) {
572 }
else if (
II &&
II->getIntrinsicID() == Intrinsic::masked_scatter) {
579 for (
auto *
II : Gathers)
580 Changed |= tryCreateStridedLoadStore(
581 II,
II->getType(),
II->getArgOperand(0),
II->getArgOperand(1));
582 for (
auto *
II : Scatters)
584 tryCreateStridedLoadStore(
II,
II->getArgOperand(0)->getType(),
585 II->getArgOperand(1),
II->getArgOperand(2));
588 while (!MaybeDeadPHIs.empty()) {
589 if (
auto *Phi = dyn_cast_or_null<PHINode>(MaybeDeadPHIs.pop_back_val()))
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static std::pair< Value *, Value * > matchStridedStart(Value *Start, IRBuilderBase &Builder)
static std::pair< Value *, Value * > matchStridedConstant(Constant *StartC)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static SymbolRef::Type getType(const Symbol *Sym)
Target-Independent Code Generator Pass Configuration Options pass.
Class for arbitrary precision integers.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
BinaryOps getOpcode() const
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
const APInt & getValue() const
Return the constant as an APInt value reference.
This is an important base class in LLVM.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
A parsed version of the target data layout string in and methods for querying it.
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Common base class shared among various IRBuilders.
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateElementCount(Type *DstType, ElementCount EC)
Create an expression which evaluates to the number of elements in EC at runtime.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Target-Independent Code Generator Pass Configuration Options.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isVectorTy() const
True if this is an instance of VectorType.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
StringRef getName() const
Return a constant reference to the value's name.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
TypeSize getSequentialElementStride(const DataLayout &DL) const
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
bool match(Val *V, const Pattern &P)
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
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.
Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
FunctionPass * createRISCVGatherScatterLoweringPass()
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
gep_type_iterator gep_type_begin(const User *GEP)
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.