LLVM 23.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 {
30 raw_ostream &OS;
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 {
44 if (!Banner.empty())
45 OS << Banner << "\n";
46 M.print(OS, nullptr, ShouldPreserveUseListOrder);
47 } else {
48 bool BannerPrinted = false;
49 for (const auto &F : M.functions()) {
50 if (llvm::isFunctionInPrintList(F.getName())) {
51 if (!BannerPrinted && !Banner.empty()) {
52 OS << Banner << "\n";
53 BannerPrinted = true;
54 }
55 F.print(OS);
56 }
57 }
58 }
59
60 return false;
61 }
62
63 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 AU.setPreservesAll();
65 }
66
67 StringRef getPassName() const override { return "Print Module IR"; }
68};
69
70class PrintFunctionPassWrapper : public FunctionPass {
71 raw_ostream &OS;
72 std::string Banner;
73
74public:
75 static char ID;
76 PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
77 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
78 : FunctionPass(ID), OS(OS), Banner(Banner) {}
79
80 // This pass just prints a banner followed by the function as it's processed.
81 bool runOnFunction(Function &F) override {
82 if (isFunctionInPrintList(F.getName())) {
84 OS << Banner << " (function: " << F.getName() << ")\n"
85 << *F.getParent();
86 else
87 OS << Banner << '\n' << static_cast<Value &>(F);
88 }
89
90 return false;
91 }
92
93 void getAnalysisUsage(AnalysisUsage &AU) const override {
94 AU.setPreservesAll();
95 }
96
97 StringRef getPassName() const override { return "Print Function IR"; }
98};
99
100} // namespace
101
102char PrintModulePassWrapper::ID = 0;
103INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
104 "Print module to stderr", false, true)
105char PrintFunctionPassWrapper::ID = 0;
106INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
107 "Print function to stderr", false, true)
108
110 const std::string &Banner,
111 bool ShouldPreserveUseListOrder) {
112 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
113}
114
116 const std::string &Banner) {
117 return new PrintFunctionPassWrapper(OS, Banner);
118}
119
121 const char *PID = (const char *)P->getPassID();
122
123 return (PID == &PrintModulePassWrapper::ID) ||
124 (PID == &PrintFunctionPassWrapper::ID);
125}
aarch64 promote const
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
Machine Check Debug Module
#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
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
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.
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:874