PrintPasses.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===- PrintPasses.cpp ----------------------------------------------------===//
  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. #include "llvm/IR/PrintPasses.h"
  9. #include "llvm/Support/CommandLine.h"
  10. #include <unordered_set>
  11. using namespace llvm;
  12. // Print IR out before/after specified passes.
  13. static cl::list<std::string>
  14. PrintBefore("print-before",
  15. llvm::cl::desc("Print IR before specified passes"),
  16. cl::CommaSeparated, cl::Hidden);
  17. static cl::list<std::string>
  18. PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"),
  19. cl::CommaSeparated, cl::Hidden);
  20. static cl::opt<bool> PrintBeforeAll("print-before-all",
  21. llvm::cl::desc("Print IR before each pass"),
  22. cl::init(false), cl::Hidden);
  23. static cl::opt<bool> PrintAfterAll("print-after-all",
  24. llvm::cl::desc("Print IR after each pass"),
  25. cl::init(false), cl::Hidden);
  26. static cl::opt<bool>
  27. PrintModuleScope("print-module-scope",
  28. cl::desc("When printing IR for print-[before|after]{-all} "
  29. "always print a module IR"),
  30. cl::init(false), cl::Hidden);
  31. static cl::list<std::string>
  32. PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
  33. cl::desc("Only print IR for functions whose name "
  34. "match this for all print-[before|after][-all] "
  35. "options"),
  36. cl::CommaSeparated, cl::Hidden);
  37. /// This is a helper to determine whether to print IR before or
  38. /// after a pass.
  39. bool llvm::shouldPrintBeforeSomePass() {
  40. return PrintBeforeAll || !PrintBefore.empty();
  41. }
  42. bool llvm::shouldPrintAfterSomePass() {
  43. return PrintAfterAll || !PrintAfter.empty();
  44. }
  45. static bool shouldPrintBeforeOrAfterPass(StringRef PassID,
  46. ArrayRef<std::string> PassesToPrint) {
  47. return llvm::is_contained(PassesToPrint, PassID);
  48. }
  49. bool llvm::shouldPrintBeforeAll() { return PrintBeforeAll; }
  50. bool llvm::shouldPrintAfterAll() { return PrintAfterAll; }
  51. bool llvm::shouldPrintBeforePass(StringRef PassID) {
  52. return PrintBeforeAll || shouldPrintBeforeOrAfterPass(PassID, PrintBefore);
  53. }
  54. bool llvm::shouldPrintAfterPass(StringRef PassID) {
  55. return PrintAfterAll || shouldPrintBeforeOrAfterPass(PassID, PrintAfter);
  56. }
  57. std::vector<std::string> llvm::printBeforePasses() {
  58. return std::vector<std::string>(PrintBefore);
  59. }
  60. std::vector<std::string> llvm::printAfterPasses() {
  61. return std::vector<std::string>(PrintAfter);
  62. }
  63. bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
  64. bool llvm::isFunctionInPrintList(StringRef FunctionName) {
  65. static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
  66. PrintFuncsList.end());
  67. return PrintFuncNames.empty() ||
  68. PrintFuncNames.count(std::string(FunctionName));
  69. }