LoopPass.cpp revision 198090
1145132Sanholt//===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2145132Sanholt//
3145132Sanholt//                     The LLVM Compiler Infrastructure
4145132Sanholt//
5145132Sanholt// This file is distributed under the University of Illinois Open Source
6145132Sanholt// License. See LICENSE.TXT for details.
7145132Sanholt//
8145132Sanholt//===----------------------------------------------------------------------===//
9145132Sanholt//
10145132Sanholt// This file implements LoopPass and LPPassManager. All loop optimization
11145132Sanholt// and transformation passes are derived from LoopPass. LPPassManager is
12145132Sanholt// responsible for managing LoopPasses.
13145132Sanholt//
14145132Sanholt//===----------------------------------------------------------------------===//
15145132Sanholt
16145132Sanholt#include "llvm/Analysis/LoopPass.h"
17145132Sanholtusing namespace llvm;
18145132Sanholt
19145132Sanholt//===----------------------------------------------------------------------===//
20145132Sanholt// LPPassManager
21145132Sanholt//
22145132Sanholt
23145132Sanholtchar LPPassManager::ID = 0;
24152909Sanholt
25152909SanholtLPPassManager::LPPassManager(int Depth)
26152909Sanholt  : FunctionPass(&ID), PMDataManager(Depth) {
27182080Srnoland  skipThisLoop = false;
28182080Srnoland  redoThisLoop = false;
29182080Srnoland  LI = NULL;
30182080Srnoland  CurrentLoop = NULL;
31182080Srnoland}
32145132Sanholt
33145132Sanholt/// Delete loop from the loop queue and loop hierarchy (LoopInfo).
34145132Sanholtvoid LPPassManager::deleteLoopFromQueue(Loop *L) {
35145132Sanholt
36145132Sanholt  if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
37145132Sanholt    // Reparent all of the blocks in this loop.  Since BBLoop had a parent,
38145132Sanholt    // they are now all in it.
39145132Sanholt    for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
40145132Sanholt         I != E; ++I)
41194759Srnoland      if (LI->getLoopFor(*I) == L)    // Don't change blocks in subloops.
42145132Sanholt        LI->changeLoopFor(*I, ParentLoop);
43145132Sanholt
44145132Sanholt    // Remove the loop from its parent loop.
45145132Sanholt    for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
46145132Sanholt         ++I) {
47145132Sanholt      assert(I != E && "Couldn't find loop");
48145132Sanholt      if (*I == L) {
49145132Sanholt        ParentLoop->removeChildLoop(I);
50145132Sanholt        break;
51194759Srnoland      }
52145132Sanholt    }
53145132Sanholt
54145132Sanholt    // Move all subloops into the parent loop.
55145132Sanholt    while (!L->empty())
56145132Sanholt      ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
57145132Sanholt  } else {
58145132Sanholt    // Reparent all of the blocks in this loop.  Since BBLoop had no parent,
59145132Sanholt    // they no longer in a loop at all.
60182080Srnoland
61145132Sanholt    for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
62145132Sanholt      // Don't change blocks in subloops.
63145132Sanholt      if (LI->getLoopFor(L->getBlocks()[i]) == L) {
64145132Sanholt        LI->removeBlock(L->getBlocks()[i]);
65145132Sanholt        --i;
66145132Sanholt      }
67183833Srnoland    }
68145132Sanholt
69145132Sanholt    // Remove the loop from the top-level LoopInfo object.
70145132Sanholt    for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
71145132Sanholt      assert(I != E && "Couldn't find loop");
72145132Sanholt      if (*I == L) {
73145132Sanholt        LI->removeLoop(I);
74145132Sanholt        break;
75145132Sanholt      }
76145132Sanholt    }
77145132Sanholt
78145132Sanholt    // Move all of the subloops to the top-level.
79145132Sanholt    while (!L->empty())
80145132Sanholt      LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
81145132Sanholt  }
82145132Sanholt
83145132Sanholt  delete L;
84145132Sanholt
85145132Sanholt  // If L is current loop then skip rest of the passes and let
86145132Sanholt  // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
87145132Sanholt  // and continue applying other passes on CurrentLoop.
88145132Sanholt  if (CurrentLoop == L) {
89145132Sanholt    skipThisLoop = true;
90145132Sanholt    return;
91145132Sanholt  }
92145132Sanholt
93145132Sanholt  for (std::deque<Loop *>::iterator I = LQ.begin(),
94145132Sanholt         E = LQ.end(); I != E; ++I) {
95145132Sanholt    if (*I == L) {
96145132Sanholt      LQ.erase(I);
97145132Sanholt      break;
98220979Skib    }
99145132Sanholt  }
100145132Sanholt}
101145132Sanholt
102145132Sanholt// Inset loop into loop nest (LoopInfo) and loop queue (LQ).
103145132Sanholtvoid LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
104145132Sanholt
105145132Sanholt  assert (CurrentLoop != L && "Cannot insert CurrentLoop");
106145132Sanholt
107145132Sanholt  // Insert into loop nest
108145132Sanholt  if (ParentLoop)
109145132Sanholt    ParentLoop->addChildLoop(L);
110145132Sanholt  else
111145132Sanholt    LI->addTopLevelLoop(L);
112145132Sanholt
113145132Sanholt  insertLoopIntoQueue(L);
114182080Srnoland}
115145132Sanholt
116145132Sanholtvoid LPPassManager::insertLoopIntoQueue(Loop *L) {
117145132Sanholt  // Insert L into loop queue
118145132Sanholt  if (L == CurrentLoop)
119183833Srnoland    redoLoop(L);
120145132Sanholt  else if (!L->getParentLoop())
121145132Sanholt    // This is top level loop.
122145132Sanholt    LQ.push_front(L);
123145132Sanholt  else {
124145132Sanholt    // Insert L after the parent loop.
125145132Sanholt    for (std::deque<Loop *>::iterator I = LQ.begin(),
126145132Sanholt           E = LQ.end(); I != E; ++I) {
127145132Sanholt      if (*I == L->getParentLoop()) {
128145132Sanholt        // deque does not support insert after.
129145132Sanholt        ++I;
130145132Sanholt        LQ.insert(I, 1, L);
131145132Sanholt        break;
132145132Sanholt      }
133145132Sanholt    }
134145132Sanholt  }
135182080Srnoland}
136145132Sanholt
137145132Sanholt// Reoptimize this loop. LPPassManager will re-insert this loop into the
138145132Sanholt// queue. This allows LoopPass to change loop nest for the loop. This
139145132Sanholt// utility may send LPPassManager into infinite loops so use caution.
140183573Srnolandvoid LPPassManager::redoLoop(Loop *L) {
141145132Sanholt  assert (CurrentLoop == L && "Can redo only CurrentLoop");
142145132Sanholt  redoThisLoop = true;
143145132Sanholt}
144145132Sanholt
145145132Sanholt/// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
146145132Sanholt/// all loop passes.
147145132Sanholtvoid LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
148145132Sanholt                                                  BasicBlock *To, Loop *L) {
149145132Sanholt  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
150145132Sanholt    Pass *P = getContainedPass(Index);
151145132Sanholt    LoopPass *LP = dynamic_cast<LoopPass *>(P);
152145132Sanholt    LP->cloneBasicBlockAnalysis(From, To, L);
153145132Sanholt  }
154145132Sanholt}
155145132Sanholt
156145132Sanholt/// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
157145132Sanholtvoid LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
158145132Sanholt  if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
159145132Sanholt    for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
160182080Srnoland         ++BI) {
161145132Sanholt      Instruction &I = *BI;
162145132Sanholt      deleteSimpleAnalysisValue(&I, L);
163145132Sanholt    }
164145132Sanholt  }
165145132Sanholt  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
166145132Sanholt    Pass *P = getContainedPass(Index);
167145132Sanholt    LoopPass *LP = dynamic_cast<LoopPass *>(P);
168145132Sanholt    LP->deleteAnalysisValue(V, L);
169145132Sanholt  }
170145132Sanholt}
171145132Sanholt
172145132Sanholt
173145132Sanholt// Recurse through all subloops and all loops  into LQ.
174145132Sanholtstatic void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
175145132Sanholt  LQ.push_back(L);
176145132Sanholt  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
177183833Srnoland    addLoopIntoQueue(*I, LQ);
178183833Srnoland}
179145132Sanholt
180145132Sanholt/// Pass Manager itself does not invalidate any analysis info.
181145132Sanholtvoid LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
182145132Sanholt  // LPPassManager needs LoopInfo. In the long term LoopInfo class will
183145132Sanholt  // become part of LPPassManager.
184145132Sanholt  Info.addRequired<LoopInfo>();
185145132Sanholt  Info.setPreservesAll();
186145132Sanholt}
187145132Sanholt
188145132Sanholt/// run - Execute all of the passes scheduled for execution.  Keep track of
189145132Sanholt/// whether any of the passes modifies the function, and if so, return true.
190189562Srnolandbool LPPassManager::runOnFunction(Function &F) {
191207066Srnoland  LI = &getAnalysis<LoopInfo>();
192145132Sanholt  bool Changed = false;
193145132Sanholt
194145132Sanholt  // Collect inherited analysis from Module level pass manager.
195145132Sanholt  populateInheritedAnalysis(TPM->activeStack);
196145132Sanholt
197145132Sanholt  // Populate Loop Queue
198145132Sanholt  for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
199145132Sanholt    addLoopIntoQueue(*I, LQ);
200145132Sanholt
201145132Sanholt  if (LQ.empty()) // No loops, skip calling finalizers
202145132Sanholt    return false;
203145132Sanholt
204145132Sanholt  // Initialization
205145132Sanholt  for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
206145132Sanholt       I != E; ++I) {
207207066Srnoland    Loop *L = *I;
208207066Srnoland    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
209207066Srnoland      Pass *P = getContainedPass(Index);
210207066Srnoland      LoopPass *LP = dynamic_cast<LoopPass *>(P);
211207066Srnoland      if (LP)
212145132Sanholt        Changed |= LP->doInitialization(L, *this);
213145132Sanholt    }
214145132Sanholt  }
215145132Sanholt
216183833Srnoland  // Walk Loops
217145132Sanholt  while (!LQ.empty()) {
218145132Sanholt
219145132Sanholt    CurrentLoop  = LQ.back();
220145132Sanholt    skipThisLoop = false;
221145132Sanholt    redoThisLoop = false;
222182080Srnoland
223145132Sanholt    // Run all passes on the current Loop.
224145132Sanholt    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
225145132Sanholt      Pass *P = getContainedPass(Index);
226145132Sanholt
227145132Sanholt      dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG,
228145132Sanholt                   CurrentLoop->getHeader()->getNameStr());
229145132Sanholt      dumpRequiredSet(P);
230145132Sanholt
231145132Sanholt      initializeAnalysisImpl(P);
232145132Sanholt
233145132Sanholt      LoopPass *LP = dynamic_cast<LoopPass *>(P);
234145132Sanholt      assert(LP && "Invalid LPPassManager member");
235145132Sanholt      {
236145132Sanholt        PassManagerPrettyStackEntry X(LP, *CurrentLoop->getHeader());
237145132Sanholt        Timer *T = StartPassTimer(P);
238145132Sanholt        Changed |= LP->runOnLoop(CurrentLoop, *this);
239145132Sanholt        StopPassTimer(P, T);
240183833Srnoland      }
241183833Srnoland
242145132Sanholt      if (Changed)
243145132Sanholt        dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
244145132Sanholt                     skipThisLoop ? "<deleted>" :
245145132Sanholt                                    CurrentLoop->getHeader()->getNameStr());
246145132Sanholt      dumpPreservedSet(P);
247145132Sanholt
248145132Sanholt      if (!skipThisLoop) {
249145132Sanholt        // Manually check that this loop is still healthy. This is done
250145132Sanholt        // instead of relying on LoopInfo::verifyLoop since LoopInfo
251145132Sanholt        // is a function pass and it's really expensive to verify every
252145132Sanholt        // loop in the function every time. That level of checking can be
253145132Sanholt        // enabled with the -verify-loop-info option.
254145132Sanholt        Timer *T = StartPassTimer(LI);
255145132Sanholt        CurrentLoop->verifyLoop();
256145132Sanholt        StopPassTimer(LI, T);
257145132Sanholt
258145132Sanholt        // Then call the regular verifyAnalysis functions.
259145132Sanholt        verifyPreservedAnalysis(LP);
260145132Sanholt      }
261145132Sanholt
262215367Snwhitehorn      removeNotPreservedAnalysis(P);
263145132Sanholt      recordAvailableAnalysis(P);
264145132Sanholt      removeDeadPasses(P,
265145132Sanholt                       skipThisLoop ? "<deleted>" :
266145132Sanholt                                      CurrentLoop->getHeader()->getNameStr(),
267145132Sanholt                       ON_LOOP_MSG);
268145132Sanholt
269145132Sanholt      if (skipThisLoop)
270145132Sanholt        // Do not run other passes on this loop.
271145132Sanholt        break;
272145132Sanholt    }
273183833Srnoland
274145132Sanholt    // If the loop was deleted, release all the loop passes. This frees up
275145132Sanholt    // some memory, and avoids trouble with the pass manager trying to call
276145132Sanholt    // verifyAnalysis on them.
277145132Sanholt    if (skipThisLoop)
278145132Sanholt      for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
279182080Srnoland        Pass *P = getContainedPass(Index);
280183573Srnoland        freePass(P, "<deleted>", ON_LOOP_MSG);
281145132Sanholt      }
282145132Sanholt
283145132Sanholt    // Pop the loop from queue after running all passes.
284145132Sanholt    LQ.pop_back();
285145132Sanholt
286145132Sanholt    if (redoThisLoop)
287145132Sanholt      LQ.push_back(CurrentLoop);
288145132Sanholt  }
289145132Sanholt
290145132Sanholt  // Finalization
291183833Srnoland  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
292183833Srnoland    Pass *P = getContainedPass(Index);
293145132Sanholt    LoopPass *LP = dynamic_cast <LoopPass *>(P);
294145132Sanholt    if (LP)
295145132Sanholt      Changed |= LP->doFinalization();
296145132Sanholt  }
297145132Sanholt
298145132Sanholt  return Changed;
299145132Sanholt}
300145132Sanholt
301145132Sanholt/// Print passes managed by this manager
302145132Sanholtvoid LPPassManager::dumpPassStructure(unsigned Offset) {
303196465Srnoland  errs().indent(Offset*2) << "Loop Pass Manager\n";
304196465Srnoland  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
305145132Sanholt    Pass *P = getContainedPass(Index);
306145132Sanholt    P->dumpPassStructure(Offset + 1);
307196465Srnoland    dumpLastUses(P, Offset+1);
308145132Sanholt  }
309196465Srnoland}
310145132Sanholt
311145132Sanholt
312145132Sanholt//===----------------------------------------------------------------------===//
313145132Sanholt// LoopPass
314145132Sanholt
315145132Sanholt// Check if this pass is suitable for the current LPPassManager, if
316145132Sanholt// available. This pass P is not suitable for a LPPassManager if P
317145132Sanholt// is not preserving higher level analysis info used by other
318183833Srnoland// LPPassManager passes. In such case, pop LPPassManager from the
319145132Sanholt// stack. This will force assignPassManager() to create new
320145132Sanholt// LPPassManger as expected.
321194759Srnolandvoid LoopPass::preparePassManager(PMStack &PMS) {
322194759Srnoland
323194759Srnoland  // Find LPPassManager
324194759Srnoland  while (!PMS.empty() &&
325194759Srnoland         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
326194759Srnoland    PMS.pop();
327194759Srnoland
328194759Srnoland  LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
329194759Srnoland
330194759Srnoland  // If this pass is destroying high level information that is used
331194759Srnoland  // by other passes that are managed by LPM then do not insert
332194759Srnoland  // this pass in current LPM. Use new LPPassManager.
333194759Srnoland  if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
334194759Srnoland    PMS.pop();
335194759Srnoland}
336194759Srnoland
337194759Srnoland/// Assign pass manager to manage this pass.
338194759Srnolandvoid LoopPass::assignPassManager(PMStack &PMS,
339194759Srnoland                                 PassManagerType PreferredType) {
340194759Srnoland  // Find LPPassManager
341194759Srnoland  while (!PMS.empty() &&
342194759Srnoland         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
343    PMS.pop();
344
345  LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
346
347  // Create new Loop Pass Manager if it does not exist.
348  if (!LPPM) {
349
350    assert (!PMS.empty() && "Unable to create Loop Pass Manager");
351    PMDataManager *PMD = PMS.top();
352
353    // [1] Create new Call Graph Pass Manager
354    LPPM = new LPPassManager(PMD->getDepth() + 1);
355    LPPM->populateInheritedAnalysis(PMS);
356
357    // [2] Set up new manager's top level manager
358    PMTopLevelManager *TPM = PMD->getTopLevelManager();
359    TPM->addIndirectPassManager(LPPM);
360
361    // [3] Assign manager to manage this new manager. This may create
362    // and push new managers into PMS
363    Pass *P = dynamic_cast<Pass *>(LPPM);
364    TPM->schedulePass(P);
365
366    // [4] Push new manager into PMS
367    PMS.push(LPPM);
368  }
369
370  LPPM->add(this);
371}
372