1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
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 // PrintModulePass and PrintFunctionPass implementations for the legacy pass
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/IRPrintingPasses.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PrintPasses.h"
19 #include "llvm/InitializePasses.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 cl::opt
<bool> WriteNewDbgInfoFormat(
27 "write-experimental-debuginfo",
28 cl::desc("Write debug info in the new non-intrinsic format. Has no effect "
29 "if --preserve-input-debuginfo-format=true."),
34 class PrintModulePassWrapper
: public ModulePass
{
37 bool ShouldPreserveUseListOrder
;
41 PrintModulePassWrapper() : ModulePass(ID
), OS(dbgs()) {}
42 PrintModulePassWrapper(raw_ostream
&OS
, const std::string
&Banner
,
43 bool ShouldPreserveUseListOrder
)
44 : ModulePass(ID
), OS(OS
), Banner(Banner
),
45 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
) {}
47 bool runOnModule(Module
&M
) override
{
48 // RemoveDIs: Regardless of the format we've processed this module in, use
49 // `WriteNewDbgInfoFormat` to determine which format we use to write it.
50 ScopedDbgInfoFormatSetter
FormatSetter(M
, WriteNewDbgInfoFormat
);
51 // Remove intrinsic declarations when printing in the new format.
52 // TODO: Move this into Module::setIsNewDbgInfoFormat when we're ready to
53 // update test output.
54 if (WriteNewDbgInfoFormat
)
55 M
.removeDebugIntrinsicDeclarations();
57 if (llvm::isFunctionInPrintList("*")) {
60 M
.print(OS
, nullptr, ShouldPreserveUseListOrder
);
62 bool BannerPrinted
= false;
63 for (const auto &F
: M
.functions()) {
64 if (llvm::isFunctionInPrintList(F
.getName())) {
65 if (!BannerPrinted
&& !Banner
.empty()) {
77 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
81 StringRef
getPassName() const override
{ return "Print Module IR"; }
84 class PrintFunctionPassWrapper
: public FunctionPass
{
90 PrintFunctionPassWrapper() : FunctionPass(ID
), OS(dbgs()) {}
91 PrintFunctionPassWrapper(raw_ostream
&OS
, const std::string
&Banner
)
92 : FunctionPass(ID
), OS(OS
), Banner(Banner
) {}
94 // This pass just prints a banner followed by the function as it's processed.
95 bool runOnFunction(Function
&F
) override
{
96 // RemoveDIs: Regardless of the format we've processed this function in, use
97 // `WriteNewDbgInfoFormat` to determine which format we use to write it.
98 ScopedDbgInfoFormatSetter
FormatSetter(F
, WriteNewDbgInfoFormat
);
100 if (isFunctionInPrintList(F
.getName())) {
101 if (forcePrintModuleIR())
102 OS
<< Banner
<< " (function: " << F
.getName() << ")\n"
105 OS
<< Banner
<< '\n' << static_cast<Value
&>(F
);
111 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
112 AU
.setPreservesAll();
115 StringRef
getPassName() const override
{ return "Print Function IR"; }
120 char PrintModulePassWrapper::ID
= 0;
121 INITIALIZE_PASS(PrintModulePassWrapper
, "print-module",
122 "Print module to stderr", false, true)
123 char PrintFunctionPassWrapper::ID
= 0;
124 INITIALIZE_PASS(PrintFunctionPassWrapper
, "print-function",
125 "Print function to stderr", false, true)
127 ModulePass
*llvm::createPrintModulePass(llvm::raw_ostream
&OS
,
128 const std::string
&Banner
,
129 bool ShouldPreserveUseListOrder
) {
130 return new PrintModulePassWrapper(OS
, Banner
, ShouldPreserveUseListOrder
);
133 FunctionPass
*llvm::createPrintFunctionPass(llvm::raw_ostream
&OS
,
134 const std::string
&Banner
) {
135 return new PrintFunctionPassWrapper(OS
, Banner
);
138 bool llvm::isIRPrintingPass(Pass
*P
) {
139 const char *PID
= (const char *)P
->getPassID();
141 return (PID
== &PrintModulePassWrapper::ID
) ||
142 (PID
== &PrintFunctionPassWrapper::ID
);