Rename include/llvm/Transforms/Instrumentation/TraceFunctions.h to Instrumentation.h
[oota-llvm.git] / lib / Transforms / Instrumentation / EmitFunctions.cpp
1 //===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
2 //
3 // This inserts a global constant table with function pointers all along
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Constants.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/Module.h"
10 #include "llvm/Pass.h"
11
12 using std::vector;
13
14 namespace {
15   struct EmitFunctionTable : public Pass {
16     bool run(Module &M);
17   };
18   
19   RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
20 }
21
22 // Per Module pass for inserting function table
23 bool EmitFunctionTable::run(Module &M){
24   vector<const Type*> vType;
25   vector<Constant *> vConsts;
26   for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
27     if (!MI->isExternal()) {
28       ConstantPointerRef *CP = ConstantPointerRef::get(MI);
29       vType.push_back(MI->getType());
30       vConsts.push_back(CP);
31     }
32   
33   StructType *sttype = StructType::get(vType);
34   ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
35
36   GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false, 
37                                           cstruct, "llvmFunctionTable");
38   M.getGlobalList().push_back(gb);
39   return true;  // Always modifies program
40 }