2013-01-02 17:10:48 +08:00
|
|
|
//===--- IR/PrintModulePass.cpp - Module/Function Printer -----------------===//
|
2008-10-22 07:33:38 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2008-10-22 07:33:38 +08:00
|
|
|
#include "llvm/Pass.h"
|
2010-01-05 09:30:18 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-10-22 11:25:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-22 07:33:38 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-10-25 14:33:48 +08:00
|
|
|
class PrintModulePass : public ModulePass {
|
2010-04-03 07:17:14 +08:00
|
|
|
std::string Banner;
|
2008-10-22 11:25:22 +08:00
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
2008-10-22 07:33:38 +08:00
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-08-07 02:33:48 +08:00
|
|
|
PrintModulePass() : ModulePass(ID), Out(&dbgs()),
|
2008-10-22 07:33:38 +08:00
|
|
|
DeleteStream(false) {}
|
2010-04-03 07:17:14 +08:00
|
|
|
PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
|
2010-08-07 02:33:48 +08:00
|
|
|
: ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
|
2008-10-22 07:33:38 +08:00
|
|
|
|
|
|
|
~PrintModulePass() {
|
|
|
|
if (DeleteStream) delete Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnModule(Module &M) {
|
2010-04-03 07:17:14 +08:00
|
|
|
(*Out) << Banner << M;
|
2008-10-22 07:33:38 +08: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 11:25:22 +08:00
|
|
|
raw_ostream *Out; // raw_ostream to print on
|
2008-10-22 07:33:38 +08:00
|
|
|
bool DeleteStream; // Delete the ostream in our dtor?
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-08-07 02:33:48 +08:00
|
|
|
PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()),
|
2008-10-22 07:33:38 +08:00
|
|
|
DeleteStream(false) {}
|
2008-10-22 11:25:22 +08:00
|
|
|
PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
|
2010-08-07 02:33:48 +08:00
|
|
|
: FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
|
2008-10-22 07:33:38 +08:00
|
|
|
|
2010-08-17 05:57:30 +08:00
|
|
|
~PrintFunctionPass() {
|
2008-10-22 07:33:38 +08: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-09 07:37:41 +08: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-22 07:33:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char PrintModulePass::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(PrintModulePass, "print-module",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Print module to stderr", false, false)
|
2008-10-22 07:33:38 +08:00
|
|
|
char PrintFunctionPass::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(PrintFunctionPass, "print-function",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Print function to stderr", false, false)
|
2013-02-09 07:37:41 +08:00
|
|
|
char PrintBasicBlockPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(PrintBasicBlockPass, "print-bb",
|
|
|
|
"Print BB to stderr", false, false)
|
2008-10-22 07:33:38 +08:00
|
|
|
|
|
|
|
/// createPrintModulePass - Create and return a pass that writes the
|
2008-10-22 11:25:22 +08:00
|
|
|
/// module to the specified raw_ostream.
|
|
|
|
ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
|
2010-04-03 07:17:14 +08:00
|
|
|
bool DeleteStream,
|
|
|
|
const std::string &Banner) {
|
|
|
|
return new PrintModulePass(Banner, OS, DeleteStream);
|
2008-10-22 07:33:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// createPrintFunctionPass - Create and return a pass that prints
|
2008-10-22 11:25:22 +08:00
|
|
|
/// functions to the specified raw_ostream as they are processed.
|
2008-10-22 07:33:38 +08:00
|
|
|
FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
|
2008-10-22 11:25:22 +08:00
|
|
|
llvm::raw_ostream *OS,
|
|
|
|
bool DeleteStream) {
|
2008-10-22 07:33:38 +08:00
|
|
|
return new PrintFunctionPass(Banner, OS, DeleteStream);
|
|
|
|
}
|
|
|
|
|
2013-02-09 07:37:41 +08: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);
|
|
|
|
}
|
|
|
|
|