blob: 92aff1217dff6479a04aff6e2ee832bd80f45d0b [file] [log] [blame]
Chris Lattnerf1197b02001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
Chris Lattner7e358902002-04-14 06:15:24 +00003// Support for inserting LLVM code to print values at basic block and function
Chris Lattnerf1197b02001-12-14 16:26:05 +00004// exits.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advea200a6c2001-10-14 23:18:45 +00007
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
9#include "llvm/GlobalVariable.h"
Chris Lattnerca142372002-04-28 19:55:58 +000010#include "llvm/Constants.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000011#include "llvm/DerivedTypes.h"
Vikram S. Advea0db1c92001-10-18 18:16:11 +000012#include "llvm/iMemory.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000013#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Chris Lattnerd92b01c2002-04-09 18:37:46 +000015#include "llvm/BasicBlock.h"
Chris Lattner57698e22002-03-26 18:01:55 +000016#include "llvm/Function.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000017#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattnera0a8b5b2001-10-18 05:28:08 +000019#include "llvm/Assembly/Writer.h"
Vikram S. Adve47f37c32002-05-19 15:39:02 +000020#include "Support/CommandLine.h"
Chris Lattner5de22042001-11-27 00:03:19 +000021#include "Support/StringExtras.h"
Chris Lattnerace7b8d2002-05-20 21:43:59 +000022#include <algorithm>
Chris Lattnere2c61262001-10-18 20:06:03 +000023#include <sstream>
Chris Lattner7f74a562002-01-20 22:54:45 +000024using std::vector;
25using std::string;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000026
Chris Lattnerace7b8d2002-05-20 21:43:59 +000027static cl::Flag DisablePtrHashing("tracedisablehashdisable",
28 "Disable pointer hashing", cl::NoFlags);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000029
Chris Lattnerace7b8d2002-05-20 21:43:59 +000030static cl::StringList TraceFuncName ("tracefunc", "trace only specific funct"
31 "ions", cl::NoFlags);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000032
33
34// We trace a particular function if no functions to trace were specified
35// or if the function is in the specified list.
36//
37inline bool
38TraceThisFunction(Function* func)
39{
40 if (TraceFuncName.getNumOccurances() == 0)
41 return true;
Chris Lattnerace7b8d2002-05-20 21:43:59 +000042
43 return std::find(TraceFuncName.begin(), TraceFuncName.end(), func->getName())
44 != TraceFuncName.end();
Vikram S. Adve47f37c32002-05-19 15:39:02 +000045}
46
47
Chris Lattner04805fa2002-02-26 21:46:54 +000048namespace {
Chris Lattnerace7b8d2002-05-20 21:43:59 +000049 struct ExternalFuncs {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000050 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
51 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner113f4f42002-06-25 16:13:24 +000052 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve47f37c32002-05-19 15:39:02 +000053 };
54
Chris Lattnerc8e66542002-04-27 06:56:12 +000055 class InsertTraceCode : public FunctionPass {
Chris Lattner57698e22002-03-26 18:01:55 +000056 bool TraceBasicBlockExits, TraceFunctionExits;
Vikram S. Adve47f37c32002-05-19 15:39:02 +000057 ExternalFuncs externalFuncs;
Chris Lattner04805fa2002-02-26 21:46:54 +000058 public:
Chris Lattner57698e22002-03-26 18:01:55 +000059 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattner04805fa2002-02-26 21:46:54 +000060 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner57698e22002-03-26 18:01:55 +000061 TraceFunctionExits(traceFunctionExits) {}
Chris Lattner37104aa2002-04-29 14:57:45 +000062
63 const char *getPassName() const { return "Trace Code Insertion"; }
Chris Lattner04805fa2002-02-26 21:46:54 +000064
Vikram S. Adve47f37c32002-05-19 15:39:02 +000065 // Add a prototype for runtime functions not already in the program.
Chris Lattner04805fa2002-02-26 21:46:54 +000066 //
Chris Lattner113f4f42002-06-25 16:13:24 +000067 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000068
69 //--------------------------------------------------------------------------
70 // Function InsertCodeToTraceValues
71 //
Chris Lattner7e358902002-04-14 06:15:24 +000072 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner57698e22002-03-26 18:01:55 +000073 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000074 //
Chris Lattner57698e22002-03-26 18:01:55 +000075 static bool doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve47f37c32002-05-19 15:39:02 +000076 bool traceFunctionExits, ExternalFuncs& externalFuncs);
77
Chris Lattner7e358902002-04-14 06:15:24 +000078 // runOnFunction - This method does the work.
Chris Lattner04805fa2002-02-26 21:46:54 +000079 //
Chris Lattner113f4f42002-06-25 16:13:24 +000080 bool runOnFunction(Function &F) {
81 return doit(&F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs);
Chris Lattner04805fa2002-02-26 21:46:54 +000082 }
Chris Lattnerf12cc842002-04-28 21:27:06 +000083
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.preservesCFG();
86 }
Chris Lattner04805fa2002-02-26 21:46:54 +000087 };
88} // end anonymous namespace
89
90
Chris Lattnerc8e66542002-04-27 06:56:12 +000091Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner04805fa2002-02-26 21:46:54 +000092 return new InsertTraceCode(false, true);
93}
94
Chris Lattner7e358902002-04-14 06:15:24 +000095Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner04805fa2002-02-26 21:46:54 +000096 return new InsertTraceCode(true, true);
97}
98
Vikram S. Adve47f37c32002-05-19 15:39:02 +000099// Add a prototype for external functions used by the tracing code.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000100//
Chris Lattner113f4f42002-06-25 16:13:24 +0000101void ExternalFuncs::doInitialization(Module &M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000102 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattnere2f2f542002-04-04 22:19:18 +0000103 const FunctionType *MTy =
104 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000106
107 // uint (sbyte*)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000108 const FunctionType *hashFuncTy =
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000109 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner113f4f42002-06-25 16:13:24 +0000110 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000111
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000112 // void (sbyte*)
113 const FunctionType *voidSBPFuncTy =
114 FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000115
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
117 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000118
119 const FunctionType *voidvoidFuncTy =
120 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
121
Chris Lattner113f4f42002-06-25 16:13:24 +0000122 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
123 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000124 voidvoidFuncTy);
125}
126
127
128// Add a prototype for external functions used by the tracing code.
129//
Chris Lattner113f4f42002-06-25 16:13:24 +0000130bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000131 externalFuncs.doInitialization(M);
Chris Lattnerc46dcca2002-03-29 03:43:24 +0000132 return false;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000133}
134
Chris Lattnerf1197b02001-12-14 16:26:05 +0000135
136static inline GlobalVariable *getStringRef(Module *M, const string &str) {
137 // Create a constant internal string reference...
138 Constant *Init = ConstantArray::get(str);
Vikram S. Adve9f129ff2002-03-18 03:40:25 +0000139
140 // Create the global variable and record it in the module
141 // The GV will be renamed to a unique name if needed.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000142 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
143 "trstr");
Chris Lattnere2c61262001-10-18 20:06:03 +0000144 M->getGlobalList().push_back(GV);
145 return GV;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000146}
147
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000148
Chris Lattnerf1197b02001-12-14 16:26:05 +0000149//
150// Check if this instruction has any uses outside its basic block,
151// or if it used by either a Call or Return instruction.
152//
153static inline bool LiveAtBBExit(const Instruction* I) {
154 const BasicBlock *BB = I->getParent();
155 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
156 if (const Instruction *UI = dyn_cast<Instruction>(*U))
157 if (UI->getParent() != BB || isa<ReturnInst>(UI))
158 return true;
159
160 return false;
161}
162
163
164static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000165 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000166 // be traced by default (VoidTy's are already excluded)
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000167 //
168 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000169 opCode != Instruction::Alloca &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000170 opCode != Instruction::PHINode &&
171 opCode != Instruction::Cast);
172}
173
Chris Lattnerf1197b02001-12-14 16:26:05 +0000174
175static bool ShouldTraceValue(const Instruction *I) {
176 return
177 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
178 TraceThisOpCode(I->getOpcode());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000179}
180
Chris Lattnerf1197b02001-12-14 16:26:05 +0000181static string getPrintfCodeFor(const Value *V) {
182 if (V == 0) return "";
Chris Lattner7e358902002-04-14 06:15:24 +0000183 if (V->getType()->isFloatingPoint())
184 return "%g";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000185 else if (V->getType() == Type::LabelTy)
Chris Lattner7e358902002-04-14 06:15:24 +0000186 return "0x%p";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000187 else if (isa<PointerType>(V->getType()))
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000188 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner7e358902002-04-14 06:15:24 +0000189 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattnerf1197b02001-12-14 16:26:05 +0000190 return "%d";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000191
Chris Lattner7e358902002-04-14 06:15:24 +0000192 assert(0 && "Illegal value to print out...");
193 return "";
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000194}
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000195
196
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000197static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
198 string Message,
199 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000200 // Escape Message by replacing all % characters with %% chars.
201 unsigned Offset = 0;
202 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner7e358902002-04-14 06:15:24 +0000203 Message.replace(Offset, 1, "%%");
Chris Lattnerf1197b02001-12-14 16:26:05 +0000204 Offset += 2; // Skip over the new %'s
205 }
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000206
Chris Lattnerf1197b02001-12-14 16:26:05 +0000207 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000208
Chris Lattner5309e102001-10-18 06:03:05 +0000209 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000210 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
211
212 // Turn the format string into an sbyte *
213 Instruction *GEP =
214 new GetElementPtrInst(fmtVal,
215 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
216 "trstr");
Chris Lattner113f4f42002-06-25 16:13:24 +0000217 BBI = ++BB->getInstList().insert(BBI, GEP);
Chris Lattner5309e102001-10-18 06:03:05 +0000218
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000219 // Insert a call to the hash function if this is a pointer value
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000220 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
221 const Type *SBP = PointerType::get(Type::SByteTy);
222 if (V->getType() != SBP) { // Cast pointer to be sbyte*
223 Instruction *I = new CastInst(V, SBP, "Hash_cast");
Chris Lattner113f4f42002-06-25 16:13:24 +0000224 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000225 V = I;
226 }
227
228 vector<Value*> HashArgs(1, V);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000229 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
Chris Lattner113f4f42002-06-25 16:13:24 +0000230 BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000231 }
232
Chris Lattner5309e102001-10-18 06:03:05 +0000233 // Insert the first print instruction to print the string flag:
Chris Lattnerf1197b02001-12-14 16:26:05 +0000234 vector<Value*> PrintArgs;
235 PrintArgs.push_back(GEP);
236 if (V) PrintArgs.push_back(V);
237 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner113f4f42002-06-25 16:13:24 +0000238 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000239}
240
Chris Lattner5309e102001-10-18 06:03:05 +0000241
Chris Lattnerf1197b02001-12-14 16:26:05 +0000242static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
243 BasicBlock::iterator &BBI,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000244 const string &Message, Function *Printf,
245 Function* HashPtrToSeqNum) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000246 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000247 if (V) WriteAsOperand(OutStr, V);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000248 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
249 Printf, HashPtrToSeqNum);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000250}
251
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000252static void
253InsertReleaseInst(Value *V, BasicBlock *BB,
254 BasicBlock::iterator &BBI,
255 Function* ReleasePtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000256
257 const Type *SBP = PointerType::get(Type::SByteTy);
258 if (V->getType() != SBP) { // Cast pointer to be sbyte*
259 Instruction *I = new CastInst(V, SBP, "RPSN_cast");
Chris Lattner113f4f42002-06-25 16:13:24 +0000260 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000261 V = I;
262 }
263 vector<Value*> releaseArgs(1, V);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000264 Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
Chris Lattner113f4f42002-06-25 16:13:24 +0000265 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000266}
267
268static void
269InsertRecordInst(Value *V, BasicBlock *BB,
270 BasicBlock::iterator &BBI,
271 Function* RecordPtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000272 const Type *SBP = PointerType::get(Type::SByteTy);
273 if (V->getType() != SBP) { // Cast pointer to be sbyte*
274 Instruction *I = new CastInst(V, SBP, "RP_cast");
Chris Lattner113f4f42002-06-25 16:13:24 +0000275 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000276 V = I;
277 }
278 vector<Value*> releaseArgs(1, V);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000279 Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
Chris Lattner113f4f42002-06-25 16:13:24 +0000280 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000281}
282
283static void
284InsertPushOnEntryFunc(Function *M,
285 Function* PushOnEntryFunc) {
286 // Get an iterator to point to the insertion location
Chris Lattner113f4f42002-06-25 16:13:24 +0000287 BasicBlock &BB = M->getEntryNode();
288 BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
289 vector<Value*>()));
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000290}
291
292static void
293InsertReleaseRecordedInst(BasicBlock *BB,
294 Function* ReleaseOnReturnFunc) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000295 BasicBlock::iterator BBI = BB->end()--;
296 BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
297 vector<Value*>()));
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000298}
299
300// Look for alloca and free instructions. These are the ptrs to release.
301// Release the free'd pointers immediately. Record the alloca'd pointers
302// to be released on return from the current function.
303//
304static void
305ReleasePtrSeqNumbers(BasicBlock *BB,
306 ExternalFuncs& externalFuncs) {
307
308 for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000309 if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000310 InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
Chris Lattner113f4f42002-06-25 16:13:24 +0000311 else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000312 {
Chris Lattner113f4f42002-06-25 16:13:24 +0000313 BasicBlock::iterator nextI = ++II;
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000314 InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
Chris Lattner113f4f42002-06-25 16:13:24 +0000315 II = --nextI;
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000316 }
317 }
318}
319
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000320
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000321// Insert print instructions at the end of the basic block *bb
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000322// 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
Chris Lattner57698e22002-03-26 18:01:55 +0000325// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner7e358902002-04-14 06:15:24 +0000326// for printing at the exit from the function. (Note that in each invocation
327// of the function, this will only get the last value stored for each static
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000328// store instruction).
329// *bb must be the block in which the value is computed;
330// this is not checked here.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000331//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000332static void TraceValuesAtBBExit(BasicBlock *BB,
333 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner57698e22002-03-26 18:01:55 +0000334 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000335 // Get an iterator to point to the insertion location, which is
336 // just before the terminator instruction.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000337 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000338 BasicBlock::iterator InsertPos = BB->end()--;
339 assert(BB->back().isTerminator());
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000340
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000341 // If the terminator is a conditional branch, insert the trace code just
342 // before the instruction that computes the branch condition (just to
343 // avoid putting a call between the CC-setting instruction and the branch).
344 // Use laterInstrSet to mark instructions that come after the setCC instr
345 // because those cannot be traced at the location we choose.
346 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000347 Instruction *SetCC = 0;
348 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
349 if (!Branch->isUnconditional())
350 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
351 if (I->getParent() == BB) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000352 InsertPos = SetCC = I; // Back up until we can insert before the setcc
Chris Lattnerf1197b02001-12-14 16:26:05 +0000353 }
354
Chris Lattner7f74a562002-01-20 22:54:45 +0000355 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000356 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000357 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
358 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000359
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000360 // Insert a print instruction for each value.
361 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000362 for (BasicBlock::iterator II = BB->begin(), IE = InsertPos++; II != IE; ++II){
363 if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000364 assert(valuesStoredInFunction &&
Chris Lattner7e358902002-04-14 06:15:24 +0000365 "Should not be printing a store instruction at function exit");
Chris Lattnerf1197b02001-12-14 16:26:05 +0000366 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
Chris Lattner113f4f42002-06-25 16:13:24 +0000367 "reload."+SI->getPointerOperand()->getName());
368 InsertPos = ++BB->getInstList().insert(InsertPos, LI);
Chris Lattner57698e22002-03-26 18:01:55 +0000369 valuesStoredInFunction->push_back(LI);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000370 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000371 if (ShouldTraceValue(II))
372 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000373 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000374}
375
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000376static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf,
377 Function* HashPtrToSeqNum){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000378 // Get an iterator to point to the insertion location
Chris Lattner113f4f42002-06-25 16:13:24 +0000379 BasicBlock &BB = M->getEntryNode();
380 BasicBlock::iterator BBI = BB.begin();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000381
Chris Lattner7f74a562002-01-20 22:54:45 +0000382 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000383 WriteAsOperand(OutStr, M, true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000384 InsertPrintInst(0, &BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000385 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000386
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000387 // Now print all the incoming arguments
Chris Lattnerf1197b02001-12-14 16:26:05 +0000388 unsigned ArgNo = 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000389 for (Function::aiterator I = M->abegin(), E = M->aend(); I != E; ++I,++ArgNo){
390 InsertVerbosePrintInst(I, &BB, BBI,
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000391 " Arg #" + utostr(ArgNo) + ": ", Printf,
392 HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000393 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000394}
395
396
Chris Lattner57698e22002-03-26 18:01:55 +0000397static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000398 Function *Printf,
399 Function* HashPtrToSeqNum) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000400 // Get an iterator to point to the insertion location
Chris Lattner113f4f42002-06-25 16:13:24 +0000401 BasicBlock::iterator BBI = BB->end()--;
402 ReturnInst &Ret = cast<ReturnInst>(BB->back());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000403
Chris Lattner7f74a562002-01-20 22:54:45 +0000404 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000405 WriteAsOperand(OutStr, BB->getParent(), true);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000406 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
407 Printf, HashPtrToSeqNum);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000408
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000409 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000410 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner113f4f42002-06-25 16:13:24 +0000411 InsertPrintInst(Ret.getReturnValue(), BB, BBI, " Returning: ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000412 Printf, HashPtrToSeqNum);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000413}
414
415
Chris Lattner57698e22002-03-26 18:01:55 +0000416bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000417 bool traceFunctionEvents,
418 ExternalFuncs& externalFuncs) {
Chris Lattner57698e22002-03-26 18:01:55 +0000419 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000420 return false;
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000421
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000422 if (!TraceThisFunction(M))
423 return false;
424
Chris Lattner57698e22002-03-26 18:01:55 +0000425 vector<Instruction*> valuesStoredInFunction;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000426 vector<BasicBlock*> exitBlocks;
427
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000428 // Insert code to trace values at function entry
Chris Lattner57698e22002-03-26 18:01:55 +0000429 if (traceFunctionEvents)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000430 InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc,
431 externalFuncs.HashPtrFunc);
432
433 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000434 if (!DisablePtrHashing)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000435 InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000436
Chris Lattner113f4f42002-06-25 16:13:24 +0000437 for (Function::iterator BB = M->begin(); BB != M->end(); ++BB) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000438 if (isa<ReturnInst>(BB->getTerminator()))
439 exitBlocks.push_back(BB); // record this as an exit block
440
441 if (traceBasicBlockExits)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000442 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
443 externalFuncs.HashPtrFunc, &valuesStoredInFunction);
444
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000445 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000446 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000447 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000448
449 for (unsigned i=0; i < exitBlocks.size(); ++i)
450 {
451 // Insert code to trace values at function exit
452 if (traceFunctionEvents)
453 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
454 externalFuncs.HashPtrFunc);
455
456 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000457 if (!DisablePtrHashing)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000458 InsertReleaseRecordedInst(exitBlocks[i],
459 externalFuncs.ReleaseOnReturnFunc);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000460 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000461
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000462 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000463}