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