1 //===-- EmitFunctions.cpp - interface to insert instrumentation -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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).
18 // NOTE: This pass is used by the reoptimizer only.
20 //===----------------------------------------------------------------------===//
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"
38 struct EmitFunctionTable : public ModulePass {
39 bool runOnModule(Module &M);
42 RegisterOpt<EmitFunctionTable>
43 X("emitfuncs", "Emit a function table for the reoptimizer");
46 static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
49 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
53 if(color[BB]!=GREY && color[BB]!=BLACK){
54 if(!doDFS(BB, color)){
60 else if(color[BB]==GREY)
69 static char hasBackEdge(Function *F){
70 std::map<BasicBlock *, Color > color;
71 return doDFS(F->begin(), color);
74 // Per Module pass for inserting function table
75 bool EmitFunctionTable::runOnModule(Module &M){
76 std::vector<const Type*> vType;
78 std::vector<Constant *> vConsts;
79 std::vector<Constant *> sBCons;
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());
88 vConsts.push_back(MI);
89 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
94 StructType *sttype = StructType::get(vType);
95 Constant *cstruct = ConstantStruct::get(sttype, vConsts);
97 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
98 GlobalValue::ExternalLinkage,
99 cstruct, "llvmFunctionTable");
100 M.getGlobalList().push_back(gb);
102 Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
106 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
107 GlobalValue::ExternalLinkage,
108 constArray, "llvmSimpleFunction");
110 M.getGlobalList().push_back(funcArray);
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
120 ModulePass *createEmitFunctionTablePass () {
121 return new EmitFunctionTable();
124 } // end namespace llvm