1//===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
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 LoopPass and LPPassManager. All loop optimization
11// and transformation passes are derived from LoopPass. LPPassManager is
12// responsible for managing LoopPasses.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/LoopPass.h"
17#include "llvm/IR/IRPrintingPasses.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/Timer.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "loop-pass-manager"
24
25namespace {
26
27/// PrintLoopPass - Print a Function corresponding to a Loop.
28///
29class PrintLoopPass : public LoopPass {
30private:
31  std::string Banner;
32  raw_ostream &Out;       // raw_ostream to print on.
33
34public:
35  static char ID;
36  PrintLoopPass(const std::string &B, raw_ostream &o)
37      : LoopPass(ID), Banner(B), Out(o) {}
38
39  void getAnalysisUsage(AnalysisUsage &AU) const override {
40    AU.setPreservesAll();
41  }
42
43  bool runOnLoop(Loop *L, LPPassManager &) override {
44    Out << Banner;
45    for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
46         b != be;
47         ++b) {
48      if (*b)
49        (*b)->print(Out);
50      else
51        Out << "Printing <null> block";
52    }
53    return false;
54  }
55};
56
57char PrintLoopPass::ID = 0;
58}
59
60//===----------------------------------------------------------------------===//
61// LPPassManager
62//
63
64char LPPassManager::ID = 0;
65
66LPPassManager::LPPassManager()
67  : FunctionPass(ID), PMDataManager() {
68  skipThisLoop = false;
69  redoThisLoop = false;
70  LI = nullptr;
71  CurrentLoop = nullptr;
72}
73
74/// Delete loop from the loop queue and loop hierarchy (LoopInfo).
75void LPPassManager::deleteLoopFromQueue(Loop *L) {
76
77  LI->updateUnloop(L);
78
79  // If L is current loop then skip rest of the passes and let
80  // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
81  // and continue applying other passes on CurrentLoop.
82  if (CurrentLoop == L)
83    skipThisLoop = true;
84
85  delete L;
86
87  if (skipThisLoop)
88    return;
89
90  for (std::deque<Loop *>::iterator I = LQ.begin(),
91         E = LQ.end(); I != E; ++I) {
92    if (*I == L) {
93      LQ.erase(I);
94      break;
95    }
96  }
97}
98
99// Inset loop into loop nest (LoopInfo) and loop queue (LQ).
100void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
101
102  assert (CurrentLoop != L && "Cannot insert CurrentLoop");
103
104  // Insert into loop nest
105  if (ParentLoop)
106    ParentLoop->addChildLoop(L);
107  else
108    LI->addTopLevelLoop(L);
109
110  insertLoopIntoQueue(L);
111}
112
113void LPPassManager::insertLoopIntoQueue(Loop *L) {
114  // Insert L into loop queue
115  if (L == CurrentLoop)
116    redoLoop(L);
117  else if (!L->getParentLoop())
118    // This is top level loop.
119    LQ.push_front(L);
120  else {
121    // Insert L after the parent loop.
122    for (std::deque<Loop *>::iterator I = LQ.begin(),
123           E = LQ.end(); I != E; ++I) {
124      if (*I == L->getParentLoop()) {
125        // deque does not support insert after.
126        ++I;
127        LQ.insert(I, 1, L);
128        break;
129      }
130    }
131  }
132}
133
134// Reoptimize this loop. LPPassManager will re-insert this loop into the
135// queue. This allows LoopPass to change loop nest for the loop. This
136// utility may send LPPassManager into infinite loops so use caution.
137void LPPassManager::redoLoop(Loop *L) {
138  assert (CurrentLoop == L && "Can redo only CurrentLoop");
139  redoThisLoop = true;
140}
141
142/// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
143/// all loop passes.
144void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
145                                                  BasicBlock *To, Loop *L) {
146  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
147    LoopPass *LP = getContainedPass(Index);
148    LP->cloneBasicBlockAnalysis(From, To, L);
149  }
150}
151
152/// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
153void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
154  if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
155    for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
156         ++BI) {
157      Instruction &I = *BI;
158      deleteSimpleAnalysisValue(&I, L);
159    }
160  }
161  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
162    LoopPass *LP = getContainedPass(Index);
163    LP->deleteAnalysisValue(V, L);
164  }
165}
166
167
168// Recurse through all subloops and all loops  into LQ.
169static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
170  LQ.push_back(L);
171  for (Loop::reverse_iterator I = L->rbegin(), E = L->rend(); I != E; ++I)
172    addLoopIntoQueue(*I, LQ);
173}
174
175/// Pass Manager itself does not invalidate any analysis info.
176void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
177  // LPPassManager needs LoopInfo. In the long term LoopInfo class will
178  // become part of LPPassManager.
179  Info.addRequired<LoopInfo>();
180  Info.setPreservesAll();
181}
182
183/// run - Execute all of the passes scheduled for execution.  Keep track of
184/// whether any of the passes modifies the function, and if so, return true.
185bool LPPassManager::runOnFunction(Function &F) {
186  LI = &getAnalysis<LoopInfo>();
187  bool Changed = false;
188
189  // Collect inherited analysis from Module level pass manager.
190  populateInheritedAnalysis(TPM->activeStack);
191
192  // Populate the loop queue in reverse program order. There is no clear need to
193  // process sibling loops in either forward or reverse order. There may be some
194  // advantage in deleting uses in a later loop before optimizing the
195  // definitions in an earlier loop. If we find a clear reason to process in
196  // forward order, then a forward variant of LoopPassManager should be created.
197  //
198  // Note that LoopInfo::iterator visits loops in reverse program
199  // order. Here, reverse_iterator gives us a forward order, and the LoopQueue
200  // reverses the order a third time by popping from the back.
201  for (LoopInfo::reverse_iterator I = LI->rbegin(), E = LI->rend(); I != E; ++I)
202    addLoopIntoQueue(*I, LQ);
203
204  if (LQ.empty()) // No loops, skip calling finalizers
205    return false;
206
207  // Initialization
208  for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
209       I != E; ++I) {
210    Loop *L = *I;
211    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
212      LoopPass *P = getContainedPass(Index);
213      Changed |= P->doInitialization(L, *this);
214    }
215  }
216
217  // Walk Loops
218  while (!LQ.empty()) {
219
220    CurrentLoop  = LQ.back();
221    skipThisLoop = false;
222    redoThisLoop = false;
223
224    // Run all passes on the current Loop.
225    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
226      LoopPass *P = getContainedPass(Index);
227
228      dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG,
229                   CurrentLoop->getHeader()->getName());
230      dumpRequiredSet(P);
231
232      initializeAnalysisImpl(P);
233
234      {
235        PassManagerPrettyStackEntry X(P, *CurrentLoop->getHeader());
236        TimeRegion PassTimer(getPassTimer(P));
237
238        Changed |= P->runOnLoop(CurrentLoop, *this);
239      }
240
241      if (Changed)
242        dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
243                     skipThisLoop ? "<deleted>" :
244                                    CurrentLoop->getHeader()->getName());
245      dumpPreservedSet(P);
246
247      if (!skipThisLoop) {
248        // Manually check that this loop is still healthy. This is done
249        // instead of relying on LoopInfo::verifyLoop since LoopInfo
250        // is a function pass and it's really expensive to verify every
251        // loop in the function every time. That level of checking can be
252        // enabled with the -verify-loop-info option.
253        {
254          TimeRegion PassTimer(getPassTimer(LI));
255          CurrentLoop->verifyLoop();
256        }
257
258        // Then call the regular verifyAnalysis functions.
259        verifyPreservedAnalysis(P);
260
261        F.getContext().yield();
262      }
263
264      removeNotPreservedAnalysis(P);
265      recordAvailableAnalysis(P);
266      removeDeadPasses(P,
267                       skipThisLoop ? "<deleted>" :
268                                      CurrentLoop->getHeader()->getName(),
269                       ON_LOOP_MSG);
270
271      if (skipThisLoop)
272        // Do not run other passes on this loop.
273        break;
274    }
275
276    // If the loop was deleted, release all the loop passes. This frees up
277    // some memory, and avoids trouble with the pass manager trying to call
278    // verifyAnalysis on them.
279    if (skipThisLoop)
280      for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
281        Pass *P = getContainedPass(Index);
282        freePass(P, "<deleted>", ON_LOOP_MSG);
283      }
284
285    // Pop the loop from queue after running all passes.
286    LQ.pop_back();
287
288    if (redoThisLoop)
289      LQ.push_back(CurrentLoop);
290  }
291
292  // Finalization
293  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
294    LoopPass *P = getContainedPass(Index);
295    Changed |= P->doFinalization();
296  }
297
298  return Changed;
299}
300
301/// Print passes managed by this manager
302void LPPassManager::dumpPassStructure(unsigned Offset) {
303  errs().indent(Offset*2) << "Loop Pass Manager\n";
304  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
305    Pass *P = getContainedPass(Index);
306    P->dumpPassStructure(Offset + 1);
307    dumpLastUses(P, Offset+1);
308  }
309}
310
311
312//===----------------------------------------------------------------------===//
313// LoopPass
314
315Pass *LoopPass::createPrinterPass(raw_ostream &O,
316                                  const std::string &Banner) const {
317  return new PrintLoopPass(Banner, O);
318}
319
320// Check if this pass is suitable for the current LPPassManager, if
321// available. This pass P is not suitable for a LPPassManager if P
322// is not preserving higher level analysis info used by other
323// LPPassManager passes. In such case, pop LPPassManager from the
324// stack. This will force assignPassManager() to create new
325// LPPassManger as expected.
326void LoopPass::preparePassManager(PMStack &PMS) {
327
328  // Find LPPassManager
329  while (!PMS.empty() &&
330         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
331    PMS.pop();
332
333  // If this pass is destroying high level information that is used
334  // by other passes that are managed by LPM then do not insert
335  // this pass in current LPM. Use new LPPassManager.
336  if (PMS.top()->getPassManagerType() == PMT_LoopPassManager &&
337      !PMS.top()->preserveHigherLevelAnalysis(this))
338    PMS.pop();
339}
340
341/// Assign pass manager to manage this pass.
342void LoopPass::assignPassManager(PMStack &PMS,
343                                 PassManagerType PreferredType) {
344  // Find LPPassManager
345  while (!PMS.empty() &&
346         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
347    PMS.pop();
348
349  LPPassManager *LPPM;
350  if (PMS.top()->getPassManagerType() == PMT_LoopPassManager)
351    LPPM = (LPPassManager*)PMS.top();
352  else {
353    // Create new Loop Pass Manager if it does not exist.
354    assert (!PMS.empty() && "Unable to create Loop Pass Manager");
355    PMDataManager *PMD = PMS.top();
356
357    // [1] Create new Loop Pass Manager
358    LPPM = new LPPassManager();
359    LPPM->populateInheritedAnalysis(PMS);
360
361    // [2] Set up new manager's top level manager
362    PMTopLevelManager *TPM = PMD->getTopLevelManager();
363    TPM->addIndirectPassManager(LPPM);
364
365    // [3] Assign manager to manage this new manager. This may create
366    // and push new managers into PMS
367    Pass *P = LPPM->getAsPass();
368    TPM->schedulePass(P);
369
370    // [4] Push new manager into PMS
371    PMS.push(LPPM);
372  }
373
374  LPPM->add(this);
375}
376
377// Containing function has Attribute::OptimizeNone and transformation
378// passes should skip it.
379bool LoopPass::skipOptnoneFunction(const Loop *L) const {
380  const Function *F = L->getHeader()->getParent();
381  if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
382    // FIXME: Report this to dbgs() only once per function.
383    DEBUG(dbgs() << "Skipping pass '" << getPassName()
384          << "' in function " << F->getName() << "\n");
385    // FIXME: Delete loop from pass manager's queue?
386    return true;
387  }
388  return false;
389}
390