blob: 7240fd05807f9d89320881d3224957c26d418cbc [file] [log] [blame]
Chandler Carruth12e9d2b2014-01-09 02:39:45 +00001//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
Daniel Dunbar54d5b9e2008-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
Chandler Carruthb8ddc702014-01-12 11:10:32 +000014#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Function.h"
16#include "llvm/IR/Module.h"
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000017#include "llvm/Pass.h"
David Greene5f2433e2010-01-05 01:30:18 +000018#include "llvm/Support/Debug.h"
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000020using namespace llvm;
21
22namespace {
23
Nick Lewycky02d5f772009-10-25 06:33:48 +000024 class PrintModulePass : public ModulePass {
David Greene9b063df2010-04-02 23:17:14 +000025 std::string Banner;
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000026 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000027 bool DeleteStream; // Delete the ostream in our dtor?
28 public:
29 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000030 PrintModulePass() : ModulePass(ID), Out(&dbgs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000031 DeleteStream(false) {}
David Greene9b063df2010-04-02 23:17:14 +000032 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
Owen Andersona7aed182010-08-06 18:33:48 +000033 : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000034
35 ~PrintModulePass() {
36 if (DeleteStream) delete Out;
37 }
38
39 bool runOnModule(Module &M) {
David Greene9b063df2010-04-02 23:17:14 +000040 (*Out) << Banner << M;
Daniel Dunbar54d5b9e2008-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 Dunbar81b5fa52008-10-22 03:25:22 +000051 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000052 bool DeleteStream; // Delete the ostream in our dtor?
53 public:
54 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000055 PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000056 DeleteStream(false) {}
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000057 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
Owen Andersona7aed182010-08-06 18:33:48 +000058 : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000059
Dan Gohman30614852010-08-16 21:57:30 +000060 ~PrintFunctionPass() {
Daniel Dunbar54d5b9e2008-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 Larincd1201b2013-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 Dunbar54d5b9e2008-10-21 23:33:38 +0000101}
102
103char PrintModulePass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000104INITIALIZE_PASS(PrintModulePass, "print-module",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000105 "Print module to stderr", false, false)
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000106char PrintFunctionPass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000107INITIALIZE_PASS(PrintFunctionPass, "print-function",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000108 "Print function to stderr", false, false)
Sergei Larincd1201b2013-02-08 23:37:41 +0000109char PrintBasicBlockPass::ID = 0;
110INITIALIZE_PASS(PrintBasicBlockPass, "print-bb",
111 "Print BB to stderr", false, false)
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000112
113/// createPrintModulePass - Create and return a pass that writes the
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000114/// module to the specified raw_ostream.
115ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
David Greene9b063df2010-04-02 23:17:14 +0000116 bool DeleteStream,
117 const std::string &Banner) {
118 return new PrintModulePass(Banner, OS, DeleteStream);
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000119}
120
121/// createPrintFunctionPass - Create and return a pass that prints
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000122/// functions to the specified raw_ostream as they are processed.
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000123FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000124 llvm::raw_ostream *OS,
125 bool DeleteStream) {
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000126 return new PrintFunctionPass(Banner, OS, DeleteStream);
127}
128
Sergei Larincd1201b2013-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