Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 1 | //===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=// |
| 2 | // |
| 3 | // This inserts a global constant table with function pointers all along |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 7 | #include "llvm/Constants.h" |
| 8 | #include "llvm/DerivedTypes.h" |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 9 | #include "llvm/Module.h" |
Chris Lattner | c56d239 | 2003-01-14 22:39:29 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 11 | #include "llvm/Support/CFG.h" |
| 12 | |
| 13 | enum Color{ |
| 14 | WHITE, |
| 15 | GREY, |
| 16 | BLACK |
| 17 | }; |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 18 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | struct EmitFunctionTable : public Pass { |
| 21 | bool run(Module &M); |
| 22 | }; |
| 23 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 24 | RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table"); |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 25 | } |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 26 | |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 27 | char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){ |
| 28 | color[node] = GREY; |
| 29 | |
Chris Lattner | 23ed9c1 | 2003-09-24 22:07:33 +0000 | [diff] [blame] | 30 | for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){ |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 31 | |
| 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 | |
| 50 | char hasBackEdge(Function *F){ |
| 51 | std::map<BasicBlock *, Color > color; |
| 52 | return doDFS(F->begin(), color); |
| 53 | } |
| 54 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 55 | // Per Module pass for inserting function table |
| 56 | bool EmitFunctionTable::run(Module &M){ |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 57 | std::vector<const Type*> vType; |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 58 | |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 59 | std::vector<Constant *> vConsts; |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 60 | std::vector<Constant *> sBCons; |
| 61 | |
| 62 | unsigned int counter = 0; |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 63 | for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 64 | if (!MI->isExternal()) { |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 65 | vType.push_back(MI->getType()); |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 66 | |
| 67 | //std::cerr<<MI; |
| 68 | |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 69 | vConsts.push_back(ConstantPointerRef::get(MI)); |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 70 | sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI))); |
| 71 | |
Anand Shukla | 619754f | 2003-06-01 02:40:49 +0000 | [diff] [blame] | 72 | counter++; |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | StructType *sttype = StructType::get(vType); |
| 76 | ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts); |
| 77 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 78 | GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, |
| 79 | GlobalValue::ExternalLinkage, |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 80 | cstruct, "llvmFunctionTable"); |
| 81 | M.getGlobalList().push_back(gb); |
Anand Shukla | 619754f | 2003-06-01 02:40:49 +0000 | [diff] [blame] | 82 | |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 83 | 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 Lattner | fa9ee73 | 2003-06-04 20:08:47 +0000 | [diff] [blame] | 93 | ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter); |
Anand Shukla | 619754f | 2003-06-01 02:40:49 +0000 | [diff] [blame] | 94 | GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true, |
| 95 | GlobalValue::ExternalLinkage, |
| 96 | cnst, "llvmFunctionCount"); |
| 97 | M.getGlobalList().push_back(fnCount); |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 98 | return true; // Always modifies program |
| 99 | } |