LLVM  3.7.0
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  OS << Banner;
31  M.print(OS, nullptr, ShouldPreserveUseListOrder);
32  return PreservedAnalyses::all();
33 }
34 
36 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
37  : OS(OS), Banner(Banner) {}
38 
40  OS << Banner << static_cast<Value &>(F);
41  return PreservedAnalyses::all();
42 }
43 
44 namespace {
45 
46 class PrintModulePassWrapper : public ModulePass {
48 
49 public:
50  static char ID;
51  PrintModulePassWrapper() : ModulePass(ID) {}
52  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
53  bool ShouldPreserveUseListOrder)
54  : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
55 
56  bool runOnModule(Module &M) override {
57  P.run(M);
58  return false;
59  }
60 
61  void getAnalysisUsage(AnalysisUsage &AU) const override {
62  AU.setPreservesAll();
63  }
64 };
65 
66 class PrintFunctionPassWrapper : public FunctionPass {
68 
69 public:
70  static char ID;
71  PrintFunctionPassWrapper() : FunctionPass(ID) {}
72  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
73  : FunctionPass(ID), P(OS, Banner) {}
74 
75  // This pass just prints a banner followed by the function as it's processed.
76  bool runOnFunction(Function &F) override {
77  P.run(F);
78  return false;
79  }
80 
81  void getAnalysisUsage(AnalysisUsage &AU) const override {
82  AU.setPreservesAll();
83  }
84 };
85 
86 class PrintBasicBlockPass : public BasicBlockPass {
87  raw_ostream &Out;
88  std::string Banner;
89 
90 public:
91  static char ID;
92  PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
93  PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
94  : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
95 
96  bool runOnBasicBlock(BasicBlock &BB) override {
97  Out << Banner << BB;
98  return false;
99  }
100 
101  void getAnalysisUsage(AnalysisUsage &AU) const override {
102  AU.setPreservesAll();
103  }
104 };
105 
106 }
107 
109 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
110  "Print module to stderr", false, false)
111 char PrintFunctionPassWrapper::ID = 0;
112 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
113  "Print function to stderr", false, false)
114 char PrintBasicBlockPass::ID = 0;
115 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
116  false)
117 
119  const std::string &Banner,
120  bool ShouldPreserveUseListOrder) {
121  return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
122 }
123 
125  const std::string &Banner) {
126  return new PrintFunctionPassWrapper(OS, Banner);
127 }
128 
130  const std::string &Banner) {
131  return new PrintBasicBlockPass(OS, Banner);
132 }
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
Pass for printing a Module as LLVM's text IR assembly.
void Print(const Unit &U, const char *PrintAfter="")
Definition: FuzzerUtil.cpp:23
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)
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW, bool ShouldPreserveUseListOrder=false) const
Print the module to an output stream with an optional AssemblyAnnotationWriter.
Definition: AsmWriter.cpp:3146
#define false
Definition: ConvertUTF.c:65
#define P(N)
An abstract set of preserved analyses following a transformation pass run.
Definition: PassManager.h:69
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:91
BasicBlockPass class - This class is used to implement most local optimizations.
Definition: Pass.h:330
Module.h This file contains the declarations for the Module class.
PreservedAnalyses run(Module &M)
BasicBlockPass * createPrintBasicBlockPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that writes the BB to the specified raw_ostream.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
void setPreservesAll()
Set by analyses that do not transform their input at all.
PreservedAnalyses run(Function &F)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
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
INITIALIZE_PASS(PrintModulePassWrapper,"print-module","Print module to stderr", false, false) char PrintFunctionPassWrapper INITIALIZE_PASS(PrintFunctionPassWrapper,"print-function","Print function to stderr", false, false) char PrintBasicBlockPass INITIALIZE_PASS(PrintBasicBlockPass,"print-bb","Print BB to stderr", false, false) ModulePass *llvm
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
print Print MemDeps of function
This header defines various interfaces for pass management in LLVM.
Pass for printing a Function as LLVM's text IR assembly.