PassBuilder.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //===- Parsing, selection, and construction of pass pipelines -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. ///
  11. /// This file provides the implementation of the PassBuilder based on our
  12. /// static pass registry as well as related functionality. It also provides
  13. /// helpers to aid in analyzing, debugging, and testing passes and pass
  14. /// pipelines.
  15. ///
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Passes/PassBuilder.h"
  18. #include "llvm/Analysis/AssumptionCache.h"
  19. #include "llvm/Analysis/CGSCCPassManager.h"
  20. #include "llvm/Analysis/LazyCallGraph.h"
  21. #include "llvm/Analysis/LoopInfo.h"
  22. #include "llvm/Analysis/TargetLibraryInfo.h"
  23. #include "llvm/Analysis/TargetTransformInfo.h"
  24. #include "llvm/IR/Dominators.h"
  25. #include "llvm/IR/IRPrintingPasses.h"
  26. #include "llvm/IR/PassManager.h"
  27. #include "llvm/IR/Verifier.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. #include "llvm/Transforms/InstCombine/InstCombine.h"
  31. #include "llvm/Transforms/Scalar/EarlyCSE.h"
  32. #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
  33. #include "llvm/Transforms/Scalar/SimplifyCFG.h"
  34. using namespace llvm;
  35. namespace {
  36. /// \brief No-op module pass which does nothing.
  37. struct NoOpModulePass {
  38. PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
  39. static StringRef name() { return "NoOpModulePass"; }
  40. };
  41. /// \brief No-op module analysis.
  42. struct NoOpModuleAnalysis {
  43. struct Result {};
  44. Result run(Module &) { return Result(); }
  45. static StringRef name() { return "NoOpModuleAnalysis"; }
  46. static void *ID() { return (void *)&PassID; }
  47. private:
  48. static char PassID;
  49. };
  50. char NoOpModuleAnalysis::PassID;
  51. /// \brief No-op CGSCC pass which does nothing.
  52. struct NoOpCGSCCPass {
  53. PreservedAnalyses run(LazyCallGraph::SCC &C) {
  54. return PreservedAnalyses::all();
  55. }
  56. static StringRef name() { return "NoOpCGSCCPass"; }
  57. };
  58. /// \brief No-op CGSCC analysis.
  59. struct NoOpCGSCCAnalysis {
  60. struct Result {};
  61. Result run(LazyCallGraph::SCC &) { return Result(); }
  62. static StringRef name() { return "NoOpCGSCCAnalysis"; }
  63. static void *ID() { return (void *)&PassID; }
  64. private:
  65. static char PassID;
  66. };
  67. char NoOpCGSCCAnalysis::PassID;
  68. /// \brief No-op function pass which does nothing.
  69. struct NoOpFunctionPass {
  70. PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
  71. static StringRef name() { return "NoOpFunctionPass"; }
  72. };
  73. /// \brief No-op function analysis.
  74. struct NoOpFunctionAnalysis {
  75. struct Result {};
  76. Result run(Function &) { return Result(); }
  77. static StringRef name() { return "NoOpFunctionAnalysis"; }
  78. static void *ID() { return (void *)&PassID; }
  79. private:
  80. static char PassID;
  81. };
  82. char NoOpFunctionAnalysis::PassID;
  83. } // End anonymous namespace.
  84. void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
  85. #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
  86. MAM.registerPass(CREATE_PASS);
  87. #include "PassRegistry.def"
  88. }
  89. void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
  90. #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
  91. CGAM.registerPass(CREATE_PASS);
  92. #include "PassRegistry.def"
  93. }
  94. void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
  95. #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
  96. FAM.registerPass(CREATE_PASS);
  97. #include "PassRegistry.def"
  98. }
  99. #ifndef NDEBUG
  100. static bool isModulePassName(StringRef Name) {
  101. #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
  102. #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
  103. if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
  104. return true;
  105. #include "PassRegistry.def"
  106. return false;
  107. }
  108. #endif
  109. static bool isCGSCCPassName(StringRef Name) {
  110. #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
  111. #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
  112. if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
  113. return true;
  114. #include "PassRegistry.def"
  115. return false;
  116. }
  117. static bool isFunctionPassName(StringRef Name) {
  118. #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
  119. #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
  120. if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
  121. return true;
  122. #include "PassRegistry.def"
  123. return false;
  124. }
  125. bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
  126. #define MODULE_PASS(NAME, CREATE_PASS) \
  127. if (Name == NAME) { \
  128. MPM.addPass(CREATE_PASS); \
  129. return true; \
  130. }
  131. #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
  132. if (Name == "require<" NAME ">") { \
  133. MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
  134. return true; \
  135. } \
  136. if (Name == "invalidate<" NAME ">") { \
  137. MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
  138. return true; \
  139. }
  140. #include "PassRegistry.def"
  141. return false;
  142. }
  143. bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
  144. #define CGSCC_PASS(NAME, CREATE_PASS) \
  145. if (Name == NAME) { \
  146. CGPM.addPass(CREATE_PASS); \
  147. return true; \
  148. }
  149. #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
  150. if (Name == "require<" NAME ">") { \
  151. CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
  152. return true; \
  153. } \
  154. if (Name == "invalidate<" NAME ">") { \
  155. CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
  156. return true; \
  157. }
  158. #include "PassRegistry.def"
  159. return false;
  160. }
  161. bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
  162. StringRef Name) {
  163. #define FUNCTION_PASS(NAME, CREATE_PASS) \
  164. if (Name == NAME) { \
  165. FPM.addPass(CREATE_PASS); \
  166. return true; \
  167. }
  168. #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
  169. if (Name == "require<" NAME ">") { \
  170. FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
  171. return true; \
  172. } \
  173. if (Name == "invalidate<" NAME ">") { \
  174. FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
  175. return true; \
  176. }
  177. #include "PassRegistry.def"
  178. return false;
  179. }
  180. bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
  181. StringRef &PipelineText,
  182. bool VerifyEachPass,
  183. bool DebugLogging) {
  184. for (;;) {
  185. // Parse nested pass managers by recursing.
  186. if (PipelineText.startswith("function(")) {
  187. FunctionPassManager NestedFPM(DebugLogging);
  188. // Parse the inner pipeline inte the nested manager.
  189. PipelineText = PipelineText.substr(strlen("function("));
  190. if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
  191. DebugLogging) ||
  192. PipelineText.empty())
  193. return false;
  194. assert(PipelineText[0] == ')');
  195. PipelineText = PipelineText.substr(1);
  196. // Add the nested pass manager with the appropriate adaptor.
  197. FPM.addPass(std::move(NestedFPM));
  198. } else {
  199. // Otherwise try to parse a pass name.
  200. size_t End = PipelineText.find_first_of(",)");
  201. if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
  202. return false;
  203. if (VerifyEachPass)
  204. FPM.addPass(VerifierPass());
  205. PipelineText = PipelineText.substr(End);
  206. }
  207. if (PipelineText.empty() || PipelineText[0] == ')')
  208. return true;
  209. assert(PipelineText[0] == ',');
  210. PipelineText = PipelineText.substr(1);
  211. }
  212. }
  213. bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
  214. StringRef &PipelineText,
  215. bool VerifyEachPass,
  216. bool DebugLogging) {
  217. for (;;) {
  218. // Parse nested pass managers by recursing.
  219. if (PipelineText.startswith("cgscc(")) {
  220. CGSCCPassManager NestedCGPM(DebugLogging);
  221. // Parse the inner pipeline into the nested manager.
  222. PipelineText = PipelineText.substr(strlen("cgscc("));
  223. if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
  224. DebugLogging) ||
  225. PipelineText.empty())
  226. return false;
  227. assert(PipelineText[0] == ')');
  228. PipelineText = PipelineText.substr(1);
  229. // Add the nested pass manager with the appropriate adaptor.
  230. CGPM.addPass(std::move(NestedCGPM));
  231. } else if (PipelineText.startswith("function(")) {
  232. FunctionPassManager NestedFPM(DebugLogging);
  233. // Parse the inner pipeline inte the nested manager.
  234. PipelineText = PipelineText.substr(strlen("function("));
  235. if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
  236. DebugLogging) ||
  237. PipelineText.empty())
  238. return false;
  239. assert(PipelineText[0] == ')');
  240. PipelineText = PipelineText.substr(1);
  241. // Add the nested pass manager with the appropriate adaptor.
  242. CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
  243. } else {
  244. // Otherwise try to parse a pass name.
  245. size_t End = PipelineText.find_first_of(",)");
  246. if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
  247. return false;
  248. // FIXME: No verifier support for CGSCC passes!
  249. PipelineText = PipelineText.substr(End);
  250. }
  251. if (PipelineText.empty() || PipelineText[0] == ')')
  252. return true;
  253. assert(PipelineText[0] == ',');
  254. PipelineText = PipelineText.substr(1);
  255. }
  256. }
  257. bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
  258. StringRef &PipelineText,
  259. bool VerifyEachPass,
  260. bool DebugLogging) {
  261. for (;;) {
  262. // Parse nested pass managers by recursing.
  263. if (PipelineText.startswith("module(")) {
  264. ModulePassManager NestedMPM(DebugLogging);
  265. // Parse the inner pipeline into the nested manager.
  266. PipelineText = PipelineText.substr(strlen("module("));
  267. if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
  268. DebugLogging) ||
  269. PipelineText.empty())
  270. return false;
  271. assert(PipelineText[0] == ')');
  272. PipelineText = PipelineText.substr(1);
  273. // Now add the nested manager as a module pass.
  274. MPM.addPass(std::move(NestedMPM));
  275. } else if (PipelineText.startswith("cgscc(")) {
  276. CGSCCPassManager NestedCGPM(DebugLogging);
  277. // Parse the inner pipeline inte the nested manager.
  278. PipelineText = PipelineText.substr(strlen("cgscc("));
  279. if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
  280. DebugLogging) ||
  281. PipelineText.empty())
  282. return false;
  283. assert(PipelineText[0] == ')');
  284. PipelineText = PipelineText.substr(1);
  285. // Add the nested pass manager with the appropriate adaptor.
  286. MPM.addPass(
  287. createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
  288. } else if (PipelineText.startswith("function(")) {
  289. FunctionPassManager NestedFPM(DebugLogging);
  290. // Parse the inner pipeline inte the nested manager.
  291. PipelineText = PipelineText.substr(strlen("function("));
  292. if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
  293. DebugLogging) ||
  294. PipelineText.empty())
  295. return false;
  296. assert(PipelineText[0] == ')');
  297. PipelineText = PipelineText.substr(1);
  298. // Add the nested pass manager with the appropriate adaptor.
  299. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
  300. } else {
  301. // Otherwise try to parse a pass name.
  302. size_t End = PipelineText.find_first_of(",)");
  303. if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
  304. return false;
  305. if (VerifyEachPass)
  306. MPM.addPass(VerifierPass());
  307. PipelineText = PipelineText.substr(End);
  308. }
  309. if (PipelineText.empty() || PipelineText[0] == ')')
  310. return true;
  311. assert(PipelineText[0] == ',');
  312. PipelineText = PipelineText.substr(1);
  313. }
  314. }
  315. // Primary pass pipeline description parsing routine.
  316. // FIXME: Should this routine accept a TargetMachine or require the caller to
  317. // pre-populate the analysis managers with target-specific stuff?
  318. bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
  319. StringRef PipelineText, bool VerifyEachPass,
  320. bool DebugLogging) {
  321. // By default, try to parse the pipeline as-if it were within an implicit
  322. // 'module(...)' pass pipeline. If this will parse at all, it needs to
  323. // consume the entire string.
  324. if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
  325. return PipelineText.empty();
  326. // This isn't parsable as a module pipeline, look for the end of a pass name
  327. // and directly drop down to that layer.
  328. StringRef FirstName =
  329. PipelineText.substr(0, PipelineText.find_first_of(",)"));
  330. assert(!isModulePassName(FirstName) &&
  331. "Already handled all module pipeline options.");
  332. // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
  333. // pipeline.
  334. if (isCGSCCPassName(FirstName)) {
  335. CGSCCPassManager CGPM(DebugLogging);
  336. if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
  337. DebugLogging) ||
  338. !PipelineText.empty())
  339. return false;
  340. MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
  341. return true;
  342. }
  343. // Similarly, if this looks like a Function pass, parse the whole thing as
  344. // a Function pipelien.
  345. if (isFunctionPassName(FirstName)) {
  346. FunctionPassManager FPM(DebugLogging);
  347. if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
  348. DebugLogging) ||
  349. !PipelineText.empty())
  350. return false;
  351. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  352. return true;
  353. }
  354. return false;
  355. }