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 a global constant table with function pointers all along.
12 // NOTE: This pass is used by the reoptimizer only.
14 //===----------------------------------------------------------------------===//
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"
30 struct EmitFunctionTable : public Pass {
34 RegisterOpt<EmitFunctionTable>
35 X("emitfuncs", "Emit a function table for the reoptimizer");
38 static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
41 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
45 if(color[BB]!=GREY && color[BB]!=BLACK){
46 if(!doDFS(BB, color)){
52 else if(color[BB]==GREY)
61 static char hasBackEdge(Function *F){
62 std::map<BasicBlock *, Color > color;
63 return doDFS(F->begin(), color);
66 // Per Module pass for inserting function table
67 bool EmitFunctionTable::run(Module &M){
68 std::vector<const Type*> vType;
70 std::vector<Constant *> vConsts;
71 std::vector<Constant *> sBCons;
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());
80 vConsts.push_back(ConstantPointerRef::get(MI));
81 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
86 StructType *sttype = StructType::get(vType);
87 Constant *cstruct = ConstantStruct::get(sttype, vConsts);
89 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
90 GlobalValue::ExternalLinkage,
91 cstruct, "llvmFunctionTable");
92 M.getGlobalList().push_back(gb);
94 Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
98 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
99 GlobalValue::ExternalLinkage,
100 constArray, "llvmSimpleFunction");
102 M.getGlobalList().push_back(funcArray);
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