Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 1 | //===- TraceBasicBlocks.cpp - Insert basic-block trace instrumentation ----===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 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" |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Instrumentation.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 23 | #include "ProfilingUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 25 | #include <set> |
Chris Lattner | 86a5484 | 2006-01-22 22:53:01 +0000 | [diff] [blame] | 26 | #include <iostream> |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 30 | class TraceBasicBlocks : public ModulePass { |
| 31 | bool runOnModule(Module &M); |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 34 | RegisterPass<TraceBasicBlocks> X("trace-basic-blocks", |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 35 | "Insert instrumentation for basic block tracing"); |
| 36 | } |
| 37 | |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 38 | ModulePass *llvm::createTraceBasicBlockPass() |
| 39 | { |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 40 | return new TraceBasicBlocks(); |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 43 | static 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 Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 50 | Type::UIntTy, (Type *)0); |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 51 | std::vector<Value*> Args (1); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 52 | Args[0] = ConstantInt::get (Type::UIntTy, BBNumber); |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 53 | |
| 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 Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 59 | new CallInst (InstrFn, Args, "", InsertPos); |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 62 | bool TraceBasicBlocks::runOnModule(Module &M) { |
Vikram S. Adve | 39c2a8e | 2004-06-29 14:20:27 +0000 | [diff] [blame] | 63 | 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 | |