blob: 5026bc2d9840cd50cc70eb2e4669827f2efff749 [file] [log] [blame]
Chandler Carruthc2c50cd2013-01-02 09:10:48 +00001//===--- IR/PrintModulePass.cpp - Module/Function Printer -----------------===//
Daniel Dunbarf4db3a52008-10-21 23:33:38 +00002//
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
14#include "llvm/Assembly/PrintModulePass.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Function.h"
16#include "llvm/IR/Module.h"
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000017#include "llvm/Pass.h"
David Greene9a6c9402010-01-05 01:30:18 +000018#include "llvm/Support/Debug.h"
Daniel Dunbar3b0da262008-10-22 03:25:22 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000020using namespace llvm;
21
22namespace {
23
Nick Lewycky6726b6d2009-10-25 06:33:48 +000024 class PrintModulePass : public ModulePass {
David Greene5c8aa952010-04-02 23:17:14 +000025 std::string Banner;
Daniel Dunbar3b0da262008-10-22 03:25:22 +000026 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000027 bool DeleteStream; // Delete the ostream in our dtor?
28 public:
29 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000030 PrintModulePass() : ModulePass(ID), Out(&dbgs()),
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000031 DeleteStream(false) {}
David Greene5c8aa952010-04-02 23:17:14 +000032 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
Owen Anderson90c579d2010-08-06 18:33:48 +000033 : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000034
35 ~PrintModulePass() {
36 if (DeleteStream) delete Out;
37 }
38
39 bool runOnModule(Module &M) {
David Greene5c8aa952010-04-02 23:17:14 +000040 (*Out) << Banner << M;
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000041 return false;
42 }
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 }
47 };
48
49 class PrintFunctionPass : public FunctionPass {
50 std::string Banner; // String to print before each function
Daniel Dunbar3b0da262008-10-22 03:25:22 +000051 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000052 bool DeleteStream; // Delete the ostream in our dtor?
53 public:
54 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000055 PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()),
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000056 DeleteStream(false) {}
Daniel Dunbar3b0da262008-10-22 03:25:22 +000057 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
Owen Anderson90c579d2010-08-06 18:33:48 +000058 : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000059
Dan Gohmanb85b31b2010-08-16 21:57:30 +000060 ~PrintFunctionPass() {
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000061 if (DeleteStream) delete Out;
62 }
63
64 // runOnFunction - This pass just prints a banner followed by the
65 // function as it's processed.
66 //
67 bool runOnFunction(Function &F) {
68 (*Out) << Banner << static_cast<Value&>(F);
69 return false;
70 }
71
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesAll();
74 }
75 };
Sergei Larin68b2faf2013-02-08 23:37:41 +000076
77 class PrintBasicBlockPass : public BasicBlockPass {
78 std::string Banner;
79 raw_ostream *Out; // raw_ostream to print on
80 bool DeleteStream; // Delete the ostream in our dtor?
81 public:
82 static char ID;
83 PrintBasicBlockPass() : BasicBlockPass(ID), Out(&dbgs()),
84 DeleteStream(false) {}
85 PrintBasicBlockPass(const std::string &B, raw_ostream *o, bool DS)
86 : BasicBlockPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
87
88 ~PrintBasicBlockPass() {
89 if (DeleteStream) delete Out;
90 }
91
92 bool runOnBasicBlock(BasicBlock &BB) {
93 (*Out) << Banner << BB;
94 return false;
95 }
96
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.setPreservesAll();
99 }
100 };
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000101}
102
103char PrintModulePass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000104INITIALIZE_PASS(PrintModulePass, "print-module",
Owen Andersonce665bd2010-10-07 22:25:06 +0000105 "Print module to stderr", false, false)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000106char PrintFunctionPass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000107INITIALIZE_PASS(PrintFunctionPass, "print-function",
Owen Andersonce665bd2010-10-07 22:25:06 +0000108 "Print function to stderr", false, false)
Sergei Larin68b2faf2013-02-08 23:37:41 +0000109char PrintBasicBlockPass::ID = 0;
110INITIALIZE_PASS(PrintBasicBlockPass, "print-bb",
111 "Print BB to stderr", false, false)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000112
113/// createPrintModulePass - Create and return a pass that writes the
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000114/// module to the specified raw_ostream.
115ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
David Greene5c8aa952010-04-02 23:17:14 +0000116 bool DeleteStream,
117 const std::string &Banner) {
118 return new PrintModulePass(Banner, OS, DeleteStream);
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000119}
120
121/// createPrintFunctionPass - Create and return a pass that prints
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000122/// functions to the specified raw_ostream as they are processed.
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000123FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000124 llvm::raw_ostream *OS,
125 bool DeleteStream) {
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000126 return new PrintFunctionPass(Banner, OS, DeleteStream);
127}
128
Sergei Larin68b2faf2013-02-08 23:37:41 +0000129/// createPrintBasicBlockPass - Create and return a pass that writes the
130/// BB to the specified raw_ostream.
131BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream *OS,
132 bool DeleteStream,
133 const std::string &Banner) {
134 return new PrintBasicBlockPass(Banner, OS, DeleteStream);
135}
136