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