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