2004-06-29 14:20:27 +00:00
|
|
|
//===- TraceBasicBlocks.cpp - Insert basic-block trace instrumentation ----===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-06-29 14:20:27 +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
|
|
|
//
|
2004-06-29 14:20:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass instruments the specified program with calls into a runtime
|
|
|
|
// library that cause it to output a trace of basic blocks as a side effect
|
|
|
|
// of normal execution.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
#include "ProfilingUtils.h"
|
2004-06-29 14:20:27 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2005-01-07 06:57:28 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-06-29 14:20:27 +00:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2004-09-20 04:48:05 +00:00
|
|
|
class TraceBasicBlocks : public ModulePass {
|
|
|
|
bool runOnModule(Module &M);
|
2004-06-29 14:20:27 +00:00
|
|
|
};
|
|
|
|
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<TraceBasicBlocks> X("trace-basic-blocks",
|
2004-06-29 14:20:27 +00:00
|
|
|
"Insert instrumentation for basic block tracing");
|
|
|
|
}
|
|
|
|
|
2005-01-07 06:57:28 +00:00
|
|
|
ModulePass *llvm::createTraceBasicBlockPass()
|
|
|
|
{
|
2005-04-23 21:38:35 +00:00
|
|
|
return new TraceBasicBlocks();
|
2005-01-07 06:57:28 +00:00
|
|
|
}
|
|
|
|
|
2004-06-29 14:20:27 +00:00
|
|
|
static void InsertInstrumentationCall (BasicBlock *BB,
|
|
|
|
const std::string FnName,
|
|
|
|
unsigned BBNumber) {
|
2006-11-26 09:17:06 +00:00
|
|
|
DOUT << "InsertInstrumentationCall (\"" << BB->getName ()
|
|
|
|
<< "\", \"" << FnName << "\", " << BBNumber << ")\n";
|
2004-06-29 14:20:27 +00:00
|
|
|
Module &M = *BB->getParent ()->getParent ();
|
2007-01-07 07:22:20 +00:00
|
|
|
Constant *InstrFn = M.getOrInsertFunction (FnName, Type::VoidTy,
|
2006-12-31 05:48:39 +00:00
|
|
|
Type::Int32Ty, (Type *)0);
|
2007-01-07 07:22:20 +00:00
|
|
|
|
|
|
|
// Insert the call after any alloca or PHI instructions.
|
2004-06-29 14:20:27 +00:00
|
|
|
BasicBlock::iterator InsertPos = BB->begin();
|
|
|
|
while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
|
|
|
|
++InsertPos;
|
|
|
|
|
2007-01-07 07:22:20 +00:00
|
|
|
new CallInst(InstrFn, ConstantInt::get (Type::Int32Ty, BBNumber),
|
|
|
|
"", InsertPos);
|
2004-06-29 14:20:27 +00:00
|
|
|
}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool TraceBasicBlocks::runOnModule(Module &M) {
|
2004-06-29 14:20:27 +00:00
|
|
|
Function *Main = M.getMainFunction();
|
|
|
|
if (Main == 0) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "WARNING: cannot insert basic-block trace instrumentation"
|
|
|
|
<< " into a module with no main function!\n";
|
2004-06-29 14:20:27 +00:00
|
|
|
return false; // No main, no instrumentation!
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BBNumber = 0;
|
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
InsertInstrumentationCall (BB, "llvm_trace_basic_block", BBNumber);
|
|
|
|
++BBNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the initialization call to main.
|
|
|
|
InsertProfilingInitCall(Main, "llvm_start_basic_block_tracing");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|