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