Pass.cpp 9.7 KB

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