BlockProfiling.cpp [plain text]
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Streams.h"
#include "llvm/Transforms/Instrumentation.h"
#include "RSProfiling.h"
#include "ProfilingUtils.h"
using namespace llvm;
namespace {
class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
public:
static char ID;
bool runOnModule(Module &M);
};
}
char FunctionProfiler::ID = 0;
static RegisterPass<FunctionProfiler>
X("insert-function-profiling",
"Insert instrumentation for function profiling");
static RegisterAnalysisGroup<RSProfilers> XG(X);
ModulePass *llvm::createFunctionProfilerPass() {
return new FunctionProfiler();
}
bool FunctionProfiler::runOnModule(Module &M) {
Function *Main = M.getFunction("main");
if (Main == 0) {
cerr << "WARNING: cannot insert function profiling into a module"
<< " with no main function!\n";
return false; }
unsigned NumFunctions = 0;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
if (!I->isDeclaration())
++NumFunctions;
const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
GlobalVariable *Counters =
new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
Constant::getNullValue(ATy), "FuncProfCounters", &M);
unsigned i = 0;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
if (!I->isDeclaration())
IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
return true;
}
namespace {
class BlockProfiler : public RSProfilers_std {
bool runOnModule(Module &M);
public:
static char ID;
};
}
char BlockProfiler::ID = 0;
static RegisterPass<BlockProfiler>
Y("insert-block-profiling", "Insert instrumentation for block profiling");
static RegisterAnalysisGroup<RSProfilers> YG(Y);
ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
bool BlockProfiler::runOnModule(Module &M) {
Function *Main = M.getFunction("main");
if (Main == 0) {
cerr << "WARNING: cannot insert block profiling into a module"
<< " with no main function!\n";
return false; }
unsigned NumBlocks = 0;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
NumBlocks += I->size();
const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
GlobalVariable *Counters =
new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
Constant::getNullValue(ATy), "BlockProfCounters", &M);
unsigned i = 0;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
IncrementCounterInBlock(BB, i++, Counters);
InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
return true;
}