xref: /llvm-project/llvm/lib/Target/PowerPC/PPCLoopInstrFormPrep.cpp (revision f71cb9dbb739bb58ce7e52e49fe384ff2ff11687)
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 // 3: common multiple chains for the load/stores with same offsets in the loop,
44 //    so that we can reuse the offsets and reduce the register pressure in the
45 //    loop. This transformation can also increase the loop ILP as now each chain
46 //    uses its own loop induction add/addi. But this will increase the number of
47 //    add/addi in the loop.
48 //
49 //    Generically, this means transforming loops like this:
50 //
51 //    char *p;
52 //    A1 = p + base1
53 //    A2 = p + base1 + offset
54 //    B1 = p + base2
55 //    B2 = p + base2 + offset
56 //
57 //    for (int i = 0; i < n; i++)
58 //      unsigned long x1 = *(unsigned long *)(A1 + i);
59 //      unsigned long x2 = *(unsigned long *)(A2 + i)
60 //      unsigned long x3 = *(unsigned long *)(B1 + i);
61 //      unsigned long x4 = *(unsigned long *)(B2 + i);
62 //    }
63 //
64 //    to look like this:
65 //
66 //    A1_new = p + base1 // chain 1
67 //    B1_new = p + base2 // chain 2, now inside the loop, common offset is
68 //                       // reused.
69 //
70 //    for (long long i = 0; i < n; i+=count) {
71 //      unsigned long x1 = *(unsigned long *)(A1_new + i);
72 //      unsigned long x2 = *(unsigned long *)((A1_new + i) + offset);
73 //      unsigned long x3 = *(unsigned long *)(B1_new + i);
74 //      unsigned long x4 = *(unsigned long *)((B1_new + i) + offset);
75 //    }
76 //===----------------------------------------------------------------------===//
77 
78 #include "PPC.h"
79 #include "PPCSubtarget.h"
80 #include "PPCTargetMachine.h"
81 #include "llvm/ADT/DepthFirstIterator.h"
82 #include "llvm/ADT/SmallPtrSet.h"
83 #include "llvm/ADT/SmallSet.h"
84 #include "llvm/ADT/SmallVector.h"
85 #include "llvm/ADT/Statistic.h"
86 #include "llvm/Analysis/LoopInfo.h"
87 #include "llvm/Analysis/ScalarEvolution.h"
88 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
89 #include "llvm/IR/BasicBlock.h"
90 #include "llvm/IR/CFG.h"
91 #include "llvm/IR/Dominators.h"
92 #include "llvm/IR/Instruction.h"
93 #include "llvm/IR/Instructions.h"
94 #include "llvm/IR/IntrinsicInst.h"
95 #include "llvm/IR/IntrinsicsPowerPC.h"
96 #include "llvm/IR/Type.h"
97 #include "llvm/IR/Value.h"
98 #include "llvm/InitializePasses.h"
99 #include "llvm/Pass.h"
100 #include "llvm/Support/Casting.h"
101 #include "llvm/Support/CommandLine.h"
102 #include "llvm/Support/Debug.h"
103 #include "llvm/Transforms/Scalar.h"
104 #include "llvm/Transforms/Utils.h"
105 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
106 #include "llvm/Transforms/Utils/Local.h"
107 #include "llvm/Transforms/Utils/LoopUtils.h"
108 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
109 #include <cassert>
110 #include <cmath>
111 #include <utility>
112 
113 #define DEBUG_TYPE "ppc-loop-instr-form-prep"
114 
115 using namespace llvm;
116 
117 static cl::opt<unsigned>
118     MaxVarsPrep("ppc-formprep-max-vars", cl::Hidden, cl::init(24),
119                 cl::desc("Potential common base number threshold per function "
120                          "for PPC loop prep"));
121 
122 static cl::opt<bool> PreferUpdateForm("ppc-formprep-prefer-update",
123                                  cl::init(true), cl::Hidden,
124   cl::desc("prefer update form when ds form is also a update form"));
125 
126 static cl::opt<bool> EnableUpdateFormForNonConstInc(
127     "ppc-formprep-update-nonconst-inc", cl::init(false), cl::Hidden,
128     cl::desc("prepare update form when the load/store increment is a loop "
129              "invariant non-const value."));
130 
131 static cl::opt<bool> EnableChainCommoning(
132     "ppc-formprep-chain-commoning", cl::init(false), cl::Hidden,
133     cl::desc("Enable chain commoning in PPC loop prepare pass."));
134 
135 // Sum of following 3 per loop thresholds for all loops can not be larger
136 // than MaxVarsPrep.
137 // now the thresholds for each kind prep are exterimental values on Power9.
138 static cl::opt<unsigned> MaxVarsUpdateForm("ppc-preinc-prep-max-vars",
139                                  cl::Hidden, cl::init(3),
140   cl::desc("Potential PHI threshold per loop for PPC loop prep of update "
141            "form"));
142 
143 static cl::opt<unsigned> MaxVarsDSForm("ppc-dsprep-max-vars",
144                                  cl::Hidden, cl::init(3),
145   cl::desc("Potential PHI threshold per loop for PPC loop prep of DS form"));
146 
147 static cl::opt<unsigned> MaxVarsDQForm("ppc-dqprep-max-vars",
148                                  cl::Hidden, cl::init(8),
149   cl::desc("Potential PHI threshold per loop for PPC loop prep of DQ form"));
150 
151 // Commoning chain will reduce the register pressure, so we don't consider about
152 // the PHI nodes number.
153 // But commoning chain will increase the addi/add number in the loop and also
154 // increase loop ILP. Maximum chain number should be same with hardware
155 // IssueWidth, because we won't benefit from ILP if the parallel chains number
156 // is bigger than IssueWidth. We assume there are 2 chains in one bucket, so
157 // there would be 4 buckets at most on P9(IssueWidth is 8).
158 static cl::opt<unsigned> MaxVarsChainCommon(
159     "ppc-chaincommon-max-vars", cl::Hidden, cl::init(4),
160     cl::desc("Bucket number per loop for PPC loop chain common"));
161 
162 // If would not be profitable if the common base has only one load/store, ISEL
163 // should already be able to choose best load/store form based on offset for
164 // single load/store. Set minimal profitable value default to 2 and make it as
165 // an option.
166 static cl::opt<unsigned> DispFormPrepMinThreshold("ppc-dispprep-min-threshold",
167                                     cl::Hidden, cl::init(2),
168   cl::desc("Minimal common base load/store instructions triggering DS/DQ form "
169            "preparation"));
170 
171 static cl::opt<unsigned> ChainCommonPrepMinThreshold(
172     "ppc-chaincommon-min-threshold", cl::Hidden, cl::init(4),
173     cl::desc("Minimal common base load/store instructions triggering chain "
174              "commoning preparation. Must be not smaller than 4"));
175 
176 STATISTIC(PHINodeAlreadyExistsUpdate, "PHI node already in pre-increment form");
177 STATISTIC(PHINodeAlreadyExistsDS, "PHI node already in DS form");
178 STATISTIC(PHINodeAlreadyExistsDQ, "PHI node already in DQ form");
179 STATISTIC(DSFormChainRewritten, "Num of DS form chain rewritten");
180 STATISTIC(DQFormChainRewritten, "Num of DQ form chain rewritten");
181 STATISTIC(UpdFormChainRewritten, "Num of update form chain rewritten");
182 STATISTIC(ChainCommoningRewritten, "Num of commoning chains");
183 
184 namespace {
185   struct BucketElement {
186     BucketElement(const SCEV *O, Instruction *I) : Offset(O), Instr(I) {}
187     BucketElement(Instruction *I) : Offset(nullptr), Instr(I) {}
188 
189     const SCEV *Offset;
190     Instruction *Instr;
191   };
192 
193   struct Bucket {
194     Bucket(const SCEV *B, Instruction *I)
195         : BaseSCEV(B), Elements(1, BucketElement(I)) {
196       ChainSize = 0;
197     }
198 
199     // The base of the whole bucket.
200     const SCEV *BaseSCEV;
201 
202     // All elements in the bucket. In the bucket, the element with the BaseSCEV
203     // has no offset and all other elements are stored as offsets to the
204     // BaseSCEV.
205     SmallVector<BucketElement, 16> Elements;
206 
207     // The potential chains size. This is used for chain commoning only.
208     unsigned ChainSize;
209 
210     // The base for each potential chain. This is used for chain commoning only.
211     SmallVector<BucketElement, 16> ChainBases;
212   };
213 
214   // "UpdateForm" is not a real PPC instruction form, it stands for dform
215   // load/store with update like ldu/stdu, or Prefetch intrinsic.
216   // For DS form instructions, their displacements must be multiple of 4.
217   // For DQ form instructions, their displacements must be multiple of 16.
218   enum PrepForm { UpdateForm = 1, DSForm = 4, DQForm = 16, ChainCommoning };
219 
220   class PPCLoopInstrFormPrep : public FunctionPass {
221   public:
222     static char ID; // Pass ID, replacement for typeid
223 
224     PPCLoopInstrFormPrep() : FunctionPass(ID) {
225       initializePPCLoopInstrFormPrepPass(*PassRegistry::getPassRegistry());
226     }
227 
228     PPCLoopInstrFormPrep(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) {
229       initializePPCLoopInstrFormPrepPass(*PassRegistry::getPassRegistry());
230     }
231 
232     void getAnalysisUsage(AnalysisUsage &AU) const override {
233       AU.addPreserved<DominatorTreeWrapperPass>();
234       AU.addRequired<LoopInfoWrapperPass>();
235       AU.addPreserved<LoopInfoWrapperPass>();
236       AU.addRequired<ScalarEvolutionWrapperPass>();
237     }
238 
239     bool runOnFunction(Function &F) override;
240 
241   private:
242     PPCTargetMachine *TM = nullptr;
243     const PPCSubtarget *ST;
244     DominatorTree *DT;
245     LoopInfo *LI;
246     ScalarEvolution *SE;
247     bool PreserveLCSSA;
248     bool HasCandidateForPrepare;
249 
250     /// Successful preparation number for Update/DS/DQ form in all inner most
251     /// loops. One successful preparation will put one common base out of loop,
252     /// this may leads to register presure like LICM does.
253     /// Make sure total preparation number can be controlled by option.
254     unsigned SuccPrepCount;
255 
256     bool runOnLoop(Loop *L);
257 
258     /// Check if required PHI node is already exist in Loop \p L.
259     bool alreadyPrepared(Loop *L, Instruction *MemI,
260                          const SCEV *BasePtrStartSCEV,
261                          const SCEV *BasePtrIncSCEV, PrepForm Form);
262 
263     /// Get the value which defines the increment SCEV \p BasePtrIncSCEV.
264     Value *getNodeForInc(Loop *L, Instruction *MemI,
265                          const SCEV *BasePtrIncSCEV);
266 
267     /// Common chains to reuse offsets for a loop to reduce register pressure.
268     bool chainCommoning(Loop *L, SmallVector<Bucket, 16> &Buckets);
269 
270     /// Find out the potential commoning chains and their bases.
271     bool prepareBasesForCommoningChains(Bucket &BucketChain);
272 
273     /// Rewrite load/store according to the common chains.
274     bool
275     rewriteLoadStoresForCommoningChains(Loop *L, Bucket &Bucket,
276                                         SmallSet<BasicBlock *, 16> &BBChanged);
277 
278     /// Collect condition matched(\p isValidCandidate() returns true)
279     /// candidates in Loop \p L.
280     SmallVector<Bucket, 16> collectCandidates(
281         Loop *L,
282         std::function<bool(const Instruction *, Value *, const Type *)>
283             isValidCandidate,
284         std::function<bool(const SCEV *)> isValidDiff,
285         unsigned MaxCandidateNum);
286 
287     /// Add a candidate to candidates \p Buckets if diff between candidate and
288     /// one base in \p Buckets matches \p isValidDiff.
289     void addOneCandidate(Instruction *MemI, const SCEV *LSCEV,
290                          SmallVector<Bucket, 16> &Buckets,
291                          std::function<bool(const SCEV *)> isValidDiff,
292                          unsigned MaxCandidateNum);
293 
294     /// Prepare all candidates in \p Buckets for update form.
295     bool updateFormPrep(Loop *L, SmallVector<Bucket, 16> &Buckets);
296 
297     /// Prepare all candidates in \p Buckets for displacement form, now for
298     /// ds/dq.
299     bool dispFormPrep(Loop *L, SmallVector<Bucket, 16> &Buckets, PrepForm Form);
300 
301     /// Prepare for one chain \p BucketChain, find the best base element and
302     /// update all other elements in \p BucketChain accordingly.
303     /// \p Form is used to find the best base element.
304     /// If success, best base element must be stored as the first element of
305     /// \p BucketChain.
306     /// Return false if no base element found, otherwise return true.
307     bool prepareBaseForDispFormChain(Bucket &BucketChain, PrepForm Form);
308 
309     /// Prepare for one chain \p BucketChain, find the best base element and
310     /// update all other elements in \p BucketChain accordingly.
311     /// If success, best base element must be stored as the first element of
312     /// \p BucketChain.
313     /// Return false if no base element found, otherwise return true.
314     bool prepareBaseForUpdateFormChain(Bucket &BucketChain);
315 
316     /// Rewrite load/store instructions in \p BucketChain according to
317     /// preparation.
318     bool rewriteLoadStores(Loop *L, Bucket &BucketChain,
319                            SmallSet<BasicBlock *, 16> &BBChanged,
320                            PrepForm Form);
321 
322     /// Rewrite for the base load/store of a chain.
323     std::pair<Instruction *, Instruction *>
324     rewriteForBase(Loop *L, const SCEVAddRecExpr *BasePtrSCEV,
325                    Instruction *BaseMemI, bool CanPreInc, PrepForm Form,
326                    SCEVExpander &SCEVE, SmallPtrSet<Value *, 16> &DeletedPtrs);
327 
328     /// Rewrite for the other load/stores of a chain according to the new \p
329     /// Base.
330     Instruction *
331     rewriteForBucketElement(std::pair<Instruction *, Instruction *> Base,
332                             const BucketElement &Element, Value *OffToBase,
333                             SmallPtrSet<Value *, 16> &DeletedPtrs);
334   };
335 
336 } // end anonymous namespace
337 
338 char PPCLoopInstrFormPrep::ID = 0;
339 static const char *name = "Prepare loop for ppc preferred instruction forms";
340 INITIALIZE_PASS_BEGIN(PPCLoopInstrFormPrep, DEBUG_TYPE, name, false, false)
341 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
342 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
343 INITIALIZE_PASS_END(PPCLoopInstrFormPrep, DEBUG_TYPE, name, false, false)
344 
345 static constexpr StringRef PHINodeNameSuffix    = ".phi";
346 static constexpr StringRef CastNodeNameSuffix   = ".cast";
347 static constexpr StringRef GEPNodeIncNameSuffix = ".inc";
348 static constexpr StringRef GEPNodeOffNameSuffix = ".off";
349 
350 FunctionPass *llvm::createPPCLoopInstrFormPrepPass(PPCTargetMachine &TM) {
351   return new PPCLoopInstrFormPrep(TM);
352 }
353 
354 static bool IsPtrInBounds(Value *BasePtr) {
355   Value *StrippedBasePtr = BasePtr;
356   while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr))
357     StrippedBasePtr = BC->getOperand(0);
358   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(StrippedBasePtr))
359     return GEP->isInBounds();
360 
361   return false;
362 }
363 
364 static std::string getInstrName(const Value *I, StringRef Suffix) {
365   assert(I && "Invalid paramater!");
366   if (I->hasName())
367     return (I->getName() + Suffix).str();
368   else
369     return "";
370 }
371 
372 static Value *getPointerOperandAndType(Value *MemI,
373                                        Type **PtrElementType = nullptr) {
374 
375   Value *PtrValue = nullptr;
376   Type *PointerElementType = nullptr;
377 
378   if (LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) {
379     PtrValue = LMemI->getPointerOperand();
380     PointerElementType = LMemI->getType();
381   } else if (StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) {
382     PtrValue = SMemI->getPointerOperand();
383     PointerElementType = SMemI->getValueOperand()->getType();
384   } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(MemI)) {
385     PointerElementType = Type::getInt8Ty(MemI->getContext());
386     if (IMemI->getIntrinsicID() == Intrinsic::prefetch ||
387         IMemI->getIntrinsicID() == Intrinsic::ppc_vsx_lxvp) {
388       PtrValue = IMemI->getArgOperand(0);
389     } else if (IMemI->getIntrinsicID() == Intrinsic::ppc_vsx_stxvp) {
390       PtrValue = IMemI->getArgOperand(1);
391     }
392   }
393   /*Get ElementType if PtrElementType is not null.*/
394   if (PtrElementType)
395     *PtrElementType = PointerElementType;
396 
397   return PtrValue;
398 }
399 
400 bool PPCLoopInstrFormPrep::runOnFunction(Function &F) {
401   if (skipFunction(F))
402     return false;
403 
404   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
405   SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
406   auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
407   DT = DTWP ? &DTWP->getDomTree() : nullptr;
408   PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
409   ST = TM ? TM->getSubtargetImpl(F) : nullptr;
410   SuccPrepCount = 0;
411 
412   bool MadeChange = false;
413 
414   for (Loop *I : *LI)
415     for (Loop *L : depth_first(I))
416       MadeChange |= runOnLoop(L);
417 
418   return MadeChange;
419 }
420 
421 // Finding the minimal(chain_number + reusable_offset_number) is a complicated
422 // algorithmic problem.
423 // For now, the algorithm used here is simply adjusted to handle the case for
424 // manually unrolling cases.
425 // FIXME: use a more powerful algorithm to find minimal sum of chain_number and
426 // reusable_offset_number for one base with multiple offsets.
427 bool PPCLoopInstrFormPrep::prepareBasesForCommoningChains(Bucket &CBucket) {
428   // The minimal size for profitable chain commoning:
429   // A1 = base + offset1
430   // A2 = base + offset2 (offset2 - offset1 = X)
431   // A3 = base + offset3
432   // A4 = base + offset4 (offset4 - offset3 = X)
433   // ======>
434   // base1 = base + offset1
435   // base2 = base + offset3
436   // A1 = base1
437   // A2 = base1 + X
438   // A3 = base2
439   // A4 = base2 + X
440   //
441   // There is benefit because of reuse of offest 'X'.
442 
443   assert(ChainCommonPrepMinThreshold >= 4 &&
444          "Thredhold can not be smaller than 4!\n");
445   if (CBucket.Elements.size() < ChainCommonPrepMinThreshold)
446     return false;
447 
448   // We simply select the FirstOffset as the first reusable offset between each
449   // chain element 1 and element 0.
450   const SCEV *FirstOffset = CBucket.Elements[1].Offset;
451 
452   // Figure out how many times above FirstOffset is used in the chain.
453   // For a success commoning chain candidate, offset difference between each
454   // chain element 1 and element 0 must be also FirstOffset.
455   unsigned FirstOffsetReusedCount = 1;
456 
457   // Figure out how many times above FirstOffset is used in the first chain.
458   // Chain number is FirstOffsetReusedCount / FirstOffsetReusedCountInFirstChain
459   unsigned FirstOffsetReusedCountInFirstChain = 1;
460 
461   unsigned EleNum = CBucket.Elements.size();
462   bool SawChainSeparater = false;
463   for (unsigned j = 2; j != EleNum; ++j) {
464     if (SE->getMinusSCEV(CBucket.Elements[j].Offset,
465                          CBucket.Elements[j - 1].Offset) == FirstOffset) {
466       if (!SawChainSeparater)
467         FirstOffsetReusedCountInFirstChain++;
468       FirstOffsetReusedCount++;
469     } else
470       // For now, if we meet any offset which is not FirstOffset, we assume we
471       // find a new Chain.
472       // This makes us miss some opportunities.
473       // For example, we can common:
474       //
475       // {OffsetA, Offset A, OffsetB, OffsetA, OffsetA, OffsetB}
476       //
477       // as two chains:
478       // {{OffsetA, Offset A, OffsetB}, {OffsetA, OffsetA, OffsetB}}
479       // FirstOffsetReusedCount = 4; FirstOffsetReusedCountInFirstChain = 2
480       //
481       // But we fail to common:
482       //
483       // {OffsetA, OffsetB, OffsetA, OffsetA, OffsetB, OffsetA}
484       // FirstOffsetReusedCount = 4; FirstOffsetReusedCountInFirstChain = 1
485 
486       SawChainSeparater = true;
487   }
488 
489   // FirstOffset is not reused, skip this bucket.
490   if (FirstOffsetReusedCount == 1)
491     return false;
492 
493   unsigned ChainNum =
494       FirstOffsetReusedCount / FirstOffsetReusedCountInFirstChain;
495 
496   // All elements are increased by FirstOffset.
497   // The number of chains should be sqrt(EleNum).
498   if (!SawChainSeparater)
499     ChainNum = (unsigned)sqrt((double)EleNum);
500 
501   CBucket.ChainSize = (unsigned)(EleNum / ChainNum);
502 
503   // If this is not a perfect chain(eg: not all elements can be put inside
504   // commoning chains.), skip now.
505   if (CBucket.ChainSize * ChainNum != EleNum)
506     return false;
507 
508   if (SawChainSeparater) {
509     // Check that the offset seqs are the same for all chains.
510     for (unsigned i = 1; i < CBucket.ChainSize; i++)
511       for (unsigned j = 1; j < ChainNum; j++)
512         if (CBucket.Elements[i].Offset !=
513             SE->getMinusSCEV(CBucket.Elements[i + j * CBucket.ChainSize].Offset,
514                              CBucket.Elements[j * CBucket.ChainSize].Offset))
515           return false;
516   }
517 
518   for (unsigned i = 0; i < ChainNum; i++)
519     CBucket.ChainBases.push_back(CBucket.Elements[i * CBucket.ChainSize]);
520 
521   LLVM_DEBUG(dbgs() << "Bucket has " << ChainNum << " chains.\n");
522 
523   return true;
524 }
525 
526 bool PPCLoopInstrFormPrep::chainCommoning(Loop *L,
527                                           SmallVector<Bucket, 16> &Buckets) {
528   bool MadeChange = false;
529 
530   if (Buckets.empty())
531     return MadeChange;
532 
533   SmallSet<BasicBlock *, 16> BBChanged;
534 
535   for (auto &Bucket : Buckets) {
536     if (prepareBasesForCommoningChains(Bucket))
537       MadeChange |= rewriteLoadStoresForCommoningChains(L, Bucket, BBChanged);
538   }
539 
540   if (MadeChange)
541     for (auto *BB : BBChanged)
542       DeleteDeadPHIs(BB);
543   return MadeChange;
544 }
545 
546 bool PPCLoopInstrFormPrep::rewriteLoadStoresForCommoningChains(
547     Loop *L, Bucket &Bucket, SmallSet<BasicBlock *, 16> &BBChanged) {
548   bool MadeChange = false;
549 
550   assert(Bucket.Elements.size() ==
551              Bucket.ChainBases.size() * Bucket.ChainSize &&
552          "invalid bucket for chain commoning!\n");
553   SmallPtrSet<Value *, 16> DeletedPtrs;
554 
555   BasicBlock *Header = L->getHeader();
556   BasicBlock *LoopPredecessor = L->getLoopPredecessor();
557 
558   SCEVExpander SCEVE(*SE, Header->getDataLayout(),
559                      "loopprepare-chaincommon");
560 
561   for (unsigned ChainIdx = 0; ChainIdx < Bucket.ChainBases.size(); ++ChainIdx) {
562     unsigned BaseElemIdx = Bucket.ChainSize * ChainIdx;
563     const SCEV *BaseSCEV =
564         ChainIdx ? SE->getAddExpr(Bucket.BaseSCEV,
565                                   Bucket.Elements[BaseElemIdx].Offset)
566                  : Bucket.BaseSCEV;
567     const SCEVAddRecExpr *BasePtrSCEV = cast<SCEVAddRecExpr>(BaseSCEV);
568 
569     // Make sure the base is able to expand.
570     if (!SCEVE.isSafeToExpand(BasePtrSCEV->getStart()))
571       return MadeChange;
572 
573     assert(BasePtrSCEV->isAffine() &&
574            "Invalid SCEV type for the base ptr for a candidate chain!\n");
575 
576     std::pair<Instruction *, Instruction *> Base = rewriteForBase(
577         L, BasePtrSCEV, Bucket.Elements[BaseElemIdx].Instr,
578         false /* CanPreInc */, ChainCommoning, SCEVE, DeletedPtrs);
579 
580     if (!Base.first || !Base.second)
581       return MadeChange;
582 
583     // Keep track of the replacement pointer values we've inserted so that we
584     // don't generate more pointer values than necessary.
585     SmallPtrSet<Value *, 16> NewPtrs;
586     NewPtrs.insert(Base.first);
587 
588     for (unsigned Idx = BaseElemIdx + 1; Idx < BaseElemIdx + Bucket.ChainSize;
589          ++Idx) {
590       BucketElement &I = Bucket.Elements[Idx];
591       Value *Ptr = getPointerOperandAndType(I.Instr);
592       assert(Ptr && "No pointer operand");
593       if (NewPtrs.count(Ptr))
594         continue;
595 
596       const SCEV *OffsetSCEV =
597           BaseElemIdx ? SE->getMinusSCEV(Bucket.Elements[Idx].Offset,
598                                          Bucket.Elements[BaseElemIdx].Offset)
599                       : Bucket.Elements[Idx].Offset;
600 
601       // Make sure offset is able to expand. Only need to check one time as the
602       // offsets are reused between different chains.
603       if (!BaseElemIdx)
604         if (!SCEVE.isSafeToExpand(OffsetSCEV))
605           return false;
606 
607       Value *OffsetValue = SCEVE.expandCodeFor(
608           OffsetSCEV, OffsetSCEV->getType(), LoopPredecessor->getTerminator());
609 
610       Instruction *NewPtr = rewriteForBucketElement(Base, Bucket.Elements[Idx],
611                                                     OffsetValue, DeletedPtrs);
612 
613       assert(NewPtr && "Wrong rewrite!\n");
614       NewPtrs.insert(NewPtr);
615     }
616 
617     ++ChainCommoningRewritten;
618   }
619 
620   // Clear the rewriter cache, because values that are in the rewriter's cache
621   // can be deleted below, causing the AssertingVH in the cache to trigger.
622   SCEVE.clear();
623 
624   for (auto *Ptr : DeletedPtrs) {
625     if (Instruction *IDel = dyn_cast<Instruction>(Ptr))
626       BBChanged.insert(IDel->getParent());
627     RecursivelyDeleteTriviallyDeadInstructions(Ptr);
628   }
629 
630   MadeChange = true;
631   return MadeChange;
632 }
633 
634 // Rewrite the new base according to BasePtrSCEV.
635 // bb.loop.preheader:
636 //   %newstart = ...
637 // bb.loop.body:
638 //   %phinode = phi [ %newstart, %bb.loop.preheader ], [ %add, %bb.loop.body ]
639 //   ...
640 //   %add = getelementptr %phinode, %inc
641 //
642 // First returned instruciton is %phinode (or a type cast to %phinode), caller
643 // needs this value to rewrite other load/stores in the same chain.
644 // Second returned instruction is %add, caller needs this value to rewrite other
645 // load/stores in the same chain.
646 std::pair<Instruction *, Instruction *>
647 PPCLoopInstrFormPrep::rewriteForBase(Loop *L, const SCEVAddRecExpr *BasePtrSCEV,
648                                      Instruction *BaseMemI, bool CanPreInc,
649                                      PrepForm Form, SCEVExpander &SCEVE,
650                                      SmallPtrSet<Value *, 16> &DeletedPtrs) {
651 
652   LLVM_DEBUG(dbgs() << "PIP: Transforming: " << *BasePtrSCEV << "\n");
653 
654   assert(BasePtrSCEV->getLoop() == L && "AddRec for the wrong loop?");
655 
656   Value *BasePtr = getPointerOperandAndType(BaseMemI);
657   assert(BasePtr && "No pointer operand");
658 
659   Type *I8Ty = Type::getInt8Ty(BaseMemI->getParent()->getContext());
660   Type *I8PtrTy =
661       PointerType::get(BaseMemI->getParent()->getContext(),
662                        BasePtr->getType()->getPointerAddressSpace());
663 
664   bool IsConstantInc = false;
665   const SCEV *BasePtrIncSCEV = BasePtrSCEV->getStepRecurrence(*SE);
666   Value *IncNode = getNodeForInc(L, BaseMemI, BasePtrIncSCEV);
667 
668   const SCEVConstant *BasePtrIncConstantSCEV =
669       dyn_cast<SCEVConstant>(BasePtrIncSCEV);
670   if (BasePtrIncConstantSCEV)
671     IsConstantInc = true;
672 
673   // No valid representation for the increment.
674   if (!IncNode) {
675     LLVM_DEBUG(dbgs() << "Loop Increasement can not be represented!\n");
676     return std::make_pair(nullptr, nullptr);
677   }
678 
679   if (Form == UpdateForm && !IsConstantInc && !EnableUpdateFormForNonConstInc) {
680     LLVM_DEBUG(
681         dbgs()
682         << "Update form prepare for non-const increment is not enabled!\n");
683     return std::make_pair(nullptr, nullptr);
684   }
685 
686   const SCEV *BasePtrStartSCEV = nullptr;
687   if (CanPreInc) {
688     assert(SE->isLoopInvariant(BasePtrIncSCEV, L) &&
689            "Increment is not loop invariant!\n");
690     BasePtrStartSCEV = SE->getMinusSCEV(BasePtrSCEV->getStart(),
691                                         IsConstantInc ? BasePtrIncConstantSCEV
692                                                       : BasePtrIncSCEV);
693   } else
694     BasePtrStartSCEV = BasePtrSCEV->getStart();
695 
696   if (alreadyPrepared(L, BaseMemI, BasePtrStartSCEV, BasePtrIncSCEV, Form)) {
697     LLVM_DEBUG(dbgs() << "Instruction form is already prepared!\n");
698     return std::make_pair(nullptr, nullptr);
699   }
700 
701   LLVM_DEBUG(dbgs() << "PIP: New start is: " << *BasePtrStartSCEV << "\n");
702 
703   BasicBlock *Header = L->getHeader();
704   unsigned HeaderLoopPredCount = pred_size(Header);
705   BasicBlock *LoopPredecessor = L->getLoopPredecessor();
706 
707   PHINode *NewPHI = PHINode::Create(I8PtrTy, HeaderLoopPredCount,
708                                     getInstrName(BaseMemI, PHINodeNameSuffix));
709   NewPHI->insertBefore(Header->getFirstNonPHIIt());
710 
711   Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy,
712                                             LoopPredecessor->getTerminator());
713 
714   // Note that LoopPredecessor might occur in the predecessor list multiple
715   // times, and we need to add it the right number of times.
716   for (auto *PI : predecessors(Header)) {
717     if (PI != LoopPredecessor)
718       continue;
719 
720     NewPHI->addIncoming(BasePtrStart, LoopPredecessor);
721   }
722 
723   Instruction *PtrInc = nullptr;
724   Instruction *NewBasePtr = nullptr;
725   if (CanPreInc) {
726     BasicBlock::iterator InsPoint = Header->getFirstInsertionPt();
727     PtrInc = GetElementPtrInst::Create(
728         I8Ty, NewPHI, IncNode, getInstrName(BaseMemI, GEPNodeIncNameSuffix),
729         InsPoint);
730     cast<GetElementPtrInst>(PtrInc)->setIsInBounds(IsPtrInBounds(BasePtr));
731     for (auto *PI : predecessors(Header)) {
732       if (PI == LoopPredecessor)
733         continue;
734 
735       NewPHI->addIncoming(PtrInc, PI);
736     }
737     if (PtrInc->getType() != BasePtr->getType())
738       NewBasePtr =
739           new BitCastInst(PtrInc, BasePtr->getType(),
740                           getInstrName(PtrInc, CastNodeNameSuffix), InsPoint);
741     else
742       NewBasePtr = PtrInc;
743   } else {
744     // Note that LoopPredecessor might occur in the predecessor list multiple
745     // times, and we need to make sure no more incoming value for them in PHI.
746     for (auto *PI : predecessors(Header)) {
747       if (PI == LoopPredecessor)
748         continue;
749 
750       // For the latch predecessor, we need to insert a GEP just before the
751       // terminator to increase the address.
752       BasicBlock *BB = PI;
753       BasicBlock::iterator InsPoint = BB->getTerminator()->getIterator();
754       PtrInc = GetElementPtrInst::Create(
755           I8Ty, NewPHI, IncNode, getInstrName(BaseMemI, GEPNodeIncNameSuffix),
756           InsPoint);
757       cast<GetElementPtrInst>(PtrInc)->setIsInBounds(IsPtrInBounds(BasePtr));
758 
759       NewPHI->addIncoming(PtrInc, PI);
760     }
761     PtrInc = NewPHI;
762     if (NewPHI->getType() != BasePtr->getType())
763       NewBasePtr = new BitCastInst(NewPHI, BasePtr->getType(),
764                                    getInstrName(NewPHI, CastNodeNameSuffix),
765                                    Header->getFirstInsertionPt());
766     else
767       NewBasePtr = NewPHI;
768   }
769 
770   BasePtr->replaceAllUsesWith(NewBasePtr);
771 
772   DeletedPtrs.insert(BasePtr);
773 
774   return std::make_pair(NewBasePtr, PtrInc);
775 }
776 
777 Instruction *PPCLoopInstrFormPrep::rewriteForBucketElement(
778     std::pair<Instruction *, Instruction *> Base, const BucketElement &Element,
779     Value *OffToBase, SmallPtrSet<Value *, 16> &DeletedPtrs) {
780   Instruction *NewBasePtr = Base.first;
781   Instruction *PtrInc = Base.second;
782   assert((NewBasePtr && PtrInc) && "base does not exist!\n");
783 
784   Type *I8Ty = Type::getInt8Ty(PtrInc->getParent()->getContext());
785 
786   Value *Ptr = getPointerOperandAndType(Element.Instr);
787   assert(Ptr && "No pointer operand");
788 
789   Instruction *RealNewPtr;
790   if (!Element.Offset ||
791       (isa<SCEVConstant>(Element.Offset) &&
792        cast<SCEVConstant>(Element.Offset)->getValue()->isZero())) {
793     RealNewPtr = NewBasePtr;
794   } else {
795     std::optional<BasicBlock::iterator> PtrIP = std::nullopt;
796     if (Instruction *I = dyn_cast<Instruction>(Ptr))
797       PtrIP = I->getIterator();
798 
799     if (PtrIP && isa<Instruction>(NewBasePtr) &&
800         cast<Instruction>(NewBasePtr)->getParent() == (*PtrIP)->getParent())
801       PtrIP = std::nullopt;
802     else if (PtrIP && isa<PHINode>(*PtrIP))
803       PtrIP = (*PtrIP)->getParent()->getFirstInsertionPt();
804     else if (!PtrIP)
805       PtrIP = Element.Instr->getIterator();
806 
807     assert(OffToBase && "There should be an offset for non base element!\n");
808     GetElementPtrInst *NewPtr = GetElementPtrInst::Create(
809         I8Ty, PtrInc, OffToBase,
810         getInstrName(Element.Instr, GEPNodeOffNameSuffix));
811     if (PtrIP)
812       NewPtr->insertBefore(*(*PtrIP)->getParent(), *PtrIP);
813     else
814       NewPtr->insertAfter(cast<Instruction>(PtrInc));
815     NewPtr->setIsInBounds(IsPtrInBounds(Ptr));
816     RealNewPtr = NewPtr;
817   }
818 
819   Instruction *ReplNewPtr;
820   if (Ptr->getType() != RealNewPtr->getType()) {
821     ReplNewPtr = new BitCastInst(RealNewPtr, Ptr->getType(),
822                                  getInstrName(Ptr, CastNodeNameSuffix));
823     ReplNewPtr->insertAfter(RealNewPtr);
824   } else
825     ReplNewPtr = RealNewPtr;
826 
827   Ptr->replaceAllUsesWith(ReplNewPtr);
828   DeletedPtrs.insert(Ptr);
829 
830   return ReplNewPtr;
831 }
832 
833 void PPCLoopInstrFormPrep::addOneCandidate(
834     Instruction *MemI, const SCEV *LSCEV, SmallVector<Bucket, 16> &Buckets,
835     std::function<bool(const SCEV *)> isValidDiff, unsigned MaxCandidateNum) {
836   assert((MemI && getPointerOperandAndType(MemI)) &&
837          "Candidate should be a memory instruction.");
838   assert(LSCEV && "Invalid SCEV for Ptr value.");
839 
840   bool FoundBucket = false;
841   for (auto &B : Buckets) {
842     if (cast<SCEVAddRecExpr>(B.BaseSCEV)->getStepRecurrence(*SE) !=
843         cast<SCEVAddRecExpr>(LSCEV)->getStepRecurrence(*SE))
844       continue;
845     const SCEV *Diff = SE->getMinusSCEV(LSCEV, B.BaseSCEV);
846     if (isValidDiff(Diff)) {
847       B.Elements.push_back(BucketElement(Diff, MemI));
848       FoundBucket = true;
849       break;
850     }
851   }
852 
853   if (!FoundBucket) {
854     if (Buckets.size() == MaxCandidateNum) {
855       LLVM_DEBUG(dbgs() << "Can not prepare more chains, reach maximum limit "
856                         << MaxCandidateNum << "\n");
857       return;
858     }
859     Buckets.push_back(Bucket(LSCEV, MemI));
860   }
861 }
862 
863 SmallVector<Bucket, 16> PPCLoopInstrFormPrep::collectCandidates(
864     Loop *L,
865     std::function<bool(const Instruction *, Value *, const Type *)>
866         isValidCandidate,
867     std::function<bool(const SCEV *)> isValidDiff, unsigned MaxCandidateNum) {
868   SmallVector<Bucket, 16> Buckets;
869 
870   for (const auto &BB : L->blocks())
871     for (auto &J : *BB) {
872       Value *PtrValue = nullptr;
873       Type *PointerElementType = nullptr;
874       PtrValue = getPointerOperandAndType(&J, &PointerElementType);
875 
876       if (!PtrValue)
877         continue;
878 
879       if (PtrValue->getType()->getPointerAddressSpace())
880         continue;
881 
882       if (L->isLoopInvariant(PtrValue))
883         continue;
884 
885       const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L);
886       const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV);
887       if (!LARSCEV || LARSCEV->getLoop() != L)
888         continue;
889 
890       // Mark that we have candidates for preparing.
891       HasCandidateForPrepare = true;
892 
893       if (isValidCandidate(&J, PtrValue, PointerElementType))
894         addOneCandidate(&J, LSCEV, Buckets, isValidDiff, MaxCandidateNum);
895     }
896   return Buckets;
897 }
898 
899 bool PPCLoopInstrFormPrep::prepareBaseForDispFormChain(Bucket &BucketChain,
900                                                        PrepForm Form) {
901   // RemainderOffsetInfo details:
902   // key:            value of (Offset urem DispConstraint). For DSForm, it can
903   //                 be [0, 4).
904   // first of pair:  the index of first BucketElement whose remainder is equal
905   //                 to key. For key 0, this value must be 0.
906   // second of pair: number of load/stores with the same remainder.
907   DenseMap<unsigned, std::pair<unsigned, unsigned>> RemainderOffsetInfo;
908 
909   for (unsigned j = 0, je = BucketChain.Elements.size(); j != je; ++j) {
910     if (!BucketChain.Elements[j].Offset)
911       RemainderOffsetInfo[0] = std::make_pair(0, 1);
912     else {
913       unsigned Remainder = cast<SCEVConstant>(BucketChain.Elements[j].Offset)
914                                ->getAPInt()
915                                .urem(Form);
916       if (!RemainderOffsetInfo.contains(Remainder))
917         RemainderOffsetInfo[Remainder] = std::make_pair(j, 1);
918       else
919         RemainderOffsetInfo[Remainder].second++;
920     }
921   }
922   // Currently we choose the most profitable base as the one which has the max
923   // number of load/store with same remainder.
924   // FIXME: adjust the base selection strategy according to load/store offset
925   // distribution.
926   // For example, if we have one candidate chain for DS form preparation, which
927   // contains following load/stores with different remainders:
928   // 1: 10 load/store whose remainder is 1;
929   // 2: 9 load/store whose remainder is 2;
930   // 3: 1 for remainder 3 and 0 for remainder 0;
931   // Now we will choose the first load/store whose remainder is 1 as base and
932   // adjust all other load/stores according to new base, so we will get 10 DS
933   // form and 10 X form.
934   // But we should be more clever, for this case we could use two bases, one for
935   // remainder 1 and the other for remainder 2, thus we could get 19 DS form and
936   // 1 X form.
937   unsigned MaxCountRemainder = 0;
938   for (unsigned j = 0; j < (unsigned)Form; j++)
939     if ((RemainderOffsetInfo.contains(j)) &&
940         RemainderOffsetInfo[j].second >
941             RemainderOffsetInfo[MaxCountRemainder].second)
942       MaxCountRemainder = j;
943 
944   // Abort when there are too few insts with common base.
945   if (RemainderOffsetInfo[MaxCountRemainder].second < DispFormPrepMinThreshold)
946     return false;
947 
948   // If the first value is most profitable, no needed to adjust BucketChain
949   // elements as they are substracted the first value when collecting.
950   if (MaxCountRemainder == 0)
951     return true;
952 
953   // Adjust load/store to the new chosen base.
954   const SCEV *Offset =
955       BucketChain.Elements[RemainderOffsetInfo[MaxCountRemainder].first].Offset;
956   BucketChain.BaseSCEV = SE->getAddExpr(BucketChain.BaseSCEV, Offset);
957   for (auto &E : BucketChain.Elements) {
958     if (E.Offset)
959       E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset));
960     else
961       E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
962   }
963 
964   std::swap(BucketChain.Elements[RemainderOffsetInfo[MaxCountRemainder].first],
965             BucketChain.Elements[0]);
966   return true;
967 }
968 
969 // FIXME: implement a more clever base choosing policy.
970 // Currently we always choose an exist load/store offset. This maybe lead to
971 // suboptimal code sequences. For example, for one DS chain with offsets
972 // {-32769, 2003, 2007, 2011}, we choose -32769 as base offset, and left disp
973 // for load/stores are {0, 34772, 34776, 34780}. Though each offset now is a
974 // multipler of 4, it cannot be represented by sint16.
975 bool PPCLoopInstrFormPrep::prepareBaseForUpdateFormChain(Bucket &BucketChain) {
976   // We have a choice now of which instruction's memory operand we use as the
977   // base for the generated PHI. Always picking the first instruction in each
978   // bucket does not work well, specifically because that instruction might
979   // be a prefetch (and there are no pre-increment dcbt variants). Otherwise,
980   // the choice is somewhat arbitrary, because the backend will happily
981   // generate direct offsets from both the pre-incremented and
982   // post-incremented pointer values. Thus, we'll pick the first non-prefetch
983   // instruction in each bucket, and adjust the recurrence and other offsets
984   // accordingly.
985   for (int j = 0, je = BucketChain.Elements.size(); j != je; ++j) {
986     if (auto *II = dyn_cast<IntrinsicInst>(BucketChain.Elements[j].Instr))
987       if (II->getIntrinsicID() == Intrinsic::prefetch)
988         continue;
989 
990     // If we'd otherwise pick the first element anyway, there's nothing to do.
991     if (j == 0)
992       break;
993 
994     // If our chosen element has no offset from the base pointer, there's
995     // nothing to do.
996     if (!BucketChain.Elements[j].Offset ||
997         cast<SCEVConstant>(BucketChain.Elements[j].Offset)->isZero())
998       break;
999 
1000     const SCEV *Offset = BucketChain.Elements[j].Offset;
1001     BucketChain.BaseSCEV = SE->getAddExpr(BucketChain.BaseSCEV, Offset);
1002     for (auto &E : BucketChain.Elements) {
1003       if (E.Offset)
1004         E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset));
1005       else
1006         E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
1007     }
1008 
1009     std::swap(BucketChain.Elements[j], BucketChain.Elements[0]);
1010     break;
1011   }
1012   return true;
1013 }
1014 
1015 bool PPCLoopInstrFormPrep::rewriteLoadStores(
1016     Loop *L, Bucket &BucketChain, SmallSet<BasicBlock *, 16> &BBChanged,
1017     PrepForm Form) {
1018   bool MadeChange = false;
1019 
1020   const SCEVAddRecExpr *BasePtrSCEV =
1021       cast<SCEVAddRecExpr>(BucketChain.BaseSCEV);
1022   if (!BasePtrSCEV->isAffine())
1023     return MadeChange;
1024 
1025   BasicBlock *Header = L->getHeader();
1026   SCEVExpander SCEVE(*SE, Header->getDataLayout(),
1027                      "loopprepare-formrewrite");
1028   if (!SCEVE.isSafeToExpand(BasePtrSCEV->getStart()))
1029     return MadeChange;
1030 
1031   SmallPtrSet<Value *, 16> DeletedPtrs;
1032 
1033   // For some DS form load/store instructions, it can also be an update form,
1034   // if the stride is constant and is a multipler of 4. Use update form if
1035   // prefer it.
1036   bool CanPreInc = (Form == UpdateForm ||
1037                     ((Form == DSForm) &&
1038                      isa<SCEVConstant>(BasePtrSCEV->getStepRecurrence(*SE)) &&
1039                      !cast<SCEVConstant>(BasePtrSCEV->getStepRecurrence(*SE))
1040                           ->getAPInt()
1041                           .urem(4) &&
1042                      PreferUpdateForm));
1043 
1044   std::pair<Instruction *, Instruction *> Base =
1045       rewriteForBase(L, BasePtrSCEV, BucketChain.Elements.begin()->Instr,
1046                      CanPreInc, Form, SCEVE, DeletedPtrs);
1047 
1048   if (!Base.first || !Base.second)
1049     return MadeChange;
1050 
1051   // Keep track of the replacement pointer values we've inserted so that we
1052   // don't generate more pointer values than necessary.
1053   SmallPtrSet<Value *, 16> NewPtrs;
1054   NewPtrs.insert(Base.first);
1055 
1056   for (const BucketElement &BE : llvm::drop_begin(BucketChain.Elements)) {
1057     Value *Ptr = getPointerOperandAndType(BE.Instr);
1058     assert(Ptr && "No pointer operand");
1059     if (NewPtrs.count(Ptr))
1060       continue;
1061 
1062     Instruction *NewPtr = rewriteForBucketElement(
1063         Base, BE,
1064         BE.Offset ? cast<SCEVConstant>(BE.Offset)->getValue() : nullptr,
1065         DeletedPtrs);
1066     assert(NewPtr && "wrong rewrite!\n");
1067     NewPtrs.insert(NewPtr);
1068   }
1069 
1070   // Clear the rewriter cache, because values that are in the rewriter's cache
1071   // can be deleted below, causing the AssertingVH in the cache to trigger.
1072   SCEVE.clear();
1073 
1074   for (auto *Ptr : DeletedPtrs) {
1075     if (Instruction *IDel = dyn_cast<Instruction>(Ptr))
1076       BBChanged.insert(IDel->getParent());
1077     RecursivelyDeleteTriviallyDeadInstructions(Ptr);
1078   }
1079 
1080   MadeChange = true;
1081 
1082   SuccPrepCount++;
1083 
1084   if (Form == DSForm && !CanPreInc)
1085     DSFormChainRewritten++;
1086   else if (Form == DQForm)
1087     DQFormChainRewritten++;
1088   else if (Form == UpdateForm || (Form == DSForm && CanPreInc))
1089     UpdFormChainRewritten++;
1090 
1091   return MadeChange;
1092 }
1093 
1094 bool PPCLoopInstrFormPrep::updateFormPrep(Loop *L,
1095                                        SmallVector<Bucket, 16> &Buckets) {
1096   bool MadeChange = false;
1097   if (Buckets.empty())
1098     return MadeChange;
1099   SmallSet<BasicBlock *, 16> BBChanged;
1100   for (auto &Bucket : Buckets)
1101     // The base address of each bucket is transformed into a phi and the others
1102     // are rewritten based on new base.
1103     if (prepareBaseForUpdateFormChain(Bucket))
1104       MadeChange |= rewriteLoadStores(L, Bucket, BBChanged, UpdateForm);
1105 
1106   if (MadeChange)
1107     for (auto *BB : BBChanged)
1108       DeleteDeadPHIs(BB);
1109   return MadeChange;
1110 }
1111 
1112 bool PPCLoopInstrFormPrep::dispFormPrep(Loop *L,
1113                                         SmallVector<Bucket, 16> &Buckets,
1114                                         PrepForm Form) {
1115   bool MadeChange = false;
1116 
1117   if (Buckets.empty())
1118     return MadeChange;
1119 
1120   SmallSet<BasicBlock *, 16> BBChanged;
1121   for (auto &Bucket : Buckets) {
1122     if (Bucket.Elements.size() < DispFormPrepMinThreshold)
1123       continue;
1124     if (prepareBaseForDispFormChain(Bucket, Form))
1125       MadeChange |= rewriteLoadStores(L, Bucket, BBChanged, Form);
1126   }
1127 
1128   if (MadeChange)
1129     for (auto *BB : BBChanged)
1130       DeleteDeadPHIs(BB);
1131   return MadeChange;
1132 }
1133 
1134 // Find the loop invariant increment node for SCEV BasePtrIncSCEV.
1135 // bb.loop.preheader:
1136 //   %start = ...
1137 // bb.loop.body:
1138 //   %phinode = phi [ %start, %bb.loop.preheader ], [ %add, %bb.loop.body ]
1139 //   ...
1140 //   %add = add %phinode, %inc  ; %inc is what we want to get.
1141 //
1142 Value *PPCLoopInstrFormPrep::getNodeForInc(Loop *L, Instruction *MemI,
1143                                            const SCEV *BasePtrIncSCEV) {
1144   // If the increment is a constant, no definition is needed.
1145   // Return the value directly.
1146   if (isa<SCEVConstant>(BasePtrIncSCEV))
1147     return cast<SCEVConstant>(BasePtrIncSCEV)->getValue();
1148 
1149   if (!SE->isLoopInvariant(BasePtrIncSCEV, L))
1150     return nullptr;
1151 
1152   BasicBlock *BB = MemI->getParent();
1153   if (!BB)
1154     return nullptr;
1155 
1156   BasicBlock *LatchBB = L->getLoopLatch();
1157 
1158   if (!LatchBB)
1159     return nullptr;
1160 
1161   // Run through the PHIs and check their operands to find valid representation
1162   // for the increment SCEV.
1163   iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis();
1164   for (auto &CurrentPHI : PHIIter) {
1165     PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI);
1166     if (!CurrentPHINode)
1167       continue;
1168 
1169     if (!SE->isSCEVable(CurrentPHINode->getType()))
1170       continue;
1171 
1172     const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L);
1173 
1174     const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV);
1175     if (!PHIBasePtrSCEV)
1176       continue;
1177 
1178     const SCEV *PHIBasePtrIncSCEV = PHIBasePtrSCEV->getStepRecurrence(*SE);
1179 
1180     if (!PHIBasePtrIncSCEV || (PHIBasePtrIncSCEV != BasePtrIncSCEV))
1181       continue;
1182 
1183     // Get the incoming value from the loop latch and check if the value has
1184     // the add form with the required increment.
1185     if (CurrentPHINode->getBasicBlockIndex(LatchBB) < 0)
1186       continue;
1187     if (Instruction *I = dyn_cast<Instruction>(
1188             CurrentPHINode->getIncomingValueForBlock(LatchBB))) {
1189       Value *StrippedBaseI = I;
1190       while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBaseI))
1191         StrippedBaseI = BC->getOperand(0);
1192 
1193       Instruction *StrippedI = dyn_cast<Instruction>(StrippedBaseI);
1194       if (!StrippedI)
1195         continue;
1196 
1197       // LSR pass may add a getelementptr instruction to do the loop increment,
1198       // also search in that getelementptr instruction.
1199       if (StrippedI->getOpcode() == Instruction::Add ||
1200           (StrippedI->getOpcode() == Instruction::GetElementPtr &&
1201            StrippedI->getNumOperands() == 2)) {
1202         if (SE->getSCEVAtScope(StrippedI->getOperand(0), L) == BasePtrIncSCEV)
1203           return StrippedI->getOperand(0);
1204         if (SE->getSCEVAtScope(StrippedI->getOperand(1), L) == BasePtrIncSCEV)
1205           return StrippedI->getOperand(1);
1206       }
1207     }
1208   }
1209   return nullptr;
1210 }
1211 
1212 // In order to prepare for the preferred instruction form, a PHI is added.
1213 // This function will check to see if that PHI already exists and will return
1214 // true if it found an existing PHI with the matched start and increment as the
1215 // one we wanted to create.
1216 bool PPCLoopInstrFormPrep::alreadyPrepared(Loop *L, Instruction *MemI,
1217                                            const SCEV *BasePtrStartSCEV,
1218                                            const SCEV *BasePtrIncSCEV,
1219                                            PrepForm Form) {
1220   BasicBlock *BB = MemI->getParent();
1221   if (!BB)
1222     return false;
1223 
1224   BasicBlock *PredBB = L->getLoopPredecessor();
1225   BasicBlock *LatchBB = L->getLoopLatch();
1226 
1227   if (!PredBB || !LatchBB)
1228     return false;
1229 
1230   // Run through the PHIs and see if we have some that looks like a preparation
1231   iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis();
1232   for (auto & CurrentPHI : PHIIter) {
1233     PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI);
1234     if (!CurrentPHINode)
1235       continue;
1236 
1237     if (!SE->isSCEVable(CurrentPHINode->getType()))
1238       continue;
1239 
1240     const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L);
1241 
1242     const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV);
1243     if (!PHIBasePtrSCEV)
1244       continue;
1245 
1246     const SCEVConstant *PHIBasePtrIncSCEV =
1247       dyn_cast<SCEVConstant>(PHIBasePtrSCEV->getStepRecurrence(*SE));
1248     if (!PHIBasePtrIncSCEV)
1249       continue;
1250 
1251     if (CurrentPHINode->getNumIncomingValues() == 2) {
1252       if ((CurrentPHINode->getIncomingBlock(0) == LatchBB &&
1253            CurrentPHINode->getIncomingBlock(1) == PredBB) ||
1254           (CurrentPHINode->getIncomingBlock(1) == LatchBB &&
1255            CurrentPHINode->getIncomingBlock(0) == PredBB)) {
1256         if (PHIBasePtrIncSCEV == BasePtrIncSCEV) {
1257           // The existing PHI (CurrentPHINode) has the same start and increment
1258           // as the PHI that we wanted to create.
1259           if ((Form == UpdateForm || Form == ChainCommoning ) &&
1260               PHIBasePtrSCEV->getStart() == BasePtrStartSCEV) {
1261             ++PHINodeAlreadyExistsUpdate;
1262             return true;
1263           }
1264           if (Form == DSForm || Form == DQForm) {
1265             const SCEVConstant *Diff = dyn_cast<SCEVConstant>(
1266                 SE->getMinusSCEV(PHIBasePtrSCEV->getStart(), BasePtrStartSCEV));
1267             if (Diff && !Diff->getAPInt().urem(Form)) {
1268               if (Form == DSForm)
1269                 ++PHINodeAlreadyExistsDS;
1270               else
1271                 ++PHINodeAlreadyExistsDQ;
1272               return true;
1273             }
1274           }
1275         }
1276       }
1277     }
1278   }
1279   return false;
1280 }
1281 
1282 bool PPCLoopInstrFormPrep::runOnLoop(Loop *L) {
1283   bool MadeChange = false;
1284 
1285   // Only prep. the inner-most loop
1286   if (!L->isInnermost())
1287     return MadeChange;
1288 
1289   // Return if already done enough preparation.
1290   if (SuccPrepCount >= MaxVarsPrep)
1291     return MadeChange;
1292 
1293   LLVM_DEBUG(dbgs() << "PIP: Examining: " << *L << "\n");
1294 
1295   BasicBlock *LoopPredecessor = L->getLoopPredecessor();
1296   // If there is no loop predecessor, or the loop predecessor's terminator
1297   // returns a value (which might contribute to determining the loop's
1298   // iteration space), insert a new preheader for the loop.
1299   if (!LoopPredecessor ||
1300       !LoopPredecessor->getTerminator()->getType()->isVoidTy()) {
1301     LoopPredecessor = InsertPreheaderForLoop(L, DT, LI, nullptr, PreserveLCSSA);
1302     if (LoopPredecessor)
1303       MadeChange = true;
1304   }
1305   if (!LoopPredecessor) {
1306     LLVM_DEBUG(dbgs() << "PIP fails since no predecessor for current loop.\n");
1307     return MadeChange;
1308   }
1309   // Check if a load/store has update form. This lambda is used by function
1310   // collectCandidates which can collect candidates for types defined by lambda.
1311   auto isUpdateFormCandidate = [&](const Instruction *I, Value *PtrValue,
1312                                    const Type *PointerElementType) {
1313     assert((PtrValue && I) && "Invalid parameter!");
1314     // There are no update forms for Altivec vector load/stores.
1315     if (ST && ST->hasAltivec() && PointerElementType->isVectorTy())
1316       return false;
1317     // There are no update forms for P10 lxvp/stxvp intrinsic.
1318     auto *II = dyn_cast<IntrinsicInst>(I);
1319     if (II && ((II->getIntrinsicID() == Intrinsic::ppc_vsx_lxvp) ||
1320                II->getIntrinsicID() == Intrinsic::ppc_vsx_stxvp))
1321       return false;
1322     // See getPreIndexedAddressParts, the displacement for LDU/STDU has to
1323     // be 4's multiple (DS-form). For i64 loads/stores when the displacement
1324     // fits in a 16-bit signed field but isn't a multiple of 4, it will be
1325     // useless and possible to break some original well-form addressing mode
1326     // to make this pre-inc prep for it.
1327     if (PointerElementType->isIntegerTy(64)) {
1328       const SCEV *LSCEV = SE->getSCEVAtScope(const_cast<Value *>(PtrValue), L);
1329       const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV);
1330       if (!LARSCEV || LARSCEV->getLoop() != L)
1331         return false;
1332       if (const SCEVConstant *StepConst =
1333               dyn_cast<SCEVConstant>(LARSCEV->getStepRecurrence(*SE))) {
1334         const APInt &ConstInt = StepConst->getValue()->getValue();
1335         if (ConstInt.isSignedIntN(16) && ConstInt.srem(4) != 0)
1336           return false;
1337       }
1338     }
1339     return true;
1340   };
1341 
1342   // Check if a load/store has DS form.
1343   auto isDSFormCandidate = [](const Instruction *I, Value *PtrValue,
1344                               const Type *PointerElementType) {
1345     assert((PtrValue && I) && "Invalid parameter!");
1346     if (isa<IntrinsicInst>(I))
1347       return false;
1348     return (PointerElementType->isIntegerTy(64)) ||
1349            (PointerElementType->isFloatTy()) ||
1350            (PointerElementType->isDoubleTy()) ||
1351            (PointerElementType->isIntegerTy(32) &&
1352             llvm::any_of(I->users(),
1353                          [](const User *U) { return isa<SExtInst>(U); }));
1354   };
1355 
1356   // Check if a load/store has DQ form.
1357   auto isDQFormCandidate = [&](const Instruction *I, Value *PtrValue,
1358                                const Type *PointerElementType) {
1359     assert((PtrValue && I) && "Invalid parameter!");
1360     // Check if it is a P10 lxvp/stxvp intrinsic.
1361     auto *II = dyn_cast<IntrinsicInst>(I);
1362     if (II)
1363       return II->getIntrinsicID() == Intrinsic::ppc_vsx_lxvp ||
1364              II->getIntrinsicID() == Intrinsic::ppc_vsx_stxvp;
1365     // Check if it is a P9 vector load/store.
1366     return ST && ST->hasP9Vector() && (PointerElementType->isVectorTy());
1367   };
1368 
1369   // Check if a load/store is candidate for chain commoning.
1370   // If the SCEV is only with one ptr operand in its start, we can use that
1371   // start as a chain separator. Mark this load/store as a candidate.
1372   auto isChainCommoningCandidate = [&](const Instruction *I, Value *PtrValue,
1373                                        const Type *PointerElementType) {
1374     const SCEVAddRecExpr *ARSCEV =
1375         cast<SCEVAddRecExpr>(SE->getSCEVAtScope(PtrValue, L));
1376     if (!ARSCEV)
1377       return false;
1378 
1379     if (!ARSCEV->isAffine())
1380       return false;
1381 
1382     const SCEV *Start = ARSCEV->getStart();
1383 
1384     // A single pointer. We can treat it as offset 0.
1385     if (isa<SCEVUnknown>(Start) && Start->getType()->isPointerTy())
1386       return true;
1387 
1388     const SCEVAddExpr *ASCEV = dyn_cast<SCEVAddExpr>(Start);
1389 
1390     // We need a SCEVAddExpr to include both base and offset.
1391     if (!ASCEV)
1392       return false;
1393 
1394     // Make sure there is only one pointer operand(base) and all other operands
1395     // are integer type.
1396     bool SawPointer = false;
1397     for (const SCEV *Op : ASCEV->operands()) {
1398       if (Op->getType()->isPointerTy()) {
1399         if (SawPointer)
1400           return false;
1401         SawPointer = true;
1402       } else if (!Op->getType()->isIntegerTy())
1403         return false;
1404     }
1405 
1406     return SawPointer;
1407   };
1408 
1409   // Check if the diff is a constant type. This is used for update/DS/DQ form
1410   // preparation.
1411   auto isValidConstantDiff = [](const SCEV *Diff) {
1412     return dyn_cast<SCEVConstant>(Diff) != nullptr;
1413   };
1414 
1415   // Make sure the diff between the base and new candidate is required type.
1416   // This is used for chain commoning preparation.
1417   auto isValidChainCommoningDiff = [](const SCEV *Diff) {
1418     assert(Diff && "Invalid Diff!\n");
1419 
1420     // Don't mess up previous dform prepare.
1421     if (isa<SCEVConstant>(Diff))
1422       return false;
1423 
1424     // A single integer type offset.
1425     if (isa<SCEVUnknown>(Diff) && Diff->getType()->isIntegerTy())
1426       return true;
1427 
1428     const SCEVNAryExpr *ADiff = dyn_cast<SCEVNAryExpr>(Diff);
1429     if (!ADiff)
1430       return false;
1431 
1432     for (const SCEV *Op : ADiff->operands())
1433       if (!Op->getType()->isIntegerTy())
1434         return false;
1435 
1436     return true;
1437   };
1438 
1439   HasCandidateForPrepare = false;
1440 
1441   LLVM_DEBUG(dbgs() << "Start to prepare for update form.\n");
1442   // Collect buckets of comparable addresses used by loads and stores for update
1443   // form.
1444   SmallVector<Bucket, 16> UpdateFormBuckets = collectCandidates(
1445       L, isUpdateFormCandidate, isValidConstantDiff, MaxVarsUpdateForm);
1446 
1447   // Prepare for update form.
1448   if (!UpdateFormBuckets.empty())
1449     MadeChange |= updateFormPrep(L, UpdateFormBuckets);
1450   else if (!HasCandidateForPrepare) {
1451     LLVM_DEBUG(
1452         dbgs()
1453         << "No prepare candidates found, stop praparation for current loop!\n");
1454     // If no candidate for preparing, return early.
1455     return MadeChange;
1456   }
1457 
1458   LLVM_DEBUG(dbgs() << "Start to prepare for DS form.\n");
1459   // Collect buckets of comparable addresses used by loads and stores for DS
1460   // form.
1461   SmallVector<Bucket, 16> DSFormBuckets = collectCandidates(
1462       L, isDSFormCandidate, isValidConstantDiff, MaxVarsDSForm);
1463 
1464   // Prepare for DS form.
1465   if (!DSFormBuckets.empty())
1466     MadeChange |= dispFormPrep(L, DSFormBuckets, DSForm);
1467 
1468   LLVM_DEBUG(dbgs() << "Start to prepare for DQ form.\n");
1469   // Collect buckets of comparable addresses used by loads and stores for DQ
1470   // form.
1471   SmallVector<Bucket, 16> DQFormBuckets = collectCandidates(
1472       L, isDQFormCandidate, isValidConstantDiff, MaxVarsDQForm);
1473 
1474   // Prepare for DQ form.
1475   if (!DQFormBuckets.empty())
1476     MadeChange |= dispFormPrep(L, DQFormBuckets, DQForm);
1477 
1478   // Collect buckets of comparable addresses used by loads and stores for chain
1479   // commoning. With chain commoning, we reuse offsets between the chains, so
1480   // the register pressure will be reduced.
1481   if (!EnableChainCommoning) {
1482     LLVM_DEBUG(dbgs() << "Chain commoning is not enabled.\n");
1483     return MadeChange;
1484   }
1485 
1486   LLVM_DEBUG(dbgs() << "Start to prepare for chain commoning.\n");
1487   SmallVector<Bucket, 16> Buckets =
1488       collectCandidates(L, isChainCommoningCandidate, isValidChainCommoningDiff,
1489                         MaxVarsChainCommon);
1490 
1491   // Prepare for chain commoning.
1492   if (!Buckets.empty())
1493     MadeChange |= chainCommoning(L, Buckets);
1494 
1495   return MadeChange;
1496 }
1497