1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // PrintModulePass and PrintFunctionPass implementations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/IRPrintingPasses.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
24 class PrintModulePass : public ModulePass {
31 PrintModulePass() : ModulePass(ID), Out(&dbgs()), DeleteStream(false) {}
32 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
33 : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
40 bool runOnModule(Module &M) {
41 (*Out) << Banner << M;
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 class PrintFunctionPass : public FunctionPass {
58 : FunctionPass(ID), Banner(""), Out(&dbgs()), DeleteStream(false) {}
59 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
60 : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
62 ~PrintFunctionPass() {
67 // This pass just prints a banner followed by the function as it's processed.
68 bool runOnFunction(Function &F) {
69 (*Out) << Banner << static_cast<Value &>(F);
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 class PrintBasicBlockPass : public BasicBlockPass {
86 : BasicBlockPass(ID), Out(&dbgs()), DeleteStream(false) {}
87 PrintBasicBlockPass(const std::string &B, raw_ostream *o, bool DS)
88 : BasicBlockPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
90 ~PrintBasicBlockPass() {
95 bool runOnBasicBlock(BasicBlock &BB) {
96 (*Out) << Banner << BB;
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
101 AU.setPreservesAll();
106 char PrintModulePass::ID = 0;
107 INITIALIZE_PASS(PrintModulePass, "print-module", "Print module to stderr",
109 char PrintFunctionPass::ID = 0;
110 INITIALIZE_PASS(PrintFunctionPass, "print-function", "Print function to stderr",
112 char PrintBasicBlockPass::ID = 0;
113 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
116 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
118 const std::string &Banner) {
119 return new PrintModulePass(Banner, OS, DeleteStream);
122 FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
123 llvm::raw_ostream *OS,
125 return new PrintFunctionPass(Banner, OS, DeleteStream);
128 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream *OS,
130 const std::string &Banner) {
131 return new PrintBasicBlockPass(Banner, OS, DeleteStream);