LLVM 22.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
29class PrintModulePassWrapper : public ModulePass {
31 std::string Banner;
32 bool ShouldPreserveUseListOrder;
33
34public:
35 static char ID;
36 PrintModulePassWrapper() : ModulePass(ID), OS(dbgs()) {}
37 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
38 bool ShouldPreserveUseListOrder)
39 : ModulePass(ID), OS(OS), Banner(Banner),
40 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
41
42 bool runOnModule(Module &M) override {
43 // Remove intrinsic declarations when printing in the new format.
44 // TODO: consider removing this as debug-intrinsics are gone.
45 M.removeDebugIntrinsicDeclarations();
46
48 if (!Banner.empty())
49 OS << Banner << "\n";
50 M.print(OS, nullptr, ShouldPreserveUseListOrder);
51 } else {
52 bool BannerPrinted = false;
53 for (const auto &F : M.functions()) {
54 if (llvm::isFunctionInPrintList(F.getName())) {
55 if (!BannerPrinted && !Banner.empty()) {
56 OS << Banner << "\n";
57 BannerPrinted = true;
58 }
59 F.print(OS);
60 }
61 }
62 }
63
64 return false;
65 }
66
67 void getAnalysisUsage(AnalysisUsage &AU) const override {
68 AU.setPreservesAll();
69 }
70
71 StringRef getPassName() const override { return "Print Module IR"; }
72};
73
74class PrintFunctionPassWrapper : public FunctionPass {
76 std::string Banner;
77
78public:
79 static char ID;
80 PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
81 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
82 : FunctionPass(ID), OS(OS), Banner(Banner) {}
83
84 // This pass just prints a banner followed by the function as it's processed.
85 bool runOnFunction(Function &F) override {
86 if (isFunctionInPrintList(F.getName())) {
88 OS << Banner << " (function: " << F.getName() << ")\n"
89 << *F.getParent();
90 else
91 OS << Banner << '\n' << static_cast<Value &>(F);
92 }
93
94 return false;
95 }
96
97 void getAnalysisUsage(AnalysisUsage &AU) const override {
98 AU.setPreservesAll();
99 }
100
101 StringRef getPassName() const override { return "Print Function IR"; }
102};
103
104} // namespace
105
106char PrintModulePassWrapper::ID = 0;
107INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
108 "Print module to stderr", false, true)
109char PrintFunctionPassWrapper::ID = 0;
110INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
111 "Print function to stderr", false, true)
112
114 const std::string &Banner,
115 bool ShouldPreserveUseListOrder) {
116 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
117}
118
120 const std::string &Banner) {
121 return new PrintFunctionPassWrapper(OS, Banner);
122}
123
125 const char *PID = (const char *)P->getPassID();
126
127 return (PID == &PrintModulePassWrapper::ID) ||
128 (PID == &PrintFunctionPassWrapper::ID);
129}
aarch64 promote const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Performs the initial survey of the specified function
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:55
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
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:314
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:255
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:67
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:112
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
TargetPassConfig.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool forcePrintModuleIR()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
bool isFunctionInPrintList(StringRef FunctionName)
LLVM_ABI bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
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.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856