1 //===- PrintPasses.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/PrintPasses.h"
10 #include "llvm/Support/CommandLine.h"
11 #include <unordered_set>
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
);
21 static cl::list
<std::string
>
22 PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"),
23 cl::CommaSeparated
, cl::Hidden
);
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
);
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
);
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] "
43 cl::CommaSeparated
, cl::Hidden
);
45 /// This is a helper to determine whether to print IR before or
48 bool llvm::shouldPrintBeforeSomePass() {
49 return PrintBeforeAll
|| !PrintBefore
.empty();
52 bool llvm::shouldPrintAfterSomePass() {
53 return PrintAfterAll
|| !PrintAfter
.empty();
56 static bool shouldPrintBeforeOrAfterPass(StringRef PassID
,
57 ArrayRef
<std::string
> PassesToPrint
) {
58 return llvm::is_contained(PassesToPrint
, PassID
);
61 bool llvm::shouldPrintBeforeAll() { return PrintBeforeAll
; }
63 bool llvm::shouldPrintAfterAll() { return PrintAfterAll
; }
65 bool llvm::shouldPrintBeforePass(StringRef PassID
) {
66 return PrintBeforeAll
|| shouldPrintBeforeOrAfterPass(PassID
, PrintBefore
);
69 bool llvm::shouldPrintAfterPass(StringRef PassID
) {
70 return PrintAfterAll
|| shouldPrintBeforeOrAfterPass(PassID
, PrintAfter
);
73 std::vector
<std::string
> llvm::printBeforePasses() {
74 return std::vector
<std::string
>(PrintBefore
);
77 std::vector
<std::string
> llvm::printAfterPasses() {
78 return std::vector
<std::string
>(PrintAfter
);
81 bool llvm::forcePrintModuleIR() { return PrintModuleScope
; }
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
));