* Cleanup pass
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / EmitFunctions / 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/Transforms/Instrumentation/EmitFunctions.h"
8 #include "llvm/Constants.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Constants.h"
11 #include "llvm/Module.h"
12
13 using std::vector;
14
15 struct EmitFunctionTable : public Pass {
16   const char *getPassName() const { return "EmitFunctionTablePass"; }
17
18   bool run(Module &M);
19 };
20
21 // Create a new pass to add function table
22 //
23 Pass *createEmitFunctionTablePass() {
24   return new EmitFunctionTable();
25 }
26
27 // Per Module pass for inserting function table
28 bool EmitFunctionTable::run(Module &M){
29   vector<const Type*> vType;
30   vector<Constant *> vConsts;
31   for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
32     if (!MI->isExternal()) {
33       ConstantPointerRef *CP = ConstantPointerRef::get(MI);
34       vType.push_back(MI->getType());
35       vConsts.push_back(CP);
36     }
37   
38   StructType *sttype = StructType::get(vType);
39   ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
40
41   GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false, 
42                                           cstruct, "llvmFunctionTable");
43   M.getGlobalList().push_back(gb);
44   return true;  // Always modifies program
45 }