2003-10-13 13:33:01 +08:00
|
|
|
//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/BasicBlock.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"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-03-04 20:32:42 +08:00
|
|
|
#include "llvm/IR/LegacyPassNameParser.h"
|
2016-04-23 06:06:11 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/OptBisect.h"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include "llvm/PassInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/PassRegistry.h"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include "llvm/PassSupport.h"
|
|
|
|
#include "llvm/Support/Compiler.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"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include <cassert>
|
|
|
|
|
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.
|
2017-09-07 07:05:38 +08:00
|
|
|
ModulePass::~ModulePass() = default;
|
2002-01-21 15:37:31 +08:00
|
|
|
|
2017-09-07 07:05:38 +08:00
|
|
|
Pass *ModulePass::createPrinterPass(raw_ostream &OS,
|
2010-04-03 07:17:14 +08:00
|
|
|
const std::string &Banner) const {
|
2017-09-07 07:05:38 +08:00
|
|
|
return createPrintModulePass(OS, Banner);
|
2010-04-03 07:17:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
PassManagerType ModulePass::getPotentialPassManagerType() const {
|
|
|
|
return PMT_ModulePassManager;
|
|
|
|
}
|
|
|
|
|
2019-02-28 12:00:55 +08:00
|
|
|
static std::string getDescription(const Module &M) {
|
|
|
|
return "module (" + M.getName().str() + ")";
|
|
|
|
}
|
|
|
|
|
2016-04-23 06:06:11 +08:00
|
|
|
bool ModulePass::skipModule(Module &M) const {
|
2019-02-28 12:00:55 +08:00
|
|
|
OptPassGate &Gate = M.getContext().getOptPassGate();
|
|
|
|
return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
|
2016-04-23 06:06:11 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef 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.
|
2017-09-07 07:05:38 +08:00
|
|
|
void Pass::print(raw_ostream &OS, const Module *) const {
|
|
|
|
OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
|
2002-07-27 09:12:17 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2006-12-08 04:04:42 +08:00
|
|
|
// dump - call print(cerr);
|
2016-01-30 04:50:44 +08:00
|
|
|
LLVM_DUMP_METHOD void Pass::dump() const {
|
2014-04-09 14:08:46 +08:00
|
|
|
print(dbgs(), nullptr);
|
2002-07-27 09:12:17 +08:00
|
|
|
}
|
2017-01-28 10:02:38 +08:00
|
|
|
#endif
|
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.
|
2017-09-07 07:05:38 +08:00
|
|
|
ImmutablePass::~ImmutablePass() = default;
|
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
|
|
|
|
2017-09-07 07:05:38 +08:00
|
|
|
Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
|
2010-04-03 07:17:14 +08:00
|
|
|
const std::string &Banner) const {
|
2017-09-07 07:05:38 +08:00
|
|
|
return createPrintFunctionPass(OS, Banner);
|
2010-04-03 07:17:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 03:43:09 +08:00
|
|
|
PassManagerType FunctionPass::getPotentialPassManagerType() const {
|
|
|
|
return PMT_FunctionPassManager;
|
|
|
|
}
|
|
|
|
|
2019-02-28 12:00:55 +08:00
|
|
|
static std::string getDescription(const Function &F) {
|
|
|
|
return "function (" + F.getName().str() + ")";
|
|
|
|
}
|
|
|
|
|
2017-01-15 18:23:18 +08:00
|
|
|
bool FunctionPass::skipFunction(const Function &F) const {
|
2019-02-28 12:00:55 +08:00
|
|
|
OptPassGate &Gate = F.getContext().getOptPassGate();
|
|
|
|
if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
|
2016-04-23 06:06:11 +08:00
|
|
|
return true;
|
|
|
|
|
2019-04-05 06:40:06 +08:00
|
|
|
if (F.hasOptNone()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
|
|
|
|
<< F.getName() << "\n");
|
2014-02-06 08:07:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
2017-09-07 07:05:38 +08:00
|
|
|
|
2016-10-01 12:03:30 +08:00
|
|
|
RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
|
2010-08-07 02:33:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-07 07:05:38 +08: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 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 {
|
2012-02-03 13:12:30 +08:00
|
|
|
|
2017-09-07 07:05:38 +08: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 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;
|
|
|
|
}
|