blob: ff9ac928b9698283b64b8c65d2f93a8aa3d2cf19 [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 Lattner37c86672002-04-28 20:46:05 +000027//===----------------------------------------------------------------------===//
28// PassManager implementation - The PassManager class is a simple Pimpl class
29// that wraps the PassManagerT template.
30//
31PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
32PassManager::~PassManager() { delete PM; }
33void PassManager::add(Pass *P) { PM->add(P); }
34bool PassManager::run(Module *M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000035
Chris Lattner37c86672002-04-28 20:46:05 +000036
37//===----------------------------------------------------------------------===//
Chris Lattnerd013ba92002-01-23 05:49:41 +000038// Pass debugging information. Often it is useful to find out what pass is
39// running when a crash occurs in a utility. When this library is compiled with
40// debugging on, a command line option (--debug-pass) is enabled that causes the
41// pass name to be printed before it executes.
42//
Chris Lattnerd013ba92002-01-23 05:49:41 +000043
Chris Lattnercdd09c22002-01-31 00:45:31 +000044// Different debug levels that can be enabled...
45enum PassDebugLevel {
46 None, PassStructure, PassExecutions, PassDetails
47};
Chris Lattnerd013ba92002-01-23 05:49:41 +000048
Chris Lattnercdd09c22002-01-31 00:45:31 +000049static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
50 "Print PassManager debugging information",
51 clEnumVal(None , "disable debug output"),
52 clEnumVal(PassStructure , "print pass structure before run()"),
53 clEnumVal(PassExecutions, "print pass name before it is executed"),
54 clEnumVal(PassDetails , "print pass details when it is executed"), 0);
55
56void PMDebug::PrintPassStructure(Pass *P) {
57 if (PassDebugging >= PassStructure)
58 P->dumpPassStructure();
59}
60
61void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +000062 Pass *P, Annotable *V) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000063 if (PassDebugging >= PassExecutions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000064 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattnercdd09c22002-01-31 00:45:31 +000065 << typeid(*P).name();
66 if (V) {
67 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +000068
69 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000070 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +000071 } else if (Function *F = dynamic_cast<Function*>(V))
72 std::cerr << "Function '" << F->getName();
73 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
74 std::cerr << "BasicBlock '" << BB->getName();
75 else if (Value *Val = dynamic_cast<Value*>(V))
76 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +000077 }
78 std::cerr << "'...\n";
79 }
80}
81
82void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +000083 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnercdd09c22002-01-31 00:45:31 +000084 if (PassDebugging >= PassDetails && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +000085 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerc8e66542002-04-27 06:56:12 +000086 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000087 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
88 std::cerr << " " << typeid(*P).name();
89 delete P;
90 }
91 std::cerr << "\n";
92 }
93}
94
95// dumpPassStructure - Implement the -debug-passes=PassStructure option
96void Pass::dumpPassStructure(unsigned Offset = 0) {
97 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +000098}
Chris Lattnerd013ba92002-01-23 05:49:41 +000099
100
Chris Lattnercdd09c22002-01-31 00:45:31 +0000101//===----------------------------------------------------------------------===//
102// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000103//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000104
Chris Lattnerc8e66542002-04-27 06:56:12 +0000105void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
106 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000107}
Chris Lattner26e4f892002-01-21 07:37:31 +0000108
Chris Lattnercdd09c22002-01-31 00:45:31 +0000109//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000110// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000111//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000112
Chris Lattnerc8e66542002-04-27 06:56:12 +0000113// run - On a module, we run this pass by initializing, runOnFunction'ing once
114// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000115//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000116bool FunctionPass::run(Module *M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000117 bool Changed = doInitialization(M);
118
119 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc8e66542002-04-27 06:56:12 +0000120 if (!(*I)->isExternal()) // Passes are not run on external functions!
121 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000122
123 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000124}
125
Chris Lattnerc8e66542002-04-27 06:56:12 +0000126// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000127//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000128bool FunctionPass::run(Function *F) {
129 if (F->isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130
Chris Lattnerc8e66542002-04-27 06:56:12 +0000131 return doInitialization(F->getParent()) | runOnFunction(F)
Chris Lattner57698e22002-03-26 18:01:55 +0000132 | doFinalization(F->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000133}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000134
Chris Lattnerc8e66542002-04-27 06:56:12 +0000135void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
136 AnalysisUsage &AU) {
137 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000138}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000139
Chris Lattnerc8e66542002-04-27 06:56:12 +0000140void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
141 AnalysisUsage &AU) {
142 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000143}
144
Chris Lattner37c86672002-04-28 20:46:05 +0000145// doesNotModifyCFG - This function should be called by our subclasses to
146// implement the getAnalysisUsage virtual function, iff they do not:
147//
148// 1. Add or remove basic blocks from the function
149// 2. Modify terminator instructions in any way.
150//
151// This function annotates the AnalysisUsage info object to say that analyses
152// that only depend on the CFG are preserved by this pass.
153//
154void FunctionPass::doesNotModifyCFG(AnalysisUsage &Info) {
155
156}
157
158
Chris Lattnercdd09c22002-01-31 00:45:31 +0000159//===----------------------------------------------------------------------===//
160// BasicBlockPass Implementation
161//
162
Chris Lattnerc8e66542002-04-27 06:56:12 +0000163// To run this pass on a function, we simply call runOnBasicBlock once for each
164// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000165//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000166bool BasicBlockPass::runOnFunction(Function *F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000167 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +0000168 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000169 Changed |= runOnBasicBlock(*I);
170 return Changed;
171}
172
173// To run directly on the basic block, we initialize, runOnBasicBlock, then
174// finalize.
175//
176bool BasicBlockPass::run(BasicBlock *BB) {
177 Module *M = BB->getParent()->getParent();
178 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
179}
180
Chris Lattner57698e22002-03-26 18:01:55 +0000181void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000182 AnalysisUsage &AU) {
183 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000184}
185
186void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000187 AnalysisUsage &AU) {
188 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000189}
190