blob: 4d2e2906082e2664cbd4d07d20a3aafcf2a92c76 [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 {
Devang Patel5b9e8d62007-02-23 18:05:55 +000029 // Loops with highest depth has the highest priority.
30 return L1->getLoopDepth() < L2->getLoopDepth();
Devang Pateld0e6e332007-02-22 23:30:07 +000031 }
32};
33
34// Loop queue used by Loop Pass Manager. This is a wrapper class
35// that hides implemenation detail (use of priority_queue) inside .cpp file.
36class LoopQueue {
Devang Patel643a79b2007-02-22 23:45:15 +000037public:
Devang Pateld0e6e332007-02-22 23:30:07 +000038 inline void push(Loop *L) { LPQ.push(L); }
39 inline void pop() { LPQ.pop(); }
40 inline Loop *top() { return LPQ.top(); }
Devang Patel643a79b2007-02-22 23:45:15 +000041 inline bool empty() { return LPQ.empty(); }
Devang Pateld0e6e332007-02-22 23:30:07 +000042private:
43 std::priority_queue<Loop *, std::vector<Loop *>, LoopCompare> LPQ;
44};
45
46} // End of LLVM namespace
47
48//===----------------------------------------------------------------------===//
Devang Patel16a31c42007-02-22 08:56:17 +000049// LPPassManager
50//
51/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
52
Devang Pateld0e6e332007-02-22 23:30:07 +000053LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) {
Devang Patel8ded5852007-02-23 00:16:44 +000054 skipThisLoop = false;
55 redoThisLoop = false;
Devang Pateld0e6e332007-02-22 23:30:07 +000056 LQ = new LoopQueue();
57}
58
59LPPassManager::~LPPassManager() {
60 delete LQ;
61}
62
Devang Patel5afdc7d2007-02-23 00:10:16 +000063/// Delete loop from the loop queue. This is used by Loop pass to inform
64/// Loop Pass Manager that it should skip rest of the passes for this loop.
65void LPPassManager::deleteLoopFromQueue(Loop *L) {
66 // Do not pop loop from LQ here. It will be done by runOnFunction while loop.
67 skipThisLoop = true;
68}
69
Devang Patel8ded5852007-02-23 00:16:44 +000070// Reoptimize this loop. LPPassManager will re-insert this loop into the
71// queue. This allows LoopPass to change loop nest for the loop. This
72// utility may send LPPassManager into infinite loops so use caution.
73void LPPassManager::redoLoop(Loop *L) {
74 redoThisLoop = true;
75}
76
Devang Patel643a79b2007-02-22 23:45:15 +000077// Recurse through all subloops and all loops into LQ.
78static void addLoopIntoQueue(Loop *L, LoopQueue *LQ) {
79 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
80 addLoopIntoQueue(*I, LQ);
81 LQ->push(L);
82}
83
Devang Patel16a31c42007-02-22 08:56:17 +000084/// run - Execute all of the passes scheduled for execution. Keep track of
85/// whether any of the passes modifies the function, and if so, return true.
86bool LPPassManager::runOnFunction(Function &F) {
87 LoopInfo &LI = getAnalysis<LoopInfo>();
88 bool Changed = false;
89
Devang Patel643a79b2007-02-22 23:45:15 +000090 // Populate Loop Queue
91 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
92 addLoopIntoQueue(*I, LQ);
93
Devang Patel16a31c42007-02-22 08:56:17 +000094 std::string Msg1 = "Executing Pass '";
95 std::string Msg3 = "' Made Modification '";
96
97 // Walk Loops
Devang Patel643a79b2007-02-22 23:45:15 +000098 while (!LQ->empty()) {
99
100 Loop *L = LQ->top();
Devang Patel5afdc7d2007-02-23 00:10:16 +0000101 skipThisLoop = false;
Devang Patel8ded5852007-02-23 00:16:44 +0000102 redoThisLoop = false;
Devang Patel5afdc7d2007-02-23 00:10:16 +0000103
Devang Patel16a31c42007-02-22 08:56:17 +0000104 // Run all passes on current SCC
105 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel643a79b2007-02-22 23:45:15 +0000106
Devang Patel16a31c42007-02-22 08:56:17 +0000107 Pass *P = getContainedPass(Index);
108 AnalysisUsage AnUsage;
109 P->getAnalysisUsage(AnUsage);
110
111 std::string Msg2 = "' on Loop ...\n'";
112 dumpPassInfo(P, Msg1, Msg2);
113 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
114
115 initializeAnalysisImpl(P);
116
117 StartPassTimer(P);
118 LoopPass *LP = dynamic_cast<LoopPass *>(P);
119 assert (LP && "Invalid LPPassManager member");
Devang Patel4b264632007-02-23 17:53:17 +0000120 LP->runOnLoop(L, *this);
Devang Patel16a31c42007-02-22 08:56:17 +0000121 StopPassTimer(P);
122
123 if (Changed)
124 dumpPassInfo(P, Msg3, Msg2);
125 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
126
127 removeNotPreservedAnalysis(P);
128 recordAvailableAnalysis(P);
129 removeDeadPasses(P, Msg2);
Devang Patel5afdc7d2007-02-23 00:10:16 +0000130
131 if (skipThisLoop)
132 // Do not run other passes on this loop.
133 break;
Devang Patel16a31c42007-02-22 08:56:17 +0000134 }
Devang Patel643a79b2007-02-22 23:45:15 +0000135
136 // Pop the loop from queue after running all passes.
137 LQ->pop();
Devang Patel8ded5852007-02-23 00:16:44 +0000138
139 if (redoThisLoop)
140 LQ->push(L);
Devang Patel16a31c42007-02-22 08:56:17 +0000141 }
142
143 return Changed;
144}
145
146
Devang Patelbfd59052007-02-23 00:36:57 +0000147//===----------------------------------------------------------------------===//
148// LoopPass
149
150/// Assign pass manager to manage this pass.
151void LoopPass::assignPassManager(PMStack &PMS,
152 PassManagerType PreferredType) {
153 // Find LPPassManager
154 while (!PMS.empty()) {
155 if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
156 PMS.pop();
157 else;
158 break;
159 }
160
161 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
162
163 // Create new Loop Pass Manager if it does not exist.
164 if (!LPPM) {
165
166 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
167 PMDataManager *PMD = PMS.top();
168
169 // [1] Create new Call Graph Pass Manager
170 LPPM = new LPPassManager(PMD->getDepth() + 1);
171
172 // [2] Set up new manager's top level manager
173 PMTopLevelManager *TPM = PMD->getTopLevelManager();
174 TPM->addIndirectPassManager(LPPM);
175
176 // [3] Assign manager to manage this new manager. This may create
177 // and push new managers into PMS
178 Pass *P = dynamic_cast<Pass *>(LPPM);
179 P->assignPassManager(PMS);
180
181 // [4] Push new manager into PMS
182 PMS.push(LPPM);
183 }
184
185 LPPM->add(this);
186}
187