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