blob: c18ff0adcc5a48fc760961fa175bd95e9cb663cc [file] [log] [blame]
Chris Lattner26e4f892002-01-21 07:37:31 +00001//===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
2//
3// This file implements the LLVM Pass infrastructure. It is primarily
4// responsible with ensuring that passes are executed and batched together
5// optimally.
6//
7//===----------------------------------------------------------------------===//
8
Chris Lattnercdd09c22002-01-31 00:45:31 +00009#include "llvm/PassManager.h"
10#include "llvm/Module.h"
11#include "llvm/Method.h"
Chris Lattner26e4f892002-01-21 07:37:31 +000012#include "Support/STLExtras.h"
Chris Lattnerd013ba92002-01-23 05:49:41 +000013#include <algorithm>
14
Chris Lattnercdd09c22002-01-31 00:45:31 +000015// Source of unique analysis ID #'s.
16unsigned AnalysisID::NextID = 0;
17
18void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
19 assert(P->Resolver == 0 && "Pass already in a PassManager!");
20 P->Resolver = AR;
21}
22
23
Chris Lattnerd013ba92002-01-23 05:49:41 +000024// Pass debugging information. Often it is useful to find out what pass is
25// running when a crash occurs in a utility. When this library is compiled with
26// debugging on, a command line option (--debug-pass) is enabled that causes the
27// pass name to be printed before it executes.
28//
Chris Lattnercdd09c22002-01-31 00:45:31 +000029#ifndef NDEBUG
Chris Lattnerd013ba92002-01-23 05:49:41 +000030#include "Support/CommandLine.h"
31#include <typeinfo>
32#include <iostream>
33
Chris Lattnercdd09c22002-01-31 00:45:31 +000034// Different debug levels that can be enabled...
35enum PassDebugLevel {
36 None, PassStructure, PassExecutions, PassDetails
37};
Chris Lattnerd013ba92002-01-23 05:49:41 +000038
Chris Lattnercdd09c22002-01-31 00:45:31 +000039static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
40 "Print PassManager debugging information",
41 clEnumVal(None , "disable debug output"),
42 clEnumVal(PassStructure , "print pass structure before run()"),
43 clEnumVal(PassExecutions, "print pass name before it is executed"),
44 clEnumVal(PassDetails , "print pass details when it is executed"), 0);
45
46void PMDebug::PrintPassStructure(Pass *P) {
47 if (PassDebugging >= PassStructure)
48 P->dumpPassStructure();
49}
50
51void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
52 Pass *P, Value *V) {
53 if (PassDebugging >= PassExecutions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000054 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattnercdd09c22002-01-31 00:45:31 +000055 << typeid(*P).name();
56 if (V) {
57 std::cerr << "' on ";
58 switch (V->getValueType()) {
59 case Value::ModuleVal:
60 std::cerr << "Module\n"; return;
61 case Value::MethodVal:
62 std::cerr << "Method '" << V->getName(); break;
63 case Value::BasicBlockVal:
64 std::cerr << "BasicBlock '" << V->getName(); break;
65 default:
66 std::cerr << typeid(*V).name() << " '" << V->getName(); break;
67 }
68 }
69 std::cerr << "'...\n";
70 }
71}
72
73void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerac3e0602002-01-31 18:32:27 +000074 Pass *P, const Pass::AnalysisSet &Set) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000075 if (PassDebugging >= PassDetails && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000076 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnercdd09c22002-01-31 00:45:31 +000077 for (unsigned i = 0; i < Set.size(); ++i) {
78 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
79 std::cerr << " " << typeid(*P).name();
80 delete P;
81 }
82 std::cerr << "\n";
83 }
84}
85
86// dumpPassStructure - Implement the -debug-passes=PassStructure option
87void Pass::dumpPassStructure(unsigned Offset = 0) {
88 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +000089}
90#endif
91
92
Chris Lattnercdd09c22002-01-31 00:45:31 +000093//===----------------------------------------------------------------------===//
94// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000095//
Chris Lattnercdd09c22002-01-31 00:45:31 +000096
Chris Lattnerac3e0602002-01-31 18:32:27 +000097void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
98 AnalysisSet &Destroyed, AnalysisSet &Provided) {
99 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000100}
Chris Lattner26e4f892002-01-21 07:37:31 +0000101
Chris Lattnercdd09c22002-01-31 00:45:31 +0000102//===----------------------------------------------------------------------===//
103// MethodPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000104//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000105
106// run - On a module, we run this pass by initializing, ronOnMethod'ing once
107// for every method in the module, then by finalizing.
108//
109bool MethodPass::run(Module *M) {
110 bool Changed = doInitialization(M);
111
112 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
113 if (!(*I)->isExternal()) // Passes are not run on external methods!
114 Changed |= runOnMethod(*I);
115
116 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000117}
118
Chris Lattnercdd09c22002-01-31 00:45:31 +0000119// run - On a method, we simply initialize, run the method, then finalize.
120//
121bool MethodPass::run(Method *M) {
122 if (M->isExternal()) return false; // Passes are not run on external methods!
123
124 return doInitialization(M->getParent()) | runOnMethod(M)
125 | doFinalization(M->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000126}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000127
Chris Lattnercdd09c22002-01-31 00:45:31 +0000128void MethodPass::addToPassManager(PassManagerT<Module> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000129 AnalysisSet &Required, AnalysisSet &Destroyed,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000131 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000132}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000133
134void MethodPass::addToPassManager(PassManagerT<Method> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000135 AnalysisSet &Required, AnalysisSet &Destroyed,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000136 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000137 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000138}
139
140//===----------------------------------------------------------------------===//
141// BasicBlockPass Implementation
142//
143
144// To run this pass on a method, we simply call runOnBasicBlock once for each
145// method.
146//
147bool BasicBlockPass::runOnMethod(Method *M) {
148 bool Changed = false;
149 for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
150 Changed |= runOnBasicBlock(*I);
151 return Changed;
152}
153
154// To run directly on the basic block, we initialize, runOnBasicBlock, then
155// finalize.
156//
157bool BasicBlockPass::run(BasicBlock *BB) {
158 Module *M = BB->getParent()->getParent();
159 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
160}
161
162void BasicBlockPass::addToPassManager(PassManagerT<Method> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000163 AnalysisSet &Required,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000164 AnalysisSet &Destroyed,
165 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000166 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000167}
168
169void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000170 AnalysisSet &Required,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000171 AnalysisSet &Destroyed,
172 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000173 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000174}
175