blob: d16397b23edc5974273bf93e9da438ac7860f9cf [file] [log] [blame]
Hal Finkelc9dd0202015-02-05 18:43:00 +00001//===------ PPCLoopPreIncPrep.cpp - Loop Pre-Inc. AM Prep. Pass -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a pass to prepare loops for pre-increment addressing
11// modes. Additional PHIs are created for loop induction variables used by
12// load/store instructions so that the pre-increment forms can be used.
13// Generically, this means transforming loops like this:
14// for (int i = 0; i < n; ++i)
15// array[i] = c;
16// to look like this:
17// T *p = array[-1];
18// for (int i = 0; i < n; ++i)
19// *++p = c;
20//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "ppc-loop-preinc-prep"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000023
Hal Finkelc9dd0202015-02-05 18:43:00 +000024#include "PPC.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000025#include "PPCSubtarget.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000026#include "PPCTargetMachine.h"
Hal Finkelffb460f2015-04-11 00:33:08 +000027#include "llvm/ADT/DepthFirstIterator.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000028#include "llvm/ADT/SmallPtrSet.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000029#include "llvm/ADT/SmallSet.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000030#include "llvm/ADT/SmallVector.h"
Stefan Pintilie9495f332017-08-21 13:36:18 +000031#include "llvm/ADT/Statistic.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000032#include "llvm/Analysis/LoopInfo.h"
33#include "llvm/Analysis/ScalarEvolution.h"
34#include "llvm/Analysis/ScalarEvolutionExpander.h"
35#include "llvm/Analysis/ScalarEvolutionExpressions.h"
David Blaikie31b98d22018-06-04 21:23:21 +000036#include "llvm/Transforms/Utils/Local.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000037#include "llvm/IR/BasicBlock.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000038#include "llvm/IR/CFG.h"
39#include "llvm/IR/Dominators.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000040#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000042#include "llvm/IR/IntrinsicInst.h"
Mehdi Amini46a43552015-03-04 18:43:29 +000043#include "llvm/IR/Module.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000044#include "llvm/IR/Type.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000045#include "llvm/IR/Value.h"
46#include "llvm/Pass.h"
47#include "llvm/Support/Casting.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000048#include "llvm/Support/CommandLine.h"
49#include "llvm/Support/Debug.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000050#include "llvm/Transforms/Scalar.h"
David Blaikiea373d182018-03-28 17:44:36 +000051#include "llvm/Transforms/Utils.h"
Hal Finkelc9dd0202015-02-05 18:43:00 +000052#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hal Finkel291cc7b2015-02-07 07:32:58 +000053#include "llvm/Transforms/Utils/LoopUtils.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000054#include <cassert>
55#include <iterator>
56#include <utility>
57
Hal Finkelc9dd0202015-02-05 18:43:00 +000058using namespace llvm;
59
60// By default, we limit this to creating 16 PHIs (which is a little over half
61// of the allocatable register set).
62static cl::opt<unsigned> MaxVars("ppc-preinc-prep-max-vars",
63 cl::Hidden, cl::init(16),
64 cl::desc("Potential PHI threshold for PPC preinc loop prep"));
65
Stefan Pintilie9495f332017-08-21 13:36:18 +000066STATISTIC(PHINodeAlreadyExists, "PHI node already in pre-increment form");
67
Hal Finkelc9dd0202015-02-05 18:43:00 +000068namespace llvm {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000069
Hal Finkelc9dd0202015-02-05 18:43:00 +000070 void initializePPCLoopPreIncPrepPass(PassRegistry&);
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000071
72} // end namespace llvm
Hal Finkelc9dd0202015-02-05 18:43:00 +000073
74namespace {
75
76 class PPCLoopPreIncPrep : public FunctionPass {
77 public:
78 static char ID; // Pass ID, replacement for typeid
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000079
Eugene Zelenko8187c192017-01-13 00:58:58 +000080 PPCLoopPreIncPrep() : FunctionPass(ID) {
Hal Finkelc9dd0202015-02-05 18:43:00 +000081 initializePPCLoopPreIncPrepPass(*PassRegistry::getPassRegistry());
82 }
Eugene Zelenko8187c192017-01-13 00:58:58 +000083
Hal Finkelc9dd0202015-02-05 18:43:00 +000084 PPCLoopPreIncPrep(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) {
85 initializePPCLoopPreIncPrepPass(*PassRegistry::getPassRegistry());
86 }
87
88 void getAnalysisUsage(AnalysisUsage &AU) const override {
89 AU.addPreserved<DominatorTreeWrapperPass>();
90 AU.addRequired<LoopInfoWrapperPass>();
91 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +000092 AU.addRequired<ScalarEvolutionWrapperPass>();
Hal Finkelc9dd0202015-02-05 18:43:00 +000093 }
94
Stefan Pintilie9495f332017-08-21 13:36:18 +000095 bool alreadyPrepared(Loop *L, Instruction* MemI,
96 const SCEV *BasePtrStartSCEV,
97 const SCEVConstant *BasePtrIncSCEV);
Hal Finkelc9dd0202015-02-05 18:43:00 +000098 bool runOnFunction(Function &F) override;
99
100 bool runOnLoop(Loop *L);
101 void simplifyLoopLatch(Loop *L);
102 bool rotateLoop(Loop *L);
103
104 private:
Eugene Zelenko8187c192017-01-13 00:58:58 +0000105 PPCTargetMachine *TM = nullptr;
Justin Bogner843fb202015-12-15 19:40:57 +0000106 DominatorTree *DT;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000107 LoopInfo *LI;
108 ScalarEvolution *SE;
Justin Bogner843fb202015-12-15 19:40:57 +0000109 bool PreserveLCSSA;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000110 };
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000111
112} // end anonymous namespace
Hal Finkelc9dd0202015-02-05 18:43:00 +0000113
114char PPCLoopPreIncPrep::ID = 0;
Benjamin Kramer970eac42015-02-06 17:51:54 +0000115static const char *name = "Prepare loop for pre-inc. addressing modes";
Hal Finkelc9dd0202015-02-05 18:43:00 +0000116INITIALIZE_PASS_BEGIN(PPCLoopPreIncPrep, DEBUG_TYPE, name, false, false)
117INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000118INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Hal Finkelc9dd0202015-02-05 18:43:00 +0000119INITIALIZE_PASS_END(PPCLoopPreIncPrep, DEBUG_TYPE, name, false, false)
120
121FunctionPass *llvm::createPPCLoopPreIncPrepPass(PPCTargetMachine &TM) {
122 return new PPCLoopPreIncPrep(TM);
123}
124
125namespace {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000126
Hal Finkelf046f722015-11-08 08:04:40 +0000127 struct BucketElement {
128 BucketElement(const SCEVConstant *O, Instruction *I) : Offset(O), Instr(I) {}
129 BucketElement(Instruction *I) : Offset(nullptr), Instr(I) {}
Hal Finkelc9dd0202015-02-05 18:43:00 +0000130
Hal Finkelf046f722015-11-08 08:04:40 +0000131 const SCEVConstant *Offset;
132 Instruction *Instr;
133 };
Hal Finkelc9dd0202015-02-05 18:43:00 +0000134
Hal Finkelf046f722015-11-08 08:04:40 +0000135 struct Bucket {
136 Bucket(const SCEV *B, Instruction *I) : BaseSCEV(B),
137 Elements(1, BucketElement(I)) {}
138
139 const SCEV *BaseSCEV;
140 SmallVector<BucketElement, 16> Elements;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000141 };
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000142
143} // end anonymous namespace
Hal Finkelc9dd0202015-02-05 18:43:00 +0000144
145static bool IsPtrInBounds(Value *BasePtr) {
146 Value *StrippedBasePtr = BasePtr;
147 while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr))
148 StrippedBasePtr = BC->getOperand(0);
149 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(StrippedBasePtr))
150 return GEP->isInBounds();
151
152 return false;
153}
154
155static Value *GetPointerOperand(Value *MemI) {
156 if (LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) {
157 return LMemI->getPointerOperand();
158 } else if (StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) {
159 return SMemI->getPointerOperand();
160 } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(MemI)) {
161 if (IMemI->getIntrinsicID() == Intrinsic::prefetch)
162 return IMemI->getArgOperand(0);
163 }
164
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000165 return nullptr;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000166}
167
168bool PPCLoopPreIncPrep::runOnFunction(Function &F) {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000169 if (skipFunction(F))
170 return false;
171
Hal Finkelc9dd0202015-02-05 18:43:00 +0000172 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000173 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Justin Bogner843fb202015-12-15 19:40:57 +0000174 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
175 DT = DTWP ? &DTWP->getDomTree() : nullptr;
176 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000177
Hal Finkelc9dd0202015-02-05 18:43:00 +0000178 bool MadeChange = false;
179
Hal Finkel5551f252015-04-12 17:18:56 +0000180 for (auto I = LI->begin(), IE = LI->end(); I != IE; ++I)
181 for (auto L = df_begin(*I), LE = df_end(*I); L != LE; ++L)
182 MadeChange |= runOnLoop(*L);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000183
184 return MadeChange;
185}
186
Stefan Pintilie9495f332017-08-21 13:36:18 +0000187// In order to prepare for the pre-increment a PHI is added.
188// This function will check to see if that PHI already exists and will return
189// true if it found an existing PHI with the same start and increment as the
190// one we wanted to create.
191bool PPCLoopPreIncPrep::alreadyPrepared(Loop *L, Instruction* MemI,
192 const SCEV *BasePtrStartSCEV,
193 const SCEVConstant *BasePtrIncSCEV) {
194 BasicBlock *BB = MemI->getParent();
195 if (!BB)
196 return false;
197
198 BasicBlock *PredBB = L->getLoopPredecessor();
199 BasicBlock *LatchBB = L->getLoopLatch();
200
201 if (!PredBB || !LatchBB)
202 return false;
203
204 // Run through the PHIs and see if we have some that looks like a preparation
205 iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis();
206 for (auto & CurrentPHI : PHIIter) {
207 PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI);
208 if (!CurrentPHINode)
209 continue;
210
211 if (!SE->isSCEVable(CurrentPHINode->getType()))
212 continue;
213
214 const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L);
215
216 const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV);
217 if (!PHIBasePtrSCEV)
218 continue;
219
220 const SCEVConstant *PHIBasePtrIncSCEV =
221 dyn_cast<SCEVConstant>(PHIBasePtrSCEV->getStepRecurrence(*SE));
222 if (!PHIBasePtrIncSCEV)
223 continue;
224
225 if (CurrentPHINode->getNumIncomingValues() == 2) {
226 if ( (CurrentPHINode->getIncomingBlock(0) == LatchBB &&
227 CurrentPHINode->getIncomingBlock(1) == PredBB) ||
228 (CurrentPHINode->getIncomingBlock(1) == LatchBB &&
229 CurrentPHINode->getIncomingBlock(0) == PredBB) ) {
230 if (PHIBasePtrSCEV->getStart() == BasePtrStartSCEV &&
231 PHIBasePtrIncSCEV == BasePtrIncSCEV) {
232 // The existing PHI (CurrentPHINode) has the same start and increment
233 // as the PHI that we wanted to create.
234 ++PHINodeAlreadyExists;
235 return true;
236 }
237 }
238 }
239 }
240 return false;
241}
242
Hal Finkelc9dd0202015-02-05 18:43:00 +0000243bool PPCLoopPreIncPrep::runOnLoop(Loop *L) {
244 bool MadeChange = false;
245
Hal Finkelc9dd0202015-02-05 18:43:00 +0000246 // Only prep. the inner-most loop
247 if (!L->empty())
248 return MadeChange;
249
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000250 LLVM_DEBUG(dbgs() << "PIP: Examining: " << *L << "\n");
Hal Finkelffb460f2015-04-11 00:33:08 +0000251
Hal Finkelc9dd0202015-02-05 18:43:00 +0000252 BasicBlock *Header = L->getHeader();
Hal Finkelc9dd0202015-02-05 18:43:00 +0000253
254 const PPCSubtarget *ST =
255 TM ? TM->getSubtargetImpl(*Header->getParent()) : nullptr;
256
Vedant Kumare0b5f862018-05-10 23:01:54 +0000257 unsigned HeaderLoopPredCount = pred_size(Header);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000258
259 // Collect buckets of comparable addresses used by loads and stores.
Hal Finkelc9dd0202015-02-05 18:43:00 +0000260 SmallVector<Bucket, 16> Buckets;
261 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
262 I != IE; ++I) {
263 for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
264 J != JE; ++J) {
265 Value *PtrValue;
266 Instruction *MemI;
267
268 if (LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
269 MemI = LMemI;
270 PtrValue = LMemI->getPointerOperand();
271 } else if (StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
272 MemI = SMemI;
273 PtrValue = SMemI->getPointerOperand();
274 } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(J)) {
275 if (IMemI->getIntrinsicID() == Intrinsic::prefetch) {
276 MemI = IMemI;
277 PtrValue = IMemI->getArgOperand(0);
278 } else continue;
279 } else continue;
280
281 unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace();
282 if (PtrAddrSpace)
283 continue;
284
285 // There are no update forms for Altivec vector load/stores.
286 if (ST && ST->hasAltivec() &&
287 PtrValue->getType()->getPointerElementType()->isVectorTy())
288 continue;
289
290 if (L->isLoopInvariant(PtrValue))
291 continue;
292
Hal Finkelffb460f2015-04-11 00:33:08 +0000293 const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L);
294 if (const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV)) {
295 if (LARSCEV->getLoop() != L)
296 continue;
297 } else {
Hal Finkelc9dd0202015-02-05 18:43:00 +0000298 continue;
Hal Finkelffb460f2015-04-11 00:33:08 +0000299 }
Hal Finkelc9dd0202015-02-05 18:43:00 +0000300
301 bool FoundBucket = false;
Hal Finkelf046f722015-11-08 08:04:40 +0000302 for (auto &B : Buckets) {
303 const SCEV *Diff = SE->getMinusSCEV(LSCEV, B.BaseSCEV);
304 if (const auto *CDiff = dyn_cast<SCEVConstant>(Diff)) {
305 B.Elements.push_back(BucketElement(CDiff, MemI));
306 FoundBucket = true;
307 break;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000308 }
Hal Finkelf046f722015-11-08 08:04:40 +0000309 }
Hal Finkelc9dd0202015-02-05 18:43:00 +0000310
311 if (!FoundBucket) {
Hal Finkelf046f722015-11-08 08:04:40 +0000312 if (Buckets.size() == MaxVars)
313 return MadeChange;
314 Buckets.push_back(Bucket(LSCEV, MemI));
Hal Finkelc9dd0202015-02-05 18:43:00 +0000315 }
316 }
317 }
318
Hal Finkelf046f722015-11-08 08:04:40 +0000319 if (Buckets.empty())
Hal Finkel291cc7b2015-02-07 07:32:58 +0000320 return MadeChange;
321
322 BasicBlock *LoopPredecessor = L->getLoopPredecessor();
323 // If there is no loop predecessor, or the loop predecessor's terminator
324 // returns a value (which might contribute to determining the loop's
325 // iteration space), insert a new preheader for the loop.
326 if (!LoopPredecessor ||
Hal Finkelffb460f2015-04-11 00:33:08 +0000327 !LoopPredecessor->getTerminator()->getType()->isVoidTy()) {
Justin Bogner843fb202015-12-15 19:40:57 +0000328 LoopPredecessor = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);
Hal Finkelffb460f2015-04-11 00:33:08 +0000329 if (LoopPredecessor)
330 MadeChange = true;
331 }
Hal Finkel291cc7b2015-02-07 07:32:58 +0000332 if (!LoopPredecessor)
Hal Finkelc9dd0202015-02-05 18:43:00 +0000333 return MadeChange;
334
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000335 LLVM_DEBUG(dbgs() << "PIP: Found " << Buckets.size() << " buckets\n");
Hal Finkelffb460f2015-04-11 00:33:08 +0000336
Hal Finkelc9dd0202015-02-05 18:43:00 +0000337 SmallSet<BasicBlock *, 16> BBChanged;
338 for (unsigned i = 0, e = Buckets.size(); i != e; ++i) {
339 // The base address of each bucket is transformed into a phi and the others
340 // are rewritten as offsets of that variable.
341
Hal Finkelf046f722015-11-08 08:04:40 +0000342 // We have a choice now of which instruction's memory operand we use as the
343 // base for the generated PHI. Always picking the first instruction in each
344 // bucket does not work well, specifically because that instruction might
345 // be a prefetch (and there are no pre-increment dcbt variants). Otherwise,
346 // the choice is somewhat arbitrary, because the backend will happily
347 // generate direct offsets from both the pre-incremented and
348 // post-incremented pointer values. Thus, we'll pick the first non-prefetch
349 // instruction in each bucket, and adjust the recurrence and other offsets
350 // accordingly.
351 for (int j = 0, je = Buckets[i].Elements.size(); j != je; ++j) {
352 if (auto *II = dyn_cast<IntrinsicInst>(Buckets[i].Elements[j].Instr))
353 if (II->getIntrinsicID() == Intrinsic::prefetch)
354 continue;
355
356 // If we'd otherwise pick the first element anyway, there's nothing to do.
357 if (j == 0)
358 break;
359
360 // If our chosen element has no offset from the base pointer, there's
361 // nothing to do.
362 if (!Buckets[i].Elements[j].Offset ||
363 Buckets[i].Elements[j].Offset->isZero())
364 break;
365
366 const SCEV *Offset = Buckets[i].Elements[j].Offset;
367 Buckets[i].BaseSCEV = SE->getAddExpr(Buckets[i].BaseSCEV, Offset);
368 for (auto &E : Buckets[i].Elements) {
369 if (E.Offset)
370 E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset));
371 else
372 E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
373 }
374
375 std::swap(Buckets[i].Elements[j], Buckets[i].Elements[0]);
376 break;
377 }
378
Hal Finkelc9dd0202015-02-05 18:43:00 +0000379 const SCEVAddRecExpr *BasePtrSCEV =
Hal Finkelf046f722015-11-08 08:04:40 +0000380 cast<SCEVAddRecExpr>(Buckets[i].BaseSCEV);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000381 if (!BasePtrSCEV->isAffine())
382 continue;
383
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000384 LLVM_DEBUG(dbgs() << "PIP: Transforming: " << *BasePtrSCEV << "\n");
Hal Finkelffb460f2015-04-11 00:33:08 +0000385 assert(BasePtrSCEV->getLoop() == L &&
386 "AddRec for the wrong loop?");
387
Hal Finkelf046f722015-11-08 08:04:40 +0000388 // The instruction corresponding to the Bucket's BaseSCEV must be the first
389 // in the vector of elements.
390 Instruction *MemI = Buckets[i].Elements.begin()->Instr;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000391 Value *BasePtr = GetPointerOperand(MemI);
392 assert(BasePtr && "No pointer operand");
393
David Blaikiec95c3ae2015-03-14 21:20:51 +0000394 Type *I8Ty = Type::getInt8Ty(MemI->getParent()->getContext());
Hal Finkelc9dd0202015-02-05 18:43:00 +0000395 Type *I8PtrTy = Type::getInt8PtrTy(MemI->getParent()->getContext(),
396 BasePtr->getType()->getPointerAddressSpace());
397
398 const SCEV *BasePtrStartSCEV = BasePtrSCEV->getStart();
399 if (!SE->isLoopInvariant(BasePtrStartSCEV, L))
400 continue;
401
402 const SCEVConstant *BasePtrIncSCEV =
403 dyn_cast<SCEVConstant>(BasePtrSCEV->getStepRecurrence(*SE));
404 if (!BasePtrIncSCEV)
405 continue;
406 BasePtrStartSCEV = SE->getMinusSCEV(BasePtrStartSCEV, BasePtrIncSCEV);
407 if (!isSafeToExpand(BasePtrStartSCEV, *SE))
408 continue;
409
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000410 LLVM_DEBUG(dbgs() << "PIP: New start is: " << *BasePtrStartSCEV << "\n");
Hal Finkelffb460f2015-04-11 00:33:08 +0000411
Stefan Pintilie9495f332017-08-21 13:36:18 +0000412 if (alreadyPrepared(L, MemI, BasePtrStartSCEV, BasePtrIncSCEV))
413 continue;
414
Hal Finkelc9dd0202015-02-05 18:43:00 +0000415 PHINode *NewPHI = PHINode::Create(I8PtrTy, HeaderLoopPredCount,
416 MemI->hasName() ? MemI->getName() + ".phi" : "",
417 Header->getFirstNonPHI());
418
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000419 SCEVExpander SCEVE(*SE, Header->getModule()->getDataLayout(), "pistart");
Hal Finkelc9dd0202015-02-05 18:43:00 +0000420 Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy,
421 LoopPredecessor->getTerminator());
422
423 // Note that LoopPredecessor might occur in the predecessor list multiple
424 // times, and we need to add it the right number of times.
425 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
426 PI != PE; ++PI) {
427 if (*PI != LoopPredecessor)
428 continue;
429
430 NewPHI->addIncoming(BasePtrStart, LoopPredecessor);
431 }
432
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +0000433 Instruction *InsPoint = &*Header->getFirstInsertionPt();
David Blaikiec95c3ae2015-03-14 21:20:51 +0000434 GetElementPtrInst *PtrInc = GetElementPtrInst::Create(
435 I8Ty, NewPHI, BasePtrIncSCEV->getValue(),
Hal Finkelc9dd0202015-02-05 18:43:00 +0000436 MemI->hasName() ? MemI->getName() + ".inc" : "", InsPoint);
437 PtrInc->setIsInBounds(IsPtrInBounds(BasePtr));
438 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
439 PI != PE; ++PI) {
440 if (*PI == LoopPredecessor)
441 continue;
442
443 NewPHI->addIncoming(PtrInc, *PI);
444 }
445
446 Instruction *NewBasePtr;
447 if (PtrInc->getType() != BasePtr->getType())
448 NewBasePtr = new BitCastInst(PtrInc, BasePtr->getType(),
449 PtrInc->hasName() ? PtrInc->getName() + ".cast" : "", InsPoint);
450 else
451 NewBasePtr = PtrInc;
452
453 if (Instruction *IDel = dyn_cast<Instruction>(BasePtr))
454 BBChanged.insert(IDel->getParent());
455 BasePtr->replaceAllUsesWith(NewBasePtr);
456 RecursivelyDeleteTriviallyDeadInstructions(BasePtr);
457
Hal Finkelf046f722015-11-08 08:04:40 +0000458 // Keep track of the replacement pointer values we've inserted so that we
459 // don't generate more pointer values than necessary.
460 SmallPtrSet<Value *, 16> NewPtrs;
461 NewPtrs.insert( NewBasePtr);
462
463 for (auto I = std::next(Buckets[i].Elements.begin()),
464 IE = Buckets[i].Elements.end(); I != IE; ++I) {
465 Value *Ptr = GetPointerOperand(I->Instr);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000466 assert(Ptr && "No pointer operand");
Hal Finkelf046f722015-11-08 08:04:40 +0000467 if (NewPtrs.count(Ptr))
Hal Finkelc9dd0202015-02-05 18:43:00 +0000468 continue;
469
470 Instruction *RealNewPtr;
Hal Finkelf046f722015-11-08 08:04:40 +0000471 if (!I->Offset || I->Offset->getValue()->isZero()) {
Hal Finkelc9dd0202015-02-05 18:43:00 +0000472 RealNewPtr = NewBasePtr;
473 } else {
474 Instruction *PtrIP = dyn_cast<Instruction>(Ptr);
475 if (PtrIP && isa<Instruction>(NewBasePtr) &&
476 cast<Instruction>(NewBasePtr)->getParent() == PtrIP->getParent())
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000477 PtrIP = nullptr;
Hal Finkelc9dd0202015-02-05 18:43:00 +0000478 else if (isa<PHINode>(PtrIP))
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +0000479 PtrIP = &*PtrIP->getParent()->getFirstInsertionPt();
Hal Finkelc9dd0202015-02-05 18:43:00 +0000480 else if (!PtrIP)
Hal Finkelf046f722015-11-08 08:04:40 +0000481 PtrIP = I->Instr;
David Blaikiec95c3ae2015-03-14 21:20:51 +0000482
483 GetElementPtrInst *NewPtr = GetElementPtrInst::Create(
Hal Finkelf046f722015-11-08 08:04:40 +0000484 I8Ty, PtrInc, I->Offset->getValue(),
485 I->Instr->hasName() ? I->Instr->getName() + ".off" : "", PtrIP);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000486 if (!PtrIP)
487 NewPtr->insertAfter(cast<Instruction>(PtrInc));
488 NewPtr->setIsInBounds(IsPtrInBounds(Ptr));
489 RealNewPtr = NewPtr;
490 }
491
492 if (Instruction *IDel = dyn_cast<Instruction>(Ptr))
493 BBChanged.insert(IDel->getParent());
494
495 Instruction *ReplNewPtr;
496 if (Ptr->getType() != RealNewPtr->getType()) {
497 ReplNewPtr = new BitCastInst(RealNewPtr, Ptr->getType(),
498 Ptr->hasName() ? Ptr->getName() + ".cast" : "");
499 ReplNewPtr->insertAfter(RealNewPtr);
500 } else
501 ReplNewPtr = RealNewPtr;
502
503 Ptr->replaceAllUsesWith(ReplNewPtr);
504 RecursivelyDeleteTriviallyDeadInstructions(Ptr);
505
Hal Finkelf046f722015-11-08 08:04:40 +0000506 NewPtrs.insert(RealNewPtr);
Hal Finkelc9dd0202015-02-05 18:43:00 +0000507 }
508
509 MadeChange = true;
510 }
511
512 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
513 I != IE; ++I) {
514 if (BBChanged.count(*I))
515 DeleteDeadPHIs(*I);
516 }
517
518 return MadeChange;
519}