blob: 6b2f9efe15014b2fcba4c32b0a3ac2609360e0d8 [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"
Andrew Trickb1eede12011-08-10 00:28:10 +000024#include "llvm/Analysis/LoopIterator.h"
Dan Gohman45b31972008-05-14 00:24:14 +000025#include "llvm/Analysis/LoopPass.h"
Dan Gohman572365e2010-07-26 18:02:06 +000026#include "llvm/Analysis/ScalarEvolution.h"
Dan Gohman45b31972008-05-14 00:24:14 +000027#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner29874e02008-12-03 19:44:02 +000029#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dan Gohman45b31972008-05-14 00:24:14 +000030#include "llvm/Transforms/Utils/Cloning.h"
31#include "llvm/Transforms/Utils/Local.h"
Dan Gohman45b31972008-05-14 00:24:14 +000032using namespace llvm;
33
Chris Lattner29874e02008-12-03 19:44:02 +000034// TODO: Should these be here or in LoopUnroll?
Dan Gohman45b31972008-05-14 00:24:14 +000035STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
Chris Lattnerb298db72011-01-11 08:00:40 +000036STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
Dan Gohman45b31972008-05-14 00:24:14 +000037
38/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +000039/// current values into those specified by VMap.
Dan Gohman45b31972008-05-14 00:24:14 +000040static inline void RemapInstruction(Instruction *I,
Rafael Espindola1ed219a2010-10-13 01:36:30 +000041 ValueToValueMapTy &VMap) {
Dan Gohman45b31972008-05-14 00:24:14 +000042 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
43 Value *Op = I->getOperand(op);
Rafael Espindola1ed219a2010-10-13 01:36:30 +000044 ValueToValueMapTy::iterator It = VMap.find(Op);
Devang Patel29d3dd82010-06-23 23:55:51 +000045 if (It != VMap.end())
Dan Gohmanb56c9662009-10-31 14:46:50 +000046 I->setOperand(op, It->second);
Dan Gohman45b31972008-05-14 00:24:14 +000047 }
Jay Foad95c3e482011-06-23 09:09:15 +000048
49 if (PHINode *PN = dyn_cast<PHINode>(I)) {
50 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
51 ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
52 if (It != VMap.end())
53 PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
54 }
55 }
Dan Gohman45b31972008-05-14 00:24:14 +000056}
57
Dan Gohman438b5832009-10-31 17:33:01 +000058/// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
59/// only has one predecessor, and that predecessor only has one successor.
60/// The LoopInfo Analysis that is passed will be kept consistent.
61/// Returns the new combined block.
Andrew Trick1009c322011-08-03 18:32:11 +000062static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI,
63 LPPassManager *LPM) {
Dan Gohman438b5832009-10-31 17:33:01 +000064 // Merge basic blocks into their predecessor if there is only one distinct
65 // pred, and if there is only one distinct successor of the predecessor, and
66 // if there are no PHI nodes.
67 BasicBlock *OnlyPred = BB->getSinglePredecessor();
68 if (!OnlyPred) return 0;
69
70 if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
71 return 0;
72
David Greenea9ad9c22010-01-05 01:26:41 +000073 DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred);
Dan Gohman438b5832009-10-31 17:33:01 +000074
75 // Resolve any PHI nodes at the start of the block. They are all
76 // guaranteed to have exactly one entry if they exist, unless there are
77 // multiple duplicate (but guaranteed to be equal) entries for the
78 // incoming edges. This occurs when there are multiple edges from
79 // OnlyPred to OnlySucc.
80 FoldSingleEntryPHINodes(BB);
81
82 // Delete the unconditional branch from the predecessor...
83 OnlyPred->getInstList().pop_back();
84
Dan Gohman438b5832009-10-31 17:33:01 +000085 // Make all PHI nodes that referred to BB now refer to Pred as their
86 // source...
87 BB->replaceAllUsesWith(OnlyPred);
88
Jay Foad95c3e482011-06-23 09:09:15 +000089 // Move all definitions in the successor to the predecessor...
90 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
91
Dan Gohman438b5832009-10-31 17:33:01 +000092 std::string OldName = BB->getName();
93
94 // Erase basic block from the function...
Andrew Trick1009c322011-08-03 18:32:11 +000095
96 // ScalarEvolution holds references to loop exit blocks.
97 if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>()) {
98 if (Loop *L = LI->getLoopFor(BB))
99 SE->forgetLoop(L);
100 }
Dan Gohman438b5832009-10-31 17:33:01 +0000101 LI->removeBlock(BB);
102 BB->eraseFromParent();
103
104 // Inherit predecessor's name if it exists...
105 if (!OldName.empty() && !OnlyPred->hasName())
106 OnlyPred->setName(OldName);
107
108 return OnlyPred;
109}
110
Dan Gohman45b31972008-05-14 00:24:14 +0000111/// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000112/// if unrolling was successful, or false if the loop was unmodified. Unrolling
Dan Gohman45b31972008-05-14 00:24:14 +0000113/// can only fail when the loop's latch block is not terminated by a conditional
114/// branch instruction. However, if the trip count (and multiple) are not known,
115/// loop unrolling will mostly produce more code that is no faster.
116///
Andrew Trick478849e2011-07-25 22:17:47 +0000117/// TripCount is generally defined as the number of times the loop header
118/// executes. UnrollLoop relaxes the definition to permit early exits: here
119/// TripCount is the iteration on which control exits LatchBlock if no early
120/// exits were taken. Note that UnrollLoop assumes that the loop counter test
121/// terminates LatchBlock in order to remove unnecesssary instances of the
122/// test. In other words, control may exit the loop prior to TripCount
123/// iterations via an early branch, but control may not exit the loop from the
124/// LatchBlock's terminator prior to TripCount iterations.
125///
126/// Similarly, TripMultiple divides the number of times that the LatchBlock may
127/// execute without exiting the loop.
128///
Dan Gohman45b31972008-05-14 00:24:14 +0000129/// The LoopInfo Analysis that is passed will be kept consistent.
130///
131/// If a LoopPassManager is passed in, and the loop is fully removed, it will be
132/// removed from the LoopPassManager as well. LPM can also be NULL.
Andrew Trick2045ce12011-07-23 00:33:05 +0000133bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount,
134 unsigned TripMultiple, LoopInfo *LI, LPPassManager *LPM) {
Dan Gohman692ad8d2009-11-05 19:44:06 +0000135 BasicBlock *Preheader = L->getLoopPreheader();
136 if (!Preheader) {
David Greenea9ad9c22010-01-05 01:26:41 +0000137 DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
Dan Gohman692ad8d2009-11-05 19:44:06 +0000138 return false;
139 }
140
Dan Gohman45b31972008-05-14 00:24:14 +0000141 BasicBlock *LatchBlock = L->getLoopLatch();
Dan Gohman692ad8d2009-11-05 19:44:06 +0000142 if (!LatchBlock) {
David Greenea9ad9c22010-01-05 01:26:41 +0000143 DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
Dan Gohman692ad8d2009-11-05 19:44:06 +0000144 return false;
145 }
146
147 BasicBlock *Header = L->getHeader();
Dan Gohman45b31972008-05-14 00:24:14 +0000148 BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
Andrew Trickba033772011-07-23 00:29:16 +0000149
Dan Gohman45b31972008-05-14 00:24:14 +0000150 if (!BI || BI->isUnconditional()) {
151 // The loop-rotate pass can be helpful to avoid this in many cases.
David Greenea9ad9c22010-01-05 01:26:41 +0000152 DEBUG(dbgs() <<
Chris Lattnerbdff5482009-08-23 04:37:46 +0000153 " Can't unroll; loop not terminated by a conditional branch.\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000154 return false;
155 }
Andrew Trickba033772011-07-23 00:29:16 +0000156
Chris Lattnerf5ebfb02011-02-18 04:25:21 +0000157 if (Header->hasAddressTaken()) {
158 // The loop-rotate pass can be helpful to avoid this in many cases.
159 DEBUG(dbgs() <<
160 " Won't unroll loop: address of header block is taken.\n");
161 return false;
162 }
Dan Gohman45b31972008-05-14 00:24:14 +0000163
Dan Gohman572365e2010-07-26 18:02:06 +0000164 // Notify ScalarEvolution that the loop will be substantially changed,
165 // if not outright eliminated.
166 if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>())
167 SE->forgetLoop(L);
168
Dan Gohman45b31972008-05-14 00:24:14 +0000169 if (TripCount != 0)
David Greenea9ad9c22010-01-05 01:26:41 +0000170 DEBUG(dbgs() << " Trip Count = " << TripCount << "\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000171 if (TripMultiple != 1)
David Greenea9ad9c22010-01-05 01:26:41 +0000172 DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000173
174 // Effectively "DCE" unrolled iterations that are beyond the tripcount
175 // and will never be executed.
176 if (TripCount != 0 && Count > TripCount)
177 Count = TripCount;
178
179 assert(Count > 0);
180 assert(TripMultiple > 0);
181 assert(TripCount == 0 || TripCount % TripMultiple == 0);
182
183 // Are we eliminating the loop control altogether?
184 bool CompletelyUnroll = Count == TripCount;
185
186 // If we know the trip count, we know the multiple...
187 unsigned BreakoutTrip = 0;
188 if (TripCount != 0) {
189 BreakoutTrip = TripCount % Count;
190 TripMultiple = 0;
191 } else {
192 // Figure out what multiple to use.
193 BreakoutTrip = TripMultiple =
194 (unsigned)GreatestCommonDivisor64(Count, TripMultiple);
195 }
196
197 if (CompletelyUnroll) {
David Greenea9ad9c22010-01-05 01:26:41 +0000198 DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000199 << " with trip count " << TripCount << "!\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000200 } else {
David Greenea9ad9c22010-01-05 01:26:41 +0000201 DEBUG(dbgs() << "UNROLLING loop %" << Header->getName()
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000202 << " by " << Count);
Dan Gohman45b31972008-05-14 00:24:14 +0000203 if (TripMultiple == 0 || BreakoutTrip != TripMultiple) {
David Greenea9ad9c22010-01-05 01:26:41 +0000204 DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip);
Dan Gohman45b31972008-05-14 00:24:14 +0000205 } else if (TripMultiple != 1) {
David Greenea9ad9c22010-01-05 01:26:41 +0000206 DEBUG(dbgs() << " with " << TripMultiple << " trips per branch");
Dan Gohman45b31972008-05-14 00:24:14 +0000207 }
David Greenea9ad9c22010-01-05 01:26:41 +0000208 DEBUG(dbgs() << "!\n");
Dan Gohman45b31972008-05-14 00:24:14 +0000209 }
210
211 std::vector<BasicBlock*> LoopBlocks = L->getBlocks();
212
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000213 bool ContinueOnTrue = L->contains(BI->getSuccessor(0));
Dan Gohman45b31972008-05-14 00:24:14 +0000214 BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue);
215
216 // For the first iteration of the loop, we should use the precloned values for
217 // PHI nodes. Insert associations now.
Devang Patel39430842010-04-20 22:24:18 +0000218 ValueToValueMapTy LastValueMap;
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000219 std::vector<PHINode*> OrigPHINode;
Dan Gohman45b31972008-05-14 00:24:14 +0000220 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
Andrew Trick70d0ca92011-08-09 03:11:29 +0000221 OrigPHINode.push_back(cast<PHINode>(I));
Dan Gohman45b31972008-05-14 00:24:14 +0000222 }
223
224 std::vector<BasicBlock*> Headers;
225 std::vector<BasicBlock*> Latches;
226 Headers.push_back(Header);
227 Latches.push_back(LatchBlock);
228
Andrew Trickb1eede12011-08-10 00:28:10 +0000229 // The current on-the-fly SSA update requires blocks to be processed in
230 // reverse postorder so that LastValueMap contains the correct value at each
231 // exit.
232 LoopBlocksDFS DFS(L);
Andrew Trick2d31ae32011-08-10 01:59:05 +0000233 DFS.perform(LI);
234
Andrew Trickb1eede12011-08-10 00:28:10 +0000235 // Stash the DFS iterators before adding blocks to the loop.
236 LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
237 LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
238
Dan Gohman45b31972008-05-14 00:24:14 +0000239 for (unsigned It = 1; It != Count; ++It) {
Dan Gohman45b31972008-05-14 00:24:14 +0000240 std::vector<BasicBlock*> NewBlocks;
Andrew Trickba033772011-07-23 00:29:16 +0000241
Andrew Trickb1eede12011-08-10 00:28:10 +0000242 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000243 ValueToValueMapTy VMap;
244 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000245 Header->getParent()->getBasicBlockList().push_back(New);
Dan Gohman45b31972008-05-14 00:24:14 +0000246
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000247 // Loop over all of the PHI nodes in the block, changing them to use the
248 // incoming values from the previous block.
249 if (*BB == Header)
250 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000251 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[i]]);
Dan Gohman45b31972008-05-14 00:24:14 +0000252 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
253 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
Dan Gohman92329c72009-12-18 01:24:09 +0000254 if (It > 1 && L->contains(InValI))
Dan Gohman45b31972008-05-14 00:24:14 +0000255 InVal = LastValueMap[InValI];
Devang Patel29d3dd82010-06-23 23:55:51 +0000256 VMap[OrigPHINode[i]] = InVal;
Dan Gohman45b31972008-05-14 00:24:14 +0000257 New->getInstList().erase(NewPHI);
258 }
259
260 // Update our running map of newest clones
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000261 LastValueMap[*BB] = New;
Devang Patel29d3dd82010-06-23 23:55:51 +0000262 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
Dan Gohman45b31972008-05-14 00:24:14 +0000263 VI != VE; ++VI)
264 LastValueMap[VI->first] = VI->second;
265
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000266 L->addBasicBlockToLoop(New, LI->getBase());
267
Andrew Trickb1eede12011-08-10 00:28:10 +0000268 // Add phi entries for newly created values to all exit blocks.
269 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB);
270 SI != SE; ++SI) {
271 if (L->contains(*SI))
272 continue;
273 for (BasicBlock::iterator BBI = (*SI)->begin();
274 PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) {
275 Value *Incoming = phi->getIncomingValueForBlock(*BB);
276 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
277 if (It != LastValueMap.end())
278 Incoming = It->second;
279 phi->addIncoming(Incoming, New);
280 }
281 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000282 // Keep track of new headers and latches as we create them, so that
283 // we can insert the proper branches later.
284 if (*BB == Header)
285 Headers.push_back(New);
Andrew Trickb1eede12011-08-10 00:28:10 +0000286 if (*BB == LatchBlock)
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000287 Latches.push_back(New);
288
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000289 NewBlocks.push_back(New);
Dan Gohman45b31972008-05-14 00:24:14 +0000290 }
Andrew Trickba033772011-07-23 00:29:16 +0000291
Dan Gohman45b31972008-05-14 00:24:14 +0000292 // Remap all instructions in the most recent iteration
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000293 for (unsigned i = 0; i < NewBlocks.size(); ++i)
Dan Gohman45b31972008-05-14 00:24:14 +0000294 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
295 E = NewBlocks[i]->end(); I != E; ++I)
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000296 ::RemapInstruction(I, LastValueMap);
Dan Gohman45b31972008-05-14 00:24:14 +0000297 }
Andrew Trickba033772011-07-23 00:29:16 +0000298
Andrew Trickb1eede12011-08-10 00:28:10 +0000299 // Loop over the PHI nodes in the original block, setting incoming values.
300 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
301 PHINode *PN = OrigPHINode[i];
302 if (CompletelyUnroll) {
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000303 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
304 Header->getInstList().erase(PN);
305 }
Andrew Trickb1eede12011-08-10 00:28:10 +0000306 else if (Count > 1) {
307 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
308 // If this value was defined in the loop, take the value defined by the
309 // last iteration of the loop.
310 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
311 if (L->contains(InValI))
312 InVal = LastValueMap[InVal];
313 }
314 assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
315 PN->addIncoming(InVal, Latches.back());
316 }
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000317 }
Dan Gohman45b31972008-05-14 00:24:14 +0000318
319 // Now that all the basic blocks for the unrolled iterations are in place,
320 // set up the branches to connect them.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000321 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
Dan Gohman45b31972008-05-14 00:24:14 +0000322 // The original branch was replicated in each unrolled iteration.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000323 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
Dan Gohman45b31972008-05-14 00:24:14 +0000324
325 // The branch destination.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000326 unsigned j = (i + 1) % e;
327 BasicBlock *Dest = Headers[j];
Dan Gohman45b31972008-05-14 00:24:14 +0000328 bool NeedConditional = true;
329
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000330 // For a complete unroll, make the last iteration end with a branch
331 // to the exit block.
332 if (CompletelyUnroll && j == 0) {
Dan Gohman45b31972008-05-14 00:24:14 +0000333 Dest = LoopExit;
334 NeedConditional = false;
335 }
336
337 // If we know the trip count or a multiple of it, we can safely use an
338 // unconditional branch for some iterations.
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000339 if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) {
Dan Gohman45b31972008-05-14 00:24:14 +0000340 NeedConditional = false;
341 }
342
343 if (NeedConditional) {
344 // Update the conditional branch's successor for the following
345 // iteration.
346 Term->setSuccessor(!ContinueOnTrue, Dest);
347 } else {
Andrew Trickb1eede12011-08-10 00:28:10 +0000348 // Remove phi operands at this loop exit
349 if (Dest != LoopExit) {
350 BasicBlock *BB = Latches[i];
351 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
352 SI != SE; ++SI) {
353 if (*SI == Headers[i])
354 continue;
355 for (BasicBlock::iterator BBI = (*SI)->begin();
356 PHINode *Phi = dyn_cast<PHINode>(BBI); ++BBI) {
357 Phi->removeIncomingValue(BB, false);
358 }
359 }
360 }
Jay Foad8f9ffbd2011-01-07 20:25:56 +0000361 // Replace the conditional branch with an unconditional one.
362 BranchInst::Create(Dest, Term);
363 Term->eraseFromParent();
Jay Foadcd35e092011-06-21 10:33:19 +0000364 }
365 }
366
Jay Foad95c3e482011-06-23 09:09:15 +0000367 // Merge adjacent basic blocks, if possible.
368 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
369 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
370 if (Term->isUnconditional()) {
371 BasicBlock *Dest = Term->getSuccessor(0);
Andrew Trick1009c322011-08-03 18:32:11 +0000372 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM))
Jay Foad95c3e482011-06-23 09:09:15 +0000373 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
374 }
375 }
Andrew Trickba033772011-07-23 00:29:16 +0000376
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000377 // At this point, the code is well formed. We now do a quick sweep over the
378 // inserted code, doing constant propagation and dead code elimination as we
379 // go.
380 const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
381 for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(),
382 BBE = NewLoopBlocks.end(); BB != BBE; ++BB)
383 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) {
Dan Gohman45b31972008-05-14 00:24:14 +0000384 Instruction *Inst = I++;
385
386 if (isInstructionTriviallyDead(Inst))
Dan Gohman8dbe7f82008-06-24 20:44:42 +0000387 (*BB)->getInstList().erase(Inst);
Duncan Sandsb6133d12010-11-23 20:26:33 +0000388 else if (Value *V = SimplifyInstruction(Inst))
389 if (LI->replacementPreservesLCSSAForm(Inst, V)) {
390 Inst->replaceAllUsesWith(V);
391 (*BB)->getInstList().erase(Inst);
392 }
Dan Gohman45b31972008-05-14 00:24:14 +0000393 }
Dan Gohman45b31972008-05-14 00:24:14 +0000394
395 NumCompletelyUnrolled += CompletelyUnroll;
396 ++NumUnrolled;
397 // Remove the loop from the LoopPassManager if it's completely removed.
398 if (CompletelyUnroll && LPM != NULL)
399 LPM->deleteLoopFromQueue(L);
400
Dan Gohman45b31972008-05-14 00:24:14 +0000401 return true;
402}