annotate llvm/lib/IR/IRPrintingPasses.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 1d019706d866
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // PrintModulePass and PrintFunctionPass implementations.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "llvm/IR/IRPrintingPasses.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
14 #include "llvm/ADT/StringRef.h"
150
anatofuz
parents:
diff changeset
15 #include "llvm/IR/Function.h"
anatofuz
parents:
diff changeset
16 #include "llvm/IR/Module.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
17 #include "llvm/IR/PrintPasses.h"
150
anatofuz
parents:
diff changeset
18 #include "llvm/InitializePasses.h"
anatofuz
parents:
diff changeset
19 #include "llvm/Pass.h"
anatofuz
parents:
diff changeset
20 #include "llvm/Support/Debug.h"
anatofuz
parents:
diff changeset
21 #include "llvm/Support/raw_ostream.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
22
150
anatofuz
parents:
diff changeset
23 using namespace llvm;
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
anatofuz
parents:
diff changeset
26 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
anatofuz
parents:
diff changeset
27 bool ShouldPreserveUseListOrder)
anatofuz
parents:
diff changeset
28 : OS(OS), Banner(Banner),
anatofuz
parents:
diff changeset
29 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
anatofuz
parents:
diff changeset
32 if (llvm::isFunctionInPrintList("*")) {
anatofuz
parents:
diff changeset
33 if (!Banner.empty())
anatofuz
parents:
diff changeset
34 OS << Banner << "\n";
anatofuz
parents:
diff changeset
35 M.print(OS, nullptr, ShouldPreserveUseListOrder);
anatofuz
parents:
diff changeset
36 }
anatofuz
parents:
diff changeset
37 else {
anatofuz
parents:
diff changeset
38 bool BannerPrinted = false;
anatofuz
parents:
diff changeset
39 for(const auto &F : M.functions()) {
anatofuz
parents:
diff changeset
40 if (llvm::isFunctionInPrintList(F.getName())) {
anatofuz
parents:
diff changeset
41 if (!BannerPrinted && !Banner.empty()) {
anatofuz
parents:
diff changeset
42 OS << Banner << "\n";
anatofuz
parents:
diff changeset
43 BannerPrinted = true;
anatofuz
parents:
diff changeset
44 }
anatofuz
parents:
diff changeset
45 F.print(OS);
anatofuz
parents:
diff changeset
46 }
anatofuz
parents:
diff changeset
47 }
anatofuz
parents:
diff changeset
48 }
anatofuz
parents:
diff changeset
49 return PreservedAnalyses::all();
anatofuz
parents:
diff changeset
50 }
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
anatofuz
parents:
diff changeset
53 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
anatofuz
parents:
diff changeset
54 : OS(OS), Banner(Banner) {}
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 PreservedAnalyses PrintFunctionPass::run(Function &F,
anatofuz
parents:
diff changeset
57 FunctionAnalysisManager &) {
anatofuz
parents:
diff changeset
58 if (isFunctionInPrintList(F.getName())) {
anatofuz
parents:
diff changeset
59 if (forcePrintModuleIR())
anatofuz
parents:
diff changeset
60 OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
anatofuz
parents:
diff changeset
61 else
anatofuz
parents:
diff changeset
62 OS << Banner << '\n' << static_cast<Value &>(F);
anatofuz
parents:
diff changeset
63 }
anatofuz
parents:
diff changeset
64 return PreservedAnalyses::all();
anatofuz
parents:
diff changeset
65 }
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 namespace {
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 class PrintModulePassWrapper : public ModulePass {
anatofuz
parents:
diff changeset
70 PrintModulePass P;
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 public:
anatofuz
parents:
diff changeset
73 static char ID;
anatofuz
parents:
diff changeset
74 PrintModulePassWrapper() : ModulePass(ID) {}
anatofuz
parents:
diff changeset
75 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
anatofuz
parents:
diff changeset
76 bool ShouldPreserveUseListOrder)
anatofuz
parents:
diff changeset
77 : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 bool runOnModule(Module &M) override {
anatofuz
parents:
diff changeset
80 ModuleAnalysisManager DummyMAM;
anatofuz
parents:
diff changeset
81 P.run(M, DummyMAM);
anatofuz
parents:
diff changeset
82 return false;
anatofuz
parents:
diff changeset
83 }
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 void getAnalysisUsage(AnalysisUsage &AU) const override {
anatofuz
parents:
diff changeset
86 AU.setPreservesAll();
anatofuz
parents:
diff changeset
87 }
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 StringRef getPassName() const override { return "Print Module IR"; }
anatofuz
parents:
diff changeset
90 };
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 class PrintFunctionPassWrapper : public FunctionPass {
anatofuz
parents:
diff changeset
93 PrintFunctionPass P;
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 public:
anatofuz
parents:
diff changeset
96 static char ID;
anatofuz
parents:
diff changeset
97 PrintFunctionPassWrapper() : FunctionPass(ID) {}
anatofuz
parents:
diff changeset
98 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
anatofuz
parents:
diff changeset
99 : FunctionPass(ID), P(OS, Banner) {}
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 // This pass just prints a banner followed by the function as it's processed.
anatofuz
parents:
diff changeset
102 bool runOnFunction(Function &F) override {
anatofuz
parents:
diff changeset
103 FunctionAnalysisManager DummyFAM;
anatofuz
parents:
diff changeset
104 P.run(F, DummyFAM);
anatofuz
parents:
diff changeset
105 return false;
anatofuz
parents:
diff changeset
106 }
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 void getAnalysisUsage(AnalysisUsage &AU) const override {
anatofuz
parents:
diff changeset
109 AU.setPreservesAll();
anatofuz
parents:
diff changeset
110 }
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 StringRef getPassName() const override { return "Print Function IR"; }
anatofuz
parents:
diff changeset
113 };
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 }
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 char PrintModulePassWrapper::ID = 0;
anatofuz
parents:
diff changeset
118 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
anatofuz
parents:
diff changeset
119 "Print module to stderr", false, true)
anatofuz
parents:
diff changeset
120 char PrintFunctionPassWrapper::ID = 0;
anatofuz
parents:
diff changeset
121 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
anatofuz
parents:
diff changeset
122 "Print function to stderr", false, true)
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
anatofuz
parents:
diff changeset
125 const std::string &Banner,
anatofuz
parents:
diff changeset
126 bool ShouldPreserveUseListOrder) {
anatofuz
parents:
diff changeset
127 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
anatofuz
parents:
diff changeset
128 }
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
anatofuz
parents:
diff changeset
131 const std::string &Banner) {
anatofuz
parents:
diff changeset
132 return new PrintFunctionPassWrapper(OS, Banner);
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 bool llvm::isIRPrintingPass(Pass *P) {
anatofuz
parents:
diff changeset
136 const char *PID = (const char*)P->getPassID();
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 return (PID == &PrintModulePassWrapper::ID) ||
anatofuz
parents:
diff changeset
139 (PID == &PrintFunctionPassWrapper::ID);
anatofuz
parents:
diff changeset
140 }