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
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/Compiler.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.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 {
43 if (llvm::isFunctionInPrintList(FunctionName: "*")) {
44 if (!Banner.empty())
45 OS << Banner << "\n";
46 M.print(OS, AAW: nullptr, ShouldPreserveUseListOrder);
47 } else {
48 bool BannerPrinted = false;
49 for (const auto &F : M.functions()) {
50 if (llvm::isFunctionInPrintList(FunctionName: 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(FunctionName: F.getName())) {
83 if (forcePrintModuleIR())
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
109ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
110 const std::string &Banner,
111 bool ShouldPreserveUseListOrder) {
112 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
113}
114
115FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
116 const std::string &Banner) {
117 return new PrintFunctionPassWrapper(OS, Banner);
118}
119
120bool llvm::isIRPrintingPass(Pass *P) {
121 const char *PID = (const char *)P->getPassID();
122
123 return (PID == &PrintModulePassWrapper::ID) ||
124 (PID == &PrintFunctionPassWrapper::ID);
125}
126