blob: dddf43ea573a4186951f80d79a83695254129f31 [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//
Dan Gohman45b31972008-05-14 00:24:14 +000014// The process of unrolling can produce extraneous basic blocks linked with
15// unconditional branches. This will be corrected in the future.
Chris Lattnerb298db72011-01-11 08:00:40 +000016//
Dan Gohman45b31972008-05-14 00:24:14 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "loop-unroll"
20#include "llvm/Transforms/Utils/UnrollLoop.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/ADT/Statistic.h"
Duncan Sandsb6133d12010-11-23 20:26:33 +000023#include "llvm/Analysis/InstructionSimplify.h"
Dan Gohman45b31972008-05-14 00:24:14 +000024#include "llvm/Analysis/LoopPass.h"
Dan Gohman572365e2010-07-26 18:02:06 +000025#include "llvm/Analysis/ScalarEvolution.h"
Dan Gohman45b31972008-05-14 00:24:14 +000026#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattner29874e02008-12-03 19:44:02 +000028#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dan Gohman45b31972008-05-14 00:24:14 +000029#include "llvm/Transforms/Utils/Cloning.h"
30#include "llvm/Transforms/Utils/Local.h"
Dan Gohman45b31972008-05-14 00:24:14 +000031using namespace llvm;
32
Chris Lattner29874e02008-12-03 19:44:02 +000033// TODO: Should these be here or in LoopUnroll?
Dan Gohman45b31972008-05-14 00:24:14 +000034STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
Chris Lattnerb298db72011-01-11 08:00:40 +000035STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
Dan Gohman45b31972008-05-14 00:24:14 +000036
37/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +000038/// current values into those specified by VMap.
Dan Gohman45b31972008-05-14 00:24:14 +000039static inline void RemapInstruction(Instruction *I,
Rafael Espindola1ed219a2010-10-13 01:36:30 +000040 ValueToValueMapTy &VMap) {
Dan Gohman45b31972008-05-14 00:24:14 +000041 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
42 Value *Op = I->getOperand(op);
Rafael Espindola1ed219a2010-10-13 01:36:30 +000043 ValueToValueMapTy::iterator It = VMap.find(Op);
Devang Patel29d3dd82010-06-23 23:55:51 +000044 if (It != VMap.end())
Dan Gohmanb56c9662009-10-31 14:46:50 +000045 I->setOperand(op, It->second);
Dan Gohman45b31972008-05-14 00:24:14 +000046 }
Jay Foad95c3e482011-06-23 09:09:15 +000047
48 if (PHINode *PN = dyn_cast<PHINode>(I)) {
49 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
50 ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
51 if (It != VMap.end())
52 PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
53 }
54 }
Dan Gohman45b31972008-05-14 00:24:14 +000055}
56
Dan Gohman438b5832009-10-31 17:33:01 +000057/// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
58/// only has one predecessor, and that predecessor only has one successor.
59/// The LoopInfo Analysis that is passed will be kept consistent.
60/// Returns the new combined block.
61static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) {
62 // Merge basic blocks into their predecessor if there is only one distinct
63 // pred, and if there is only one distinct successor of the predecessor, and
64 // if there are no PHI nodes.
65 BasicBlock *OnlyPred = BB->getSinglePredecessor();
66 if (!OnlyPred) return 0;
67
68 if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
69 return 0;
70
David Greenea9ad9c22010-01-05 01:26:41 +000071 DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred);
Dan Gohman438b5832009-10-31 17:33:01 +000072
73 // Resolve any PHI nodes at the start of the block. They are all
74 // guaranteed to have exactly one entry if they exist, unless there are
75 // multiple duplicate (but guaranteed to be equal) entries for the
76 // incoming edges. This occurs when there are multiple edges from
77 // OnlyPred to OnlySucc.
78 FoldSingleEntryPHINodes(BB);
79
80 // Delete the unconditional branch from the predecessor...
81 OnlyPred->getInstList().pop_back();
82
Dan Gohman438b5832009-10-31 17:33:01 +000083 // Make all PHI nodes that referred to BB now refer to Pred as their
84 // source...
85 BB->replaceAllUsesWith(OnlyPred);
86
Jay Foad95c3e482011-06-23 09:09:15 +000087 // Move all definitions in the successor to the predecessor...
88 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
89
Dan Gohman438b5832009-10-31 17:33:01 +000090 std::string OldName = BB->getName();
91
92 // Erase basic block from the function...
93 LI->removeBlock(BB);
94 BB->eraseFromParent();
95
96 // Inherit predecessor's name if it exists...
97 if (!OldName.empty() && !OnlyPred->hasName())
98 OnlyPred->setName(OldName);
99
100 return OnlyPred;
101}
102
Dan Gohman45b31972008-05-14 00:24:14 +0000103/// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000104/// if unrolling was successful, or false if the loop was unmodified. Unrolling
Dan Gohman45b31972008-05-14 00:24:14 +0000105/// can only fail when the loop's latch block is not terminated by a conditional
106/// branch instruction. However, if the trip count (and multiple) are not known,
107/// loop unrolling will mostly produce more code that is no faster.
108///
Andrew Trick478849e2011-07-25 22:17:47 +0000109/// TripCount is generally defined as the number of times the loop header
110/// executes. UnrollLoop relaxes the definition to permit early exits: here
111/// TripCount is the iteration on which control exits LatchBlock if no early
112/// exits were taken. Note that UnrollLoop assumes that the loop counter test
113/// terminates LatchBlock in order to remove unnecesssary instances of the
114/// test. In other words, control may exit the loop prior to TripCount
115/// iterations via an early branch, but control may not exit the loop from the
116/// LatchBlock's terminator prior to TripCount iterations.
117///
118/// Similarly, TripMultiple divides the number of times that the LatchBlock may
119/// execute without exiting the loop.
120///
Dan Gohman45b31972008-05-14 00:24:14 +0000121/// The LoopInfo Analysis that is passed will be kept consistent.
122///
123/// If a LoopPassManager is passed in, and the loop is fully removed, it will be
124/// removed from the LoopPassManager as well. LPM can also be NULL.
Andrew Trick2045ce12011-07-23 00:33:05 +0000125bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount,
126 unsigned TripMultiple, LoopInfo *LI, LPPassManager *LPM) {
Dan Gohman692ad8d2009-11-05 19:44:06 +0000127 BasicBlock *Preheader = L->getLoopPreheader();
128 if (!Preheader) {
David Greenea9ad9c22010-01-05 01:26:41 +0000129 DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
Dan Gohman692ad8d2009-11-05 19:44:06 +0000130 return false;
131 }
132
Dan Gohman45b31972008-05-14 00:24:14 +0000133 BasicBlock *LatchBlock = L->getLoopLatch();
Dan Gohman692ad8d2009-11-05 19:44:06 +0000134 if (!LatchBlock) {
David Greenea9ad9c22010-01-05 01:26:41 +0000135 DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
Dan Gohman692ad8d2009-11-05 19:44:06 +0000136 return false;
137 }
138
139 BasicBlock *Header = L->getHeader();
Dan Gohman45b31972008-05-14 00:24:14 +0000140 BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
Andrew Trickba033772011-07-23 00:29:16 +0000141
Dan Gohman45b31972008-05-14 00:24:14 +0000142 if (!BI || BI->isUnconditional()) {
143 // The loop-rotate pass can be helpful to avoid this in many cases.
David Greenea9ad9c22010-01-05 01:26:41 +0000144 DEBUG(dbgs() <<
Chris Lattnerbdff5482009-08-23 04:37:46 +0000145 " Can't unroll; loop not terminated by a conditional branch.\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000146 return false;
147 }
Andrew Trickba033772011-07-23 00:29:16 +0000148
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000149 if (Header->hasAddressTaken()) {
150 // The loop-rotate pass can be helpful to avoid this in many cases.
151 DEBUG(dbgs() <<
152 " Won't unroll loop: address of header block is taken.\n");
153 return false;
154 }
Dan Gohman45b31972008-05-14 00:24:14 +0000155
Dan Gohman572365e2010-07-26 18:02:06 +0000156 // Notify ScalarEvolution that the loop will be substantially changed,
157 // if not outright eliminated.
158 if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>())
159 SE->forgetLoop(L);
160
Dan Gohman45b31972008-05-14 00:24:14 +0000161 if (TripCount != 0)
David Greenea9ad9c22010-01-05 01:26:41 +0000162 DEBUG(dbgs() << " Trip Count = " << TripCount << "\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000163 if (TripMultiple != 1)
David Greenea9ad9c22010-01-05 01:26:41 +0000164 DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000165
166 // Effectively "DCE" unrolled iterations that are beyond the tripcount
167 // and will never be executed.
168 if (TripCount != 0 && Count > TripCount)
169 Count = TripCount;
170
171 assert(Count > 0);
172 assert(TripMultiple > 0);
173 assert(TripCount == 0 || TripCount % TripMultiple == 0);
174
175 // Are we eliminating the loop control altogether?
176 bool CompletelyUnroll = Count == TripCount;
177
178 // If we know the trip count, we know the multiple...
179 unsigned BreakoutTrip = 0;
180 if (TripCount != 0) {
181 BreakoutTrip = TripCount % Count;
182 TripMultiple = 0;
183 } else {
184 // Figure out what multiple to use.
185 BreakoutTrip = TripMultiple =
186 (unsigned)GreatestCommonDivisor64(Count, TripMultiple);
187 }
188
189 if (CompletelyUnroll) {
David Greenea9ad9c22010-01-05 01:26:41 +0000190 DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000191 << " with trip count " << TripCount << "!\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000192 } else {
David Greenea9ad9c22010-01-05 01:26:41 +0000193 DEBUG(dbgs() << "UNROLLING loop %" << Header->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000194 << " by " << Count);
Dan Gohman45b31972008-05-14 00:24:14 +0000195 if (TripMultiple == 0 || BreakoutTrip != TripMultiple) {
David Greenea9ad9c22010-01-05 01:26:41 +0000196 DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip);
Dan Gohman45b31972008-05-14 00:24:14 +0000197 } else if (TripMultiple != 1) {
David Greenea9ad9c22010-01-05 01:26:41 +0000198 DEBUG(dbgs() << " with " << TripMultiple << " trips per branch");
Dan Gohman45b31972008-05-14 00:24:14 +0000199 }
David Greenea9ad9c22010-01-05 01:26:41 +0000200 DEBUG(dbgs() << "!\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000201 }
202
203 std::vector<BasicBlock*> LoopBlocks = L->getBlocks();
204
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000205 bool ContinueOnTrue = L->contains(BI->getSuccessor(0));
Dan Gohman45b31972008-05-14 00:24:14 +0000206 BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue);
207
208 // For the first iteration of the loop, we should use the precloned values for
209 // PHI nodes. Insert associations now.
Devang Patel39430842010-04-20 22:24:18 +0000210 ValueToValueMapTy LastValueMap;
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000211 std::vector<PHINode*> OrigPHINode;
Dan Gohman45b31972008-05-14 00:24:14 +0000212 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
213 PHINode *PN = cast<PHINode>(I);
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000214 OrigPHINode.push_back(PN);
Andrew Trickba033772011-07-23 00:29:16 +0000215 if (Instruction *I =
Dan Gohman45b31972008-05-14 00:24:14 +0000216 dyn_cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)))
Dan Gohman92329c72009-12-18 01:24:09 +0000217 if (L->contains(I))
Dan Gohman45b31972008-05-14 00:24:14 +0000218 LastValueMap[I] = I;
219 }
220
221 std::vector<BasicBlock*> Headers;
222 std::vector<BasicBlock*> Latches;
223 Headers.push_back(Header);
224 Latches.push_back(LatchBlock);
225
226 for (unsigned It = 1; It != Count; ++It) {
Dan Gohman45b31972008-05-14 00:24:14 +0000227 std::vector<BasicBlock*> NewBlocks;
Andrew Trickba033772011-07-23 00:29:16 +0000228
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000229 for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(),
230 E = LoopBlocks.end(); BB != E; ++BB) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000231 ValueToValueMapTy VMap;
232 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000233 Header->getParent()->getBasicBlockList().push_back(New);
Dan Gohman45b31972008-05-14 00:24:14 +0000234
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000235 // Loop over all of the PHI nodes in the block, changing them to use the
236 // incoming values from the previous block.
237 if (*BB == Header)
238 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000239 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[i]]);
Dan Gohman45b31972008-05-14 00:24:14 +0000240 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
241 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
Dan Gohman92329c72009-12-18 01:24:09 +0000242 if (It > 1 && L->contains(InValI))
Dan Gohman45b31972008-05-14 00:24:14 +0000243 InVal = LastValueMap[InValI];
Devang Patel29d3dd82010-06-23 23:55:51 +0000244 VMap[OrigPHINode[i]] = InVal;
Dan Gohman45b31972008-05-14 00:24:14 +0000245 New->getInstList().erase(NewPHI);
246 }
247
248 // Update our running map of newest clones
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000249 LastValueMap[*BB] = New;
Devang Patel29d3dd82010-06-23 23:55:51 +0000250 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
Dan Gohman45b31972008-05-14 00:24:14 +0000251 VI != VE; ++VI)
252 LastValueMap[VI->first] = VI->second;
253
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000254 L->addBasicBlockToLoop(New, LI->getBase());
255
256 // Add phi entries for newly created values to all exit blocks except
257 // the successor of the latch block. The successor of the exit block will
258 // be updated specially after unrolling all the way.
259 if (*BB != LatchBlock)
Jay Foad95c3e482011-06-23 09:09:15 +0000260 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB); SI != SE;
261 ++SI)
262 if (!L->contains(*SI))
263 for (BasicBlock::iterator BBI = (*SI)->begin();
264 PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) {
265 Value *Incoming = phi->getIncomingValueForBlock(*BB);
266 phi->addIncoming(Incoming, New);
267 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000268
269 // Keep track of new headers and latches as we create them, so that
270 // we can insert the proper branches later.
271 if (*BB == Header)
272 Headers.push_back(New);
273 if (*BB == LatchBlock) {
274 Latches.push_back(New);
275
276 // Also, clear out the new latch's back edge so that it doesn't look
277 // like a new loop, so that it's amenable to being merged with adjacent
278 // blocks later on.
279 TerminatorInst *Term = New->getTerminator();
280 assert(L->contains(Term->getSuccessor(!ContinueOnTrue)));
281 assert(Term->getSuccessor(ContinueOnTrue) == LoopExit);
282 Term->setSuccessor(!ContinueOnTrue, NULL);
Dan Gohman45b31972008-05-14 00:24:14 +0000283 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000284
285 NewBlocks.push_back(New);
Dan Gohman45b31972008-05-14 00:24:14 +0000286 }
Andrew Trickba033772011-07-23 00:29:16 +0000287
Dan Gohman45b31972008-05-14 00:24:14 +0000288 // Remap all instructions in the most recent iteration
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000289 for (unsigned i = 0; i < NewBlocks.size(); ++i)
Dan Gohman45b31972008-05-14 00:24:14 +0000290 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
291 E = NewBlocks[i]->end(); I != E; ++I)
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000292 ::RemapInstruction(I, LastValueMap);
Dan Gohman45b31972008-05-14 00:24:14 +0000293 }
Andrew Trickba033772011-07-23 00:29:16 +0000294
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000295 // The latch block exits the loop. If there are any PHI nodes in the
296 // successor blocks, update them to use the appropriate values computed as the
297 // last iteration of the loop.
298 if (Count != 1) {
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000299 BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]);
Jay Foad95c3e482011-06-23 09:09:15 +0000300 for (succ_iterator SI = succ_begin(LatchBlock), SE = succ_end(LatchBlock);
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000301 SI != SE; ++SI) {
Jay Foad95c3e482011-06-23 09:09:15 +0000302 for (BasicBlock::iterator BBI = (*SI)->begin();
303 PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI) {
304 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
305 // If this value was defined in the loop, take the value defined by the
306 // last iteration of the loop.
307 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
308 if (L->contains(InValI))
309 InVal = LastValueMap[InVal];
310 }
311 PN->addIncoming(InVal, LastIterationBB);
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000312 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000313 }
314 }
315
316 // Now, if we're doing complete unrolling, loop over the PHI nodes in the
317 // original block, setting them to their incoming values.
318 if (CompletelyUnroll) {
319 BasicBlock *Preheader = L->getLoopPreheader();
320 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
321 PHINode *PN = OrigPHINode[i];
322 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
323 Header->getInstList().erase(PN);
324 }
325 }
Dan Gohman45b31972008-05-14 00:24:14 +0000326
327 // Now that all the basic blocks for the unrolled iterations are in place,
328 // set up the branches to connect them.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000329 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
Dan Gohman45b31972008-05-14 00:24:14 +0000330 // The original branch was replicated in each unrolled iteration.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000331 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
Dan Gohman45b31972008-05-14 00:24:14 +0000332
333 // The branch destination.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000334 unsigned j = (i + 1) % e;
335 BasicBlock *Dest = Headers[j];
Dan Gohman45b31972008-05-14 00:24:14 +0000336 bool NeedConditional = true;
337
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000338 // For a complete unroll, make the last iteration end with a branch
339 // to the exit block.
340 if (CompletelyUnroll && j == 0) {
Dan Gohman45b31972008-05-14 00:24:14 +0000341 Dest = LoopExit;
342 NeedConditional = false;
343 }
344
345 // If we know the trip count or a multiple of it, we can safely use an
346 // unconditional branch for some iterations.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000347 if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) {
Dan Gohman45b31972008-05-14 00:24:14 +0000348 NeedConditional = false;
349 }
350
351 if (NeedConditional) {
352 // Update the conditional branch's successor for the following
353 // iteration.
354 Term->setSuccessor(!ContinueOnTrue, Dest);
355 } else {
Jay Foad8f9ffbd2011-01-07 20:25:56 +0000356 // Replace the conditional branch with an unconditional one.
357 BranchInst::Create(Dest, Term);
358 Term->eraseFromParent();
Jay Foadcd35e092011-06-21 10:33:19 +0000359 }
360 }
361
Jay Foad95c3e482011-06-23 09:09:15 +0000362 // Merge adjacent basic blocks, if possible.
363 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
364 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
365 if (Term->isUnconditional()) {
366 BasicBlock *Dest = Term->getSuccessor(0);
367 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI))
368 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
369 }
370 }
Andrew Trickba033772011-07-23 00:29:16 +0000371
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000372 // At this point, the code is well formed. We now do a quick sweep over the
373 // inserted code, doing constant propagation and dead code elimination as we
374 // go.
375 const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
376 for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(),
377 BBE = NewLoopBlocks.end(); BB != BBE; ++BB)
378 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) {
Dan Gohman45b31972008-05-14 00:24:14 +0000379 Instruction *Inst = I++;
380
381 if (isInstructionTriviallyDead(Inst))
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000382 (*BB)->getInstList().erase(Inst);
Duncan Sandsb6133d12010-11-23 20:26:33 +0000383 else if (Value *V = SimplifyInstruction(Inst))
384 if (LI->replacementPreservesLCSSAForm(Inst, V)) {
385 Inst->replaceAllUsesWith(V);
386 (*BB)->getInstList().erase(Inst);
387 }
Dan Gohman45b31972008-05-14 00:24:14 +0000388 }
Dan Gohman45b31972008-05-14 00:24:14 +0000389
390 NumCompletelyUnrolled += CompletelyUnroll;
391 ++NumUnrolled;
392 // Remove the loop from the LoopPassManager if it's completely removed.
393 if (CompletelyUnroll && LPM != NULL)
394 LPM->deleteLoopFromQueue(L);
395
Dan Gohman45b31972008-05-14 00:24:14 +0000396 return true;
397}