Explain what this pass does.
[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 into the input module three new global constants containing
11 // mapping information pertinent to the Reoptimizer's runtime library:
12 // 1) a structure containing a pointer to each function;
13 // 2) an array containing a boolean which is true iff the corresponding
14 //    function in 1) contains a back-edge branch suitable for the Reoptimizer's
15 //    first-level instrumentation;
16 // 3) an integer containing the number of entries in 1) and 2).
17 //
18 // NOTE: This pass is used by the reoptimizer only.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CFG.h"
27 using namespace llvm;
28
29 namespace llvm { 
30
31 namespace {
32   enum Color{
33     WHITE,
34     GREY,
35     BLACK
36   };
37   
38   struct EmitFunctionTable : public ModulePass {
39     bool runOnModule(Module &M);
40   };
41   
42   RegisterOpt<EmitFunctionTable>
43   X("emitfuncs", "Emit a function table for the reoptimizer");
44 }
45
46 static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
47   color[node] = GREY;
48
49   for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
50    
51     BasicBlock *BB = *vl; 
52     
53     if(color[BB]!=GREY && color[BB]!=BLACK){
54       if(!doDFS(BB, color)){
55         return 0;
56       }
57     }
58
59     //if has backedge
60     else if(color[BB]==GREY)
61       return 0;
62
63   }
64
65   color[node] = BLACK;
66   return 1;
67 }
68
69 static char hasBackEdge(Function *F){
70   std::map<BasicBlock *, Color > color;
71   return doDFS(F->begin(), color);
72 }
73
74 // Per Module pass for inserting function table
75 bool EmitFunctionTable::runOnModule(Module &M){
76   std::vector<const Type*> vType;
77  
78   std::vector<Constant *> vConsts;
79   std::vector<Constant *> sBCons;
80
81   unsigned int counter = 0;
82   for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
83     if (!MI->isExternal()) {
84       vType.push_back(MI->getType());
85     
86       //std::cerr<<MI;
87
88       vConsts.push_back(MI);
89       sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
90       
91       counter++;
92     }
93   
94   StructType *sttype = StructType::get(vType);
95   Constant *cstruct = ConstantStruct::get(sttype, vConsts);
96
97   GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
98                                           GlobalValue::ExternalLinkage, 
99                                           cstruct, "llvmFunctionTable");
100   M.getGlobalList().push_back(gb);
101
102   Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy, 
103                                                                 sBCons.size()),
104                                                  sBCons);
105
106   GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
107                                               GlobalValue::ExternalLinkage,
108                                               constArray, "llvmSimpleFunction");
109
110   M.getGlobalList().push_back(funcArray);
111
112   ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter); 
113   GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true, 
114                                                GlobalValue::ExternalLinkage, 
115                                                cnst, "llvmFunctionCount");
116   M.getGlobalList().push_back(fnCount);
117   return true;  // Always modifies program
118 }
119
120 ModulePass *createEmitFunctionTablePass () {
121   return new EmitFunctionTable();
122 }
123
124 } // end namespace llvm