1 //===------ PPCLoopInstrFormPrep.cpp - Loop Instr Form Prep Pass ----------===//
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
7 //===----------------------------------------------------------------------===//
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
12 // Additional PHIs are created for loop induction variables used by load/store
13 // instructions so that preferred addressing modes can be used.
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);
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);
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)
40 // for (int i = 0; i < n; ++i)
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.
49 // Generically, this means transforming loops like this:
53 // A2 = p + base1 + offset
55 // B2 = p + base2 + offset
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);
66 // A1_new = p + base1 // chain 1
67 // B1_new = p + base2 // chain 2, now inside the loop, common offset is
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);
76 //===----------------------------------------------------------------------===//
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"
113 #define DEBUG_TYPE "ppc-loop-instr-form-prep"
115 using namespace llvm
;
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"));
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"));
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."));
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."));
135 // Sum of following 3 per loop thresholds for all loops can not be larger
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 "
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"));
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"));
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"));
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
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 "
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"));
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");
185 struct BucketElement
{
186 BucketElement(const SCEV
*O
, Instruction
*I
) : Offset(O
), Instr(I
) {}
187 BucketElement(Instruction
*I
) : Offset(nullptr), Instr(I
) {}
194 Bucket(const SCEV
*B
, Instruction
*I
)
195 : BaseSCEV(B
), Elements(1, BucketElement(I
)) {
199 // The base of the whole bucket.
200 const SCEV
*BaseSCEV
;
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
205 SmallVector
<BucketElement
, 16> Elements
;
207 // The potential chains size. This is used for chain commoning only.
210 // The base for each potential chain. This is used for chain commoning only.
211 SmallVector
<BucketElement
, 16> ChainBases
;
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
};
220 class PPCLoopInstrFormPrep
: public FunctionPass
{
222 static char ID
; // Pass ID, replacement for typeid
224 PPCLoopInstrFormPrep() : FunctionPass(ID
) {
225 initializePPCLoopInstrFormPrepPass(*PassRegistry::getPassRegistry());
228 PPCLoopInstrFormPrep(PPCTargetMachine
&TM
) : FunctionPass(ID
), TM(&TM
) {
229 initializePPCLoopInstrFormPrepPass(*PassRegistry::getPassRegistry());
232 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
233 AU
.addPreserved
<DominatorTreeWrapperPass
>();
234 AU
.addRequired
<LoopInfoWrapperPass
>();
235 AU
.addPreserved
<LoopInfoWrapperPass
>();
236 AU
.addRequired
<ScalarEvolutionWrapperPass
>();
239 bool runOnFunction(Function
&F
) override
;
242 PPCTargetMachine
*TM
= nullptr;
243 const PPCSubtarget
*ST
;
248 bool HasCandidateForPrepare
;
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
;
256 bool runOnLoop(Loop
*L
);
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
);
263 /// Get the value which defines the increment SCEV \p BasePtrIncSCEV.
264 Value
*getNodeForInc(Loop
*L
, Instruction
*MemI
,
265 const SCEV
*BasePtrIncSCEV
);
267 /// Common chains to reuse offsets for a loop to reduce register pressure.
268 bool chainCommoning(Loop
*L
, SmallVector
<Bucket
, 16> &Buckets
);
270 /// Find out the potential commoning chains and their bases.
271 bool prepareBasesForCommoningChains(Bucket
&BucketChain
);
273 /// Rewrite load/store according to the common chains.
275 rewriteLoadStoresForCommoningChains(Loop
*L
, Bucket
&Bucket
,
276 SmallSet
<BasicBlock
*, 16> &BBChanged
);
278 /// Collect condition matched(\p isValidCandidate() returns true)
279 /// candidates in Loop \p L.
280 SmallVector
<Bucket
, 16> collectCandidates(
282 std::function
<bool(const Instruction
*, Value
*, const Type
*)>
284 std::function
<bool(const SCEV
*)> isValidDiff
,
285 unsigned MaxCandidateNum
);
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
);
294 /// Prepare all candidates in \p Buckets for update form.
295 bool updateFormPrep(Loop
*L
, SmallVector
<Bucket
, 16> &Buckets
);
297 /// Prepare all candidates in \p Buckets for displacement form, now for
299 bool dispFormPrep(Loop
*L
, SmallVector
<Bucket
, 16> &Buckets
, PrepForm Form
);
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
306 /// Return false if no base element found, otherwise return true.
307 bool prepareBaseForDispFormChain(Bucket
&BucketChain
, PrepForm Form
);
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
313 /// Return false if no base element found, otherwise return true.
314 bool prepareBaseForUpdateFormChain(Bucket
&BucketChain
);
316 /// Rewrite load/store instructions in \p BucketChain according to
318 bool rewriteLoadStores(Loop
*L
, Bucket
&BucketChain
,
319 SmallSet
<BasicBlock
*, 16> &BBChanged
,
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
);
328 /// Rewrite for the other load/stores of a chain according to the new \p
331 rewriteForBucketElement(std::pair
<Instruction
*, Instruction
*> Base
,
332 const BucketElement
&Element
, Value
*OffToBase
,
333 SmallPtrSet
<Value
*, 16> &DeletedPtrs
);
336 } // end anonymous namespace
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)
345 static constexpr StringRef PHINodeNameSuffix
= ".phi";
346 static constexpr StringRef CastNodeNameSuffix
= ".cast";
347 static constexpr StringRef GEPNodeIncNameSuffix
= ".inc";
348 static constexpr StringRef GEPNodeOffNameSuffix
= ".off";
350 FunctionPass
*llvm::createPPCLoopInstrFormPrepPass(PPCTargetMachine
&TM
) {
351 return new PPCLoopInstrFormPrep(TM
);
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();
364 static std::string
getInstrName(const Value
*I
, StringRef Suffix
) {
365 assert(I
&& "Invalid paramater!");
367 return (I
->getName() + Suffix
).str();
372 static Value
*getPointerOperandAndType(Value
*MemI
,
373 Type
**PtrElementType
= nullptr) {
375 Value
*PtrValue
= nullptr;
376 Type
*PointerElementType
= nullptr;
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);
393 /*Get ElementType if PtrElementType is not null.*/
395 *PtrElementType
= PointerElementType
;
400 bool PPCLoopInstrFormPrep::runOnFunction(Function
&F
) {
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;
412 bool MadeChange
= false;
415 for (Loop
*L
: depth_first(I
))
416 MadeChange
|= runOnLoop(L
);
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)
434 // base1 = base + offset1
435 // base2 = base + offset3
441 // There is benefit because of reuse of offest 'X'.
443 assert(ChainCommonPrepMinThreshold
>= 4 &&
444 "Thredhold can not be smaller than 4!\n");
445 if (CBucket
.Elements
.size() < ChainCommonPrepMinThreshold
)
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
;
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;
457 // Figure out how many times above FirstOffset is used in the first chain.
458 // Chain number is FirstOffsetReusedCount / FirstOffsetReusedCountInFirstChain
459 unsigned FirstOffsetReusedCountInFirstChain
= 1;
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
++;
470 // For now, if we meet any offset which is not FirstOffset, we assume we
472 // This makes us miss some opportunities.
473 // For example, we can common:
475 // {OffsetA, Offset A, OffsetB, OffsetA, OffsetA, OffsetB}
478 // {{OffsetA, Offset A, OffsetB}, {OffsetA, OffsetA, OffsetB}}
479 // FirstOffsetReusedCount = 4; FirstOffsetReusedCountInFirstChain = 2
481 // But we fail to common:
483 // {OffsetA, OffsetB, OffsetA, OffsetA, OffsetB, OffsetA}
484 // FirstOffsetReusedCount = 4; FirstOffsetReusedCountInFirstChain = 1
486 SawChainSeparater
= true;
489 // FirstOffset is not reused, skip this bucket.
490 if (FirstOffsetReusedCount
== 1)
494 FirstOffsetReusedCount
/ FirstOffsetReusedCountInFirstChain
;
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
);
501 CBucket
.ChainSize
= (unsigned)(EleNum
/ ChainNum
);
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
)
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
))
518 for (unsigned i
= 0; i
< ChainNum
; i
++)
519 CBucket
.ChainBases
.push_back(CBucket
.Elements
[i
* CBucket
.ChainSize
]);
521 LLVM_DEBUG(dbgs() << "Bucket has " << ChainNum
<< " chains.\n");
526 bool PPCLoopInstrFormPrep::chainCommoning(Loop
*L
,
527 SmallVector
<Bucket
, 16> &Buckets
) {
528 bool MadeChange
= false;
533 SmallSet
<BasicBlock
*, 16> BBChanged
;
535 for (auto &Bucket
: Buckets
) {
536 if (prepareBasesForCommoningChains(Bucket
))
537 MadeChange
|= rewriteLoadStoresForCommoningChains(L
, Bucket
, BBChanged
);
541 for (auto *BB
: BBChanged
)
546 bool PPCLoopInstrFormPrep::rewriteLoadStoresForCommoningChains(
547 Loop
*L
, Bucket
&Bucket
, SmallSet
<BasicBlock
*, 16> &BBChanged
) {
548 bool MadeChange
= false;
550 assert(Bucket
.Elements
.size() ==
551 Bucket
.ChainBases
.size() * Bucket
.ChainSize
&&
552 "invalid bucket for chain commoning!\n");
553 SmallPtrSet
<Value
*, 16> DeletedPtrs
;
555 BasicBlock
*Header
= L
->getHeader();
556 BasicBlock
*LoopPredecessor
= L
->getLoopPredecessor();
558 SCEVExpander
SCEVE(*SE
, Header
->getDataLayout(),
559 "loopprepare-chaincommon");
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
)
567 const SCEVAddRecExpr
*BasePtrSCEV
= cast
<SCEVAddRecExpr
>(BaseSCEV
);
569 // Make sure the base is able to expand.
570 if (!SCEVE
.isSafeToExpand(BasePtrSCEV
->getStart()))
573 assert(BasePtrSCEV
->isAffine() &&
574 "Invalid SCEV type for the base ptr for a candidate chain!\n");
576 std::pair
<Instruction
*, Instruction
*> Base
= rewriteForBase(
577 L
, BasePtrSCEV
, Bucket
.Elements
[BaseElemIdx
].Instr
,
578 false /* CanPreInc */, ChainCommoning
, SCEVE
, DeletedPtrs
);
580 if (!Base
.first
|| !Base
.second
)
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
);
588 for (unsigned Idx
= BaseElemIdx
+ 1; Idx
< BaseElemIdx
+ Bucket
.ChainSize
;
590 BucketElement
&I
= Bucket
.Elements
[Idx
];
591 Value
*Ptr
= getPointerOperandAndType(I
.Instr
);
592 assert(Ptr
&& "No pointer operand");
593 if (NewPtrs
.count(Ptr
))
596 const SCEV
*OffsetSCEV
=
597 BaseElemIdx
? SE
->getMinusSCEV(Bucket
.Elements
[Idx
].Offset
,
598 Bucket
.Elements
[BaseElemIdx
].Offset
)
599 : Bucket
.Elements
[Idx
].Offset
;
601 // Make sure offset is able to expand. Only need to check one time as the
602 // offsets are reused between different chains.
604 if (!SCEVE
.isSafeToExpand(OffsetSCEV
))
607 Value
*OffsetValue
= SCEVE
.expandCodeFor(
608 OffsetSCEV
, OffsetSCEV
->getType(), LoopPredecessor
->getTerminator());
610 Instruction
*NewPtr
= rewriteForBucketElement(Base
, Bucket
.Elements
[Idx
],
611 OffsetValue
, DeletedPtrs
);
613 assert(NewPtr
&& "Wrong rewrite!\n");
614 NewPtrs
.insert(NewPtr
);
617 ++ChainCommoningRewritten
;
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.
624 for (auto *Ptr
: DeletedPtrs
) {
625 if (Instruction
*IDel
= dyn_cast
<Instruction
>(Ptr
))
626 BBChanged
.insert(IDel
->getParent());
627 RecursivelyDeleteTriviallyDeadInstructions(Ptr
);
634 // Rewrite the new base according to BasePtrSCEV.
635 // bb.loop.preheader:
638 // %phinode = phi [ %newstart, %bb.loop.preheader ], [ %add, %bb.loop.body ]
640 // %add = getelementptr %phinode, %inc
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
) {
652 LLVM_DEBUG(dbgs() << "PIP: Transforming: " << *BasePtrSCEV
<< "\n");
654 assert(BasePtrSCEV
->getLoop() == L
&& "AddRec for the wrong loop?");
656 Value
*BasePtr
= getPointerOperandAndType(BaseMemI
);
657 assert(BasePtr
&& "No pointer operand");
659 Type
*I8Ty
= Type::getInt8Ty(BaseMemI
->getParent()->getContext());
661 PointerType::get(BaseMemI
->getParent()->getContext(),
662 BasePtr
->getType()->getPointerAddressSpace());
664 bool IsConstantInc
= false;
665 const SCEV
*BasePtrIncSCEV
= BasePtrSCEV
->getStepRecurrence(*SE
);
666 Value
*IncNode
= getNodeForInc(L
, BaseMemI
, BasePtrIncSCEV
);
668 const SCEVConstant
*BasePtrIncConstantSCEV
=
669 dyn_cast
<SCEVConstant
>(BasePtrIncSCEV
);
670 if (BasePtrIncConstantSCEV
)
671 IsConstantInc
= true;
673 // No valid representation for the increment.
675 LLVM_DEBUG(dbgs() << "Loop Increasement can not be represented!\n");
676 return std::make_pair(nullptr, nullptr);
679 if (Form
== UpdateForm
&& !IsConstantInc
&& !EnableUpdateFormForNonConstInc
) {
682 << "Update form prepare for non-const increment is not enabled!\n");
683 return std::make_pair(nullptr, nullptr);
686 const SCEV
*BasePtrStartSCEV
= nullptr;
688 assert(SE
->isLoopInvariant(BasePtrIncSCEV
, L
) &&
689 "Increment is not loop invariant!\n");
690 BasePtrStartSCEV
= SE
->getMinusSCEV(BasePtrSCEV
->getStart(),
691 IsConstantInc
? BasePtrIncConstantSCEV
694 BasePtrStartSCEV
= BasePtrSCEV
->getStart();
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);
701 LLVM_DEBUG(dbgs() << "PIP: New start is: " << *BasePtrStartSCEV
<< "\n");
703 BasicBlock
*Header
= L
->getHeader();
704 unsigned HeaderLoopPredCount
= pred_size(Header
);
705 BasicBlock
*LoopPredecessor
= L
->getLoopPredecessor();
707 PHINode
*NewPHI
= PHINode::Create(I8PtrTy
, HeaderLoopPredCount
,
708 getInstrName(BaseMemI
, PHINodeNameSuffix
));
709 NewPHI
->insertBefore(Header
->getFirstNonPHIIt());
711 Value
*BasePtrStart
= SCEVE
.expandCodeFor(BasePtrStartSCEV
, I8PtrTy
,
712 LoopPredecessor
->getTerminator());
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
)
720 NewPHI
->addIncoming(BasePtrStart
, LoopPredecessor
);
723 Instruction
*PtrInc
= nullptr;
724 Instruction
*NewBasePtr
= nullptr;
726 BasicBlock::iterator InsPoint
= Header
->getFirstInsertionPt();
727 PtrInc
= GetElementPtrInst::Create(
728 I8Ty
, NewPHI
, IncNode
, getInstrName(BaseMemI
, GEPNodeIncNameSuffix
),
730 cast
<GetElementPtrInst
>(PtrInc
)->setIsInBounds(IsPtrInBounds(BasePtr
));
731 for (auto *PI
: predecessors(Header
)) {
732 if (PI
== LoopPredecessor
)
735 NewPHI
->addIncoming(PtrInc
, PI
);
737 if (PtrInc
->getType() != BasePtr
->getType())
739 new BitCastInst(PtrInc
, BasePtr
->getType(),
740 getInstrName(PtrInc
, CastNodeNameSuffix
), InsPoint
);
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
)
750 // For the latch predecessor, we need to insert a GEP just before the
751 // terminator to increase the address.
753 BasicBlock::iterator InsPoint
= BB
->getTerminator()->getIterator();
754 PtrInc
= GetElementPtrInst::Create(
755 I8Ty
, NewPHI
, IncNode
, getInstrName(BaseMemI
, GEPNodeIncNameSuffix
),
757 cast
<GetElementPtrInst
>(PtrInc
)->setIsInBounds(IsPtrInBounds(BasePtr
));
759 NewPHI
->addIncoming(PtrInc
, PI
);
762 if (NewPHI
->getType() != BasePtr
->getType())
763 NewBasePtr
= new BitCastInst(NewPHI
, BasePtr
->getType(),
764 getInstrName(NewPHI
, CastNodeNameSuffix
),
765 Header
->getFirstInsertionPt());
770 BasePtr
->replaceAllUsesWith(NewBasePtr
);
772 DeletedPtrs
.insert(BasePtr
);
774 return std::make_pair(NewBasePtr
, PtrInc
);
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");
784 Type
*I8Ty
= Type::getInt8Ty(PtrInc
->getParent()->getContext());
786 Value
*Ptr
= getPointerOperandAndType(Element
.Instr
);
787 assert(Ptr
&& "No pointer operand");
789 Instruction
*RealNewPtr
;
790 if (!Element
.Offset
||
791 (isa
<SCEVConstant
>(Element
.Offset
) &&
792 cast
<SCEVConstant
>(Element
.Offset
)->getValue()->isZero())) {
793 RealNewPtr
= NewBasePtr
;
795 std::optional
<BasicBlock::iterator
> PtrIP
= std::nullopt
;
796 if (Instruction
*I
= dyn_cast
<Instruction
>(Ptr
))
797 PtrIP
= I
->getIterator();
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();
805 PtrIP
= Element
.Instr
->getIterator();
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
));
812 NewPtr
->insertBefore(*(*PtrIP
)->getParent(), *PtrIP
);
814 NewPtr
->insertAfter(cast
<Instruction
>(PtrInc
));
815 NewPtr
->setIsInBounds(IsPtrInBounds(Ptr
));
819 Instruction
*ReplNewPtr
;
820 if (Ptr
->getType() != RealNewPtr
->getType()) {
821 ReplNewPtr
= new BitCastInst(RealNewPtr
, Ptr
->getType(),
822 getInstrName(Ptr
, CastNodeNameSuffix
));
823 ReplNewPtr
->insertAfter(RealNewPtr
);
825 ReplNewPtr
= RealNewPtr
;
827 Ptr
->replaceAllUsesWith(ReplNewPtr
);
828 DeletedPtrs
.insert(Ptr
);
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.");
840 bool FoundBucket
= false;
841 for (auto &B
: Buckets
) {
842 if (cast
<SCEVAddRecExpr
>(B
.BaseSCEV
)->getStepRecurrence(*SE
) !=
843 cast
<SCEVAddRecExpr
>(LSCEV
)->getStepRecurrence(*SE
))
845 const SCEV
*Diff
= SE
->getMinusSCEV(LSCEV
, B
.BaseSCEV
);
846 if (isValidDiff(Diff
)) {
847 B
.Elements
.push_back(BucketElement(Diff
, MemI
));
854 if (Buckets
.size() == MaxCandidateNum
) {
855 LLVM_DEBUG(dbgs() << "Can not prepare more chains, reach maximum limit "
856 << MaxCandidateNum
<< "\n");
859 Buckets
.push_back(Bucket(LSCEV
, MemI
));
863 SmallVector
<Bucket
, 16> PPCLoopInstrFormPrep::collectCandidates(
865 std::function
<bool(const Instruction
*, Value
*, const Type
*)>
867 std::function
<bool(const SCEV
*)> isValidDiff
, unsigned MaxCandidateNum
) {
868 SmallVector
<Bucket
, 16> Buckets
;
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
);
879 if (PtrValue
->getType()->getPointerAddressSpace())
882 if (L
->isLoopInvariant(PtrValue
))
885 const SCEV
*LSCEV
= SE
->getSCEVAtScope(PtrValue
, L
);
886 const SCEVAddRecExpr
*LARSCEV
= dyn_cast
<SCEVAddRecExpr
>(LSCEV
);
887 if (!LARSCEV
|| LARSCEV
->getLoop() != L
)
890 // Mark that we have candidates for preparing.
891 HasCandidateForPrepare
= true;
893 if (isValidCandidate(&J
, PtrValue
, PointerElementType
))
894 addOneCandidate(&J
, LSCEV
, Buckets
, isValidDiff
, MaxCandidateNum
);
899 bool PPCLoopInstrFormPrep::prepareBaseForDispFormChain(Bucket
&BucketChain
,
901 // RemainderOffsetInfo details:
902 // key: value of (Offset urem DispConstraint). For DSForm, it can
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
;
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);
913 unsigned Remainder
= cast
<SCEVConstant
>(BucketChain
.Elements
[j
].Offset
)
916 if (!RemainderOffsetInfo
.contains(Remainder
))
917 RemainderOffsetInfo
[Remainder
] = std::make_pair(j
, 1);
919 RemainderOffsetInfo
[Remainder
].second
++;
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
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
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
;
944 // Abort when there are too few insts with common base.
945 if (RemainderOffsetInfo
[MaxCountRemainder
].second
< DispFormPrepMinThreshold
)
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)
953 // Adjust load/store to the new chosen base.
955 BucketChain
.Elements
[RemainderOffsetInfo
[MaxCountRemainder
].first
].Offset
;
956 BucketChain
.BaseSCEV
= SE
->getAddExpr(BucketChain
.BaseSCEV
, Offset
);
957 for (auto &E
: BucketChain
.Elements
) {
959 E
.Offset
= cast
<SCEVConstant
>(SE
->getMinusSCEV(E
.Offset
, Offset
));
961 E
.Offset
= cast
<SCEVConstant
>(SE
->getNegativeSCEV(Offset
));
964 std::swap(BucketChain
.Elements
[RemainderOffsetInfo
[MaxCountRemainder
].first
],
965 BucketChain
.Elements
[0]);
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
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
)
990 // If we'd otherwise pick the first element anyway, there's nothing to do.
994 // If our chosen element has no offset from the base pointer, there's
996 if (!BucketChain
.Elements
[j
].Offset
||
997 cast
<SCEVConstant
>(BucketChain
.Elements
[j
].Offset
)->isZero())
1000 const SCEV
*Offset
= BucketChain
.Elements
[j
].Offset
;
1001 BucketChain
.BaseSCEV
= SE
->getAddExpr(BucketChain
.BaseSCEV
, Offset
);
1002 for (auto &E
: BucketChain
.Elements
) {
1004 E
.Offset
= cast
<SCEVConstant
>(SE
->getMinusSCEV(E
.Offset
, Offset
));
1006 E
.Offset
= cast
<SCEVConstant
>(SE
->getNegativeSCEV(Offset
));
1009 std::swap(BucketChain
.Elements
[j
], BucketChain
.Elements
[0]);
1015 bool PPCLoopInstrFormPrep::rewriteLoadStores(
1016 Loop
*L
, Bucket
&BucketChain
, SmallSet
<BasicBlock
*, 16> &BBChanged
,
1018 bool MadeChange
= false;
1020 const SCEVAddRecExpr
*BasePtrSCEV
=
1021 cast
<SCEVAddRecExpr
>(BucketChain
.BaseSCEV
);
1022 if (!BasePtrSCEV
->isAffine())
1025 BasicBlock
*Header
= L
->getHeader();
1026 SCEVExpander
SCEVE(*SE
, Header
->getDataLayout(),
1027 "loopprepare-formrewrite");
1028 if (!SCEVE
.isSafeToExpand(BasePtrSCEV
->getStart()))
1031 SmallPtrSet
<Value
*, 16> DeletedPtrs
;
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
1036 bool CanPreInc
= (Form
== UpdateForm
||
1037 ((Form
== DSForm
) &&
1038 isa
<SCEVConstant
>(BasePtrSCEV
->getStepRecurrence(*SE
)) &&
1039 !cast
<SCEVConstant
>(BasePtrSCEV
->getStepRecurrence(*SE
))
1044 std::pair
<Instruction
*, Instruction
*> Base
=
1045 rewriteForBase(L
, BasePtrSCEV
, BucketChain
.Elements
.begin()->Instr
,
1046 CanPreInc
, Form
, SCEVE
, DeletedPtrs
);
1048 if (!Base
.first
|| !Base
.second
)
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
);
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
))
1062 Instruction
*NewPtr
= rewriteForBucketElement(
1064 BE
.Offset
? cast
<SCEVConstant
>(BE
.Offset
)->getValue() : nullptr,
1066 assert(NewPtr
&& "wrong rewrite!\n");
1067 NewPtrs
.insert(NewPtr
);
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.
1074 for (auto *Ptr
: DeletedPtrs
) {
1075 if (Instruction
*IDel
= dyn_cast
<Instruction
>(Ptr
))
1076 BBChanged
.insert(IDel
->getParent());
1077 RecursivelyDeleteTriviallyDeadInstructions(Ptr
);
1084 if (Form
== DSForm
&& !CanPreInc
)
1085 DSFormChainRewritten
++;
1086 else if (Form
== DQForm
)
1087 DQFormChainRewritten
++;
1088 else if (Form
== UpdateForm
|| (Form
== DSForm
&& CanPreInc
))
1089 UpdFormChainRewritten
++;
1094 bool PPCLoopInstrFormPrep::updateFormPrep(Loop
*L
,
1095 SmallVector
<Bucket
, 16> &Buckets
) {
1096 bool MadeChange
= false;
1097 if (Buckets
.empty())
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
);
1107 for (auto *BB
: BBChanged
)
1112 bool PPCLoopInstrFormPrep::dispFormPrep(Loop
*L
,
1113 SmallVector
<Bucket
, 16> &Buckets
,
1115 bool MadeChange
= false;
1117 if (Buckets
.empty())
1120 SmallSet
<BasicBlock
*, 16> BBChanged
;
1121 for (auto &Bucket
: Buckets
) {
1122 if (Bucket
.Elements
.size() < DispFormPrepMinThreshold
)
1124 if (prepareBaseForDispFormChain(Bucket
, Form
))
1125 MadeChange
|= rewriteLoadStores(L
, Bucket
, BBChanged
, Form
);
1129 for (auto *BB
: BBChanged
)
1134 // Find the loop invariant increment node for SCEV BasePtrIncSCEV.
1135 // bb.loop.preheader:
1138 // %phinode = phi [ %start, %bb.loop.preheader ], [ %add, %bb.loop.body ]
1140 // %add = add %phinode, %inc ; %inc is what we want to get.
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();
1149 if (!SE
->isLoopInvariant(BasePtrIncSCEV
, L
))
1152 BasicBlock
*BB
= MemI
->getParent();
1156 BasicBlock
*LatchBB
= L
->getLoopLatch();
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
)
1169 if (!SE
->isSCEVable(CurrentPHINode
->getType()))
1172 const SCEV
*PHISCEV
= SE
->getSCEVAtScope(CurrentPHINode
, L
);
1174 const SCEVAddRecExpr
*PHIBasePtrSCEV
= dyn_cast
<SCEVAddRecExpr
>(PHISCEV
);
1175 if (!PHIBasePtrSCEV
)
1178 const SCEV
*PHIBasePtrIncSCEV
= PHIBasePtrSCEV
->getStepRecurrence(*SE
);
1180 if (!PHIBasePtrIncSCEV
|| (PHIBasePtrIncSCEV
!= BasePtrIncSCEV
))
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)
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);
1193 Instruction
*StrippedI
= dyn_cast
<Instruction
>(StrippedBaseI
);
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);
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
,
1220 BasicBlock
*BB
= MemI
->getParent();
1224 BasicBlock
*PredBB
= L
->getLoopPredecessor();
1225 BasicBlock
*LatchBB
= L
->getLoopLatch();
1227 if (!PredBB
|| !LatchBB
)
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
)
1237 if (!SE
->isSCEVable(CurrentPHINode
->getType()))
1240 const SCEV
*PHISCEV
= SE
->getSCEVAtScope(CurrentPHINode
, L
);
1242 const SCEVAddRecExpr
*PHIBasePtrSCEV
= dyn_cast
<SCEVAddRecExpr
>(PHISCEV
);
1243 if (!PHIBasePtrSCEV
)
1246 const SCEVConstant
*PHIBasePtrIncSCEV
=
1247 dyn_cast
<SCEVConstant
>(PHIBasePtrSCEV
->getStepRecurrence(*SE
));
1248 if (!PHIBasePtrIncSCEV
)
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
;
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
)) {
1269 ++PHINodeAlreadyExistsDS
;
1271 ++PHINodeAlreadyExistsDQ
;
1282 bool PPCLoopInstrFormPrep::runOnLoop(Loop
*L
) {
1283 bool MadeChange
= false;
1285 // Only prep. the inner-most loop
1286 if (!L
->isInnermost())
1289 // Return if already done enough preparation.
1290 if (SuccPrepCount
>= MaxVarsPrep
)
1293 LLVM_DEBUG(dbgs() << "PIP: Examining: " << *L
<< "\n");
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
)
1305 if (!LoopPredecessor
) {
1306 LLVM_DEBUG(dbgs() << "PIP fails since no predecessor for current loop.\n");
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())
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
))
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
)
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)
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
))
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
); }));
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
);
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());
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
));
1379 if (!ARSCEV
->isAffine())
1382 const SCEV
*Start
= ARSCEV
->getStart();
1384 // A single pointer. We can treat it as offset 0.
1385 if (isa
<SCEVUnknown
>(Start
) && Start
->getType()->isPointerTy())
1388 const SCEVAddExpr
*ASCEV
= dyn_cast
<SCEVAddExpr
>(Start
);
1390 // We need a SCEVAddExpr to include both base and offset.
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()) {
1402 } else if (!Op
->getType()->isIntegerTy())
1409 // Check if the diff is a constant type. This is used for update/DS/DQ form
1411 auto isValidConstantDiff
= [](const SCEV
*Diff
) {
1412 return dyn_cast
<SCEVConstant
>(Diff
) != nullptr;
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");
1420 // Don't mess up previous dform prepare.
1421 if (isa
<SCEVConstant
>(Diff
))
1424 // A single integer type offset.
1425 if (isa
<SCEVUnknown
>(Diff
) && Diff
->getType()->isIntegerTy())
1428 const SCEVNAryExpr
*ADiff
= dyn_cast
<SCEVNAryExpr
>(Diff
);
1432 for (const SCEV
*Op
: ADiff
->operands())
1433 if (!Op
->getType()->isIntegerTy())
1439 HasCandidateForPrepare
= false;
1441 LLVM_DEBUG(dbgs() << "Start to prepare for update form.\n");
1442 // Collect buckets of comparable addresses used by loads and stores for update
1444 SmallVector
<Bucket
, 16> UpdateFormBuckets
= collectCandidates(
1445 L
, isUpdateFormCandidate
, isValidConstantDiff
, MaxVarsUpdateForm
);
1447 // Prepare for update form.
1448 if (!UpdateFormBuckets
.empty())
1449 MadeChange
|= updateFormPrep(L
, UpdateFormBuckets
);
1450 else if (!HasCandidateForPrepare
) {
1453 << "No prepare candidates found, stop praparation for current loop!\n");
1454 // If no candidate for preparing, return early.
1458 LLVM_DEBUG(dbgs() << "Start to prepare for DS form.\n");
1459 // Collect buckets of comparable addresses used by loads and stores for DS
1461 SmallVector
<Bucket
, 16> DSFormBuckets
= collectCandidates(
1462 L
, isDSFormCandidate
, isValidConstantDiff
, MaxVarsDSForm
);
1464 // Prepare for DS form.
1465 if (!DSFormBuckets
.empty())
1466 MadeChange
|= dispFormPrep(L
, DSFormBuckets
, DSForm
);
1468 LLVM_DEBUG(dbgs() << "Start to prepare for DQ form.\n");
1469 // Collect buckets of comparable addresses used by loads and stores for DQ
1471 SmallVector
<Bucket
, 16> DQFormBuckets
= collectCandidates(
1472 L
, isDQFormCandidate
, isValidConstantDiff
, MaxVarsDQForm
);
1474 // Prepare for DQ form.
1475 if (!DQFormBuckets
.empty())
1476 MadeChange
|= dispFormPrep(L
, DQFormBuckets
, DQForm
);
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");
1486 LLVM_DEBUG(dbgs() << "Start to prepare for chain commoning.\n");
1487 SmallVector
<Bucket
, 16> Buckets
=
1488 collectCandidates(L
, isChainCommoningCandidate
, isValidChainCommoningDiff
,
1489 MaxVarsChainCommon
);
1491 // Prepare for chain commoning.
1492 if (!Buckets
.empty())
1493 MadeChange
|= chainCommoning(L
, Buckets
);