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