Duncan pointed out that the Extended case in getTypeForMVT could
[oota-llvm.git] / lib / VMCore / PrintModulePass.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Assembly/PrintModulePass.h"
15
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 namespace {
24
25   class VISIBILITY_HIDDEN PrintModulePass : public ModulePass {
26     raw_ostream *Out;       // raw_ostream to print on
27     bool DeleteStream;      // Delete the ostream in our dtor?
28   public:
29     static char ID;
30     PrintModulePass() : ModulePass(intptr_t(&ID)), Out(&errs()), 
31       DeleteStream(false) {}
32     PrintModulePass(raw_ostream *o, bool DS)
33       : ModulePass(intptr_t(&ID)), Out(o), DeleteStream(DS) {}
34     
35     ~PrintModulePass() {
36       if (DeleteStream) delete Out;
37     }
38     
39     bool runOnModule(Module &M) {
40       (*Out) << M;
41       Out->flush();
42       return false;
43     }
44     
45     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46       AU.setPreservesAll();
47     }
48   };
49   
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(intptr_t(&ID)), Banner(""), Out(&errs()), 
57                           DeleteStream(false) {}
58     PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
59       : FunctionPass(intptr_t(&ID)), Banner(B), Out(o), DeleteStream(DS) {}
60     
61     inline ~PrintFunctionPass() {
62       if (DeleteStream) delete Out;
63     }
64     
65     // runOnFunction - This pass just prints a banner followed by the
66     // function as it's processed.
67     //
68     bool runOnFunction(Function &F) {
69       (*Out) << Banner << static_cast<Value&>(F);
70       Out->flush();
71       return false;
72     }
73     
74     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75       AU.setPreservesAll();
76     }
77   };
78 }
79
80 char PrintModulePass::ID = 0;
81 static RegisterPass<PrintModulePass>
82 X("print-module", "Print module to stderr");
83 char PrintFunctionPass::ID = 0;
84 static RegisterPass<PrintFunctionPass>
85 Y("print-function","Print function to stderr");
86
87 /// createPrintModulePass - Create and return a pass that writes the
88 /// module to the specified raw_ostream.
89 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, 
90                                         bool DeleteStream) {
91   return new PrintModulePass(OS, DeleteStream);
92 }
93
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);
100 }
101