blob: 14ebd50dd16a8e875e1cac2c5787c7e96b719b0d [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 Lattnerc8e66542002-04-27 06:56:12 +000074 Pass *P, const std::vector<AnalysisID> &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 Lattnerc8e66542002-04-27 06:56:12 +000077 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000078 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 Lattnerc8e66542002-04-27 06:56:12 +000096void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
97 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +000098}
Chris Lattner26e4f892002-01-21 07:37:31 +000099
Chris Lattnercdd09c22002-01-31 00:45:31 +0000100//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000101// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000102//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000103
Chris Lattnerc8e66542002-04-27 06:56:12 +0000104// run - On a module, we run this pass by initializing, runOnFunction'ing once
105// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000106//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000107bool FunctionPass::run(Module *M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000108 bool Changed = doInitialization(M);
109
110 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc8e66542002-04-27 06:56:12 +0000111 if (!(*I)->isExternal()) // Passes are not run on external functions!
112 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000113
114 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000115}
116
Chris Lattnerc8e66542002-04-27 06:56:12 +0000117// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000118//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000119bool FunctionPass::run(Function *F) {
120 if (F->isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000121
Chris Lattnerc8e66542002-04-27 06:56:12 +0000122 return doInitialization(F->getParent()) | runOnFunction(F)
Chris Lattner57698e22002-03-26 18:01:55 +0000123 | doFinalization(F->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000124}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000125
Chris Lattnerc8e66542002-04-27 06:56:12 +0000126void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
127 AnalysisUsage &AU) {
128 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000129}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130
Chris Lattnerc8e66542002-04-27 06:56:12 +0000131void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
132 AnalysisUsage &AU) {
133 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000134}
135
136//===----------------------------------------------------------------------===//
137// BasicBlockPass Implementation
138//
139
Chris Lattnerc8e66542002-04-27 06:56:12 +0000140// To run this pass on a function, we simply call runOnBasicBlock once for each
141// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000142//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000143bool BasicBlockPass::runOnFunction(Function *F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000144 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +0000145 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000146 Changed |= runOnBasicBlock(*I);
147 return Changed;
148}
149
150// To run directly on the basic block, we initialize, runOnBasicBlock, then
151// finalize.
152//
153bool BasicBlockPass::run(BasicBlock *BB) {
154 Module *M = BB->getParent()->getParent();
155 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
156}
157
Chris Lattner57698e22002-03-26 18:01:55 +0000158void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000159 AnalysisUsage &AU) {
160 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000161}
162
163void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000164 AnalysisUsage &AU) {
165 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000166}
167