llc (et al): Add support for --show-encoding and --show-inst.
[llvm.git] / lib / VMCore / PrintModulePass.cpp
blob2d69dce07f3f7eda96b5c971a8c81eef7660a316
1 //===--- VMCore/PrintModulePass.cpp - Module/Function Printer -------------===//
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Assembly/PrintModulePass.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
23 namespace {
25 class PrintModulePass : public ModulePass {
26 std::string Banner;
27 raw_ostream *Out; // raw_ostream to print on
28 bool DeleteStream; // Delete the ostream in our dtor?
29 public:
30 static char ID;
31 PrintModulePass() : ModulePass(&ID), Out(&dbgs()),
32 DeleteStream(false) {}
33 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
34 : ModulePass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
36 ~PrintModulePass() {
37 if (DeleteStream) delete Out;
40 bool runOnModule(Module &M) {
41 (*Out) << Banner << M;
42 return false;
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
50 class PrintFunctionPass : public FunctionPass {
51 std::string Banner; // String to print before each function
52 raw_ostream *Out; // raw_ostream to print on
53 bool DeleteStream; // Delete the ostream in our dtor?
54 public:
55 static char ID;
56 PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&dbgs()),
57 DeleteStream(false) {}
58 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
59 : FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
61 inline ~PrintFunctionPass() {
62 if (DeleteStream) delete Out;
65 // runOnFunction - This pass just prints a banner followed by the
66 // function as it's processed.
68 bool runOnFunction(Function &F) {
69 (*Out) << Banner << static_cast<Value&>(F);
70 return false;
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.setPreservesAll();
79 char PrintModulePass::ID = 0;
80 static RegisterPass<PrintModulePass>
81 X("print-module", "Print module to stderr");
82 char PrintFunctionPass::ID = 0;
83 static RegisterPass<PrintFunctionPass>
84 Y("print-function","Print function to stderr");
86 /// createPrintModulePass - Create and return a pass that writes the
87 /// module to the specified raw_ostream.
88 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
89 bool DeleteStream,
90 const std::string &Banner) {
91 return new PrintModulePass(Banner, OS, DeleteStream);
94 /// createPrintFunctionPass - Create and return a pass that prints
95 /// functions to the specified raw_ostream as they are processed.
96 FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
97 llvm::raw_ostream *OS,
98 bool DeleteStream) {
99 return new PrintFunctionPass(Banner, OS, DeleteStream);