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