blob: 6772511b5d5a2408c3d69a131e2eb8ac93d5a83e [file] [log] [blame]
Dan Gohman45b31972008-05-14 00:24:14 +00001//===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===//
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 file implements some loop unrolling utilities. It does not define any
11// actual pass or policy, but provides a single function to perform loop
12// unrolling.
13//
14// It works best when loops have been canonicalized by the -indvars pass,
15// allowing it to determine the trip counts of loops easily.
16//
17// The process of unrolling can produce extraneous basic blocks linked with
18// unconditional branches. This will be corrected in the future.
Chris Lattnerb298db72011-01-11 08:00:40 +000019//
Dan Gohman45b31972008-05-14 00:24:14 +000020//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "loop-unroll"
23#include "llvm/Transforms/Utils/UnrollLoop.h"
24#include "llvm/BasicBlock.h"
25#include "llvm/ADT/Statistic.h"
Duncan Sandsb6133d12010-11-23 20:26:33 +000026#include "llvm/Analysis/InstructionSimplify.h"
Dan Gohman45b31972008-05-14 00:24:14 +000027#include "llvm/Analysis/LoopPass.h"
Dan Gohman572365e2010-07-26 18:02:06 +000028#include "llvm/Analysis/ScalarEvolution.h"
Dan Gohman45b31972008-05-14 00:24:14 +000029#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattner29874e02008-12-03 19:44:02 +000031#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dan Gohman45b31972008-05-14 00:24:14 +000032#include "llvm/Transforms/Utils/Cloning.h"
33#include "llvm/Transforms/Utils/Local.h"
Dan Gohman45b31972008-05-14 00:24:14 +000034using namespace llvm;
35
Chris Lattner29874e02008-12-03 19:44:02 +000036// TODO: Should these be here or in LoopUnroll?
Dan Gohman45b31972008-05-14 00:24:14 +000037STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
Chris Lattnerb298db72011-01-11 08:00:40 +000038STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
Dan Gohman45b31972008-05-14 00:24:14 +000039
40/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +000041/// current values into those specified by VMap.
Dan Gohman45b31972008-05-14 00:24:14 +000042static inline void RemapInstruction(Instruction *I,
Rafael Espindola1ed219a2010-10-13 01:36:30 +000043 ValueToValueMapTy &VMap) {
Dan Gohman45b31972008-05-14 00:24:14 +000044 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
45 Value *Op = I->getOperand(op);
Rafael Espindola1ed219a2010-10-13 01:36:30 +000046 ValueToValueMapTy::iterator It = VMap.find(Op);
Devang Patel29d3dd82010-06-23 23:55:51 +000047 if (It != VMap.end())
Dan Gohmanb56c9662009-10-31 14:46:50 +000048 I->setOperand(op, It->second);
Dan Gohman45b31972008-05-14 00:24:14 +000049 }
Jay Foad95c3e482011-06-23 09:09:15 +000050
51 if (PHINode *PN = dyn_cast<PHINode>(I)) {
52 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
53 ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
54 if (It != VMap.end())
55 PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
56 }
57 }
Dan Gohman45b31972008-05-14 00:24:14 +000058}
59
Dan Gohman438b5832009-10-31 17:33:01 +000060/// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
61/// only has one predecessor, and that predecessor only has one successor.
62/// The LoopInfo Analysis that is passed will be kept consistent.
63/// Returns the new combined block.
64static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) {
65 // Merge basic blocks into their predecessor if there is only one distinct
66 // pred, and if there is only one distinct successor of the predecessor, and
67 // if there are no PHI nodes.
68 BasicBlock *OnlyPred = BB->getSinglePredecessor();
69 if (!OnlyPred) return 0;
70
71 if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
72 return 0;
73
David Greenea9ad9c22010-01-05 01:26:41 +000074 DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred);
Dan Gohman438b5832009-10-31 17:33:01 +000075
76 // Resolve any PHI nodes at the start of the block. They are all
77 // guaranteed to have exactly one entry if they exist, unless there are
78 // multiple duplicate (but guaranteed to be equal) entries for the
79 // incoming edges. This occurs when there are multiple edges from
80 // OnlyPred to OnlySucc.
81 FoldSingleEntryPHINodes(BB);
82
83 // Delete the unconditional branch from the predecessor...
84 OnlyPred->getInstList().pop_back();
85
Dan Gohman438b5832009-10-31 17:33:01 +000086 // Make all PHI nodes that referred to BB now refer to Pred as their
87 // source...
88 BB->replaceAllUsesWith(OnlyPred);
89
Jay Foad95c3e482011-06-23 09:09:15 +000090 // Move all definitions in the successor to the predecessor...
91 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
92
Dan Gohman438b5832009-10-31 17:33:01 +000093 std::string OldName = BB->getName();
94
95 // Erase basic block from the function...
96 LI->removeBlock(BB);
97 BB->eraseFromParent();
98
99 // Inherit predecessor's name if it exists...
100 if (!OldName.empty() && !OnlyPred->hasName())
101 OnlyPred->setName(OldName);
102
103 return OnlyPred;
104}
105
Dan Gohman45b31972008-05-14 00:24:14 +0000106/// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000107/// if unrolling was successful, or false if the loop was unmodified. Unrolling
Dan Gohman45b31972008-05-14 00:24:14 +0000108/// can only fail when the loop's latch block is not terminated by a conditional
109/// branch instruction. However, if the trip count (and multiple) are not known,
110/// loop unrolling will mostly produce more code that is no faster.
111///
112/// The LoopInfo Analysis that is passed will be kept consistent.
113///
114/// If a LoopPassManager is passed in, and the loop is fully removed, it will be
115/// removed from the LoopPassManager as well. LPM can also be NULL.
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000116bool llvm::UnrollLoop(Loop *L, unsigned Count,
117 LoopInfo *LI, LPPassManager *LPM) {
Dan Gohman692ad8d2009-11-05 19:44:06 +0000118 BasicBlock *Preheader = L->getLoopPreheader();
119 if (!Preheader) {
David Greenea9ad9c22010-01-05 01:26:41 +0000120 DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
Dan Gohman692ad8d2009-11-05 19:44:06 +0000121 return false;
122 }
123
Dan Gohman45b31972008-05-14 00:24:14 +0000124 BasicBlock *LatchBlock = L->getLoopLatch();
Dan Gohman692ad8d2009-11-05 19:44:06 +0000125 if (!LatchBlock) {
David Greenea9ad9c22010-01-05 01:26:41 +0000126 DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
Dan Gohman692ad8d2009-11-05 19:44:06 +0000127 return false;
128 }
129
130 BasicBlock *Header = L->getHeader();
Dan Gohman45b31972008-05-14 00:24:14 +0000131 BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
Jay Foad95c3e482011-06-23 09:09:15 +0000132
Dan Gohman45b31972008-05-14 00:24:14 +0000133 if (!BI || BI->isUnconditional()) {
134 // The loop-rotate pass can be helpful to avoid this in many cases.
David Greenea9ad9c22010-01-05 01:26:41 +0000135 DEBUG(dbgs() <<
Chris Lattnerbdff5482009-08-23 04:37:46 +0000136 " Can't unroll; loop not terminated by a conditional branch.\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000137 return false;
138 }
Jay Foad95c3e482011-06-23 09:09:15 +0000139
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000140 if (Header->hasAddressTaken()) {
141 // The loop-rotate pass can be helpful to avoid this in many cases.
142 DEBUG(dbgs() <<
143 " Won't unroll loop: address of header block is taken.\n");
144 return false;
145 }
Dan Gohman45b31972008-05-14 00:24:14 +0000146
Dan Gohman572365e2010-07-26 18:02:06 +0000147 // Notify ScalarEvolution that the loop will be substantially changed,
148 // if not outright eliminated.
149 if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>())
150 SE->forgetLoop(L);
151
Dan Gohman45b31972008-05-14 00:24:14 +0000152 // Find trip count
153 unsigned TripCount = L->getSmallConstantTripCount();
154 // Find trip multiple if count is not available
155 unsigned TripMultiple = 1;
156 if (TripCount == 0)
157 TripMultiple = L->getSmallConstantTripMultiple();
158
159 if (TripCount != 0)
David Greenea9ad9c22010-01-05 01:26:41 +0000160 DEBUG(dbgs() << " Trip Count = " << TripCount << "\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000161 if (TripMultiple != 1)
David Greenea9ad9c22010-01-05 01:26:41 +0000162 DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000163
164 // Effectively "DCE" unrolled iterations that are beyond the tripcount
165 // and will never be executed.
166 if (TripCount != 0 && Count > TripCount)
167 Count = TripCount;
168
169 assert(Count > 0);
170 assert(TripMultiple > 0);
171 assert(TripCount == 0 || TripCount % TripMultiple == 0);
172
173 // Are we eliminating the loop control altogether?
174 bool CompletelyUnroll = Count == TripCount;
175
176 // If we know the trip count, we know the multiple...
177 unsigned BreakoutTrip = 0;
178 if (TripCount != 0) {
179 BreakoutTrip = TripCount % Count;
180 TripMultiple = 0;
181 } else {
182 // Figure out what multiple to use.
183 BreakoutTrip = TripMultiple =
184 (unsigned)GreatestCommonDivisor64(Count, TripMultiple);
185 }
186
187 if (CompletelyUnroll) {
David Greenea9ad9c22010-01-05 01:26:41 +0000188 DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000189 << " with trip count " << TripCount << "!\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000190 } else {
David Greenea9ad9c22010-01-05 01:26:41 +0000191 DEBUG(dbgs() << "UNROLLING loop %" << Header->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000192 << " by " << Count);
Dan Gohman45b31972008-05-14 00:24:14 +0000193 if (TripMultiple == 0 || BreakoutTrip != TripMultiple) {
David Greenea9ad9c22010-01-05 01:26:41 +0000194 DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip);
Dan Gohman45b31972008-05-14 00:24:14 +0000195 } else if (TripMultiple != 1) {
David Greenea9ad9c22010-01-05 01:26:41 +0000196 DEBUG(dbgs() << " with " << TripMultiple << " trips per branch");
Dan Gohman45b31972008-05-14 00:24:14 +0000197 }
David Greenea9ad9c22010-01-05 01:26:41 +0000198 DEBUG(dbgs() << "!\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000199 }
200
201 std::vector<BasicBlock*> LoopBlocks = L->getBlocks();
202
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000203 bool ContinueOnTrue = L->contains(BI->getSuccessor(0));
Dan Gohman45b31972008-05-14 00:24:14 +0000204 BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue);
205
206 // For the first iteration of the loop, we should use the precloned values for
207 // PHI nodes. Insert associations now.
Devang Patel39430842010-04-20 22:24:18 +0000208 ValueToValueMapTy LastValueMap;
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000209 std::vector<PHINode*> OrigPHINode;
Dan Gohman45b31972008-05-14 00:24:14 +0000210 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
211 PHINode *PN = cast<PHINode>(I);
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000212 OrigPHINode.push_back(PN);
Jay Foad95c3e482011-06-23 09:09:15 +0000213 if (Instruction *I =
Dan Gohman45b31972008-05-14 00:24:14 +0000214 dyn_cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)))
Dan Gohman92329c72009-12-18 01:24:09 +0000215 if (L->contains(I))
Dan Gohman45b31972008-05-14 00:24:14 +0000216 LastValueMap[I] = I;
217 }
218
219 std::vector<BasicBlock*> Headers;
220 std::vector<BasicBlock*> Latches;
221 Headers.push_back(Header);
222 Latches.push_back(LatchBlock);
223
224 for (unsigned It = 1; It != Count; ++It) {
Dan Gohman45b31972008-05-14 00:24:14 +0000225 std::vector<BasicBlock*> NewBlocks;
Jay Foad95c3e482011-06-23 09:09:15 +0000226
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000227 for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(),
228 E = LoopBlocks.end(); BB != E; ++BB) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000229 ValueToValueMapTy VMap;
230 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000231 Header->getParent()->getBasicBlockList().push_back(New);
Dan Gohman45b31972008-05-14 00:24:14 +0000232
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000233 // Loop over all of the PHI nodes in the block, changing them to use the
234 // incoming values from the previous block.
235 if (*BB == Header)
236 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000237 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[i]]);
Dan Gohman45b31972008-05-14 00:24:14 +0000238 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
239 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
Dan Gohman92329c72009-12-18 01:24:09 +0000240 if (It > 1 && L->contains(InValI))
Dan Gohman45b31972008-05-14 00:24:14 +0000241 InVal = LastValueMap[InValI];
Devang Patel29d3dd82010-06-23 23:55:51 +0000242 VMap[OrigPHINode[i]] = InVal;
Dan Gohman45b31972008-05-14 00:24:14 +0000243 New->getInstList().erase(NewPHI);
244 }
245
246 // Update our running map of newest clones
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000247 LastValueMap[*BB] = New;
Devang Patel29d3dd82010-06-23 23:55:51 +0000248 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
Dan Gohman45b31972008-05-14 00:24:14 +0000249 VI != VE; ++VI)
250 LastValueMap[VI->first] = VI->second;
251
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000252 L->addBasicBlockToLoop(New, LI->getBase());
253
254 // Add phi entries for newly created values to all exit blocks except
255 // the successor of the latch block. The successor of the exit block will
256 // be updated specially after unrolling all the way.
257 if (*BB != LatchBlock)
Jay Foad95c3e482011-06-23 09:09:15 +0000258 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB); SI != SE;
259 ++SI)
260 if (!L->contains(*SI))
261 for (BasicBlock::iterator BBI = (*SI)->begin();
262 PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) {
263 Value *Incoming = phi->getIncomingValueForBlock(*BB);
264 phi->addIncoming(Incoming, New);
265 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000266
267 // Keep track of new headers and latches as we create them, so that
268 // we can insert the proper branches later.
269 if (*BB == Header)
270 Headers.push_back(New);
271 if (*BB == LatchBlock) {
272 Latches.push_back(New);
273
274 // Also, clear out the new latch's back edge so that it doesn't look
275 // like a new loop, so that it's amenable to being merged with adjacent
276 // blocks later on.
277 TerminatorInst *Term = New->getTerminator();
278 assert(L->contains(Term->getSuccessor(!ContinueOnTrue)));
279 assert(Term->getSuccessor(ContinueOnTrue) == LoopExit);
280 Term->setSuccessor(!ContinueOnTrue, NULL);
Dan Gohman45b31972008-05-14 00:24:14 +0000281 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000282
283 NewBlocks.push_back(New);
Dan Gohman45b31972008-05-14 00:24:14 +0000284 }
Jay Foad95c3e482011-06-23 09:09:15 +0000285
Dan Gohman45b31972008-05-14 00:24:14 +0000286 // Remap all instructions in the most recent iteration
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000287 for (unsigned i = 0; i < NewBlocks.size(); ++i)
Dan Gohman45b31972008-05-14 00:24:14 +0000288 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
289 E = NewBlocks[i]->end(); I != E; ++I)
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000290 ::RemapInstruction(I, LastValueMap);
Dan Gohman45b31972008-05-14 00:24:14 +0000291 }
Jay Foad95c3e482011-06-23 09:09:15 +0000292
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000293 // The latch block exits the loop. If there are any PHI nodes in the
294 // successor blocks, update them to use the appropriate values computed as the
295 // last iteration of the loop.
296 if (Count != 1) {
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000297 BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]);
Jay Foad95c3e482011-06-23 09:09:15 +0000298 for (succ_iterator SI = succ_begin(LatchBlock), SE = succ_end(LatchBlock);
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000299 SI != SE; ++SI) {
Jay Foad95c3e482011-06-23 09:09:15 +0000300 for (BasicBlock::iterator BBI = (*SI)->begin();
301 PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI) {
302 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
303 // If this value was defined in the loop, take the value defined by the
304 // last iteration of the loop.
305 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
306 if (L->contains(InValI))
307 InVal = LastValueMap[InVal];
308 }
309 PN->addIncoming(InVal, LastIterationBB);
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000310 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000311 }
312 }
313
314 // Now, if we're doing complete unrolling, loop over the PHI nodes in the
315 // original block, setting them to their incoming values.
316 if (CompletelyUnroll) {
317 BasicBlock *Preheader = L->getLoopPreheader();
318 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
319 PHINode *PN = OrigPHINode[i];
320 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
321 Header->getInstList().erase(PN);
322 }
323 }
Dan Gohman45b31972008-05-14 00:24:14 +0000324
325 // Now that all the basic blocks for the unrolled iterations are in place,
326 // set up the branches to connect them.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000327 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
Dan Gohman45b31972008-05-14 00:24:14 +0000328 // The original branch was replicated in each unrolled iteration.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000329 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
Dan Gohman45b31972008-05-14 00:24:14 +0000330
331 // The branch destination.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000332 unsigned j = (i + 1) % e;
333 BasicBlock *Dest = Headers[j];
Dan Gohman45b31972008-05-14 00:24:14 +0000334 bool NeedConditional = true;
335
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000336 // For a complete unroll, make the last iteration end with a branch
337 // to the exit block.
338 if (CompletelyUnroll && j == 0) {
Dan Gohman45b31972008-05-14 00:24:14 +0000339 Dest = LoopExit;
340 NeedConditional = false;
341 }
342
343 // If we know the trip count or a multiple of it, we can safely use an
344 // unconditional branch for some iterations.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000345 if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) {
Dan Gohman45b31972008-05-14 00:24:14 +0000346 NeedConditional = false;
347 }
348
349 if (NeedConditional) {
350 // Update the conditional branch's successor for the following
351 // iteration.
352 Term->setSuccessor(!ContinueOnTrue, Dest);
353 } else {
Jay Foad8f9ffbd2011-01-07 20:25:56 +0000354 // Replace the conditional branch with an unconditional one.
355 BranchInst::Create(Dest, Term);
356 Term->eraseFromParent();
Jay Foadcd35e092011-06-21 10:33:19 +0000357 }
358 }
359
Jay Foad95c3e482011-06-23 09:09:15 +0000360 // Merge adjacent basic blocks, if possible.
361 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
362 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
363 if (Term->isUnconditional()) {
364 BasicBlock *Dest = Term->getSuccessor(0);
365 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI))
366 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
367 }
368 }
369
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000370 // At this point, the code is well formed. We now do a quick sweep over the
371 // inserted code, doing constant propagation and dead code elimination as we
372 // go.
373 const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
374 for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(),
375 BBE = NewLoopBlocks.end(); BB != BBE; ++BB)
376 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) {
Dan Gohman45b31972008-05-14 00:24:14 +0000377 Instruction *Inst = I++;
378
379 if (isInstructionTriviallyDead(Inst))
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000380 (*BB)->getInstList().erase(Inst);
Duncan Sandsb6133d12010-11-23 20:26:33 +0000381 else if (Value *V = SimplifyInstruction(Inst))
382 if (LI->replacementPreservesLCSSAForm(Inst, V)) {
383 Inst->replaceAllUsesWith(V);
384 (*BB)->getInstList().erase(Inst);
385 }
Dan Gohman45b31972008-05-14 00:24:14 +0000386 }
Dan Gohman45b31972008-05-14 00:24:14 +0000387
388 NumCompletelyUnrolled += CompletelyUnroll;
389 ++NumUnrolled;
390 // Remove the loop from the LoopPassManager if it's completely removed.
391 if (CompletelyUnroll && LPM != NULL)
392 LPM->deleteLoopFromQueue(L);
393
Dan Gohman45b31972008-05-14 00:24:14 +0000394 return true;
395}