2013-01-02 09:10:48 +00:00
|
|
|
//===--- IR/PrintModulePass.cpp - Module/Function Printer -----------------===//
|
2008-10-21 23:33:38 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// PrintModulePass and PrintFunctionPass implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-07 12:34:26 +00:00
|
|
|
#include "llvm/IR/PrintModulePass.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2008-10-21 23:33:38 +00:00
|
|
|
#include "llvm/Pass.h"
|
2010-01-05 01:30:18 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-10-22 03:25:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-21 23:33:38 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-10-25 06:33:48 +00:00
|
|
|
class PrintModulePass : public ModulePass {
|
2010-04-02 23:17:14 +00:00
|
|
|
std::string Banner;
|
2008-10-22 03:25:22 +00:00
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
2008-10-21 23:33:38 +00:00
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-08-06 18:33:48 +00:00
|
|
|
PrintModulePass() : ModulePass(ID), Out(&dbgs()),
|
2008-10-21 23:33:38 +00:00
|
|
|
DeleteStream(false) {}
|
2010-04-02 23:17:14 +00:00
|
|
|
PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
|
2010-08-06 18:33:48 +00:00
|
|
|
: ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
|
2008-10-21 23:33:38 +00:00
|
|
|
|
|
|
|
~PrintModulePass() {
|
|
|
|
if (DeleteStream) delete Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnModule(Module &M) {
|
2010-04-02 23:17:14 +00:00
|
|
|
(*Out) << Banner << M;
|
2008-10-21 23:33:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PrintFunctionPass : public FunctionPass {
|
|
|
|
std::string Banner; // String to print before each function
|
2008-10-22 03:25:22 +00:00
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
2008-10-21 23:33:38 +00:00
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-08-06 18:33:48 +00:00
|
|
|
PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()),
|
2008-10-21 23:33:38 +00:00
|
|
|
DeleteStream(false) {}
|
2008-10-22 03:25:22 +00:00
|
|
|
PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
|
2010-08-06 18:33:48 +00:00
|
|
|
: FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
|
2008-10-21 23:33:38 +00:00
|
|
|
|
2010-08-16 21:57:30 +00:00
|
|
|
~PrintFunctionPass() {
|
2008-10-21 23:33:38 +00:00
|
|
|
if (DeleteStream) delete Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// runOnFunction - This pass just prints a banner followed by the
|
|
|
|
// function as it's processed.
|
|
|
|
//
|
|
|
|
bool runOnFunction(Function &F) {
|
|
|
|
(*Out) << Banner << static_cast<Value&>(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2013-02-08 23:37:41 +00:00
|
|
|
|
|
|
|
class PrintBasicBlockPass : public BasicBlockPass {
|
|
|
|
std::string Banner;
|
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
PrintBasicBlockPass() : BasicBlockPass(ID), Out(&dbgs()),
|
|
|
|
DeleteStream(false) {}
|
|
|
|
PrintBasicBlockPass(const std::string &B, raw_ostream *o, bool DS)
|
|
|
|
: BasicBlockPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
|
|
|
|
|
|
|
|
~PrintBasicBlockPass() {
|
|
|
|
if (DeleteStream) delete Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnBasicBlock(BasicBlock &BB) {
|
|
|
|
(*Out) << Banner << BB;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-10-21 23:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char PrintModulePass::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(PrintModulePass, "print-module",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Print module to stderr", false, false)
|
2008-10-21 23:33:38 +00:00
|
|
|
char PrintFunctionPass::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(PrintFunctionPass, "print-function",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Print function to stderr", false, false)
|
2013-02-08 23:37:41 +00:00
|
|
|
char PrintBasicBlockPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(PrintBasicBlockPass, "print-bb",
|
|
|
|
"Print BB to stderr", false, false)
|
2008-10-21 23:33:38 +00:00
|
|
|
|
|
|
|
/// createPrintModulePass - Create and return a pass that writes the
|
2008-10-22 03:25:22 +00:00
|
|
|
/// module to the specified raw_ostream.
|
|
|
|
ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
|
2010-04-02 23:17:14 +00:00
|
|
|
bool DeleteStream,
|
|
|
|
const std::string &Banner) {
|
|
|
|
return new PrintModulePass(Banner, OS, DeleteStream);
|
2008-10-21 23:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// createPrintFunctionPass - Create and return a pass that prints
|
2008-10-22 03:25:22 +00:00
|
|
|
/// functions to the specified raw_ostream as they are processed.
|
2008-10-21 23:33:38 +00:00
|
|
|
FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
|
2008-10-22 03:25:22 +00:00
|
|
|
llvm::raw_ostream *OS,
|
|
|
|
bool DeleteStream) {
|
2008-10-21 23:33:38 +00:00
|
|
|
return new PrintFunctionPass(Banner, OS, DeleteStream);
|
|
|
|
}
|
|
|
|
|
2013-02-08 23:37:41 +00:00
|
|
|
/// createPrintBasicBlockPass - Create and return a pass that writes the
|
|
|
|
/// BB to the specified raw_ostream.
|
|
|
|
BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream *OS,
|
|
|
|
bool DeleteStream,
|
|
|
|
const std::string &Banner) {
|
|
|
|
return new PrintBasicBlockPass(Banner, OS, DeleteStream);
|
|
|
|
}
|
|
|
|
|