3258bf5b21b6f432e3a8094971451be5f7cba64a
[oota-llvm.git] / lib / Transforms / Instrumentation / TraceBasicBlocks.cpp
1 //===- TraceBasicBlocks.cpp - Insert basic-block trace instrumentation ----===//
2 // 
3 //                      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.
7 // 
8 //===----------------------------------------------------------------------===//
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"
21 #include "llvm/iOther.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/iPHINode.h"
24 #include "ProfilingUtils.h"
25 #include "Support/Debug.h"
26 #include <iostream>
27 #include <set>
28 using namespace llvm;
29
30 namespace {
31   class TraceBasicBlocks : public Pass {
32     bool run(Module &M);
33   };
34
35   RegisterOpt<TraceBasicBlocks> X("trace-basic-blocks",
36                               "Insert instrumentation for basic block tracing");
37 }
38
39 static void InsertInstrumentationCall (BasicBlock *BB,
40                                        const std::string FnName,
41                                        unsigned BBNumber) {
42   DEBUG (std::cerr << "InsertInstrumentationCall (\"" << BB->getName ()
43                    << "\", \"" << FnName << "\", " << BBNumber << ")\n");
44   Module &M = *BB->getParent ()->getParent ();
45   Function *InstrFn = M.getOrInsertFunction (FnName, Type::VoidTy,
46                                              Type::UIntTy, 0);
47   std::vector<Value*> Args (1);
48   Args[0] = ConstantUInt::get (Type::UIntTy, BBNumber);
49
50   // Insert the call after any alloca or PHI instructions...
51   BasicBlock::iterator InsertPos = BB->begin();
52   while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
53     ++InsertPos;
54
55   Instruction *InstrCall = new CallInst (InstrFn, Args, "", InsertPos);
56 }
57
58 bool TraceBasicBlocks::run(Module &M) {
59   Function *Main = M.getMainFunction();
60   if (Main == 0) {
61     std::cerr << "WARNING: cannot insert basic-block trace instrumentation"
62               << " into a module with no main function!\n";
63     return false;  // No main, no instrumentation!
64   }
65
66   unsigned BBNumber = 0;
67   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
68     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
69       InsertInstrumentationCall (BB, "llvm_trace_basic_block", BBNumber);
70       ++BBNumber;
71     }
72
73   // Add the initialization call to main.
74   InsertProfilingInitCall(Main, "llvm_start_basic_block_tracing");
75   return true;
76 }
77