blob: 003ea0d26ae3c071708b57f74a6f11479e54bc1b [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
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Module.h"
19#include "llvm/Pass.h"
20#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000021#include "llvm/Transforms/Instrumentation.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000022#include "llvm/Instructions.h"
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000023#include "ProfilingUtils.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>
Chris Lattner86a54842006-01-22 22:53:01 +000026#include <iostream>
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000027using namespace llvm;
28
29namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000030 class TraceBasicBlocks : public ModulePass {
31 bool runOnModule(Module &M);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000032 };
33
Chris Lattner7f8897f2006-08-27 22:42:52 +000034 RegisterPass<TraceBasicBlocks> X("trace-basic-blocks",
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000035 "Insert instrumentation for basic block tracing");
36}
37
Jeff Cohend9ed8c82005-01-07 06:57:28 +000038ModulePass *llvm::createTraceBasicBlockPass()
39{
Jeff Cohen9d809302005-04-23 21:38:35 +000040 return new TraceBasicBlocks();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000041}
42
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000043static void InsertInstrumentationCall (BasicBlock *BB,
44 const std::string FnName,
45 unsigned BBNumber) {
46 DEBUG (std::cerr << "InsertInstrumentationCall (\"" << BB->getName ()
47 << "\", \"" << FnName << "\", " << BBNumber << ")\n");
48 Module &M = *BB->getParent ()->getParent ();
49 Function *InstrFn = M.getOrInsertFunction (FnName, Type::VoidTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000050 Type::UIntTy, (Type *)0);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000051 std::vector<Value*> Args (1);
Reid Spencerb83eb642006-10-20 07:07:24 +000052 Args[0] = ConstantInt::get (Type::UIntTy, BBNumber);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000053
54 // Insert the call after any alloca or PHI instructions...
55 BasicBlock::iterator InsertPos = BB->begin();
56 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
57 ++InsertPos;
58
Reid Spencer3ed469c2006-11-02 20:25:50 +000059 new CallInst (InstrFn, Args, "", InsertPos);
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000060}
61
Chris Lattnerb12914b2004-09-20 04:48:05 +000062bool TraceBasicBlocks::runOnModule(Module &M) {
Vikram S. Adve39c2a8e2004-06-29 14:20:27 +000063 Function *Main = M.getMainFunction();
64 if (Main == 0) {
65 std::cerr << "WARNING: cannot insert basic-block trace instrumentation"
66 << " into a module with no main function!\n";
67 return false; // No main, no instrumentation!
68 }
69
70 unsigned BBNumber = 0;
71 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
72 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
73 InsertInstrumentationCall (BB, "llvm_trace_basic_block", BBNumber);
74 ++BBNumber;
75 }
76
77 // Add the initialization call to main.
78 InsertProfilingInitCall(Main, "llvm_start_basic_block_tracing");
79 return true;
80}
81