Vikram S. Adve | df1892f | 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" |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 22 | #include "llvm/iMemory.h" |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 23 | #include "llvm/iTerminators.h" |
| 24 | #include "llvm/iOther.h" |
| 25 | #include "llvm/BasicBlock.h" |
| 26 | #include "llvm/Method.h" |
| 27 | #include "llvm/Module.h" |
| 28 | #include "llvm/SymbolTable.h" |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 29 | #include "llvm/Assembly/Writer.h" |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 30 | #include "llvm/Support/HashExtras.h" |
| 31 | #include <hash_set> |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 32 | #include <sstream> |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 33 | |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 34 | |
| 35 | static const char* |
| 36 | PrintMethodNameForType(const Type* type) |
| 37 | { |
| 38 | if (PointerType* pty = dyn_cast<PointerType>(type)) |
| 39 | { |
| 40 | const Type* elemTy; |
| 41 | if (ArrayType* aty = dyn_cast<ArrayType>(pty->getValueType())) |
| 42 | elemTy = aty->getElementType(); |
| 43 | else |
| 44 | elemTy = pty->getValueType(); |
| 45 | if (elemTy == Type::SByteTy || elemTy == Type::UByteTy) |
| 46 | return "printString"; |
| 47 | } |
| 48 | |
| 49 | switch (type->getPrimitiveID()) |
| 50 | { |
| 51 | case Type::BoolTyID: return "printBool"; |
| 52 | case Type::UByteTyID: return "printUByte"; |
| 53 | case Type::SByteTyID: return "printSByte"; |
| 54 | case Type::UShortTyID: return "printUShort"; |
| 55 | case Type::ShortTyID: return "printShort"; |
| 56 | case Type::UIntTyID: return "printUInt"; |
| 57 | case Type::IntTyID: return "printInt"; |
| 58 | case Type::ULongTyID: return "printULong"; |
| 59 | case Type::LongTyID: return "printLong"; |
| 60 | case Type::FloatTyID: return "printFloat"; |
| 61 | case Type::DoubleTyID: return "printDouble"; |
| 62 | case Type::PointerTyID: return "printPointer"; |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 63 | default: |
| 64 | assert(0 && "Unsupported type for printing"); |
| 65 | return NULL; |
| 66 | } |
| 67 | } |
| 68 | |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 69 | static inline GlobalVariable *GetStringRef(Module *M, const string &str) { |
| 70 | ConstPoolArray *Init = ConstPoolArray::get(str); |
| 71 | GlobalVariable *GV = new GlobalVariable(Init->getType(), /*Const*/true, Init); |
| 72 | M->getGlobalList().push_back(GV); |
| 73 | return GV; |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 76 | |
Chris Lattner | f84b9bc | 2001-10-15 13:07:21 +0000 | [diff] [blame] | 77 | static inline bool |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 78 | TraceThisOpCode(unsigned opCode) |
| 79 | { |
| 80 | // Explicitly test for opCodes *not* to trace so that any new opcodes will |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 81 | // be traced by default (VoidTy's are already excluded) |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 82 | // |
| 83 | return (opCode < Instruction::FirstOtherOp && |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 84 | opCode != Instruction::Alloca && |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 85 | opCode != Instruction::PHINode && |
| 86 | opCode != Instruction::Cast); |
| 87 | } |
| 88 | |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 89 | // |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 90 | // Check if this instruction has any uses outside its basic block, |
| 91 | // or if it used by either a Call or Return instruction. |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 92 | // |
| 93 | static inline bool |
| 94 | LiveAtBBExit(Instruction* I) |
| 95 | { |
| 96 | BasicBlock* bb = I->getParent(); |
| 97 | bool isLive = false; |
| 98 | for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U) |
| 99 | { |
| 100 | const Instruction* userI = dyn_cast<Instruction>(*U); |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 101 | if (userI == NULL |
| 102 | || userI->getParent() != bb |
| 103 | || userI->getOpcode() == Instruction::Call |
| 104 | || userI->getOpcode() == Instruction::Ret) |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 105 | isLive = true; |
| 106 | } |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 107 | return isLive; |
| 108 | } |
| 109 | |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 111 | static void |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 112 | FindValuesToTraceInBB(BasicBlock* bb, vector<Instruction*>& valuesToTraceInBB) |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 113 | { |
| 114 | for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II) |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 115 | if ((*II)->getOpcode() == Instruction::Store |
| 116 | || (LiveAtBBExit(*II) && |
| 117 | (*II)->getType()->isPrimitiveType() && |
| 118 | (*II)->getType() != Type::VoidTy && |
| 119 | TraceThisOpCode((*II)->getOpcode()))) |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 120 | { |
| 121 | valuesToTraceInBB.push_back(*II); |
| 122 | } |
| 123 | } |
| 124 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 125 | // |
| 126 | // Let's save this code for future use; it has been tested and works: |
| 127 | // |
| 128 | // The signatures of the printf methods supported are: |
| 129 | // int printf(ubyte*, ubyte*, ubyte*, ubyte*, int intValue) |
| 130 | // int printf(ubyte*, ubyte*, ubyte*, ubyte*, unsigned uintValue) |
| 131 | // int printf(ubyte*, ubyte*, ubyte*, ubyte*, float floatValue) |
| 132 | // int printf(ubyte*, ubyte*, ubyte*, ubyte*, double doubleValue) |
| 133 | // int printf(ubyte*, ubyte*, ubyte*, ubyte*, char* stringValue) |
| 134 | // int printf(ubyte*, ubyte*, ubyte*, ubyte*, void* ptrValue) |
| 135 | // |
| 136 | // The invocation should be: |
| 137 | // call "printf"(fmt, bbName, valueName, valueTypeName, value). |
| 138 | // |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 139 | Value *GetPrintfMethodForType(Module* module, const Type* valueType) |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 140 | { |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 141 | PointerType *ubytePtrTy = PointerType::get(ArrayType::get(Type::UByteTy)); |
| 142 | vector<const Type*> argTypesVec(4, ubytePtrTy); |
| 143 | argTypesVec.push_back(valueType); |
| 144 | |
| 145 | MethodType *printMethodTy = MethodType::get(Type::IntTy, argTypesVec, |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 146 | /*isVarArg*/ false); |
| 147 | |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 148 | SymbolTable *ST = module->getSymbolTable(); |
| 149 | if (Value *Meth = ST->lookup(PointerType::get(printMethodTy), "printf")) |
| 150 | return Meth; |
| 151 | |
| 152 | // Create a new method and add it to the module |
| 153 | Method *printMethod = new Method(printMethodTy, "printf"); |
| 154 | module->getMethodList().push_back(printMethod); |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 155 | |
| 156 | return printMethod; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | Instruction* |
| 161 | CreatePrintfInstr(Value* val, |
| 162 | const BasicBlock* bb, |
| 163 | Module* module, |
| 164 | unsigned int indent, |
| 165 | bool isMethodExit) |
| 166 | { |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 167 | ostringstream fmtString, scopeNameString, valNameString; |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 168 | vector<Value*> paramList; |
| 169 | const Type* valueType = val->getType(); |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 170 | Method* printMethod = cast<Method>(GetPrintfMethodForType(module,valueType)); |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 171 | |
| 172 | if (! valueType->isPrimitiveType() || |
| 173 | valueType->getPrimitiveID() == Type::VoidTyID || |
| 174 | valueType->getPrimitiveID() == Type::TypeTyID || |
| 175 | valueType->getPrimitiveID() == Type::LabelTyID) |
| 176 | { |
| 177 | assert(0 && "Unsupported type for printing"); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | const Value* scopeToUse = (isMethodExit)? (const Value*) bb->getParent() |
| 182 | : (const Value*) bb; |
| 183 | if (scopeToUse->hasName()) |
| 184 | scopeNameString << scopeToUse->getName() << ends; |
| 185 | else |
| 186 | scopeNameString << scopeToUse << ends; |
| 187 | |
| 188 | if (val->hasName()) |
| 189 | valNameString << val->getName() << ends; |
| 190 | else |
| 191 | valNameString << val << ends; |
| 192 | |
| 193 | for (unsigned i=0; i < indent; i++) |
| 194 | fmtString << " "; |
| 195 | |
| 196 | fmtString << " At exit of " |
| 197 | << ((isMethodExit)? "Method " : "BB ") |
| 198 | << "%s : val %s = %s "; |
| 199 | |
| 200 | GlobalVariable* scopeNameVal = GetStringRef(module, scopeNameString.str()); |
| 201 | GlobalVariable* valNameVal = GetStringRef(module,valNameString.str()); |
| 202 | GlobalVariable* typeNameVal = GetStringRef(module, |
| 203 | val->getType()->getDescription().c_str()); |
| 204 | |
| 205 | switch(valueType->getPrimitiveID()) |
| 206 | { |
| 207 | case Type::BoolTyID: |
| 208 | case Type::UByteTyID: case Type::UShortTyID: |
| 209 | case Type::UIntTyID: case Type::ULongTyID: |
| 210 | case Type::SByteTyID: case Type::ShortTyID: |
| 211 | case Type::IntTyID: case Type::LongTyID: |
Vikram S. Adve | c426c63 | 2001-10-28 22:44:02 +0000 | [diff] [blame] | 212 | fmtString << " %d\n"; |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 213 | break; |
| 214 | |
| 215 | case Type::FloatTyID: case Type::DoubleTyID: |
Vikram S. Adve | c426c63 | 2001-10-28 22:44:02 +0000 | [diff] [blame] | 216 | fmtString << " %g\n"; |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 217 | break; |
| 218 | |
| 219 | case Type::PointerTyID: |
Vikram S. Adve | c426c63 | 2001-10-28 22:44:02 +0000 | [diff] [blame] | 220 | fmtString << " %p\n"; |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 221 | break; |
| 222 | |
| 223 | default: |
| 224 | assert(0 && "Should not get here. Check the IF expression above"); |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | fmtString << ends; |
| 229 | GlobalVariable* fmtVal = GetStringRef(module, fmtString.str()); |
| 230 | |
| 231 | paramList.push_back(fmtVal); |
| 232 | paramList.push_back(scopeNameVal); |
| 233 | paramList.push_back(valNameVal); |
| 234 | paramList.push_back(typeNameVal); |
| 235 | paramList.push_back(val); |
| 236 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 237 | return new CallInst(printMethod, paramList); |
| 238 | } |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 239 | |
| 240 | |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 241 | // The invocation should be: |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 242 | // call "printString"([ubyte*] or [sbyte*] or ubyte* or sbyte*). |
| 243 | // call "printLong"(long) |
| 244 | // call "printInt"(int) ... |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 245 | // |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 246 | static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) { |
| 247 | MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy), |
| 248 | /*isVarArg*/ false); |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 249 | |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 250 | const char* printMethodName = PrintMethodNameForType(VTy); |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 251 | SymbolTable *ST = Mod->getSymbolTableSure(); |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 252 | if (Value *V = ST->lookup(PointerType::get(MTy), printMethodName)) |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 253 | return V; |
| 254 | |
| 255 | // Create a new method and add it to the module |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 256 | Method *M = new Method(MTy, printMethodName); |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 257 | Mod->getMethodList().push_back(M); |
| 258 | return M; |
| 259 | } |
| 260 | |
| 261 | |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 262 | static void |
| 263 | InsertPrintInsts(Value *Val, |
| 264 | BasicBlock* BB, |
| 265 | BasicBlock::iterator &BBI, |
| 266 | Module *Mod, |
| 267 | unsigned int indent, |
| 268 | bool isMethodExit) |
| 269 | { |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 270 | const Type* ValTy = Val->getType(); |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 111bd01 | 2001-10-29 17:27:38 +0000 | [diff] [blame^] | 272 | assert((ValTy->isPrimitiveType() || isa<PointerType>(ValTy)) && |
| 273 | ValTy != Type::VoidTy && ValTy != Type::TypeTy && |
| 274 | ValTy != Type::LabelTy && "Unsupported type for printing"); |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 276 | const Value* scopeToUse = |
| 277 | isMethodExit ? (const Value*)BB->getParent() : (const Value*)BB; |
| 278 | |
| 279 | // Create the marker string... |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 280 | ostringstream scopeNameString; |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 281 | WriteAsOperand(scopeNameString, scopeToUse) << " : "; |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 282 | WriteAsOperand(scopeNameString, Val) << " = "; |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 283 | |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 284 | string fmtString(indent, ' '); |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 285 | fmtString += string(" At exit of") + scopeNameString.str(); |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 287 | // Turn the marker string into a global variable... |
| 288 | GlobalVariable *fmtVal = GetStringRef(Mod, fmtString); |
| 289 | |
| 290 | // Insert the first print instruction to print the string flag: |
| 291 | Instruction *I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()), |
| 292 | vector<Value*>(1, fmtVal)); |
| 293 | BBI = BB->getInstList().insert(BBI, I)+1; |
| 294 | |
| 295 | // Insert the next print instruction to print the value: |
| 296 | I = new CallInst(GetPrintMethodForType(Mod, ValTy), |
| 297 | vector<Value*>(1, Val)); |
| 298 | BBI = BB->getInstList().insert(BBI, I)+1; |
| 299 | |
| 300 | // Print out a newline |
Vikram S. Adve | c426c63 | 2001-10-28 22:44:02 +0000 | [diff] [blame] | 301 | fmtVal = GetStringRef(Mod, "\n"); |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 302 | I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()), |
| 303 | vector<Value*>(1, fmtVal)); |
| 304 | BBI = BB->getInstList().insert(BBI, I)+1; |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 308 | static LoadInst* |
| 309 | InsertLoadInst(StoreInst* storeInst, |
| 310 | BasicBlock *bb, |
| 311 | BasicBlock::iterator &BBI) |
| 312 | { |
| 313 | LoadInst* loadInst = new LoadInst(storeInst->getPtrOperand(), |
| 314 | storeInst->getIndexVec()); |
| 315 | BBI = bb->getInstList().insert(BBI, loadInst) + 1; |
| 316 | return loadInst; |
| 317 | } |
| 318 | |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 319 | |
| 320 | // |
| 321 | // Insert print instructions at the end of the basic block *bb |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 322 | // for each value in valueVec[] that is live at the end of that basic block, |
| 323 | // or that is stored to memory in this basic block. |
| 324 | // If the value is stored to memory, we load it back before printing |
| 325 | // We also return all such loaded values in the vector valuesStoredInMethod |
| 326 | // for printing at the exit from the method. (Note that in each invocation |
| 327 | // of the method, this will only get the last value stored for each static |
| 328 | // store instruction). |
| 329 | // *bb must be the block in which the value is computed; |
| 330 | // this is not checked here. |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 331 | // |
| 332 | static void |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 333 | TraceValuesAtBBExit(const vector<Instruction*>& valueVec, |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 334 | BasicBlock* bb, |
| 335 | Module* module, |
| 336 | unsigned int indent, |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 337 | bool isMethodExit, |
| 338 | vector<Instruction*>* valuesStoredInMethod) |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 339 | { |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 340 | // Get an iterator to point to the insertion location, which is |
| 341 | // just before the terminator instruction. |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 342 | // |
| 343 | BasicBlock::InstListType& instList = bb->getInstList(); |
Chris Lattner | 4457163 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 344 | BasicBlock::iterator here = instList.end()-1; |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 345 | assert((*here)->isTerminator()); |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 346 | |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 347 | // If the terminator is a conditional branch, insert the trace code just |
| 348 | // before the instruction that computes the branch condition (just to |
| 349 | // avoid putting a call between the CC-setting instruction and the branch). |
| 350 | // Use laterInstrSet to mark instructions that come after the setCC instr |
| 351 | // because those cannot be traced at the location we choose. |
| 352 | // |
| 353 | hash_set<Instruction*> laterInstrSet; |
| 354 | if (BranchInst* brInst = dyn_cast<BranchInst>(*here)) |
| 355 | if (! brInst->isUnconditional()) |
| 356 | if (Instruction* setCC = dyn_cast<Instruction>(brInst->getCondition())) |
| 357 | if (setCC->getParent() == bb) |
| 358 | { |
| 359 | while ((*here) != setCC && here != instList.begin()) |
| 360 | { |
| 361 | --here; |
| 362 | laterInstrSet.insert(*here); |
| 363 | } |
| 364 | assert((*here) == setCC && "Missed the setCC instruction?"); |
| 365 | laterInstrSet.insert(*here); |
| 366 | } |
| 367 | |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 368 | // Insert a print instruction for each value. |
| 369 | // |
| 370 | for (unsigned i=0, N=valueVec.size(); i < N; i++) |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 371 | { |
| 372 | Instruction* I = valueVec[i]; |
| 373 | if (I->getOpcode() == Instruction::Store) |
| 374 | { |
| 375 | assert(valuesStoredInMethod != NULL && |
| 376 | "Should not be printing a store instruction at method exit"); |
| 377 | I = InsertLoadInst((StoreInst*) I, bb, here); |
| 378 | valuesStoredInMethod->push_back(I); |
| 379 | } |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 380 | if (laterInstrSet.find(I) == laterInstrSet.end()) |
| 381 | InsertPrintInsts(I, bb, here, module, indent, isMethodExit); |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 382 | } |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 385 | |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 386 | |
| 387 | static Instruction* |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 388 | CreateMethodTraceInst(Method* method, |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 389 | unsigned int indent, |
| 390 | const string& msg) |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 391 | { |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 392 | string fmtString(indent, ' '); |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 393 | ostringstream methodNameString; |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 394 | WriteAsOperand(methodNameString, method); |
Vikram S. Adve | c426c63 | 2001-10-28 22:44:02 +0000 | [diff] [blame] | 395 | fmtString += msg + methodNameString.str() + '\n'; |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 396 | |
| 397 | GlobalVariable *fmtVal = GetStringRef(method->getParent(), fmtString); |
| 398 | Instruction *printInst = |
| 399 | new CallInst(GetPrintMethodForType(method->getParent(), fmtVal->getType()), |
| 400 | vector<Value*>(1, fmtVal)); |
| 401 | |
| 402 | return printInst; |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 405 | |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 406 | static inline void |
| 407 | InsertCodeToShowMethodEntry(Method* method, |
| 408 | BasicBlock* entryBB, |
| 409 | unsigned int indent) |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 410 | { |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 411 | // Get an iterator to point to the insertion location |
| 412 | BasicBlock::InstListType& instList = entryBB->getInstList(); |
| 413 | BasicBlock::iterator here = instList.begin(); |
| 414 | |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 415 | Instruction *printInst = CreateMethodTraceInst(method, indent, |
| 416 | "Entering Method"); |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 417 | |
| 418 | here = entryBB->getInstList().insert(here, printInst) + 1; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | static inline void |
| 423 | InsertCodeToShowMethodExit(Method* method, |
| 424 | BasicBlock* exitBB, |
| 425 | unsigned int indent) |
| 426 | { |
| 427 | // Get an iterator to point to the insertion location |
| 428 | BasicBlock::InstListType& instList = exitBB->getInstList(); |
| 429 | BasicBlock::iterator here = instList.end()-1; |
| 430 | assert((*here)->isTerminator()); |
| 431 | |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 432 | Instruction *printInst = CreateMethodTraceInst(method, indent, |
| 433 | "Leaving Method"); |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 434 | |
Chris Lattner | b81a0bf | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 435 | exitBB->getInstList().insert(here, printInst) + 1; |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 439 | //************************** External Functions ****************************/ |
| 440 | |
| 441 | |
| 442 | bool |
| 443 | InsertTraceCode::doInsertTraceCode(Method *M, |
| 444 | bool traceBasicBlockExits, |
| 445 | bool traceMethodExits) |
| 446 | { |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 447 | vector<Instruction*> valuesStoredInMethod; |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 448 | Module* module = M->getParent(); |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 449 | vector<BasicBlock*> exitBlocks; |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 451 | if (M->isExternal() || |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 452 | (! traceBasicBlockExits && ! traceMethodExits)) |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 453 | return false; |
Vikram S. Adve | d889330 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 454 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 455 | if (traceMethodExits) |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 456 | InsertCodeToShowMethodEntry(M, M->getEntryNode(), /*indent*/ 0); |
| 457 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 458 | for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) |
| 459 | { |
| 460 | BasicBlock* bb = *BI; |
| 461 | bool isExitBlock = false; |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 462 | vector<Instruction*> valuesToTraceInBB; |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 463 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 464 | FindValuesToTraceInBB(bb, valuesToTraceInBB); |
| 465 | |
| 466 | if (bb->succ_begin() == bb->succ_end()) |
| 467 | { // record this as an exit block |
| 468 | exitBlocks.push_back(bb); |
| 469 | isExitBlock = true; |
| 470 | } |
| 471 | |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 472 | if (traceBasicBlockExits) |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 473 | TraceValuesAtBBExit(valuesToTraceInBB, bb, module, |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 474 | /*indent*/ 4, /*isMethodExit*/ false, |
| 475 | &valuesStoredInMethod); |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 476 | } |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 477 | |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 478 | if (traceMethodExits) |
| 479 | for (unsigned i=0; i < exitBlocks.size(); ++i) |
| 480 | { |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 481 | TraceValuesAtBBExit(valuesStoredInMethod, exitBlocks[i], module, |
| 482 | /*indent*/ 0, /*isMethodExit*/ true, |
| 483 | /*valuesStoredInMethod*/ NULL); |
| 484 | InsertCodeToShowMethodExit(M, exitBlocks[i], /*indent*/ 0); |
Vikram S. Adve | bedb00d | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 485 | } |
Vikram S. Adve | 631b9a3 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 8d9e377 | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 487 | return true; |
Vikram S. Adve | df1892f | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 488 | } |