Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 1 | //===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=// |
| 2 | // |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 3 | // Support for inserting LLVM code to print values at basic block and function |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 4 | // exits. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 7 | |
| 8 | #include "llvm/Transforms/Instrumentation/TraceValues.h" |
| 9 | #include "llvm/GlobalVariable.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 10 | #include "llvm/Constants.h" |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 11 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 12 | #include "llvm/iMemory.h" |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 13 | #include "llvm/iTerminators.h" |
| 14 | #include "llvm/iOther.h" |
Chris Lattner | d92b01c | 2002-04-09 18:37:46 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/Writer.h" |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 20 | #include "Support/CommandLine.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 21 | #include "Support/StringExtras.h" |
Chris Lattner | e2c6126 | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 22 | #include <sstream> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 23 | using std::vector; |
| 24 | using std::string; |
Vikram S. Adve | 96f6ac9 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 25 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 26 | |
| 27 | enum TraceHashPtrOpt { |
| 28 | HashToSeqNum, NoHash |
| 29 | }; |
| 30 | |
| 31 | static cl::Enum<enum TraceHashPtrOpt> TraceHashPtrs("tracehash", cl::NoFlags, |
| 32 | "Hash pointer values when tracing", |
| 33 | clEnumValN(HashToSeqNum, "on", "Hash pointers to sequence number"), |
| 34 | clEnumValN(NoHash , "off","Disable hashing of pointers"), 0); |
| 35 | |
| 36 | |
| 37 | static cl::StringList TraceFuncName ("tracefunc", "trace these functions", cl::NoFlags); |
| 38 | |
| 39 | |
| 40 | // We trace a particular function if no functions to trace were specified |
| 41 | // or if the function is in the specified list. |
| 42 | // |
| 43 | inline bool |
| 44 | TraceThisFunction(Function* func) |
| 45 | { |
| 46 | if (TraceFuncName.getNumOccurances() == 0) |
| 47 | return true; |
| 48 | |
| 49 | for (std::vector<std::string>::const_iterator SI=TraceFuncName.begin(), |
| 50 | SE=TraceFuncName.end(); SI != SE; ++SI) |
| 51 | if (func->getName() == *SI) |
| 52 | return true; |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 58 | namespace { |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 59 | class ExternalFuncs { |
| 60 | public: |
| 61 | Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc; |
| 62 | Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc; |
| 63 | void doInitialization(Module *M); // Add prototypes for external functions |
| 64 | }; |
| 65 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 66 | class InsertTraceCode : public FunctionPass { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 67 | bool TraceBasicBlockExits, TraceFunctionExits; |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 68 | ExternalFuncs externalFuncs; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 69 | public: |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 70 | InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits) |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 71 | : TraceBasicBlockExits(traceBasicBlockExits), |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 72 | TraceFunctionExits(traceFunctionExits) {} |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 73 | |
| 74 | const char *getPassName() const { return "Trace Code Insertion"; } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 75 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 76 | // Add a prototype for runtime functions not already in the program. |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 77 | // |
| 78 | bool doInitialization(Module *M); |
| 79 | |
| 80 | //-------------------------------------------------------------------------- |
| 81 | // Function InsertCodeToTraceValues |
| 82 | // |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 83 | // Inserts tracing code for all live values at basic block and/or function |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 84 | // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'. |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 85 | // |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 86 | static bool doit(Function *M, bool traceBasicBlockExits, |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 87 | bool traceFunctionExits, ExternalFuncs& externalFuncs); |
| 88 | |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 89 | // runOnFunction - This method does the work. |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 90 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 91 | bool runOnFunction(Function *F) { |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 92 | return doit(F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 94 | |
| 95 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 96 | AU.preservesCFG(); |
| 97 | } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 98 | }; |
| 99 | } // end anonymous namespace |
| 100 | |
| 101 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 102 | Pass *createTraceValuesPassForFunction() { // Just trace functions |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 103 | return new InsertTraceCode(false, true); |
| 104 | } |
| 105 | |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 106 | Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 107 | return new InsertTraceCode(true, true); |
| 108 | } |
| 109 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 110 | // Add a prototype for external functions used by the tracing code. |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 111 | // |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 112 | void ExternalFuncs::doInitialization(Module *M) { |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 113 | const Type *SBP = PointerType::get(Type::SByteTy); |
Chris Lattner | e2f2f54 | 2002-04-04 22:19:18 +0000 | [diff] [blame] | 114 | const FunctionType *MTy = |
| 115 | FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true); |
Chris Lattner | c46dcca | 2002-03-29 03:43:24 +0000 | [diff] [blame] | 116 | PrintfFunc = M->getOrInsertFunction("printf", MTy); |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 117 | |
| 118 | // Use varargs functions with no args instead of func(sbyte*) so that |
| 119 | // we don't have to generate cast instructions below :-) |
| 120 | // |
| 121 | const FunctionType *hashFuncTy = |
| 122 | FunctionType::get(Type::UIntTy, vector<const Type*>(), true); |
| 123 | HashPtrFunc = M->getOrInsertFunction("HashPointerToSeqNum", hashFuncTy); |
| 124 | |
| 125 | // varargs again, same reason. |
| 126 | const FunctionType *voidVAFuncTy = |
| 127 | FunctionType::get(Type::VoidTy, vector<const Type*>(), true); |
| 128 | |
| 129 | ReleasePtrFunc =M->getOrInsertFunction("ReleasePointerSeqNum", voidVAFuncTy); |
| 130 | RecordPtrFunc = M->getOrInsertFunction("RecordPointer", voidVAFuncTy); |
| 131 | |
| 132 | const FunctionType *voidvoidFuncTy = |
| 133 | FunctionType::get(Type::VoidTy, vector<const Type*>(), false); |
| 134 | |
| 135 | PushOnEntryFunc = M->getOrInsertFunction("PushPointerSet", voidvoidFuncTy); |
| 136 | ReleaseOnReturnFunc = M->getOrInsertFunction("ReleasePointersPopSet", |
| 137 | voidvoidFuncTy); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | // Add a prototype for external functions used by the tracing code. |
| 142 | // |
| 143 | bool InsertTraceCode::doInitialization(Module *M) { |
| 144 | externalFuncs.doInitialization(M); |
Chris Lattner | c46dcca | 2002-03-29 03:43:24 +0000 | [diff] [blame] | 145 | return false; |
Vikram S. Adve | 96f6ac9 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 148 | |
| 149 | static inline GlobalVariable *getStringRef(Module *M, const string &str) { |
| 150 | // Create a constant internal string reference... |
| 151 | Constant *Init = ConstantArray::get(str); |
Vikram S. Adve | 9f129ff | 2002-03-18 03:40:25 +0000 | [diff] [blame] | 152 | |
| 153 | // Create the global variable and record it in the module |
| 154 | // The GV will be renamed to a unique name if needed. |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 155 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init, |
| 156 | "trstr"); |
Chris Lattner | e2c6126 | 2001-10-18 20:06:03 +0000 | [diff] [blame] | 157 | M->getGlobalList().push_back(GV); |
| 158 | return GV; |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Vikram S. Adve | 7ac553a | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 161 | |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 162 | // |
| 163 | // Check if this instruction has any uses outside its basic block, |
| 164 | // or if it used by either a Call or Return instruction. |
| 165 | // |
| 166 | static inline bool LiveAtBBExit(const Instruction* I) { |
| 167 | const BasicBlock *BB = I->getParent(); |
| 168 | for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U) |
| 169 | if (const Instruction *UI = dyn_cast<Instruction>(*U)) |
| 170 | if (UI->getParent() != BB || isa<ReturnInst>(UI)) |
| 171 | return true; |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | static inline bool TraceThisOpCode(unsigned opCode) { |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 178 | // 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] | 179 | // be traced by default (VoidTy's are already excluded) |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 180 | // |
| 181 | return (opCode < Instruction::FirstOtherOp && |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 182 | opCode != Instruction::Alloca && |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 183 | opCode != Instruction::PHINode && |
| 184 | opCode != Instruction::Cast); |
| 185 | } |
| 186 | |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 187 | |
| 188 | static bool ShouldTraceValue(const Instruction *I) { |
| 189 | return |
| 190 | I->getType() != Type::VoidTy && LiveAtBBExit(I) && |
| 191 | TraceThisOpCode(I->getOpcode()); |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 194 | static string getPrintfCodeFor(const Value *V) { |
| 195 | if (V == 0) return ""; |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 196 | if (V->getType()->isFloatingPoint()) |
| 197 | return "%g"; |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 198 | else if (V->getType() == Type::LabelTy) |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 199 | return "0x%p"; |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 200 | else if (isa<PointerType>(V->getType())) |
| 201 | return (TraceHashPtrs == NoHash)? "0x%p" : "%d"; |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 202 | else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy) |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 203 | return "%d"; |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 205 | assert(0 && "Illegal value to print out..."); |
| 206 | return ""; |
Vikram S. Adve | 7ac553a | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 207 | } |
Vikram S. Adve | 7ac553a | 2001-10-18 13:49:22 +0000 | [diff] [blame] | 208 | |
| 209 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 210 | static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI, |
| 211 | string Message, |
| 212 | Function *Printf, Function* HashPtrToSeqNum) { |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 213 | // Escape Message by replacing all % characters with %% chars. |
| 214 | unsigned Offset = 0; |
| 215 | while ((Offset = Message.find('%', Offset)) != string::npos) { |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 216 | Message.replace(Offset, 1, "%%"); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 217 | Offset += 2; // Skip over the new %'s |
| 218 | } |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 219 | |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 220 | Module *Mod = BB->getParent()->getParent(); |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 222 | // Turn the marker string into a global variable... |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 223 | GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n"); |
| 224 | |
| 225 | // Turn the format string into an sbyte * |
| 226 | Instruction *GEP = |
| 227 | new GetElementPtrInst(fmtVal, |
| 228 | vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)), |
| 229 | "trstr"); |
| 230 | BBI = BB->getInstList().insert(BBI, GEP)+1; |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 231 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 232 | // Insert a call to the hash function if this is a pointer value |
| 233 | if (V && isa<PointerType>(V->getType()) && TraceHashPtrs == HashToSeqNum) { |
| 234 | vector<Value*> HashArgs; |
| 235 | HashArgs.push_back(V); |
| 236 | V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum"); |
| 237 | BBI = BB->getInstList().insert(BBI, cast<Instruction>(V))+1; |
| 238 | } |
| 239 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 240 | // Insert the first print instruction to print the string flag: |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 241 | vector<Value*> PrintArgs; |
| 242 | PrintArgs.push_back(GEP); |
| 243 | if (V) PrintArgs.push_back(V); |
| 244 | Instruction *I = new CallInst(Printf, PrintArgs, "trace"); |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 245 | BBI = BB->getInstList().insert(BBI, I)+1; |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Chris Lattner | 5309e10 | 2001-10-18 06:03:05 +0000 | [diff] [blame] | 248 | |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 249 | static void InsertVerbosePrintInst(Value *V, BasicBlock *BB, |
| 250 | BasicBlock::iterator &BBI, |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 251 | const string &Message, Function *Printf, |
| 252 | Function* HashPtrToSeqNum) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 253 | std::ostringstream OutStr; |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 254 | if (V) WriteAsOperand(OutStr, V); |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 255 | InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", |
| 256 | Printf, HashPtrToSeqNum); |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 259 | static void |
| 260 | InsertReleaseInst(Value *V, BasicBlock *BB, |
| 261 | BasicBlock::iterator &BBI, |
| 262 | Function* ReleasePtrFunc) { |
| 263 | vector<Value*> releaseArgs; |
| 264 | releaseArgs.push_back(V); |
| 265 | Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs); |
| 266 | BBI = BB->getInstList().insert(BBI, I)+1; |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | InsertRecordInst(Value *V, BasicBlock *BB, |
| 271 | BasicBlock::iterator &BBI, |
| 272 | Function* RecordPtrFunc) { |
| 273 | vector<Value*> releaseArgs; |
| 274 | releaseArgs.push_back(V); |
| 275 | Instruction *I = new CallInst(RecordPtrFunc, releaseArgs); |
| 276 | BBI = BB->getInstList().insert(BBI, I)+1; |
| 277 | } |
| 278 | |
| 279 | static void |
| 280 | InsertPushOnEntryFunc(Function *M, |
| 281 | Function* PushOnEntryFunc) { |
| 282 | // Get an iterator to point to the insertion location |
| 283 | BasicBlock *BB = M->getEntryNode(); |
| 284 | BB->getInstList().insert(BB->begin(), new CallInst(PushOnEntryFunc, |
| 285 | vector<Value*> ())); |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | InsertReleaseRecordedInst(BasicBlock *BB, |
| 290 | Function* ReleaseOnReturnFunc) { |
| 291 | BasicBlock::iterator BBI = BB->end()-1; |
| 292 | BBI = 1 + BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc, |
| 293 | vector<Value*>())); |
| 294 | } |
| 295 | |
| 296 | // Look for alloca and free instructions. These are the ptrs to release. |
| 297 | // Release the free'd pointers immediately. Record the alloca'd pointers |
| 298 | // to be released on return from the current function. |
| 299 | // |
| 300 | static void |
| 301 | ReleasePtrSeqNumbers(BasicBlock *BB, |
| 302 | ExternalFuncs& externalFuncs) { |
| 303 | |
| 304 | for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) { |
| 305 | if (FreeInst *FI = dyn_cast<FreeInst>(*II)) |
| 306 | InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc); |
| 307 | else if (AllocaInst *AI = dyn_cast<AllocaInst>(*II)) |
| 308 | { |
| 309 | BasicBlock::iterator nextI = II+1; |
| 310 | InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc); |
| 311 | II = nextI - 1; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 316 | |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 317 | // Insert print instructions at the end of the basic block *bb |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 318 | // for each value in valueVec[] that is live at the end of that basic block, |
| 319 | // or that is stored to memory in this basic block. |
| 320 | // If the value is stored to memory, we load it back before printing |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 321 | // We also return all such loaded values in the vector valuesStoredInFunction |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 322 | // for printing at the exit from the function. (Note that in each invocation |
| 323 | // of the function, this will only get the last value stored for each static |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 324 | // store instruction). |
| 325 | // *bb must be the block in which the value is computed; |
| 326 | // this is not checked here. |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 327 | // |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 328 | static void TraceValuesAtBBExit(BasicBlock *BB, |
| 329 | Function *Printf, Function* HashPtrToSeqNum, |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 330 | vector<Instruction*> *valuesStoredInFunction) { |
Vikram S. Adve | 96f6ac9 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 331 | // Get an iterator to point to the insertion location, which is |
| 332 | // just before the terminator instruction. |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 333 | // |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 334 | BasicBlock::iterator InsertPos = BB->end()-1; |
| 335 | assert((*InsertPos)->isTerminator()); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 336 | |
Vikram S. Adve | 96f6ac9 | 2001-10-28 21:37:25 +0000 | [diff] [blame] | 337 | // If the terminator is a conditional branch, insert the trace code just |
| 338 | // before the instruction that computes the branch condition (just to |
| 339 | // avoid putting a call between the CC-setting instruction and the branch). |
| 340 | // Use laterInstrSet to mark instructions that come after the setCC instr |
| 341 | // because those cannot be traced at the location we choose. |
| 342 | // |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 343 | Instruction *SetCC = 0; |
| 344 | if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator())) |
| 345 | if (!Branch->isUnconditional()) |
| 346 | if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition())) |
| 347 | if (I->getParent() == BB) { |
| 348 | SetCC = I; |
| 349 | while (*InsertPos != SetCC) |
| 350 | --InsertPos; // Back up until we can insert before the setcc |
| 351 | } |
| 352 | |
| 353 | // Copy all of the instructions into a vector to avoid problems with Setcc |
| 354 | const vector<Instruction*> Insts(BB->begin(), InsertPos); |
| 355 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 356 | std::ostringstream OutStr; |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 357 | WriteAsOperand(OutStr, BB, false); |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 358 | InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), |
| 359 | Printf, HashPtrToSeqNum); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 360 | |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 361 | // Insert a print instruction for each value. |
| 362 | // |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 363 | for (vector<Instruction*>::const_iterator II = Insts.begin(), |
| 364 | IE = Insts.end(); II != IE; ++II) { |
| 365 | Instruction *I = *II; |
| 366 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 367 | assert(valuesStoredInFunction && |
Chris Lattner | 7e35890 | 2002-04-14 06:15:24 +0000 | [diff] [blame] | 368 | "Should not be printing a store instruction at function exit"); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 369 | LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(), |
| 370 | "reload"); |
| 371 | InsertPos = BB->getInstList().insert(InsertPos, LI) + 1; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 372 | valuesStoredInFunction->push_back(LI); |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 373 | } |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 374 | if (ShouldTraceValue(I)) |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 375 | InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf, HashPtrToSeqNum); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 376 | } |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 379 | static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf, |
| 380 | Function* HashPtrToSeqNum){ |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 381 | // Get an iterator to point to the insertion location |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 382 | BasicBlock *BB = M->getEntryNode(); |
| 383 | BasicBlock::iterator BBI = BB->begin(); |
| 384 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 385 | std::ostringstream OutStr; |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 386 | WriteAsOperand(OutStr, M, true); |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 387 | InsertPrintInst(0, BB, BBI, "ENTERING FUNCTION: " + OutStr.str(), |
| 388 | Printf, HashPtrToSeqNum); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 389 | |
Vikram S. Adve | b601fda | 2001-11-15 15:00:16 +0000 | [diff] [blame] | 390 | // Now print all the incoming arguments |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 391 | const Function::ArgumentListType &argList = M->getArgumentList(); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 392 | unsigned ArgNo = 0; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 393 | for (Function::ArgumentListType::const_iterator |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 394 | I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) { |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 395 | InsertVerbosePrintInst((Value*)*I, BB, BBI, |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 396 | " Arg #" + utostr(ArgNo), Printf, HashPtrToSeqNum); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 397 | } |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 401 | static inline void InsertCodeToShowFunctionExit(BasicBlock *BB, |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 402 | Function *Printf, |
| 403 | Function* HashPtrToSeqNum) { |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 404 | // Get an iterator to point to the insertion location |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 405 | BasicBlock::iterator BBI = BB->end()-1; |
| 406 | ReturnInst *Ret = cast<ReturnInst>(*BBI); |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 408 | std::ostringstream OutStr; |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 409 | WriteAsOperand(OutStr, BB->getParent(), true); |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 410 | InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(), |
| 411 | Printf, HashPtrToSeqNum); |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 412 | |
Vikram S. Adve | b601fda | 2001-11-15 15:00:16 +0000 | [diff] [blame] | 413 | // print the return value, if any |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 414 | if (BB->getParent()->getReturnType() != Type::VoidTy) |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 415 | InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", |
| 416 | Printf, HashPtrToSeqNum); |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 420 | bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits, |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 421 | bool traceFunctionEvents, |
| 422 | ExternalFuncs& externalFuncs) { |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 423 | if (!traceBasicBlockExits && !traceFunctionEvents) |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 424 | return false; |
Vikram S. Adve | a0db1c9 | 2001-10-18 18:16:11 +0000 | [diff] [blame] | 425 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 426 | if (!TraceThisFunction(M)) |
| 427 | return false; |
| 428 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 429 | vector<Instruction*> valuesStoredInFunction; |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 430 | vector<BasicBlock*> exitBlocks; |
| 431 | |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 432 | // Insert code to trace values at function entry |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 433 | if (traceFunctionEvents) |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 434 | InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc, |
| 435 | externalFuncs.HashPtrFunc); |
| 436 | |
| 437 | // Push a pointer set for recording alloca'd pointers at entry. |
| 438 | if (TraceHashPtrs == HashToSeqNum) |
| 439 | InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 440 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 441 | for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) { |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 442 | BasicBlock *BB = *BI; |
| 443 | if (isa<ReturnInst>(BB->getTerminator())) |
| 444 | exitBlocks.push_back(BB); // record this as an exit block |
| 445 | |
| 446 | if (traceBasicBlockExits) |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 447 | TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc, |
| 448 | externalFuncs.HashPtrFunc, &valuesStoredInFunction); |
| 449 | |
| 450 | if (TraceHashPtrs == HashToSeqNum) // release seq. numbers on free/ret |
| 451 | ReleasePtrSeqNumbers(BB, externalFuncs); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 452 | } |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 453 | |
| 454 | for (unsigned i=0; i < exitBlocks.size(); ++i) |
| 455 | { |
| 456 | // Insert code to trace values at function exit |
| 457 | if (traceFunctionEvents) |
| 458 | InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc, |
| 459 | externalFuncs.HashPtrFunc); |
| 460 | |
| 461 | // Release all recorded pointers before RETURN. Do this LAST! |
| 462 | if (TraceHashPtrs == HashToSeqNum) |
| 463 | InsertReleaseRecordedInst(exitBlocks[i], |
| 464 | externalFuncs.ReleaseOnReturnFunc); |
Chris Lattner | f1197b0 | 2001-12-14 16:26:05 +0000 | [diff] [blame] | 465 | } |
Vikram S. Adve | 47f37c3 | 2002-05-19 15:39:02 +0000 | [diff] [blame] | 466 | |
Chris Lattner | a0a8b5b | 2001-10-18 05:28:08 +0000 | [diff] [blame] | 467 | return true; |
Vikram S. Adve | a200a6c | 2001-10-14 23:18:45 +0000 | [diff] [blame] | 468 | } |