blob: fe1672a403706453ef02d65bd34f2168298d6986 [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"
17using namespace llvm;
18
19//===----------------------------------------------------------------------===//
20// LPPassManager
21//
22/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
23
Devang Pateld0e6e332007-02-22 23:30:07 +000024LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) {
Devang Patel8ded5852007-02-23 00:16:44 +000025 skipThisLoop = false;
26 redoThisLoop = false;
Devang Pateld0e6e332007-02-22 23:30:07 +000027}
28
Devang Patel5afdc7d2007-02-23 00:10:16 +000029/// Delete loop from the loop queue. This is used by Loop pass to inform
30/// Loop Pass Manager that it should skip rest of the passes for this loop.
31void LPPassManager::deleteLoopFromQueue(Loop *L) {
32 // Do not pop loop from LQ here. It will be done by runOnFunction while loop.
33 skipThisLoop = true;
34}
35
Devang Patel8ded5852007-02-23 00:16:44 +000036// Reoptimize this loop. LPPassManager will re-insert this loop into the
37// queue. This allows LoopPass to change loop nest for the loop. This
38// utility may send LPPassManager into infinite loops so use caution.
39void LPPassManager::redoLoop(Loop *L) {
40 redoThisLoop = true;
41}
42
Devang Patel643a79b2007-02-22 23:45:15 +000043// Recurse through all subloops and all loops into LQ.
Devang Patel30159722007-03-06 02:30:46 +000044static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
Devang Patel643a79b2007-02-22 23:45:15 +000045 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
46 addLoopIntoQueue(*I, LQ);
Devang Patel30159722007-03-06 02:30:46 +000047 LQ.push_back(L);
Devang Patel643a79b2007-02-22 23:45:15 +000048}
49
Devang Patel16a31c42007-02-22 08:56:17 +000050/// run - Execute all of the passes scheduled for execution. Keep track of
51/// whether any of the passes modifies the function, and if so, return true.
52bool LPPassManager::runOnFunction(Function &F) {
53 LoopInfo &LI = getAnalysis<LoopInfo>();
54 bool Changed = false;
55
Devang Patel643a79b2007-02-22 23:45:15 +000056 // Populate Loop Queue
57 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
58 addLoopIntoQueue(*I, LQ);
59
Devang Patel16a31c42007-02-22 08:56:17 +000060 // Walk Loops
Devang Patel30159722007-03-06 02:30:46 +000061 while (!LQ.empty()) {
Devang Patel643a79b2007-02-22 23:45:15 +000062
Devang Patel30159722007-03-06 02:30:46 +000063 Loop *L = LQ.back();
Devang Patel5afdc7d2007-02-23 00:10:16 +000064 skipThisLoop = false;
Devang Patel8ded5852007-02-23 00:16:44 +000065 redoThisLoop = false;
Devang Patel5afdc7d2007-02-23 00:10:16 +000066
Devang Patel16a31c42007-02-22 08:56:17 +000067 // Run all passes on current SCC
68 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel643a79b2007-02-22 23:45:15 +000069
Devang Patel16a31c42007-02-22 08:56:17 +000070 Pass *P = getContainedPass(Index);
71 AnalysisUsage AnUsage;
72 P->getAnalysisUsage(AnUsage);
73
Devang Patel7f997612007-03-05 20:01:30 +000074 dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
Devang Patel16a31c42007-02-22 08:56:17 +000075 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
76
77 initializeAnalysisImpl(P);
78
79 StartPassTimer(P);
80 LoopPass *LP = dynamic_cast<LoopPass *>(P);
81 assert (LP && "Invalid LPPassManager member");
Devang Patel4b264632007-02-23 17:53:17 +000082 LP->runOnLoop(L, *this);
Devang Patel16a31c42007-02-22 08:56:17 +000083 StopPassTimer(P);
84
85 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +000086 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
Devang Patel16a31c42007-02-22 08:56:17 +000087 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
88
89 removeNotPreservedAnalysis(P);
90 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +000091 removeDeadPasses(P, "", ON_LOOP_MSG);
Devang Patel5afdc7d2007-02-23 00:10:16 +000092
93 if (skipThisLoop)
94 // Do not run other passes on this loop.
95 break;
Devang Patel16a31c42007-02-22 08:56:17 +000096 }
Devang Patel643a79b2007-02-22 23:45:15 +000097
98 // Pop the loop from queue after running all passes.
Devang Patel30159722007-03-06 02:30:46 +000099 LQ.pop_back();
Devang Patel8ded5852007-02-23 00:16:44 +0000100
101 if (redoThisLoop)
Devang Patel30159722007-03-06 02:30:46 +0000102 LQ.push_back(L);
Devang Patel16a31c42007-02-22 08:56:17 +0000103 }
104
105 return Changed;
106}
107
108
Devang Patelbfd59052007-02-23 00:36:57 +0000109//===----------------------------------------------------------------------===//
110// LoopPass
111
112/// Assign pass manager to manage this pass.
113void LoopPass::assignPassManager(PMStack &PMS,
114 PassManagerType PreferredType) {
115 // Find LPPassManager
116 while (!PMS.empty()) {
117 if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
118 PMS.pop();
119 else;
120 break;
121 }
122
123 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
124
125 // Create new Loop Pass Manager if it does not exist.
126 if (!LPPM) {
127
128 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
129 PMDataManager *PMD = PMS.top();
130
131 // [1] Create new Call Graph Pass Manager
132 LPPM = new LPPassManager(PMD->getDepth() + 1);
133
134 // [2] Set up new manager's top level manager
135 PMTopLevelManager *TPM = PMD->getTopLevelManager();
136 TPM->addIndirectPassManager(LPPM);
137
138 // [3] Assign manager to manage this new manager. This may create
139 // and push new managers into PMS
140 Pass *P = dynamic_cast<Pass *>(LPPM);
141 P->assignPassManager(PMS);
142
143 // [4] Push new manager into PMS
144 PMS.push(LPPM);
145 }
146
147 LPPM->add(this);
148}
149