LLVM  8.0.1
IRPrintingPasses.cpp
Go to the documentation of this file.
1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // PrintModulePass and PrintFunctionPass implementations.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
21 using namespace llvm;
22 
24 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
25  bool ShouldPreserveUseListOrder)
26  : OS(OS), Banner(Banner),
27  ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
28 
30  if (!Banner.empty())
31  OS << Banner << "\n";
33  M.print(OS, nullptr, ShouldPreserveUseListOrder);
34  else {
35  for(const auto &F : M.functions())
36  if (llvm::isFunctionInPrintList(F.getName()))
37  F.print(OS);
38  }
39  return PreservedAnalyses::all();
40 }
41 
43 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
44  : OS(OS), Banner(Banner) {}
45 
48  if (isFunctionInPrintList(F.getName())) {
49  if (forcePrintModuleIR())
50  OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
51  else
52  OS << Banner << static_cast<Value &>(F);
53  }
54  return PreservedAnalyses::all();
55 }
56 
57 namespace {
58 
59 class PrintModulePassWrapper : public ModulePass {
61 
62 public:
63  static char ID;
64  PrintModulePassWrapper() : ModulePass(ID) {}
65  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
66  bool ShouldPreserveUseListOrder)
67  : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
68 
69  bool runOnModule(Module &M) override {
70  ModuleAnalysisManager DummyMAM;
71  P.run(M, DummyMAM);
72  return false;
73  }
74 
75  void getAnalysisUsage(AnalysisUsage &AU) const override {
76  AU.setPreservesAll();
77  }
78 
79  StringRef getPassName() const override { return "Print Module IR"; }
80 };
81 
82 class PrintFunctionPassWrapper : public FunctionPass {
84 
85 public:
86  static char ID;
87  PrintFunctionPassWrapper() : FunctionPass(ID) {}
88  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
89  : FunctionPass(ID), P(OS, Banner) {}
90 
91  // This pass just prints a banner followed by the function as it's processed.
92  bool runOnFunction(Function &F) override {
93  FunctionAnalysisManager DummyFAM;
94  P.run(F, DummyFAM);
95  return false;
96  }
97 
98  void getAnalysisUsage(AnalysisUsage &AU) const override {
99  AU.setPreservesAll();
100  }
101 
102  StringRef getPassName() const override { return "Print Function IR"; }
103 };
104 
105 class PrintBasicBlockPass : public BasicBlockPass {
106  raw_ostream &Out;
107  std::string Banner;
108 
109 public:
110  static char ID;
111  PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
112  PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
113  : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
114 
115  bool runOnBasicBlock(BasicBlock &BB) override {
116  Out << Banner << BB;
117  return false;
118  }
119 
120  void getAnalysisUsage(AnalysisUsage &AU) const override {
121  AU.setPreservesAll();
122  }
123 
124  StringRef getPassName() const override { return "Print BasicBlock IR"; }
125 };
126 
127 }
128 
130 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
131  "Print module to stderr", false, true)
133 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
134  "Print function to stderr", false, true)
135 char PrintBasicBlockPass::ID = 0;
136 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
137  true)
138 
140  const std::string &Banner,
141  bool ShouldPreserveUseListOrder) {
142  return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
143 }
144 
146  const std::string &Banner) {
147  return new PrintFunctionPassWrapper(OS, Banner);
148 }
149 
151  const std::string &Banner) {
152  return new PrintBasicBlockPass(OS, Banner);
153 }
154 
156  const char *PID = (const char*)P->getPassID();
157 
158  return (PID == &PrintModulePassWrapper::ID)
159  || (PID == &PrintFunctionPassWrapper::ID)
160  || (PID == &PrintBasicBlockPass::ID);
161 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
bool forcePrintModuleIR()
forcePrintModuleIR - returns true if IR printing passes should
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
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:938
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:606
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:100
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
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
BasicBlockPass class - This class is used to implement most local optimizations.
Definition: Pass.h:319
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:4065
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:133
static bool runOnBasicBlock(MachineBasicBlock *MBB, std::vector< StringRef > &bbNames, std::vector< unsigned > &renamedInOtherBB, unsigned &basicBlockNum, unsigned &VRegGapIndex, NamedVRegCursor &NVC)
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:225
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:566
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:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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.