blob: dc5c5683fb218d72b844796b5080f4b4aff7bbb8 [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) {
Devang Patel8ded5852007-02-23 00:16:44 +000053 skipThisLoop = false;
54 redoThisLoop = false;
Devang Pateld0e6e332007-02-22 23:30:07 +000055 LQ = new LoopQueue();
56}
57
58LPPassManager::~LPPassManager() {
59 delete LQ;
60}
61
Devang Patel5afdc7d2007-02-23 00:10:16 +000062/// Delete loop from the loop queue. This is used by Loop pass to inform
63/// Loop Pass Manager that it should skip rest of the passes for this loop.
64void LPPassManager::deleteLoopFromQueue(Loop *L) {
65 // Do not pop loop from LQ here. It will be done by runOnFunction while loop.
66 skipThisLoop = true;
67}
68
Devang Patel8ded5852007-02-23 00:16:44 +000069// Reoptimize this loop. LPPassManager will re-insert this loop into the
70// queue. This allows LoopPass to change loop nest for the loop. This
71// utility may send LPPassManager into infinite loops so use caution.
72void LPPassManager::redoLoop(Loop *L) {
73 redoThisLoop = true;
74}
75
Devang Patel643a79b2007-02-22 23:45:15 +000076// Recurse through all subloops and all loops into LQ.
77static void addLoopIntoQueue(Loop *L, LoopQueue *LQ) {
78 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
79 addLoopIntoQueue(*I, LQ);
80 LQ->push(L);
81}
82
Devang Patel16a31c42007-02-22 08:56:17 +000083/// run - Execute all of the passes scheduled for execution. Keep track of
84/// whether any of the passes modifies the function, and if so, return true.
85bool LPPassManager::runOnFunction(Function &F) {
86 LoopInfo &LI = getAnalysis<LoopInfo>();
87 bool Changed = false;
88
Devang Patel643a79b2007-02-22 23:45:15 +000089 // Populate Loop Queue
90 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
91 addLoopIntoQueue(*I, LQ);
92
Devang Patel16a31c42007-02-22 08:56:17 +000093 std::string Msg1 = "Executing Pass '";
94 std::string Msg3 = "' Made Modification '";
95
96 // Walk Loops
Devang Patel643a79b2007-02-22 23:45:15 +000097 while (!LQ->empty()) {
98
99 Loop *L = LQ->top();
Devang Patel5afdc7d2007-02-23 00:10:16 +0000100 skipThisLoop = false;
Devang Patel8ded5852007-02-23 00:16:44 +0000101 redoThisLoop = false;
Devang Patel5afdc7d2007-02-23 00:10:16 +0000102
Devang Patel16a31c42007-02-22 08:56:17 +0000103 // Run all passes on current SCC
104 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel643a79b2007-02-22 23:45:15 +0000105
Devang Patel16a31c42007-02-22 08:56:17 +0000106 Pass *P = getContainedPass(Index);
107 AnalysisUsage AnUsage;
108 P->getAnalysisUsage(AnUsage);
109
110 std::string Msg2 = "' on Loop ...\n'";
111 dumpPassInfo(P, Msg1, Msg2);
112 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
113
114 initializeAnalysisImpl(P);
115
116 StartPassTimer(P);
117 LoopPass *LP = dynamic_cast<LoopPass *>(P);
118 assert (LP && "Invalid LPPassManager member");
119 LP->runOnLoop(*L, *this);
120 StopPassTimer(P);
121
122 if (Changed)
123 dumpPassInfo(P, Msg3, Msg2);
124 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
125
126 removeNotPreservedAnalysis(P);
127 recordAvailableAnalysis(P);
128 removeDeadPasses(P, Msg2);
Devang Patel5afdc7d2007-02-23 00:10:16 +0000129
130 if (skipThisLoop)
131 // Do not run other passes on this loop.
132 break;
Devang Patel16a31c42007-02-22 08:56:17 +0000133 }
Devang Patel643a79b2007-02-22 23:45:15 +0000134
135 // Pop the loop from queue after running all passes.
136 LQ->pop();
Devang Patel8ded5852007-02-23 00:16:44 +0000137
138 if (redoThisLoop)
139 LQ->push(L);
Devang Patel16a31c42007-02-22 08:56:17 +0000140 }
141
142 return Changed;
143}
144
145