Pass.cpp 8.6 KB

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