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