LLVM 24.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"
22#include "llvm/Support/Debug.h"
24
25using namespace llvm;
26
27namespace {
28
29static void printModule(raw_ostream &OS, StringRef Banner,
30 bool ShouldPreserveUseListOrder, Module &M) {
32 if (!Banner.empty())
33 OS << Banner << "\n";
34 M.print(OS, nullptr, ShouldPreserveUseListOrder);
35 return;
36 }
37
38 bool BannerPrinted = false;
39 for (const auto &F : M.functions()) {
41 continue;
42 if (!BannerPrinted && !Banner.empty()) {
43 OS << Banner << "\n";
44 BannerPrinted = true;
45 }
46 F.print(OS);
47 }
48}
49
50class PrintModulePassWrapper : public ModulePass {
51 raw_ostream &OS;
52 std::string Banner;
53 bool ShouldPreserveUseListOrder;
54 bool ShouldRenumberMetadata;
55
56public:
57 static char ID;
58 PrintModulePassWrapper()
59 : ModulePass(ID), OS(dbgs()), ShouldPreserveUseListOrder(false),
60 ShouldRenumberMetadata(false) {}
61 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
62 bool ShouldPreserveUseListOrder,
63 bool ShouldRenumberMetadata)
64 : ModulePass(ID), OS(OS), Banner(Banner),
65 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
66 ShouldRenumberMetadata(ShouldRenumberMetadata) {}
67
68 bool runOnModule(Module &M) override {
69 if (ShouldRenumberMetadata)
70 M.renumberMetadataForAssembly();
71 printModule(OS, Banner, ShouldPreserveUseListOrder, M);
72 return false;
73 }
74
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
76 AU.setPreservesAll();
77 }
78
79 StringRef getPassName() const override { return "Print Module IR"; }
80};
81
82class PrintFunctionPassWrapper : public FunctionPass {
83 raw_ostream &OS;
84 std::string Banner;
85
86public:
87 static char ID;
88 PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
89 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
90 : FunctionPass(ID), OS(OS), Banner(Banner) {}
91
92 // This pass just prints a banner followed by the function as it's processed.
93 bool runOnFunction(Function &F) override {
95 if (forcePrintModuleIR()) {
96 OS << Banner << " (function: " << F.getName() << ")\n";
97 F.getParent()->print(OS, nullptr);
98 } else
99 OS << Banner << '\n' << static_cast<Value &>(F);
100 }
101
102 return false;
103 }
104
105 void getAnalysisUsage(AnalysisUsage &AU) const override {
106 AU.setPreservesAll();
107 }
108
109 StringRef getPassName() const override { return "Print Function IR"; }
110};
111
112} // namespace
113
114char PrintModulePassWrapper::ID = 0;
115INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
116 "Print module to stderr", false, true)
117char PrintFunctionPassWrapper::ID = 0;
118INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
119 "Print function to stderr", false, true)
120
122 const std::string &Banner,
123 bool ShouldPreserveUseListOrder) {
124 return createPrintModulePass(OS, Banner, ShouldPreserveUseListOrder,
125 /*ShouldRenumberMetadata=*/false);
126}
127
129 const std::string &Banner,
130 bool ShouldPreserveUseListOrder,
131 bool ShouldRenumberMetadata) {
132 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder,
133 ShouldRenumberMetadata);
134}
135
137 const std::string &Banner) {
138 return new PrintFunctionPassWrapper(OS, Banner);
139}
140
142 const char *PID = (const char *)P->getPassID();
143
144 return (PID == &PrintModulePassWrapper::ID) ||
145 (PID == &PrintFunctionPassWrapper::ID);
146}
aarch64 promote const
Function Alias Analysis false
static bool runOnFunction(Function &F, bool PostInlining)
This file contains an interface for creating legacy passes to print out IR in various granularities.
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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:314
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
TargetPassConfig.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool forcePrintModuleIR()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
LLVM_ABI bool shouldPrintFunction(const Function &F)
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI bool shouldPrintAllFunctions()
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878