blob: 6605666e45d102026860e5db44ed2082505116e0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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.
13//
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.
Dan Gohman024f6132009-06-14 22:38:41 +000020// 3. The canonical induction variable is guaranteed to be in a wide enough
21// type so that IV expressions need not be (directly) zero-extended or
22// sign-extended.
23// 4. Any pointer arithmetic recurrences are raised to use array subscripts.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024//
25// If the trip count of a loop is computable, this pass also makes the following
26// changes:
27// 1. The exit condition for the loop is canonicalized to compare the
28// induction value against the exit value. This turns loops like:
29// 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
30// 2. Any use outside of the loop of an expression derived from the indvar
31// is changed to compute the derived value outside of the loop, eliminating
32// the dependence on the exit value of the induction variable. If the only
33// purpose of the loop is to compute the exit value of some derived
34// expression, this transformation will make the loop dead.
35//
36// This transformation should be followed by strength reduction after all of the
Dan Gohman211ca4a2009-05-19 20:38:47 +000037// desired loop transformations have been performed.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//
39//===----------------------------------------------------------------------===//
40
41#define DEBUG_TYPE "indvars"
42#include "llvm/Transforms/Scalar.h"
43#include "llvm/BasicBlock.h"
44#include "llvm/Constants.h"
45#include "llvm/Instructions.h"
Devang Patelacdcabc2010-03-15 22:23:03 +000046#include "llvm/IntrinsicInst.h"
Owen Anderson24be4c12009-07-03 00:17:18 +000047#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048#include "llvm/Type.h"
Dan Gohman28055122009-05-12 02:17:14 +000049#include "llvm/Analysis/Dominators.h"
50#include "llvm/Analysis/IVUsers.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051#include "llvm/Analysis/ScalarEvolutionExpander.h"
52#include "llvm/Analysis/LoopInfo.h"
53#include "llvm/Analysis/LoopPass.h"
54#include "llvm/Support/CFG.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000055#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056#include "llvm/Support/Debug.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000057#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058#include "llvm/Transforms/Utils/Local.h"
Dan Gohman28055122009-05-12 02:17:14 +000059#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060#include "llvm/ADT/SmallVector.h"
61#include "llvm/ADT/Statistic.h"
Dan Gohman28055122009-05-12 02:17:14 +000062#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063using namespace llvm;
64
65STATISTIC(NumRemoved , "Number of aux indvars removed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066STATISTIC(NumInserted, "Number of canonical indvars added");
67STATISTIC(NumReplaced, "Number of exit values replaced");
68STATISTIC(NumLFTR , "Number of loop exit tests replaced");
69
70namespace {
Chris Lattnerfa2d1ba2009-09-02 06:11:42 +000071 class IndVarSimplify : public LoopPass {
Dan Gohman28055122009-05-12 02:17:14 +000072 IVUsers *IU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 LoopInfo *LI;
74 ScalarEvolution *SE;
Dan Gohman402142e2009-06-27 05:16:57 +000075 DominatorTree *DT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 bool Changed;
77 public:
78
Dan Gohman518beed2009-07-15 01:26:32 +000079 static char ID; // Pass identification, replacement for typeid
80 IndVarSimplify() : LoopPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
Dan Gohman518beed2009-07-15 01:26:32 +000082 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
Dan Gohmanf3a060a2009-02-17 20:49:49 +000083
Dan Gohman518beed2009-07-15 01:26:32 +000084 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.addRequired<DominatorTree>();
86 AU.addRequired<LoopInfo>();
87 AU.addRequired<ScalarEvolution>();
88 AU.addRequiredID(LoopSimplifyID);
89 AU.addRequiredID(LCSSAID);
90 AU.addRequired<IVUsers>();
91 AU.addPreserved<ScalarEvolution>();
92 AU.addPreservedID(LoopSimplifyID);
93 AU.addPreservedID(LCSSAID);
94 AU.addPreserved<IVUsers>();
95 AU.setPreservesCFG();
96 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
98 private:
99
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000100 void RewriteNonIntegerIVs(Loop *L);
101
Dan Gohman161ea032009-07-07 17:06:11 +0000102 ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount,
Dan Gohman1247dc32009-02-17 15:57:39 +0000103 Value *IndVar,
Dan Gohmancacd2012009-02-12 22:19:27 +0000104 BasicBlock *ExitingBlock,
105 BranchInst *BI,
Dan Gohmanebac2542009-02-23 23:20:35 +0000106 SCEVExpander &Rewriter);
Dan Gohmane3979a22010-02-22 04:11:59 +0000107 void RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
Dan Gohmane3979a22010-02-22 04:11:59 +0000109 void RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter);
Devang Patelbda43802008-09-09 21:41:07 +0000110
Dan Gohman9a0303b2009-06-26 22:53:46 +0000111 void SinkUnusedInvariants(Loop *L);
Dan Gohman28055122009-05-12 02:17:14 +0000112
113 void HandleFloatingPointIV(Loop *L, PHINode *PH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115}
116
Dan Gohman089efff2008-05-13 00:00:25 +0000117char IndVarSimplify::ID = 0;
118static RegisterPass<IndVarSimplify>
119X("indvars", "Canonicalize Induction Variables");
120
Daniel Dunbar163555a2008-10-22 23:32:42 +0000121Pass *llvm::createIndVarSimplifyPass() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 return new IndVarSimplify();
123}
124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125/// LinearFunctionTestReplace - This method rewrites the exit condition of the
126/// loop to be a canonical != comparison against the incremented loop induction
127/// variable. This pass is able to rewrite the exit tests of any loop where the
128/// SCEV analysis can determine a loop-invariant trip count of the loop, which
129/// is actually a much broader range than just linear tests.
Dan Gohman28055122009-05-12 02:17:14 +0000130ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
Dan Gohman161ea032009-07-07 17:06:11 +0000131 const SCEV *BackedgeTakenCount,
Dan Gohmancacd2012009-02-12 22:19:27 +0000132 Value *IndVar,
133 BasicBlock *ExitingBlock,
134 BranchInst *BI,
Dan Gohmanebac2542009-02-23 23:20:35 +0000135 SCEVExpander &Rewriter) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 // If the exiting block is not the same as the backedge block, we must compare
137 // against the preincremented value, otherwise we prefer to compare against
138 // the post-incremented value.
Dan Gohmancacd2012009-02-12 22:19:27 +0000139 Value *CmpIndVar;
Dan Gohman161ea032009-07-07 17:06:11 +0000140 const SCEV *RHS = BackedgeTakenCount;
Dan Gohmancacd2012009-02-12 22:19:27 +0000141 if (ExitingBlock == L->getLoopLatch()) {
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000142 // Add one to the "backedge-taken" count to get the trip count.
143 // If this addition may overflow, we have to be more pessimistic and
144 // cast the induction variable before doing the add.
Dan Gohman161ea032009-07-07 17:06:11 +0000145 const SCEV *Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
146 const SCEV *N =
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000147 SE->getAddExpr(BackedgeTakenCount,
148 SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
Dan Gohmancacd2012009-02-12 22:19:27 +0000149 if ((isa<SCEVConstant>(N) && !N->isZero()) ||
150 SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
151 // No overflow. Cast the sum.
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000152 RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
Dan Gohmancacd2012009-02-12 22:19:27 +0000153 } else {
154 // Potential overflow. Cast before doing the add.
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000155 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
156 IndVar->getType());
157 RHS = SE->getAddExpr(RHS,
158 SE->getIntegerSCEV(1, IndVar->getType()));
Dan Gohmancacd2012009-02-12 22:19:27 +0000159 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000161 // The BackedgeTaken expression contains the number of times that the
162 // backedge branches to the loop header. This is one less than the
163 // number of times the loop executes, so use the incremented indvar.
Dan Gohmancacd2012009-02-12 22:19:27 +0000164 CmpIndVar = L->getCanonicalInductionVariableIncrement();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 } else {
166 // We have to use the preincremented value...
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000167 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
168 IndVar->getType());
Dan Gohmancacd2012009-02-12 22:19:27 +0000169 CmpIndVar = IndVar;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171
Dan Gohman9a0303b2009-06-26 22:53:46 +0000172 // Expand the code for the iteration count.
Dan Gohman423ed6c2009-06-24 01:18:18 +0000173 assert(RHS->isLoopInvariant(L) &&
174 "Computed iteration count is not loop invariant!");
Dan Gohman9a0303b2009-06-26 22:53:46 +0000175 Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), BI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
177 // Insert a new icmp_ne or icmp_eq instruction before the branch.
178 ICmpInst::Predicate Opcode;
179 if (L->contains(BI->getSuccessor(0)))
180 Opcode = ICmpInst::ICMP_NE;
181 else
182 Opcode = ICmpInst::ICMP_EQ;
183
David Greene36daa092010-01-05 01:27:06 +0000184 DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n"
Chris Lattner8a6411c2009-08-23 04:37:46 +0000185 << " LHS:" << *CmpIndVar << '\n'
186 << " op:\t"
187 << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
188 << " RHS:\t" << *RHS << "\n");
Dan Gohmancacd2012009-02-12 22:19:27 +0000189
Owen Anderson6601fcd2009-07-09 23:48:35 +0000190 ICmpInst *Cond = new ICmpInst(BI, Opcode, CmpIndVar, ExitCnt, "exitcond");
Dan Gohman28055122009-05-12 02:17:14 +0000191
Dan Gohman68e5ec52010-02-22 02:07:36 +0000192 Value *OrigCond = BI->getCondition();
Dan Gohman53cc9222009-05-24 19:11:38 +0000193 // It's tempting to use replaceAllUsesWith here to fully replace the old
194 // comparison, but that's not immediately safe, since users of the old
195 // comparison may not be dominated by the new comparison. Instead, just
196 // update the branch to use the new comparison; in the common case this
197 // will make old comparison dead.
198 BI->setCondition(Cond);
Dan Gohman28055122009-05-12 02:17:14 +0000199 RecursivelyDeleteTriviallyDeadInstructions(OrigCond);
200
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 ++NumLFTR;
202 Changed = true;
Dan Gohman28055122009-05-12 02:17:14 +0000203 return Cond;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204}
205
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206/// RewriteLoopExitValues - Check to see if this loop has a computable
207/// loop-invariant execution count. If so, this means that we can compute the
208/// final value of any expressions that are recurrent in the loop, and
209/// substitute the exit values from the loop into any instructions outside of
210/// the loop that use the final values of the current expressions.
Dan Gohman28055122009-05-12 02:17:14 +0000211///
212/// This is mostly redundant with the regular IndVarSimplify activities that
213/// happen later, except that it's more powerful in some cases, because it's
214/// able to brute-force evaluate arbitrary instructions as long as they have
215/// constant operands at the beginning of the loop.
Dan Gohman9a769972009-04-18 17:56:28 +0000216void IndVarSimplify::RewriteLoopExitValues(Loop *L,
Dan Gohman9a0303b2009-06-26 22:53:46 +0000217 SCEVExpander &Rewriter) {
Dan Gohman28055122009-05-12 02:17:14 +0000218 // Verify the input to the pass in already in LCSSA form.
Dan Gohmana76b5582010-03-10 19:38:49 +0000219 assert(L->isLCSSAForm(*DT));
Dan Gohman28055122009-05-12 02:17:14 +0000220
Devang Patel02451fa2007-08-21 00:31:24 +0000221 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 L->getUniqueExitBlocks(ExitBlocks);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223
224 // Find all values that are computed inside the loop, but used outside of it.
225 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
226 // the exit blocks of the loop to find them.
227 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
228 BasicBlock *ExitBB = ExitBlocks[i];
Dan Gohman963fc812009-02-17 19:13:57 +0000229
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // If there are no PHI nodes in this exit block, then no values defined
231 // inside the loop are used on this path, skip it.
232 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
233 if (!PN) continue;
Dan Gohman963fc812009-02-17 19:13:57 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 unsigned NumPreds = PN->getNumIncomingValues();
Dan Gohman963fc812009-02-17 19:13:57 +0000236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 // Iterate over all of the PHI nodes.
238 BasicBlock::iterator BBI = ExitBB->begin();
239 while ((PN = dyn_cast<PHINode>(BBI++))) {
Edwin Törökf06a8f22009-05-24 19:36:09 +0000240 if (PN->use_empty())
241 continue; // dead use, don't replace it
Dan Gohman50e4dfc2010-02-18 21:34:02 +0000242
243 // SCEV only supports integer expressions for now.
244 if (!PN->getType()->isIntegerTy() && !PN->getType()->isPointerTy())
245 continue;
246
Dale Johannesend43e4a82010-02-19 07:14:22 +0000247 // It's necessary to tell ScalarEvolution about this explicitly so that
248 // it can walk the def-use list and forget all SCEVs, as it may not be
249 // watching the PHI itself. Once the new exit value is in place, there
250 // may not be a def-use connection between the loop and every instruction
251 // which got a SCEVAddRecExpr for that loop.
252 SE->forgetValue(PN);
253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 // Iterate over all of the values in all the PHI nodes.
255 for (unsigned i = 0; i != NumPreds; ++i) {
256 // If the value being merged in is not integer or is not defined
257 // in the loop, skip it.
258 Value *InVal = PN->getIncomingValue(i);
Dan Gohman50e4dfc2010-02-18 21:34:02 +0000259 if (!isa<Instruction>(InVal))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 continue;
261
262 // If this pred is for a subloop, not L itself, skip it.
Dan Gohman963fc812009-02-17 19:13:57 +0000263 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 continue; // The Block is in a subloop, skip it.
265
266 // Check that InVal is defined in the loop.
267 Instruction *Inst = cast<Instruction>(InVal);
Dan Gohmane8df7dd2009-12-18 01:24:09 +0000268 if (!L->contains(Inst))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 continue;
Dan Gohman963fc812009-02-17 19:13:57 +0000270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 // Okay, this instruction has a user outside of the current loop
272 // and varies predictably *inside* the loop. Evaluate the value it
273 // contains when the loop exits, if possible.
Dan Gohman161ea032009-07-07 17:06:11 +0000274 const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
Dan Gohmanaff14d62009-05-24 23:25:42 +0000275 if (!ExitValue->isLoopInvariant(L))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 continue;
277
278 Changed = true;
279 ++NumReplaced;
Dan Gohman963fc812009-02-17 19:13:57 +0000280
Dan Gohman9a0303b2009-06-26 22:53:46 +0000281 Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
Dan Gohman963fc812009-02-17 19:13:57 +0000282
David Greene36daa092010-01-05 01:27:06 +0000283 DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
Chris Lattner8a6411c2009-08-23 04:37:46 +0000284 << " LoopVal = " << *Inst << "\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285
286 PN->setIncomingValue(i, ExitVal);
Dan Gohman963fc812009-02-17 19:13:57 +0000287
Dan Gohman28055122009-05-12 02:17:14 +0000288 // If this instruction is dead now, delete it.
289 RecursivelyDeleteTriviallyDeadInstructions(Inst);
Dan Gohman963fc812009-02-17 19:13:57 +0000290
Dan Gohman09923242009-07-14 01:09:02 +0000291 if (NumPreds == 1) {
292 // Completely replace a single-pred PHI. This is safe, because the
293 // NewVal won't be variant in the loop, so we don't need an LCSSA phi
294 // node anymore.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 PN->replaceAllUsesWith(ExitVal);
Dan Gohman28055122009-05-12 02:17:14 +0000296 RecursivelyDeleteTriviallyDeadInstructions(PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 }
298 }
Dan Gohman09923242009-07-14 01:09:02 +0000299 if (NumPreds != 1) {
Dan Gohman9a0303b2009-06-26 22:53:46 +0000300 // Clone the PHI and delete the original one. This lets IVUsers and
301 // any other maps purge the original user from their records.
Devang Patel12236e12009-10-27 22:16:29 +0000302 PHINode *NewPN = cast<PHINode>(PN->clone());
Dan Gohman9a0303b2009-06-26 22:53:46 +0000303 NewPN->takeName(PN);
304 NewPN->insertBefore(PN);
305 PN->replaceAllUsesWith(NewPN);
306 PN->eraseFromParent();
307 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 }
309 }
Dan Gohman063d8fe2010-03-20 03:53:53 +0000310
311 // The insertion point instruction may have been deleted; clear it out
312 // so that the rewriter doesn't trip over it later.
313 Rewriter.clearInsertPoint();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314}
315
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000316void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000317 // First step. Check to see if there are any floating-point recurrences.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 // If there are, change them into integer recurrences, permitting analysis by
319 // the SCEV routines.
320 //
321 BasicBlock *Header = L->getHeader();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
Dan Gohman28055122009-05-12 02:17:14 +0000323 SmallVector<WeakVH, 8> PHIs;
324 for (BasicBlock::iterator I = Header->begin();
325 PHINode *PN = dyn_cast<PHINode>(I); ++I)
326 PHIs.push_back(PN);
327
328 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
329 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i]))
330 HandleFloatingPointIV(L, PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331
Dan Gohman01c2ee72009-04-16 03:18:22 +0000332 // If the loop previously had floating-point IV, ScalarEvolution
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000333 // may not have been able to compute a trip count. Now that we've done some
334 // re-writing, the trip count may be computable.
335 if (Changed)
Dan Gohmanf893ba32009-10-31 15:04:55 +0000336 SE->forgetLoop(L);
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000337}
338
Dan Gohmancacd2012009-02-12 22:19:27 +0000339bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Dan Gohman28055122009-05-12 02:17:14 +0000340 IU = &getAnalysis<IVUsers>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 LI = &getAnalysis<LoopInfo>();
342 SE = &getAnalysis<ScalarEvolution>();
Dan Gohman402142e2009-06-27 05:16:57 +0000343 DT = &getAnalysis<DominatorTree>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 Changed = false;
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000345
Dan Gohman01c2ee72009-04-16 03:18:22 +0000346 // If there are any floating-point recurrences, attempt to
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000347 // transform them to use integer recurrences.
348 RewriteNonIntegerIVs(L);
349
Dan Gohman28055122009-05-12 02:17:14 +0000350 BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null
Dan Gohman161ea032009-07-07 17:06:11 +0000351 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
Dan Gohman9a0303b2009-06-26 22:53:46 +0000353 // Create a rewriter object which we'll use to transform the code with.
354 SCEVExpander Rewriter(*SE);
355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 // Check to see if this loop has a computable loop-invariant execution count.
357 // If so, this means that we can compute the final value of any expressions
358 // that are recurrent in the loop, and substitute the exit values from the
359 // loop into any instructions outside of the loop that use the final values of
360 // the current expressions.
361 //
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000362 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
Dan Gohmane3979a22010-02-22 04:11:59 +0000363 RewriteLoopExitValues(L, Rewriter);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
Dan Gohman28055122009-05-12 02:17:14 +0000365 // Compute the type of the largest recurrence expression, and decide whether
366 // a canonical induction variable should be inserted.
Dan Gohmancacd2012009-02-12 22:19:27 +0000367 const Type *LargestType = 0;
Dan Gohman28055122009-05-12 02:17:14 +0000368 bool NeedCannIV = false;
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000369 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
370 LargestType = BackedgeTakenCount->getType();
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000371 LargestType = SE->getEffectiveSCEVType(LargestType);
Dan Gohman28055122009-05-12 02:17:14 +0000372 // If we have a known trip count and a single exit block, we'll be
373 // rewriting the loop exit test condition below, which requires a
374 // canonical induction variable.
375 if (ExitingBlock)
376 NeedCannIV = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 }
Dan Gohmand7df9e12010-02-12 10:34:29 +0000378 for (IVUsers::const_iterator I = IU->begin(), E = IU->end(); I != E; ++I) {
379 const Type *Ty =
380 SE->getEffectiveSCEVType(I->getOperandValToReplace()->getType());
Dan Gohmancacd2012009-02-12 22:19:27 +0000381 if (!LargestType ||
Dan Gohman28055122009-05-12 02:17:14 +0000382 SE->getTypeSizeInBits(Ty) >
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000383 SE->getTypeSizeInBits(LargestType))
Dan Gohman28055122009-05-12 02:17:14 +0000384 LargestType = Ty;
Dan Gohmand7df9e12010-02-12 10:34:29 +0000385 NeedCannIV = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 }
387
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000388 // Now that we know the largest of the induction variable expressions
Dan Gohman28055122009-05-12 02:17:14 +0000389 // in this loop, insert a canonical induction variable of the largest size.
Dan Gohmancacd2012009-02-12 22:19:27 +0000390 Value *IndVar = 0;
Dan Gohman28055122009-05-12 02:17:14 +0000391 if (NeedCannIV) {
Dan Gohman8eb9f252010-02-25 06:57:05 +0000392 // Check to see if the loop already has any canonical-looking induction
393 // variables. If any are present and wider than the planned canonical
394 // induction variable, temporarily remove them, so that the Rewriter
395 // doesn't attempt to reuse them.
396 SmallVector<PHINode *, 2> OldCannIVs;
397 while (PHINode *OldCannIV = L->getCanonicalInductionVariable()) {
Dan Gohmanfc4d0712009-06-13 16:25:49 +0000398 if (SE->getTypeSizeInBits(OldCannIV->getType()) >
399 SE->getTypeSizeInBits(LargestType))
400 OldCannIV->removeFromParent();
401 else
Dan Gohman8eb9f252010-02-25 06:57:05 +0000402 break;
403 OldCannIVs.push_back(OldCannIV);
Dan Gohmanfc4d0712009-06-13 16:25:49 +0000404 }
405
Dan Gohman9a0303b2009-06-26 22:53:46 +0000406 IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType);
Dan Gohmanfc4d0712009-06-13 16:25:49 +0000407
Dan Gohmancacd2012009-02-12 22:19:27 +0000408 ++NumInserted;
409 Changed = true;
David Greene36daa092010-01-05 01:27:06 +0000410 DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n');
Dan Gohmanfc4d0712009-06-13 16:25:49 +0000411
412 // Now that the official induction variable is established, reinsert
Dan Gohman8eb9f252010-02-25 06:57:05 +0000413 // any old canonical-looking variables after it so that the IR remains
414 // consistent. They will be deleted as part of the dead-PHI deletion at
Dan Gohmanfc4d0712009-06-13 16:25:49 +0000415 // the end of the pass.
Dan Gohman8eb9f252010-02-25 06:57:05 +0000416 while (!OldCannIVs.empty()) {
417 PHINode *OldCannIV = OldCannIVs.pop_back_val();
418 OldCannIV->insertBefore(L->getHeader()->getFirstNonPHI());
419 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 }
421
Dan Gohmancacd2012009-02-12 22:19:27 +0000422 // If we have a trip count expression, rewrite the loop's exit condition
423 // using it. We can currently only handle loops with a single exit.
Dan Gohman28055122009-05-12 02:17:14 +0000424 ICmpInst *NewICmp = 0;
Dan Gohman8eb9f252010-02-25 06:57:05 +0000425 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
426 !BackedgeTakenCount->isZero() &&
427 ExitingBlock) {
Dan Gohman28055122009-05-12 02:17:14 +0000428 assert(NeedCannIV &&
429 "LinearFunctionTestReplace requires a canonical induction variable");
Dan Gohmancacd2012009-02-12 22:19:27 +0000430 // Can't rewrite non-branch yet.
Dan Gohman28055122009-05-12 02:17:14 +0000431 if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
432 NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
433 ExitingBlock, BI, Rewriter);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 }
435
Edwin Törökacec1c02009-05-24 20:08:21 +0000436 // Rewrite IV-derived expressions. Clears the rewriter cache.
Dan Gohmane3979a22010-02-22 04:11:59 +0000437 RewriteIVExpressions(L, Rewriter);
Dan Gohmancacd2012009-02-12 22:19:27 +0000438
Dan Gohman9a0303b2009-06-26 22:53:46 +0000439 // The Rewriter may not be used from this point on.
Edwin Törökacec1c02009-05-24 20:08:21 +0000440
Dan Gohman28055122009-05-12 02:17:14 +0000441 // Loop-invariant instructions in the preheader that aren't used in the
442 // loop may be sunk below the loop to reduce register pressure.
Dan Gohman9a0303b2009-06-26 22:53:46 +0000443 SinkUnusedInvariants(L);
Dan Gohman28055122009-05-12 02:17:14 +0000444
445 // For completeness, inform IVUsers of the IV use in the newly-created
446 // loop exit test instruction.
447 if (NewICmp)
448 IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0)));
449
450 // Clean up dead instructions.
Dan Gohman19f58082010-01-05 16:31:45 +0000451 Changed |= DeleteDeadPHIs(L->getHeader());
Dan Gohman28055122009-05-12 02:17:14 +0000452 // Check a post-condition.
Dan Gohmana76b5582010-03-10 19:38:49 +0000453 assert(L->isLCSSAForm(*DT) && "Indvars did not leave the loop in lcssa form!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 return Changed;
455}
Devang Patelbda43802008-09-09 21:41:07 +0000456
Dan Gohmane3979a22010-02-22 04:11:59 +0000457void IndVarSimplify::RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter) {
Dan Gohman28055122009-05-12 02:17:14 +0000458 SmallVector<WeakVH, 16> DeadInsts;
459
460 // Rewrite all induction variable expressions in terms of the canonical
461 // induction variable.
462 //
463 // If there were induction variables of other sizes or offsets, manually
464 // add the offsets to the primary induction variable and cast, avoiding
465 // the need for the code evaluation methods to insert induction variables
466 // of different sizes.
Dan Gohmand7df9e12010-02-12 10:34:29 +0000467 for (IVUsers::iterator UI = IU->begin(), E = IU->end(); UI != E; ++UI) {
468 const SCEV *Stride = UI->getStride();
469 Value *Op = UI->getOperandValToReplace();
470 const Type *UseTy = Op->getType();
471 Instruction *User = UI->getUser();
Dan Gohman28055122009-05-12 02:17:14 +0000472
Dan Gohmand7df9e12010-02-12 10:34:29 +0000473 // Compute the final addrec to expand into code.
474 const SCEV *AR = IU->getReplacementExpr(*UI);
Dan Gohman28055122009-05-12 02:17:14 +0000475
Dan Gohmand7df9e12010-02-12 10:34:29 +0000476 // Evaluate the expression out of the loop, if possible.
477 if (!L->contains(UI->getUser())) {
478 const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop());
479 if (ExitVal->isLoopInvariant(L))
480 AR = ExitVal;
Dan Gohman28055122009-05-12 02:17:14 +0000481 }
Dan Gohmand7df9e12010-02-12 10:34:29 +0000482
483 // FIXME: It is an extremely bad idea to indvar substitute anything more
484 // complex than affine induction variables. Doing so will put expensive
485 // polynomial evaluations inside of the loop, and the str reduction pass
486 // currently can only reduce affine polynomials. For now just disable
487 // indvar subst on anything more complex than an affine addrec, unless
488 // it can be expanded to a trivial value.
489 if (!AR->isLoopInvariant(L) && !Stride->isLoopInvariant(L))
490 continue;
491
492 // Determine the insertion point for this user. By default, insert
493 // immediately before the user. The SCEVExpander class will automatically
494 // hoist loop invariants out of the loop. For PHI nodes, there may be
495 // multiple uses, so compute the nearest common dominator for the
496 // incoming blocks.
497 Instruction *InsertPt = User;
498 if (PHINode *PHI = dyn_cast<PHINode>(InsertPt))
499 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
500 if (PHI->getIncomingValue(i) == Op) {
501 if (InsertPt == User)
502 InsertPt = PHI->getIncomingBlock(i)->getTerminator();
503 else
504 InsertPt =
505 DT->findNearestCommonDominator(InsertPt->getParent(),
506 PHI->getIncomingBlock(i))
507 ->getTerminator();
508 }
509
510 // Now expand it into actual Instructions and patch it into place.
511 Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt);
512
Dan Gohman23b05502010-04-02 14:48:31 +0000513 // Inform ScalarEvolution that this value is changing. The change doesn't
514 // affect its value, but it does potentially affect which use lists the
515 // value will be on after the replacement, which affects ScalarEvolution's
516 // ability to walk use lists and drop dangling pointers when a value is
517 // deleted.
518 SE->forgetValue(User);
519
Dan Gohmand7df9e12010-02-12 10:34:29 +0000520 // Patch the new value into place.
521 if (Op->hasName())
522 NewVal->takeName(Op);
523 User->replaceUsesOfWith(Op, NewVal);
524 UI->setOperandValToReplace(NewVal);
525 DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n'
526 << " into = " << *NewVal << "\n");
527 ++NumRemoved;
528 Changed = true;
529
530 // The old value may be dead now.
531 DeadInsts.push_back(Op);
Dan Gohman28055122009-05-12 02:17:14 +0000532 }
533
Edwin Törökacec1c02009-05-24 20:08:21 +0000534 // Clear the rewriter cache, because values that are in the rewriter's cache
535 // can be deleted in the loop below, causing the AssertingVH in the cache to
536 // trigger.
537 Rewriter.clear();
Dan Gohman28055122009-05-12 02:17:14 +0000538 // Now that we're done iterating through lists, clean up any instructions
539 // which are now dead.
Dan Gohmand7140f12010-01-21 02:09:26 +0000540 while (!DeadInsts.empty())
541 if (Instruction *Inst =
542 dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()))
Dan Gohman28055122009-05-12 02:17:14 +0000543 RecursivelyDeleteTriviallyDeadInstructions(Inst);
Dan Gohman28055122009-05-12 02:17:14 +0000544}
545
546/// If there's a single exit block, sink any loop-invariant values that
547/// were defined in the preheader but not used inside the loop into the
548/// exit block to reduce register pressure in the loop.
Dan Gohman9a0303b2009-06-26 22:53:46 +0000549void IndVarSimplify::SinkUnusedInvariants(Loop *L) {
Dan Gohman28055122009-05-12 02:17:14 +0000550 BasicBlock *ExitBlock = L->getExitBlock();
551 if (!ExitBlock) return;
552
Dan Gohman28055122009-05-12 02:17:14 +0000553 BasicBlock *Preheader = L->getLoopPreheader();
Dan Gohmanaef6c7a2009-11-05 21:11:53 +0000554 if (!Preheader) return;
555
556 Instruction *InsertPt = ExitBlock->getFirstNonPHI();
Dan Gohman28055122009-05-12 02:17:14 +0000557 BasicBlock::iterator I = Preheader->getTerminator();
558 while (I != Preheader->begin()) {
559 --I;
Dan Gohman9a0303b2009-06-26 22:53:46 +0000560 // New instructions were inserted at the end of the preheader.
561 if (isa<PHINode>(I))
Dan Gohman28055122009-05-12 02:17:14 +0000562 break;
Bill Wendling96d22882010-03-23 21:15:59 +0000563
Eli Friedman6f5d8b62009-07-15 22:48:29 +0000564 // Don't move instructions which might have side effects, since the side
Bill Wendling96d22882010-03-23 21:15:59 +0000565 // effects need to complete before instructions inside the loop. Also don't
566 // move instructions which might read memory, since the loop may modify
567 // memory. Note that it's okay if the instruction might have undefined
568 // behavior: LoopSimplify guarantees that the preheader dominates the exit
569 // block.
Eli Friedman6f5d8b62009-07-15 22:48:29 +0000570 if (I->mayHaveSideEffects() || I->mayReadFromMemory())
Dan Gohman9a0303b2009-06-26 22:53:46 +0000571 continue;
Bill Wendling96d22882010-03-23 21:15:59 +0000572
Devang Patelacdcabc2010-03-15 22:23:03 +0000573 // Skip debug info intrinsics.
574 if (isa<DbgInfoIntrinsic>(I))
575 continue;
Bill Wendling96d22882010-03-23 21:15:59 +0000576
Dan Gohman208278c2009-08-25 17:42:10 +0000577 // Don't sink static AllocaInsts out of the entry block, which would
578 // turn them into dynamic allocas!
579 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
580 if (AI->isStaticAlloca())
581 continue;
Bill Wendling96d22882010-03-23 21:15:59 +0000582
Dan Gohman28055122009-05-12 02:17:14 +0000583 // Determine if there is a use in or before the loop (direct or
584 // otherwise).
585 bool UsedInLoop = false;
586 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
587 UI != UE; ++UI) {
588 BasicBlock *UseBB = cast<Instruction>(UI)->getParent();
589 if (PHINode *P = dyn_cast<PHINode>(UI)) {
590 unsigned i =
591 PHINode::getIncomingValueNumForOperand(UI.getOperandNo());
592 UseBB = P->getIncomingBlock(i);
593 }
594 if (UseBB == Preheader || L->contains(UseBB)) {
595 UsedInLoop = true;
596 break;
597 }
598 }
Bill Wendling96d22882010-03-23 21:15:59 +0000599
Dan Gohman28055122009-05-12 02:17:14 +0000600 // If there is, the def must remain in the preheader.
601 if (UsedInLoop)
602 continue;
Bill Wendling96d22882010-03-23 21:15:59 +0000603
Dan Gohman28055122009-05-12 02:17:14 +0000604 // Otherwise, sink it to the exit block.
605 Instruction *ToMove = I;
606 bool Done = false;
Bill Wendling96d22882010-03-23 21:15:59 +0000607
608 if (I != Preheader->begin()) {
609 // Skip debug info intrinsics.
610 do {
611 --I;
612 } while (isa<DbgInfoIntrinsic>(I) && I != Preheader->begin());
613
614 if (isa<DbgInfoIntrinsic>(I) && I == Preheader->begin())
615 Done = true;
616 } else {
Dan Gohman28055122009-05-12 02:17:14 +0000617 Done = true;
Bill Wendling96d22882010-03-23 21:15:59 +0000618 }
619
Dan Gohman9a0303b2009-06-26 22:53:46 +0000620 ToMove->moveBefore(InsertPt);
Bill Wendling96d22882010-03-23 21:15:59 +0000621 if (Done) break;
Dan Gohman9a0303b2009-06-26 22:53:46 +0000622 InsertPt = ToMove;
Dan Gohman28055122009-05-12 02:17:14 +0000623 }
624}
625
Chris Lattnera7214882010-04-03 06:41:49 +0000626/// ConvertToSInt - Convert APF to an integer, if possible.
627static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal) {
Devang Patele2ba01d2008-11-17 23:27:13 +0000628 bool isExact = false;
Evan Cheng30e65f62008-11-26 01:11:57 +0000629 if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
630 return false;
Chris Lattnera7214882010-04-03 06:41:49 +0000631 // See if we can convert this to an int64_t
632 uint64_t UIntVal;
633 if (APF.convertToInteger(&UIntVal, 64, true, APFloat::rmTowardZero,
634 &isExact) != APFloat::opOK || !isExact)
Devang Patele2ba01d2008-11-17 23:27:13 +0000635 return false;
Chris Lattnera7214882010-04-03 06:41:49 +0000636 IntVal = UIntVal;
Devang Patele2ba01d2008-11-17 23:27:13 +0000637 return true;
Devang Patele2ba01d2008-11-17 23:27:13 +0000638}
639
Devang Patel7ca23c92008-11-03 18:32:19 +0000640/// HandleFloatingPointIV - If the loop has floating induction variable
641/// then insert corresponding integer induction variable if possible.
Devang Patelc8dac622008-11-17 21:32:02 +0000642/// For example,
643/// for(double i = 0; i < 10000; ++i)
644/// bar(i)
645/// is converted into
646/// for(int i = 0; i < 10000; ++i)
647/// bar((double)i);
648///
Chris Lattner97ea59b2010-04-03 06:17:08 +0000649void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) {
650 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
Devang Patelc8dac622008-11-17 21:32:02 +0000651 unsigned BackEdge = IncomingEdge^1;
Dan Gohman963fc812009-02-17 19:13:57 +0000652
Devang Patelc8dac622008-11-17 21:32:02 +0000653 // Check incoming value.
Chris Lattnerac028512010-04-03 06:05:10 +0000654 ConstantFP *InitValueVal =
Chris Lattner97ea59b2010-04-03 06:17:08 +0000655 dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge));
Chris Lattner083545f2010-04-03 07:18:48 +0000656
Chris Lattnera7214882010-04-03 06:41:49 +0000657 int64_t InitValue;
Chris Lattner083545f2010-04-03 07:18:48 +0000658 if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue))
Devang Patele2ba01d2008-11-17 23:27:13 +0000659 return;
660
Chris Lattner97ea59b2010-04-03 06:17:08 +0000661 // Check IV increment. Reject this PN if increment operation is not
Devang Patele2ba01d2008-11-17 23:27:13 +0000662 // an add or increment value can not be represented by an integer.
Dan Gohman963fc812009-02-17 19:13:57 +0000663 BinaryOperator *Incr =
Chris Lattner97ea59b2010-04-03 06:17:08 +0000664 dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge));
Chris Lattner135fc922010-04-03 05:54:59 +0000665 if (Incr == 0 || Incr->getOpcode() != Instruction::FAdd) return;
666
667 // If this is not an add of the PHI with a constantfp, or if the constant fp
668 // is not an integer, bail out.
Chris Lattnerac028512010-04-03 06:05:10 +0000669 ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1));
Chris Lattner083545f2010-04-03 07:18:48 +0000670 int64_t IncValue;
Chris Lattner97ea59b2010-04-03 06:17:08 +0000671 if (IncValueVal == 0 || Incr->getOperand(0) != PN ||
Chris Lattner083545f2010-04-03 07:18:48 +0000672 !ConvertToSInt(IncValueVal->getValueAPF(), IncValue))
Devang Patele2ba01d2008-11-17 23:27:13 +0000673 return;
Dan Gohman963fc812009-02-17 19:13:57 +0000674
Chris Lattner97ea59b2010-04-03 06:17:08 +0000675 // Check Incr uses. One user is PN and the other user is an exit condition
Chris Lattner135fc922010-04-03 05:54:59 +0000676 // used by the conditional terminator.
Devang Patelc8dac622008-11-17 21:32:02 +0000677 Value::use_iterator IncrUse = Incr->use_begin();
678 Instruction *U1 = cast<Instruction>(IncrUse++);
679 if (IncrUse == Incr->use_end()) return;
680 Instruction *U2 = cast<Instruction>(IncrUse++);
681 if (IncrUse != Incr->use_end()) return;
Dan Gohman963fc812009-02-17 19:13:57 +0000682
Chris Lattner135fc922010-04-03 05:54:59 +0000683 // Find exit condition, which is an fcmp. If it doesn't exist, or if it isn't
684 // only used by a branch, we can't transform it.
Chris Lattneref95ed72010-04-03 06:11:07 +0000685 FCmpInst *Compare = dyn_cast<FCmpInst>(U1);
686 if (!Compare)
687 Compare = dyn_cast<FCmpInst>(U2);
688 if (Compare == 0 || !Compare->hasOneUse() ||
689 !isa<BranchInst>(Compare->use_back()))
Chris Lattner135fc922010-04-03 05:54:59 +0000690 return;
Chris Lattnerac028512010-04-03 06:05:10 +0000691
Chris Lattneref95ed72010-04-03 06:11:07 +0000692 BranchInst *TheBr = cast<BranchInst>(Compare->use_back());
Devang Patelc8dac622008-11-17 21:32:02 +0000693
Chris Lattner4a20a892010-04-03 07:21:39 +0000694 // We need to verify that the branch actually controls the iteration count
695 // of the loop. If not, the new IV can overflow and no one will notice.
696 // The branch block must be in the loop and one of the successors must be out
697 // of the loop.
698 assert(TheBr->isConditional() && "Can't use fcmp if not conditional");
699 if (!L->contains(TheBr->getParent()) ||
700 (L->contains(TheBr->getSuccessor(0)) &&
701 L->contains(TheBr->getSuccessor(1))))
702 return;
Chris Lattner083545f2010-04-03 07:18:48 +0000703
704
Chris Lattner135fc922010-04-03 05:54:59 +0000705 // If it isn't a comparison with an integer-as-fp (the exit value), we can't
706 // transform it.
Chris Lattneref95ed72010-04-03 06:11:07 +0000707 ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1));
Chris Lattnera7214882010-04-03 06:41:49 +0000708 int64_t ExitValue;
709 if (ExitValueVal == 0 ||
710 !ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue))
Devang Patelc8dac622008-11-17 21:32:02 +0000711 return;
Chris Lattnera7214882010-04-03 06:41:49 +0000712
Devang Patelc8dac622008-11-17 21:32:02 +0000713 // Find new predicate for integer comparison.
714 CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
Chris Lattneref95ed72010-04-03 06:11:07 +0000715 switch (Compare->getPredicate()) {
Chris Lattnerac028512010-04-03 06:05:10 +0000716 default: return; // Unknown comparison.
Devang Patelc8dac622008-11-17 21:32:02 +0000717 case CmpInst::FCMP_OEQ:
Chris Lattnerac028512010-04-03 06:05:10 +0000718 case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break;
Chris Lattner083545f2010-04-03 07:18:48 +0000719 case CmpInst::FCMP_ONE:
720 case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break;
Devang Patelc8dac622008-11-17 21:32:02 +0000721 case CmpInst::FCMP_OGT:
Chris Lattner40f27452010-04-03 06:25:21 +0000722 case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break;
Devang Patelc8dac622008-11-17 21:32:02 +0000723 case CmpInst::FCMP_OGE:
Chris Lattner40f27452010-04-03 06:25:21 +0000724 case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break;
Devang Patelc8dac622008-11-17 21:32:02 +0000725 case CmpInst::FCMP_OLT:
Chris Lattner7151ee62010-04-03 06:30:03 +0000726 case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break;
Devang Patelc8dac622008-11-17 21:32:02 +0000727 case CmpInst::FCMP_OLE:
Chris Lattner7151ee62010-04-03 06:30:03 +0000728 case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break;
Devang Patel7ca23c92008-11-03 18:32:19 +0000729 }
Chris Lattner083545f2010-04-03 07:18:48 +0000730
731 // We convert the floating point induction variable to a signed i32 value if
732 // we can. This is only safe if the comparison will not overflow in a way
733 // that won't be trapped by the integer equivalent operations. Check for this
734 // now.
735 // TODO: We could use i64 if it is native and the range requires it.
736
737 // The start/stride/exit values must all fit in signed i32.
738 if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue))
739 return;
740
741 // If not actually striding (add x, 0.0), avoid touching the code.
742 if (IncValue == 0)
743 return;
744
745 // Positive and negative strides have different safety conditions.
746 if (IncValue > 0) {
747 // If we have a positive stride, we require the init to be less than the
748 // exit value and an equality or less than comparison.
749 if (InitValue >= ExitValue ||
750 NewPred == CmpInst::ICMP_SGT || NewPred == CmpInst::ICMP_SGE)
751 return;
752
753 uint32_t Range = uint32_t(ExitValue-InitValue);
754 if (NewPred == CmpInst::ICMP_SLE) {
755 // Normalize SLE -> SLT, check for infinite loop.
756 if (++Range == 0) return; // Range overflows.
757 }
758
759 unsigned Leftover = Range % uint32_t(IncValue);
760
761 // If this is an equality comparison, we require that the strided value
762 // exactly land on the exit value, otherwise the IV condition will wrap
763 // around and do things the fp IV wouldn't.
764 if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
765 Leftover != 0)
766 return;
767
768 // If the stride would wrap around the i32 before exiting, we can't
769 // transform the IV.
770 if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue)
771 return;
772
773 } else {
774 // If we have a negative stride, we require the init to be greater than the
775 // exit value and an equality or greater than comparison.
776 if (InitValue >= ExitValue ||
777 NewPred == CmpInst::ICMP_SLT || NewPred == CmpInst::ICMP_SLE)
778 return;
779
780 uint32_t Range = uint32_t(InitValue-ExitValue);
781 if (NewPred == CmpInst::ICMP_SGE) {
782 // Normalize SGE -> SGT, check for infinite loop.
783 if (++Range == 0) return; // Range overflows.
784 }
785
786 unsigned Leftover = Range % uint32_t(-IncValue);
787
788 // If this is an equality comparison, we require that the strided value
789 // exactly land on the exit value, otherwise the IV condition will wrap
790 // around and do things the fp IV wouldn't.
791 if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
792 Leftover != 0)
793 return;
794
795 // If the stride would wrap around the i32 before exiting, we can't
796 // transform the IV.
797 if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue)
798 return;
799 }
800
801 const IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext());
Dan Gohman963fc812009-02-17 19:13:57 +0000802
Chris Lattnera7214882010-04-03 06:41:49 +0000803 // Insert new integer induction variable.
Chris Lattner97ea59b2010-04-03 06:17:08 +0000804 PHINode *NewPHI = PHINode::Create(Int32Ty, PN->getName()+".int", PN);
Chris Lattnerac028512010-04-03 06:05:10 +0000805 NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue),
Chris Lattner97ea59b2010-04-03 06:17:08 +0000806 PN->getIncomingBlock(IncomingEdge));
Devang Patelc8dac622008-11-17 21:32:02 +0000807
Chris Lattnerac028512010-04-03 06:05:10 +0000808 Value *NewAdd =
Chris Lattner083545f2010-04-03 07:18:48 +0000809 BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue),
Chris Lattnerac028512010-04-03 06:05:10 +0000810 Incr->getName()+".int", Incr);
Chris Lattner97ea59b2010-04-03 06:17:08 +0000811 NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge));
Devang Patelc8dac622008-11-17 21:32:02 +0000812
Chris Lattneref95ed72010-04-03 06:11:07 +0000813 ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, NewAdd,
814 ConstantInt::get(Int32Ty, ExitValue),
815 Compare->getName());
Dan Gohman963fc812009-02-17 19:13:57 +0000816
Chris Lattner97ea59b2010-04-03 06:17:08 +0000817 // In the following deletions, PN may become dead and may be deleted.
Dan Gohman28055122009-05-12 02:17:14 +0000818 // Use a WeakVH to observe whether this happens.
Chris Lattner97ea59b2010-04-03 06:17:08 +0000819 WeakVH WeakPH = PN;
Dan Gohman28055122009-05-12 02:17:14 +0000820
Chris Lattneref95ed72010-04-03 06:11:07 +0000821 // Delete the old floating point exit comparison. The branch starts using the
822 // new comparison.
823 NewCompare->takeName(Compare);
824 Compare->replaceAllUsesWith(NewCompare);
825 RecursivelyDeleteTriviallyDeadInstructions(Compare);
Dan Gohman963fc812009-02-17 19:13:57 +0000826
Chris Lattneref95ed72010-04-03 06:11:07 +0000827 // Delete the old floating point increment.
Owen Andersonb99ecca2009-07-30 23:03:37 +0000828 Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
Dan Gohman28055122009-05-12 02:17:14 +0000829 RecursivelyDeleteTriviallyDeadInstructions(Incr);
Dan Gohman963fc812009-02-17 19:13:57 +0000830
Chris Lattner5ab68672010-04-03 06:16:22 +0000831 // If the FP induction variable still has uses, this is because something else
832 // in the loop uses its value. In order to canonicalize the induction
833 // variable, we chose to eliminate the IV and rewrite it in terms of an
834 // int->fp cast.
835 //
836 // We give preference to sitofp over uitofp because it is faster on most
837 // platforms.
838 if (WeakPH) {
Chris Lattner40f27452010-04-03 06:25:21 +0000839 Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
840 PN->getParent()->getFirstNonPHI());
841 PN->replaceAllUsesWith(Conv);
Chris Lattner97ea59b2010-04-03 06:17:08 +0000842 RecursivelyDeleteTriviallyDeadInstructions(PN);
Devang Patele2ba01d2008-11-17 23:27:13 +0000843 }
Devang Patel7ca23c92008-11-03 18:32:19 +0000844
Dan Gohman28055122009-05-12 02:17:14 +0000845 // Add a new IVUsers entry for the newly-created integer PHI.
846 IU->AddUsersIfInteresting(NewPHI);
847}