LLVM  6.0.0svn
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;
32  M.print(OS, nullptr, ShouldPreserveUseListOrder);
33  else {
34  for(const auto &F : M.functions())
35  if (llvm::isFunctionInPrintList(F.getName()))
36  F.print(OS);
37  }
38  return PreservedAnalyses::all();
39 }
40 
42 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
43  : OS(OS), Banner(Banner) {}
44 
48  OS << Banner << static_cast<Value &>(F);
49  return PreservedAnalyses::all();
50 }
51 
52 namespace {
53 
54 class PrintModulePassWrapper : public ModulePass {
56 
57 public:
58  static char ID;
59  PrintModulePassWrapper() : ModulePass(ID) {}
60  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
61  bool ShouldPreserveUseListOrder)
62  : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
63 
64  bool runOnModule(Module &M) override {
65  ModuleAnalysisManager DummyMAM;
66  P.run(M, DummyMAM);
67  return false;
68  }
69 
70  void getAnalysisUsage(AnalysisUsage &AU) const override {
71  AU.setPreservesAll();
72  }
73 
74  StringRef getPassName() const override { return "Print Module IR"; }
75 };
76 
77 class PrintFunctionPassWrapper : public FunctionPass {
79 
80 public:
81  static char ID;
82  PrintFunctionPassWrapper() : FunctionPass(ID) {}
83  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
84  : FunctionPass(ID), P(OS, Banner) {}
85 
86  // This pass just prints a banner followed by the function as it's processed.
87  bool runOnFunction(Function &F) override {
88  FunctionAnalysisManager DummyFAM;
89  P.run(F, DummyFAM);
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 class PrintBasicBlockPass : public BasicBlockPass {
101  raw_ostream &Out;
102  std::string Banner;
103 
104 public:
105  static char ID;
106  PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
107  PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
108  : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
109 
110  bool runOnBasicBlock(BasicBlock &BB) override {
111  Out << Banner << BB;
112  return false;
113  }
114 
115  void getAnalysisUsage(AnalysisUsage &AU) const override {
116  AU.setPreservesAll();
117  }
118 
119  StringRef getPassName() const override { return "Print BasicBlock IR"; }
120 };
121 
122 }
123 
125 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
126  "Print module to stderr", false, false)
128 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
129  "Print function to stderr", false, false)
130 char PrintBasicBlockPass::ID = 0;
131 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
132  false)
133 
135  const std::string &Banner,
136  bool ShouldPreserveUseListOrder) {
137  return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
138 }
139 
141  const std::string &Banner) {
142  return new PrintFunctionPassWrapper(OS, Banner);
143 }
144 
146  const std::string &Banner) {
147  return new PrintBasicBlockPass(OS, Banner);
148 }
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:920
static bool runOnFunction(Function &F, bool PostInlining)
#define P(N)
static bool runOnBasicBlock(MachineBasicBlock *MBB, std::vector< StringRef > &bbNames, std::vector< unsigned > &renamedInOtherBB, unsigned &basicBlockNum, unsigned &VRegGapIndex)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
iterator_range< iterator > functions()
Definition: Module.h:583
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
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:159
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:3411
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.
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
PreservedAnalyses run(Module &M, AnalysisManager< Module > &)
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
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
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:44
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.