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
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file 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"
|
2009-07-06 18:42:36 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2003-10-28 18:59:04 +00:00
|
|
|
#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
|
|
|
};
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
2003-10-29 21:24:22 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char FunctionProfiler::ID = 0;
|
2005-11-28 18:00:38 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterPass<FunctionProfiler>
|
|
|
|
X("insert-function-profiling",
|
|
|
|
"Insert instrumentation for function profiling");
|
|
|
|
static RegisterAnalysisGroup<RSProfilers> XG(X);
|
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;
|
|
|
|
|
2009-07-06 18:42:36 +00:00
|
|
|
const Type *ATy = Context->getArrayType(Type::Int32Ty, NumFunctions);
|
2003-10-29 21:24:22 +00:00
|
|
|
GlobalVariable *Counters =
|
2009-07-08 19:03:57 +00:00
|
|
|
new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
|
|
|
|
Context->getNullValue(ATy), "FuncProfCounters");
|
2003-10-29 21:24:22 +00:00
|
|
|
|
|
|
|
// 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
|
2008-10-21 03:10:28 +00:00
|
|
|
IncrementCounterInBlock(&I->getEntryBlock(), 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char BlockProfiler::ID = 0;
|
|
|
|
static RegisterPass<BlockProfiler>
|
|
|
|
Y("insert-block-profiling", "Insert instrumentation for block profiling");
|
|
|
|
static RegisterAnalysisGroup<RSProfilers> YG(Y);
|
|
|
|
|
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();
|
|
|
|
|
2009-07-06 18:42:36 +00:00
|
|
|
const Type *ATy = Context->getArrayType(Type::Int32Ty, NumBlocks);
|
2003-10-29 21:24:22 +00:00
|
|
|
GlobalVariable *Counters =
|
2009-07-08 19:03:57 +00:00
|
|
|
new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
|
|
|
|
Context->getNullValue(ATy), "BlockProfCounters");
|
2003-10-29 21:24:22 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|