blob: 95a18e5b00d349555679779abb787ae47f83e652 [file] [log] [blame]
Devang Patel16a31c42007-02-22 08:56:17 +00001//===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Devang Patel and is distributed under
6// the University of Illinois Open Source 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"
Devang Pateld0e6e332007-02-22 23:30:07 +000017#include <queue>
Devang Patel16a31c42007-02-22 08:56:17 +000018using namespace llvm;
19
20//===----------------------------------------------------------------------===//
Devang Pateld0e6e332007-02-22 23:30:07 +000021// LoopQueue
22
23namespace llvm {
24
25// Compare Two loops based on their depth in loop nest.
26class LoopCompare {
27public:
28 bool operator()( Loop *L1, Loop *L2) const {
29 return L1->getLoopDepth() > L2->getLoopDepth();
30 }
31};
32
33// Loop queue used by Loop Pass Manager. This is a wrapper class
34// that hides implemenation detail (use of priority_queue) inside .cpp file.
35class LoopQueue {
Devang Patel643a79b2007-02-22 23:45:15 +000036public:
Devang Pateld0e6e332007-02-22 23:30:07 +000037 inline void push(Loop *L) { LPQ.push(L); }
38 inline void pop() { LPQ.pop(); }
39 inline Loop *top() { return LPQ.top(); }
Devang Patel643a79b2007-02-22 23:45:15 +000040 inline bool empty() { return LPQ.empty(); }
Devang Pateld0e6e332007-02-22 23:30:07 +000041private:
42 std::priority_queue<Loop *, std::vector<Loop *>, LoopCompare> LPQ;
43};
44
45} // End of LLVM namespace
46
47//===----------------------------------------------------------------------===//
Devang Patel16a31c42007-02-22 08:56:17 +000048// LPPassManager
49//
50/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
51
Devang Pateld0e6e332007-02-22 23:30:07 +000052LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) {
53 LQ = new LoopQueue();
54}
55
56LPPassManager::~LPPassManager() {
57 delete LQ;
58}
59
Devang Patel643a79b2007-02-22 23:45:15 +000060// Recurse through all subloops and all loops into LQ.
61static void addLoopIntoQueue(Loop *L, LoopQueue *LQ) {
62 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
63 addLoopIntoQueue(*I, LQ);
64 LQ->push(L);
65}
66
Devang Patel16a31c42007-02-22 08:56:17 +000067/// run - Execute all of the passes scheduled for execution. Keep track of
68/// whether any of the passes modifies the function, and if so, return true.
69bool LPPassManager::runOnFunction(Function &F) {
70 LoopInfo &LI = getAnalysis<LoopInfo>();
71 bool Changed = false;
72
Devang Patel643a79b2007-02-22 23:45:15 +000073 // Populate Loop Queue
74 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
75 addLoopIntoQueue(*I, LQ);
76
Devang Patel16a31c42007-02-22 08:56:17 +000077 std::string Msg1 = "Executing Pass '";
78 std::string Msg3 = "' Made Modification '";
79
80 // Walk Loops
Devang Patel643a79b2007-02-22 23:45:15 +000081 while (!LQ->empty()) {
82
83 Loop *L = LQ->top();
Devang Patel16a31c42007-02-22 08:56:17 +000084 // Run all passes on current SCC
85 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel643a79b2007-02-22 23:45:15 +000086
Devang Patel16a31c42007-02-22 08:56:17 +000087 Pass *P = getContainedPass(Index);
88 AnalysisUsage AnUsage;
89 P->getAnalysisUsage(AnUsage);
90
91 std::string Msg2 = "' on Loop ...\n'";
92 dumpPassInfo(P, Msg1, Msg2);
93 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
94
95 initializeAnalysisImpl(P);
96
97 StartPassTimer(P);
98 LoopPass *LP = dynamic_cast<LoopPass *>(P);
99 assert (LP && "Invalid LPPassManager member");
100 LP->runOnLoop(*L, *this);
101 StopPassTimer(P);
102
103 if (Changed)
104 dumpPassInfo(P, Msg3, Msg2);
105 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
106
107 removeNotPreservedAnalysis(P);
108 recordAvailableAnalysis(P);
109 removeDeadPasses(P, Msg2);
110 }
Devang Patel643a79b2007-02-22 23:45:15 +0000111
112 // Pop the loop from queue after running all passes.
113 LQ->pop();
Devang Patel16a31c42007-02-22 08:56:17 +0000114 }
115
116 return Changed;
117}
118
119