//
//===----------------------------------------------------------------------===//
-#include "llvm/Pass.h"
+#include "llvm/PassManager.h"
+#include "llvm/Module.h"
+#include "llvm/Function.h"
+#include "llvm/BasicBlock.h"
#include "Support/STLExtras.h"
+#include <algorithm>
-PassManager::~PassManager() {
- for_each(Passes.begin(), Passes.end(), deleter<Pass>);
-}
-
-class BasicBlockPassBatcher : public MethodPass {
- typedef std::vector<BasicBlockPass*> SubPassesType;
- SubPassesType SubPasses;
-public:
- ~BasicBlockPassBatcher() {
- for_each(SubPasses.begin(), SubPasses.end(), deleter<BasicBlockPass>);
- }
-
- void add(BasicBlockPass *P) { SubPasses.push_back(P); }
-
- virtual bool doPassInitialization(Module *M) {
- bool Changed = false;
- for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
- I != E; ++I)
- Changed |= (*I)->doInitialization(M);
- return Changed;
- }
+// Source of unique analysis ID #'s.
+unsigned AnalysisID::NextID = 0;
- virtual bool runOnMethod(Method *M) {
- bool Changed = false;
+void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
+ assert(P->Resolver == 0 && "Pass already in a PassManager!");
+ P->Resolver = AR;
+}
- for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
- for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
- I != E; ++I)
- Changed |= (*I)->runOnBasicBlock(*MI);
- return Changed;
- }
- virtual bool doFinalization(Module *M) {
- bool Changed = false;
- for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
- I != E; ++I)
- Changed |= (*I)->doFinalization(M);
+// 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.
+//
+#include "Support/CommandLine.h"
+#include <typeinfo>
+#include <iostream>
- return Changed;
- }
+// Different debug levels that can be enabled...
+enum PassDebugLevel {
+ None, PassStructure, PassExecutions, PassDetails
};
-class MethodPassBatcher : public Pass {
- typedef std::vector<MethodPass*> SubPassesType;
- SubPassesType SubPasses;
- BasicBlockPassBatcher *BBPBatcher;
-public:
- ~MethodPassBatcher() {
- for_each(SubPasses.begin(), SubPasses.end(), deleter<MethodPass>);
- }
+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);
- void add(BasicBlockPass *BBP) {
- if (BBPBatcher == 0) {
- BBPBatcher = new BasicBlockPassBatcher();
- SubPasses.push_back(BBPBatcher);
+void PMDebug::PrintPassStructure(Pass *P) {
+ if (PassDebugging >= PassStructure)
+ P->dumpPassStructure();
+}
+
+void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
+ Pass *P, Value *V) {
+ if (PassDebugging >= PassExecutions) {
+ std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
+ << typeid(*P).name();
+ if (V) {
+ std::cerr << "' on ";
+ switch (V->getValueType()) {
+ case Value::ModuleVal:
+ std::cerr << "Module\n"; return;
+ case Value::FunctionVal:
+ std::cerr << "Function '" << V->getName(); break;
+ case Value::BasicBlockVal:
+ std::cerr << "BasicBlock '" << V->getName(); break;
+ default:
+ std::cerr << typeid(*V).name() << " '" << V->getName(); break;
+ }
}
- BBPBatcher->add(BBP);
+ std::cerr << "'...\n";
}
+}
- void add(MethodPass *P) {
- if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) {
- add(BBP);
- } else {
- BBPBatcher = 0; // Ensure that passes don't get accidentally reordered
- SubPasses.push_back(P);
+void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
+ Pass *P, const Pass::AnalysisSet &Set) {
+ if (PassDebugging >= PassDetails && !Set.empty()) {
+ std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
+ 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;
}
+ std::cerr << "\n";
}
+}
- virtual bool run(Module *M) {
- bool Changed = false;
- for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
- I != E; ++I)
- Changed |= (*I)->doInitialization(M);
+// dumpPassStructure - Implement the -debug-passes=PassStructure option
+void Pass::dumpPassStructure(unsigned Offset = 0) {
+ std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
+}
- for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
- for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
- I != E; ++I)
- Changed |= (*I)->runOnMethod(*MI);
- for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
- I != E; ++I)
- Changed |= (*I)->doFinalization(M);
+//===----------------------------------------------------------------------===//
+// Pass Implementation
+//
- return Changed;
- }
-};
+void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
+ AnalysisSet &Destroyed, AnalysisSet &Provided) {
+ PM->addPass(this, Required, Destroyed, Provided);
+}
-// add(BasicBlockPass*) - If we know it's a BasicBlockPass, we don't have to do
-// any checking...
+//===----------------------------------------------------------------------===//
+// MethodPass Implementation
+//
+
+// run - On a module, we run this pass by initializing, ronOnMethod'ing once
+// for every method in the module, then by finalizing.
//
-void PassManager::add(BasicBlockPass *BBP) {
- if (Batcher == 0) // If we don't have a batcher yet, make one now.
- add((MethodPass*)BBP);
- else
- Batcher->add(BBP);
+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);
}
+// run - On a method, we simply initialize, run the method, then finalize.
+//
+bool MethodPass::run(Function *F) {
+ if (F->isExternal()) return false; // Passes are not run on external methods!
+
+ return doInitialization(F->getParent()) | runOnMethod(F)
+ | doFinalization(F->getParent());
+}
-// add(MethodPass*) - MethodPass's must be batched together... make sure this
-// happens now.
+void MethodPass::addToPassManager(PassManagerT<Module> *PM,
+ AnalysisSet &Required, AnalysisSet &Destroyed,
+ AnalysisSet &Provided) {
+ PM->addPass(this, Required, Destroyed, Provided);
+}
+
+void MethodPass::addToPassManager(PassManagerT<Function> *PM,
+ AnalysisSet &Required, AnalysisSet &Destroyed,
+ AnalysisSet &Provided) {
+ PM->addPass(this, Required, Destroyed, Provided);
+}
+
+//===----------------------------------------------------------------------===//
+// BasicBlockPass Implementation
//
-void PassManager::add(MethodPass *MP) {
- if (Batcher == 0) { // If we don't have a batcher yet, make one now.
- Batcher = new MethodPassBatcher();
- Passes.push_back(Batcher);
- }
- Batcher->add(MP); // The Batcher will queue them passes up
+
+// To run this pass on a method, we simply call runOnBasicBlock once for each
+// method.
+//
+bool BasicBlockPass::runOnMethod(Function *F) {
+ bool Changed = false;
+ for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
+ Changed |= runOnBasicBlock(*I);
+ return Changed;
}
-// add - Add a pass to the PassManager, batching it up as appropriate...
-void PassManager::add(Pass *P) {
- if (MethodPass *MP = dynamic_cast<MethodPass*>(P)) {
- add(MP); // Use the methodpass specific code to do the addition
- } else {
- Batcher = 0; // Ensure that passes don't get accidentally reordered
- Passes.push_back(P);
- }
+// 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);
}
+
+void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
+ AnalysisSet &Required,
+ AnalysisSet &Destroyed,
+ AnalysisSet &Provided) {
+ PM->addPass(this, Required, Destroyed, Provided);
+}
+
+void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
+ AnalysisSet &Required,
+ AnalysisSet &Destroyed,
+ AnalysisSet &Provided) {
+ PM->addPass(this, Required, Destroyed, Provided);
+}
+