Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Transforms / Instrumentation / EmitFunctions.cpp
1 //===-- EmitFunctions.cpp - interface to insert 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 inserts a global constant table with function pointers all along
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Constants.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/CFG.h"
19
20 enum Color{
21   WHITE,
22   GREY,
23   BLACK
24 };
25
26 namespace {
27   struct EmitFunctionTable : public Pass {
28     bool run(Module &M);
29   };
30   
31   RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
32 }
33
34 char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
35   color[node] = GREY;
36
37   for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
38    
39     BasicBlock *BB = *vl; 
40     
41     if(color[BB]!=GREY && color[BB]!=BLACK){
42       if(!doDFS(BB, color)){
43         return 0;
44       }
45     }
46
47     //if has backedge
48     else if(color[BB]==GREY)
49       return 0;
50
51   }
52
53   color[node] = BLACK;
54   return 1;
55 }
56
57 char hasBackEdge(Function *F){
58   std::map<BasicBlock *, Color > color;
59   return doDFS(F->begin(), color);
60 }
61
62 // Per Module pass for inserting function table
63 bool EmitFunctionTable::run(Module &M){
64   std::vector<const Type*> vType;
65  
66   std::vector<Constant *> vConsts;
67   std::vector<Constant *> sBCons;
68
69   unsigned int counter = 0;
70   for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
71     if (!MI->isExternal()) {
72       vType.push_back(MI->getType());
73     
74       //std::cerr<<MI;
75
76       vConsts.push_back(ConstantPointerRef::get(MI));
77       sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
78       
79       counter++;
80     }
81   
82   StructType *sttype = StructType::get(vType);
83   ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
84
85   GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
86                                           GlobalValue::ExternalLinkage, 
87                                           cstruct, "llvmFunctionTable");
88   M.getGlobalList().push_back(gb);
89
90   ConstantArray *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy, 
91                                                                 sBCons.size()),
92                                                  sBCons);
93
94   GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
95                                               GlobalValue::ExternalLinkage,
96                                               constArray, "llvmSimpleFunction");
97
98   M.getGlobalList().push_back(funcArray);
99
100   ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter); 
101   GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true, 
102                                                GlobalValue::ExternalLinkage, 
103                                                cnst, "llvmFunctionCount");
104   M.getGlobalList().push_back(fnCount);
105   return true;  // Always modifies program
106 }