xref: /llvm-project/llvm/lib/Target/PowerPC/PPCLoopInstrFormPrep.cpp (revision ffa9fa9ed232f35c0b0dcd4ab48fc565cf8a2f42)
1 //===------ PPCLoopInstrFormPrep.cpp - Loop Instr Form Prep Pass ----------===//
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 file implements a pass to prepare loops for ppc preferred addressing
10 // modes, leveraging different instruction form. (eg: DS/DQ form, D/DS form with
11 // update)
12 // Additional PHIs are created for loop induction variables used by load/store
13 // instructions so that preferred addressing modes can be used.
14 //
15 // 1: DS/DQ form preparation, prepare the load/store instructions so that they
16 //    can satisfy the DS/DQ form displacement requirements.
17 //    Generically, this means transforming loops like this:
18 //    for (int i = 0; i < n; ++i) {
19 //      unsigned long x1 = *(unsigned long *)(p + i + 5);
20 //      unsigned long x2 = *(unsigned long *)(p + i + 9);
21 //    }
22 //
23 //    to look like this:
24 //
25 //    unsigned NewP = p + 5;
26 //    for (int i = 0; i < n; ++i) {
27 //      unsigned long x1 = *(unsigned long *)(i + NewP);
28 //      unsigned long x2 = *(unsigned long *)(i + NewP + 4);
29 //    }
30 //
31 // 2: D/DS form with update preparation, prepare the load/store instructions so
32 //    that we can use update form to do pre-increment.
33 //    Generically, this means transforming loops like this:
34 //    for (int i = 0; i < n; ++i)
35 //      array[i] = c;
36 //
37 //    to look like this:
38 //
39 //    T *p = array[-1];
40 //    for (int i = 0; i < n; ++i)
41 //      *++p = c;
42 //===----------------------------------------------------------------------===//
43 
44 #include "PPC.h"
45 #include "PPCSubtarget.h"
46 #include "PPCTargetMachine.h"
47 #include "llvm/ADT/DepthFirstIterator.h"
48 #include "llvm/ADT/SmallPtrSet.h"
49 #include "llvm/ADT/SmallSet.h"
50 #include "llvm/ADT/SmallVector.h"
51 #include "llvm/ADT/Statistic.h"
52 #include "llvm/Analysis/LoopInfo.h"
53 #include "llvm/Analysis/ScalarEvolution.h"
54 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
55 #include "llvm/IR/BasicBlock.h"
56 #include "llvm/IR/CFG.h"
57 #include "llvm/IR/Dominators.h"
58 #include "llvm/IR/Instruction.h"
59 #include "llvm/IR/Instructions.h"
60 #include "llvm/IR/IntrinsicInst.h"
61 #include "llvm/IR/IntrinsicsPowerPC.h"
62 #include "llvm/IR/Module.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/Value.h"
65 #include "llvm/InitializePasses.h"
66 #include "llvm/Pass.h"
67 #include "llvm/Support/Casting.h"
68 #include "llvm/Support/CommandLine.h"
69 #include "llvm/Support/Debug.h"
70 #include "llvm/Transforms/Scalar.h"
71 #include "llvm/Transforms/Utils.h"
72 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
73 #include "llvm/Transforms/Utils/Local.h"
74 #include "llvm/Transforms/Utils/LoopUtils.h"
75 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
76 #include <cassert>
77 #include <iterator>
78 #include <utility>
79 
80 #define DEBUG_TYPE "ppc-loop-instr-form-prep"
81 
82 using namespace llvm;
83 
84 static cl::opt<unsigned> MaxVarsPrep("ppc-formprep-max-vars",
85                                  cl::Hidden, cl::init(24),
86   cl::desc("Potential common base number threshold per function for PPC loop "
87            "prep"));
88 
89 static cl::opt<bool> PreferUpdateForm("ppc-formprep-prefer-update",
90                                  cl::init(true), cl::Hidden,
91   cl::desc("prefer update form when ds form is also a update form"));
92 
93 // Sum of following 3 per loop thresholds for all loops can not be larger
94 // than MaxVarsPrep.
95 // now the thresholds for each kind prep are exterimental values on Power9.
96 static cl::opt<unsigned> MaxVarsUpdateForm("ppc-preinc-prep-max-vars",
97                                  cl::Hidden, cl::init(3),
98   cl::desc("Potential PHI threshold per loop for PPC loop prep of update "
99            "form"));
100 
101 static cl::opt<unsigned> MaxVarsDSForm("ppc-dsprep-max-vars",
102                                  cl::Hidden, cl::init(3),
103   cl::desc("Potential PHI threshold per loop for PPC loop prep of DS form"));
104 
105 static cl::opt<unsigned> MaxVarsDQForm("ppc-dqprep-max-vars",
106                                  cl::Hidden, cl::init(8),
107   cl::desc("Potential PHI threshold per loop for PPC loop prep of DQ form"));
108 
109 
110 // If would not be profitable if the common base has only one load/store, ISEL
111 // should already be able to choose best load/store form based on offset for
112 // single load/store. Set minimal profitable value default to 2 and make it as
113 // an option.
114 static cl::opt<unsigned> DispFormPrepMinThreshold("ppc-dispprep-min-threshold",
115                                     cl::Hidden, cl::init(2),
116   cl::desc("Minimal common base load/store instructions triggering DS/DQ form "
117            "preparation"));
118 
119 STATISTIC(PHINodeAlreadyExistsUpdate, "PHI node already in pre-increment form");
120 STATISTIC(PHINodeAlreadyExistsDS, "PHI node already in DS form");
121 STATISTIC(PHINodeAlreadyExistsDQ, "PHI node already in DQ form");
122 STATISTIC(DSFormChainRewritten, "Num of DS form chain rewritten");
123 STATISTIC(DQFormChainRewritten, "Num of DQ form chain rewritten");
124 STATISTIC(UpdFormChainRewritten, "Num of update form chain rewritten");
125 
126 namespace {
127   struct BucketElement {
128     BucketElement(const SCEVConstant *O, Instruction *I) : Offset(O), Instr(I) {}
129     BucketElement(Instruction *I) : Offset(nullptr), Instr(I) {}
130 
131     const SCEVConstant *Offset;
132     Instruction *Instr;
133   };
134 
135   struct Bucket {
136     Bucket(const SCEV *B, Instruction *I) : BaseSCEV(B),
137                                             Elements(1, BucketElement(I)) {}
138 
139     const SCEV *BaseSCEV;
140     SmallVector<BucketElement, 16> Elements;
141   };
142 
143   // "UpdateForm" is not a real PPC instruction form, it stands for dform
144   // load/store with update like ldu/stdu, or Prefetch intrinsic.
145   // For DS form instructions, their displacements must be multiple of 4.
146   // For DQ form instructions, their displacements must be multiple of 16.
147   enum InstrForm { UpdateForm = 1, DSForm = 4, DQForm = 16 };
148 
149   class PPCLoopInstrFormPrep : public FunctionPass {
150   public:
151     static char ID; // Pass ID, replacement for typeid
152 
153     PPCLoopInstrFormPrep() : FunctionPass(ID) {
154       initializePPCLoopInstrFormPrepPass(*PassRegistry::getPassRegistry());
155     }
156 
157     PPCLoopInstrFormPrep(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) {
158       initializePPCLoopInstrFormPrepPass(*PassRegistry::getPassRegistry());
159     }
160 
161     void getAnalysisUsage(AnalysisUsage &AU) const override {
162       AU.addPreserved<DominatorTreeWrapperPass>();
163       AU.addRequired<LoopInfoWrapperPass>();
164       AU.addPreserved<LoopInfoWrapperPass>();
165       AU.addRequired<ScalarEvolutionWrapperPass>();
166     }
167 
168     bool runOnFunction(Function &F) override;
169 
170   private:
171     PPCTargetMachine *TM = nullptr;
172     const PPCSubtarget *ST;
173     DominatorTree *DT;
174     LoopInfo *LI;
175     ScalarEvolution *SE;
176     bool PreserveLCSSA;
177 
178     /// Successful preparation number for Update/DS/DQ form in all inner most
179     /// loops. One successful preparation will put one common base out of loop,
180     /// this may leads to register presure like LICM does.
181     /// Make sure total preparation number can be controlled by option.
182     unsigned SuccPrepCount;
183 
184     bool runOnLoop(Loop *L);
185 
186     /// Check if required PHI node is already exist in Loop \p L.
187     bool alreadyPrepared(Loop *L, Instruction *MemI,
188                          const SCEV *BasePtrStartSCEV,
189                          const SCEV *BasePtrIncSCEV, InstrForm Form);
190 
191     /// Get the value which defines the increment SCEV \p BasePtrIncSCEV.
192     Value *getNodeForInc(Loop *L, Instruction *MemI,
193                          const SCEV *BasePtrIncSCEV);
194 
195     /// Collect condition matched(\p isValidCandidate() returns true)
196     /// candidates in Loop \p L.
197     SmallVector<Bucket, 16> collectCandidates(
198         Loop *L,
199         std::function<bool(const Instruction *, const Value *, const Type *)>
200             isValidCandidate,
201         unsigned MaxCandidateNum);
202 
203     /// Add a candidate to candidates \p Buckets.
204     void addOneCandidate(Instruction *MemI, const SCEV *LSCEV,
205                          SmallVector<Bucket, 16> &Buckets,
206                          unsigned MaxCandidateNum);
207 
208     /// Prepare all candidates in \p Buckets for update form.
209     bool updateFormPrep(Loop *L, SmallVector<Bucket, 16> &Buckets);
210 
211     /// Prepare all candidates in \p Buckets for displacement form, now for
212     /// ds/dq.
213     bool dispFormPrep(Loop *L, SmallVector<Bucket, 16> &Buckets,
214                       InstrForm Form);
215 
216     /// Prepare for one chain \p BucketChain, find the best base element and
217     /// update all other elements in \p BucketChain accordingly.
218     /// \p Form is used to find the best base element.
219     /// If success, best base element must be stored as the first element of
220     /// \p BucketChain.
221     /// Return false if no base element found, otherwise return true.
222     bool prepareBaseForDispFormChain(Bucket &BucketChain,
223                                      InstrForm Form);
224 
225     /// Prepare for one chain \p BucketChain, find the best base element and
226     /// update all other elements in \p BucketChain accordingly.
227     /// If success, best base element must be stored as the first element of
228     /// \p BucketChain.
229     /// Return false if no base element found, otherwise return true.
230     bool prepareBaseForUpdateFormChain(Bucket &BucketChain);
231 
232     /// Rewrite load/store instructions in \p BucketChain according to
233     /// preparation.
234     bool rewriteLoadStores(Loop *L, Bucket &BucketChain,
235                            SmallSet<BasicBlock *, 16> &BBChanged,
236                            InstrForm Form);
237   };
238 
239 } // end anonymous namespace
240 
241 char PPCLoopInstrFormPrep::ID = 0;
242 static const char *name = "Prepare loop for ppc preferred instruction forms";
243 INITIALIZE_PASS_BEGIN(PPCLoopInstrFormPrep, DEBUG_TYPE, name, false, false)
244 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
245 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
246 INITIALIZE_PASS_END(PPCLoopInstrFormPrep, DEBUG_TYPE, name, false, false)
247 
248 static constexpr StringRef PHINodeNameSuffix    = ".phi";
249 static constexpr StringRef CastNodeNameSuffix   = ".cast";
250 static constexpr StringRef GEPNodeIncNameSuffix = ".inc";
251 static constexpr StringRef GEPNodeOffNameSuffix = ".off";
252 
253 FunctionPass *llvm::createPPCLoopInstrFormPrepPass(PPCTargetMachine &TM) {
254   return new PPCLoopInstrFormPrep(TM);
255 }
256 
257 static bool IsPtrInBounds(Value *BasePtr) {
258   Value *StrippedBasePtr = BasePtr;
259   while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr))
260     StrippedBasePtr = BC->getOperand(0);
261   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(StrippedBasePtr))
262     return GEP->isInBounds();
263 
264   return false;
265 }
266 
267 static std::string getInstrName(const Value *I, StringRef Suffix) {
268   assert(I && "Invalid paramater!");
269   if (I->hasName())
270     return (I->getName() + Suffix).str();
271   else
272     return "";
273 }
274 
275 static Value *getPointerOperandAndType(Value *MemI,
276                                        Type **PtrElementType = nullptr) {
277 
278   Value *PtrValue = nullptr;
279   Type *PointerElementType = nullptr;
280 
281   if (LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) {
282     PtrValue = LMemI->getPointerOperand();
283     PointerElementType = LMemI->getType();
284   } else if (StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) {
285     PtrValue = SMemI->getPointerOperand();
286     PointerElementType = SMemI->getValueOperand()->getType();
287   } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(MemI)) {
288     PointerElementType = Type::getInt8Ty(MemI->getContext());
289     if (IMemI->getIntrinsicID() == Intrinsic::prefetch ||
290         IMemI->getIntrinsicID() == Intrinsic::ppc_vsx_lxvp) {
291       PtrValue = IMemI->getArgOperand(0);
292     } else if (IMemI->getIntrinsicID() == Intrinsic::ppc_vsx_stxvp) {
293       PtrValue = IMemI->getArgOperand(1);
294     }
295   }
296   /*Get ElementType if PtrElementType is not null.*/
297   if (PtrElementType)
298     *PtrElementType = PointerElementType;
299 
300   return PtrValue;
301 }
302 
303 bool PPCLoopInstrFormPrep::runOnFunction(Function &F) {
304   if (skipFunction(F))
305     return false;
306 
307   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
308   SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
309   auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
310   DT = DTWP ? &DTWP->getDomTree() : nullptr;
311   PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
312   ST = TM ? TM->getSubtargetImpl(F) : nullptr;
313   SuccPrepCount = 0;
314 
315   bool MadeChange = false;
316 
317   for (auto I = LI->begin(), IE = LI->end(); I != IE; ++I)
318     for (auto L = df_begin(*I), LE = df_end(*I); L != LE; ++L)
319       MadeChange |= runOnLoop(*L);
320 
321   return MadeChange;
322 }
323 
324 void PPCLoopInstrFormPrep::addOneCandidate(Instruction *MemI, const SCEV *LSCEV,
325                                         SmallVector<Bucket, 16> &Buckets,
326                                         unsigned MaxCandidateNum) {
327   assert((MemI && getPointerOperandAndType(MemI)) &&
328          "Candidate should be a memory instruction.");
329   assert(LSCEV && "Invalid SCEV for Ptr value.");
330   bool FoundBucket = false;
331   for (auto &B : Buckets) {
332     const SCEV *Diff = SE->getMinusSCEV(LSCEV, B.BaseSCEV);
333     if (const auto *CDiff = dyn_cast<SCEVConstant>(Diff)) {
334       B.Elements.push_back(BucketElement(CDiff, MemI));
335       FoundBucket = true;
336       break;
337     }
338   }
339 
340   if (!FoundBucket) {
341     if (Buckets.size() == MaxCandidateNum)
342       return;
343     Buckets.push_back(Bucket(LSCEV, MemI));
344   }
345 }
346 
347 SmallVector<Bucket, 16> PPCLoopInstrFormPrep::collectCandidates(
348     Loop *L,
349     std::function<bool(const Instruction *, const Value *, const Type *)>
350         isValidCandidate,
351     unsigned MaxCandidateNum) {
352   SmallVector<Bucket, 16> Buckets;
353   for (const auto &BB : L->blocks())
354     for (auto &J : *BB) {
355       Value *PtrValue = nullptr;
356       Type *PointerElementType = nullptr;
357       PtrValue = getPointerOperandAndType(&J, &PointerElementType);
358 
359       if (!PtrValue)
360         continue;
361 
362       if (PtrValue->getType()->getPointerAddressSpace())
363         continue;
364 
365       if (L->isLoopInvariant(PtrValue))
366         continue;
367 
368       const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L);
369       const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV);
370       if (!LARSCEV || LARSCEV->getLoop() != L)
371         continue;
372 
373       if (isValidCandidate(&J, PtrValue, PointerElementType))
374         addOneCandidate(&J, LSCEV, Buckets, MaxCandidateNum);
375     }
376   return Buckets;
377 }
378 
379 bool PPCLoopInstrFormPrep::prepareBaseForDispFormChain(Bucket &BucketChain,
380                                                     InstrForm Form) {
381   // RemainderOffsetInfo details:
382   // key:            value of (Offset urem DispConstraint). For DSForm, it can
383   //                 be [0, 4).
384   // first of pair:  the index of first BucketElement whose remainder is equal
385   //                 to key. For key 0, this value must be 0.
386   // second of pair: number of load/stores with the same remainder.
387   DenseMap<unsigned, std::pair<unsigned, unsigned>> RemainderOffsetInfo;
388 
389   for (unsigned j = 0, je = BucketChain.Elements.size(); j != je; ++j) {
390     if (!BucketChain.Elements[j].Offset)
391       RemainderOffsetInfo[0] = std::make_pair(0, 1);
392     else {
393       unsigned Remainder =
394           BucketChain.Elements[j].Offset->getAPInt().urem(Form);
395       if (RemainderOffsetInfo.find(Remainder) == RemainderOffsetInfo.end())
396         RemainderOffsetInfo[Remainder] = std::make_pair(j, 1);
397       else
398         RemainderOffsetInfo[Remainder].second++;
399     }
400   }
401   // Currently we choose the most profitable base as the one which has the max
402   // number of load/store with same remainder.
403   // FIXME: adjust the base selection strategy according to load/store offset
404   // distribution.
405   // For example, if we have one candidate chain for DS form preparation, which
406   // contains following load/stores with different remainders:
407   // 1: 10 load/store whose remainder is 1;
408   // 2: 9 load/store whose remainder is 2;
409   // 3: 1 for remainder 3 and 0 for remainder 0;
410   // Now we will choose the first load/store whose remainder is 1 as base and
411   // adjust all other load/stores according to new base, so we will get 10 DS
412   // form and 10 X form.
413   // But we should be more clever, for this case we could use two bases, one for
414   // remainder 1 and the other for remainder 2, thus we could get 19 DS form and
415   // 1 X form.
416   unsigned MaxCountRemainder = 0;
417   for (unsigned j = 0; j < (unsigned)Form; j++)
418     if ((RemainderOffsetInfo.find(j) != RemainderOffsetInfo.end()) &&
419         RemainderOffsetInfo[j].second >
420             RemainderOffsetInfo[MaxCountRemainder].second)
421       MaxCountRemainder = j;
422 
423   // Abort when there are too few insts with common base.
424   if (RemainderOffsetInfo[MaxCountRemainder].second < DispFormPrepMinThreshold)
425     return false;
426 
427   // If the first value is most profitable, no needed to adjust BucketChain
428   // elements as they are substracted the first value when collecting.
429   if (MaxCountRemainder == 0)
430     return true;
431 
432   // Adjust load/store to the new chosen base.
433   const SCEV *Offset =
434       BucketChain.Elements[RemainderOffsetInfo[MaxCountRemainder].first].Offset;
435   BucketChain.BaseSCEV = SE->getAddExpr(BucketChain.BaseSCEV, Offset);
436   for (auto &E : BucketChain.Elements) {
437     if (E.Offset)
438       E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset));
439     else
440       E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
441   }
442 
443   std::swap(BucketChain.Elements[RemainderOffsetInfo[MaxCountRemainder].first],
444             BucketChain.Elements[0]);
445   return true;
446 }
447 
448 // FIXME: implement a more clever base choosing policy.
449 // Currently we always choose an exist load/store offset. This maybe lead to
450 // suboptimal code sequences. For example, for one DS chain with offsets
451 // {-32769, 2003, 2007, 2011}, we choose -32769 as base offset, and left disp
452 // for load/stores are {0, 34772, 34776, 34780}. Though each offset now is a
453 // multipler of 4, it cannot be represented by sint16.
454 bool PPCLoopInstrFormPrep::prepareBaseForUpdateFormChain(Bucket &BucketChain) {
455   // We have a choice now of which instruction's memory operand we use as the
456   // base for the generated PHI. Always picking the first instruction in each
457   // bucket does not work well, specifically because that instruction might
458   // be a prefetch (and there are no pre-increment dcbt variants). Otherwise,
459   // the choice is somewhat arbitrary, because the backend will happily
460   // generate direct offsets from both the pre-incremented and
461   // post-incremented pointer values. Thus, we'll pick the first non-prefetch
462   // instruction in each bucket, and adjust the recurrence and other offsets
463   // accordingly.
464   for (int j = 0, je = BucketChain.Elements.size(); j != je; ++j) {
465     if (auto *II = dyn_cast<IntrinsicInst>(BucketChain.Elements[j].Instr))
466       if (II->getIntrinsicID() == Intrinsic::prefetch)
467         continue;
468 
469     // If we'd otherwise pick the first element anyway, there's nothing to do.
470     if (j == 0)
471       break;
472 
473     // If our chosen element has no offset from the base pointer, there's
474     // nothing to do.
475     if (!BucketChain.Elements[j].Offset ||
476         BucketChain.Elements[j].Offset->isZero())
477       break;
478 
479     const SCEV *Offset = BucketChain.Elements[j].Offset;
480     BucketChain.BaseSCEV = SE->getAddExpr(BucketChain.BaseSCEV, Offset);
481     for (auto &E : BucketChain.Elements) {
482       if (E.Offset)
483         E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset));
484       else
485         E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
486     }
487 
488     std::swap(BucketChain.Elements[j], BucketChain.Elements[0]);
489     break;
490   }
491   return true;
492 }
493 
494 bool PPCLoopInstrFormPrep::rewriteLoadStores(Loop *L, Bucket &BucketChain,
495                                           SmallSet<BasicBlock *, 16> &BBChanged,
496                                           InstrForm Form) {
497   bool MadeChange = false;
498   const SCEVAddRecExpr *BasePtrSCEV =
499       cast<SCEVAddRecExpr>(BucketChain.BaseSCEV);
500   if (!BasePtrSCEV->isAffine())
501     return MadeChange;
502 
503   LLVM_DEBUG(dbgs() << "PIP: Transforming: " << *BasePtrSCEV << "\n");
504 
505   assert(BasePtrSCEV->getLoop() == L && "AddRec for the wrong loop?");
506 
507   // The instruction corresponding to the Bucket's BaseSCEV must be the first
508   // in the vector of elements.
509   Instruction *MemI = BucketChain.Elements.begin()->Instr;
510   Value *BasePtr = getPointerOperandAndType(MemI);
511   assert(BasePtr && "No pointer operand");
512 
513   Type *I8Ty = Type::getInt8Ty(MemI->getParent()->getContext());
514   Type *I8PtrTy = Type::getInt8PtrTy(MemI->getParent()->getContext(),
515     BasePtr->getType()->getPointerAddressSpace());
516 
517   if (!SE->isLoopInvariant(BasePtrSCEV->getStart(), L))
518     return MadeChange;
519 
520   bool IsConstantInc = false;
521   const SCEV *BasePtrIncSCEV = BasePtrSCEV->getStepRecurrence(*SE);
522   Value *IncNode = getNodeForInc(L, MemI, BasePtrIncSCEV);
523 
524   const SCEVConstant *BasePtrIncConstantSCEV =
525       dyn_cast<SCEVConstant>(BasePtrIncSCEV);
526   if (BasePtrIncConstantSCEV)
527     IsConstantInc = true;
528 
529   // No valid representation for the increment.
530   if (!IncNode) {
531     LLVM_DEBUG(dbgs() << "Loop Increasement can not be represented!\n");
532     return MadeChange;
533   }
534 
535   // For some DS form load/store instructions, it can also be an update form,
536   // if the stride is constant and is a multipler of 4. Use update form if
537   // prefer it.
538   bool CanPreInc =
539       (Form == UpdateForm ||
540        ((Form == DSForm) && IsConstantInc &&
541         !BasePtrIncConstantSCEV->getAPInt().urem(4) && PreferUpdateForm));
542   const SCEV *BasePtrStartSCEV = nullptr;
543   if (CanPreInc) {
544     assert(SE->isLoopInvariant(BasePtrIncSCEV, L) &&
545            "Increment is not loop invariant!\n");
546     BasePtrStartSCEV = SE->getMinusSCEV(BasePtrSCEV->getStart(),
547                                         IsConstantInc ? BasePtrIncConstantSCEV
548                                                       : BasePtrIncSCEV);
549   } else
550     BasePtrStartSCEV = BasePtrSCEV->getStart();
551 
552   if (!isSafeToExpand(BasePtrStartSCEV, *SE))
553     return MadeChange;
554 
555   if (alreadyPrepared(L, MemI, BasePtrStartSCEV, BasePtrIncSCEV, Form)) {
556     LLVM_DEBUG(dbgs() << "Instruction form is already prepared!\n");
557     return MadeChange;
558   }
559 
560   LLVM_DEBUG(dbgs() << "PIP: New start is: " << *BasePtrStartSCEV << "\n");
561 
562   BasicBlock *Header = L->getHeader();
563   unsigned HeaderLoopPredCount = pred_size(Header);
564   BasicBlock *LoopPredecessor = L->getLoopPredecessor();
565 
566   PHINode *NewPHI =
567       PHINode::Create(I8PtrTy, HeaderLoopPredCount,
568                       getInstrName(MemI, PHINodeNameSuffix),
569                       Header->getFirstNonPHI());
570 
571   SCEVExpander SCEVE(*SE, Header->getModule()->getDataLayout(), "pistart");
572   Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy,
573                                             LoopPredecessor->getTerminator());
574 
575   // Note that LoopPredecessor might occur in the predecessor list multiple
576   // times, and we need to add it the right number of times.
577   for (auto PI : predecessors(Header)) {
578     if (PI != LoopPredecessor)
579       continue;
580 
581     NewPHI->addIncoming(BasePtrStart, LoopPredecessor);
582   }
583 
584   Instruction *PtrInc = nullptr;
585   Instruction *NewBasePtr = nullptr;
586   if (CanPreInc) {
587     Instruction *InsPoint = &*Header->getFirstInsertionPt();
588     PtrInc = GetElementPtrInst::Create(I8Ty, NewPHI, IncNode,
589                                        getInstrName(MemI, GEPNodeIncNameSuffix),
590                                        InsPoint);
591     cast<GetElementPtrInst>(PtrInc)->setIsInBounds(IsPtrInBounds(BasePtr));
592     for (auto PI : predecessors(Header)) {
593       if (PI == LoopPredecessor)
594         continue;
595 
596       NewPHI->addIncoming(PtrInc, PI);
597     }
598     if (PtrInc->getType() != BasePtr->getType())
599       NewBasePtr = new BitCastInst(
600           PtrInc, BasePtr->getType(),
601           getInstrName(PtrInc, CastNodeNameSuffix), InsPoint);
602     else
603       NewBasePtr = PtrInc;
604   } else {
605     // Note that LoopPredecessor might occur in the predecessor list multiple
606     // times, and we need to make sure no more incoming value for them in PHI.
607     for (auto PI : predecessors(Header)) {
608       if (PI == LoopPredecessor)
609         continue;
610 
611       // For the latch predecessor, we need to insert a GEP just before the
612       // terminator to increase the address.
613       BasicBlock *BB = PI;
614       Instruction *InsPoint = BB->getTerminator();
615       PtrInc = GetElementPtrInst::Create(
616           I8Ty, NewPHI, IncNode, getInstrName(MemI, GEPNodeIncNameSuffix),
617           InsPoint);
618       cast<GetElementPtrInst>(PtrInc)->setIsInBounds(IsPtrInBounds(BasePtr));
619 
620       NewPHI->addIncoming(PtrInc, PI);
621     }
622     PtrInc = NewPHI;
623     if (NewPHI->getType() != BasePtr->getType())
624       NewBasePtr =
625           new BitCastInst(NewPHI, BasePtr->getType(),
626                           getInstrName(NewPHI, CastNodeNameSuffix),
627                           &*Header->getFirstInsertionPt());
628     else
629       NewBasePtr = NewPHI;
630   }
631 
632   // Clear the rewriter cache, because values that are in the rewriter's cache
633   // can be deleted below, causing the AssertingVH in the cache to trigger.
634   SCEVE.clear();
635 
636   if (Instruction *IDel = dyn_cast<Instruction>(BasePtr))
637     BBChanged.insert(IDel->getParent());
638   BasePtr->replaceAllUsesWith(NewBasePtr);
639   RecursivelyDeleteTriviallyDeadInstructions(BasePtr);
640 
641   // Keep track of the replacement pointer values we've inserted so that we
642   // don't generate more pointer values than necessary.
643   SmallPtrSet<Value *, 16> NewPtrs;
644   NewPtrs.insert(NewBasePtr);
645 
646   for (auto I = std::next(BucketChain.Elements.begin()),
647        IE = BucketChain.Elements.end(); I != IE; ++I) {
648     Value *Ptr = getPointerOperandAndType(I->Instr);
649     assert(Ptr && "No pointer operand");
650     if (NewPtrs.count(Ptr))
651       continue;
652 
653     Instruction *RealNewPtr;
654     if (!I->Offset || I->Offset->getValue()->isZero()) {
655       RealNewPtr = NewBasePtr;
656     } else {
657       Instruction *PtrIP = dyn_cast<Instruction>(Ptr);
658       if (PtrIP && isa<Instruction>(NewBasePtr) &&
659           cast<Instruction>(NewBasePtr)->getParent() == PtrIP->getParent())
660         PtrIP = nullptr;
661       else if (PtrIP && isa<PHINode>(PtrIP))
662         PtrIP = &*PtrIP->getParent()->getFirstInsertionPt();
663       else if (!PtrIP)
664         PtrIP = I->Instr;
665 
666       GetElementPtrInst *NewPtr = GetElementPtrInst::Create(
667           I8Ty, PtrInc, I->Offset->getValue(),
668           getInstrName(I->Instr, GEPNodeOffNameSuffix), PtrIP);
669       if (!PtrIP)
670         NewPtr->insertAfter(cast<Instruction>(PtrInc));
671       NewPtr->setIsInBounds(IsPtrInBounds(Ptr));
672       RealNewPtr = NewPtr;
673     }
674 
675     if (Instruction *IDel = dyn_cast<Instruction>(Ptr))
676       BBChanged.insert(IDel->getParent());
677 
678     Instruction *ReplNewPtr;
679     if (Ptr->getType() != RealNewPtr->getType()) {
680       ReplNewPtr = new BitCastInst(RealNewPtr, Ptr->getType(),
681         getInstrName(Ptr, CastNodeNameSuffix));
682       ReplNewPtr->insertAfter(RealNewPtr);
683     } else
684       ReplNewPtr = RealNewPtr;
685 
686     Ptr->replaceAllUsesWith(ReplNewPtr);
687     RecursivelyDeleteTriviallyDeadInstructions(Ptr);
688 
689     NewPtrs.insert(RealNewPtr);
690   }
691 
692   MadeChange = true;
693 
694   SuccPrepCount++;
695 
696   if (Form == DSForm && !CanPreInc)
697     DSFormChainRewritten++;
698   else if (Form == DQForm)
699     DQFormChainRewritten++;
700   else if (Form == UpdateForm || (Form == DSForm && CanPreInc))
701     UpdFormChainRewritten++;
702 
703   return MadeChange;
704 }
705 
706 bool PPCLoopInstrFormPrep::updateFormPrep(Loop *L,
707                                        SmallVector<Bucket, 16> &Buckets) {
708   bool MadeChange = false;
709   if (Buckets.empty())
710     return MadeChange;
711   SmallSet<BasicBlock *, 16> BBChanged;
712   for (auto &Bucket : Buckets)
713     // The base address of each bucket is transformed into a phi and the others
714     // are rewritten based on new base.
715     if (prepareBaseForUpdateFormChain(Bucket))
716       MadeChange |= rewriteLoadStores(L, Bucket, BBChanged, UpdateForm);
717 
718   if (MadeChange)
719     for (auto &BB : L->blocks())
720       if (BBChanged.count(BB))
721         DeleteDeadPHIs(BB);
722   return MadeChange;
723 }
724 
725 bool PPCLoopInstrFormPrep::dispFormPrep(Loop *L, SmallVector<Bucket, 16> &Buckets,
726                                      InstrForm Form) {
727   bool MadeChange = false;
728 
729   if (Buckets.empty())
730     return MadeChange;
731 
732   SmallSet<BasicBlock *, 16> BBChanged;
733   for (auto &Bucket : Buckets) {
734     if (Bucket.Elements.size() < DispFormPrepMinThreshold)
735       continue;
736     if (prepareBaseForDispFormChain(Bucket, Form))
737       MadeChange |= rewriteLoadStores(L, Bucket, BBChanged, Form);
738   }
739 
740   if (MadeChange)
741     for (auto &BB : L->blocks())
742       if (BBChanged.count(BB))
743         DeleteDeadPHIs(BB);
744   return MadeChange;
745 }
746 
747 // Find the loop invariant increment node for SCEV BasePtrIncSCEV.
748 // bb.loop.preheader:
749 //   %start = ...
750 // bb.loop.body:
751 //   %phinode = phi [ %start, %bb.loop.preheader ], [ %add, %bb.loop.body ]
752 //   ...
753 //   %add = add %phinode, %inc  ; %inc is what we want to get.
754 //
755 Value *PPCLoopInstrFormPrep::getNodeForInc(Loop *L, Instruction *MemI,
756                                            const SCEV *BasePtrIncSCEV) {
757   // If the increment is a constant, no definition is needed.
758   // Return the value directly.
759   if (isa<SCEVConstant>(BasePtrIncSCEV))
760     return cast<SCEVConstant>(BasePtrIncSCEV)->getValue();
761 
762   if (!SE->isLoopInvariant(BasePtrIncSCEV, L))
763     return nullptr;
764 
765   BasicBlock *BB = MemI->getParent();
766   if (!BB)
767     return nullptr;
768 
769   BasicBlock *LatchBB = L->getLoopLatch();
770 
771   if (!LatchBB)
772     return nullptr;
773 
774   // Run through the PHIs and check their operands to find valid representation
775   // for the increment SCEV.
776   iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis();
777   for (auto &CurrentPHI : PHIIter) {
778     PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI);
779     if (!CurrentPHINode)
780       continue;
781 
782     if (!SE->isSCEVable(CurrentPHINode->getType()))
783       continue;
784 
785     const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L);
786 
787     const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV);
788     if (!PHIBasePtrSCEV)
789       continue;
790 
791     const SCEV *PHIBasePtrIncSCEV = PHIBasePtrSCEV->getStepRecurrence(*SE);
792 
793     if (!PHIBasePtrIncSCEV || (PHIBasePtrIncSCEV != BasePtrIncSCEV))
794       continue;
795 
796     // Get the incoming value from the loop latch and check if the value has
797     // the add form with the required increment.
798     if (Instruction *I = dyn_cast<Instruction>(
799             CurrentPHINode->getIncomingValueForBlock(LatchBB))) {
800       Value *StrippedBaseI = I;
801       while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBaseI))
802         StrippedBaseI = BC->getOperand(0);
803 
804       Instruction *StrippedI = dyn_cast<Instruction>(StrippedBaseI);
805       if (!StrippedI)
806         continue;
807 
808       // LSR pass may add a getelementptr instruction to do the loop increment,
809       // also search in that getelementptr instruction.
810       if (StrippedI->getOpcode() == Instruction::Add ||
811           (StrippedI->getOpcode() == Instruction::GetElementPtr &&
812            StrippedI->getNumOperands() == 2)) {
813         if (SE->getSCEVAtScope(StrippedI->getOperand(0), L) == BasePtrIncSCEV)
814           return StrippedI->getOperand(0);
815         if (SE->getSCEVAtScope(StrippedI->getOperand(1), L) == BasePtrIncSCEV)
816           return StrippedI->getOperand(1);
817       }
818     }
819   }
820   return nullptr;
821 }
822 
823 // In order to prepare for the preferred instruction form, a PHI is added.
824 // This function will check to see if that PHI already exists and will return
825 // true if it found an existing PHI with the matched start and increment as the
826 // one we wanted to create.
827 bool PPCLoopInstrFormPrep::alreadyPrepared(Loop *L, Instruction *MemI,
828                                            const SCEV *BasePtrStartSCEV,
829                                            const SCEV *BasePtrIncSCEV,
830                                            InstrForm Form) {
831   BasicBlock *BB = MemI->getParent();
832   if (!BB)
833     return false;
834 
835   BasicBlock *PredBB = L->getLoopPredecessor();
836   BasicBlock *LatchBB = L->getLoopLatch();
837 
838   if (!PredBB || !LatchBB)
839     return false;
840 
841   // Run through the PHIs and see if we have some that looks like a preparation
842   iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis();
843   for (auto & CurrentPHI : PHIIter) {
844     PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI);
845     if (!CurrentPHINode)
846       continue;
847 
848     if (!SE->isSCEVable(CurrentPHINode->getType()))
849       continue;
850 
851     const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L);
852 
853     const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV);
854     if (!PHIBasePtrSCEV)
855       continue;
856 
857     const SCEVConstant *PHIBasePtrIncSCEV =
858       dyn_cast<SCEVConstant>(PHIBasePtrSCEV->getStepRecurrence(*SE));
859     if (!PHIBasePtrIncSCEV)
860       continue;
861 
862     if (CurrentPHINode->getNumIncomingValues() == 2) {
863       if ((CurrentPHINode->getIncomingBlock(0) == LatchBB &&
864            CurrentPHINode->getIncomingBlock(1) == PredBB) ||
865           (CurrentPHINode->getIncomingBlock(1) == LatchBB &&
866            CurrentPHINode->getIncomingBlock(0) == PredBB)) {
867         if (PHIBasePtrIncSCEV == BasePtrIncSCEV) {
868           // The existing PHI (CurrentPHINode) has the same start and increment
869           // as the PHI that we wanted to create.
870           if (Form == UpdateForm &&
871               PHIBasePtrSCEV->getStart() == BasePtrStartSCEV) {
872             ++PHINodeAlreadyExistsUpdate;
873             return true;
874           }
875           if (Form == DSForm || Form == DQForm) {
876             const SCEVConstant *Diff = dyn_cast<SCEVConstant>(
877                 SE->getMinusSCEV(PHIBasePtrSCEV->getStart(), BasePtrStartSCEV));
878             if (Diff && !Diff->getAPInt().urem(Form)) {
879               if (Form == DSForm)
880                 ++PHINodeAlreadyExistsDS;
881               else
882                 ++PHINodeAlreadyExistsDQ;
883               return true;
884             }
885           }
886         }
887       }
888     }
889   }
890   return false;
891 }
892 
893 bool PPCLoopInstrFormPrep::runOnLoop(Loop *L) {
894   bool MadeChange = false;
895 
896   // Only prep. the inner-most loop
897   if (!L->isInnermost())
898     return MadeChange;
899 
900   // Return if already done enough preparation.
901   if (SuccPrepCount >= MaxVarsPrep)
902     return MadeChange;
903 
904   LLVM_DEBUG(dbgs() << "PIP: Examining: " << *L << "\n");
905 
906   BasicBlock *LoopPredecessor = L->getLoopPredecessor();
907   // If there is no loop predecessor, or the loop predecessor's terminator
908   // returns a value (which might contribute to determining the loop's
909   // iteration space), insert a new preheader for the loop.
910   if (!LoopPredecessor ||
911       !LoopPredecessor->getTerminator()->getType()->isVoidTy()) {
912     LoopPredecessor = InsertPreheaderForLoop(L, DT, LI, nullptr, PreserveLCSSA);
913     if (LoopPredecessor)
914       MadeChange = true;
915   }
916   if (!LoopPredecessor) {
917     LLVM_DEBUG(dbgs() << "PIP fails since no predecessor for current loop.\n");
918     return MadeChange;
919   }
920   // Check if a load/store has update form. This lambda is used by function
921   // collectCandidates which can collect candidates for types defined by lambda.
922   auto isUpdateFormCandidate = [&](const Instruction *I, const Value *PtrValue,
923                                    const Type *PointerElementType) {
924     assert((PtrValue && I) && "Invalid parameter!");
925     // There are no update forms for Altivec vector load/stores.
926     if (ST && ST->hasAltivec() && PointerElementType->isVectorTy())
927       return false;
928     // There are no update forms for P10 lxvp/stxvp intrinsic.
929     auto *II = dyn_cast<IntrinsicInst>(I);
930     if (II && ((II->getIntrinsicID() == Intrinsic::ppc_vsx_lxvp) ||
931                II->getIntrinsicID() == Intrinsic::ppc_vsx_stxvp))
932       return false;
933     // See getPreIndexedAddressParts, the displacement for LDU/STDU has to
934     // be 4's multiple (DS-form). For i64 loads/stores when the displacement
935     // fits in a 16-bit signed field but isn't a multiple of 4, it will be
936     // useless and possible to break some original well-form addressing mode
937     // to make this pre-inc prep for it.
938     if (PointerElementType->isIntegerTy(64)) {
939       const SCEV *LSCEV = SE->getSCEVAtScope(const_cast<Value *>(PtrValue), L);
940       const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV);
941       if (!LARSCEV || LARSCEV->getLoop() != L)
942         return false;
943       if (const SCEVConstant *StepConst =
944               dyn_cast<SCEVConstant>(LARSCEV->getStepRecurrence(*SE))) {
945         const APInt &ConstInt = StepConst->getValue()->getValue();
946         if (ConstInt.isSignedIntN(16) && ConstInt.srem(4) != 0)
947           return false;
948       }
949     }
950     return true;
951   };
952 
953   // Check if a load/store has DS form.
954   auto isDSFormCandidate = [](const Instruction *I, const Value *PtrValue,
955                               const Type *PointerElementType) {
956     assert((PtrValue && I) && "Invalid parameter!");
957     if (isa<IntrinsicInst>(I))
958       return false;
959     return (PointerElementType->isIntegerTy(64)) ||
960            (PointerElementType->isFloatTy()) ||
961            (PointerElementType->isDoubleTy()) ||
962            (PointerElementType->isIntegerTy(32) &&
963             llvm::any_of(I->users(),
964                          [](const User *U) { return isa<SExtInst>(U); }));
965   };
966 
967   // Check if a load/store has DQ form.
968   auto isDQFormCandidate = [&](const Instruction *I, const Value *PtrValue,
969                                const Type *PointerElementType) {
970     assert((PtrValue && I) && "Invalid parameter!");
971     // Check if it is a P10 lxvp/stxvp intrinsic.
972     auto *II = dyn_cast<IntrinsicInst>(I);
973     if (II)
974       return II->getIntrinsicID() == Intrinsic::ppc_vsx_lxvp ||
975              II->getIntrinsicID() == Intrinsic::ppc_vsx_stxvp;
976     // Check if it is a P9 vector load/store.
977     return ST && ST->hasP9Vector() && (PointerElementType->isVectorTy());
978   };
979 
980   // Collect buckets of comparable addresses used by loads and stores for update
981   // form.
982   SmallVector<Bucket, 16> UpdateFormBuckets =
983       collectCandidates(L, isUpdateFormCandidate, MaxVarsUpdateForm);
984 
985   // Prepare for update form.
986   if (!UpdateFormBuckets.empty())
987     MadeChange |= updateFormPrep(L, UpdateFormBuckets);
988 
989   // Collect buckets of comparable addresses used by loads and stores for DS
990   // form.
991   SmallVector<Bucket, 16> DSFormBuckets =
992       collectCandidates(L, isDSFormCandidate, MaxVarsDSForm);
993 
994   // Prepare for DS form.
995   if (!DSFormBuckets.empty())
996     MadeChange |= dispFormPrep(L, DSFormBuckets, DSForm);
997 
998   // Collect buckets of comparable addresses used by loads and stores for DQ
999   // form.
1000   SmallVector<Bucket, 16> DQFormBuckets =
1001       collectCandidates(L, isDQFormCandidate, MaxVarsDQForm);
1002 
1003   // Prepare for DQ form.
1004   if (!DQFormBuckets.empty())
1005     MadeChange |= dispFormPrep(L, DQFormBuckets, DQForm);
1006 
1007   return MadeChange;
1008 }
1009