LoopPass.cpp revision 212904
1193323Sed//===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements LoopPass and LPPassManager. All loop optimization
11193323Sed// and transformation passes are derived from LoopPass. LPPassManager is
12193323Sed// responsible for managing LoopPasses.
13193323Sed//
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#include "llvm/Analysis/LoopPass.h"
17206124Srdivacky#include "llvm/Assembly/PrintModulePass.h"
18206124Srdivacky#include "llvm/Support/Debug.h"
19206083Srdivacky#include "llvm/Support/Timer.h"
20193323Sedusing namespace llvm;
21193323Sed
22206124Srdivackynamespace {
23206124Srdivacky
24206124Srdivacky/// PrintLoopPass - Print a Function corresponding to a Loop.
25206124Srdivacky///
26206124Srdivackyclass PrintLoopPass : public LoopPass {
27206124Srdivackyprivate:
28206124Srdivacky  std::string Banner;
29206124Srdivacky  raw_ostream &Out;       // raw_ostream to print on.
30206124Srdivacky
31206124Srdivackypublic:
32206124Srdivacky  static char ID;
33212904Sdim  PrintLoopPass() : LoopPass(ID), Out(dbgs()) {}
34206124Srdivacky  PrintLoopPass(const std::string &B, raw_ostream &o)
35212904Sdim      : LoopPass(ID), Banner(B), Out(o) {}
36206124Srdivacky
37206124Srdivacky  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38206124Srdivacky    AU.setPreservesAll();
39206124Srdivacky  }
40206124Srdivacky
41206124Srdivacky  bool runOnLoop(Loop *L, LPPassManager &) {
42206124Srdivacky    Out << Banner;
43206124Srdivacky    for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
44206124Srdivacky         b != be;
45206124Srdivacky         ++b) {
46206124Srdivacky      (*b)->print(Out);
47206124Srdivacky    }
48206124Srdivacky    return false;
49206124Srdivacky  }
50206124Srdivacky};
51206124Srdivacky
52206124Srdivackychar PrintLoopPass::ID = 0;
53206124Srdivacky}
54206124Srdivacky
55193323Sed//===----------------------------------------------------------------------===//
56193323Sed// LPPassManager
57193323Sed//
58193323Sed
59193323Sedchar LPPassManager::ID = 0;
60193323Sed
61193323SedLPPassManager::LPPassManager(int Depth)
62212904Sdim  : FunctionPass(ID), PMDataManager(Depth) {
63193323Sed  skipThisLoop = false;
64193323Sed  redoThisLoop = false;
65193323Sed  LI = NULL;
66193323Sed  CurrentLoop = NULL;
67193323Sed}
68193323Sed
69193323Sed/// Delete loop from the loop queue and loop hierarchy (LoopInfo).
70193323Sedvoid LPPassManager::deleteLoopFromQueue(Loop *L) {
71193323Sed
72193323Sed  if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
73193323Sed    // Reparent all of the blocks in this loop.  Since BBLoop had a parent,
74193323Sed    // they are now all in it.
75193323Sed    for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
76193323Sed         I != E; ++I)
77193323Sed      if (LI->getLoopFor(*I) == L)    // Don't change blocks in subloops.
78193323Sed        LI->changeLoopFor(*I, ParentLoop);
79193323Sed
80193323Sed    // Remove the loop from its parent loop.
81193323Sed    for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
82193323Sed         ++I) {
83193323Sed      assert(I != E && "Couldn't find loop");
84193323Sed      if (*I == L) {
85193323Sed        ParentLoop->removeChildLoop(I);
86193323Sed        break;
87193323Sed      }
88193323Sed    }
89193323Sed
90193323Sed    // Move all subloops into the parent loop.
91193323Sed    while (!L->empty())
92193323Sed      ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
93193323Sed  } else {
94193323Sed    // Reparent all of the blocks in this loop.  Since BBLoop had no parent,
95193323Sed    // they no longer in a loop at all.
96193323Sed
97193323Sed    for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
98193323Sed      // Don't change blocks in subloops.
99193323Sed      if (LI->getLoopFor(L->getBlocks()[i]) == L) {
100193323Sed        LI->removeBlock(L->getBlocks()[i]);
101193323Sed        --i;
102193323Sed      }
103193323Sed    }
104193323Sed
105193323Sed    // Remove the loop from the top-level LoopInfo object.
106193323Sed    for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
107193323Sed      assert(I != E && "Couldn't find loop");
108193323Sed      if (*I == L) {
109193323Sed        LI->removeLoop(I);
110193323Sed        break;
111193323Sed      }
112193323Sed    }
113193323Sed
114193323Sed    // Move all of the subloops to the top-level.
115193323Sed    while (!L->empty())
116193323Sed      LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
117193323Sed  }
118193323Sed
119193323Sed  delete L;
120193323Sed
121193323Sed  // If L is current loop then skip rest of the passes and let
122193323Sed  // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
123193323Sed  // and continue applying other passes on CurrentLoop.
124193323Sed  if (CurrentLoop == L) {
125193323Sed    skipThisLoop = true;
126193323Sed    return;
127193323Sed  }
128193323Sed
129193323Sed  for (std::deque<Loop *>::iterator I = LQ.begin(),
130193323Sed         E = LQ.end(); I != E; ++I) {
131193323Sed    if (*I == L) {
132193323Sed      LQ.erase(I);
133193323Sed      break;
134193323Sed    }
135193323Sed  }
136193323Sed}
137193323Sed
138193323Sed// Inset loop into loop nest (LoopInfo) and loop queue (LQ).
139193323Sedvoid LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
140193323Sed
141193323Sed  assert (CurrentLoop != L && "Cannot insert CurrentLoop");
142193323Sed
143193323Sed  // Insert into loop nest
144193323Sed  if (ParentLoop)
145193323Sed    ParentLoop->addChildLoop(L);
146193323Sed  else
147193323Sed    LI->addTopLevelLoop(L);
148193323Sed
149198090Srdivacky  insertLoopIntoQueue(L);
150198090Srdivacky}
151198090Srdivacky
152198090Srdivackyvoid LPPassManager::insertLoopIntoQueue(Loop *L) {
153193323Sed  // Insert L into loop queue
154193323Sed  if (L == CurrentLoop)
155193323Sed    redoLoop(L);
156198090Srdivacky  else if (!L->getParentLoop())
157193323Sed    // This is top level loop.
158193323Sed    LQ.push_front(L);
159193323Sed  else {
160198090Srdivacky    // Insert L after the parent loop.
161193323Sed    for (std::deque<Loop *>::iterator I = LQ.begin(),
162193323Sed           E = LQ.end(); I != E; ++I) {
163198090Srdivacky      if (*I == L->getParentLoop()) {
164193323Sed        // deque does not support insert after.
165193323Sed        ++I;
166193323Sed        LQ.insert(I, 1, L);
167193323Sed        break;
168193323Sed      }
169193323Sed    }
170193323Sed  }
171193323Sed}
172193323Sed
173193323Sed// Reoptimize this loop. LPPassManager will re-insert this loop into the
174193323Sed// queue. This allows LoopPass to change loop nest for the loop. This
175193323Sed// utility may send LPPassManager into infinite loops so use caution.
176193323Sedvoid LPPassManager::redoLoop(Loop *L) {
177193323Sed  assert (CurrentLoop == L && "Can redo only CurrentLoop");
178193323Sed  redoThisLoop = true;
179193323Sed}
180193323Sed
181193323Sed/// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
182193323Sed/// all loop passes.
183193323Sedvoid LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
184193323Sed                                                  BasicBlock *To, Loop *L) {
185193323Sed  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
186212904Sdim    LoopPass *LP = getContainedPass(Index);
187193323Sed    LP->cloneBasicBlockAnalysis(From, To, L);
188193323Sed  }
189193323Sed}
190193323Sed
191193323Sed/// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
192193323Sedvoid LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
193193323Sed  if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
194193323Sed    for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
195193323Sed         ++BI) {
196193323Sed      Instruction &I = *BI;
197193323Sed      deleteSimpleAnalysisValue(&I, L);
198193323Sed    }
199193323Sed  }
200193323Sed  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
201212904Sdim    LoopPass *LP = getContainedPass(Index);
202193323Sed    LP->deleteAnalysisValue(V, L);
203193323Sed  }
204193323Sed}
205193323Sed
206193323Sed
207193323Sed// Recurse through all subloops and all loops  into LQ.
208193323Sedstatic void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
209193323Sed  LQ.push_back(L);
210193323Sed  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
211193323Sed    addLoopIntoQueue(*I, LQ);
212193323Sed}
213193323Sed
214193323Sed/// Pass Manager itself does not invalidate any analysis info.
215193323Sedvoid LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
216193323Sed  // LPPassManager needs LoopInfo. In the long term LoopInfo class will
217193323Sed  // become part of LPPassManager.
218193323Sed  Info.addRequired<LoopInfo>();
219193323Sed  Info.setPreservesAll();
220193323Sed}
221193323Sed
222193323Sed/// run - Execute all of the passes scheduled for execution.  Keep track of
223193323Sed/// whether any of the passes modifies the function, and if so, return true.
224193323Sedbool LPPassManager::runOnFunction(Function &F) {
225193323Sed  LI = &getAnalysis<LoopInfo>();
226193323Sed  bool Changed = false;
227193323Sed
228193323Sed  // Collect inherited analysis from Module level pass manager.
229193323Sed  populateInheritedAnalysis(TPM->activeStack);
230193323Sed
231193323Sed  // Populate Loop Queue
232193323Sed  for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
233193323Sed    addLoopIntoQueue(*I, LQ);
234193323Sed
235195340Sed  if (LQ.empty()) // No loops, skip calling finalizers
236195340Sed    return false;
237195340Sed
238193323Sed  // Initialization
239193323Sed  for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
240193323Sed       I != E; ++I) {
241193323Sed    Loop *L = *I;
242193323Sed    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
243212904Sdim      LoopPass *P = getContainedPass(Index);
244202878Srdivacky      Changed |= P->doInitialization(L, *this);
245193323Sed    }
246193323Sed  }
247193323Sed
248193323Sed  // Walk Loops
249193323Sed  while (!LQ.empty()) {
250193323Sed
251193323Sed    CurrentLoop  = LQ.back();
252193323Sed    skipThisLoop = false;
253193323Sed    redoThisLoop = false;
254193323Sed
255198090Srdivacky    // Run all passes on the current Loop.
256193323Sed    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
257212904Sdim      LoopPass *P = getContainedPass(Index);
258193323Sed
259198090Srdivacky      dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG,
260206083Srdivacky                   CurrentLoop->getHeader()->getName());
261193323Sed      dumpRequiredSet(P);
262193323Sed
263193323Sed      initializeAnalysisImpl(P);
264193323Sed
265193323Sed      {
266202878Srdivacky        PassManagerPrettyStackEntry X(P, *CurrentLoop->getHeader());
267206083Srdivacky        TimeRegion PassTimer(getPassTimer(P));
268206083Srdivacky
269202878Srdivacky        Changed |= P->runOnLoop(CurrentLoop, *this);
270193323Sed      }
271193323Sed
272193323Sed      if (Changed)
273198090Srdivacky        dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
274198090Srdivacky                     skipThisLoop ? "<deleted>" :
275206083Srdivacky                                    CurrentLoop->getHeader()->getName());
276193323Sed      dumpPreservedSet(P);
277193323Sed
278198090Srdivacky      if (!skipThisLoop) {
279198090Srdivacky        // Manually check that this loop is still healthy. This is done
280198090Srdivacky        // instead of relying on LoopInfo::verifyLoop since LoopInfo
281198090Srdivacky        // is a function pass and it's really expensive to verify every
282198090Srdivacky        // loop in the function every time. That level of checking can be
283198090Srdivacky        // enabled with the -verify-loop-info option.
284206083Srdivacky        {
285206083Srdivacky          TimeRegion PassTimer(getPassTimer(LI));
286206083Srdivacky          CurrentLoop->verifyLoop();
287206083Srdivacky        }
288198090Srdivacky
289198090Srdivacky        // Then call the regular verifyAnalysis functions.
290202878Srdivacky        verifyPreservedAnalysis(P);
291198090Srdivacky      }
292198090Srdivacky
293193323Sed      removeNotPreservedAnalysis(P);
294193323Sed      recordAvailableAnalysis(P);
295198090Srdivacky      removeDeadPasses(P,
296198090Srdivacky                       skipThisLoop ? "<deleted>" :
297206083Srdivacky                                      CurrentLoop->getHeader()->getName(),
298198090Srdivacky                       ON_LOOP_MSG);
299193323Sed
300193323Sed      if (skipThisLoop)
301193323Sed        // Do not run other passes on this loop.
302193323Sed        break;
303193323Sed    }
304193323Sed
305198090Srdivacky    // If the loop was deleted, release all the loop passes. This frees up
306198090Srdivacky    // some memory, and avoids trouble with the pass manager trying to call
307198090Srdivacky    // verifyAnalysis on them.
308198090Srdivacky    if (skipThisLoop)
309198090Srdivacky      for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
310198090Srdivacky        Pass *P = getContainedPass(Index);
311198090Srdivacky        freePass(P, "<deleted>", ON_LOOP_MSG);
312198090Srdivacky      }
313198090Srdivacky
314193323Sed    // Pop the loop from queue after running all passes.
315193323Sed    LQ.pop_back();
316193323Sed
317193323Sed    if (redoThisLoop)
318193323Sed      LQ.push_back(CurrentLoop);
319193323Sed  }
320193323Sed
321193323Sed  // Finalization
322193323Sed  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
323212904Sdim    LoopPass *P = getContainedPass(Index);
324202878Srdivacky    Changed |= P->doFinalization();
325193323Sed  }
326193323Sed
327193323Sed  return Changed;
328193323Sed}
329193323Sed
330193323Sed/// Print passes managed by this manager
331193323Sedvoid LPPassManager::dumpPassStructure(unsigned Offset) {
332198090Srdivacky  errs().indent(Offset*2) << "Loop Pass Manager\n";
333193323Sed  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
334193323Sed    Pass *P = getContainedPass(Index);
335193323Sed    P->dumpPassStructure(Offset + 1);
336193323Sed    dumpLastUses(P, Offset+1);
337193323Sed  }
338193323Sed}
339193323Sed
340193323Sed
341193323Sed//===----------------------------------------------------------------------===//
342193323Sed// LoopPass
343193323Sed
344206124SrdivackyPass *LoopPass::createPrinterPass(raw_ostream &O,
345206124Srdivacky                                  const std::string &Banner) const {
346206124Srdivacky  return new PrintLoopPass(Banner, O);
347206124Srdivacky}
348206124Srdivacky
349193323Sed// Check if this pass is suitable for the current LPPassManager, if
350193323Sed// available. This pass P is not suitable for a LPPassManager if P
351193323Sed// is not preserving higher level analysis info used by other
352193323Sed// LPPassManager passes. In such case, pop LPPassManager from the
353193323Sed// stack. This will force assignPassManager() to create new
354193323Sed// LPPassManger as expected.
355193323Sedvoid LoopPass::preparePassManager(PMStack &PMS) {
356193323Sed
357193323Sed  // Find LPPassManager
358193323Sed  while (!PMS.empty() &&
359193323Sed         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
360193323Sed    PMS.pop();
361193323Sed
362193323Sed  // If this pass is destroying high level information that is used
363193323Sed  // by other passes that are managed by LPM then do not insert
364193323Sed  // this pass in current LPM. Use new LPPassManager.
365202878Srdivacky  if (PMS.top()->getPassManagerType() == PMT_LoopPassManager &&
366202878Srdivacky      !PMS.top()->preserveHigherLevelAnalysis(this))
367193323Sed    PMS.pop();
368193323Sed}
369193323Sed
370193323Sed/// Assign pass manager to manage this pass.
371193323Sedvoid LoopPass::assignPassManager(PMStack &PMS,
372193323Sed                                 PassManagerType PreferredType) {
373193323Sed  // Find LPPassManager
374193323Sed  while (!PMS.empty() &&
375193323Sed         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
376193323Sed    PMS.pop();
377193323Sed
378202878Srdivacky  LPPassManager *LPPM;
379202878Srdivacky  if (PMS.top()->getPassManagerType() == PMT_LoopPassManager)
380202878Srdivacky    LPPM = (LPPassManager*)PMS.top();
381202878Srdivacky  else {
382202878Srdivacky    // Create new Loop Pass Manager if it does not exist.
383193323Sed    assert (!PMS.empty() && "Unable to create Loop Pass Manager");
384193323Sed    PMDataManager *PMD = PMS.top();
385193323Sed
386193323Sed    // [1] Create new Call Graph Pass Manager
387193323Sed    LPPM = new LPPassManager(PMD->getDepth() + 1);
388193323Sed    LPPM->populateInheritedAnalysis(PMS);
389193323Sed
390193323Sed    // [2] Set up new manager's top level manager
391193323Sed    PMTopLevelManager *TPM = PMD->getTopLevelManager();
392193323Sed    TPM->addIndirectPassManager(LPPM);
393193323Sed
394193323Sed    // [3] Assign manager to manage this new manager. This may create
395193323Sed    // and push new managers into PMS
396202878Srdivacky    Pass *P = LPPM->getAsPass();
397193323Sed    TPM->schedulePass(P);
398193323Sed
399193323Sed    // [4] Push new manager into PMS
400193323Sed    PMS.push(LPPM);
401193323Sed  }
402193323Sed
403193323Sed  LPPM->add(this);
404193323Sed}
405