blob: b607fe4b804c8f99e0e3eb0a134933d8c88317f6 [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"
Chris Lattner57698e22002-03-26 18:01:55 +000011#include "llvm/Function.h"
Chris Lattner60a65912002-02-12 21:07:25 +000012#include "llvm/BasicBlock.h"
Chris Lattner26e4f892002-01-21 07:37:31 +000013#include "Support/STLExtras.h"
Chris Lattnerd013ba92002-01-23 05:49:41 +000014#include <algorithm>
15
Chris Lattnercdd09c22002-01-31 00:45:31 +000016// Source of unique analysis ID #'s.
17unsigned AnalysisID::NextID = 0;
18
19void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
20 assert(P->Resolver == 0 && "Pass already in a PassManager!");
21 P->Resolver = AR;
22}
23
24
Chris Lattnerd013ba92002-01-23 05:49:41 +000025// Pass debugging information. Often it is useful to find out what pass is
26// running when a crash occurs in a utility. When this library is compiled with
27// debugging on, a command line option (--debug-pass) is enabled that causes the
28// pass name to be printed before it executes.
29//
Chris Lattnercdd09c22002-01-31 00:45:31 +000030#ifndef NDEBUG
Chris Lattnerd013ba92002-01-23 05:49:41 +000031#include "Support/CommandLine.h"
32#include <typeinfo>
33#include <iostream>
34
Chris Lattnercdd09c22002-01-31 00:45:31 +000035// Different debug levels that can be enabled...
36enum PassDebugLevel {
37 None, PassStructure, PassExecutions, PassDetails
38};
Chris Lattnerd013ba92002-01-23 05:49:41 +000039
Chris Lattnercdd09c22002-01-31 00:45:31 +000040static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
41 "Print PassManager debugging information",
42 clEnumVal(None , "disable debug output"),
43 clEnumVal(PassStructure , "print pass structure before run()"),
44 clEnumVal(PassExecutions, "print pass name before it is executed"),
45 clEnumVal(PassDetails , "print pass details when it is executed"), 0);
46
47void PMDebug::PrintPassStructure(Pass *P) {
48 if (PassDebugging >= PassStructure)
49 P->dumpPassStructure();
50}
51
52void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
53 Pass *P, Value *V) {
54 if (PassDebugging >= PassExecutions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000055 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattnercdd09c22002-01-31 00:45:31 +000056 << typeid(*P).name();
57 if (V) {
58 std::cerr << "' on ";
59 switch (V->getValueType()) {
60 case Value::ModuleVal:
61 std::cerr << "Module\n"; return;
Chris Lattner57698e22002-03-26 18:01:55 +000062 case Value::FunctionVal:
63 std::cerr << "Function '" << V->getName(); break;
Chris Lattnercdd09c22002-01-31 00:45:31 +000064 case Value::BasicBlockVal:
65 std::cerr << "BasicBlock '" << V->getName(); break;
66 default:
67 std::cerr << typeid(*V).name() << " '" << V->getName(); break;
68 }
69 }
70 std::cerr << "'...\n";
71 }
72}
73
74void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerac3e0602002-01-31 18:32:27 +000075 Pass *P, const Pass::AnalysisSet &Set) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000076 if (PassDebugging >= PassDetails && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000077 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnercdd09c22002-01-31 00:45:31 +000078 for (unsigned i = 0; i < Set.size(); ++i) {
79 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
80 std::cerr << " " << typeid(*P).name();
81 delete P;
82 }
83 std::cerr << "\n";
84 }
85}
86
87// dumpPassStructure - Implement the -debug-passes=PassStructure option
88void Pass::dumpPassStructure(unsigned Offset = 0) {
89 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +000090}
91#endif
92
93
Chris Lattnercdd09c22002-01-31 00:45:31 +000094//===----------------------------------------------------------------------===//
95// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000096//
Chris Lattnercdd09c22002-01-31 00:45:31 +000097
Chris Lattnerac3e0602002-01-31 18:32:27 +000098void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
99 AnalysisSet &Destroyed, AnalysisSet &Provided) {
100 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000101}
Chris Lattner26e4f892002-01-21 07:37:31 +0000102
Chris Lattnercdd09c22002-01-31 00:45:31 +0000103//===----------------------------------------------------------------------===//
104// MethodPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000105//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000106
107// run - On a module, we run this pass by initializing, ronOnMethod'ing once
108// for every method in the module, then by finalizing.
109//
110bool MethodPass::run(Module *M) {
111 bool Changed = doInitialization(M);
112
113 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
114 if (!(*I)->isExternal()) // Passes are not run on external methods!
115 Changed |= runOnMethod(*I);
116
117 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000118}
119
Chris Lattnercdd09c22002-01-31 00:45:31 +0000120// run - On a method, we simply initialize, run the method, then finalize.
121//
Chris Lattner57698e22002-03-26 18:01:55 +0000122bool MethodPass::run(Function *F) {
123 if (F->isExternal()) return false; // Passes are not run on external methods!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000124
Chris Lattner57698e22002-03-26 18:01:55 +0000125 return doInitialization(F->getParent()) | runOnMethod(F)
126 | doFinalization(F->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000127}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000128
Chris Lattnercdd09c22002-01-31 00:45:31 +0000129void MethodPass::addToPassManager(PassManagerT<Module> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000130 AnalysisSet &Required, AnalysisSet &Destroyed,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000131 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000132 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000133}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000134
Chris Lattner57698e22002-03-26 18:01:55 +0000135void MethodPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000136 AnalysisSet &Required, AnalysisSet &Destroyed,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000137 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000138 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000139}
140
141//===----------------------------------------------------------------------===//
142// BasicBlockPass Implementation
143//
144
145// To run this pass on a method, we simply call runOnBasicBlock once for each
146// method.
147//
Chris Lattner57698e22002-03-26 18:01:55 +0000148bool BasicBlockPass::runOnMethod(Function *F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000149 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +0000150 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000151 Changed |= runOnBasicBlock(*I);
152 return Changed;
153}
154
155// To run directly on the basic block, we initialize, runOnBasicBlock, then
156// finalize.
157//
158bool BasicBlockPass::run(BasicBlock *BB) {
159 Module *M = BB->getParent()->getParent();
160 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
161}
162
Chris Lattner57698e22002-03-26 18:01:55 +0000163void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000164 AnalysisSet &Required,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000165 AnalysisSet &Destroyed,
166 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000167 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000168}
169
170void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000171 AnalysisSet &Required,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000172 AnalysisSet &Destroyed,
173 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000174 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000175}
176