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