blob: 379005359dd96f8c69050222d0fb095b1320c4af [file] [log] [blame]
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +00001//===- TraceBasicBlocks.cpp - Insert basic-block trace instrumentation ----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +00008//===----------------------------------------------------------------------===//
9//
10// This pass instruments the specified program with calls into a runtime
11// library that cause it to output a trace of basic blocks as a side effect
12// of normal execution.
13//
14//===----------------------------------------------------------------------===//
15
Bill Wendlinge8156192006-12-07 01:30:32 +000016#include "ProfilingUtils.h"
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
20#include "llvm/Pass.h"
21#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000022#include "llvm/Transforms/Instrumentation.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000023#include "llvm/Instructions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/Debug.h"
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000025#include <set>
26using namespace llvm;
27
28namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000029 class TraceBasicBlocks : public ModulePass {
30 bool runOnModule(Module &M);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000031 };
32
Chris Lattner7f8897f2006-08-27 22:42:52 +000033 RegisterPass<TraceBasicBlocks> X("trace-basic-blocks",
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000034 "Insert instrumentation for basic block tracing");
35}
36
Jeff Cohend9ed8c82005-01-07 06:57:28 +000037ModulePass *llvm::createTraceBasicBlockPass()
38{
Jeff Cohen9d809302005-04-23 21:38:35 +000039 return new TraceBasicBlocks();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000040}
41
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000042static void InsertInstrumentationCall (BasicBlock *BB,
43 const std::string FnName,
44 unsigned BBNumber) {
Bill Wendling62c804a2006-11-26 09:17:06 +000045 DOUT << "InsertInstrumentationCall (\"" << BB->getName ()
46 << "\", \"" << FnName << "\", " << BBNumber << ")\n";
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000047 Module &M = *BB->getParent ()->getParent ();
48 Function *InstrFn = M.getOrInsertFunction (FnName, Type::VoidTy,
Reid Spencerc5b206b2006-12-31 05:48:39 +000049 Type::Int32Ty, (Type *)0);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000050 std::vector<Value*> Args (1);
Reid Spencerc5b206b2006-12-31 05:48:39 +000051 Args[0] = ConstantInt::get (Type::Int32Ty, BBNumber);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000052
53 // Insert the call after any alloca or PHI instructions...
54 BasicBlock::iterator InsertPos = BB->begin();
55 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
56 ++InsertPos;
57
Reid Spencer3ed469c2006-11-02 20:25:50 +000058 new CallInst (InstrFn, Args, "", InsertPos);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000059}
60
Chris Lattnerb12914b2004-09-20 04:48:05 +000061bool TraceBasicBlocks::runOnModule(Module &M) {
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000062 Function *Main = M.getMainFunction();
63 if (Main == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000064 cerr << "WARNING: cannot insert basic-block trace instrumentation"
65 << " into a module with no main function!\n";
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000066 return false; // No main, no instrumentation!
67 }
68
69 unsigned BBNumber = 0;
70 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
71 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
72 InsertInstrumentationCall (BB, "llvm_trace_basic_block", BBNumber);
73 ++BBNumber;
74 }
75
76 // Add the initialization call to main.
77 InsertProfilingInitCall(Main, "llvm_start_basic_block_tracing");
78 return true;
79}
80