Correlated Exprs pass now requires BCE pass instead of doing it manually
[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/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 namespace {
16   struct EmitFunctionTable : public Pass {
17     bool run(Module &M);
18   };
19   
20   RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
21 }
22
23 // Create a new pass to add function table
24 //
25 Pass *createEmitFunctionTablePass() {
26   return new EmitFunctionTable();
27 }
28
29 // Per Module pass for inserting function table
30 bool EmitFunctionTable::run(Module &M){
31   vector<const Type*> vType;
32   vector<Constant *> vConsts;
33   for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
34     if (!MI->isExternal()) {
35       ConstantPointerRef *CP = ConstantPointerRef::get(MI);
36       vType.push_back(MI->getType());
37       vConsts.push_back(CP);
38     }
39   
40   StructType *sttype = StructType::get(vType);
41   ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
42
43   GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false, 
44                                           cstruct, "llvmFunctionTable");
45   M.getGlobalList().push_back(gb);
46   return true;  // Always modifies program
47 }