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