Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/PowerPC/PPCLoopPreIncPrep.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ PPCLoopPreIncPrep.cpp - Loop Pre-Inc. AM 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 pre-increment addressing
10
// modes. Additional PHIs are created for loop induction variables used by
11
// load/store instructions so that the pre-increment forms can be used.
12
// Generically, this means transforming loops like this:
13
//   for (int i = 0; i < n; ++i)
14
//     array[i] = c;
15
// to look like this:
16
//   T *p = array[-1];
17
//   for (int i = 0; i < n; ++i)
18
//     *++p = c;
19
//===----------------------------------------------------------------------===//
20
21
#define DEBUG_TYPE "ppc-loop-preinc-prep"
22
23
#include "PPC.h"
24
#include "PPCSubtarget.h"
25
#include "PPCTargetMachine.h"
26
#include "llvm/ADT/DepthFirstIterator.h"
27
#include "llvm/ADT/SmallPtrSet.h"
28
#include "llvm/ADT/SmallSet.h"
29
#include "llvm/ADT/SmallVector.h"
30
#include "llvm/ADT/Statistic.h"
31
#include "llvm/Analysis/LoopInfo.h"
32
#include "llvm/Analysis/ScalarEvolution.h"
33
#include "llvm/Analysis/ScalarEvolutionExpander.h"
34
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
35
#include "llvm/Transforms/Utils/Local.h"
36
#include "llvm/IR/BasicBlock.h"
37
#include "llvm/IR/CFG.h"
38
#include "llvm/IR/Dominators.h"
39
#include "llvm/IR/Instruction.h"
40
#include "llvm/IR/Instructions.h"
41
#include "llvm/IR/IntrinsicInst.h"
42
#include "llvm/IR/Module.h"
43
#include "llvm/IR/Type.h"
44
#include "llvm/IR/Value.h"
45
#include "llvm/Pass.h"
46
#include "llvm/Support/Casting.h"
47
#include "llvm/Support/CommandLine.h"
48
#include "llvm/Support/Debug.h"
49
#include "llvm/Transforms/Scalar.h"
50
#include "llvm/Transforms/Utils.h"
51
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
52
#include "llvm/Transforms/Utils/LoopUtils.h"
53
#include <cassert>
54
#include <iterator>
55
#include <utility>
56
57
using namespace llvm;
58
59
// By default, we limit this to creating 16 PHIs (which is a little over half
60
// of the allocatable register set).
61
static cl::opt<unsigned> MaxVars("ppc-preinc-prep-max-vars",
62
                                 cl::Hidden, cl::init(16),
63
  cl::desc("Potential PHI threshold for PPC preinc loop prep"));
64
65
STATISTIC(PHINodeAlreadyExists, "PHI node already in pre-increment form");
66
67
namespace {
68
69
  class PPCLoopPreIncPrep : public FunctionPass {
70
  public:
71
    static char ID; // Pass ID, replacement for typeid
72
73
0
    PPCLoopPreIncPrep() : FunctionPass(ID) {
74
0
      initializePPCLoopPreIncPrepPass(*PassRegistry::getPassRegistry());
75
0
    }
76
77
1.66k
    PPCLoopPreIncPrep(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) {
78
1.66k
      initializePPCLoopPreIncPrepPass(*PassRegistry::getPassRegistry());
79
1.66k
    }
80
81
1.64k
    void getAnalysisUsage(AnalysisUsage &AU) const override {
82
1.64k
      AU.addPreserved<DominatorTreeWrapperPass>();
83
1.64k
      AU.addRequired<LoopInfoWrapperPass>();
84
1.64k
      AU.addPreserved<LoopInfoWrapperPass>();
85
1.64k
      AU.addRequired<ScalarEvolutionWrapperPass>();
86
1.64k
    }
87
88
    bool alreadyPrepared(Loop *L, Instruction* MemI,
89
                         const SCEV *BasePtrStartSCEV,
90
                         const SCEVConstant *BasePtrIncSCEV);
91
    bool runOnFunction(Function &F) override;
92
93
    bool runOnLoop(Loop *L);
94
    void simplifyLoopLatch(Loop *L);
95
    bool rotateLoop(Loop *L);
96
97
  private:
98
    PPCTargetMachine *TM = nullptr;
99
    DominatorTree *DT;
100
    LoopInfo *LI;
101
    ScalarEvolution *SE;
102
    bool PreserveLCSSA;
103
  };
104
105
} // end anonymous namespace
106
107
char PPCLoopPreIncPrep::ID = 0;
108
static const char *name = "Prepare loop for pre-inc. addressing modes";
109
101k
INITIALIZE_PASS_BEGIN(PPCLoopPreIncPrep, DEBUG_TYPE, name, false, false)
110
101k
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
111
101k
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
112
101k
INITIALIZE_PASS_END(PPCLoopPreIncPrep, DEBUG_TYPE, name, false, false)
113
114
1.66k
FunctionPass *llvm::createPPCLoopPreIncPrepPass(PPCTargetMachine &TM) {
115
1.66k
  return new PPCLoopPreIncPrep(TM);
116
1.66k
}
117
118
namespace {
119
120
  struct BucketElement {
121
383
    BucketElement(const SCEVConstant *O, Instruction *I) : Offset(O), Instr(I) {}
122
205
    BucketElement(Instruction *I) : Offset(nullptr), Instr(I) {}
123
124
    const SCEVConstant *Offset;
125
    Instruction *Instr;
126
  };
127
128
  struct Bucket {
129
    Bucket(const SCEV *B, Instruction *I) : BaseSCEV(B),
130
205
                                            Elements(1, BucketElement(I)) {}
131
132
    const SCEV *BaseSCEV;
133
    SmallVector<BucketElement, 16> Elements;
134
  };
135
136
} // end anonymous namespace
137
138
396
static bool IsPtrInBounds(Value *BasePtr) {
139
396
  Value *StrippedBasePtr = BasePtr;
140
487
  while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr))
141
91
    StrippedBasePtr = BC->getOperand(0);
142
396
  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(StrippedBasePtr))
143
280
    return GEP->isInBounds();
144
116
145
116
  return false;
146
116
}
147
148
515
static Value *GetPointerOperand(Value *MemI) {
149
515
  if (LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) {
150
255
    return LMemI->getPointerOperand();
151
260
  } else if (StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) {
152
241
    return SMemI->getPointerOperand();
153
241
  } else 
if (IntrinsicInst *19
IMemI19
= dyn_cast<IntrinsicInst>(MemI)) {
154
19
    if (IMemI->getIntrinsicID() == Intrinsic::prefetch)
155
19
      return IMemI->getArgOperand(0);
156
0
  }
157
0
158
0
  return nullptr;
159
0
}
160
161
10.3k
bool PPCLoopPreIncPrep::runOnFunction(Function &F) {
162
10.3k
  if (skipFunction(F))
163
1
    return false;
164
10.3k
165
10.3k
  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
166
10.3k
  SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
167
10.3k
  auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
168
10.3k
  DT = DTWP ? &DTWP->getDomTree() : 
nullptr0
;
169
10.3k
  PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
170
10.3k
171
10.3k
  bool MadeChange = false;
172
10.3k
173
10.7k
  for (auto I = LI->begin(), IE = LI->end(); I != IE; 
++I377
)
174
835
    
for (auto L = df_begin(*I), LE = df_end(*I); 377
L != LE;
++L458
)
175
458
      MadeChange |= runOnLoop(*L);
176
10.3k
177
10.3k
  return MadeChange;
178
10.3k
}
179
180
// In order to prepare for the pre-increment a PHI is added.
181
// This function will check to see if that PHI already exists and will return
182
//  true if it found an existing PHI with the same start and increment as the
183
//  one we wanted to create.
184
bool PPCLoopPreIncPrep::alreadyPrepared(Loop *L, Instruction* MemI,
185
                                        const SCEV *BasePtrStartSCEV,
186
195
                                        const SCEVConstant *BasePtrIncSCEV) {
187
195
  BasicBlock *BB = MemI->getParent();
188
195
  if (!BB)
189
0
    return false;
190
195
191
195
  BasicBlock *PredBB = L->getLoopPredecessor();
192
195
  BasicBlock *LatchBB = L->getLoopLatch();
193
195
194
195
  if (!PredBB || !LatchBB)
195
0
    return false;
196
195
197
195
  // Run through the PHIs and see if we have some that looks like a preparation
198
195
  iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis();
199
397
  for (auto & CurrentPHI : PHIIter) {
200
397
    PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI);
201
397
    if (!CurrentPHINode)
202
0
      continue;
203
397
204
397
    if (!SE->isSCEVable(CurrentPHINode->getType()))
205
4
      continue;
206
393
207
393
    const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L);
208
393
209
393
    const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV);
210
393
    if (!PHIBasePtrSCEV)
211
35
      continue;
212
358
213
358
    const SCEVConstant *PHIBasePtrIncSCEV =
214
358
      dyn_cast<SCEVConstant>(PHIBasePtrSCEV->getStepRecurrence(*SE));
215
358
    if (!PHIBasePtrIncSCEV)
216
5
      continue;
217
353
218
353
    if (CurrentPHINode->getNumIncomingValues() == 2) {
219
353
      if ( (CurrentPHINode->getIncomingBlock(0) == LatchBB &&
220
353
            
CurrentPHINode->getIncomingBlock(1) == PredBB210
) ||
221
353
            
(143
CurrentPHINode->getIncomingBlock(1) == LatchBB143
&&
222
353
            
CurrentPHINode->getIncomingBlock(0) == PredBB143
) ) {
223
353
        if (PHIBasePtrSCEV->getStart() == BasePtrStartSCEV &&
224
353
            
PHIBasePtrIncSCEV == BasePtrIncSCEV25
) {
225
25
          // The existing PHI (CurrentPHINode) has the same start and increment
226
25
          //  as the PHI that we wanted to create.
227
25
          ++PHINodeAlreadyExists;
228
25
          return true;
229
25
        }
230
353
      }
231
353
    }
232
353
  }
233
195
  
return false170
;
234
195
}
235
236
458
bool PPCLoopPreIncPrep::runOnLoop(Loop *L) {
237
458
  bool MadeChange = false;
238
458
239
458
  // Only prep. the inner-most loop
240
458
  if (!L->empty())
241
70
    return MadeChange;
242
388
243
388
  LLVM_DEBUG(dbgs() << "PIP: Examining: " << *L << "\n");
244
388
245
388
  BasicBlock *Header = L->getHeader();
246
388
247
388
  const PPCSubtarget *ST =
248
388
    TM ? TM->getSubtargetImpl(*Header->getParent()) : 
nullptr0
;
249
388
250
388
  unsigned HeaderLoopPredCount = pred_size(Header);
251
388
252
388
  // Collect buckets of comparable addresses used by loads and stores.
253
388
  SmallVector<Bucket, 16> Buckets;
254
388
  for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
255
925
       I != IE; 
++I537
) {
256
537
    for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
257
5.88k
        J != JE; 
++J5.34k
) {
258
5.34k
      Value *PtrValue;
259
5.34k
      Instruction *MemI;
260
5.34k
261
5.34k
      if (LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
262
451
        MemI = LMemI;
263
451
        PtrValue = LMemI->getPointerOperand();
264
4.89k
      } else if (StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
265
433
        MemI = SMemI;
266
433
        PtrValue = SMemI->getPointerOperand();
267
4.46k
      } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(J)) {
268
27
        if (IMemI->getIntrinsicID() == Intrinsic::prefetch) {
269
19
          MemI = IMemI;
270
19
          PtrValue = IMemI->getArgOperand(0);
271
19
        } else 
continue8
;
272
4.43k
      } else continue;
273
903
274
903
      unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace();
275
903
      if (PtrAddrSpace)
276
0
        continue;
277
903
278
903
      // There are no update forms for Altivec vector load/stores.
279
903
      if (ST && ST->hasAltivec() &&
280
903
          
PtrValue->getType()->getPointerElementType()->isVectorTy()600
)
281
143
        continue;
282
760
283
760
      if (L->isLoopInvariant(PtrValue))
284
88
        continue;
285
672
286
672
      const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L);
287
672
      if (const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV)) {
288
621
        if (LARSCEV->getLoop() != L)
289
1
          continue;
290
620
        // See getPreIndexedAddressParts, the displacement for LDU/STDU has to
291
620
        // be 4's multiple (DS-form). For i64 loads/stores when the displacement
292
620
        // fits in a 16-bit signed field but isn't a multiple of 4, it will be
293
620
        // useless and possible to break some original well-form addressing mode
294
620
        // to make this pre-inc prep for it.
295
620
        if (PtrValue->getType()->getPointerElementType()->isIntegerTy(64)) {
296
47
          if (const SCEVConstant *StepConst =
297
47
                  dyn_cast<SCEVConstant>(LARSCEV->getStepRecurrence(*SE))) {
298
47
            const APInt &ConstInt = StepConst->getValue()->getValue();
299
47
            if (ConstInt.isSignedIntN(16) && 
ConstInt.srem(4) != 043
)
300
32
              continue;
301
51
          }
302
47
        }
303
51
      } else {
304
51
        continue;
305
51
      }
306
588
307
588
      bool FoundBucket = false;
308
588
      for (auto &B : Buckets) {
309
444
        const SCEV *Diff = SE->getMinusSCEV(LSCEV, B.BaseSCEV);
310
444
        if (const auto *CDiff = dyn_cast<SCEVConstant>(Diff)) {
311
383
          B.Elements.push_back(BucketElement(CDiff, MemI));
312
383
          FoundBucket = true;
313
383
          break;
314
383
        }
315
444
      }
316
588
317
588
      if (!FoundBucket) {
318
205
        if (Buckets.size() == MaxVars)
319
0
          return MadeChange;
320
205
        Buckets.push_back(Bucket(LSCEV, MemI));
321
205
      }
322
588
    }
323
537
  }
324
388
325
388
  if (Buckets.empty())
326
201
    return MadeChange;
327
187
328
187
  BasicBlock *LoopPredecessor = L->getLoopPredecessor();
329
187
  // If there is no loop predecessor, or the loop predecessor's terminator
330
187
  // returns a value (which might contribute to determining the loop's
331
187
  // iteration space), insert a new preheader for the loop.
332
187
  if (!LoopPredecessor ||
333
187
      !LoopPredecessor->getTerminator()->getType()->isVoidTy()) {
334
0
    LoopPredecessor = InsertPreheaderForLoop(L, DT, LI, nullptr, PreserveLCSSA);
335
0
    if (LoopPredecessor)
336
0
      MadeChange = true;
337
0
  }
338
187
  if (!LoopPredecessor)
339
0
    return MadeChange;
340
187
341
187
  LLVM_DEBUG(dbgs() << "PIP: Found " << Buckets.size() << " buckets\n");
342
187
343
187
  SmallSet<BasicBlock *, 16> BBChanged;
344
392
  for (unsigned i = 0, e = Buckets.size(); i != e; 
++i205
) {
345
205
    // The base address of each bucket is transformed into a phi and the others
346
205
    // are rewritten as offsets of that variable.
347
205
348
205
    // We have a choice now of which instruction's memory operand we use as the
349
205
    // base for the generated PHI. Always picking the first instruction in each
350
205
    // bucket does not work well, specifically because that instruction might
351
205
    // be a prefetch (and there are no pre-increment dcbt variants). Otherwise,
352
205
    // the choice is somewhat arbitrary, because the backend will happily
353
205
    // generate direct offsets from both the pre-incremented and
354
205
    // post-incremented pointer values. Thus, we'll pick the first non-prefetch
355
205
    // instruction in each bucket, and adjust the recurrence and other offsets
356
205
    // accordingly.
357
218
    for (int j = 0, je = Buckets[i].Elements.size(); j != je; 
++j13
) {
358
218
      if (auto *II = dyn_cast<IntrinsicInst>(Buckets[i].Elements[j].Instr))
359
13
        if (II->getIntrinsicID() == Intrinsic::prefetch)
360
13
          continue;
361
205
362
205
      // If we'd otherwise pick the first element anyway, there's nothing to do.
363
205
      if (j == 0)
364
192
        break;
365
13
366
13
      // If our chosen element has no offset from the base pointer, there's
367
13
      // nothing to do.
368
13
      if (!Buckets[i].Elements[j].Offset ||
369
13
          Buckets[i].Elements[j].Offset->isZero())
370
0
        break;
371
13
372
13
      const SCEV *Offset = Buckets[i].Elements[j].Offset;
373
13
      Buckets[i].BaseSCEV = SE->getAddExpr(Buckets[i].BaseSCEV, Offset);
374
45
      for (auto &E : Buckets[i].Elements) {
375
45
        if (E.Offset)
376
32
          E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset));
377
13
        else
378
13
          E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
379
45
      }
380
13
381
13
      std::swap(Buckets[i].Elements[j], Buckets[i].Elements[0]);
382
13
      break;
383
13
    }
384
205
385
205
    const SCEVAddRecExpr *BasePtrSCEV =
386
205
      cast<SCEVAddRecExpr>(Buckets[i].BaseSCEV);
387
205
    if (!BasePtrSCEV->isAffine())
388
0
      continue;
389
205
390
205
    LLVM_DEBUG(dbgs() << "PIP: Transforming: " << *BasePtrSCEV << "\n");
391
205
    assert(BasePtrSCEV->getLoop() == L &&
392
205
           "AddRec for the wrong loop?");
393
205
394
205
    // The instruction corresponding to the Bucket's BaseSCEV must be the first
395
205
    // in the vector of elements.
396
205
    Instruction *MemI = Buckets[i].Elements.begin()->Instr;
397
205
    Value *BasePtr = GetPointerOperand(MemI);
398
205
    assert(BasePtr && "No pointer operand");
399
205
400
205
    Type *I8Ty = Type::getInt8Ty(MemI->getParent()->getContext());
401
205
    Type *I8PtrTy = Type::getInt8PtrTy(MemI->getParent()->getContext(),
402
205
      BasePtr->getType()->getPointerAddressSpace());
403
205
404
205
    const SCEV *BasePtrStartSCEV = BasePtrSCEV->getStart();
405
205
    if (!SE->isLoopInvariant(BasePtrStartSCEV, L))
406
0
      continue;
407
205
408
205
    const SCEVConstant *BasePtrIncSCEV =
409
205
      dyn_cast<SCEVConstant>(BasePtrSCEV->getStepRecurrence(*SE));
410
205
    if (!BasePtrIncSCEV)
411
10
      continue;
412
195
    BasePtrStartSCEV = SE->getMinusSCEV(BasePtrStartSCEV, BasePtrIncSCEV);
413
195
    if (!isSafeToExpand(BasePtrStartSCEV, *SE))
414
0
      continue;
415
195
416
195
    LLVM_DEBUG(dbgs() << "PIP: New start is: " << *BasePtrStartSCEV << "\n");
417
195
418
195
    if (alreadyPrepared(L, MemI, BasePtrStartSCEV, BasePtrIncSCEV))
419
25
      continue;
420
170
421
170
    PHINode *NewPHI = PHINode::Create(I8PtrTy, HeaderLoopPredCount,
422
170
      MemI->hasName() ? 
MemI->getName() + ".phi"4
:
""166
,
423
170
      Header->getFirstNonPHI());
424
170
425
170
    SCEVExpander SCEVE(*SE, Header->getModule()->getDataLayout(), "pistart");
426
170
    Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy,
427
170
      LoopPredecessor->getTerminator());
428
170
429
170
    // Note that LoopPredecessor might occur in the predecessor list multiple
430
170
    // times, and we need to add it the right number of times.
431
170
    for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
432
510
         PI != PE; 
++PI340
) {
433
340
      if (*PI != LoopPredecessor)
434
170
        continue;
435
170
436
170
      NewPHI->addIncoming(BasePtrStart, LoopPredecessor);
437
170
    }
438
170
439
170
    Instruction *InsPoint = &*Header->getFirstInsertionPt();
440
170
    GetElementPtrInst *PtrInc = GetElementPtrInst::Create(
441
170
        I8Ty, NewPHI, BasePtrIncSCEV->getValue(),
442
170
        MemI->hasName() ? 
MemI->getName() + ".inc"4
:
""166
, InsPoint);
443
170
    PtrInc->setIsInBounds(IsPtrInBounds(BasePtr));
444
170
    for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
445
510
         PI != PE; 
++PI340
) {
446
340
      if (*PI == LoopPredecessor)
447
170
        continue;
448
170
449
170
      NewPHI->addIncoming(PtrInc, *PI);
450
170
    }
451
170
452
170
    Instruction *NewBasePtr;
453
170
    if (PtrInc->getType() != BasePtr->getType())
454
123
      NewBasePtr = new BitCastInst(PtrInc, BasePtr->getType(),
455
123
        PtrInc->hasName() ? 
PtrInc->getName() + ".cast"4
:
""119
, InsPoint);
456
47
    else
457
47
      NewBasePtr = PtrInc;
458
170
459
170
    if (Instruction *IDel = dyn_cast<Instruction>(BasePtr))
460
170
      BBChanged.insert(IDel->getParent());
461
170
    BasePtr->replaceAllUsesWith(NewBasePtr);
462
170
    RecursivelyDeleteTriviallyDeadInstructions(BasePtr);
463
170
464
170
    // Keep track of the replacement pointer values we've inserted so that we
465
170
    // don't generate more pointer values than necessary.
466
170
    SmallPtrSet<Value *, 16> NewPtrs;
467
170
    NewPtrs.insert( NewBasePtr);
468
170
469
170
    for (auto I = std::next(Buckets[i].Elements.begin()),
470
480
         IE = Buckets[i].Elements.end(); I != IE; 
++I310
) {
471
310
      Value *Ptr = GetPointerOperand(I->Instr);
472
310
      assert(Ptr && "No pointer operand");
473
310
      if (NewPtrs.count(Ptr))
474
84
        continue;
475
226
476
226
      Instruction *RealNewPtr;
477
226
      if (!I->Offset || I->Offset->getValue()->isZero()) {
478
0
        RealNewPtr = NewBasePtr;
479
226
      } else {
480
226
        Instruction *PtrIP = dyn_cast<Instruction>(Ptr);
481
226
        if (PtrIP && isa<Instruction>(NewBasePtr) &&
482
226
            cast<Instruction>(NewBasePtr)->getParent() == PtrIP->getParent())
483
226
          PtrIP = nullptr;
484
0
        else if (isa<PHINode>(PtrIP))
485
0
          PtrIP = &*PtrIP->getParent()->getFirstInsertionPt();
486
0
        else if (!PtrIP)
487
0
          PtrIP = I->Instr;
488
226
489
226
        GetElementPtrInst *NewPtr = GetElementPtrInst::Create(
490
226
            I8Ty, PtrInc, I->Offset->getValue(),
491
226
            I->Instr->hasName() ? 
I->Instr->getName() + ".off"15
:
""211
, PtrIP);
492
226
        if (!PtrIP)
493
226
          NewPtr->insertAfter(cast<Instruction>(PtrInc));
494
226
        NewPtr->setIsInBounds(IsPtrInBounds(Ptr));
495
226
        RealNewPtr = NewPtr;
496
226
      }
497
226
498
226
      if (Instruction *IDel = dyn_cast<Instruction>(Ptr))
499
226
        BBChanged.insert(IDel->getParent());
500
226
501
226
      Instruction *ReplNewPtr;
502
226
      if (Ptr->getType() != RealNewPtr->getType()) {
503
207
        ReplNewPtr = new BitCastInst(RealNewPtr, Ptr->getType(),
504
207
          Ptr->hasName() ? 
Ptr->getName() + ".cast"192
:
""15
);
505
207
        ReplNewPtr->insertAfter(RealNewPtr);
506
207
      } else
507
19
        ReplNewPtr = RealNewPtr;
508
226
509
226
      Ptr->replaceAllUsesWith(ReplNewPtr);
510
226
      RecursivelyDeleteTriviallyDeadInstructions(Ptr);
511
226
512
226
      NewPtrs.insert(RealNewPtr);
513
226
    }
514
170
515
170
    MadeChange = true;
516
170
  }
517
187
518
187
  for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
519
401
       I != IE; 
++I214
) {
520
214
    if (BBChanged.count(*I))
521
158
      DeleteDeadPHIs(*I);
522
214
  }
523
187
524
187
  return MadeChange;
525
187
}