blob: 4f445bc868258b355bcb526fa8e600da18dbba25 [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
Chris Lattner23ed9c12003-09-24 22:07:33 +000030 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
Anand Shuklaa235e142003-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 Shuklae0b51422002-07-16 18:58:08 +000055// Per Module pass for inserting function table
56bool EmitFunctionTable::run(Module &M){
Chris Lattnerde579f12003-05-22 22:00:07 +000057 std::vector<const Type*> vType;
Anand Shuklaa235e142003-07-18 20:55:26 +000058
Chris Lattnerde579f12003-05-22 22:00:07 +000059 std::vector<Constant *> vConsts;
Anand Shuklaa235e142003-07-18 20:55:26 +000060 std::vector<Constant *> sBCons;
61
62 unsigned int counter = 0;
Chris Lattnerde579f12003-05-22 22:00:07 +000063 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Anand Shuklae0b51422002-07-16 18:58:08 +000064 if (!MI->isExternal()) {
Anand Shuklae0b51422002-07-16 18:58:08 +000065 vType.push_back(MI->getType());
Anand Shuklaa235e142003-07-18 20:55:26 +000066
67 //std::cerr<<MI;
68
Chris Lattnerde579f12003-05-22 22:00:07 +000069 vConsts.push_back(ConstantPointerRef::get(MI));
Anand Shuklaa235e142003-07-18 20:55:26 +000070 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
71
Anand Shukla619754f2003-06-01 02:40:49 +000072 counter++;
Anand Shuklae0b51422002-07-16 18:58:08 +000073 }
74
75 StructType *sttype = StructType::get(vType);
76 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
77
Chris Lattner4ad02e72003-04-16 20:28:45 +000078 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
79 GlobalValue::ExternalLinkage,
Anand Shuklae0b51422002-07-16 18:58:08 +000080 cstruct, "llvmFunctionTable");
81 M.getGlobalList().push_back(gb);
Anand Shukla619754f2003-06-01 02:40:49 +000082
Anand Shuklaa235e142003-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 Lattnerfa9ee732003-06-04 20:08:47 +000093 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
Anand Shukla619754f2003-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 Shuklae0b51422002-07-16 18:58:08 +000098 return true; // Always modifies program
99}