2003-10-28 18:59:04 +00:00
|
|
|
//===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-28 18:59:04 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-28 18:59:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass instruments the specified program with counters for basic block or
|
|
|
|
// function profiling. This is the most basic form of profiling, which can tell
|
|
|
|
// which blocks are hot, but cannot reliably detect hot paths through the CFG.
|
|
|
|
// Block profiling counts the number of times each basic block executes, and
|
|
|
|
// function profiling counts the number of times each function is called.
|
|
|
|
//
|
|
|
|
// Note that this implementation is very naive. Control equivalent regions of
|
|
|
|
// the CFG should not require duplicate counters, but we do put duplicate
|
|
|
|
// counters in.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-11-26 09:17:06 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2005-01-07 06:57:28 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2005-11-28 18:00:38 +00:00
|
|
|
#include "RSProfiling.h"
|
2004-03-08 17:06:13 +00:00
|
|
|
#include "ProfilingUtils.h"
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-29 21:24:22 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
|
2007-05-01 21:15:47 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module &M);
|
2003-10-29 21:24:22 +00:00
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char FunctionProfiler::ID = 0;
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<FunctionProfiler> X("insert-function-profiling",
|
2003-10-29 21:24:22 +00:00
|
|
|
"Insert instrumentation for function profiling");
|
2006-08-28 00:42:29 +00:00
|
|
|
RegisterAnalysisGroup<RSProfilers> XG(X);
|
2005-11-28 18:00:38 +00:00
|
|
|
|
2003-10-29 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
2005-01-07 07:05:34 +00:00
|
|
|
ModulePass *llvm::createFunctionProfilerPass() {
|
|
|
|
return new FunctionProfiler();
|
2005-01-07 06:57:28 +00:00
|
|
|
}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool FunctionProfiler::runOnModule(Module &M) {
|
2007-02-05 21:19:13 +00:00
|
|
|
Function *Main = M.getFunction("main");
|
2003-10-29 21:24:22 +00:00
|
|
|
if (Main == 0) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "WARNING: cannot insert function profiling into a module"
|
|
|
|
<< " with no main function!\n";
|
2003-10-29 21:24:22 +00:00
|
|
|
return false; // No main, no instrumentation!
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumFunctions = 0;
|
2005-04-21 23:48:37 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!I->isDeclaration())
|
2003-10-29 21:24:22 +00:00
|
|
|
++NumFunctions;
|
|
|
|
|
2006-12-31 05:48:39 +00:00
|
|
|
const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
|
2003-10-29 21:24:22 +00:00
|
|
|
GlobalVariable *Counters =
|
|
|
|
new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
|
|
|
|
Constant::getNullValue(ATy), "FuncProfCounters", &M);
|
|
|
|
|
|
|
|
// Instrument all of the functions...
|
|
|
|
unsigned i = 0;
|
2005-04-21 23:48:37 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!I->isDeclaration())
|
2003-10-29 21:24:22 +00:00
|
|
|
// Insert counter at the start of the function
|
2004-07-18 00:44:37 +00:00
|
|
|
IncrementCounterInBlock(I->begin(), i++, Counters);
|
2003-10-29 21:24:22 +00:00
|
|
|
|
|
|
|
// Add the initialization call to main.
|
2004-03-08 17:06:13 +00:00
|
|
|
InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
|
2003-10-29 21:24:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2005-11-28 18:00:38 +00:00
|
|
|
class BlockProfiler : public RSProfilers_std {
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module &M);
|
2007-05-01 21:15:47 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2003-10-29 21:24:22 +00:00
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char BlockProfiler::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<BlockProfiler> Y("insert-block-profiling",
|
|
|
|
"Insert instrumentation for block profiling");
|
2006-08-28 00:42:29 +00:00
|
|
|
RegisterAnalysisGroup<RSProfilers> YG(Y);
|
2003-10-29 21:24:22 +00:00
|
|
|
}
|
|
|
|
|
2005-01-07 06:57:28 +00:00
|
|
|
ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool BlockProfiler::runOnModule(Module &M) {
|
2007-02-05 21:19:13 +00:00
|
|
|
Function *Main = M.getFunction("main");
|
2003-10-29 21:24:22 +00:00
|
|
|
if (Main == 0) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "WARNING: cannot insert block profiling into a module"
|
|
|
|
<< " with no main function!\n";
|
2003-10-29 21:24:22 +00:00
|
|
|
return false; // No main, no instrumentation!
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumBlocks = 0;
|
2005-04-21 23:48:37 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2003-10-29 21:24:22 +00:00
|
|
|
NumBlocks += I->size();
|
|
|
|
|
2006-12-31 05:48:39 +00:00
|
|
|
const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
|
2003-10-29 21:24:22 +00:00
|
|
|
GlobalVariable *Counters =
|
|
|
|
new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
|
|
|
|
Constant::getNullValue(ATy), "BlockProfCounters", &M);
|
|
|
|
|
|
|
|
// Instrument all of the blocks...
|
|
|
|
unsigned i = 0;
|
2005-04-21 23:48:37 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2003-10-29 21:24:22 +00:00
|
|
|
for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
|
|
|
|
// Insert counter at the start of the block
|
2004-07-18 00:44:37 +00:00
|
|
|
IncrementCounterInBlock(BB, i++, Counters);
|
2003-10-29 21:24:22 +00:00
|
|
|
|
|
|
|
// Add the initialization call to main.
|
2004-03-08 17:06:13 +00:00
|
|
|
InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
|
2003-10-29 21:24:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|