blob: 0025d3e2b601aca3350da25fbd4e51ca6fc12167 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- EmitFunctions.cpp - interface to insert instrumentation -----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Anand Shuklae0b51422002-07-16 18:58:08 +00009//
10// This inserts a global constant table with function pointers all along
11//
12//===----------------------------------------------------------------------===//
13
Anand Shuklae0b51422002-07-16 18:58:08 +000014#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
Anand Shuklae0b51422002-07-16 18:58:08 +000016#include "llvm/Module.h"
Chris Lattnerc56d2392003-01-14 22:39:29 +000017#include "llvm/Pass.h"
Anand Shuklaa235e142003-07-18 20:55:26 +000018#include "llvm/Support/CFG.h"
19
Brian Gaeked0fde302003-11-11 22:41:34 +000020namespace llvm {
21
Anand Shuklaa235e142003-07-18 20:55:26 +000022enum Color{
23 WHITE,
24 GREY,
25 BLACK
26};
Anand Shuklae0b51422002-07-16 18:58:08 +000027
Chris Lattnerf6293092002-07-23 18:06:35 +000028namespace {
29 struct EmitFunctionTable : public Pass {
30 bool run(Module &M);
31 };
32
Chris Lattnera6275cc2002-07-26 21:12:46 +000033 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
Chris Lattnerf6293092002-07-23 18:06:35 +000034}
Anand Shuklae0b51422002-07-16 18:58:08 +000035
Anand Shuklaa235e142003-07-18 20:55:26 +000036char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
37 color[node] = GREY;
38
Chris Lattner23ed9c12003-09-24 22:07:33 +000039 for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
Anand Shuklaa235e142003-07-18 20:55:26 +000040
41 BasicBlock *BB = *vl;
42
43 if(color[BB]!=GREY && color[BB]!=BLACK){
44 if(!doDFS(BB, color)){
45 return 0;
46 }
47 }
48
49 //if has backedge
50 else if(color[BB]==GREY)
51 return 0;
52
53 }
54
55 color[node] = BLACK;
56 return 1;
57}
58
59char hasBackEdge(Function *F){
60 std::map<BasicBlock *, Color > color;
61 return doDFS(F->begin(), color);
62}
63
Anand Shuklae0b51422002-07-16 18:58:08 +000064// Per Module pass for inserting function table
65bool EmitFunctionTable::run(Module &M){
Chris Lattnerde579f12003-05-22 22:00:07 +000066 std::vector<const Type*> vType;
Anand Shuklaa235e142003-07-18 20:55:26 +000067
Chris Lattnerde579f12003-05-22 22:00:07 +000068 std::vector<Constant *> vConsts;
Anand Shuklaa235e142003-07-18 20:55:26 +000069 std::vector<Constant *> sBCons;
70
71 unsigned int counter = 0;
Chris Lattnerde579f12003-05-22 22:00:07 +000072 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Anand Shuklae0b51422002-07-16 18:58:08 +000073 if (!MI->isExternal()) {
Anand Shuklae0b51422002-07-16 18:58:08 +000074 vType.push_back(MI->getType());
Anand Shuklaa235e142003-07-18 20:55:26 +000075
76 //std::cerr<<MI;
77
Chris Lattnerde579f12003-05-22 22:00:07 +000078 vConsts.push_back(ConstantPointerRef::get(MI));
Anand Shuklaa235e142003-07-18 20:55:26 +000079 sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
80
Anand Shukla619754f2003-06-01 02:40:49 +000081 counter++;
Anand Shuklae0b51422002-07-16 18:58:08 +000082 }
83
84 StructType *sttype = StructType::get(vType);
85 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
86
Chris Lattner4ad02e72003-04-16 20:28:45 +000087 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
88 GlobalValue::ExternalLinkage,
Anand Shuklae0b51422002-07-16 18:58:08 +000089 cstruct, "llvmFunctionTable");
90 M.getGlobalList().push_back(gb);
Anand Shukla619754f2003-06-01 02:40:49 +000091
Chris Lattner37106442004-02-15 04:05:58 +000092 Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
Anand Shuklaa235e142003-07-18 20:55:26 +000093 sBCons.size()),
94 sBCons);
95
96 GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
97 GlobalValue::ExternalLinkage,
98 constArray, "llvmSimpleFunction");
99
100 M.getGlobalList().push_back(funcArray);
101
Chris Lattnerfa9ee732003-06-04 20:08:47 +0000102 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
Anand Shukla619754f2003-06-01 02:40:49 +0000103 GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
104 GlobalValue::ExternalLinkage,
105 cnst, "llvmFunctionCount");
106 M.getGlobalList().push_back(fnCount);
Anand Shuklae0b51422002-07-16 18:58:08 +0000107 return true; // Always modifies program
108}
Brian Gaeked0fde302003-11-11 22:41:34 +0000109
110} // End llvm namespace