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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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-06-29 14:20:27 +00:00
|
|
|
#include "ProfilingUtils.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-06-29 14:20:27 +00:00
|
|
|
#include <set>
|
2006-01-22 22:53:01 +00:00
|
|
|
#include <iostream>
|
2004-06-29 14:20:27 +00:00
|
|
|
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) {
|
|
|
|
DEBUG (std::cerr << "InsertInstrumentationCall (\"" << BB->getName ()
|
|
|
|
<< "\", \"" << FnName << "\", " << BBNumber << ")\n");
|
|
|
|
Module &M = *BB->getParent ()->getParent ();
|
|
|
|
Function *InstrFn = M.getOrInsertFunction (FnName, Type::VoidTy,
|
2005-10-23 04:37:20 +00:00
|
|
|
Type::UIntTy, (Type *)0);
|
2004-06-29 14:20:27 +00:00
|
|
|
std::vector<Value*> Args (1);
|
2006-10-20 07:07:24 +00:00
|
|
|
Args[0] = ConstantInt::get (Type::UIntTy, BBNumber);
|
2004-06-29 14:20:27 +00:00
|
|
|
|
|
|
|
// Insert the call after any alloca or PHI instructions...
|
|
|
|
BasicBlock::iterator InsertPos = BB->begin();
|
|
|
|
while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
|
|
|
|
++InsertPos;
|
|
|
|
|
2006-11-02 20:25:50 +00:00
|
|
|
new CallInst (InstrFn, Args, "", 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) {
|
|
|
|
std::cerr << "WARNING: cannot insert basic-block trace instrumentation"
|
|
|
|
<< " into a module with no main function!\n";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|