blob: a676edcc01b053f0fe29c56f2c659b4985073214 [file] [log] [blame]
Anand Shuklae0b51422002-07-16 18:58:08 +00001//===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
2//
3// This inserts a global constant table with function pointers all along
4//
5//===----------------------------------------------------------------------===//
6
Anand Shuklae0b51422002-07-16 18:58:08 +00007#include "llvm/Constants.h"
8#include "llvm/DerivedTypes.h"
Anand Shuklae0b51422002-07-16 18:58:08 +00009#include "llvm/Module.h"
Chris Lattnerc56d2392003-01-14 22:39:29 +000010#include "llvm/Pass.h"
Anand Shuklaa235e142003-07-18 20:55:26 +000011#include "llvm/Support/CFG.h"
12
13enum Color{
14 WHITE,
15 GREY,
16 BLACK
17};
Anand Shuklae0b51422002-07-16 18:58:08 +000018
Chris Lattnerf6293092002-07-23 18:06:35 +000019namespace {
20 struct EmitFunctionTable : public Pass {
21 bool run(Module &M);
22 };
23
Chris Lattnera6275cc2002-07-26 21:12:46 +000024 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
Chris Lattnerf6293092002-07-23 18:06:35 +000025}
Anand Shuklae0b51422002-07-16 18:58:08 +000026
Anand Shuklaa235e142003-07-18 20:55:26 +000027char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
28 color[node] = GREY;
29
30 for(BasicBlock::succ_iterator vl = succ_begin(node),
31 ve = succ_end(node); vl != ve; ++vl){
32
33 BasicBlock *BB = *vl;
34
35 if(color[BB]!=GREY && color[BB]!=BLACK){
36 if(!doDFS(BB, color)){
37 return 0;
38 }
39 }
40
41 //if has backedge
42 else if(color[BB]==GREY)
43 return 0;
44
45 }
46
47 color[node] = BLACK;
48 return 1;
49}
50
51char hasBackEdge(Function *F){
52 std::map<BasicBlock *, Color > color;
53 return doDFS(F->begin(), color);
54}
55
Anand Shuklae0b51422002-07-16 18:58:08 +000056// Per Module pass for inserting function table
57bool EmitFunctionTable::run(Module &M){
Chris Lattnerde579f12003-05-22 22:00:07 +000058 std::vector<const Type*> vType;
Anand Shuklaa235e142003-07-18 20:55:26 +000059
Chris Lattnerde579f12003-05-22 22:00:07 +000060 std::vector<Constant *> vConsts;
Anand Shuklaa235e142003-07-18 20:55:26 +000061 std::vector<Constant *> sBCons;
62
63 unsigned int counter = 0;
Chris Lattnerde579f12003-05-22 22:00:07 +000064 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Anand Shuklae0b51422002-07-16 18:58:08 +000065 if (!MI->isExternal()) {
Anand Shuklae0b51422002-07-16 18:58:08 +000066 vType.push_back(MI->getType());
Anand Shuklaa235e142003-07-18 20:55:26 +000067
68 //std::cerr<<MI;
69
Chris Lattnerde579f12003-05-22 22:00:07 +000070 vConsts.push_back(ConstantPointerRef::get(MI));
Anand Shuklaa235e142003-07-18 20:55:26 +000071 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
72
Anand Shukla619754f2003-06-01 02:40:49 +000073 counter++;
Anand Shuklae0b51422002-07-16 18:58:08 +000074 }
75
76 StructType *sttype = StructType::get(vType);
77 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
78
Chris Lattner4ad02e72003-04-16 20:28:45 +000079 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
80 GlobalValue::ExternalLinkage,
Anand Shuklae0b51422002-07-16 18:58:08 +000081 cstruct, "llvmFunctionTable");
82 M.getGlobalList().push_back(gb);
Anand Shukla619754f2003-06-01 02:40:49 +000083
Anand Shuklaa235e142003-07-18 20:55:26 +000084 ConstantArray *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
85 sBCons.size()),
86 sBCons);
87
88 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
89 GlobalValue::ExternalLinkage,
90 constArray, "llvmSimpleFunction");
91
92 M.getGlobalList().push_back(funcArray);
93
Chris Lattnerfa9ee732003-06-04 20:08:47 +000094 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
Anand Shukla619754f2003-06-01 02:40:49 +000095 GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
96 GlobalValue::ExternalLinkage,
97 cnst, "llvmFunctionCount");
98 M.getGlobalList().push_back(fnCount);
Anand Shuklae0b51422002-07-16 18:58:08 +000099 return true; // Always modifies program
100}