LLVM 19.0.0git
IRPrintingPasses.cpp
Go to the documentation of this file.
1//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
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// PrintModulePass and PrintFunctionPass implementations for the legacy pass
10// manager.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/StringRef.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/PrintPasses.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/Debug.h"
23
24using namespace llvm;
25
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."),
30 cl::init(true));
31
32namespace {
33
34class PrintModulePassWrapper : public ModulePass {
36 std::string Banner;
37 bool ShouldPreserveUseListOrder;
38
39public:
40 static char ID;
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) {}
46
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.
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.
55 M.removeDebugIntrinsicDeclarations();
56
58 if (!Banner.empty())
59 OS << Banner << "\n";
60 M.print(OS, nullptr, ShouldPreserveUseListOrder);
61 } else {
62 bool BannerPrinted = false;
63 for (const auto &F : M.functions()) {
64 if (llvm::isFunctionInPrintList(F.getName())) {
65 if (!BannerPrinted && !Banner.empty()) {
66 OS << Banner << "\n";
67 BannerPrinted = true;
68 }
69 F.print(OS);
70 }
71 }
72 }
73
74 return false;
75 }
76
77 void getAnalysisUsage(AnalysisUsage &AU) const override {
78 AU.setPreservesAll();
79 }
80
81 StringRef getPassName() const override { return "Print Module IR"; }
82};
83
84class PrintFunctionPassWrapper : public FunctionPass {
86 std::string Banner;
87
88public:
89 static char ID;
90 PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
91 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
92 : FunctionPass(ID), OS(OS), Banner(Banner) {}
93
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.
99
100 if (isFunctionInPrintList(F.getName())) {
101 if (forcePrintModuleIR())
102 OS << Banner << " (function: " << F.getName() << ")\n"
103 << *F.getParent();
104 else
105 OS << Banner << '\n' << static_cast<Value &>(F);
106 }
107
108 return false;
109 }
110
111 void getAnalysisUsage(AnalysisUsage &AU) const override {
112 AU.setPreservesAll();
113 }
114
115 StringRef getPassName() const override { return "Print Function IR"; }
116};
117
118} // namespace
119
120char PrintModulePassWrapper::ID = 0;
121INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
122 "Print module to stderr", false, true)
123char PrintFunctionPassWrapper::ID = 0;
124INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
125 "Print function to stderr", false, true)
126
128 const std::string &Banner,
129 bool ShouldPreserveUseListOrder) {
130 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
131}
132
134 const std::string &Banner) {
135 return new PrintFunctionPassWrapper(OS, Banner);
136}
137
139 const char *PID = (const char *)P->getPassID();
140
141 return (PID == &PrintModulePassWrapper::ID) ||
142 (PID == &PrintFunctionPassWrapper::ID);
143}
aarch64 promote const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
basic Basic Alias true
Performs the initial survey of the specified function
cl::opt< bool > WriteNewDbgInfoFormat("write-experimental-debuginfo", cl::desc("Write debug info in the new non-intrinsic format. Has no effect " "if --preserve-input-debuginfo-format=true."), cl::init(true))
This file contains an interface for creating legacy passes to print out IR in various granularities.
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
raw_pwrite_stream & OS
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Used to temporarily set the debug info format of a function, module, or basic block for the duration ...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
TargetPassConfig.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool forcePrintModuleIR()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool isFunctionInPrintList(StringRef FunctionName)
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
ModulePass * createPrintModulePass(raw_ostream &OS, const std::string &Banner="", bool ShouldPreserveUseListOrder=false)
Create and return a pass that writes the module to the specified raw_ostream.
FunctionPass * createPrintFunctionPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that prints functions to the specified raw_ostream as they are processed.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858