2002-01-21 07:37:31 +00:00
|
|
|
//===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVM Pass infrastructure. It is primarily
|
|
|
|
// responsible with ensuring that passes are executed and batched together
|
|
|
|
// optimally.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Method.h"
|
2002-01-21 07:37:31 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2002-01-23 05:49:41 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// Source of unique analysis ID #'s.
|
|
|
|
unsigned AnalysisID::NextID = 0;
|
|
|
|
|
|
|
|
void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
|
|
|
|
assert(P->Resolver == 0 && "Pass already in a PassManager!");
|
|
|
|
P->Resolver = AR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-23 05:49:41 +00:00
|
|
|
// Pass debugging information. Often it is useful to find out what pass is
|
|
|
|
// running when a crash occurs in a utility. When this library is compiled with
|
|
|
|
// debugging on, a command line option (--debug-pass) is enabled that causes the
|
|
|
|
// pass name to be printed before it executes.
|
|
|
|
//
|
2002-01-31 00:45:31 +00:00
|
|
|
#ifndef NDEBUG
|
2002-01-23 05:49:41 +00:00
|
|
|
#include "Support/CommandLine.h"
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <iostream>
|
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// Different debug levels that can be enabled...
|
|
|
|
enum PassDebugLevel {
|
|
|
|
None, PassStructure, PassExecutions, PassDetails
|
|
|
|
};
|
2002-01-23 05:49:41 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
|
|
|
|
"Print PassManager debugging information",
|
|
|
|
clEnumVal(None , "disable debug output"),
|
|
|
|
clEnumVal(PassStructure , "print pass structure before run()"),
|
|
|
|
clEnumVal(PassExecutions, "print pass name before it is executed"),
|
|
|
|
clEnumVal(PassDetails , "print pass details when it is executed"), 0);
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void PMDebug::PrintPassStructure(Pass *P) {
|
|
|
|
if (PassDebugging >= PassStructure)
|
|
|
|
P->dumpPassStructure();
|
2002-01-21 07:37:31 +00:00
|
|
|
}
|
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
|
|
|
|
Pass *P, Value *V) {
|
|
|
|
if (PassDebugging >= PassExecutions) {
|
2002-01-31 18:32:27 +00:00
|
|
|
std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
|
2002-01-31 00:45:31 +00:00
|
|
|
<< typeid(*P).name();
|
|
|
|
if (V) {
|
|
|
|
std::cerr << "' on ";
|
|
|
|
switch (V->getValueType()) {
|
|
|
|
case Value::ModuleVal:
|
|
|
|
std::cerr << "Module\n"; return;
|
|
|
|
case Value::MethodVal:
|
|
|
|
std::cerr << "Method '" << V->getName(); break;
|
|
|
|
case Value::BasicBlockVal:
|
|
|
|
std::cerr << "BasicBlock '" << V->getName(); break;
|
|
|
|
default:
|
|
|
|
std::cerr << typeid(*V).name() << " '" << V->getName(); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cerr << "'...\n";
|
2002-01-21 07:37:31 +00:00
|
|
|
}
|
2002-01-31 00:45:31 +00:00
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
|
2002-01-31 18:32:27 +00:00
|
|
|
Pass *P, const Pass::AnalysisSet &Set) {
|
2002-01-31 00:45:31 +00:00
|
|
|
if (PassDebugging >= PassDetails && !Set.empty()) {
|
2002-01-31 18:32:27 +00:00
|
|
|
std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
|
2002-01-31 00:45:31 +00:00
|
|
|
for (unsigned i = 0; i < Set.size(); ++i) {
|
|
|
|
Pass *P = Set[i].createPass(); // Good thing this is just debug code...
|
|
|
|
std::cerr << " " << typeid(*P).name();
|
|
|
|
delete P;
|
2002-01-23 05:49:41 +00:00
|
|
|
}
|
2002-01-31 00:45:31 +00:00
|
|
|
std::cerr << "\n";
|
2002-01-21 07:37:31 +00:00
|
|
|
}
|
2002-01-31 00:45:31 +00:00
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// dumpPassStructure - Implement the -debug-passes=PassStructure option
|
|
|
|
void Pass::dumpPassStructure(unsigned Offset = 0) {
|
|
|
|
std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
|
|
|
|
}
|
|
|
|
#endif
|
2002-01-21 07:37:31 +00:00
|
|
|
|
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Implementation
|
|
|
|
//
|
2002-01-23 05:49:41 +00:00
|
|
|
|
2002-01-31 18:32:27 +00:00
|
|
|
void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
|
|
|
|
AnalysisSet &Destroyed, AnalysisSet &Provided) {
|
|
|
|
PM->addPass(this, Required, Destroyed, Provided);
|
2002-01-31 00:45:31 +00:00
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MethodPass Implementation
|
|
|
|
//
|
2002-01-22 00:17:48 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// run - On a module, we run this pass by initializing, ronOnMethod'ing once
|
|
|
|
// for every method in the module, then by finalizing.
|
|
|
|
//
|
|
|
|
bool MethodPass::run(Module *M) {
|
|
|
|
bool Changed = doInitialization(M);
|
|
|
|
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!(*I)->isExternal()) // Passes are not run on external methods!
|
|
|
|
Changed |= runOnMethod(*I);
|
|
|
|
|
|
|
|
return Changed | doFinalization(M);
|
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// run - On a method, we simply initialize, run the method, then finalize.
|
|
|
|
//
|
|
|
|
bool MethodPass::run(Method *M) {
|
|
|
|
if (M->isExternal()) return false; // Passes are not run on external methods!
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
return doInitialization(M->getParent()) | runOnMethod(M)
|
|
|
|
| doFinalization(M->getParent());
|
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void MethodPass::addToPassManager(PassManagerT<Module> *PM,
|
2002-01-31 18:32:27 +00:00
|
|
|
AnalysisSet &Required, AnalysisSet &Destroyed,
|
2002-01-31 00:45:31 +00:00
|
|
|
AnalysisSet &Provided) {
|
2002-01-31 18:32:27 +00:00
|
|
|
PM->addPass(this, Required, Destroyed, Provided);
|
2002-01-31 00:45:31 +00:00
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void MethodPass::addToPassManager(PassManagerT<Method> *PM,
|
2002-01-31 18:32:27 +00:00
|
|
|
AnalysisSet &Required, AnalysisSet &Destroyed,
|
2002-01-31 00:45:31 +00:00
|
|
|
AnalysisSet &Provided) {
|
2002-01-31 18:32:27 +00:00
|
|
|
PM->addPass(this, Required, Destroyed, Provided);
|
2002-01-22 00:17:48 +00:00
|
|
|
}
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BasicBlockPass Implementation
|
|
|
|
//
|
2002-01-21 07:37:31 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// To run this pass on a method, we simply call runOnBasicBlock once for each
|
|
|
|
// method.
|
2002-01-21 07:37:31 +00:00
|
|
|
//
|
2002-01-31 00:45:31 +00:00
|
|
|
bool BasicBlockPass::runOnMethod(Method *M) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
Changed |= runOnBasicBlock(*I);
|
|
|
|
return Changed;
|
2002-01-21 07:37:31 +00:00
|
|
|
}
|
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
// To run directly on the basic block, we initialize, runOnBasicBlock, then
|
|
|
|
// finalize.
|
|
|
|
//
|
|
|
|
bool BasicBlockPass::run(BasicBlock *BB) {
|
|
|
|
Module *M = BB->getParent()->getParent();
|
|
|
|
return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
|
2002-01-21 07:37:31 +00:00
|
|
|
}
|
2002-01-23 05:49:41 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void BasicBlockPass::addToPassManager(PassManagerT<Method> *PM,
|
2002-01-31 18:32:27 +00:00
|
|
|
AnalysisSet &Required,
|
2002-01-31 00:45:31 +00:00
|
|
|
AnalysisSet &Destroyed,
|
|
|
|
AnalysisSet &Provided) {
|
2002-01-31 18:32:27 +00:00
|
|
|
PM->addPass(this, Required, Destroyed, Provided);
|
2002-01-31 00:45:31 +00:00
|
|
|
}
|
2002-01-23 05:49:41 +00:00
|
|
|
|
2002-01-31 00:45:31 +00:00
|
|
|
void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
|
2002-01-31 18:32:27 +00:00
|
|
|
AnalysisSet &Required,
|
2002-01-31 00:45:31 +00:00
|
|
|
AnalysisSet &Destroyed,
|
|
|
|
AnalysisSet &Provided) {
|
2002-01-31 18:32:27 +00:00
|
|
|
PM->addPass(this, Required, Destroyed, Provided);
|
2002-01-23 05:49:41 +00:00
|
|
|
}
|
2002-01-31 00:45:31 +00:00
|
|
|
|