Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- EmitFunctions.cpp - interface to insert instrumentation -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 9 | // |
Brian Gaeke | a30dd79 | 2004-10-20 19:38:58 +0000 | [diff] [blame] | 10 | // This inserts into the input module three new global constants containing |
| 11 | // mapping information pertinent to the Reoptimizer's runtime library: |
| 12 | // 1) a structure containing a pointer to each function; |
| 13 | // 2) an array containing a boolean which is true iff the corresponding |
| 14 | // function in 1) contains a back-edge branch suitable for the Reoptimizer's |
| 15 | // first-level instrumentation; |
| 16 | // 3) an integer containing the number of entries in 1) and 2). |
Chris Lattner | c51733c | 2004-03-08 16:45:53 +0000 | [diff] [blame] | 17 | // |
| 18 | // NOTE: This pass is used by the reoptimizer only. |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Chris Lattner | c56d239 | 2003-01-14 22:39:29 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CFG.h" |
Jeff Cohen | 8ca7191 | 2005-01-06 05:46:44 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Instrumentation.h" |
Chris Lattner | c51733c | 2004-03-08 16:45:53 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 29 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 30 | namespace llvm { |
Brian Gaeke | ccb87cd | 2004-09-30 20:14:07 +0000 | [diff] [blame] | 31 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | c51733c | 2004-03-08 16:45:53 +0000 | [diff] [blame] | 33 | enum Color{ |
| 34 | WHITE, |
| 35 | GREY, |
| 36 | BLACK |
| 37 | }; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 38 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 39 | struct EmitFunctionTable : public ModulePass { |
| 40 | bool runOnModule(Module &M); |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 41 | }; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | |
Chris Lattner | c51733c | 2004-03-08 16:45:53 +0000 | [diff] [blame] | 43 | RegisterOpt<EmitFunctionTable> |
| 44 | X("emitfuncs", "Emit a function table for the reoptimizer"); |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 45 | } |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 46 | |
Chris Lattner | c51733c | 2004-03-08 16:45:53 +0000 | [diff] [blame] | 47 | static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){ |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 48 | color[node] = GREY; |
| 49 | |
Chris Lattner | 23ed9c1 | 2003-09-24 22:07:33 +0000 | [diff] [blame] | 50 | for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){ |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 51 | |
| 52 | BasicBlock *BB = *vl; |
| 53 | |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 54 | if(color[BB]!=GREY && color[BB]!=BLACK){ |
| 55 | if(!doDFS(BB, color)){ |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 56 | return 0; |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | //if has backedge |
| 61 | else if(color[BB]==GREY) |
| 62 | return 0; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | color[node] = BLACK; |
| 67 | return 1; |
| 68 | } |
| 69 | |
Chris Lattner | c51733c | 2004-03-08 16:45:53 +0000 | [diff] [blame] | 70 | static char hasBackEdge(Function *F){ |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 71 | std::map<BasicBlock *, Color > color; |
| 72 | return doDFS(F->begin(), color); |
| 73 | } |
| 74 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 75 | // Per Module pass for inserting function table |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 76 | bool EmitFunctionTable::runOnModule(Module &M){ |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 77 | std::vector<const Type*> vType; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 78 | |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 79 | std::vector<Constant *> vConsts; |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 80 | std::vector<Constant *> sBCons; |
| 81 | |
| 82 | unsigned int counter = 0; |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 83 | for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 84 | if (!MI->isExternal()) { |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 85 | vType.push_back(MI->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 86 | |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 87 | //std::cerr<<MI; |
| 88 | |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 89 | vConsts.push_back(MI); |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 90 | sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI))); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 91 | |
Anand Shukla | 619754f | 2003-06-01 02:40:49 +0000 | [diff] [blame] | 92 | counter++; |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 93 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 94 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 95 | StructType *sttype = StructType::get(vType); |
Chris Lattner | 04d1fb6 | 2004-02-15 04:07:32 +0000 | [diff] [blame] | 96 | Constant *cstruct = ConstantStruct::get(sttype, vConsts); |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 98 | GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 99 | GlobalValue::ExternalLinkage, |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 100 | cstruct, "llvmFunctionTable"); |
| 101 | M.getGlobalList().push_back(gb); |
Anand Shukla | 619754f | 2003-06-01 02:40:49 +0000 | [diff] [blame] | 102 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 103 | Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy, |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 104 | sBCons.size()), |
| 105 | sBCons); |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 106 | |
| 107 | GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true, |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 108 | GlobalValue::ExternalLinkage, |
| 109 | constArray, "llvmSimpleFunction"); |
Anand Shukla | a235e14 | 2003-07-18 20:55:26 +0000 | [diff] [blame] | 110 | |
| 111 | M.getGlobalList().push_back(funcArray); |
| 112 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 113 | ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter); |
| 114 | GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true, |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 115 | GlobalValue::ExternalLinkage, |
| 116 | cnst, "llvmFunctionCount"); |
Anand Shukla | 619754f | 2003-06-01 02:40:49 +0000 | [diff] [blame] | 117 | M.getGlobalList().push_back(fnCount); |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 118 | return true; // Always modifies program |
| 119 | } |
Brian Gaeke | ccb87cd | 2004-09-30 20:14:07 +0000 | [diff] [blame] | 120 | |
| 121 | ModulePass *createEmitFunctionTablePass () { |
| 122 | return new EmitFunctionTable(); |
| 123 | } |
| 124 | |
| 125 | } // end namespace llvm |