LLVM  10.0.0svn
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.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/PassManager.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/Debug.h"
20 using namespace llvm;
21 
23 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
24  bool ShouldPreserveUseListOrder)
25  : OS(OS), Banner(Banner),
26  ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
27 
29  if (llvm::isFunctionInPrintList("*")) {
30  if (!Banner.empty())
31  OS << Banner << "\n";
32  M.print(OS, nullptr, ShouldPreserveUseListOrder);
33  }
34  else {
35  bool BannerPrinted = false;
36  for(const auto &F : M.functions()) {
37  if (llvm::isFunctionInPrintList(F.getName())) {
38  if (!BannerPrinted && !Banner.empty()) {
39  OS << Banner << "\n";
40  BannerPrinted = true;
41  }
42  F.print(OS);
43  }
44  }
45  }
46  return PreservedAnalyses::all();
47 }
48 
50 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
51  : OS(OS), Banner(Banner) {}
52 
55  if (isFunctionInPrintList(F.getName())) {
56  if (forcePrintModuleIR())
57  OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
58  else
59  OS << Banner << static_cast<Value &>(F);
60  }
61  return PreservedAnalyses::all();
62 }
63 
64 namespace {
65 
66 class PrintModulePassWrapper : public ModulePass {
68 
69 public:
70  static char ID;
71  PrintModulePassWrapper() : ModulePass(ID) {}
72  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
73  bool ShouldPreserveUseListOrder)
74  : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
75 
76  bool runOnModule(Module &M) override {
77  ModuleAnalysisManager DummyMAM;
78  P.run(M, DummyMAM);
79  return false;
80  }
81 
82  void getAnalysisUsage(AnalysisUsage &AU) const override {
83  AU.setPreservesAll();
84  }
85 
86  StringRef getPassName() const override { return "Print Module IR"; }
87 };
88 
89 class PrintFunctionPassWrapper : public FunctionPass {
91 
92 public:
93  static char ID;
94  PrintFunctionPassWrapper() : FunctionPass(ID) {}
95  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
96  : FunctionPass(ID), P(OS, Banner) {}
97 
98  // This pass just prints a banner followed by the function as it's processed.
99  bool runOnFunction(Function &F) override {
100  FunctionAnalysisManager DummyFAM;
101  P.run(F, DummyFAM);
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 class PrintBasicBlockPass : public BasicBlockPass {
113  raw_ostream &Out;
114  std::string Banner;
115 
116 public:
117  static char ID;
118  PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
119  PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
120  : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
121 
122  bool runOnBasicBlock(BasicBlock &BB) override {
123  Out << Banner << BB;
124  return false;
125  }
126 
127  void getAnalysisUsage(AnalysisUsage &AU) const override {
128  AU.setPreservesAll();
129  }
130 
131  StringRef getPassName() const override { return "Print BasicBlock IR"; }
132 };
133 
134 }
135 
137 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
138  "Print module to stderr", false, true)
140 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
141  "Print function to stderr", false, true)
142 char PrintBasicBlockPass::ID = 0;
143 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
144  true)
145 
147  const std::string &Banner,
148  bool ShouldPreserveUseListOrder) {
149  return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
150 }
151 
153  const std::string &Banner) {
154  return new PrintFunctionPassWrapper(OS, Banner);
155 }
156 
158  const std::string &Banner) {
159  return new PrintBasicBlockPass(OS, Banner);
160 }
161 
163  const char *PID = (const char*)P->getPassID();
164 
165  return (PID == &PrintModulePassWrapper::ID)
166  || (PID == &PrintFunctionPassWrapper::ID)
167  || (PID == &PrintBasicBlockPass::ID);
168 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
bool forcePrintModuleIR()
forcePrintModuleIR - returns true if IR printing passes should
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
Pass for printing a Module as LLVM&#39;s text IR assembly.
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...
F(f)
Definition: BitVector.h:937
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
static bool runOnFunction(Function &F, bool PostInlining)
#define P(N)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
iterator_range< iterator > functions()
Definition: Module.h:610
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:99
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
INITIALIZE_PASS(PrintModulePassWrapper, "print-module", "Print module to stderr", false, true) char PrintFunctionPassWrapper INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function", "Print function to stderr", false, true) char PrintBasicBlockPass INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false, true) ModulePass *llvm
Deprecated - do not create new passes as BasicBlockPasses.
Definition: Pass.h:321
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the module to an output stream with an optional AssemblyAnnotationWriter.
Definition: AsmWriter.cpp:4196
static bool runOnBasicBlock(MachineBasicBlock *MBB, std::vector< StringRef > &bbNames, unsigned &basicBlockNum, NamedVRegCursor &NVC)
Module.h This file contains the declarations for the Module class.
BasicBlockPass * createPrintBasicBlockPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that writes the BB to the specified raw_ostream.
bool isFunctionInPrintList(StringRef FunctionName)
isFunctionInPrintList - returns true if a function should be printed via
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void setPreservesAll()
Set by analyses that do not transform their input at all.
PreservedAnalyses run(Module &M, AnalysisManager< Module > &)
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
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.
This file defines passes to print out IR in various granularities.
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
PreservedAnalyses run(Function &F, AnalysisManager< Function > &)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
Pass for printing a Function as LLVM&#39;s text IR assembly.