blob: 56829bd60ca3e01bb2d8f1920ce7518475a5f553 [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.
20// 3. Any pointer arithmetic recurrences are raised to use array subscripts.
21//
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.
32//
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).
37//
38//===----------------------------------------------------------------------===//
39
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"
57using namespace llvm;
58
59STATISTIC(NumRemoved , "Number of aux indvars removed");
60STATISTIC(NumPointer , "Number of pointer indvars promoted");
61STATISTIC(NumInserted, "Number of canonical indvars added");
62STATISTIC(NumReplaced, "Number of exit values replaced");
63STATISTIC(NumLFTR , "Number of loop exit tests replaced");
64
65namespace {
66 class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
67 LoopInfo *LI;
68 ScalarEvolution *SE;
69 bool Changed;
70 public:
71
72 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000073 IndVarSimplify() : LoopPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 bool runOnLoop(Loop *L, LPPassManager &LPM);
76 bool doInitialization(Loop *L, LPPassManager &LPM);
77 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Devang Patele6a8d482007-09-10 18:08:23 +000078 AU.addRequired<ScalarEvolution>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 AU.addRequiredID(LCSSAID);
80 AU.addRequiredID(LoopSimplifyID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 AU.addRequired<LoopInfo>();
82 AU.addPreservedID(LoopSimplifyID);
83 AU.addPreservedID(LCSSAID);
84 AU.setPreservesCFG();
85 }
86
87 private:
88
89 void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
90 std::set<Instruction*> &DeadInsts);
91 Instruction *LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
92 SCEVExpander &RW);
Dan Gohmand8dc3bb2008-08-05 22:34:21 +000093 void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
Devang Patelbda43802008-09-09 21:41:07 +000096
97 void OptimizeCanonicalIVType(Loop *L);
Devang Patel7ca23c92008-11-03 18:32:19 +000098 void HandleFloatingPointIV(Loop *L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
Dan Gohman089efff2008-05-13 00:00:25 +0000102char IndVarSimplify::ID = 0;
103static RegisterPass<IndVarSimplify>
104X("indvars", "Canonicalize Induction Variables");
105
Daniel Dunbar163555a2008-10-22 23:32:42 +0000106Pass *llvm::createIndVarSimplifyPass() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return new IndVarSimplify();
108}
109
110/// DeleteTriviallyDeadInstructions - If any of the instructions is the
111/// specified set are trivially dead, delete them and see if this makes any of
112/// their operands subsequently dead.
113void IndVarSimplify::
114DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
115 while (!Insts.empty()) {
116 Instruction *I = *Insts.begin();
117 Insts.erase(Insts.begin());
118 if (isInstructionTriviallyDead(I)) {
119 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
120 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
121 Insts.insert(U);
122 SE->deleteValueFromRecords(I);
123 DOUT << "INDVARS: Deleting: " << *I;
124 I->eraseFromParent();
125 Changed = true;
126 }
127 }
128}
129
130
131/// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer
132/// recurrence. If so, change it into an integer recurrence, permitting
133/// analysis by the SCEV routines.
134void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
135 BasicBlock *Preheader,
136 std::set<Instruction*> &DeadInsts) {
137 assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!");
138 unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader);
139 unsigned BackedgeIdx = PreheaderIdx^1;
140 if (GetElementPtrInst *GEPI =
141 dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx)))
142 if (GEPI->getOperand(0) == PN) {
143 assert(GEPI->getNumOperands() == 2 && "GEP types must match!");
144 DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI;
145
146 // Okay, we found a pointer recurrence. Transform this pointer
147 // recurrence into an integer recurrence. Compute the value that gets
148 // added to the pointer at every iteration.
149 Value *AddedVal = GEPI->getOperand(1);
150
151 // Insert a new integer PHI node into the top of the block.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000152 PHINode *NewPhi = PHINode::Create(AddedVal->getType(),
153 PN->getName()+".rec", PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader);
155
156 // Create the new add instruction.
Gabor Greifa645dd32008-05-16 19:29:10 +0000157 Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 GEPI->getName()+".rec", GEPI);
159 NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
160
161 // Update the existing GEP to use the recurrence.
162 GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx));
163
164 // Update the GEP to use the new recurrence we just inserted.
165 GEPI->setOperand(1, NewAdd);
166
167 // If the incoming value is a constant expr GEP, try peeling out the array
168 // 0 index if possible to make things simpler.
169 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0)))
170 if (CE->getOpcode() == Instruction::GetElementPtr) {
171 unsigned NumOps = CE->getNumOperands();
172 assert(NumOps > 1 && "CE folding didn't work!");
173 if (CE->getOperand(NumOps-1)->isNullValue()) {
174 // Check to make sure the last index really is an array index.
175 gep_type_iterator GTI = gep_type_begin(CE);
176 for (unsigned i = 1, e = CE->getNumOperands()-1;
177 i != e; ++i, ++GTI)
178 /*empty*/;
179 if (isa<SequentialType>(*GTI)) {
180 // Pull the last index out of the constant expr GEP.
181 SmallVector<Value*, 8> CEIdxs(CE->op_begin()+1, CE->op_end()-1);
182 Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0),
183 &CEIdxs[0],
184 CEIdxs.size());
David Greene393be882007-09-04 15:46:09 +0000185 Value *Idx[2];
186 Idx[0] = Constant::getNullValue(Type::Int32Ty);
187 Idx[1] = NewAdd;
Gabor Greifd6da1d02008-04-06 20:25:17 +0000188 GetElementPtrInst *NGEPI = GetElementPtrInst::Create(
David Greene393be882007-09-04 15:46:09 +0000189 NCE, Idx, Idx + 2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 GEPI->getName(), GEPI);
191 SE->deleteValueFromRecords(GEPI);
192 GEPI->replaceAllUsesWith(NGEPI);
193 GEPI->eraseFromParent();
194 GEPI = NGEPI;
195 }
196 }
197 }
198
199
200 // Finally, if there are any other users of the PHI node, we must
201 // insert a new GEP instruction that uses the pre-incremented version
202 // of the induction amount.
203 if (!PN->use_empty()) {
204 BasicBlock::iterator InsertPos = PN; ++InsertPos;
205 while (isa<PHINode>(InsertPos)) ++InsertPos;
206 Value *PreInc =
Gabor Greifd6da1d02008-04-06 20:25:17 +0000207 GetElementPtrInst::Create(PN->getIncomingValue(PreheaderIdx),
208 NewPhi, "", InsertPos);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 PreInc->takeName(PN);
210 PN->replaceAllUsesWith(PreInc);
211 }
212
213 // Delete the old PHI for sure, and the GEP if its otherwise unused.
214 DeadInsts.insert(PN);
215
216 ++NumPointer;
217 Changed = true;
218 }
219}
220
221/// LinearFunctionTestReplace - This method rewrites the exit condition of the
222/// loop to be a canonical != comparison against the incremented loop induction
223/// variable. This pass is able to rewrite the exit tests of any loop where the
224/// SCEV analysis can determine a loop-invariant trip count of the loop, which
225/// is actually a much broader range than just linear tests.
226///
227/// This method returns a "potentially dead" instruction whose computation chain
228/// should be deleted when convenient.
229Instruction *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
230 SCEV *IterationCount,
231 SCEVExpander &RW) {
232 // Find the exit block for the loop. We can currently only handle loops with
233 // a single exit.
Devang Patel02451fa2007-08-21 00:31:24 +0000234 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 L->getExitBlocks(ExitBlocks);
236 if (ExitBlocks.size() != 1) return 0;
237 BasicBlock *ExitBlock = ExitBlocks[0];
238
239 // Make sure there is only one predecessor block in the loop.
240 BasicBlock *ExitingBlock = 0;
241 for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
242 PI != PE; ++PI)
243 if (L->contains(*PI)) {
244 if (ExitingBlock == 0)
245 ExitingBlock = *PI;
246 else
247 return 0; // Multiple exits from loop to this block.
248 }
249 assert(ExitingBlock && "Loop info is broken");
250
251 if (!isa<BranchInst>(ExitingBlock->getTerminator()))
252 return 0; // Can't rewrite non-branch yet
253 BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator());
254 assert(BI->isConditional() && "Must be conditional to be part of loop!");
255
256 Instruction *PotentiallyDeadInst = dyn_cast<Instruction>(BI->getCondition());
257
258 // If the exiting block is not the same as the backedge block, we must compare
259 // against the preincremented value, otherwise we prefer to compare against
260 // the post-incremented value.
261 BasicBlock *Header = L->getHeader();
262 pred_iterator HPI = pred_begin(Header);
263 assert(HPI != pred_end(Header) && "Loop with zero preds???");
264 if (!L->contains(*HPI)) ++HPI;
265 assert(HPI != pred_end(Header) && L->contains(*HPI) &&
266 "No backedge in loop?");
267
268 SCEVHandle TripCount = IterationCount;
269 Value *IndVar;
270 if (*HPI == ExitingBlock) {
271 // The IterationCount expression contains the number of times that the
272 // backedge actually branches to the loop header. This is one less than the
273 // number of times the loop executes, so add one to it.
274 ConstantInt *OneC = ConstantInt::get(IterationCount->getType(), 1);
Dan Gohman89f85052007-10-22 18:31:58 +0000275 TripCount = SE->getAddExpr(IterationCount, SE->getConstant(OneC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 IndVar = L->getCanonicalInductionVariableIncrement();
277 } else {
278 // We have to use the preincremented value...
279 IndVar = L->getCanonicalInductionVariable();
280 }
281
282 DOUT << "INDVARS: LFTR: TripCount = " << *TripCount
283 << " IndVar = " << *IndVar << "\n";
284
285 // Expand the code for the iteration count into the preheader of the loop.
286 BasicBlock *Preheader = L->getLoopPreheader();
287 Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator());
288
289 // Insert a new icmp_ne or icmp_eq instruction before the branch.
290 ICmpInst::Predicate Opcode;
291 if (L->contains(BI->getSuccessor(0)))
292 Opcode = ICmpInst::ICMP_NE;
293 else
294 Opcode = ICmpInst::ICMP_EQ;
295
296 Value *Cond = new ICmpInst(Opcode, IndVar, ExitCnt, "exitcond", BI);
297 BI->setCondition(Cond);
298 ++NumLFTR;
299 Changed = true;
300 return PotentiallyDeadInst;
301}
302
303
304/// RewriteLoopExitValues - Check to see if this loop has a computable
305/// loop-invariant execution count. If so, this means that we can compute the
306/// final value of any expressions that are recurrent in the loop, and
307/// substitute the exit values from the loop into any instructions outside of
308/// the loop that use the final values of the current expressions.
Dan Gohmand8dc3bb2008-08-05 22:34:21 +0000309void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 BasicBlock *Preheader = L->getLoopPreheader();
311
312 // Scan all of the instructions in the loop, looking at those that have
313 // extra-loop users and which are recurrences.
314 SCEVExpander Rewriter(*SE, *LI);
315
316 // We insert the code into the preheader of the loop if the loop contains
317 // multiple exit blocks, or in the exit block if there is exactly one.
318 BasicBlock *BlockToInsertInto;
Devang Patel02451fa2007-08-21 00:31:24 +0000319 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 L->getUniqueExitBlocks(ExitBlocks);
321 if (ExitBlocks.size() == 1)
322 BlockToInsertInto = ExitBlocks[0];
323 else
324 BlockToInsertInto = Preheader;
Dan Gohman514277c2008-05-23 21:05:58 +0000325 BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
Dan Gohmand8dc3bb2008-08-05 22:34:21 +0000327 bool HasConstantItCount = isa<SCEVConstant>(IterationCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
329 std::set<Instruction*> InstructionsToDelete;
330 std::map<Instruction*, Value*> ExitValues;
331
332 // Find all values that are computed inside the loop, but used outside of it.
333 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
334 // the exit blocks of the loop to find them.
335 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
336 BasicBlock *ExitBB = ExitBlocks[i];
337
338 // If there are no PHI nodes in this exit block, then no values defined
339 // inside the loop are used on this path, skip it.
340 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
341 if (!PN) continue;
342
343 unsigned NumPreds = PN->getNumIncomingValues();
344
345 // Iterate over all of the PHI nodes.
346 BasicBlock::iterator BBI = ExitBB->begin();
347 while ((PN = dyn_cast<PHINode>(BBI++))) {
348
349 // Iterate over all of the values in all the PHI nodes.
350 for (unsigned i = 0; i != NumPreds; ++i) {
351 // If the value being merged in is not integer or is not defined
352 // in the loop, skip it.
353 Value *InVal = PN->getIncomingValue(i);
354 if (!isa<Instruction>(InVal) ||
355 // SCEV only supports integer expressions for now.
356 !isa<IntegerType>(InVal->getType()))
357 continue;
358
359 // If this pred is for a subloop, not L itself, skip it.
360 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
361 continue; // The Block is in a subloop, skip it.
362
363 // Check that InVal is defined in the loop.
364 Instruction *Inst = cast<Instruction>(InVal);
365 if (!L->contains(Inst->getParent()))
366 continue;
367
368 // We require that this value either have a computable evolution or that
369 // the loop have a constant iteration count. In the case where the loop
370 // has a constant iteration count, we can sometimes force evaluation of
371 // the exit value through brute force.
372 SCEVHandle SH = SE->getSCEV(Inst);
373 if (!SH->hasComputableLoopEvolution(L) && !HasConstantItCount)
374 continue; // Cannot get exit evolution for the loop value.
375
376 // Okay, this instruction has a user outside of the current loop
377 // and varies predictably *inside* the loop. Evaluate the value it
378 // contains when the loop exits, if possible.
379 SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
380 if (isa<SCEVCouldNotCompute>(ExitValue) ||
381 !ExitValue->isLoopInvariant(L))
382 continue;
383
384 Changed = true;
385 ++NumReplaced;
386
387 // See if we already computed the exit value for the instruction, if so,
388 // just reuse it.
389 Value *&ExitVal = ExitValues[Inst];
390 if (!ExitVal)
391 ExitVal = Rewriter.expandCodeFor(ExitValue, InsertPt);
392
393 DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
394 << " LoopVal = " << *Inst << "\n";
395
396 PN->setIncomingValue(i, ExitVal);
397
398 // If this instruction is dead now, schedule it to be removed.
399 if (Inst->use_empty())
400 InstructionsToDelete.insert(Inst);
401
402 // See if this is a single-entry LCSSA PHI node. If so, we can (and
403 // have to) remove
404 // the PHI entirely. This is safe, because the NewVal won't be variant
405 // in the loop, so we don't need an LCSSA phi node anymore.
406 if (NumPreds == 1) {
407 SE->deleteValueFromRecords(PN);
408 PN->replaceAllUsesWith(ExitVal);
409 PN->eraseFromParent();
410 break;
411 }
412 }
413 }
414 }
415
416 DeleteTriviallyDeadInstructions(InstructionsToDelete);
417}
418
419bool IndVarSimplify::doInitialization(Loop *L, LPPassManager &LPM) {
420
421 Changed = false;
422 // First step. Check to see if there are any trivial GEP pointer recurrences.
423 // If there are, change them into integer recurrences, permitting analysis by
424 // the SCEV routines.
425 //
426 BasicBlock *Header = L->getHeader();
427 BasicBlock *Preheader = L->getLoopPreheader();
428 SE = &LPM.getAnalysis<ScalarEvolution>();
429
430 std::set<Instruction*> DeadInsts;
431 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
432 PHINode *PN = cast<PHINode>(I);
433 if (isa<PointerType>(PN->getType()))
434 EliminatePointerRecurrence(PN, Preheader, DeadInsts);
435 }
436
437 if (!DeadInsts.empty())
438 DeleteTriviallyDeadInstructions(DeadInsts);
439
440 return Changed;
441}
442
443bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
444
445
446 LI = &getAnalysis<LoopInfo>();
447 SE = &getAnalysis<ScalarEvolution>();
448
449 Changed = false;
450 BasicBlock *Header = L->getHeader();
451 std::set<Instruction*> DeadInsts;
452
453 // Verify the input to the pass in already in LCSSA form.
454 assert(L->isLCSSAForm());
455
456 // Check to see if this loop has a computable loop-invariant execution count.
457 // If so, this means that we can compute the final value of any expressions
458 // that are recurrent in the loop, and substitute the exit values from the
459 // loop into any instructions outside of the loop that use the final values of
460 // the current expressions.
461 //
462 SCEVHandle IterationCount = SE->getIterationCount(L);
463 if (!isa<SCEVCouldNotCompute>(IterationCount))
Dan Gohmand8dc3bb2008-08-05 22:34:21 +0000464 RewriteLoopExitValues(L, IterationCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465
466 // Next, analyze all of the induction variables in the loop, canonicalizing
467 // auxillary induction variables.
468 std::vector<std::pair<PHINode*, SCEVHandle> > IndVars;
469
Devang Patel7ca23c92008-11-03 18:32:19 +0000470 HandleFloatingPointIV(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
472 PHINode *PN = cast<PHINode>(I);
473 if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable!
474 SCEVHandle SCEV = SE->getSCEV(PN);
475 if (SCEV->hasComputableLoopEvolution(L))
476 // FIXME: It is an extremely bad idea to indvar substitute anything more
477 // complex than affine induction variables. Doing so will put expensive
478 // polynomial evaluations inside of the loop, and the str reduction pass
479 // currently can only reduce affine polynomials. For now just disable
480 // indvar subst on anything more complex than an affine addrec.
481 if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV))
482 if (AR->isAffine())
483 IndVars.push_back(std::make_pair(PN, SCEV));
484 }
485 }
486
487 // If there are no induction variables in the loop, there is nothing more to
488 // do.
489 if (IndVars.empty()) {
490 // Actually, if we know how many times the loop iterates, lets insert a
491 // canonical induction variable to help subsequent passes.
492 if (!isa<SCEVCouldNotCompute>(IterationCount)) {
493 SCEVExpander Rewriter(*SE, *LI);
494 Rewriter.getOrInsertCanonicalInductionVariable(L,
495 IterationCount->getType());
496 if (Instruction *I = LinearFunctionTestReplace(L, IterationCount,
497 Rewriter)) {
498 std::set<Instruction*> InstructionsToDelete;
499 InstructionsToDelete.insert(I);
500 DeleteTriviallyDeadInstructions(InstructionsToDelete);
501 }
502 }
503 return Changed;
504 }
505
506 // Compute the type of the largest recurrence expression.
507 //
508 const Type *LargestType = IndVars[0].first->getType();
509 bool DifferingSizes = false;
510 for (unsigned i = 1, e = IndVars.size(); i != e; ++i) {
511 const Type *Ty = IndVars[i].first->getType();
512 DifferingSizes |=
513 Ty->getPrimitiveSizeInBits() != LargestType->getPrimitiveSizeInBits();
514 if (Ty->getPrimitiveSizeInBits() > LargestType->getPrimitiveSizeInBits())
515 LargestType = Ty;
516 }
517
518 // Create a rewriter object which we'll use to transform the code with.
519 SCEVExpander Rewriter(*SE, *LI);
520
521 // Now that we know the largest of of the induction variables in this loop,
522 // insert a canonical induction variable of the largest size.
523 Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
524 ++NumInserted;
525 Changed = true;
526 DOUT << "INDVARS: New CanIV: " << *IndVar;
527
528 if (!isa<SCEVCouldNotCompute>(IterationCount)) {
Wojciech Matyjewiczc561c132008-06-13 17:02:03 +0000529 IterationCount = SE->getTruncateOrZeroExtend(IterationCount, LargestType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 if (Instruction *DI = LinearFunctionTestReplace(L, IterationCount,Rewriter))
531 DeadInsts.insert(DI);
532 }
533
534 // Now that we have a canonical induction variable, we can rewrite any
535 // recurrences in terms of the induction variable. Start with the auxillary
536 // induction variables, and recursively rewrite any of their uses.
Dan Gohman514277c2008-05-23 21:05:58 +0000537 BasicBlock::iterator InsertPt = Header->getFirstNonPHI();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
539 // If there were induction variables of other sizes, cast the primary
540 // induction variable to the right size for them, avoiding the need for the
541 // code evaluation methods to insert induction variables of different sizes.
542 if (DifferingSizes) {
543 SmallVector<unsigned,4> InsertedSizes;
544 InsertedSizes.push_back(LargestType->getPrimitiveSizeInBits());
545 for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
546 unsigned ithSize = IndVars[i].first->getType()->getPrimitiveSizeInBits();
547 if (std::find(InsertedSizes.begin(), InsertedSizes.end(), ithSize)
548 == InsertedSizes.end()) {
549 PHINode *PN = IndVars[i].first;
550 InsertedSizes.push_back(ithSize);
551 Instruction *New = new TruncInst(IndVar, PN->getType(), "indvar",
552 InsertPt);
553 Rewriter.addInsertedValue(New, SE->getSCEV(New));
554 DOUT << "INDVARS: Made trunc IV for " << *PN
555 << " NewVal = " << *New << "\n";
556 }
557 }
558 }
559
560 // Rewrite all induction variables in terms of the canonical induction
561 // variable.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 while (!IndVars.empty()) {
563 PHINode *PN = IndVars.back().first;
564 Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt);
565 DOUT << "INDVARS: Rewrote IV '" << *IndVars.back().second << "' " << *PN
566 << " into = " << *NewVal << "\n";
567 NewVal->takeName(PN);
568
569 // Replace the old PHI Node with the inserted computation.
570 PN->replaceAllUsesWith(NewVal);
571 DeadInsts.insert(PN);
572 IndVars.pop_back();
573 ++NumRemoved;
574 Changed = true;
575 }
576
577#if 0
578 // Now replace all derived expressions in the loop body with simpler
579 // expressions.
Dan Gohman4d2e8ae2008-06-22 20:18:58 +0000580 for (LoopInfo::block_iterator I = L->block_begin(), E = L->block_end();
581 I != E; ++I) {
582 BasicBlock *BB = *I;
583 if (LI->getLoopFor(BB) == L) { // Not in a subloop...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
585 if (I->getType()->isInteger() && // Is an integer instruction
586 !I->use_empty() &&
587 !Rewriter.isInsertedInstruction(I)) {
588 SCEVHandle SH = SE->getSCEV(I);
589 Value *V = Rewriter.expandCodeFor(SH, I, I->getType());
590 if (V != I) {
591 if (isa<Instruction>(V))
592 V->takeName(I);
593 I->replaceAllUsesWith(V);
594 DeadInsts.insert(I);
595 ++NumRemoved;
596 Changed = true;
597 }
598 }
599 }
Dan Gohman4d2e8ae2008-06-22 20:18:58 +0000600 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601#endif
602
603 DeleteTriviallyDeadInstructions(DeadInsts);
Devang Patelbda43802008-09-09 21:41:07 +0000604 OptimizeCanonicalIVType(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 assert(L->isLCSSAForm());
606 return Changed;
607}
Devang Patelbda43802008-09-09 21:41:07 +0000608
609/// OptimizeCanonicalIVType - If loop induction variable is always
Devang Patel7c9fc2a2008-09-10 14:49:55 +0000610/// sign or zero extended then extend the type of the induction
Devang Patelbda43802008-09-09 21:41:07 +0000611/// variable.
612void IndVarSimplify::OptimizeCanonicalIVType(Loop *L) {
613 PHINode *PH = L->getCanonicalInductionVariable();
614 if (!PH) return;
615
616 // Check loop iteration count.
617 SCEVHandle IC = SE->getIterationCount(L);
618 if (isa<SCEVCouldNotCompute>(IC)) return;
619 SCEVConstant *IterationCount = dyn_cast<SCEVConstant>(IC);
620 if (!IterationCount) return;
621
622 unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
623 unsigned BackEdge = IncomingEdge^1;
624
625 // Check IV uses. If all IV uses are either SEXT or ZEXT (except
626 // IV increment instruction) then this IV is suitable for this
Devang Patel7c9fc2a2008-09-10 14:49:55 +0000627 // transformation.
628 bool isSEXT = false;
Devang Patelbda43802008-09-09 21:41:07 +0000629 BinaryOperator *Incr = NULL;
Devang Patel7c9fc2a2008-09-10 14:49:55 +0000630 const Type *NewType = NULL;
Devang Patelbda43802008-09-09 21:41:07 +0000631 for(Value::use_iterator UI = PH->use_begin(), UE = PH->use_end();
632 UI != UE; ++UI) {
633 const Type *CandidateType = NULL;
634 if (ZExtInst *ZI = dyn_cast<ZExtInst>(UI))
635 CandidateType = ZI->getDestTy();
636 else if (SExtInst *SI = dyn_cast<SExtInst>(UI)) {
637 CandidateType = SI->getDestTy();
Devang Patel7c9fc2a2008-09-10 14:49:55 +0000638 isSEXT = true;
Devang Patelbda43802008-09-09 21:41:07 +0000639 }
640 else if ((Incr = dyn_cast<BinaryOperator>(UI))) {
641 // Validate IV increment instruction.
642 if (PH->getIncomingValue(BackEdge) == Incr)
643 continue;
644 }
645 if (!CandidateType) {
646 NewType = NULL;
647 break;
648 }
649 if (!NewType)
650 NewType = CandidateType;
651 else if (NewType != CandidateType) {
652 NewType = NULL;
653 break;
654 }
655 }
656
657 // IV uses are not suitable then avoid this transformation.
658 if (!NewType || !Incr)
659 return;
660
661 // IV increment instruction has two uses, one is loop exit condition
662 // and second is the IV (phi node) itself.
663 ICmpInst *Exit = NULL;
664 for(Value::use_iterator II = Incr->use_begin(), IE = Incr->use_end();
665 II != IE; ++II) {
666 if (PH == *II) continue;
667 Exit = dyn_cast<ICmpInst>(*II);
668 break;
669 }
670 if (!Exit) return;
671 ConstantInt *EV = dyn_cast<ConstantInt>(Exit->getOperand(0));
672 if (!EV)
673 EV = dyn_cast<ConstantInt>(Exit->getOperand(1));
674 if (!EV) return;
675
676 // Check iteration count max value to avoid loops that wrap around IV.
677 APInt ICount = IterationCount->getValue()->getValue();
678 if (ICount.isNegative()) return;
679 uint32_t BW = PH->getType()->getPrimitiveSizeInBits();
680 APInt Max = (isSEXT ? APInt::getSignedMaxValue(BW) : APInt::getMaxValue(BW));
681 if (ICount.getZExtValue() > Max.getZExtValue()) return;
682
683 // Extend IV type.
684
685 SCEVExpander Rewriter(*SE, *LI);
686 Value *NewIV = Rewriter.getOrInsertCanonicalInductionVariable(L,NewType);
687 PHINode *NewPH = cast<PHINode>(NewIV);
688 Instruction *NewIncr = cast<Instruction>(NewPH->getIncomingValue(BackEdge));
689
690 // Replace all SEXT or ZEXT uses.
691 SmallVector<Instruction *, 4> PHUses;
692 for(Value::use_iterator UI = PH->use_begin(), UE = PH->use_end();
693 UI != UE; ++UI) {
694 Instruction *I = cast<Instruction>(UI);
695 PHUses.push_back(I);
696 }
697 while (!PHUses.empty()){
698 Instruction *Use = PHUses.back(); PHUses.pop_back();
699 if (Incr == Use) continue;
700
701 SE->deleteValueFromRecords(Use);
702 Use->replaceAllUsesWith(NewIV);
703 Use->eraseFromParent();
704 }
705
706 // Replace exit condition.
707 ConstantInt *NEV = ConstantInt::get(NewType, EV->getZExtValue());
708 Instruction *NE = new ICmpInst(Exit->getPredicate(),
709 NewIncr, NEV, "new.exit",
710 Exit->getParent()->getTerminator());
711 SE->deleteValueFromRecords(Exit);
712 Exit->replaceAllUsesWith(NE);
713 Exit->eraseFromParent();
714
715 // Remove old IV and increment instructions.
716 SE->deleteValueFromRecords(PH);
717 PH->removeIncomingValue((unsigned)0);
718 PH->removeIncomingValue((unsigned)0);
719 SE->deleteValueFromRecords(Incr);
720 Incr->eraseFromParent();
721}
722
Devang Patel7ca23c92008-11-03 18:32:19 +0000723/// HandleFloatingPointIV - If the loop has floating induction variable
724/// then insert corresponding integer induction variable if possible.
725void IndVarSimplify::HandleFloatingPointIV(Loop *L) {
726 BasicBlock *Header = L->getHeader();
727 SmallVector <PHINode *, 4> FPHIs;
728 Instruction *NonPHIInsn = NULL;
729
730 // Collect all floating point IVs first.
731 BasicBlock::iterator I = Header->begin();
732 while(true) {
733 if (!isa<PHINode>(I)) {
734 NonPHIInsn = I;
735 break;
736 }
737 PHINode *PH = cast<PHINode>(I);
738 if (PH->getType()->isFloatingPoint())
739 FPHIs.push_back(PH);
740 ++I;
741 }
742
743 for (SmallVector<PHINode *, 4>::iterator I = FPHIs.begin(), E = FPHIs.end();
744 I != E; ++I) {
745 PHINode *PH = *I;
746 unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
747 unsigned BackEdge = IncomingEdge^1;
748
749 // Check incoming value.
750 ConstantFP *CZ = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge));
751 if (!CZ) continue;
752 APFloat PHInit = CZ->getValueAPF();
753 if (!PHInit.isPosZero()) continue;
754
755 // Check IV increment.
756 BinaryOperator *Incr =
757 dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge));
758 if (!Incr) continue;
759 if (Incr->getOpcode() != Instruction::Add) continue;
760 ConstantFP *IncrValue = NULL;
761 unsigned IncrVIndex = 1;
762 if (Incr->getOperand(1) == PH)
763 IncrVIndex = 0;
764 IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex));
765 if (!IncrValue) continue;
766 APFloat IVAPF = IncrValue->getValueAPF();
767 APFloat One = APFloat(IVAPF.getSemantics(), 1);
768 if (!IVAPF.bitwiseIsEqual(One)) continue;
769
770 // Check Incr uses.
771 Value::use_iterator IncrUse = Incr->use_begin();
772 Instruction *U1 = cast<Instruction>(IncrUse++);
773 if (IncrUse == Incr->use_end()) continue;
774 Instruction *U2 = cast<Instruction>(IncrUse++);
775 if (IncrUse != Incr->use_end()) continue;
776
777 // Find exict condition.
778 FCmpInst *EC = dyn_cast<FCmpInst>(U1);
779 if (!EC)
780 EC = dyn_cast<FCmpInst>(U2);
781 if (!EC) continue;
782 bool skip = false;
783 Instruction *Terminator = EC->getParent()->getTerminator();
784 for(Value::use_iterator ECUI = EC->use_begin(), ECUE = EC->use_end();
785 ECUI != ECUE; ++ECUI) {
786 Instruction *U = cast<Instruction>(ECUI);
787 if (U != Terminator) {
788 skip = true;
789 break;
790 }
791 }
792 if (skip) continue;
793
794 // Find exit value.
795 ConstantFP *EV = NULL;
796 unsigned EVIndex = 1;
797 if (EC->getOperand(1) == Incr)
798 EVIndex = 0;
799 EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex));
800 if (!EV) continue;
801 APFloat EVAPF = EV->getValueAPF();
802 if (EVAPF.isNegative()) continue;
803
804 // Find corresponding integer exit value.
805 uint64_t integerVal = Type::Int32Ty->getPrimitiveSizeInBits();
806 bool isExact = false;
807 if (EVAPF.convertToInteger(&integerVal, 32, false, APFloat::rmTowardZero, &isExact)
808 != APFloat::opOK)
809 continue;
810 if (!isExact) continue;
811
812 // Find new predicate for integer comparison.
813 CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
814 switch (EC->getPredicate()) {
815 case CmpInst::FCMP_OEQ:
816 case CmpInst::FCMP_UEQ:
817 NewPred = CmpInst::ICMP_EQ;
818 break;
819 case CmpInst::FCMP_OGT:
820 case CmpInst::FCMP_UGT:
821 NewPred = CmpInst::ICMP_UGT;
822 break;
823 case CmpInst::FCMP_OGE:
824 case CmpInst::FCMP_UGE:
825 NewPred = CmpInst::ICMP_UGE;
826 break;
827 case CmpInst::FCMP_OLT:
828 case CmpInst::FCMP_ULT:
829 NewPred = CmpInst::ICMP_ULT;
830 break;
831 case CmpInst::FCMP_OLE:
832 case CmpInst::FCMP_ULE:
833 NewPred = CmpInst::ICMP_ULE;
834 break;
835 default:
836 break;
837 }
838 if (NewPred == CmpInst::BAD_ICMP_PREDICATE) continue;
839
840 // Insert new integer induction variable.
841 SCEVExpander Rewriter(*SE, *LI);
842 PHINode *NewIV =
843 cast<PHINode>(Rewriter.getOrInsertCanonicalInductionVariable(L,Type::Int32Ty));
844 ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, integerVal);
845 Value *LHS = (EVIndex == 1 ? NewIV->getIncomingValue(BackEdge) : NewEV);
846 Value *RHS = (EVIndex == 1 ? NewEV : NewIV->getIncomingValue(BackEdge));
847 ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(),
848 EC->getParent()->getTerminator());
849
850 // Delete old, floating point, exit comparision instruction.
851 SE->deleteValueFromRecords(EC);
852 EC->replaceAllUsesWith(NewEC);
853 EC->eraseFromParent();
854
855 // Delete old, floating point, increment instruction.
856 SE->deleteValueFromRecords(Incr);
857 Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
858 Incr->eraseFromParent();
859
860 // Replace floating induction variable.
861 UIToFPInst *Conv = new UIToFPInst(NewIV, PH->getType(), "indvar.conv",
862 NonPHIInsn);
863 PH->replaceAllUsesWith(Conv);
864
865 SE->deleteValueFromRecords(PH);
866 PH->removeIncomingValue((unsigned)0);
867 PH->removeIncomingValue((unsigned)0);
868 }
869}
870