Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 1 | // $Id$ |
| 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // TraceValues.cpp |
| 5 | // |
| 6 | // Purpose: |
| 7 | // Support for inserting LLVM code to print values at basic block |
| 8 | // and method exits. Also exports functions to create a call |
| 9 | // "printf" instruction with one of the signatures listed below. |
| 10 | // |
| 11 | // History: |
| 12 | // 10/11/01 - Vikram Adve - Created |
| 13 | //**************************************************************************/ |
| 14 | |
| 15 | |
| 16 | #include "llvm/Transforms/Instrumentation/TraceValues.h" |
| 17 | #include "llvm/GlobalVariable.h" |
| 18 | #include "llvm/ConstPoolVals.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Instruction.h" |
| 22 | #include "llvm/iTerminators.h" |
| 23 | #include "llvm/iOther.h" |
| 24 | #include "llvm/BasicBlock.h" |
| 25 | #include "llvm/Method.h" |
| 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/SymbolTable.h" |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 28 | #include <strstream> |
| 29 | #include "llvm/Assembly/Writer.h" |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 30 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 31 | static inline GlobalVariable * |
| 32 | GetStringRef(Module *M, const string &str) |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 33 | { |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 34 | ConstPoolArray *Init = ConstPoolArray::get(str); |
| 35 | GlobalVariable *V = new GlobalVariable(Init->getType(), /*Const*/true, Init); |
| 36 | M->getGlobalList().push_back(V); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 37 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 38 | return V; |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 1afbdc0 | 2001-10-15 13:07:21 +0000 | [diff] [blame] | 41 | static inline bool |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 42 | TraceThisOpCode(unsigned opCode) |
| 43 | { |
| 44 | // Explicitly test for opCodes *not* to trace so that any new opcodes will |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 45 | // be traced by default (VoidTy's are already excluded) |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 46 | // |
| 47 | return (opCode < Instruction::FirstOtherOp && |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 48 | opCode != Instruction::Alloca && |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 49 | opCode != Instruction::PHINode && |
| 50 | opCode != Instruction::Cast); |
| 51 | } |
| 52 | |
| 53 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 54 | static void |
| 55 | FindValuesToTraceInBB(BasicBlock* bb, vector<Value*>& valuesToTraceInBB) |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 56 | { |
| 57 | for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II) |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 58 | if ((*II)->getType()->isPrimitiveType() && |
| 59 | (*II)->getType() != Type::VoidTy && |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 60 | TraceThisOpCode((*II)->getOpcode())) |
| 61 | { |
| 62 | valuesToTraceInBB.push_back(*II); |
| 63 | } |
| 64 | } |
| 65 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 66 | // The invocation should be: |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 67 | // call "printVal"(value). |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 68 | // |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 69 | static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) { |
| 70 | MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy), |
| 71 | /*isVarArg*/ false); |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 72 | |
| 73 | SymbolTable *ST = Mod->getSymbolTableSure(); |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 74 | if (Value *V = ST->lookup(PointerType::get(MTy), "printVal")) |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 75 | return V; |
| 76 | |
| 77 | // Create a new method and add it to the module |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 78 | Method *M = new Method(MTy, "printVal"); |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 79 | Mod->getMethodList().push_back(M); |
| 80 | return M; |
| 81 | } |
| 82 | |
| 83 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 84 | static void InsertPrintInsts(Value *Val, |
| 85 | BasicBlock::iterator &BBI, |
| 86 | Module *Mod, |
| 87 | unsigned int indent, |
| 88 | bool isMethodExit) { |
| 89 | const Type* ValTy = Val->getType(); |
| 90 | BasicBlock *BB = (*BBI)->getParent(); |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 92 | assert(ValTy->isPrimitiveType() && |
| 93 | ValTy->getPrimitiveID() != Type::VoidTyID && |
| 94 | ValTy->getPrimitiveID() != Type::TypeTyID && |
| 95 | ValTy->getPrimitiveID() != Type::LabelTyID && |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 96 | "Unsupported type for printing"); |
| 97 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 98 | const Value* scopeToUse = |
| 99 | isMethodExit ? (const Value*)BB->getParent() : (const Value*)BB; |
| 100 | |
| 101 | // Create the marker string... |
| 102 | strstream scopeNameString; |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 103 | WriteAsOperand(scopeNameString, scopeToUse) << " : "; |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 104 | WriteAsOperand(scopeNameString, Val) << " = " << ends; |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 105 | string fmtString(indent, ' '); |
| 106 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 107 | fmtString += string(" At exit of") + scopeNameString.str(); |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 109 | // Turn the marker string into a global variable... |
| 110 | GlobalVariable *fmtVal = GetStringRef(Mod, fmtString); |
| 111 | |
| 112 | // Insert the first print instruction to print the string flag: |
| 113 | Instruction *I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()), |
| 114 | vector<Value*>(1, fmtVal)); |
| 115 | BBI = BB->getInstList().insert(BBI, I)+1; |
| 116 | |
| 117 | // Insert the next print instruction to print the value: |
| 118 | I = new CallInst(GetPrintMethodForType(Mod, ValTy), |
| 119 | vector<Value*>(1, Val)); |
| 120 | BBI = BB->getInstList().insert(BBI, I)+1; |
| 121 | |
| 122 | // Print out a newline |
| 123 | fmtVal = GetStringRef(Mod, "\n"); |
| 124 | I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()), |
| 125 | vector<Value*>(1, fmtVal)); |
| 126 | BBI = BB->getInstList().insert(BBI, I)+1; |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 130 | |
| 131 | // |
| 132 | // Insert print instructions at the end of the basic block *bb |
| 133 | // for each value in valueVec[]. *bb must postdominate the block |
| 134 | // in which the value is computed; this is not checked here. |
| 135 | // |
| 136 | static void |
| 137 | TraceValuesAtBBExit(const vector<Value*>& valueVec, |
| 138 | BasicBlock* bb, |
| 139 | Module* module, |
| 140 | unsigned int indent, |
| 141 | bool isMethodExit) |
| 142 | { |
| 143 | // Get an iterator to point to the insertion location |
| 144 | // |
| 145 | BasicBlock::InstListType& instList = bb->getInstList(); |
| 146 | TerminatorInst* termInst = bb->getTerminator(); |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 147 | BasicBlock::iterator here = instList.end()-1; |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 148 | assert((*here)->isTerminator()); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 149 | |
| 150 | // Insert a print instruction for each value. |
| 151 | // |
| 152 | for (unsigned i=0, N=valueVec.size(); i < N; i++) |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 153 | InsertPrintInsts(valueVec[i], here, module, indent, isMethodExit); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 1afbdc0 | 2001-10-15 13:07:21 +0000 | [diff] [blame] | 156 | static void |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 157 | InsertCodeToShowMethodEntry(BasicBlock* entryBB) |
| 158 | { |
| 159 | } |
| 160 | |
Chris Lattner | 1afbdc0 | 2001-10-15 13:07:21 +0000 | [diff] [blame] | 161 | static void |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 162 | InsertCodeToShowMethodExit(BasicBlock* exitBB) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 167 | bool InsertTraceCode::doInsertTraceCode(Method *M, bool traceBasicBlockExits, |
| 168 | bool traceMethodExits) { |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 169 | vector<Value*> valuesToTraceInMethod; |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 170 | Module* module = M->getParent(); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 171 | BasicBlock* exitBB = NULL; |
| 172 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 173 | if (M->isExternal() || |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 174 | (! traceBasicBlockExits && ! traceMethodExits)) |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 175 | return false; |
| 176 | |
| 177 | if (traceMethodExits) { |
| 178 | InsertCodeToShowMethodEntry(M->getEntryNode()); |
| 179 | exitBB = M->getBasicBlocks().front(); //getExitNode(); |
| 180 | } |
| 181 | |
| 182 | for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) { |
| 183 | BasicBlock* bb = *BI; |
| 184 | |
| 185 | vector<Value*> valuesToTraceInBB; |
| 186 | FindValuesToTraceInBB(bb, valuesToTraceInBB); |
| 187 | |
| 188 | if (traceBasicBlockExits && bb != exitBB) |
| 189 | TraceValuesAtBBExit(valuesToTraceInBB, bb, module, |
| 190 | /*indent*/ 4, /*isMethodExit*/ false); |
| 191 | |
| 192 | if (traceMethodExits) { |
| 193 | valuesToTraceInMethod.insert(valuesToTraceInMethod.end(), |
| 194 | valuesToTraceInBB.begin(), |
| 195 | valuesToTraceInBB.end()); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 198 | |
| 199 | #if 0 |
| 200 | // Disable this code until we have a proper exit node. |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 201 | if (traceMethodExits) { |
| 202 | TraceValuesAtBBExit(valuesToTraceInMethod, exitBB, module, |
| 203 | /*indent*/ 0, /*isMethodExit*/ true); |
| 204 | InsertCodeToShowMethodExit(exitBB); |
| 205 | } |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 206 | #endif |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 207 | return true; |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 208 | } |