blob: 62762d32eb53cdf72bc8512e6526a58130e4da98 [file] [log] [blame]
Chris Lattner946b2552004-04-18 05:20:17 +00001//===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner946b2552004-04-18 05:20:17 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner946b2552004-04-18 05:20:17 +00008//===----------------------------------------------------------------------===//
9//
10// This pass implements a simple loop unroller. It works best when loops have
11// been canonicalized by the -indvars pass, allowing it to determine the trip
12// counts of loops easily.
13//
Owen Andersone001d812006-08-24 21:28:19 +000014// This pass will multi-block loops only if they contain no non-unrolled
15// subloops. The process of unrolling can produce extraneous basic blocks
16// linked with unconditional branches. This will be corrected in the future.
Chris Lattner946b2552004-04-18 05:20:17 +000017//
18//===----------------------------------------------------------------------===//
19
20#define DEBUG_TYPE "loop-unroll"
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Constants.h"
23#include "llvm/Function.h"
24#include "llvm/Instructions.h"
Chris Lattner024f4ab2007-01-30 23:46:24 +000025#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner946b2552004-04-18 05:20:17 +000026#include "llvm/Analysis/LoopInfo.h"
Devang Patel9779e562007-03-07 01:38:05 +000027#include "llvm/Analysis/LoopPass.h"
Chris Lattner946b2552004-04-18 05:20:17 +000028#include "llvm/Transforms/Utils/Cloning.h"
29#include "llvm/Transforms/Utils/Local.h"
Owen Anderson62c84fe2006-08-28 02:09:46 +000030#include "llvm/Support/CFG.h"
Reid Spencer557ab152007-02-05 23:32:05 +000031#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/STLExtras.h"
Chris Lattner1bfc7ab2007-02-03 00:08:31 +000036#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner6d048a02004-11-22 17:18:36 +000037#include "llvm/IntrinsicInst.h"
Chris Lattner946b2552004-04-18 05:20:17 +000038#include <cstdio>
Reid Spencerce078332004-10-18 14:38:48 +000039#include <algorithm>
Chris Lattner946b2552004-04-18 05:20:17 +000040using namespace llvm;
41
Chris Lattner79a42ac2006-12-19 21:40:18 +000042STATISTIC(NumUnrolled, "Number of loops completely unrolled");
Chris Lattner946b2552004-04-18 05:20:17 +000043
Chris Lattner79a42ac2006-12-19 21:40:18 +000044namespace {
Chris Lattner946b2552004-04-18 05:20:17 +000045 cl::opt<unsigned>
Chris Lattnerd1525022004-04-18 18:06:14 +000046 UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden,
Chris Lattner946b2552004-04-18 05:20:17 +000047 cl::desc("The cut-off point for loop unrolling"));
48
Devang Patel9779e562007-03-07 01:38:05 +000049 class VISIBILITY_HIDDEN LoopUnroll : public LoopPass {
Chris Lattner946b2552004-04-18 05:20:17 +000050 LoopInfo *LI; // The current loop information
51 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000052 static char ID; // Pass ID, replacement for typeid
Dan Gohman2e1f8042007-05-08 15:19:19 +000053 LoopUnroll() : LoopPass((intptr_t)&ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000054
Devang Patel9779e562007-03-07 01:38:05 +000055 bool runOnLoop(Loop *L, LPPassManager &LPM);
Dan Gohman2e1f8042007-05-08 15:19:19 +000056 BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB);
Chris Lattner946b2552004-04-18 05:20:17 +000057
58 /// This transformation requires natural loop information & requires that
59 /// loop preheaders be inserted into the CFG...
60 ///
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner946b2552004-04-18 05:20:17 +000062 AU.addRequiredID(LoopSimplifyID);
Owen Andersone001d812006-08-24 21:28:19 +000063 AU.addRequiredID(LCSSAID);
Chris Lattner946b2552004-04-18 05:20:17 +000064 AU.addRequired<LoopInfo>();
Owen Andersone001d812006-08-24 21:28:19 +000065 AU.addPreservedID(LCSSAID);
Chris Lattnerf2cc8412004-04-18 05:38:37 +000066 AU.addPreserved<LoopInfo>();
Chris Lattner946b2552004-04-18 05:20:17 +000067 }
68 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000069 char LoopUnroll::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000070 RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops");
Chris Lattner946b2552004-04-18 05:20:17 +000071}
72
Devang Patel9779e562007-03-07 01:38:05 +000073LoopPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); }
Chris Lattner946b2552004-04-18 05:20:17 +000074
Dan Gohman49d08a52007-05-08 15:14:19 +000075/// ApproximateLoopSize - Approximate the size of the loop.
Chris Lattner946b2552004-04-18 05:20:17 +000076static unsigned ApproximateLoopSize(const Loop *L) {
77 unsigned Size = 0;
78 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
79 BasicBlock *BB = L->getBlocks()[i];
80 Instruction *Term = BB->getTerminator();
81 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
82 if (isa<PHINode>(I) && BB == L->getHeader()) {
83 // Ignore PHI nodes in the header.
84 } else if (I->hasOneUse() && I->use_back() == Term) {
85 // Ignore instructions only used by the loop terminator.
Reid Spencerde46e482006-11-02 20:25:50 +000086 } else if (isa<DbgInfoIntrinsic>(I)) {
Jeff Cohen82639852005-04-23 21:38:35 +000087 // Ignore debug instructions
Chris Lattner946b2552004-04-18 05:20:17 +000088 } else {
89 ++Size;
90 }
91
92 // TODO: Ignore expressions derived from PHI and constants if inval of phi
93 // is a constant, or if operation is associative. This will get induction
94 // variables.
95 }
96 }
97
98 return Size;
99}
100
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101// RemapInstruction - Convert the instruction operands from referencing the
Chris Lattner946b2552004-04-18 05:20:17 +0000102// current values into those specified by ValueMap.
103//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000104static inline void RemapInstruction(Instruction *I,
Chris Lattner1bfc7ab2007-02-03 00:08:31 +0000105 DenseMap<const Value *, Value*> &ValueMap) {
Chris Lattner946b2552004-04-18 05:20:17 +0000106 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
107 Value *Op = I->getOperand(op);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +0000108 DenseMap<const Value *, Value*>::iterator It = ValueMap.find(Op);
Chris Lattner946b2552004-04-18 05:20:17 +0000109 if (It != ValueMap.end()) Op = It->second;
110 I->setOperand(op, Op);
111 }
112}
113
Owen Anderson62c84fe2006-08-28 02:09:46 +0000114// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
115// only has one predecessor, and that predecessor only has one successor.
116// Returns the new combined block.
Dan Gohman2e1f8042007-05-08 15:19:19 +0000117BasicBlock *LoopUnroll::FoldBlockIntoPredecessor(BasicBlock *BB) {
Owen Anderson62c84fe2006-08-28 02:09:46 +0000118 // Merge basic blocks into their predecessor if there is only one distinct
119 // pred, and if there is only one distinct successor of the predecessor, and
120 // if there are no PHI nodes.
121 //
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000122 BasicBlock *OnlyPred = BB->getSinglePredecessor();
123 if (!OnlyPred) return 0;
Owen Anderson62c84fe2006-08-28 02:09:46 +0000124
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000125 if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
126 return 0;
127
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000128 DOUT << "Merging: " << *BB << "into: " << *OnlyPred;
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000129
130 // Resolve any PHI nodes at the start of the block. They are all
131 // guaranteed to have exactly one entry if they exist, unless there are
132 // multiple duplicate (but guaranteed to be equal) entries for the
133 // incoming edges. This occurs when there are multiple edges from
134 // OnlyPred to OnlySucc.
135 //
136 while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
137 PN->replaceAllUsesWith(PN->getIncomingValue(0));
138 BB->getInstList().pop_front(); // Delete the phi node...
Owen Anderson62c84fe2006-08-28 02:09:46 +0000139 }
140
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000141 // Delete the unconditional branch from the predecessor...
142 OnlyPred->getInstList().pop_back();
Owen Anderson62c84fe2006-08-28 02:09:46 +0000143
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000144 // Move all definitions in the successor to the predecessor...
145 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
Owen Anderson62c84fe2006-08-28 02:09:46 +0000146
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000147 // Make all PHI nodes that referred to BB now refer to Pred as their
148 // source...
149 BB->replaceAllUsesWith(OnlyPred);
Owen Anderson62c84fe2006-08-28 02:09:46 +0000150
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000151 std::string OldName = BB->getName();
Owen Anderson62c84fe2006-08-28 02:09:46 +0000152
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000153 // Erase basic block from the function...
154 LI->removeBlock(BB);
155 BB->eraseFromParent();
Owen Anderson62c84fe2006-08-28 02:09:46 +0000156
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000157 // Inherit predecessors name if it exists...
158 if (!OldName.empty() && !OnlyPred->hasName())
159 OnlyPred->setName(OldName);
Owen Anderson62c84fe2006-08-28 02:09:46 +0000160
Owen Andersona8a2e5c2006-08-29 06:10:56 +0000161 return OnlyPred;
Owen Anderson62c84fe2006-08-28 02:09:46 +0000162}
163
Devang Patel9779e562007-03-07 01:38:05 +0000164bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
Chris Lattner946b2552004-04-18 05:20:17 +0000165 bool Changed = false;
Devang Patel9779e562007-03-07 01:38:05 +0000166 LI = &getAnalysis<LoopInfo>();
Chris Lattner946b2552004-04-18 05:20:17 +0000167
Dan Gohman2e1f8042007-05-08 15:19:19 +0000168 BasicBlock *Header = L->getHeader();
169 BasicBlock *LatchBlock = L->getLoopLatch();
Chris Lattner946b2552004-04-18 05:20:17 +0000170
Owen Andersone001d812006-08-24 21:28:19 +0000171 BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
Chris Lattner946b2552004-04-18 05:20:17 +0000172 if (BI == 0) return Changed; // Must end in a conditional branch
173
174 ConstantInt *TripCountC = dyn_cast_or_null<ConstantInt>(L->getTripCount());
175 if (!TripCountC) return Changed; // Must have constant trip count!
176
Reid Spencer29fe20a2007-03-02 23:31:34 +0000177 // Guard against huge trip counts. This also guards against assertions in
178 // APInt from the use of getZExtValue, below.
179 if (TripCountC->getValue().getActiveBits() > 32)
Chris Lattner946b2552004-04-18 05:20:17 +0000180 return Changed; // More than 2^32 iterations???
181
Reid Spencer29fe20a2007-03-02 23:31:34 +0000182 uint64_t TripCountFull = TripCountC->getZExtValue();
183 if (TripCountFull == 0)
184 return Changed; // Zero iteraitons?
185
Chris Lattner946b2552004-04-18 05:20:17 +0000186 unsigned LoopSize = ApproximateLoopSize(L);
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000187 DOUT << "Loop Unroll: F[" << Header->getParent()->getName()
188 << "] Loop %" << Header->getName() << " Loop Size = "
189 << LoopSize << " Trip Count = " << TripCountFull << " - ";
Chris Lattner47f395c2005-01-08 19:37:20 +0000190 uint64_t Size = (uint64_t)LoopSize*TripCountFull;
Chris Lattnerc12c9452004-05-13 20:43:31 +0000191 if (Size > UnrollThreshold) {
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000192 DOUT << "TOO LARGE: " << Size << ">" << UnrollThreshold << "\n";
Chris Lattner946b2552004-04-18 05:20:17 +0000193 return Changed;
194 }
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000195 DOUT << "UNROLLING!\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000196
Owen Andersone001d812006-08-24 21:28:19 +0000197 std::vector<BasicBlock*> LoopBlocks = L->getBlocks();
198
Chris Lattner47f395c2005-01-08 19:37:20 +0000199 unsigned TripCount = (unsigned)TripCountFull;
200
Owen Andersone001d812006-08-24 21:28:19 +0000201 BasicBlock *LoopExit = BI->getSuccessor(L->contains(BI->getSuccessor(0)));
Chris Lattner946b2552004-04-18 05:20:17 +0000202
203 // For the first iteration of the loop, we should use the precloned values for
204 // PHI nodes. Insert associations now.
Chris Lattner1077d2a2007-05-05 18:49:57 +0000205 typedef DenseMap<const Value*, Value*> ValueMapTy;
206 ValueMapTy LastValueMap;
Chris Lattner946b2552004-04-18 05:20:17 +0000207 std::vector<PHINode*> OrigPHINode;
Owen Andersone001d812006-08-24 21:28:19 +0000208 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
Reid Spencer66149462004-09-15 17:06:42 +0000209 PHINode *PN = cast<PHINode>(I);
Chris Lattner946b2552004-04-18 05:20:17 +0000210 OrigPHINode.push_back(PN);
Owen Andersone001d812006-08-24 21:28:19 +0000211 if (Instruction *I =
212 dyn_cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)))
213 if (L->contains(I->getParent()))
Chris Lattner946b2552004-04-18 05:20:17 +0000214 LastValueMap[I] = I;
215 }
216
217 // Remove the exit branch from the loop
Owen Andersone001d812006-08-24 21:28:19 +0000218 LatchBlock->getInstList().erase(BI);
219
220 std::vector<BasicBlock*> Headers;
221 std::vector<BasicBlock*> Latches;
222 Headers.push_back(Header);
223 Latches.push_back(LatchBlock);
Chris Lattner946b2552004-04-18 05:20:17 +0000224
225 assert(TripCount != 0 && "Trip count of 0 is impossible!");
226 for (unsigned It = 1; It != TripCount; ++It) {
227 char SuffixBuffer[100];
228 sprintf(SuffixBuffer, ".%d", It);
Owen Andersone001d812006-08-24 21:28:19 +0000229
230 std::vector<BasicBlock*> NewBlocks;
231
232 for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(),
233 E = LoopBlocks.end(); BB != E; ++BB) {
Chris Lattner1077d2a2007-05-05 18:49:57 +0000234 ValueMapTy ValueMap;
Owen Andersone001d812006-08-24 21:28:19 +0000235 BasicBlock *New = CloneBasicBlock(*BB, ValueMap, SuffixBuffer);
236 Header->getParent()->getBasicBlockList().push_back(New);
Chris Lattner946b2552004-04-18 05:20:17 +0000237
Owen Andersone001d812006-08-24 21:28:19 +0000238 // Loop over all of the PHI nodes in the block, changing them to use the
239 // incoming values from the previous block.
240 if (*BB == Header)
241 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
242 PHINode *NewPHI = cast<PHINode>(ValueMap[OrigPHINode[i]]);
243 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
244 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
245 if (It > 1 && L->contains(InValI->getParent()))
246 InVal = LastValueMap[InValI];
247 ValueMap[OrigPHINode[i]] = InVal;
248 New->getInstList().erase(NewPHI);
249 }
250
251 // Update our running map of newest clones
252 LastValueMap[*BB] = New;
Chris Lattner1077d2a2007-05-05 18:49:57 +0000253 for (ValueMapTy::iterator VI = ValueMap.begin(), VE = ValueMap.end();
254 VI != VE; ++VI)
Owen Andersone001d812006-08-24 21:28:19 +0000255 LastValueMap[VI->first] = VI->second;
256
257 L->addBasicBlockToLoop(New, *LI);
258
259 // Add phi entries for newly created values to all exit blocks except
260 // the successor of the latch block. The successor of the exit block will
261 // be updated specially after unrolling all the way.
262 if (*BB != LatchBlock)
263 for (Value::use_iterator UI = (*BB)->use_begin(), UE = (*BB)->use_end();
264 UI != UE; ++UI) {
Dan Gohman2e1f8042007-05-08 15:19:19 +0000265 Instruction *UseInst = cast<Instruction>(*UI);
Owen Andersone001d812006-08-24 21:28:19 +0000266 if (isa<PHINode>(UseInst) && !L->contains(UseInst->getParent())) {
Dan Gohman2e1f8042007-05-08 15:19:19 +0000267 PHINode *phi = cast<PHINode>(UseInst);
268 Value *Incoming = phi->getIncomingValueForBlock(*BB);
Owen Andersone001d812006-08-24 21:28:19 +0000269 if (isa<Instruction>(Incoming))
270 Incoming = LastValueMap[Incoming];
271
272 phi->addIncoming(Incoming, New);
273 }
274 }
275
276 // Keep track of new headers and latches as we create them, so that
277 // we can insert the proper branches later.
278 if (*BB == Header)
279 Headers.push_back(New);
280 if (*BB == LatchBlock)
281 Latches.push_back(New);
282
283 NewBlocks.push_back(New);
Chris Lattner946b2552004-04-18 05:20:17 +0000284 }
Owen Andersone001d812006-08-24 21:28:19 +0000285
286 // Remap all instructions in the most recent iteration
287 for (unsigned i = 0; i < NewBlocks.size(); ++i)
288 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
289 E = NewBlocks[i]->end(); I != E; ++I)
Chris Lattner230bcb62004-04-18 17:32:39 +0000290 RemapInstruction(I, LastValueMap);
Chris Lattner230bcb62004-04-18 17:32:39 +0000291 }
Chris Lattner946b2552004-04-18 05:20:17 +0000292
Owen Andersone001d812006-08-24 21:28:19 +0000293
Chris Lattner1077d2a2007-05-05 18:49:57 +0000294 // The latch block exits the loop. If there are any PHI nodes in the
295 // successor blocks, update them to use the appropriate values computed as the
296 // last iteration of the loop.
Owen Andersone001d812006-08-24 21:28:19 +0000297 if (TripCount > 1) {
Chris Lattner1bfc7ab2007-02-03 00:08:31 +0000298 SmallPtrSet<PHINode*, 8> Users;
Owen Andersone001d812006-08-24 21:28:19 +0000299 for (Value::use_iterator UI = LatchBlock->use_begin(),
300 UE = LatchBlock->use_end(); UI != UE; ++UI)
Dan Gohman2e1f8042007-05-08 15:19:19 +0000301 if (PHINode *phi = dyn_cast<PHINode>(*UI))
Owen Andersone001d812006-08-24 21:28:19 +0000302 Users.insert(phi);
Chris Lattner1077d2a2007-05-05 18:49:57 +0000303
304 BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +0000305 for (SmallPtrSet<PHINode*,8>::iterator SI = Users.begin(), SE = Users.end();
Owen Andersone001d812006-08-24 21:28:19 +0000306 SI != SE; ++SI) {
Chris Lattner57d89a52007-05-05 18:36:36 +0000307 PHINode *PN = *SI;
Chris Lattner1077d2a2007-05-05 18:49:57 +0000308 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
309 // If this value was defined in the loop, take the value defined by the
310 // last iteration of the loop.
311 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
312 if (L->contains(InValI->getParent()))
313 InVal = LastValueMap[InVal];
Devang Patelabdff3f2007-04-16 23:03:45 +0000314 }
Chris Lattner1077d2a2007-05-05 18:49:57 +0000315 PN->addIncoming(InVal, LastIterationBB);
Owen Andersone001d812006-08-24 21:28:19 +0000316 }
317 }
Chris Lattner946b2552004-04-18 05:20:17 +0000318
319 // Now loop over the PHI nodes in the original block, setting them to their
320 // incoming values.
321 BasicBlock *Preheader = L->getLoopPreheader();
322 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
323 PHINode *PN = OrigPHINode[i];
324 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
Owen Andersone001d812006-08-24 21:28:19 +0000325 Header->getInstList().erase(PN);
Owen Anderson62c84fe2006-08-28 02:09:46 +0000326 }
327
328 // Insert the branches that link the different iterations together
329 for (unsigned i = 0; i < Latches.size()-1; ++i) {
330 new BranchInst(Headers[i+1], Latches[i]);
Dan Gohman2e1f8042007-05-08 15:19:19 +0000331 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Headers[i+1])) {
Owen Anderson62c84fe2006-08-28 02:09:46 +0000332 std::replace(Latches.begin(), Latches.end(), Headers[i+1], Fold);
333 std::replace(Headers.begin(), Headers.end(), Headers[i+1], Fold);
334 }
335 }
336
337 // Finally, add an unconditional branch to the block to continue into the exit
338 // block.
339 new BranchInst(LoopExit, Latches[Latches.size()-1]);
340 FoldBlockIntoPredecessor(LoopExit);
341
Chris Lattner946b2552004-04-18 05:20:17 +0000342 // At this point, the code is well formed. We now do a quick sweep over the
343 // inserted code, doing constant propagation and dead code elimination as we
344 // go.
Owen Andersone001d812006-08-24 21:28:19 +0000345 const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
346 for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(),
Owen Anderson62c84fe2006-08-28 02:09:46 +0000347 BBE = NewLoopBlocks.end(); BB != BBE; ++BB)
Owen Andersone001d812006-08-24 21:28:19 +0000348 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) {
349 Instruction *Inst = I++;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000350
Owen Andersone001d812006-08-24 21:28:19 +0000351 if (isInstructionTriviallyDead(Inst))
352 (*BB)->getInstList().erase(Inst);
353 else if (Constant *C = ConstantFoldInstruction(Inst)) {
354 Inst->replaceAllUsesWith(C);
355 (*BB)->getInstList().erase(Inst);
356 }
Chris Lattner946b2552004-04-18 05:20:17 +0000357 }
Chris Lattner946b2552004-04-18 05:20:17 +0000358
Chris Lattnerf2cc8412004-04-18 05:38:37 +0000359 // Update the loop information for this loop.
Chris Lattnerf2cc8412004-04-18 05:38:37 +0000360 // Remove the loop from the parent.
Devang Patel9779e562007-03-07 01:38:05 +0000361 LPM.deleteLoopFromQueue(L);
Chris Lattnerf2cc8412004-04-18 05:38:37 +0000362
Chris Lattner946b2552004-04-18 05:20:17 +0000363 ++NumUnrolled;
364 return true;
365}