Pass.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the LLVM Pass infrastructure. It is primarily
  10. // responsible with ensuring that passes are executed and batched together
  11. // optimally.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Pass.h"
  15. #include "llvm/Config/llvm-config.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRPrintingPasses.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/LegacyPassNameParser.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/OptBisect.h"
  22. #include "llvm/PassInfo.h"
  23. #include "llvm/PassRegistry.h"
  24. #include "llvm/Support/Compiler.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <cassert>
  28. using namespace llvm;
  29. #define DEBUG_TYPE "ir"
  30. //===----------------------------------------------------------------------===//
  31. // Pass Implementation
  32. //
  33. // Force out-of-line virtual method.
  34. Pass::~Pass() {
  35. delete Resolver;
  36. }
  37. // Force out-of-line virtual method.
  38. ModulePass::~ModulePass() = default;
  39. Pass *ModulePass::createPrinterPass(raw_ostream &OS,
  40. const std::string &Banner) const {
  41. return createPrintModulePass(OS, Banner);
  42. }
  43. PassManagerType ModulePass::getPotentialPassManagerType() const {
  44. return PMT_ModulePassManager;
  45. }
  46. static std::string getDescription(const Module &M) {
  47. return "module (" + M.getName().str() + ")";
  48. }
  49. bool ModulePass::skipModule(Module &M) const {
  50. OptPassGate &Gate = M.getContext().getOptPassGate();
  51. return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
  52. }
  53. bool Pass::mustPreserveAnalysisID(char &AID) const {
  54. return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
  55. }
  56. // dumpPassStructure - Implement the -debug-pass=Structure option
  57. void Pass::dumpPassStructure(unsigned Offset) {
  58. dbgs().indent(Offset*2) << getPassName() << "\n";
  59. }
  60. /// getPassName - Return a nice clean name for a pass. This usually
  61. /// implemented in terms of the name that is registered by one of the
  62. /// Registration templates, but can be overloaded directly.
  63. StringRef Pass::getPassName() const {
  64. AnalysisID AID = getPassID();
  65. const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
  66. if (PI)
  67. return PI->getPassName();
  68. return "Unnamed pass: implement Pass::getPassName()";
  69. }
  70. void Pass::preparePassManager(PMStack &) {
  71. // By default, don't do anything.
  72. }
  73. PassManagerType Pass::getPotentialPassManagerType() const {
  74. // Default implementation.
  75. return PMT_Unknown;
  76. }
  77. void Pass::getAnalysisUsage(AnalysisUsage &) const {
  78. // By default, no analysis results are used, all are invalidated.
  79. }
  80. void Pass::releaseMemory() {
  81. // By default, don't do anything.
  82. }
  83. void Pass::verifyAnalysis() const {
  84. // By default, don't do anything.
  85. }
  86. void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
  87. return this;
  88. }
  89. ImmutablePass *Pass::getAsImmutablePass() {
  90. return nullptr;
  91. }
  92. PMDataManager *Pass::getAsPMDataManager() {
  93. return nullptr;
  94. }
  95. void Pass::setResolver(AnalysisResolver *AR) {
  96. assert(!Resolver && "Resolver is already set");
  97. Resolver = AR;
  98. }
  99. // print - Print out the internal state of the pass. This is called by Analyze
  100. // to print out the contents of an analysis. Otherwise it is not necessary to
  101. // implement this method.
  102. void Pass::print(raw_ostream &OS, const Module *) const {
  103. OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
  104. }
  105. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  106. // dump - call print(cerr);
  107. LLVM_DUMP_METHOD void Pass::dump() const {
  108. print(dbgs(), nullptr);
  109. }
  110. #endif
  111. //===----------------------------------------------------------------------===//
  112. // ImmutablePass Implementation
  113. //
  114. // Force out-of-line virtual method.
  115. ImmutablePass::~ImmutablePass() = default;
  116. void ImmutablePass::initializePass() {
  117. // By default, don't do anything.
  118. }
  119. //===----------------------------------------------------------------------===//
  120. // FunctionPass Implementation
  121. //
  122. Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
  123. const std::string &Banner) const {
  124. return createPrintFunctionPass(OS, Banner);
  125. }
  126. PassManagerType FunctionPass::getPotentialPassManagerType() const {
  127. return PMT_FunctionPassManager;
  128. }
  129. static std::string getDescription(const Function &F) {
  130. return "function (" + F.getName().str() + ")";
  131. }
  132. bool FunctionPass::skipFunction(const Function &F) const {
  133. OptPassGate &Gate = F.getContext().getOptPassGate();
  134. if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
  135. return true;
  136. if (F.hasOptNone()) {
  137. LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
  138. << F.getName() << "\n");
  139. return true;
  140. }
  141. return false;
  142. }
  143. const PassInfo *Pass::lookupPassInfo(const void *TI) {
  144. return PassRegistry::getPassRegistry()->getPassInfo(TI);
  145. }
  146. const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
  147. return PassRegistry::getPassRegistry()->getPassInfo(Arg);
  148. }
  149. Pass *Pass::createPass(AnalysisID ID) {
  150. const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
  151. if (!PI)
  152. return nullptr;
  153. return PI->createPass();
  154. }
  155. //===----------------------------------------------------------------------===//
  156. // Analysis Group Implementation Code
  157. //===----------------------------------------------------------------------===//
  158. // RegisterAGBase implementation
  159. RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
  160. const void *PassID, bool isDefault)
  161. : PassInfo(Name, InterfaceID) {
  162. PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
  163. *this, isDefault);
  164. }
  165. //===----------------------------------------------------------------------===//
  166. // PassRegistrationListener implementation
  167. //
  168. // enumeratePasses - Iterate over the registered passes, calling the
  169. // passEnumerate callback on each PassInfo object.
  170. void PassRegistrationListener::enumeratePasses() {
  171. PassRegistry::getPassRegistry()->enumerateWith(this);
  172. }
  173. PassNameParser::PassNameParser(cl::Option &O)
  174. : cl::parser<const PassInfo *>(O) {
  175. PassRegistry::getPassRegistry()->addRegistrationListener(this);
  176. }
  177. // This only gets called during static destruction, in which case the
  178. // PassRegistry will have already been destroyed by llvm_shutdown(). So
  179. // attempting to remove the registration listener is an error.
  180. PassNameParser::~PassNameParser() = default;
  181. //===----------------------------------------------------------------------===//
  182. // AnalysisUsage Class Implementation
  183. //
  184. namespace {
  185. struct GetCFGOnlyPasses : public PassRegistrationListener {
  186. using VectorType = AnalysisUsage::VectorType;
  187. VectorType &CFGOnlyList;
  188. GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
  189. void passEnumerate(const PassInfo *P) override {
  190. if (P->isCFGOnlyPass())
  191. CFGOnlyList.push_back(P->getTypeInfo());
  192. }
  193. };
  194. } // end anonymous namespace
  195. // setPreservesCFG - This function should be called to by the pass, iff they do
  196. // not:
  197. //
  198. // 1. Add or remove basic blocks from the function
  199. // 2. Modify terminator instructions in any way.
  200. //
  201. // This function annotates the AnalysisUsage info object to say that analyses
  202. // that only depend on the CFG are preserved by this pass.
  203. void AnalysisUsage::setPreservesCFG() {
  204. // Since this transformation doesn't modify the CFG, it preserves all analyses
  205. // that only depend on the CFG (like dominators, loop info, etc...)
  206. GetCFGOnlyPasses(Preserved).enumeratePasses();
  207. }
  208. AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
  209. const PassInfo *PI = Pass::lookupPassInfo(Arg);
  210. // If the pass exists, preserve it. Otherwise silently do nothing.
  211. if (PI)
  212. pushUnique(Preserved, PI->getTypeInfo());
  213. return *this;
  214. }
  215. AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
  216. pushUnique(Required, ID);
  217. return *this;
  218. }
  219. AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
  220. pushUnique(Required, &ID);
  221. return *this;
  222. }
  223. AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
  224. pushUnique(Required, &ID);
  225. pushUnique(RequiredTransitive, &ID);
  226. return *this;
  227. }