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"
32 struct EmitFunctionTable : public ModulePass {
33 bool runOnModule(Module &M);
36 RegisterOpt<EmitFunctionTable>
37 X("emitfuncs", "Emit a function table for the reoptimizer");
40 static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
43 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
47 if(color[BB]!=GREY && color[BB]!=BLACK){
48 if(!doDFS(BB, color)){
54 else if(color[BB]==GREY)
63 static char hasBackEdge(Function *F){
64 std::map<BasicBlock *, Color > color;
65 return doDFS(F->begin(), color);
68 // Per Module pass for inserting function table
69 bool EmitFunctionTable::runOnModule(Module &M){
70 std::vector<const Type*> vType;
72 std::vector<Constant *> vConsts;
73 std::vector<Constant *> sBCons;
75 unsigned int counter = 0;
76 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 if (!MI->isExternal()) {
78 vType.push_back(MI->getType());
82 vConsts.push_back(MI);
83 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
88 StructType *sttype = StructType::get(vType);
89 Constant *cstruct = ConstantStruct::get(sttype, vConsts);
91 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
92 GlobalValue::ExternalLinkage,
93 cstruct, "llvmFunctionTable");
94 M.getGlobalList().push_back(gb);
96 Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
100 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
101 GlobalValue::ExternalLinkage,
102 constArray, "llvmSimpleFunction");
104 M.getGlobalList().push_back(funcArray);
106 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
107 GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
108 GlobalValue::ExternalLinkage,
109 cnst, "llvmFunctionCount");
110 M.getGlobalList().push_back(fnCount);
111 return true; // Always modifies program
114 ModulePass *createEmitFunctionTablePass () {
115 return new EmitFunctionTable();
118 } // end namespace llvm