blob: c3c9d456b5e387b1d5d6d4c1fac9985441fffe9e [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"
Chris Lattner37c86672002-04-28 20:46:05 +000010#include "PassManagerT.h" // PassManagerT implementation
Chris Lattnercdd09c22002-01-31 00:45:31 +000011#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000012#include "llvm/Function.h"
Chris Lattner60a65912002-02-12 21:07:25 +000013#include "llvm/BasicBlock.h"
Chris Lattner26e4f892002-01-21 07:37:31 +000014#include "Support/STLExtras.h"
Chris Lattner37c86672002-04-28 20:46:05 +000015#include "Support/CommandLine.h"
16#include <typeinfo>
17#include <iostream>
Chris Lattnerd013ba92002-01-23 05:49:41 +000018
Chris Lattnercdd09c22002-01-31 00:45:31 +000019// Source of unique analysis ID #'s.
20unsigned AnalysisID::NextID = 0;
21
22void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
23 assert(P->Resolver == 0 && "Pass already in a PassManager!");
24 P->Resolver = AR;
25}
26
Chris Lattneree2ff5d2002-04-28 21:25:41 +000027
28// preservesCFG - This function should be called to by the pass, iff they do
29// not:
30//
31// 1. Add or remove basic blocks from the function
32// 2. Modify terminator instructions in any way.
33//
34// This function annotates the AnalysisUsage info object to say that analyses
35// that only depend on the CFG are preserved by this pass.
36//
37void AnalysisUsage::preservesCFG() {
38 // FIXME: implement preservesCFG
39}
40
41
Chris Lattner37c86672002-04-28 20:46:05 +000042//===----------------------------------------------------------------------===//
43// PassManager implementation - The PassManager class is a simple Pimpl class
44// that wraps the PassManagerT template.
45//
46PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
47PassManager::~PassManager() { delete PM; }
48void PassManager::add(Pass *P) { PM->add(P); }
49bool PassManager::run(Module *M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000050
Chris Lattner37c86672002-04-28 20:46:05 +000051
52//===----------------------------------------------------------------------===//
Chris Lattnerd013ba92002-01-23 05:49:41 +000053// Pass debugging information. Often it is useful to find out what pass is
54// running when a crash occurs in a utility. When this library is compiled with
55// debugging on, a command line option (--debug-pass) is enabled that causes the
56// pass name to be printed before it executes.
57//
Chris Lattnerd013ba92002-01-23 05:49:41 +000058
Chris Lattnercdd09c22002-01-31 00:45:31 +000059// Different debug levels that can be enabled...
60enum PassDebugLevel {
61 None, PassStructure, PassExecutions, PassDetails
62};
Chris Lattnerd013ba92002-01-23 05:49:41 +000063
Chris Lattnercdd09c22002-01-31 00:45:31 +000064static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
65 "Print PassManager debugging information",
66 clEnumVal(None , "disable debug output"),
67 clEnumVal(PassStructure , "print pass structure before run()"),
68 clEnumVal(PassExecutions, "print pass name before it is executed"),
69 clEnumVal(PassDetails , "print pass details when it is executed"), 0);
70
71void PMDebug::PrintPassStructure(Pass *P) {
72 if (PassDebugging >= PassStructure)
73 P->dumpPassStructure();
74}
75
76void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +000077 Pass *P, Annotable *V) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000078 if (PassDebugging >= PassExecutions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000079 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattnercdd09c22002-01-31 00:45:31 +000080 << typeid(*P).name();
81 if (V) {
82 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +000083
84 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000085 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +000086 } else if (Function *F = dynamic_cast<Function*>(V))
87 std::cerr << "Function '" << F->getName();
88 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
89 std::cerr << "BasicBlock '" << BB->getName();
90 else if (Value *Val = dynamic_cast<Value*>(V))
91 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +000092 }
93 std::cerr << "'...\n";
94 }
95}
96
97void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +000098 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnercdd09c22002-01-31 00:45:31 +000099 if (PassDebugging >= PassDetails && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000100 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerc8e66542002-04-27 06:56:12 +0000101 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000102 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
103 std::cerr << " " << typeid(*P).name();
104 delete P;
105 }
106 std::cerr << "\n";
107 }
108}
109
110// dumpPassStructure - Implement the -debug-passes=PassStructure option
111void Pass::dumpPassStructure(unsigned Offset = 0) {
112 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +0000113}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000114
115
Chris Lattnercdd09c22002-01-31 00:45:31 +0000116//===----------------------------------------------------------------------===//
117// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000118//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000119
Chris Lattnerc8e66542002-04-27 06:56:12 +0000120void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
121 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000122}
Chris Lattner26e4f892002-01-21 07:37:31 +0000123
Chris Lattnercdd09c22002-01-31 00:45:31 +0000124//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000125// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000126//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000127
Chris Lattnerc8e66542002-04-27 06:56:12 +0000128// run - On a module, we run this pass by initializing, runOnFunction'ing once
129// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000131bool FunctionPass::run(Module *M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000132 bool Changed = doInitialization(M);
133
134 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc8e66542002-04-27 06:56:12 +0000135 if (!(*I)->isExternal()) // Passes are not run on external functions!
136 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000137
138 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000139}
140
Chris Lattnerc8e66542002-04-27 06:56:12 +0000141// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000142//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000143bool FunctionPass::run(Function *F) {
144 if (F->isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000145
Chris Lattnerc8e66542002-04-27 06:56:12 +0000146 return doInitialization(F->getParent()) | runOnFunction(F)
Chris Lattner57698e22002-03-26 18:01:55 +0000147 | doFinalization(F->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000148}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000149
Chris Lattnerc8e66542002-04-27 06:56:12 +0000150void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
151 AnalysisUsage &AU) {
152 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000153}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000154
Chris Lattnerc8e66542002-04-27 06:56:12 +0000155void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
156 AnalysisUsage &AU) {
157 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000158}
159
160//===----------------------------------------------------------------------===//
161// BasicBlockPass Implementation
162//
163
Chris Lattnerc8e66542002-04-27 06:56:12 +0000164// To run this pass on a function, we simply call runOnBasicBlock once for each
165// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000166//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000167bool BasicBlockPass::runOnFunction(Function *F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000168 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +0000169 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000170 Changed |= runOnBasicBlock(*I);
171 return Changed;
172}
173
174// To run directly on the basic block, we initialize, runOnBasicBlock, then
175// finalize.
176//
177bool BasicBlockPass::run(BasicBlock *BB) {
178 Module *M = BB->getParent()->getParent();
179 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
180}
181
Chris Lattner57698e22002-03-26 18:01:55 +0000182void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000183 AnalysisUsage &AU) {
184 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000185}
186
187void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000188 AnalysisUsage &AU) {
189 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000190}
191