2003-10-13 13:33:01 +08:00
|
|
|
//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-01-21 15:37:31 +08:00
|
|
|
//
|
|
|
|
// This file implements the LLVM Pass infrastructure. It is primarily
|
|
|
|
// responsible with ensuring that passes are executed and batched together
|
|
|
|
// optimally.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-24 04:40:06 +08:00
|
|
|
#include "llvm/Pass.h"
|
2014-02-06 08:07:05 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2014-03-04 20:32:42 +08:00
|
|
|
#include "llvm/IR/LegacyPassNameParser.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/PassRegistry.h"
|
2010-01-05 09:30:04 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-23 14:03:38 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-11-22 04:23:48 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "ir"
|
|
|
|
|
2002-01-31 08:45:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Implementation
|
|
|
|
//
|
2002-01-23 13:49:41 +08:00
|
|
|
|
2007-04-27 05:33:42 +08:00
|
|
|
// Force out-of-line virtual method.
|
2012-02-03 13:12:30 +08:00
|
|
|
Pass::~Pass() {
|
|
|
|
delete Resolver;
|
2007-04-27 05:33:42 +08:00
|
|
|
}
|
|
|
|
|
2006-12-23 06:49:00 +08:00
|
|
|
// Force out-of-line virtual method.
|
|
|
|
ModulePass::~ModulePass() { }
|
2002-01-21 15:37:31 +08:00
|
|
|
|
2010-04-03 07:17:14 +08:00
|
|
|
Pass *ModulePass::createPrinterPass(raw_ostream &O,
|
|
|
|
const std::string &Banner) const {
|
2014-01-12 19:30:46 +08:00
|
|
|
return createPrintModulePass(O, Banner);
|
2010-04-03 07:17:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
PassManagerType ModulePass::getPotentialPassManagerType() const {
|
|
|
|
return PMT_ModulePassManager;
|
|
|
|
}
|
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
bool Pass::mustPreserveAnalysisID(char &AID) const {
|
2014-04-09 14:08:46 +08:00
|
|
|
return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
|
2003-03-22 05:41:02 +08:00
|
|
|
}
|
|
|
|
|
2011-11-21 12:42:21 +08:00
|
|
|
// dumpPassStructure - Implement the -debug-pass=Structure option
|
2010-08-19 09:29:07 +08:00
|
|
|
void Pass::dumpPassStructure(unsigned Offset) {
|
2010-01-05 09:30:04 +08:00
|
|
|
dbgs().indent(Offset*2) << getPassName() << "\n";
|
2002-07-31 00:27:02 +08:00
|
|
|
}
|
2002-04-29 22:57:45 +08:00
|
|
|
|
2008-03-15 02:27:04 +08:00
|
|
|
/// getPassName - Return a nice clean name for a pass. This usually
|
|
|
|
/// implemented in terms of the name that is registered by one of the
|
|
|
|
/// Registration templates, but can be overloaded directly.
|
|
|
|
///
|
2002-07-30 05:02:31 +08:00
|
|
|
const char *Pass::getPassName() const {
|
2010-08-07 02:33:48 +08:00
|
|
|
AnalysisID AID = getPassID();
|
|
|
|
const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
|
|
|
|
if (PI)
|
2002-07-30 05:02:31 +08:00
|
|
|
return PI->getPassName();
|
2007-10-19 00:11:18 +08:00
|
|
|
return "Unnamed pass: implement Pass::getPassName()";
|
2002-07-30 05:02:31 +08:00
|
|
|
}
|
2002-04-29 22:57:45 +08:00
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
void Pass::preparePassManager(PMStack &) {
|
|
|
|
// By default, don't do anything.
|
|
|
|
}
|
|
|
|
|
|
|
|
PassManagerType Pass::getPotentialPassManagerType() const {
|
|
|
|
// Default implementation.
|
2012-02-03 13:12:30 +08:00
|
|
|
return PMT_Unknown;
|
2009-12-15 03:43:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pass::getAnalysisUsage(AnalysisUsage &) const {
|
|
|
|
// By default, no analysis results are used, all are invalidated.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pass::releaseMemory() {
|
|
|
|
// By default, don't do anything.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pass::verifyAnalysis() const {
|
|
|
|
// By default, don't do anything.
|
|
|
|
}
|
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
|
2010-06-22 02:46:45 +08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImmutablePass *Pass::getAsImmutablePass() {
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2010-06-22 02:46:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PMDataManager *Pass::getAsPMDataManager() {
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2010-06-22 02:46:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pass::setResolver(AnalysisResolver *AR) {
|
|
|
|
assert(!Resolver && "Resolver is already set");
|
|
|
|
Resolver = AR;
|
|
|
|
}
|
|
|
|
|
2003-10-15 05:38:42 +08:00
|
|
|
// print - Print out the internal state of the pass. This is called by Analyze
|
2003-08-18 22:43:39 +08:00
|
|
|
// to print out the contents of an analysis. Otherwise it is not necessary to
|
2002-07-27 09:12:17 +08:00
|
|
|
// implement this method.
|
|
|
|
//
|
2009-08-23 14:03:38 +08:00
|
|
|
void Pass::print(raw_ostream &O,const Module*) const {
|
2002-07-27 09:12:17 +08:00
|
|
|
O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
|
|
|
|
}
|
|
|
|
|
2006-12-08 04:04:42 +08:00
|
|
|
// dump - call print(cerr);
|
2002-07-27 09:12:17 +08:00
|
|
|
void Pass::dump() const {
|
2014-04-09 14:08:46 +08:00
|
|
|
print(dbgs(), nullptr);
|
2002-07-27 09:12:17 +08:00
|
|
|
}
|
|
|
|
|
2002-09-26 05:59:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ImmutablePass Implementation
|
|
|
|
//
|
2006-12-23 06:49:00 +08:00
|
|
|
// Force out-of-line virtual method.
|
|
|
|
ImmutablePass::~ImmutablePass() { }
|
2002-09-26 05:59:11 +08:00
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
void ImmutablePass::initializePass() {
|
|
|
|
// By default, don't do anything.
|
|
|
|
}
|
|
|
|
|
2002-01-31 08:45:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-27 14:56:12 +08:00
|
|
|
// FunctionPass Implementation
|
2002-01-31 08:45:31 +08:00
|
|
|
//
|
2002-01-22 08:17:48 +08:00
|
|
|
|
2010-04-03 07:17:14 +08:00
|
|
|
Pass *FunctionPass::createPrinterPass(raw_ostream &O,
|
|
|
|
const std::string &Banner) const {
|
2014-01-12 19:30:46 +08:00
|
|
|
return createPrintFunctionPass(O, Banner);
|
2010-04-03 07:17:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
PassManagerType FunctionPass::getPotentialPassManagerType() const {
|
|
|
|
return PMT_FunctionPassManager;
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:23:26 +08:00
|
|
|
bool FunctionPass::skipOptnoneFunction(const Function &F) const {
|
2014-02-06 08:07:05 +08:00
|
|
|
if (F.hasFnAttribute(Attribute::OptimizeNone)) {
|
|
|
|
DEBUG(dbgs() << "Skipping pass '" << getPassName()
|
|
|
|
<< "' on function " << F.getName() << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-01-31 08:45:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BasicBlockPass Implementation
|
|
|
|
//
|
2002-01-21 15:37:31 +08:00
|
|
|
|
2010-04-03 07:17:14 +08:00
|
|
|
Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
|
|
|
|
const std::string &Banner) const {
|
2014-01-12 19:30:46 +08:00
|
|
|
return createPrintBasicBlockPass(O, Banner);
|
2010-04-03 07:17:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
bool BasicBlockPass::doInitialization(Function &) {
|
|
|
|
// By default, don't do anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasicBlockPass::doFinalization(Function &) {
|
|
|
|
// By default, don't do anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:23:26 +08:00
|
|
|
bool BasicBlockPass::skipOptnoneFunction(const BasicBlock &BB) const {
|
|
|
|
const Function *F = BB.getParent();
|
2014-02-06 08:07:05 +08:00
|
|
|
if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
|
|
|
|
// Report this only once per function.
|
|
|
|
if (&BB == &F->getEntryBlock())
|
|
|
|
DEBUG(dbgs() << "Skipping pass '" << getPassName()
|
|
|
|
<< "' on function " << F->getName() << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
|
2012-02-03 13:12:30 +08:00
|
|
|
return PMT_BasicBlockPassManager;
|
2009-12-15 03:43:09 +08:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
const PassInfo *Pass::lookupPassInfo(const void *TI) {
|
2010-07-21 05:22:24 +08:00
|
|
|
return PassRegistry::getPassRegistry()->getPassInfo(TI);
|
2002-07-24 02:08:00 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 16:26:15 +08:00
|
|
|
const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
|
2010-07-21 05:22:24 +08:00
|
|
|
return PassRegistry::getPassRegistry()->getPassInfo(Arg);
|
2009-10-09 01:00:02 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 11:21:47 +08:00
|
|
|
Pass *Pass::createPass(AnalysisID ID) {
|
|
|
|
const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
|
2012-02-09 05:22:34 +08:00
|
|
|
if (!PI)
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2012-02-09 05:22:34 +08:00
|
|
|
return PI->createPass();
|
|
|
|
}
|
|
|
|
|
2002-08-22 06:17:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analysis Group Implementation Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// RegisterAGBase implementation
|
|
|
|
//
|
2010-08-07 02:33:48 +08:00
|
|
|
RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
|
|
|
|
const void *PassID, bool isDefault)
|
2010-07-22 01:52:45 +08:00
|
|
|
: PassInfo(Name, InterfaceID) {
|
|
|
|
PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
|
|
|
|
*this, isDefault);
|
2002-08-22 06:17:09 +08:00
|
|
|
}
|
|
|
|
|
2002-07-24 02:08:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PassRegistrationListener implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
// enumeratePasses - Iterate over the registered passes, calling the
|
|
|
|
// passEnumerate callback on each PassInfo object.
|
|
|
|
//
|
|
|
|
void PassRegistrationListener::enumeratePasses() {
|
2010-07-21 05:22:24 +08:00
|
|
|
PassRegistry::getPassRegistry()->enumerateWith(this);
|
2002-07-24 02:08:00 +08:00
|
|
|
}
|
2005-04-25 09:01:35 +08:00
|
|
|
|
2015-01-23 05:01:12 +08:00
|
|
|
PassNameParser::PassNameParser(cl::Option &O)
|
|
|
|
: cl::parser<const PassInfo *>(O) {
|
2014-06-12 08:16:36 +08:00
|
|
|
PassRegistry::getPassRegistry()->addRegistrationListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
PassNameParser::~PassNameParser() {
|
|
|
|
// This only gets called during static destruction, in which case the
|
|
|
|
// PassRegistry will have already been destroyed by llvm_shutdown(). So
|
|
|
|
// attempting to remove the registration listener is an error.
|
|
|
|
}
|
2010-01-22 14:29:25 +08:00
|
|
|
|
2006-12-02 06:21:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisUsage Class Implementation
|
|
|
|
//
|
|
|
|
|
2006-12-02 07:27:45 +08:00
|
|
|
namespace {
|
|
|
|
struct GetCFGOnlyPasses : public PassRegistrationListener {
|
2008-08-08 13:33:04 +08:00
|
|
|
typedef AnalysisUsage::VectorType VectorType;
|
|
|
|
VectorType &CFGOnlyList;
|
|
|
|
GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
|
2012-02-03 13:12:30 +08:00
|
|
|
|
2014-03-05 14:35:38 +08:00
|
|
|
void passEnumerate(const PassInfo *P) override {
|
2006-12-02 07:27:45 +08:00
|
|
|
if (P->isCFGOnlyPass())
|
2010-08-07 02:33:48 +08:00
|
|
|
CFGOnlyList.push_back(P->getTypeInfo());
|
2006-12-02 07:27:45 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2006-12-02 07:27:45 +08:00
|
|
|
|
2012-09-27 18:14:43 +08:00
|
|
|
// setPreservesCFG - This function should be called to by the pass, iff they do
|
2006-12-02 06:21:11 +08:00
|
|
|
// not:
|
|
|
|
//
|
|
|
|
// 1. Add or remove basic blocks from the function
|
|
|
|
// 2. Modify terminator instructions in any way.
|
|
|
|
//
|
|
|
|
// This function annotates the AnalysisUsage info object to say that analyses
|
|
|
|
// that only depend on the CFG are preserved by this pass.
|
|
|
|
//
|
|
|
|
void AnalysisUsage::setPreservesCFG() {
|
|
|
|
// Since this transformation doesn't modify the CFG, it preserves all analyses
|
|
|
|
// that only depend on the CFG (like dominators, loop info, etc...)
|
2006-12-02 07:27:45 +08:00
|
|
|
GetCFGOnlyPasses(Preserved).enumeratePasses();
|
2006-12-02 06:21:11 +08:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
|
|
|
|
const PassInfo *PI = Pass::lookupPassInfo(Arg);
|
|
|
|
// If the pass exists, preserve it. Otherwise silently do nothing.
|
|
|
|
if (PI) Preserved.push_back(PI->getTypeInfo());
|
2010-08-06 07:42:04 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
|
2010-08-06 08:23:35 +08:00
|
|
|
Required.push_back(ID);
|
2010-08-07 02:33:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
|
|
|
|
Required.push_back(&ID);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
|
|
|
|
Required.push_back(&ID);
|
|
|
|
RequiredTransitive.push_back(&ID);
|
2010-06-22 02:46:45 +08:00
|
|
|
return *this;
|
|
|
|
}
|