blob: 6961c53a71a2882344b1956dc4ecf710cf02b3f6 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- EmitFunctions.cpp - interface to insert instrumentation -----------===//
Anand Shuklad5f25dc2002-07-16 18:58:08 +00002//
3// This inserts a global constant table with function pointers all along
4//
5//===----------------------------------------------------------------------===//
6
Anand Shuklad5f25dc2002-07-16 18:58:08 +00007#include "llvm/Constants.h"
8#include "llvm/DerivedTypes.h"
Anand Shuklad5f25dc2002-07-16 18:58:08 +00009#include "llvm/Module.h"
Chris Lattner57fd3072003-01-14 22:39:29 +000010#include "llvm/Pass.h"
Anand Shukla1db75a02003-07-18 20:55:26 +000011#include "llvm/Support/CFG.h"
12
13enum Color{
14 WHITE,
15 GREY,
16 BLACK
17};
Anand Shuklad5f25dc2002-07-16 18:58:08 +000018
Chris Lattnerb28b6802002-07-23 18:06:35 +000019namespace {
20 struct EmitFunctionTable : public Pass {
21 bool run(Module &M);
22 };
23
Chris Lattnerc8b70922002-07-26 21:12:46 +000024 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
Chris Lattnerb28b6802002-07-23 18:06:35 +000025}
Anand Shuklad5f25dc2002-07-16 18:58:08 +000026
Anand Shukla1db75a02003-07-18 20:55:26 +000027char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
28 color[node] = GREY;
29
Chris Lattnere3178562003-09-24 22:07:33 +000030 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
Anand Shukla1db75a02003-07-18 20:55:26 +000031
32 BasicBlock *BB = *vl;
33
34 if(color[BB]!=GREY && color[BB]!=BLACK){
35 if(!doDFS(BB, color)){
36 return 0;
37 }
38 }
39
40 //if has backedge
41 else if(color[BB]==GREY)
42 return 0;
43
44 }
45
46 color[node] = BLACK;
47 return 1;
48}
49
50char hasBackEdge(Function *F){
51 std::map<BasicBlock *, Color > color;
52 return doDFS(F->begin(), color);
53}
54
Anand Shuklad5f25dc2002-07-16 18:58:08 +000055// Per Module pass for inserting function table
56bool EmitFunctionTable::run(Module &M){
Chris Lattner8d0a71a2003-05-22 22:00:07 +000057 std::vector<const Type*> vType;
Anand Shukla1db75a02003-07-18 20:55:26 +000058
Chris Lattner8d0a71a2003-05-22 22:00:07 +000059 std::vector<Constant *> vConsts;
Anand Shukla1db75a02003-07-18 20:55:26 +000060 std::vector<Constant *> sBCons;
61
62 unsigned int counter = 0;
Chris Lattner8d0a71a2003-05-22 22:00:07 +000063 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Anand Shuklad5f25dc2002-07-16 18:58:08 +000064 if (!MI->isExternal()) {
Anand Shuklad5f25dc2002-07-16 18:58:08 +000065 vType.push_back(MI->getType());
Anand Shukla1db75a02003-07-18 20:55:26 +000066
67 //std::cerr<<MI;
68
Chris Lattner8d0a71a2003-05-22 22:00:07 +000069 vConsts.push_back(ConstantPointerRef::get(MI));
Anand Shukla1db75a02003-07-18 20:55:26 +000070 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
71
Anand Shukla03a21342003-06-01 02:40:49 +000072 counter++;
Anand Shuklad5f25dc2002-07-16 18:58:08 +000073 }
74
75 StructType *sttype = StructType::get(vType);
76 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
77
Chris Lattner379a8d22003-04-16 20:28:45 +000078 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
79 GlobalValue::ExternalLinkage,
Anand Shuklad5f25dc2002-07-16 18:58:08 +000080 cstruct, "llvmFunctionTable");
81 M.getGlobalList().push_back(gb);
Anand Shukla03a21342003-06-01 02:40:49 +000082
Anand Shukla1db75a02003-07-18 20:55:26 +000083 ConstantArray *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
84 sBCons.size()),
85 sBCons);
86
87 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
88 GlobalValue::ExternalLinkage,
89 constArray, "llvmSimpleFunction");
90
91 M.getGlobalList().push_back(funcArray);
92
Chris Lattnera485efa2003-06-04 20:08:47 +000093 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
Anand Shukla03a21342003-06-01 02:40:49 +000094 GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
95 GlobalValue::ExternalLinkage,
96 cnst, "llvmFunctionCount");
97 M.getGlobalList().push_back(fnCount);
Anand Shuklad5f25dc2002-07-16 18:58:08 +000098 return true; // Always modifies program
99}