Merge branch 'master' into msp430
[llvm/msp430.git] / lib / Transforms / Scalar / IndVarSimplify.cpp
blob2287f20086c98e93f50eafc62d150345681f46c1
1 //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
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 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
23 // changes:
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/Dominators.h"
47 #include "llvm/Analysis/IVUsers.h"
48 #include "llvm/Analysis/ScalarEvolutionExpander.h"
49 #include "llvm/Analysis/LoopInfo.h"
50 #include "llvm/Analysis/LoopPass.h"
51 #include "llvm/Support/CFG.h"
52 #include "llvm/Support/Compiler.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/GetElementPtrTypeIterator.h"
55 #include "llvm/Transforms/Utils/Local.h"
56 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
57 #include "llvm/Support/CommandLine.h"
58 #include "llvm/ADT/SmallVector.h"
59 #include "llvm/ADT/SetVector.h"
60 #include "llvm/ADT/Statistic.h"
61 #include "llvm/ADT/STLExtras.h"
62 using namespace llvm;
64 STATISTIC(NumRemoved , "Number of aux indvars removed");
65 STATISTIC(NumInserted, "Number of canonical indvars added");
66 STATISTIC(NumReplaced, "Number of exit values replaced");
67 STATISTIC(NumLFTR , "Number of loop exit tests replaced");
69 namespace {
70 class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
71 IVUsers *IU;
72 LoopInfo *LI;
73 ScalarEvolution *SE;
74 bool Changed;
75 public:
77 static char ID; // Pass identification, replacement for typeid
78 IndVarSimplify() : LoopPass(&ID) {}
80 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.addRequired<DominatorTree>();
84 AU.addRequired<ScalarEvolution>();
85 AU.addRequiredID(LCSSAID);
86 AU.addRequiredID(LoopSimplifyID);
87 AU.addRequired<LoopInfo>();
88 AU.addRequired<IVUsers>();
89 AU.addPreserved<ScalarEvolution>();
90 AU.addPreservedID(LoopSimplifyID);
91 AU.addPreserved<IVUsers>();
92 AU.addPreservedID(LCSSAID);
93 AU.setPreservesCFG();
96 private:
98 void RewriteNonIntegerIVs(Loop *L);
100 ICmpInst *LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount,
101 Value *IndVar,
102 BasicBlock *ExitingBlock,
103 BranchInst *BI,
104 SCEVExpander &Rewriter);
105 void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount);
107 void RewriteIVExpressions(Loop *L, const Type *LargestType,
108 SCEVExpander &Rewriter);
110 void SinkUnusedInvariants(Loop *L, SCEVExpander &Rewriter);
112 void FixUsesBeforeDefs(Loop *L, SCEVExpander &Rewriter);
114 void HandleFloatingPointIV(Loop *L, PHINode *PH);
118 char IndVarSimplify::ID = 0;
119 static RegisterPass<IndVarSimplify>
120 X("indvars", "Canonicalize Induction Variables");
122 Pass *llvm::createIndVarSimplifyPass() {
123 return new IndVarSimplify();
126 /// LinearFunctionTestReplace - This method rewrites the exit condition of the
127 /// loop to be a canonical != comparison against the incremented loop induction
128 /// variable. This pass is able to rewrite the exit tests of any loop where the
129 /// SCEV analysis can determine a loop-invariant trip count of the loop, which
130 /// is actually a much broader range than just linear tests.
131 ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
132 SCEVHandle BackedgeTakenCount,
133 Value *IndVar,
134 BasicBlock *ExitingBlock,
135 BranchInst *BI,
136 SCEVExpander &Rewriter) {
137 // If the exiting block is not the same as the backedge block, we must compare
138 // against the preincremented value, otherwise we prefer to compare against
139 // the post-incremented value.
140 Value *CmpIndVar;
141 SCEVHandle RHS = BackedgeTakenCount;
142 if (ExitingBlock == L->getLoopLatch()) {
143 // Add one to the "backedge-taken" count to get the trip count.
144 // If this addition may overflow, we have to be more pessimistic and
145 // cast the induction variable before doing the add.
146 SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
147 SCEVHandle N =
148 SE->getAddExpr(BackedgeTakenCount,
149 SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
150 if ((isa<SCEVConstant>(N) && !N->isZero()) ||
151 SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
152 // No overflow. Cast the sum.
153 RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
154 } else {
155 // Potential overflow. Cast before doing the add.
156 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
157 IndVar->getType());
158 RHS = SE->getAddExpr(RHS,
159 SE->getIntegerSCEV(1, IndVar->getType()));
162 // The BackedgeTaken expression contains the number of times that the
163 // backedge branches to the loop header. This is one less than the
164 // number of times the loop executes, so use the incremented indvar.
165 CmpIndVar = L->getCanonicalInductionVariableIncrement();
166 } else {
167 // We have to use the preincremented value...
168 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
169 IndVar->getType());
170 CmpIndVar = IndVar;
173 // Expand the code for the iteration count into the preheader of the loop.
174 BasicBlock *Preheader = L->getLoopPreheader();
175 Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(),
176 Preheader->getTerminator());
178 // Insert a new icmp_ne or icmp_eq instruction before the branch.
179 ICmpInst::Predicate Opcode;
180 if (L->contains(BI->getSuccessor(0)))
181 Opcode = ICmpInst::ICMP_NE;
182 else
183 Opcode = ICmpInst::ICMP_EQ;
185 DOUT << "INDVARS: Rewriting loop exit condition to:\n"
186 << " LHS:" << *CmpIndVar // includes a newline
187 << " op:\t"
188 << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
189 << " RHS:\t" << *RHS << "\n";
191 ICmpInst *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI);
193 Instruction *OrigCond = cast<Instruction>(BI->getCondition());
194 OrigCond->replaceAllUsesWith(Cond);
195 RecursivelyDeleteTriviallyDeadInstructions(OrigCond);
197 ++NumLFTR;
198 Changed = true;
199 return Cond;
202 /// RewriteLoopExitValues - Check to see if this loop has a computable
203 /// loop-invariant execution count. If so, this means that we can compute the
204 /// final value of any expressions that are recurrent in the loop, and
205 /// substitute the exit values from the loop into any instructions outside of
206 /// the loop that use the final values of the current expressions.
208 /// This is mostly redundant with the regular IndVarSimplify activities that
209 /// happen later, except that it's more powerful in some cases, because it's
210 /// able to brute-force evaluate arbitrary instructions as long as they have
211 /// constant operands at the beginning of the loop.
212 void IndVarSimplify::RewriteLoopExitValues(Loop *L,
213 const SCEV *BackedgeTakenCount) {
214 // Verify the input to the pass in already in LCSSA form.
215 assert(L->isLCSSAForm());
217 BasicBlock *Preheader = L->getLoopPreheader();
219 // Scan all of the instructions in the loop, looking at those that have
220 // extra-loop users and which are recurrences.
221 SCEVExpander Rewriter(*SE, *LI);
223 // We insert the code into the preheader of the loop if the loop contains
224 // multiple exit blocks, or in the exit block if there is exactly one.
225 BasicBlock *BlockToInsertInto;
226 SmallVector<BasicBlock*, 8> ExitBlocks;
227 L->getUniqueExitBlocks(ExitBlocks);
228 if (ExitBlocks.size() == 1)
229 BlockToInsertInto = ExitBlocks[0];
230 else
231 BlockToInsertInto = Preheader;
232 BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
234 std::map<Instruction*, Value*> ExitValues;
236 // Find all values that are computed inside the loop, but used outside of it.
237 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
238 // the exit blocks of the loop to find them.
239 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
240 BasicBlock *ExitBB = ExitBlocks[i];
242 // If there are no PHI nodes in this exit block, then no values defined
243 // inside the loop are used on this path, skip it.
244 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
245 if (!PN) continue;
247 unsigned NumPreds = PN->getNumIncomingValues();
249 // Iterate over all of the PHI nodes.
250 BasicBlock::iterator BBI = ExitBB->begin();
251 while ((PN = dyn_cast<PHINode>(BBI++))) {
253 // Iterate over all of the values in all the PHI nodes.
254 for (unsigned i = 0; i != NumPreds; ++i) {
255 // If the value being merged in is not integer or is not defined
256 // in the loop, skip it.
257 Value *InVal = PN->getIncomingValue(i);
258 if (!isa<Instruction>(InVal) ||
259 // SCEV only supports integer expressions for now.
260 (!isa<IntegerType>(InVal->getType()) &&
261 !isa<PointerType>(InVal->getType())))
262 continue;
264 // If this pred is for a subloop, not L itself, skip it.
265 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
266 continue; // The Block is in a subloop, skip it.
268 // Check that InVal is defined in the loop.
269 Instruction *Inst = cast<Instruction>(InVal);
270 if (!L->contains(Inst->getParent()))
271 continue;
273 // Okay, this instruction has a user outside of the current loop
274 // and varies predictably *inside* the loop. Evaluate the value it
275 // contains when the loop exits, if possible.
276 SCEVHandle SH = SE->getSCEV(Inst);
277 SCEVHandle ExitValue = SE->getSCEVAtScope(SH, L->getParentLoop());
278 if (isa<SCEVCouldNotCompute>(ExitValue) ||
279 !ExitValue->isLoopInvariant(L))
280 continue;
282 Changed = true;
283 ++NumReplaced;
285 // See if we already computed the exit value for the instruction, if so,
286 // just reuse it.
287 Value *&ExitVal = ExitValues[Inst];
288 if (!ExitVal)
289 ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), InsertPt);
291 DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
292 << " LoopVal = " << *Inst << "\n";
294 PN->setIncomingValue(i, ExitVal);
296 // If this instruction is dead now, delete it.
297 RecursivelyDeleteTriviallyDeadInstructions(Inst);
299 // See if this is a single-entry LCSSA PHI node. If so, we can (and
300 // have to) remove
301 // the PHI entirely. This is safe, because the NewVal won't be variant
302 // in the loop, so we don't need an LCSSA phi node anymore.
303 if (NumPreds == 1) {
304 PN->replaceAllUsesWith(ExitVal);
305 RecursivelyDeleteTriviallyDeadInstructions(PN);
306 break;
313 void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
314 // First step. Check to see if there are any floating-point recurrences.
315 // If there are, change them into integer recurrences, permitting analysis by
316 // the SCEV routines.
318 BasicBlock *Header = L->getHeader();
320 SmallVector<WeakVH, 8> PHIs;
321 for (BasicBlock::iterator I = Header->begin();
322 PHINode *PN = dyn_cast<PHINode>(I); ++I)
323 PHIs.push_back(PN);
325 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
326 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i]))
327 HandleFloatingPointIV(L, PN);
329 // If the loop previously had floating-point IV, ScalarEvolution
330 // may not have been able to compute a trip count. Now that we've done some
331 // re-writing, the trip count may be computable.
332 if (Changed)
333 SE->forgetLoopBackedgeTakenCount(L);
336 bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
337 IU = &getAnalysis<IVUsers>();
338 LI = &getAnalysis<LoopInfo>();
339 SE = &getAnalysis<ScalarEvolution>();
340 Changed = false;
342 // If there are any floating-point recurrences, attempt to
343 // transform them to use integer recurrences.
344 RewriteNonIntegerIVs(L);
346 BasicBlock *Header = L->getHeader();
347 BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null
348 SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
350 // Check to see if this loop has a computable loop-invariant execution count.
351 // If so, this means that we can compute the final value of any expressions
352 // that are recurrent in the loop, and substitute the exit values from the
353 // loop into any instructions outside of the loop that use the final values of
354 // the current expressions.
356 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
357 RewriteLoopExitValues(L, BackedgeTakenCount);
359 // Compute the type of the largest recurrence expression, and decide whether
360 // a canonical induction variable should be inserted.
361 const Type *LargestType = 0;
362 bool NeedCannIV = false;
363 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
364 LargestType = BackedgeTakenCount->getType();
365 LargestType = SE->getEffectiveSCEVType(LargestType);
366 // If we have a known trip count and a single exit block, we'll be
367 // rewriting the loop exit test condition below, which requires a
368 // canonical induction variable.
369 if (ExitingBlock)
370 NeedCannIV = true;
372 for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
373 SCEVHandle Stride = IU->StrideOrder[i];
374 const Type *Ty = SE->getEffectiveSCEVType(Stride->getType());
375 if (!LargestType ||
376 SE->getTypeSizeInBits(Ty) >
377 SE->getTypeSizeInBits(LargestType))
378 LargestType = Ty;
380 std::map<SCEVHandle, IVUsersOfOneStride *>::iterator SI =
381 IU->IVUsesByStride.find(IU->StrideOrder[i]);
382 assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!");
384 if (!SI->second->Users.empty())
385 NeedCannIV = true;
388 // Create a rewriter object which we'll use to transform the code with.
389 SCEVExpander Rewriter(*SE, *LI);
391 // Now that we know the largest of of the induction variable expressions
392 // in this loop, insert a canonical induction variable of the largest size.
393 Value *IndVar = 0;
394 if (NeedCannIV) {
395 IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
396 ++NumInserted;
397 Changed = true;
398 DOUT << "INDVARS: New CanIV: " << *IndVar;
401 // If we have a trip count expression, rewrite the loop's exit condition
402 // using it. We can currently only handle loops with a single exit.
403 ICmpInst *NewICmp = 0;
404 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) {
405 assert(NeedCannIV &&
406 "LinearFunctionTestReplace requires a canonical induction variable");
407 // Can't rewrite non-branch yet.
408 if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
409 NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
410 ExitingBlock, BI, Rewriter);
413 Rewriter.setInsertionPoint(Header->getFirstNonPHI());
415 // Rewrite IV-derived expressions.
416 RewriteIVExpressions(L, LargestType, Rewriter);
418 // Loop-invariant instructions in the preheader that aren't used in the
419 // loop may be sunk below the loop to reduce register pressure.
420 SinkUnusedInvariants(L, Rewriter);
422 // Reorder instructions to avoid use-before-def conditions.
423 FixUsesBeforeDefs(L, Rewriter);
425 // For completeness, inform IVUsers of the IV use in the newly-created
426 // loop exit test instruction.
427 if (NewICmp)
428 IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0)));
430 // Clean up dead instructions.
431 DeleteDeadPHIs(L->getHeader());
432 // Check a post-condition.
433 assert(L->isLCSSAForm() && "Indvars did not leave the loop in lcssa form!");
434 return Changed;
437 void IndVarSimplify::RewriteIVExpressions(Loop *L, const Type *LargestType,
438 SCEVExpander &Rewriter) {
439 SmallVector<WeakVH, 16> DeadInsts;
441 // Rewrite all induction variable expressions in terms of the canonical
442 // induction variable.
444 // If there were induction variables of other sizes or offsets, manually
445 // add the offsets to the primary induction variable and cast, avoiding
446 // the need for the code evaluation methods to insert induction variables
447 // of different sizes.
448 for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
449 SCEVHandle Stride = IU->StrideOrder[i];
451 std::map<SCEVHandle, IVUsersOfOneStride *>::iterator SI =
452 IU->IVUsesByStride.find(IU->StrideOrder[i]);
453 assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!");
454 ilist<IVStrideUse> &List = SI->second->Users;
455 for (ilist<IVStrideUse>::iterator UI = List.begin(),
456 E = List.end(); UI != E; ++UI) {
457 SCEVHandle Offset = UI->getOffset();
458 Value *Op = UI->getOperandValToReplace();
459 Instruction *User = UI->getUser();
460 bool isSigned = UI->isSigned();
462 // Compute the final addrec to expand into code.
463 SCEVHandle AR = IU->getReplacementExpr(*UI);
465 // FIXME: It is an extremely bad idea to indvar substitute anything more
466 // complex than affine induction variables. Doing so will put expensive
467 // polynomial evaluations inside of the loop, and the str reduction pass
468 // currently can only reduce affine polynomials. For now just disable
469 // indvar subst on anything more complex than an affine addrec, unless
470 // it can be expanded to a trivial value.
471 if (!Stride->isLoopInvariant(L) &&
472 !isa<SCEVConstant>(AR) &&
473 L->contains(User->getParent()))
474 continue;
476 Value *NewVal = 0;
477 if (AR->isLoopInvariant(L)) {
478 BasicBlock::iterator I = Rewriter.getInsertionPoint();
479 // Expand loop-invariant values in the loop preheader. They will
480 // be sunk to the exit block later, if possible.
481 NewVal =
482 Rewriter.expandCodeFor(AR, LargestType,
483 L->getLoopPreheader()->getTerminator());
484 Rewriter.setInsertionPoint(I);
485 ++NumReplaced;
486 } else {
487 const Type *IVTy = Offset->getType();
488 const Type *UseTy = Op->getType();
490 // Promote the Offset and Stride up to the canonical induction
491 // variable's bit width.
492 SCEVHandle PromotedOffset = Offset;
493 SCEVHandle PromotedStride = Stride;
494 if (SE->getTypeSizeInBits(IVTy) != SE->getTypeSizeInBits(LargestType)) {
495 // It doesn't matter for correctness whether zero or sign extension
496 // is used here, since the value is truncated away below, but if the
497 // value is signed, sign extension is more likely to be folded.
498 if (isSigned) {
499 PromotedOffset = SE->getSignExtendExpr(PromotedOffset, LargestType);
500 PromotedStride = SE->getSignExtendExpr(PromotedStride, LargestType);
501 } else {
502 PromotedOffset = SE->getZeroExtendExpr(PromotedOffset, LargestType);
503 // If the stride is obviously negative, use sign extension to
504 // produce things like x-1 instead of x+255.
505 if (isa<SCEVConstant>(PromotedStride) &&
506 cast<SCEVConstant>(PromotedStride)
507 ->getValue()->getValue().isNegative())
508 PromotedStride = SE->getSignExtendExpr(PromotedStride,
509 LargestType);
510 else
511 PromotedStride = SE->getZeroExtendExpr(PromotedStride,
512 LargestType);
516 // Create the SCEV representing the offset from the canonical
517 // induction variable, still in the canonical induction variable's
518 // type, so that all expanded arithmetic is done in the same type.
519 SCEVHandle NewAR = SE->getAddRecExpr(SE->getIntegerSCEV(0, LargestType),
520 PromotedStride, L);
521 // Add the PromotedOffset as a separate step, because it may not be
522 // loop-invariant.
523 NewAR = SE->getAddExpr(NewAR, PromotedOffset);
525 // Expand the addrec into instructions.
526 Value *V = Rewriter.expandCodeFor(NewAR, LargestType);
528 // Insert an explicit cast if necessary to truncate the value
529 // down to the original stride type. This is done outside of
530 // SCEVExpander because in SCEV expressions, a truncate of an
531 // addrec is always folded.
532 if (LargestType != IVTy) {
533 if (SE->getTypeSizeInBits(IVTy) != SE->getTypeSizeInBits(LargestType))
534 NewAR = SE->getTruncateExpr(NewAR, IVTy);
535 if (Rewriter.isInsertedExpression(NewAR))
536 V = Rewriter.expandCodeFor(NewAR, IVTy);
537 else {
538 V = Rewriter.InsertCastOfTo(CastInst::getCastOpcode(V, false,
539 IVTy, false),
540 V, IVTy);
541 assert(!isa<SExtInst>(V) && !isa<ZExtInst>(V) &&
542 "LargestType wasn't actually the largest type!");
543 // Force the rewriter to use this trunc whenever this addrec
544 // appears so that it doesn't insert new phi nodes or
545 // arithmetic in a different type.
546 Rewriter.addInsertedValue(V, NewAR);
550 DOUT << "INDVARS: Made offset-and-trunc IV for offset "
551 << *IVTy << " " << *Offset << ": ";
552 DEBUG(WriteAsOperand(*DOUT, V, false));
553 DOUT << "\n";
555 // Now expand it into actual Instructions and patch it into place.
556 NewVal = Rewriter.expandCodeFor(AR, UseTy);
559 // Patch the new value into place.
560 if (Op->hasName())
561 NewVal->takeName(Op);
562 User->replaceUsesOfWith(Op, NewVal);
563 UI->setOperandValToReplace(NewVal);
564 DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *Op
565 << " into = " << *NewVal << "\n";
566 ++NumRemoved;
567 Changed = true;
569 // The old value may be dead now.
570 DeadInsts.push_back(Op);
574 // Now that we're done iterating through lists, clean up any instructions
575 // which are now dead.
576 while (!DeadInsts.empty()) {
577 Instruction *Inst = dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val());
578 if (Inst)
579 RecursivelyDeleteTriviallyDeadInstructions(Inst);
583 /// If there's a single exit block, sink any loop-invariant values that
584 /// were defined in the preheader but not used inside the loop into the
585 /// exit block to reduce register pressure in the loop.
586 void IndVarSimplify::SinkUnusedInvariants(Loop *L, SCEVExpander &Rewriter) {
587 BasicBlock *ExitBlock = L->getExitBlock();
588 if (!ExitBlock) return;
590 Instruction *NonPHI = ExitBlock->getFirstNonPHI();
591 BasicBlock *Preheader = L->getLoopPreheader();
592 BasicBlock::iterator I = Preheader->getTerminator();
593 while (I != Preheader->begin()) {
594 --I;
595 // New instructions were inserted at the end of the preheader. Only
596 // consider those new instructions.
597 if (!Rewriter.isInsertedInstruction(I))
598 break;
599 // Determine if there is a use in or before the loop (direct or
600 // otherwise).
601 bool UsedInLoop = false;
602 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
603 UI != UE; ++UI) {
604 BasicBlock *UseBB = cast<Instruction>(UI)->getParent();
605 if (PHINode *P = dyn_cast<PHINode>(UI)) {
606 unsigned i =
607 PHINode::getIncomingValueNumForOperand(UI.getOperandNo());
608 UseBB = P->getIncomingBlock(i);
610 if (UseBB == Preheader || L->contains(UseBB)) {
611 UsedInLoop = true;
612 break;
615 // If there is, the def must remain in the preheader.
616 if (UsedInLoop)
617 continue;
618 // Otherwise, sink it to the exit block.
619 Instruction *ToMove = I;
620 bool Done = false;
621 if (I != Preheader->begin())
622 --I;
623 else
624 Done = true;
625 ToMove->moveBefore(NonPHI);
626 if (Done)
627 break;
631 /// Re-schedule the inserted instructions to put defs before uses. This
632 /// fixes problems that arrise when SCEV expressions contain loop-variant
633 /// values unrelated to the induction variable which are defined inside the
634 /// loop. FIXME: It would be better to insert instructions in the right
635 /// place so that this step isn't needed.
636 void IndVarSimplify::FixUsesBeforeDefs(Loop *L, SCEVExpander &Rewriter) {
637 // Visit all the blocks in the loop in pre-order dom-tree dfs order.
638 DominatorTree *DT = &getAnalysis<DominatorTree>();
639 std::map<Instruction *, unsigned> NumPredsLeft;
640 SmallVector<DomTreeNode *, 16> Worklist;
641 Worklist.push_back(DT->getNode(L->getHeader()));
642 do {
643 DomTreeNode *Node = Worklist.pop_back_val();
644 for (DomTreeNode::iterator I = Node->begin(), E = Node->end(); I != E; ++I)
645 if (L->contains((*I)->getBlock()))
646 Worklist.push_back(*I);
647 BasicBlock *BB = Node->getBlock();
648 // Visit all the instructions in the block top down.
649 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
650 // Count the number of operands that aren't properly dominating.
651 unsigned NumPreds = 0;
652 if (Rewriter.isInsertedInstruction(I) && !isa<PHINode>(I))
653 for (User::op_iterator OI = I->op_begin(), OE = I->op_end();
654 OI != OE; ++OI)
655 if (Instruction *Inst = dyn_cast<Instruction>(OI))
656 if (L->contains(Inst->getParent()) && !NumPredsLeft.count(Inst))
657 ++NumPreds;
658 NumPredsLeft[I] = NumPreds;
659 // Notify uses of the position of this instruction, and move the
660 // users (and their dependents, recursively) into place after this
661 // instruction if it is their last outstanding operand.
662 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
663 UI != UE; ++UI) {
664 Instruction *Inst = cast<Instruction>(UI);
665 std::map<Instruction *, unsigned>::iterator Z = NumPredsLeft.find(Inst);
666 if (Z != NumPredsLeft.end() && Z->second != 0 && --Z->second == 0) {
667 SmallVector<Instruction *, 4> UseWorkList;
668 UseWorkList.push_back(Inst);
669 BasicBlock::iterator InsertPt = next(I);
670 while (isa<PHINode>(InsertPt)) ++InsertPt;
671 do {
672 Instruction *Use = UseWorkList.pop_back_val();
673 Use->moveBefore(InsertPt);
674 NumPredsLeft.erase(Use);
675 for (Value::use_iterator IUI = Use->use_begin(),
676 IUE = Use->use_end(); IUI != IUE; ++IUI) {
677 Instruction *IUIInst = cast<Instruction>(IUI);
678 if (L->contains(IUIInst->getParent()) &&
679 Rewriter.isInsertedInstruction(IUIInst) &&
680 !isa<PHINode>(IUIInst))
681 UseWorkList.push_back(IUIInst);
683 } while (!UseWorkList.empty());
687 } while (!Worklist.empty());
690 /// Return true if it is OK to use SIToFPInst for an inducation variable
691 /// with given inital and exit values.
692 static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV,
693 uint64_t intIV, uint64_t intEV) {
695 if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative())
696 return true;
698 // If the iteration range can be handled by SIToFPInst then use it.
699 APInt Max = APInt::getSignedMaxValue(32);
700 if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV)))
701 return true;
703 return false;
706 /// convertToInt - Convert APF to an integer, if possible.
707 static bool convertToInt(const APFloat &APF, uint64_t *intVal) {
709 bool isExact = false;
710 if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
711 return false;
712 if (APF.convertToInteger(intVal, 32, APF.isNegative(),
713 APFloat::rmTowardZero, &isExact)
714 != APFloat::opOK)
715 return false;
716 if (!isExact)
717 return false;
718 return true;
722 /// HandleFloatingPointIV - If the loop has floating induction variable
723 /// then insert corresponding integer induction variable if possible.
724 /// For example,
725 /// for(double i = 0; i < 10000; ++i)
726 /// bar(i)
727 /// is converted into
728 /// for(int i = 0; i < 10000; ++i)
729 /// bar((double)i);
731 void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) {
733 unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
734 unsigned BackEdge = IncomingEdge^1;
736 // Check incoming value.
737 ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge));
738 if (!InitValue) return;
739 uint64_t newInitValue = Type::Int32Ty->getPrimitiveSizeInBits();
740 if (!convertToInt(InitValue->getValueAPF(), &newInitValue))
741 return;
743 // Check IV increment. Reject this PH if increement operation is not
744 // an add or increment value can not be represented by an integer.
745 BinaryOperator *Incr =
746 dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge));
747 if (!Incr) return;
748 if (Incr->getOpcode() != Instruction::Add) return;
749 ConstantFP *IncrValue = NULL;
750 unsigned IncrVIndex = 1;
751 if (Incr->getOperand(1) == PH)
752 IncrVIndex = 0;
753 IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex));
754 if (!IncrValue) return;
755 uint64_t newIncrValue = Type::Int32Ty->getPrimitiveSizeInBits();
756 if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue))
757 return;
759 // Check Incr uses. One user is PH and the other users is exit condition used
760 // by the conditional terminator.
761 Value::use_iterator IncrUse = Incr->use_begin();
762 Instruction *U1 = cast<Instruction>(IncrUse++);
763 if (IncrUse == Incr->use_end()) return;
764 Instruction *U2 = cast<Instruction>(IncrUse++);
765 if (IncrUse != Incr->use_end()) return;
767 // Find exit condition.
768 FCmpInst *EC = dyn_cast<FCmpInst>(U1);
769 if (!EC)
770 EC = dyn_cast<FCmpInst>(U2);
771 if (!EC) return;
773 if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) {
774 if (!BI->isConditional()) return;
775 if (BI->getCondition() != EC) return;
778 // Find exit value. If exit value can not be represented as an interger then
779 // do not handle this floating point PH.
780 ConstantFP *EV = NULL;
781 unsigned EVIndex = 1;
782 if (EC->getOperand(1) == Incr)
783 EVIndex = 0;
784 EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex));
785 if (!EV) return;
786 uint64_t intEV = Type::Int32Ty->getPrimitiveSizeInBits();
787 if (!convertToInt(EV->getValueAPF(), &intEV))
788 return;
790 // Find new predicate for integer comparison.
791 CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
792 switch (EC->getPredicate()) {
793 case CmpInst::FCMP_OEQ:
794 case CmpInst::FCMP_UEQ:
795 NewPred = CmpInst::ICMP_EQ;
796 break;
797 case CmpInst::FCMP_OGT:
798 case CmpInst::FCMP_UGT:
799 NewPred = CmpInst::ICMP_UGT;
800 break;
801 case CmpInst::FCMP_OGE:
802 case CmpInst::FCMP_UGE:
803 NewPred = CmpInst::ICMP_UGE;
804 break;
805 case CmpInst::FCMP_OLT:
806 case CmpInst::FCMP_ULT:
807 NewPred = CmpInst::ICMP_ULT;
808 break;
809 case CmpInst::FCMP_OLE:
810 case CmpInst::FCMP_ULE:
811 NewPred = CmpInst::ICMP_ULE;
812 break;
813 default:
814 break;
816 if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return;
818 // Insert new integer induction variable.
819 PHINode *NewPHI = PHINode::Create(Type::Int32Ty,
820 PH->getName()+".int", PH);
821 NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue),
822 PH->getIncomingBlock(IncomingEdge));
824 Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
825 ConstantInt::get(Type::Int32Ty,
826 newIncrValue),
827 Incr->getName()+".int", Incr);
828 NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));
830 // The back edge is edge 1 of newPHI, whatever it may have been in the
831 // original PHI.
832 ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV);
833 Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV);
834 Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1));
835 ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(),
836 EC->getParent()->getTerminator());
838 // In the following deltions, PH may become dead and may be deleted.
839 // Use a WeakVH to observe whether this happens.
840 WeakVH WeakPH = PH;
842 // Delete old, floating point, exit comparision instruction.
843 EC->replaceAllUsesWith(NewEC);
844 RecursivelyDeleteTriviallyDeadInstructions(EC);
846 // Delete old, floating point, increment instruction.
847 Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
848 RecursivelyDeleteTriviallyDeadInstructions(Incr);
850 // Replace floating induction variable, if it isn't already deleted.
851 // Give SIToFPInst preference over UIToFPInst because it is faster on
852 // platforms that are widely used.
853 if (WeakPH && !PH->use_empty()) {
854 if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) {
855 SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv",
856 PH->getParent()->getFirstNonPHI());
857 PH->replaceAllUsesWith(Conv);
858 } else {
859 UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv",
860 PH->getParent()->getFirstNonPHI());
861 PH->replaceAllUsesWith(Conv);
863 RecursivelyDeleteTriviallyDeadInstructions(PH);
866 // Add a new IVUsers entry for the newly-created integer PHI.
867 IU->AddUsersIfInteresting(NewPHI);