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"
27 #include "llvm/Transforms/Instrumentation.h"
39 struct EmitFunctionTable : public ModulePass {
40 bool runOnModule(Module &M);
43 RegisterPass<EmitFunctionTable>
44 X("emitfuncs", "Emit a function table for the reoptimizer");
47 static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
50 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
54 if(color[BB]!=GREY && color[BB]!=BLACK){
55 if(!doDFS(BB, color)){
61 else if(color[BB]==GREY)
70 static char hasBackEdge(Function *F){
71 std::map<BasicBlock *, Color > color;
72 return doDFS(F->begin(), color);
75 // Per Module pass for inserting function table
76 bool EmitFunctionTable::runOnModule(Module &M){
77 std::vector<const Type*> vType;
79 std::vector<Constant *> vConsts;
80 std::vector<Constant *> sBCons;
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());
89 vConsts.push_back(MI);
90 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
95 StructType *sttype = StructType::get(vType);
96 Constant *cstruct = ConstantStruct::get(sttype, vConsts);
98 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
99 GlobalValue::ExternalLinkage,
100 cstruct, "llvmFunctionTable");
101 M.getGlobalList().push_back(gb);
103 Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
107 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
108 GlobalValue::ExternalLinkage,
109 constArray, "llvmSimpleFunction");
111 M.getGlobalList().push_back(funcArray);
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
121 ModulePass *createEmitFunctionTablePass () {
122 return new EmitFunctionTable();
125 } // end namespace llvm