1 //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This transformation analyzes and transforms the induction variables (and
11 // computations derived from them) into simpler forms suitable for subsequent
12 // analysis and transformation.
14 // This transformation makes the following changes to each loop with an
15 // identifiable induction variable:
16 // 1. All loops are transformed to have a SINGLE canonical induction variable
17 // which starts at zero and steps by one.
18 // 2. The canonical induction variable is guaranteed to be the first PHI node
19 // in the loop header block.
20 // 3. Any pointer arithmetic recurrences are raised to use array subscripts.
22 // If the trip count of a loop is computable, this pass also makes the following
24 // 1. The exit condition for the loop is canonicalized to compare the
25 // induction value against the exit value. This turns loops like:
26 // 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
27 // 2. Any use outside of the loop of an expression derived from the indvar
28 // is changed to compute the derived value outside of the loop, eliminating
29 // the dependence on the exit value of the induction variable. If the only
30 // purpose of the loop is to compute the exit value of some derived
31 // expression, this transformation will make the loop dead.
33 // This transformation should be followed by strength reduction after all of the
34 // desired loop transformations have been performed. Additionally, on targets
35 // where it is profitable, the loop could be transformed to count down to zero
36 // (the "do loop" optimization).
38 //===----------------------------------------------------------------------===//
40 #define DEBUG_TYPE "indvars"
41 #include "llvm/Transforms/Scalar.h"
42 #include "llvm/BasicBlock.h"
43 #include "llvm/Constants.h"
44 #include "llvm/Instructions.h"
45 #include "llvm/Type.h"
46 #include "llvm/Analysis/ScalarEvolutionExpander.h"
47 #include "llvm/Analysis/LoopInfo.h"
48 #include "llvm/Analysis/LoopPass.h"
49 #include "llvm/Support/CFG.h"
50 #include "llvm/Support/Compiler.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/GetElementPtrTypeIterator.h"
53 #include "llvm/Transforms/Utils/Local.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/ADT/SmallVector.h"
56 #include "llvm/ADT/Statistic.h"
59 STATISTIC(NumRemoved , "Number of aux indvars removed");
60 STATISTIC(NumPointer , "Number of pointer indvars promoted");
61 STATISTIC(NumInserted, "Number of canonical indvars added");
62 STATISTIC(NumReplaced, "Number of exit values replaced");
63 STATISTIC(NumLFTR , "Number of loop exit tests replaced");
66 class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
72 static char ID; // Pass identification, replacement for typeid
73 IndVarSimplify() : LoopPass(&ID) {}
75 bool runOnLoop(Loop *L, LPPassManager &LPM);
76 bool doInitialization(Loop *L, LPPassManager &LPM);
77 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 AU.addRequired<ScalarEvolution>();
79 AU.addRequiredID(LCSSAID);
80 AU.addRequiredID(LoopSimplifyID);
81 AU.addRequired<LoopInfo>();
82 AU.addPreservedID(LoopSimplifyID);
83 AU.addPreservedID(LCSSAID);
89 void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
90 std::set<Instruction*> &DeadInsts);
91 Instruction *LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
93 void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
95 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
97 void OptimizeCanonicalIVType(Loop *L);
101 char IndVarSimplify::ID = 0;
102 static RegisterPass<IndVarSimplify>
103 X("indvars", "Canonicalize Induction Variables");
105 LoopPass *llvm::createIndVarSimplifyPass() {
106 return new IndVarSimplify();
109 /// DeleteTriviallyDeadInstructions - If any of the instructions is the
110 /// specified set are trivially dead, delete them and see if this makes any of
111 /// their operands subsequently dead.
112 void IndVarSimplify::
113 DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
114 while (!Insts.empty()) {
115 Instruction *I = *Insts.begin();
116 Insts.erase(Insts.begin());
117 if (isInstructionTriviallyDead(I)) {
118 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
119 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
121 SE->deleteValueFromRecords(I);
122 DOUT << "INDVARS: Deleting: " << *I;
123 I->eraseFromParent();
130 /// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer
131 /// recurrence. If so, change it into an integer recurrence, permitting
132 /// analysis by the SCEV routines.
133 void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
134 BasicBlock *Preheader,
135 std::set<Instruction*> &DeadInsts) {
136 assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!");
137 unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader);
138 unsigned BackedgeIdx = PreheaderIdx^1;
139 if (GetElementPtrInst *GEPI =
140 dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx)))
141 if (GEPI->getOperand(0) == PN) {
142 assert(GEPI->getNumOperands() == 2 && "GEP types must match!");
143 DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI;
145 // Okay, we found a pointer recurrence. Transform this pointer
146 // recurrence into an integer recurrence. Compute the value that gets
147 // added to the pointer at every iteration.
148 Value *AddedVal = GEPI->getOperand(1);
150 // Insert a new integer PHI node into the top of the block.
151 PHINode *NewPhi = PHINode::Create(AddedVal->getType(),
152 PN->getName()+".rec", PN);
153 NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader);
155 // Create the new add instruction.
156 Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal,
157 GEPI->getName()+".rec", GEPI);
158 NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
160 // Update the existing GEP to use the recurrence.
161 GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx));
163 // Update the GEP to use the new recurrence we just inserted.
164 GEPI->setOperand(1, NewAdd);
166 // If the incoming value is a constant expr GEP, try peeling out the array
167 // 0 index if possible to make things simpler.
168 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0)))
169 if (CE->getOpcode() == Instruction::GetElementPtr) {
170 unsigned NumOps = CE->getNumOperands();
171 assert(NumOps > 1 && "CE folding didn't work!");
172 if (CE->getOperand(NumOps-1)->isNullValue()) {
173 // Check to make sure the last index really is an array index.
174 gep_type_iterator GTI = gep_type_begin(CE);
175 for (unsigned i = 1, e = CE->getNumOperands()-1;
178 if (isa<SequentialType>(*GTI)) {
179 // Pull the last index out of the constant expr GEP.
180 SmallVector<Value*, 8> CEIdxs(CE->op_begin()+1, CE->op_end()-1);
181 Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0),
185 Idx[0] = Constant::getNullValue(Type::Int32Ty);
187 GetElementPtrInst *NGEPI = GetElementPtrInst::Create(
189 GEPI->getName(), GEPI);
190 SE->deleteValueFromRecords(GEPI);
191 GEPI->replaceAllUsesWith(NGEPI);
192 GEPI->eraseFromParent();
199 // Finally, if there are any other users of the PHI node, we must
200 // insert a new GEP instruction that uses the pre-incremented version
201 // of the induction amount.
202 if (!PN->use_empty()) {
203 BasicBlock::iterator InsertPos = PN; ++InsertPos;
204 while (isa<PHINode>(InsertPos)) ++InsertPos;
206 GetElementPtrInst::Create(PN->getIncomingValue(PreheaderIdx),
207 NewPhi, "", InsertPos);
208 PreInc->takeName(PN);
209 PN->replaceAllUsesWith(PreInc);
212 // Delete the old PHI for sure, and the GEP if its otherwise unused.
213 DeadInsts.insert(PN);
220 /// LinearFunctionTestReplace - This method rewrites the exit condition of the
221 /// loop to be a canonical != comparison against the incremented loop induction
222 /// variable. This pass is able to rewrite the exit tests of any loop where the
223 /// SCEV analysis can determine a loop-invariant trip count of the loop, which
224 /// is actually a much broader range than just linear tests.
226 /// This method returns a "potentially dead" instruction whose computation chain
227 /// should be deleted when convenient.
228 Instruction *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
229 SCEV *IterationCount,
231 // Find the exit block for the loop. We can currently only handle loops with
233 SmallVector<BasicBlock*, 8> ExitBlocks;
234 L->getExitBlocks(ExitBlocks);
235 if (ExitBlocks.size() != 1) return 0;
236 BasicBlock *ExitBlock = ExitBlocks[0];
238 // Make sure there is only one predecessor block in the loop.
239 BasicBlock *ExitingBlock = 0;
240 for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
242 if (L->contains(*PI)) {
243 if (ExitingBlock == 0)
246 return 0; // Multiple exits from loop to this block.
248 assert(ExitingBlock && "Loop info is broken");
250 if (!isa<BranchInst>(ExitingBlock->getTerminator()))
251 return 0; // Can't rewrite non-branch yet
252 BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator());
253 assert(BI->isConditional() && "Must be conditional to be part of loop!");
255 Instruction *PotentiallyDeadInst = dyn_cast<Instruction>(BI->getCondition());
257 // If the exiting block is not the same as the backedge block, we must compare
258 // against the preincremented value, otherwise we prefer to compare against
259 // the post-incremented value.
260 BasicBlock *Header = L->getHeader();
261 pred_iterator HPI = pred_begin(Header);
262 assert(HPI != pred_end(Header) && "Loop with zero preds???");
263 if (!L->contains(*HPI)) ++HPI;
264 assert(HPI != pred_end(Header) && L->contains(*HPI) &&
265 "No backedge in loop?");
267 SCEVHandle TripCount = IterationCount;
269 if (*HPI == ExitingBlock) {
270 // The IterationCount expression contains the number of times that the
271 // backedge actually branches to the loop header. This is one less than the
272 // number of times the loop executes, so add one to it.
273 ConstantInt *OneC = ConstantInt::get(IterationCount->getType(), 1);
274 TripCount = SE->getAddExpr(IterationCount, SE->getConstant(OneC));
275 IndVar = L->getCanonicalInductionVariableIncrement();
277 // We have to use the preincremented value...
278 IndVar = L->getCanonicalInductionVariable();
281 DOUT << "INDVARS: LFTR: TripCount = " << *TripCount
282 << " IndVar = " << *IndVar << "\n";
284 // Expand the code for the iteration count into the preheader of the loop.
285 BasicBlock *Preheader = L->getLoopPreheader();
286 Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator());
288 // Insert a new icmp_ne or icmp_eq instruction before the branch.
289 ICmpInst::Predicate Opcode;
290 if (L->contains(BI->getSuccessor(0)))
291 Opcode = ICmpInst::ICMP_NE;
293 Opcode = ICmpInst::ICMP_EQ;
295 Value *Cond = new ICmpInst(Opcode, IndVar, ExitCnt, "exitcond", BI);
296 BI->setCondition(Cond);
299 return PotentiallyDeadInst;
303 /// RewriteLoopExitValues - Check to see if this loop has a computable
304 /// loop-invariant execution count. If so, this means that we can compute the
305 /// final value of any expressions that are recurrent in the loop, and
306 /// substitute the exit values from the loop into any instructions outside of
307 /// the loop that use the final values of the current expressions.
308 void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) {
309 BasicBlock *Preheader = L->getLoopPreheader();
311 // Scan all of the instructions in the loop, looking at those that have
312 // extra-loop users and which are recurrences.
313 SCEVExpander Rewriter(*SE, *LI);
315 // We insert the code into the preheader of the loop if the loop contains
316 // multiple exit blocks, or in the exit block if there is exactly one.
317 BasicBlock *BlockToInsertInto;
318 SmallVector<BasicBlock*, 8> ExitBlocks;
319 L->getUniqueExitBlocks(ExitBlocks);
320 if (ExitBlocks.size() == 1)
321 BlockToInsertInto = ExitBlocks[0];
323 BlockToInsertInto = Preheader;
324 BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
326 bool HasConstantItCount = isa<SCEVConstant>(IterationCount);
328 std::set<Instruction*> InstructionsToDelete;
329 std::map<Instruction*, Value*> ExitValues;
331 // Find all values that are computed inside the loop, but used outside of it.
332 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
333 // the exit blocks of the loop to find them.
334 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
335 BasicBlock *ExitBB = ExitBlocks[i];
337 // If there are no PHI nodes in this exit block, then no values defined
338 // inside the loop are used on this path, skip it.
339 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
342 unsigned NumPreds = PN->getNumIncomingValues();
344 // Iterate over all of the PHI nodes.
345 BasicBlock::iterator BBI = ExitBB->begin();
346 while ((PN = dyn_cast<PHINode>(BBI++))) {
348 // Iterate over all of the values in all the PHI nodes.
349 for (unsigned i = 0; i != NumPreds; ++i) {
350 // If the value being merged in is not integer or is not defined
351 // in the loop, skip it.
352 Value *InVal = PN->getIncomingValue(i);
353 if (!isa<Instruction>(InVal) ||
354 // SCEV only supports integer expressions for now.
355 !isa<IntegerType>(InVal->getType()))
358 // If this pred is for a subloop, not L itself, skip it.
359 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
360 continue; // The Block is in a subloop, skip it.
362 // Check that InVal is defined in the loop.
363 Instruction *Inst = cast<Instruction>(InVal);
364 if (!L->contains(Inst->getParent()))
367 // We require that this value either have a computable evolution or that
368 // the loop have a constant iteration count. In the case where the loop
369 // has a constant iteration count, we can sometimes force evaluation of
370 // the exit value through brute force.
371 SCEVHandle SH = SE->getSCEV(Inst);
372 if (!SH->hasComputableLoopEvolution(L) && !HasConstantItCount)
373 continue; // Cannot get exit evolution for the loop value.
375 // Okay, this instruction has a user outside of the current loop
376 // and varies predictably *inside* the loop. Evaluate the value it
377 // contains when the loop exits, if possible.
378 SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
379 if (isa<SCEVCouldNotCompute>(ExitValue) ||
380 !ExitValue->isLoopInvariant(L))
386 // See if we already computed the exit value for the instruction, if so,
388 Value *&ExitVal = ExitValues[Inst];
390 ExitVal = Rewriter.expandCodeFor(ExitValue, InsertPt);
392 DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
393 << " LoopVal = " << *Inst << "\n";
395 PN->setIncomingValue(i, ExitVal);
397 // If this instruction is dead now, schedule it to be removed.
398 if (Inst->use_empty())
399 InstructionsToDelete.insert(Inst);
401 // See if this is a single-entry LCSSA PHI node. If so, we can (and
403 // the PHI entirely. This is safe, because the NewVal won't be variant
404 // in the loop, so we don't need an LCSSA phi node anymore.
406 SE->deleteValueFromRecords(PN);
407 PN->replaceAllUsesWith(ExitVal);
408 PN->eraseFromParent();
415 DeleteTriviallyDeadInstructions(InstructionsToDelete);
418 bool IndVarSimplify::doInitialization(Loop *L, LPPassManager &LPM) {
421 // First step. Check to see if there are any trivial GEP pointer recurrences.
422 // If there are, change them into integer recurrences, permitting analysis by
423 // the SCEV routines.
425 BasicBlock *Header = L->getHeader();
426 BasicBlock *Preheader = L->getLoopPreheader();
427 SE = &LPM.getAnalysis<ScalarEvolution>();
429 std::set<Instruction*> DeadInsts;
430 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
431 PHINode *PN = cast<PHINode>(I);
432 if (isa<PointerType>(PN->getType()))
433 EliminatePointerRecurrence(PN, Preheader, DeadInsts);
436 if (!DeadInsts.empty())
437 DeleteTriviallyDeadInstructions(DeadInsts);
442 bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
445 LI = &getAnalysis<LoopInfo>();
446 SE = &getAnalysis<ScalarEvolution>();
449 BasicBlock *Header = L->getHeader();
450 std::set<Instruction*> DeadInsts;
452 // Verify the input to the pass in already in LCSSA form.
453 assert(L->isLCSSAForm());
455 // Check to see if this loop has a computable loop-invariant execution count.
456 // If so, this means that we can compute the final value of any expressions
457 // that are recurrent in the loop, and substitute the exit values from the
458 // loop into any instructions outside of the loop that use the final values of
459 // the current expressions.
461 SCEVHandle IterationCount = SE->getIterationCount(L);
462 if (!isa<SCEVCouldNotCompute>(IterationCount))
463 RewriteLoopExitValues(L, IterationCount);
465 // Next, analyze all of the induction variables in the loop, canonicalizing
466 // auxillary induction variables.
467 std::vector<std::pair<PHINode*, SCEVHandle> > IndVars;
469 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
470 PHINode *PN = cast<PHINode>(I);
471 if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable!
472 SCEVHandle SCEV = SE->getSCEV(PN);
473 if (SCEV->hasComputableLoopEvolution(L))
474 // FIXME: It is an extremely bad idea to indvar substitute anything more
475 // complex than affine induction variables. Doing so will put expensive
476 // polynomial evaluations inside of the loop, and the str reduction pass
477 // currently can only reduce affine polynomials. For now just disable
478 // indvar subst on anything more complex than an affine addrec.
479 if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV))
481 IndVars.push_back(std::make_pair(PN, SCEV));
485 // If there are no induction variables in the loop, there is nothing more to
487 if (IndVars.empty()) {
488 // Actually, if we know how many times the loop iterates, lets insert a
489 // canonical induction variable to help subsequent passes.
490 if (!isa<SCEVCouldNotCompute>(IterationCount)) {
491 SCEVExpander Rewriter(*SE, *LI);
492 Rewriter.getOrInsertCanonicalInductionVariable(L,
493 IterationCount->getType());
494 if (Instruction *I = LinearFunctionTestReplace(L, IterationCount,
496 std::set<Instruction*> InstructionsToDelete;
497 InstructionsToDelete.insert(I);
498 DeleteTriviallyDeadInstructions(InstructionsToDelete);
504 // Compute the type of the largest recurrence expression.
506 const Type *LargestType = IndVars[0].first->getType();
507 bool DifferingSizes = false;
508 for (unsigned i = 1, e = IndVars.size(); i != e; ++i) {
509 const Type *Ty = IndVars[i].first->getType();
511 Ty->getPrimitiveSizeInBits() != LargestType->getPrimitiveSizeInBits();
512 if (Ty->getPrimitiveSizeInBits() > LargestType->getPrimitiveSizeInBits())
516 // Create a rewriter object which we'll use to transform the code with.
517 SCEVExpander Rewriter(*SE, *LI);
519 // Now that we know the largest of of the induction variables in this loop,
520 // insert a canonical induction variable of the largest size.
521 Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
524 DOUT << "INDVARS: New CanIV: " << *IndVar;
526 if (!isa<SCEVCouldNotCompute>(IterationCount)) {
527 IterationCount = SE->getTruncateOrZeroExtend(IterationCount, LargestType);
528 if (Instruction *DI = LinearFunctionTestReplace(L, IterationCount,Rewriter))
529 DeadInsts.insert(DI);
532 // Now that we have a canonical induction variable, we can rewrite any
533 // recurrences in terms of the induction variable. Start with the auxillary
534 // induction variables, and recursively rewrite any of their uses.
535 BasicBlock::iterator InsertPt = Header->getFirstNonPHI();
537 // If there were induction variables of other sizes, cast the primary
538 // induction variable to the right size for them, avoiding the need for the
539 // code evaluation methods to insert induction variables of different sizes.
540 if (DifferingSizes) {
541 SmallVector<unsigned,4> InsertedSizes;
542 InsertedSizes.push_back(LargestType->getPrimitiveSizeInBits());
543 for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
544 unsigned ithSize = IndVars[i].first->getType()->getPrimitiveSizeInBits();
545 if (std::find(InsertedSizes.begin(), InsertedSizes.end(), ithSize)
546 == InsertedSizes.end()) {
547 PHINode *PN = IndVars[i].first;
548 InsertedSizes.push_back(ithSize);
549 Instruction *New = new TruncInst(IndVar, PN->getType(), "indvar",
551 Rewriter.addInsertedValue(New, SE->getSCEV(New));
552 DOUT << "INDVARS: Made trunc IV for " << *PN
553 << " NewVal = " << *New << "\n";
558 // Rewrite all induction variables in terms of the canonical induction
560 while (!IndVars.empty()) {
561 PHINode *PN = IndVars.back().first;
562 Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt);
563 DOUT << "INDVARS: Rewrote IV '" << *IndVars.back().second << "' " << *PN
564 << " into = " << *NewVal << "\n";
565 NewVal->takeName(PN);
567 // Replace the old PHI Node with the inserted computation.
568 PN->replaceAllUsesWith(NewVal);
569 DeadInsts.insert(PN);
576 // Now replace all derived expressions in the loop body with simpler
578 for (LoopInfo::block_iterator I = L->block_begin(), E = L->block_end();
581 if (LI->getLoopFor(BB) == L) { // Not in a subloop...
582 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
583 if (I->getType()->isInteger() && // Is an integer instruction
585 !Rewriter.isInsertedInstruction(I)) {
586 SCEVHandle SH = SE->getSCEV(I);
587 Value *V = Rewriter.expandCodeFor(SH, I, I->getType());
589 if (isa<Instruction>(V))
591 I->replaceAllUsesWith(V);
601 DeleteTriviallyDeadInstructions(DeadInsts);
602 OptimizeCanonicalIVType(L);
603 assert(L->isLCSSAForm());
607 /// OptimizeCanonicalIVType - If loop induction variable is always
608 /// sign or zero extended then extend the type of the induction
610 void IndVarSimplify::OptimizeCanonicalIVType(Loop *L) {
611 PHINode *PH = L->getCanonicalInductionVariable();
614 // Check loop iteration count.
615 SCEVHandle IC = SE->getIterationCount(L);
616 if (isa<SCEVCouldNotCompute>(IC)) return;
617 SCEVConstant *IterationCount = dyn_cast<SCEVConstant>(IC);
618 if (!IterationCount) return;
620 unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
621 unsigned BackEdge = IncomingEdge^1;
623 // Check IV uses. If all IV uses are either SEXT or ZEXT (except
624 // IV increment instruction) then this IV is suitable for this
627 BinaryOperator *Incr = NULL;
628 const Type *NewType = NULL;
629 for(Value::use_iterator UI = PH->use_begin(), UE = PH->use_end();
631 const Type *CandidateType = NULL;
632 if (ZExtInst *ZI = dyn_cast<ZExtInst>(UI))
633 CandidateType = ZI->getDestTy();
634 else if (SExtInst *SI = dyn_cast<SExtInst>(UI)) {
635 CandidateType = SI->getDestTy();
638 else if ((Incr = dyn_cast<BinaryOperator>(UI))) {
639 // Validate IV increment instruction.
640 if (PH->getIncomingValue(BackEdge) == Incr)
643 if (!CandidateType) {
648 NewType = CandidateType;
649 else if (NewType != CandidateType) {
655 // IV uses are not suitable then avoid this transformation.
656 if (!NewType || !Incr)
659 // IV increment instruction has two uses, one is loop exit condition
660 // and second is the IV (phi node) itself.
661 ICmpInst *Exit = NULL;
662 for(Value::use_iterator II = Incr->use_begin(), IE = Incr->use_end();
664 if (PH == *II) continue;
665 Exit = dyn_cast<ICmpInst>(*II);
669 ConstantInt *EV = dyn_cast<ConstantInt>(Exit->getOperand(0));
671 EV = dyn_cast<ConstantInt>(Exit->getOperand(1));
674 // Check iteration count max value to avoid loops that wrap around IV.
675 APInt ICount = IterationCount->getValue()->getValue();
676 if (ICount.isNegative()) return;
677 uint32_t BW = PH->getType()->getPrimitiveSizeInBits();
678 APInt Max = (isSEXT ? APInt::getSignedMaxValue(BW) : APInt::getMaxValue(BW));
679 if (ICount.getZExtValue() > Max.getZExtValue()) return;
683 SCEVExpander Rewriter(*SE, *LI);
684 Value *NewIV = Rewriter.getOrInsertCanonicalInductionVariable(L,NewType);
685 PHINode *NewPH = cast<PHINode>(NewIV);
686 Instruction *NewIncr = cast<Instruction>(NewPH->getIncomingValue(BackEdge));
688 // Replace all SEXT or ZEXT uses.
689 SmallVector<Instruction *, 4> PHUses;
690 for(Value::use_iterator UI = PH->use_begin(), UE = PH->use_end();
692 Instruction *I = cast<Instruction>(UI);
695 while (!PHUses.empty()){
696 Instruction *Use = PHUses.back(); PHUses.pop_back();
697 if (Incr == Use) continue;
699 SE->deleteValueFromRecords(Use);
700 Use->replaceAllUsesWith(NewIV);
701 Use->eraseFromParent();
704 // Replace exit condition.
705 ConstantInt *NEV = ConstantInt::get(NewType, EV->getZExtValue());
706 Instruction *NE = new ICmpInst(Exit->getPredicate(),
707 NewIncr, NEV, "new.exit",
708 Exit->getParent()->getTerminator());
709 SE->deleteValueFromRecords(Exit);
710 Exit->replaceAllUsesWith(NE);
711 Exit->eraseFromParent();
713 // Remove old IV and increment instructions.
714 SE->deleteValueFromRecords(PH);
715 PH->removeIncomingValue((unsigned)0);
716 PH->removeIncomingValue((unsigned)0);
717 SE->deleteValueFromRecords(Incr);
718 Incr->eraseFromParent();