xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVGatherScatterLowering.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1349cc55cSDimitry Andric //===- RISCVGatherScatterLowering.cpp - Gather/Scatter lowering -----------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric //
9349cc55cSDimitry Andric // This pass custom lowers llvm.gather and llvm.scatter instructions to
10*06c3fb27SDimitry Andric // RISC-V intrinsics.
11349cc55cSDimitry Andric //
12349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
13349cc55cSDimitry Andric 
14349cc55cSDimitry Andric #include "RISCV.h"
15349cc55cSDimitry Andric #include "RISCVTargetMachine.h"
16*06c3fb27SDimitry Andric #include "llvm/Analysis/InstSimplifyFolder.h"
17349cc55cSDimitry Andric #include "llvm/Analysis/LoopInfo.h"
18349cc55cSDimitry Andric #include "llvm/Analysis/ValueTracking.h"
19349cc55cSDimitry Andric #include "llvm/Analysis/VectorUtils.h"
20349cc55cSDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
21349cc55cSDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
22349cc55cSDimitry Andric #include "llvm/IR/IRBuilder.h"
23349cc55cSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
24349cc55cSDimitry Andric #include "llvm/IR/IntrinsicsRISCV.h"
25bdd1243dSDimitry Andric #include "llvm/IR/PatternMatch.h"
26349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Local.h"
27bdd1243dSDimitry Andric #include <optional>
28349cc55cSDimitry Andric 
29349cc55cSDimitry Andric using namespace llvm;
30bdd1243dSDimitry Andric using namespace PatternMatch;
31349cc55cSDimitry Andric 
32349cc55cSDimitry Andric #define DEBUG_TYPE "riscv-gather-scatter-lowering"
33349cc55cSDimitry Andric 
34349cc55cSDimitry Andric namespace {
35349cc55cSDimitry Andric 
36349cc55cSDimitry Andric class RISCVGatherScatterLowering : public FunctionPass {
37349cc55cSDimitry Andric   const RISCVSubtarget *ST = nullptr;
38349cc55cSDimitry Andric   const RISCVTargetLowering *TLI = nullptr;
39349cc55cSDimitry Andric   LoopInfo *LI = nullptr;
40349cc55cSDimitry Andric   const DataLayout *DL = nullptr;
41349cc55cSDimitry Andric 
42349cc55cSDimitry Andric   SmallVector<WeakTrackingVH> MaybeDeadPHIs;
43349cc55cSDimitry Andric 
4481ad6265SDimitry Andric   // Cache of the BasePtr and Stride determined from this GEP. When a GEP is
4581ad6265SDimitry Andric   // used by multiple gathers/scatters, this allow us to reuse the scalar
4681ad6265SDimitry Andric   // instructions we created for the first gather/scatter for the others.
4781ad6265SDimitry Andric   DenseMap<GetElementPtrInst *, std::pair<Value *, Value *>> StridedAddrs;
4881ad6265SDimitry Andric 
49349cc55cSDimitry Andric public:
50349cc55cSDimitry Andric   static char ID; // Pass identification, replacement for typeid
51349cc55cSDimitry Andric 
52349cc55cSDimitry Andric   RISCVGatherScatterLowering() : FunctionPass(ID) {}
53349cc55cSDimitry Andric 
54349cc55cSDimitry Andric   bool runOnFunction(Function &F) override;
55349cc55cSDimitry Andric 
56349cc55cSDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
57349cc55cSDimitry Andric     AU.setPreservesCFG();
58349cc55cSDimitry Andric     AU.addRequired<TargetPassConfig>();
59349cc55cSDimitry Andric     AU.addRequired<LoopInfoWrapperPass>();
60349cc55cSDimitry Andric   }
61349cc55cSDimitry Andric 
62349cc55cSDimitry Andric   StringRef getPassName() const override {
63*06c3fb27SDimitry Andric     return "RISC-V gather/scatter lowering";
64349cc55cSDimitry Andric   }
65349cc55cSDimitry Andric 
66349cc55cSDimitry Andric private:
67349cc55cSDimitry Andric   bool tryCreateStridedLoadStore(IntrinsicInst *II, Type *DataType, Value *Ptr,
68349cc55cSDimitry Andric                                  Value *AlignOp);
69349cc55cSDimitry Andric 
70349cc55cSDimitry Andric   std::pair<Value *, Value *> determineBaseAndStride(GetElementPtrInst *GEP,
71*06c3fb27SDimitry Andric                                                      IRBuilderBase &Builder);
72349cc55cSDimitry Andric 
73349cc55cSDimitry Andric   bool matchStridedRecurrence(Value *Index, Loop *L, Value *&Stride,
74349cc55cSDimitry Andric                               PHINode *&BasePtr, BinaryOperator *&Inc,
75*06c3fb27SDimitry Andric                               IRBuilderBase &Builder);
76349cc55cSDimitry Andric };
77349cc55cSDimitry Andric 
78349cc55cSDimitry Andric } // end anonymous namespace
79349cc55cSDimitry Andric 
80349cc55cSDimitry Andric char RISCVGatherScatterLowering::ID = 0;
81349cc55cSDimitry Andric 
82349cc55cSDimitry Andric INITIALIZE_PASS(RISCVGatherScatterLowering, DEBUG_TYPE,
83*06c3fb27SDimitry Andric                 "RISC-V gather/scatter lowering pass", false, false)
84349cc55cSDimitry Andric 
85349cc55cSDimitry Andric FunctionPass *llvm::createRISCVGatherScatterLoweringPass() {
86349cc55cSDimitry Andric   return new RISCVGatherScatterLowering();
87349cc55cSDimitry Andric }
88349cc55cSDimitry Andric 
89349cc55cSDimitry Andric // TODO: Should we consider the mask when looking for a stride?
90349cc55cSDimitry Andric static std::pair<Value *, Value *> matchStridedConstant(Constant *StartC) {
91*06c3fb27SDimitry Andric   if (!isa<FixedVectorType>(StartC->getType()))
92*06c3fb27SDimitry Andric     return std::make_pair(nullptr, nullptr);
93*06c3fb27SDimitry Andric 
94349cc55cSDimitry Andric   unsigned NumElts = cast<FixedVectorType>(StartC->getType())->getNumElements();
95349cc55cSDimitry Andric 
96349cc55cSDimitry Andric   // Check that the start value is a strided constant.
97349cc55cSDimitry Andric   auto *StartVal =
98349cc55cSDimitry Andric       dyn_cast_or_null<ConstantInt>(StartC->getAggregateElement((unsigned)0));
99349cc55cSDimitry Andric   if (!StartVal)
100349cc55cSDimitry Andric     return std::make_pair(nullptr, nullptr);
101349cc55cSDimitry Andric   APInt StrideVal(StartVal->getValue().getBitWidth(), 0);
102349cc55cSDimitry Andric   ConstantInt *Prev = StartVal;
103349cc55cSDimitry Andric   for (unsigned i = 1; i != NumElts; ++i) {
104349cc55cSDimitry Andric     auto *C = dyn_cast_or_null<ConstantInt>(StartC->getAggregateElement(i));
105349cc55cSDimitry Andric     if (!C)
106349cc55cSDimitry Andric       return std::make_pair(nullptr, nullptr);
107349cc55cSDimitry Andric 
108349cc55cSDimitry Andric     APInt LocalStride = C->getValue() - Prev->getValue();
109349cc55cSDimitry Andric     if (i == 1)
110349cc55cSDimitry Andric       StrideVal = LocalStride;
111349cc55cSDimitry Andric     else if (StrideVal != LocalStride)
112349cc55cSDimitry Andric       return std::make_pair(nullptr, nullptr);
113349cc55cSDimitry Andric 
114349cc55cSDimitry Andric     Prev = C;
115349cc55cSDimitry Andric   }
116349cc55cSDimitry Andric 
117349cc55cSDimitry Andric   Value *Stride = ConstantInt::get(StartVal->getType(), StrideVal);
118349cc55cSDimitry Andric 
119349cc55cSDimitry Andric   return std::make_pair(StartVal, Stride);
120349cc55cSDimitry Andric }
121349cc55cSDimitry Andric 
12204eeddc0SDimitry Andric static std::pair<Value *, Value *> matchStridedStart(Value *Start,
123*06c3fb27SDimitry Andric                                                      IRBuilderBase &Builder) {
12404eeddc0SDimitry Andric   // Base case, start is a strided constant.
12504eeddc0SDimitry Andric   auto *StartC = dyn_cast<Constant>(Start);
12604eeddc0SDimitry Andric   if (StartC)
12704eeddc0SDimitry Andric     return matchStridedConstant(StartC);
12804eeddc0SDimitry Andric 
129bdd1243dSDimitry Andric   // Base case, start is a stepvector
130bdd1243dSDimitry Andric   if (match(Start, m_Intrinsic<Intrinsic::experimental_stepvector>())) {
131bdd1243dSDimitry Andric     auto *Ty = Start->getType()->getScalarType();
132bdd1243dSDimitry Andric     return std::make_pair(ConstantInt::get(Ty, 0), ConstantInt::get(Ty, 1));
133bdd1243dSDimitry Andric   }
134bdd1243dSDimitry Andric 
135*06c3fb27SDimitry Andric   // Not a constant, maybe it's a strided constant with a splat added or
136*06c3fb27SDimitry Andric   // multipled.
13704eeddc0SDimitry Andric   auto *BO = dyn_cast<BinaryOperator>(Start);
138*06c3fb27SDimitry Andric   if (!BO || (BO->getOpcode() != Instruction::Add &&
139*06c3fb27SDimitry Andric               BO->getOpcode() != Instruction::Shl &&
140*06c3fb27SDimitry Andric               BO->getOpcode() != Instruction::Mul))
14104eeddc0SDimitry Andric     return std::make_pair(nullptr, nullptr);
14204eeddc0SDimitry Andric 
14304eeddc0SDimitry Andric   // Look for an operand that is splatted.
144*06c3fb27SDimitry Andric   unsigned OtherIndex = 0;
145*06c3fb27SDimitry Andric   Value *Splat = getSplatValue(BO->getOperand(1));
146*06c3fb27SDimitry Andric   if (!Splat && Instruction::isCommutative(BO->getOpcode())) {
147*06c3fb27SDimitry Andric     Splat = getSplatValue(BO->getOperand(0));
148*06c3fb27SDimitry Andric     OtherIndex = 1;
14904eeddc0SDimitry Andric   }
15004eeddc0SDimitry Andric   if (!Splat)
15104eeddc0SDimitry Andric     return std::make_pair(nullptr, nullptr);
15204eeddc0SDimitry Andric 
15304eeddc0SDimitry Andric   Value *Stride;
15404eeddc0SDimitry Andric   std::tie(Start, Stride) = matchStridedStart(BO->getOperand(OtherIndex),
15504eeddc0SDimitry Andric                                               Builder);
15604eeddc0SDimitry Andric   if (!Start)
15704eeddc0SDimitry Andric     return std::make_pair(nullptr, nullptr);
15804eeddc0SDimitry Andric 
15904eeddc0SDimitry Andric   Builder.SetInsertPoint(BO);
16004eeddc0SDimitry Andric   Builder.SetCurrentDebugLocation(DebugLoc());
161*06c3fb27SDimitry Andric   // Add the splat value to the start or multiply the start and stride by the
162*06c3fb27SDimitry Andric   // splat.
163*06c3fb27SDimitry Andric   switch (BO->getOpcode()) {
164*06c3fb27SDimitry Andric   default:
165*06c3fb27SDimitry Andric     llvm_unreachable("Unexpected opcode");
166*06c3fb27SDimitry Andric   case Instruction::Add:
16704eeddc0SDimitry Andric     Start = Builder.CreateAdd(Start, Splat);
168*06c3fb27SDimitry Andric     break;
169*06c3fb27SDimitry Andric   case Instruction::Mul:
170*06c3fb27SDimitry Andric     Start = Builder.CreateMul(Start, Splat);
171*06c3fb27SDimitry Andric     Stride = Builder.CreateMul(Stride, Splat);
172*06c3fb27SDimitry Andric     break;
173*06c3fb27SDimitry Andric   case Instruction::Shl:
174*06c3fb27SDimitry Andric     Start = Builder.CreateShl(Start, Splat);
175*06c3fb27SDimitry Andric     Stride = Builder.CreateShl(Stride, Splat);
176*06c3fb27SDimitry Andric     break;
177*06c3fb27SDimitry Andric   }
178*06c3fb27SDimitry Andric 
17904eeddc0SDimitry Andric   return std::make_pair(Start, Stride);
18004eeddc0SDimitry Andric }
18104eeddc0SDimitry Andric 
182349cc55cSDimitry Andric // Recursively, walk about the use-def chain until we find a Phi with a strided
183349cc55cSDimitry Andric // start value. Build and update a scalar recurrence as we unwind the recursion.
184349cc55cSDimitry Andric // We also update the Stride as we unwind. Our goal is to move all of the
185349cc55cSDimitry Andric // arithmetic out of the loop.
186349cc55cSDimitry Andric bool RISCVGatherScatterLowering::matchStridedRecurrence(Value *Index, Loop *L,
187349cc55cSDimitry Andric                                                         Value *&Stride,
188349cc55cSDimitry Andric                                                         PHINode *&BasePtr,
189349cc55cSDimitry Andric                                                         BinaryOperator *&Inc,
190*06c3fb27SDimitry Andric                                                         IRBuilderBase &Builder) {
191349cc55cSDimitry Andric   // Our base case is a Phi.
192349cc55cSDimitry Andric   if (auto *Phi = dyn_cast<PHINode>(Index)) {
193349cc55cSDimitry Andric     // A phi node we want to perform this function on should be from the
194349cc55cSDimitry Andric     // loop header.
195349cc55cSDimitry Andric     if (Phi->getParent() != L->getHeader())
196349cc55cSDimitry Andric       return false;
197349cc55cSDimitry Andric 
198349cc55cSDimitry Andric     Value *Step, *Start;
199349cc55cSDimitry Andric     if (!matchSimpleRecurrence(Phi, Inc, Start, Step) ||
200349cc55cSDimitry Andric         Inc->getOpcode() != Instruction::Add)
201349cc55cSDimitry Andric       return false;
202349cc55cSDimitry Andric     assert(Phi->getNumIncomingValues() == 2 && "Expected 2 operand phi.");
203349cc55cSDimitry Andric     unsigned IncrementingBlock = Phi->getIncomingValue(0) == Inc ? 0 : 1;
204349cc55cSDimitry Andric     assert(Phi->getIncomingValue(IncrementingBlock) == Inc &&
205349cc55cSDimitry Andric            "Expected one operand of phi to be Inc");
206349cc55cSDimitry Andric 
207349cc55cSDimitry Andric     // Only proceed if the step is loop invariant.
208349cc55cSDimitry Andric     if (!L->isLoopInvariant(Step))
209349cc55cSDimitry Andric       return false;
210349cc55cSDimitry Andric 
211349cc55cSDimitry Andric     // Step should be a splat.
212349cc55cSDimitry Andric     Step = getSplatValue(Step);
213349cc55cSDimitry Andric     if (!Step)
214349cc55cSDimitry Andric       return false;
215349cc55cSDimitry Andric 
21604eeddc0SDimitry Andric     std::tie(Start, Stride) = matchStridedStart(Start, Builder);
217349cc55cSDimitry Andric     if (!Start)
218349cc55cSDimitry Andric       return false;
219349cc55cSDimitry Andric     assert(Stride != nullptr);
220349cc55cSDimitry Andric 
221349cc55cSDimitry Andric     // Build scalar phi and increment.
222349cc55cSDimitry Andric     BasePtr =
223349cc55cSDimitry Andric         PHINode::Create(Start->getType(), 2, Phi->getName() + ".scalar", Phi);
224349cc55cSDimitry Andric     Inc = BinaryOperator::CreateAdd(BasePtr, Step, Inc->getName() + ".scalar",
225349cc55cSDimitry Andric                                     Inc);
226349cc55cSDimitry Andric     BasePtr->addIncoming(Start, Phi->getIncomingBlock(1 - IncrementingBlock));
227349cc55cSDimitry Andric     BasePtr->addIncoming(Inc, Phi->getIncomingBlock(IncrementingBlock));
228349cc55cSDimitry Andric 
229349cc55cSDimitry Andric     // Note that this Phi might be eligible for removal.
230349cc55cSDimitry Andric     MaybeDeadPHIs.push_back(Phi);
231349cc55cSDimitry Andric     return true;
232349cc55cSDimitry Andric   }
233349cc55cSDimitry Andric 
234349cc55cSDimitry Andric   // Otherwise look for binary operator.
235349cc55cSDimitry Andric   auto *BO = dyn_cast<BinaryOperator>(Index);
236349cc55cSDimitry Andric   if (!BO)
237349cc55cSDimitry Andric     return false;
238349cc55cSDimitry Andric 
239*06c3fb27SDimitry Andric   switch (BO->getOpcode()) {
240*06c3fb27SDimitry Andric   default:
241349cc55cSDimitry Andric     return false;
242*06c3fb27SDimitry Andric   case Instruction::Or:
243349cc55cSDimitry Andric     // We need to be able to treat Or as Add.
244*06c3fb27SDimitry Andric     if (!haveNoCommonBitsSet(BO->getOperand(0), BO->getOperand(1), *DL))
245349cc55cSDimitry Andric       return false;
246*06c3fb27SDimitry Andric     break;
247*06c3fb27SDimitry Andric   case Instruction::Add:
248*06c3fb27SDimitry Andric     break;
249*06c3fb27SDimitry Andric   case Instruction::Shl:
250*06c3fb27SDimitry Andric     break;
251*06c3fb27SDimitry Andric   case Instruction::Mul:
252*06c3fb27SDimitry Andric     break;
253*06c3fb27SDimitry Andric   }
254349cc55cSDimitry Andric 
255349cc55cSDimitry Andric   // We should have one operand in the loop and one splat.
256349cc55cSDimitry Andric   Value *OtherOp;
257349cc55cSDimitry Andric   if (isa<Instruction>(BO->getOperand(0)) &&
258349cc55cSDimitry Andric       L->contains(cast<Instruction>(BO->getOperand(0)))) {
259349cc55cSDimitry Andric     Index = cast<Instruction>(BO->getOperand(0));
260349cc55cSDimitry Andric     OtherOp = BO->getOperand(1);
261349cc55cSDimitry Andric   } else if (isa<Instruction>(BO->getOperand(1)) &&
262*06c3fb27SDimitry Andric              L->contains(cast<Instruction>(BO->getOperand(1))) &&
263*06c3fb27SDimitry Andric              Instruction::isCommutative(BO->getOpcode())) {
264349cc55cSDimitry Andric     Index = cast<Instruction>(BO->getOperand(1));
265349cc55cSDimitry Andric     OtherOp = BO->getOperand(0);
266349cc55cSDimitry Andric   } else {
267349cc55cSDimitry Andric     return false;
268349cc55cSDimitry Andric   }
269349cc55cSDimitry Andric 
270349cc55cSDimitry Andric   // Make sure other op is loop invariant.
271349cc55cSDimitry Andric   if (!L->isLoopInvariant(OtherOp))
272349cc55cSDimitry Andric     return false;
273349cc55cSDimitry Andric 
274349cc55cSDimitry Andric   // Make sure we have a splat.
275349cc55cSDimitry Andric   Value *SplatOp = getSplatValue(OtherOp);
276349cc55cSDimitry Andric   if (!SplatOp)
277349cc55cSDimitry Andric     return false;
278349cc55cSDimitry Andric 
279349cc55cSDimitry Andric   // Recurse up the use-def chain.
280349cc55cSDimitry Andric   if (!matchStridedRecurrence(Index, L, Stride, BasePtr, Inc, Builder))
281349cc55cSDimitry Andric     return false;
282349cc55cSDimitry Andric 
283349cc55cSDimitry Andric   // Locate the Step and Start values from the recurrence.
284349cc55cSDimitry Andric   unsigned StepIndex = Inc->getOperand(0) == BasePtr ? 1 : 0;
285349cc55cSDimitry Andric   unsigned StartBlock = BasePtr->getOperand(0) == Inc ? 1 : 0;
286349cc55cSDimitry Andric   Value *Step = Inc->getOperand(StepIndex);
287349cc55cSDimitry Andric   Value *Start = BasePtr->getOperand(StartBlock);
288349cc55cSDimitry Andric 
289349cc55cSDimitry Andric   // We need to adjust the start value in the preheader.
290349cc55cSDimitry Andric   Builder.SetInsertPoint(
291349cc55cSDimitry Andric       BasePtr->getIncomingBlock(StartBlock)->getTerminator());
292349cc55cSDimitry Andric   Builder.SetCurrentDebugLocation(DebugLoc());
293349cc55cSDimitry Andric 
294349cc55cSDimitry Andric   switch (BO->getOpcode()) {
295349cc55cSDimitry Andric   default:
296349cc55cSDimitry Andric     llvm_unreachable("Unexpected opcode!");
297349cc55cSDimitry Andric   case Instruction::Add:
298349cc55cSDimitry Andric   case Instruction::Or: {
299349cc55cSDimitry Andric     // An add only affects the start value. It's ok to do this for Or because
300349cc55cSDimitry Andric     // we already checked that there are no common set bits.
301349cc55cSDimitry Andric     Start = Builder.CreateAdd(Start, SplatOp, "start");
302349cc55cSDimitry Andric     break;
303349cc55cSDimitry Andric   }
304349cc55cSDimitry Andric   case Instruction::Mul: {
305349cc55cSDimitry Andric     Start = Builder.CreateMul(Start, SplatOp, "start");
306349cc55cSDimitry Andric     Step = Builder.CreateMul(Step, SplatOp, "step");
307349cc55cSDimitry Andric     Stride = Builder.CreateMul(Stride, SplatOp, "stride");
308349cc55cSDimitry Andric     break;
309349cc55cSDimitry Andric   }
310349cc55cSDimitry Andric   case Instruction::Shl: {
311349cc55cSDimitry Andric     Start = Builder.CreateShl(Start, SplatOp, "start");
312349cc55cSDimitry Andric     Step = Builder.CreateShl(Step, SplatOp, "step");
313349cc55cSDimitry Andric     Stride = Builder.CreateShl(Stride, SplatOp, "stride");
314349cc55cSDimitry Andric     break;
315349cc55cSDimitry Andric   }
316349cc55cSDimitry Andric   }
317349cc55cSDimitry Andric 
318*06c3fb27SDimitry Andric   Inc->setOperand(StepIndex, Step);
319*06c3fb27SDimitry Andric   BasePtr->setIncomingValue(StartBlock, Start);
320349cc55cSDimitry Andric   return true;
321349cc55cSDimitry Andric }
322349cc55cSDimitry Andric 
323349cc55cSDimitry Andric std::pair<Value *, Value *>
324349cc55cSDimitry Andric RISCVGatherScatterLowering::determineBaseAndStride(GetElementPtrInst *GEP,
325*06c3fb27SDimitry Andric                                                    IRBuilderBase &Builder) {
326349cc55cSDimitry Andric 
32781ad6265SDimitry Andric   auto I = StridedAddrs.find(GEP);
32881ad6265SDimitry Andric   if (I != StridedAddrs.end())
32981ad6265SDimitry Andric     return I->second;
33081ad6265SDimitry Andric 
331349cc55cSDimitry Andric   SmallVector<Value *, 2> Ops(GEP->operands());
332349cc55cSDimitry Andric 
333349cc55cSDimitry Andric   // Base pointer needs to be a scalar.
334349cc55cSDimitry Andric   if (Ops[0]->getType()->isVectorTy())
335349cc55cSDimitry Andric     return std::make_pair(nullptr, nullptr);
336349cc55cSDimitry Andric 
337bdd1243dSDimitry Andric   std::optional<unsigned> VecOperand;
338349cc55cSDimitry Andric   unsigned TypeScale = 0;
339349cc55cSDimitry Andric 
340349cc55cSDimitry Andric   // Look for a vector operand and scale.
341349cc55cSDimitry Andric   gep_type_iterator GTI = gep_type_begin(GEP);
342349cc55cSDimitry Andric   for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
343349cc55cSDimitry Andric     if (!Ops[i]->getType()->isVectorTy())
344349cc55cSDimitry Andric       continue;
345349cc55cSDimitry Andric 
346349cc55cSDimitry Andric     if (VecOperand)
347349cc55cSDimitry Andric       return std::make_pair(nullptr, nullptr);
348349cc55cSDimitry Andric 
349349cc55cSDimitry Andric     VecOperand = i;
350349cc55cSDimitry Andric 
351349cc55cSDimitry Andric     TypeSize TS = DL->getTypeAllocSize(GTI.getIndexedType());
352349cc55cSDimitry Andric     if (TS.isScalable())
353349cc55cSDimitry Andric       return std::make_pair(nullptr, nullptr);
354349cc55cSDimitry Andric 
355bdd1243dSDimitry Andric     TypeScale = TS.getFixedValue();
356349cc55cSDimitry Andric   }
357349cc55cSDimitry Andric 
358349cc55cSDimitry Andric   // We need to find a vector index to simplify.
359349cc55cSDimitry Andric   if (!VecOperand)
360349cc55cSDimitry Andric     return std::make_pair(nullptr, nullptr);
361349cc55cSDimitry Andric 
362349cc55cSDimitry Andric   // We can't extract the stride if the arithmetic is done at a different size
363349cc55cSDimitry Andric   // than the pointer type. Adding the stride later may not wrap correctly.
364349cc55cSDimitry Andric   // Technically we could handle wider indices, but I don't expect that in
365349cc55cSDimitry Andric   // practice.
366349cc55cSDimitry Andric   Value *VecIndex = Ops[*VecOperand];
367349cc55cSDimitry Andric   Type *VecIntPtrTy = DL->getIntPtrType(GEP->getType());
368349cc55cSDimitry Andric   if (VecIndex->getType() != VecIntPtrTy)
369349cc55cSDimitry Andric     return std::make_pair(nullptr, nullptr);
370349cc55cSDimitry Andric 
371bdd1243dSDimitry Andric   // Handle the non-recursive case.  This is what we see if the vectorizer
372bdd1243dSDimitry Andric   // decides to use a scalar IV + vid on demand instead of a vector IV.
373bdd1243dSDimitry Andric   auto [Start, Stride] = matchStridedStart(VecIndex, Builder);
374bdd1243dSDimitry Andric   if (Start) {
375bdd1243dSDimitry Andric     assert(Stride);
376bdd1243dSDimitry Andric     Builder.SetInsertPoint(GEP);
377bdd1243dSDimitry Andric 
378bdd1243dSDimitry Andric     // Replace the vector index with the scalar start and build a scalar GEP.
379bdd1243dSDimitry Andric     Ops[*VecOperand] = Start;
380bdd1243dSDimitry Andric     Type *SourceTy = GEP->getSourceElementType();
381bdd1243dSDimitry Andric     Value *BasePtr =
382bdd1243dSDimitry Andric         Builder.CreateGEP(SourceTy, Ops[0], ArrayRef(Ops).drop_front());
383bdd1243dSDimitry Andric 
384bdd1243dSDimitry Andric     // Convert stride to pointer size if needed.
385bdd1243dSDimitry Andric     Type *IntPtrTy = DL->getIntPtrType(BasePtr->getType());
386bdd1243dSDimitry Andric     assert(Stride->getType() == IntPtrTy && "Unexpected type");
387bdd1243dSDimitry Andric 
388bdd1243dSDimitry Andric     // Scale the stride by the size of the indexed type.
389bdd1243dSDimitry Andric     if (TypeScale != 1)
390bdd1243dSDimitry Andric       Stride = Builder.CreateMul(Stride, ConstantInt::get(IntPtrTy, TypeScale));
391bdd1243dSDimitry Andric 
392bdd1243dSDimitry Andric     auto P = std::make_pair(BasePtr, Stride);
393bdd1243dSDimitry Andric     StridedAddrs[GEP] = P;
394bdd1243dSDimitry Andric     return P;
395bdd1243dSDimitry Andric   }
396bdd1243dSDimitry Andric 
397bdd1243dSDimitry Andric   // Make sure we're in a loop and that has a pre-header and a single latch.
398bdd1243dSDimitry Andric   Loop *L = LI->getLoopFor(GEP->getParent());
399bdd1243dSDimitry Andric   if (!L || !L->getLoopPreheader() || !L->getLoopLatch())
400bdd1243dSDimitry Andric     return std::make_pair(nullptr, nullptr);
401bdd1243dSDimitry Andric 
402349cc55cSDimitry Andric   BinaryOperator *Inc;
403349cc55cSDimitry Andric   PHINode *BasePhi;
404349cc55cSDimitry Andric   if (!matchStridedRecurrence(VecIndex, L, Stride, BasePhi, Inc, Builder))
405349cc55cSDimitry Andric     return std::make_pair(nullptr, nullptr);
406349cc55cSDimitry Andric 
407349cc55cSDimitry Andric   assert(BasePhi->getNumIncomingValues() == 2 && "Expected 2 operand phi.");
408349cc55cSDimitry Andric   unsigned IncrementingBlock = BasePhi->getOperand(0) == Inc ? 0 : 1;
409349cc55cSDimitry Andric   assert(BasePhi->getIncomingValue(IncrementingBlock) == Inc &&
410349cc55cSDimitry Andric          "Expected one operand of phi to be Inc");
411349cc55cSDimitry Andric 
412349cc55cSDimitry Andric   Builder.SetInsertPoint(GEP);
413349cc55cSDimitry Andric 
414349cc55cSDimitry Andric   // Replace the vector index with the scalar phi and build a scalar GEP.
415349cc55cSDimitry Andric   Ops[*VecOperand] = BasePhi;
416349cc55cSDimitry Andric   Type *SourceTy = GEP->getSourceElementType();
417349cc55cSDimitry Andric   Value *BasePtr =
418bdd1243dSDimitry Andric       Builder.CreateGEP(SourceTy, Ops[0], ArrayRef(Ops).drop_front());
419349cc55cSDimitry Andric 
420349cc55cSDimitry Andric   // Final adjustments to stride should go in the start block.
421349cc55cSDimitry Andric   Builder.SetInsertPoint(
422349cc55cSDimitry Andric       BasePhi->getIncomingBlock(1 - IncrementingBlock)->getTerminator());
423349cc55cSDimitry Andric 
424349cc55cSDimitry Andric   // Convert stride to pointer size if needed.
425349cc55cSDimitry Andric   Type *IntPtrTy = DL->getIntPtrType(BasePtr->getType());
426349cc55cSDimitry Andric   assert(Stride->getType() == IntPtrTy && "Unexpected type");
427349cc55cSDimitry Andric 
428349cc55cSDimitry Andric   // Scale the stride by the size of the indexed type.
429349cc55cSDimitry Andric   if (TypeScale != 1)
430349cc55cSDimitry Andric     Stride = Builder.CreateMul(Stride, ConstantInt::get(IntPtrTy, TypeScale));
431349cc55cSDimitry Andric 
43281ad6265SDimitry Andric   auto P = std::make_pair(BasePtr, Stride);
43381ad6265SDimitry Andric   StridedAddrs[GEP] = P;
43481ad6265SDimitry Andric   return P;
435349cc55cSDimitry Andric }
436349cc55cSDimitry Andric 
437349cc55cSDimitry Andric bool RISCVGatherScatterLowering::tryCreateStridedLoadStore(IntrinsicInst *II,
438349cc55cSDimitry Andric                                                            Type *DataType,
439349cc55cSDimitry Andric                                                            Value *Ptr,
440349cc55cSDimitry Andric                                                            Value *AlignOp) {
441349cc55cSDimitry Andric   // Make sure the operation will be supported by the backend.
442*06c3fb27SDimitry Andric   MaybeAlign MA = cast<ConstantInt>(AlignOp)->getMaybeAlignValue();
443*06c3fb27SDimitry Andric   EVT DataTypeVT = TLI->getValueType(*DL, DataType);
444*06c3fb27SDimitry Andric   if (!MA || !TLI->isLegalStridedLoadStore(DataTypeVT, *MA))
445*06c3fb27SDimitry Andric     return false;
446*06c3fb27SDimitry Andric 
447*06c3fb27SDimitry Andric   // FIXME: Let the backend type legalize by splitting/widening?
448*06c3fb27SDimitry Andric   if (!TLI->isTypeLegal(DataTypeVT))
449349cc55cSDimitry Andric     return false;
450349cc55cSDimitry Andric 
451349cc55cSDimitry Andric   // Pointer should be a GEP.
452349cc55cSDimitry Andric   auto *GEP = dyn_cast<GetElementPtrInst>(Ptr);
453349cc55cSDimitry Andric   if (!GEP)
454349cc55cSDimitry Andric     return false;
455349cc55cSDimitry Andric 
456*06c3fb27SDimitry Andric   LLVMContext &Ctx = GEP->getContext();
457*06c3fb27SDimitry Andric   IRBuilder<InstSimplifyFolder> Builder(Ctx, *DL);
458*06c3fb27SDimitry Andric   Builder.SetInsertPoint(GEP);
459349cc55cSDimitry Andric 
460349cc55cSDimitry Andric   Value *BasePtr, *Stride;
461349cc55cSDimitry Andric   std::tie(BasePtr, Stride) = determineBaseAndStride(GEP, Builder);
462349cc55cSDimitry Andric   if (!BasePtr)
463349cc55cSDimitry Andric     return false;
464349cc55cSDimitry Andric   assert(Stride != nullptr);
465349cc55cSDimitry Andric 
466349cc55cSDimitry Andric   Builder.SetInsertPoint(II);
467349cc55cSDimitry Andric 
468349cc55cSDimitry Andric   CallInst *Call;
469349cc55cSDimitry Andric   if (II->getIntrinsicID() == Intrinsic::masked_gather)
470349cc55cSDimitry Andric     Call = Builder.CreateIntrinsic(
471349cc55cSDimitry Andric         Intrinsic::riscv_masked_strided_load,
472349cc55cSDimitry Andric         {DataType, BasePtr->getType(), Stride->getType()},
473349cc55cSDimitry Andric         {II->getArgOperand(3), BasePtr, Stride, II->getArgOperand(2)});
474349cc55cSDimitry Andric   else
475349cc55cSDimitry Andric     Call = Builder.CreateIntrinsic(
476349cc55cSDimitry Andric         Intrinsic::riscv_masked_strided_store,
477349cc55cSDimitry Andric         {DataType, BasePtr->getType(), Stride->getType()},
478349cc55cSDimitry Andric         {II->getArgOperand(0), BasePtr, Stride, II->getArgOperand(3)});
479349cc55cSDimitry Andric 
480349cc55cSDimitry Andric   Call->takeName(II);
481349cc55cSDimitry Andric   II->replaceAllUsesWith(Call);
482349cc55cSDimitry Andric   II->eraseFromParent();
483349cc55cSDimitry Andric 
484349cc55cSDimitry Andric   if (GEP->use_empty())
485349cc55cSDimitry Andric     RecursivelyDeleteTriviallyDeadInstructions(GEP);
486349cc55cSDimitry Andric 
487349cc55cSDimitry Andric   return true;
488349cc55cSDimitry Andric }
489349cc55cSDimitry Andric 
490349cc55cSDimitry Andric bool RISCVGatherScatterLowering::runOnFunction(Function &F) {
491349cc55cSDimitry Andric   if (skipFunction(F))
492349cc55cSDimitry Andric     return false;
493349cc55cSDimitry Andric 
494349cc55cSDimitry Andric   auto &TPC = getAnalysis<TargetPassConfig>();
495349cc55cSDimitry Andric   auto &TM = TPC.getTM<RISCVTargetMachine>();
496349cc55cSDimitry Andric   ST = &TM.getSubtarget<RISCVSubtarget>(F);
497349cc55cSDimitry Andric   if (!ST->hasVInstructions() || !ST->useRVVForFixedLengthVectors())
498349cc55cSDimitry Andric     return false;
499349cc55cSDimitry Andric 
500349cc55cSDimitry Andric   TLI = ST->getTargetLowering();
501349cc55cSDimitry Andric   DL = &F.getParent()->getDataLayout();
502349cc55cSDimitry Andric   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
503349cc55cSDimitry Andric 
50481ad6265SDimitry Andric   StridedAddrs.clear();
50581ad6265SDimitry Andric 
506349cc55cSDimitry Andric   SmallVector<IntrinsicInst *, 4> Gathers;
507349cc55cSDimitry Andric   SmallVector<IntrinsicInst *, 4> Scatters;
508349cc55cSDimitry Andric 
509349cc55cSDimitry Andric   bool Changed = false;
510349cc55cSDimitry Andric 
511349cc55cSDimitry Andric   for (BasicBlock &BB : F) {
512349cc55cSDimitry Andric     for (Instruction &I : BB) {
513349cc55cSDimitry Andric       IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
514bdd1243dSDimitry Andric       if (II && II->getIntrinsicID() == Intrinsic::masked_gather) {
515349cc55cSDimitry Andric         Gathers.push_back(II);
516bdd1243dSDimitry Andric       } else if (II && II->getIntrinsicID() == Intrinsic::masked_scatter) {
517349cc55cSDimitry Andric         Scatters.push_back(II);
518349cc55cSDimitry Andric       }
519349cc55cSDimitry Andric     }
520349cc55cSDimitry Andric   }
521349cc55cSDimitry Andric 
522349cc55cSDimitry Andric   // Rewrite gather/scatter to form strided load/store if possible.
523349cc55cSDimitry Andric   for (auto *II : Gathers)
524349cc55cSDimitry Andric     Changed |= tryCreateStridedLoadStore(
525349cc55cSDimitry Andric         II, II->getType(), II->getArgOperand(0), II->getArgOperand(1));
526349cc55cSDimitry Andric   for (auto *II : Scatters)
527349cc55cSDimitry Andric     Changed |=
528349cc55cSDimitry Andric         tryCreateStridedLoadStore(II, II->getArgOperand(0)->getType(),
529349cc55cSDimitry Andric                                   II->getArgOperand(1), II->getArgOperand(2));
530349cc55cSDimitry Andric 
531349cc55cSDimitry Andric   // Remove any dead phis.
532349cc55cSDimitry Andric   while (!MaybeDeadPHIs.empty()) {
533349cc55cSDimitry Andric     if (auto *Phi = dyn_cast_or_null<PHINode>(MaybeDeadPHIs.pop_back_val()))
534349cc55cSDimitry Andric       RecursivelyDeleteDeadPHINode(Phi);
535349cc55cSDimitry Andric   }
536349cc55cSDimitry Andric 
537349cc55cSDimitry Andric   return Changed;
538349cc55cSDimitry Andric }
539