2003-10-13 07:33:01 +02:00
|
|
|
//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-01-21 08:37:31 +01:00
|
|
|
//
|
|
|
|
// This file implements the LLVM Pass infrastructure. It is primarily
|
|
|
|
// responsible with ensuring that passes are executed and batched together
|
|
|
|
// optimally.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-23 22:40:06 +02:00
|
|
|
#include "llvm/Pass.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2014-02-06 01:07:05 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
2014-01-12 12:10:32 +01:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-03-04 13:32:42 +01:00
|
|
|
#include "llvm/IR/LegacyPassNameParser.h"
|
2016-04-23 00:06:11 +02:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/OptBisect.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include "llvm/PassInfo.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/PassRegistry.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include "llvm/PassSupport.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-01-05 02:30:04 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-23 08:03:38 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
2003-11-21 21:23:48 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2014-04-22 00:55:11 +02:00
|
|
|
#define DEBUG_TYPE "ir"
|
|
|
|
|
2002-01-31 01:45:31 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Implementation
|
|
|
|
//
|
2002-01-23 06:49:41 +01:00
|
|
|
|
2007-04-26 23:33:42 +02:00
|
|
|
// Force out-of-line virtual method.
|
2012-02-03 06:12:30 +01:00
|
|
|
Pass::~Pass() {
|
|
|
|
delete Resolver;
|
2007-04-26 23:33:42 +02:00
|
|
|
}
|
|
|
|
|
2006-12-22 23:49:00 +01:00
|
|
|
// Force out-of-line virtual method.
|
2017-09-07 01:05:38 +02:00
|
|
|
ModulePass::~ModulePass() = default;
|
2002-01-21 08:37:31 +01:00
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
Pass *ModulePass::createPrinterPass(raw_ostream &OS,
|
2010-04-03 01:17:14 +02:00
|
|
|
const std::string &Banner) const {
|
2017-09-07 01:05:38 +02:00
|
|
|
return createPrintModulePass(OS, Banner);
|
2010-04-03 01:17:14 +02:00
|
|
|
}
|
|
|
|
|
2009-12-14 20:43:09 +01:00
|
|
|
PassManagerType ModulePass::getPotentialPassManagerType() const {
|
|
|
|
return PMT_ModulePassManager;
|
|
|
|
}
|
|
|
|
|
2016-04-23 00:06:11 +02:00
|
|
|
bool ModulePass::skipModule(Module &M) const {
|
2018-03-27 18:57:20 +02:00
|
|
|
return !M.getContext().getOptPassGate().shouldRunPass(this, M);
|
2016-04-23 00:06:11 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 20:33:48 +02:00
|
|
|
bool Pass::mustPreserveAnalysisID(char &AID) const {
|
2014-04-09 08:08:46 +02:00
|
|
|
return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
|
2003-03-21 22:41:02 +01:00
|
|
|
}
|
|
|
|
|
2011-11-21 05:42:21 +01:00
|
|
|
// dumpPassStructure - Implement the -debug-pass=Structure option
|
2010-08-19 03:29:07 +02:00
|
|
|
void Pass::dumpPassStructure(unsigned Offset) {
|
2010-01-05 02:30:04 +01:00
|
|
|
dbgs().indent(Offset*2) << getPassName() << "\n";
|
2002-07-30 18:27:02 +02:00
|
|
|
}
|
2002-04-29 16:57:45 +02:00
|
|
|
|
2008-03-14 19:27:04 +01: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.
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef Pass::getPassName() const {
|
2010-08-06 20:33:48 +02:00
|
|
|
AnalysisID AID = getPassID();
|
|
|
|
const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
|
|
|
|
if (PI)
|
2002-07-29 23:02:31 +02:00
|
|
|
return PI->getPassName();
|
2007-10-18 18:11:18 +02:00
|
|
|
return "Unnamed pass: implement Pass::getPassName()";
|
2002-07-29 23:02:31 +02:00
|
|
|
}
|
2002-04-29 16:57:45 +02:00
|
|
|
|
2009-12-14 20:43:09 +01:00
|
|
|
void Pass::preparePassManager(PMStack &) {
|
|
|
|
// By default, don't do anything.
|
|
|
|
}
|
|
|
|
|
|
|
|
PassManagerType Pass::getPotentialPassManagerType() const {
|
|
|
|
// Default implementation.
|
2012-02-03 06:12:30 +01:00
|
|
|
return PMT_Unknown;
|
2009-12-14 20:43:09 +01: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-06 20:33:48 +02:00
|
|
|
void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
|
2010-06-21 20:46:45 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImmutablePass *Pass::getAsImmutablePass() {
|
2014-04-09 08:08:46 +02:00
|
|
|
return nullptr;
|
2010-06-21 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PMDataManager *Pass::getAsPMDataManager() {
|
2014-04-09 08:08:46 +02:00
|
|
|
return nullptr;
|
2010-06-21 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pass::setResolver(AnalysisResolver *AR) {
|
|
|
|
assert(!Resolver && "Resolver is already set");
|
|
|
|
Resolver = AR;
|
|
|
|
}
|
|
|
|
|
2003-10-14 23:38:42 +02:00
|
|
|
// print - Print out the internal state of the pass. This is called by Analyze
|
2003-08-18 16:43:39 +02:00
|
|
|
// to print out the contents of an analysis. Otherwise it is not necessary to
|
2002-07-27 03:12:17 +02:00
|
|
|
// implement this method.
|
2017-09-07 01:05:38 +02:00
|
|
|
void Pass::print(raw_ostream &OS, const Module *) const {
|
|
|
|
OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
|
2002-07-27 03:12:17 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 16:32:27 +02:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2006-12-07 21:04:42 +01:00
|
|
|
// dump - call print(cerr);
|
2016-01-29 21:50:44 +01:00
|
|
|
LLVM_DUMP_METHOD void Pass::dump() const {
|
2014-04-09 08:08:46 +02:00
|
|
|
print(dbgs(), nullptr);
|
2002-07-27 03:12:17 +02:00
|
|
|
}
|
2017-01-28 03:02:38 +01:00
|
|
|
#endif
|
2002-07-27 03:12:17 +02:00
|
|
|
|
2002-09-25 23:59:11 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ImmutablePass Implementation
|
|
|
|
//
|
2006-12-22 23:49:00 +01:00
|
|
|
// Force out-of-line virtual method.
|
2017-09-07 01:05:38 +02:00
|
|
|
ImmutablePass::~ImmutablePass() = default;
|
2002-09-25 23:59:11 +02:00
|
|
|
|
2009-12-14 20:43:09 +01:00
|
|
|
void ImmutablePass::initializePass() {
|
|
|
|
// By default, don't do anything.
|
|
|
|
}
|
|
|
|
|
2002-01-31 01:45:31 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-27 08:56:12 +02:00
|
|
|
// FunctionPass Implementation
|
2002-01-31 01:45:31 +01:00
|
|
|
//
|
2002-01-22 01:17:48 +01:00
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
|
2010-04-03 01:17:14 +02:00
|
|
|
const std::string &Banner) const {
|
2017-09-07 01:05:38 +02:00
|
|
|
return createPrintFunctionPass(OS, Banner);
|
2010-04-03 01:17:14 +02:00
|
|
|
}
|
|
|
|
|
2009-12-14 20:43:09 +01:00
|
|
|
PassManagerType FunctionPass::getPotentialPassManagerType() const {
|
|
|
|
return PMT_FunctionPassManager;
|
|
|
|
}
|
|
|
|
|
2017-01-15 11:23:18 +01:00
|
|
|
bool FunctionPass::skipFunction(const Function &F) const {
|
2018-03-27 18:57:20 +02:00
|
|
|
if (!F.getContext().getOptPassGate().shouldRunPass(this, F))
|
2016-04-23 00:06:11 +02:00
|
|
|
return true;
|
|
|
|
|
2014-02-06 01:07:05 +01:00
|
|
|
if (F.hasFnAttribute(Attribute::OptimizeNone)) {
|
2016-04-23 00:06:11 +02:00
|
|
|
DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
|
|
|
|
<< F.getName() << "\n");
|
2014-02-06 01:07:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-01-31 01:45:31 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BasicBlockPass Implementation
|
|
|
|
//
|
2002-01-21 08:37:31 +01:00
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
Pass *BasicBlockPass::createPrinterPass(raw_ostream &OS,
|
2010-04-03 01:17:14 +02:00
|
|
|
const std::string &Banner) const {
|
2017-09-07 01:05:38 +02:00
|
|
|
return createPrintBasicBlockPass(OS, Banner);
|
2010-04-03 01:17:14 +02:00
|
|
|
}
|
|
|
|
|
2009-12-14 20:43:09 +01: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;
|
|
|
|
}
|
|
|
|
|
2016-04-23 00:06:11 +02:00
|
|
|
bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
|
2014-02-26 02:23:26 +01:00
|
|
|
const Function *F = BB.getParent();
|
2016-04-23 00:06:11 +02:00
|
|
|
if (!F)
|
|
|
|
return false;
|
2018-03-27 18:57:20 +02:00
|
|
|
if (!F->getContext().getOptPassGate().shouldRunPass(this, BB))
|
2016-04-23 00:06:11 +02:00
|
|
|
return true;
|
|
|
|
if (F->hasFnAttribute(Attribute::OptimizeNone)) {
|
2014-02-06 01:07:05 +01:00
|
|
|
// 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-14 20:43:09 +01:00
|
|
|
PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
|
2012-02-03 06:12:30 +01:00
|
|
|
return PMT_BasicBlockPassManager;
|
2009-12-14 20:43:09 +01:00
|
|
|
}
|
|
|
|
|
2010-08-06 20:33:48 +02:00
|
|
|
const PassInfo *Pass::lookupPassInfo(const void *TI) {
|
2010-07-20 23:22:24 +02:00
|
|
|
return PassRegistry::getPassRegistry()->getPassInfo(TI);
|
2002-07-23 20:08:00 +02:00
|
|
|
}
|
|
|
|
|
2010-07-20 10:26:15 +02:00
|
|
|
const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
|
2010-07-20 23:22:24 +02:00
|
|
|
return PassRegistry::getPassRegistry()->getPassInfo(Arg);
|
2009-10-08 19:00:02 +02:00
|
|
|
}
|
|
|
|
|
2012-02-15 04:21:47 +01:00
|
|
|
Pass *Pass::createPass(AnalysisID ID) {
|
|
|
|
const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
|
2012-02-08 22:22:34 +01:00
|
|
|
if (!PI)
|
2014-04-09 08:08:46 +02:00
|
|
|
return nullptr;
|
2012-02-08 22:22:34 +01:00
|
|
|
return PI->createPass();
|
|
|
|
}
|
|
|
|
|
2002-08-22 00:17:09 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analysis Group Implementation Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// RegisterAGBase implementation
|
2017-09-07 01:05:38 +02:00
|
|
|
|
2016-10-01 06:03:30 +02:00
|
|
|
RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
|
2010-08-06 20:33:48 +02:00
|
|
|
const void *PassID, bool isDefault)
|
2010-07-21 19:52:45 +02:00
|
|
|
: PassInfo(Name, InterfaceID) {
|
|
|
|
PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
|
|
|
|
*this, isDefault);
|
2002-08-22 00:17:09 +02:00
|
|
|
}
|
|
|
|
|
2002-07-23 20:08:00 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PassRegistrationListener implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
// enumeratePasses - Iterate over the registered passes, calling the
|
|
|
|
// passEnumerate callback on each PassInfo object.
|
|
|
|
void PassRegistrationListener::enumeratePasses() {
|
2010-07-20 23:22:24 +02:00
|
|
|
PassRegistry::getPassRegistry()->enumerateWith(this);
|
2002-07-23 20:08:00 +02:00
|
|
|
}
|
2005-04-25 03:01:35 +02:00
|
|
|
|
2015-01-22 22:01:12 +01:00
|
|
|
PassNameParser::PassNameParser(cl::Option &O)
|
|
|
|
: cl::parser<const PassInfo *>(O) {
|
2014-06-12 02:16:36 +02:00
|
|
|
PassRegistry::getPassRegistry()->addRegistrationListener(this);
|
|
|
|
}
|
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
// 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.
|
|
|
|
PassNameParser::~PassNameParser() = default;
|
2010-01-22 07:29:25 +01:00
|
|
|
|
2006-12-01 23:21:11 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisUsage Class Implementation
|
|
|
|
//
|
|
|
|
|
2006-12-02 00:27:45 +01:00
|
|
|
namespace {
|
2012-02-03 06:12:30 +01:00
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
struct GetCFGOnlyPasses : public PassRegistrationListener {
|
|
|
|
using VectorType = AnalysisUsage::VectorType;
|
|
|
|
|
|
|
|
VectorType &CFGOnlyList;
|
|
|
|
|
|
|
|
GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
|
|
|
|
|
|
|
|
void passEnumerate(const PassInfo *P) override {
|
|
|
|
if (P->isCFGOnlyPass())
|
|
|
|
CFGOnlyList.push_back(P->getTypeInfo());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
2006-12-02 00:27:45 +01:00
|
|
|
|
2012-09-27 12:14:43 +02:00
|
|
|
// setPreservesCFG - This function should be called to by the pass, iff they do
|
2006-12-01 23:21:11 +01: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 00:27:45 +01:00
|
|
|
GetCFGOnlyPasses(Preserved).enumeratePasses();
|
2006-12-01 23:21:11 +01:00
|
|
|
}
|
|
|
|
|
2010-08-06 20:33:48 +02: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 01:42:04 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-08-06 20:33:48 +02:00
|
|
|
AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
|
2010-08-06 02:23:35 +02:00
|
|
|
Required.push_back(ID);
|
2010-08-06 20:33:48 +02: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-21 20:46:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|