blob: 9596f5234e26594ed68d870b3383b4b6fbb9b2c4 [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 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;
Chris Lattner57698e22002-03-26 18:01:55 +000061 case Value::FunctionVal:
62 std::cerr << "Function '" << V->getName(); break;
Chris Lattnercdd09c22002-01-31 00:45:31 +000063 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}
Chris Lattnerd013ba92002-01-23 05:49:41 +000090
91
Chris Lattnercdd09c22002-01-31 00:45:31 +000092//===----------------------------------------------------------------------===//
93// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000094//
Chris Lattnercdd09c22002-01-31 00:45:31 +000095
Chris Lattnerac3e0602002-01-31 18:32:27 +000096void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
97 AnalysisSet &Destroyed, AnalysisSet &Provided) {
98 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattner654b5bc2002-01-22 00:17:48 +000099}
Chris Lattner26e4f892002-01-21 07:37:31 +0000100
Chris Lattnercdd09c22002-01-31 00:45:31 +0000101//===----------------------------------------------------------------------===//
102// MethodPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000103//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000104
105// run - On a module, we run this pass by initializing, ronOnMethod'ing once
106// for every method in the module, then by finalizing.
107//
108bool MethodPass::run(Module *M) {
109 bool Changed = doInitialization(M);
110
111 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
112 if (!(*I)->isExternal()) // Passes are not run on external methods!
113 Changed |= runOnMethod(*I);
114
115 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000116}
117
Chris Lattnercdd09c22002-01-31 00:45:31 +0000118// run - On a method, we simply initialize, run the method, then finalize.
119//
Chris Lattner57698e22002-03-26 18:01:55 +0000120bool MethodPass::run(Function *F) {
121 if (F->isExternal()) return false; // Passes are not run on external methods!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000122
Chris Lattner57698e22002-03-26 18:01:55 +0000123 return doInitialization(F->getParent()) | runOnMethod(F)
124 | doFinalization(F->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000125}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000126
Chris Lattnercdd09c22002-01-31 00:45:31 +0000127void MethodPass::addToPassManager(PassManagerT<Module> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000128 AnalysisSet &Required, AnalysisSet &Destroyed,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000129 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000130 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000131}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000132
Chris Lattner57698e22002-03-26 18:01:55 +0000133void MethodPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000134 AnalysisSet &Required, AnalysisSet &Destroyed,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000135 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000136 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000137}
138
139//===----------------------------------------------------------------------===//
140// BasicBlockPass Implementation
141//
142
143// To run this pass on a method, we simply call runOnBasicBlock once for each
144// method.
145//
Chris Lattner57698e22002-03-26 18:01:55 +0000146bool BasicBlockPass::runOnMethod(Function *F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000147 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +0000148 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000149 Changed |= runOnBasicBlock(*I);
150 return Changed;
151}
152
153// To run directly on the basic block, we initialize, runOnBasicBlock, then
154// finalize.
155//
156bool BasicBlockPass::run(BasicBlock *BB) {
157 Module *M = BB->getParent()->getParent();
158 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
159}
160
Chris Lattner57698e22002-03-26 18:01:55 +0000161void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000162 AnalysisSet &Required,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000163 AnalysisSet &Destroyed,
164 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000165 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000166}
167
168void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerac3e0602002-01-31 18:32:27 +0000169 AnalysisSet &Required,
Chris Lattnercdd09c22002-01-31 00:45:31 +0000170 AnalysisSet &Destroyed,
171 AnalysisSet &Provided) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000172 PM->addPass(this, Required, Destroyed, Provided);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000173}
174