blob: 3e0cb4b7f48813ebed0084cd056160a42d4f9e93 [file] [log] [blame]
Chris Lattner29c14732001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
Chris Lattner649f5dd2002-04-14 06:15:24 +00003// Support for inserting LLVM code to print values at basic block and function
Chris Lattner29c14732001-12-14 16:26:05 +00004// exits.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advedf1892f2001-10-14 23:18:45 +00007
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
9#include "llvm/GlobalVariable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000010#include "llvm/Constants.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000011#include "llvm/DerivedTypes.h"
Vikram S. Adve631b9a32001-10-18 18:16:11 +000012#include "llvm/iMemory.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000013#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000015#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000016#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000017#include "llvm/Assembly/Writer.h"
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000018#include "Support/CommandLine.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/StringExtras.h"
Chris Lattner2e0769e2002-05-20 21:43:59 +000020#include <algorithm>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000021#include <sstream>
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::vector;
23using std::string;
Vikram S. Adved8893302001-10-28 21:37:25 +000024
Chris Lattner5ff62e92002-07-22 02:10:13 +000025static cl::opt<bool>
26DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
27 cl::desc("Disable pointer hashing"));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000028
Chris Lattner5ff62e92002-07-22 02:10:13 +000029static cl::list<string>
30TraceFuncName("tracefunc", cl::desc("trace only specific functions"),
Chris Lattner46897282002-07-22 02:17:27 +000031 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve1e2ddcf2002-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{
Chris Lattner5ff62e92002-07-22 02:10:13 +000040 if (TraceFuncName.size() == 0)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000041 return true;
Chris Lattner2e0769e2002-05-20 21:43:59 +000042
43 return std::find(TraceFuncName.begin(), TraceFuncName.end(), func->getName())
44 != TraceFuncName.end();
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000045}
46
47
Chris Lattnerbd0ef772002-02-26 21:46:54 +000048namespace {
Chris Lattner2e0769e2002-05-20 21:43:59 +000049 struct ExternalFuncs {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000050 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
51 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner7e708292002-06-25 16:13:24 +000052 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000053 };
54
Chris Lattnerf57b8452002-04-27 06:56:12 +000055 class InsertTraceCode : public FunctionPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000056 bool TraceBasicBlockExits, TraceFunctionExits;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000057 ExternalFuncs externalFuncs;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000058 public:
Chris Lattner79df7c02002-03-26 18:01:55 +000059 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattnerbd0ef772002-02-26 21:46:54 +000060 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner79df7c02002-03-26 18:01:55 +000061 TraceFunctionExits(traceFunctionExits) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000062
63 const char *getPassName() const { return "Trace Code Insertion"; }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000064
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000065 // Add a prototype for runtime functions not already in the program.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066 //
Chris Lattner7e708292002-06-25 16:13:24 +000067 bool doInitialization(Module &M);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000068
69 //--------------------------------------------------------------------------
70 // Function InsertCodeToTraceValues
71 //
Chris Lattner649f5dd2002-04-14 06:15:24 +000072 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner79df7c02002-03-26 18:01:55 +000073 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000074 //
Chris Lattner79df7c02002-03-26 18:01:55 +000075 static bool doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000076 bool traceFunctionExits, ExternalFuncs& externalFuncs);
77
Chris Lattner649f5dd2002-04-14 06:15:24 +000078 // runOnFunction - This method does the work.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000079 //
Chris Lattner7e708292002-06-25 16:13:24 +000080 bool runOnFunction(Function &F) {
81 return doit(&F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000082 }
Chris Lattner97e52e42002-04-28 21:27:06 +000083
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.preservesCFG();
86 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000087 };
88} // end anonymous namespace
89
90
Chris Lattnerf57b8452002-04-27 06:56:12 +000091Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000092 return new InsertTraceCode(false, true);
93}
94
Chris Lattner649f5dd2002-04-14 06:15:24 +000095Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000096 return new InsertTraceCode(true, true);
97}
98
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000099// Add a prototype for external functions used by the tracing code.
Chris Lattner29c14732001-12-14 16:26:05 +0000100//
Chris Lattner7e708292002-06-25 16:13:24 +0000101void ExternalFuncs::doInitialization(Module &M) {
Chris Lattner29c14732001-12-14 16:26:05 +0000102 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000103 const FunctionType *MTy =
104 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner7e708292002-06-25 16:13:24 +0000105 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000106
107 // uint (sbyte*)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000108 const FunctionType *hashFuncTy =
Chris Lattner2e0769e2002-05-20 21:43:59 +0000109 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner7e708292002-06-25 16:13:24 +0000110 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000111
Chris Lattner2e0769e2002-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. Adve1e2ddcf2002-05-19 15:39:02 +0000115
Chris Lattner7e708292002-06-25 16:13:24 +0000116 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
117 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000118
119 const FunctionType *voidvoidFuncTy =
120 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
121
Chris Lattner7e708292002-06-25 16:13:24 +0000122 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
123 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000124 voidvoidFuncTy);
125}
126
127
128// Add a prototype for external functions used by the tracing code.
129//
Chris Lattner7e708292002-06-25 16:13:24 +0000130bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000131 externalFuncs.doInitialization(M);
Chris Lattner89851072002-03-29 03:43:24 +0000132 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000133}
134
Chris Lattner29c14732001-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. Adve524185a2002-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 Lattner29c14732001-12-14 16:26:05 +0000142 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
143 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000144 M->getGlobalList().push_back(GV);
145 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000146}
147
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000148
Chris Lattner29c14732001-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. Advedf1892f2001-10-14 23:18:45 +0000165 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000166 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000167 //
168 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000169 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000170 opCode != Instruction::PHINode &&
171 opCode != Instruction::Cast);
172}
173
Chris Lattner29c14732001-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. Adve631b9a32001-10-18 18:16:11 +0000179}
180
Chris Lattner29c14732001-12-14 16:26:05 +0000181static string getPrintfCodeFor(const Value *V) {
182 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000183 if (V->getType()->isFloatingPoint())
184 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000185 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000186 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000187 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000188 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000189 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattner29c14732001-12-14 16:26:05 +0000190 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000191
Chris Lattner649f5dd2002-04-14 06:15:24 +0000192 assert(0 && "Illegal value to print out...");
193 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000194}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000195
196
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000197static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
198 string Message,
199 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-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 Lattner649f5dd2002-04-14 06:15:24 +0000203 Message.replace(Offset, 1, "%%");
Chris Lattner29c14732001-12-14 16:26:05 +0000204 Offset += 2; // Skip over the new %'s
205 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000206
Chris Lattner29c14732001-12-14 16:26:05 +0000207 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000208
Chris Lattner44571632001-10-18 06:03:05 +0000209 // Turn the marker string into a global variable...
Chris Lattner29c14732001-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 Lattner7e708292002-06-25 16:13:24 +0000217 BBI = ++BB->getInstList().insert(BBI, GEP);
Chris Lattner44571632001-10-18 06:03:05 +0000218
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000219 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-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 Lattner7e708292002-06-25 16:13:24 +0000224 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000225 V = I;
226 }
227
228 vector<Value*> HashArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000229 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
Chris Lattner7e708292002-06-25 16:13:24 +0000230 BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000231 }
232
Chris Lattner44571632001-10-18 06:03:05 +0000233 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-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 Lattner7e708292002-06-25 16:13:24 +0000238 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner29c14732001-12-14 16:26:05 +0000239}
240
Chris Lattner44571632001-10-18 06:03:05 +0000241
Chris Lattner29c14732001-12-14 16:26:05 +0000242static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
243 BasicBlock::iterator &BBI,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000244 const string &Message, Function *Printf,
245 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000246 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000247 if (V) WriteAsOperand(OutStr, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000248 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
249 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000250}
251
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000252static void
253InsertReleaseInst(Value *V, BasicBlock *BB,
254 BasicBlock::iterator &BBI,
255 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-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 Lattner7e708292002-06-25 16:13:24 +0000260 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000261 V = I;
262 }
263 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000264 Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
Chris Lattner7e708292002-06-25 16:13:24 +0000265 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000266}
267
268static void
269InsertRecordInst(Value *V, BasicBlock *BB,
270 BasicBlock::iterator &BBI,
271 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-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 Lattner7e708292002-06-25 16:13:24 +0000275 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000276 V = I;
277 }
278 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000279 Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
Chris Lattner7e708292002-06-25 16:13:24 +0000280 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve1e2ddcf2002-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 Lattner7e708292002-06-25 16:13:24 +0000287 BasicBlock &BB = M->getEntryNode();
288 BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
289 vector<Value*>()));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000290}
291
292static void
293InsertReleaseRecordedInst(BasicBlock *BB,
294 Function* ReleaseOnReturnFunc) {
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000295 BasicBlock::iterator BBI = --BB->end();
Chris Lattner7e708292002-06-25 16:13:24 +0000296 BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
297 vector<Value*>()));
Vikram S. Adve1e2ddcf2002-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 Lattner7e708292002-06-25 16:13:24 +0000309 if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000310 InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
Chris Lattner7e708292002-06-25 16:13:24 +0000311 else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000312 {
Chris Lattner7e708292002-06-25 16:13:24 +0000313 BasicBlock::iterator nextI = ++II;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000314 InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
Chris Lattner7e708292002-06-25 16:13:24 +0000315 II = --nextI;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000316 }
317 }
318}
319
Chris Lattner8d9e3772001-10-18 05:28:08 +0000320
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000321// Insert print instructions at the end of basic block BB for each value
322// computed in BB that is live at the end of BB,
323// or that is stored to memory in BB.
324// If the value is stored to memory, we load it back before printing it
Chris Lattner79df7c02002-03-26 18:01:55 +0000325// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-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. Adve631b9a32001-10-18 18:16:11 +0000328// store instruction).
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000329//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000330static void TraceValuesAtBBExit(BasicBlock *BB,
331 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner79df7c02002-03-26 18:01:55 +0000332 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000333 // Get an iterator to point to the insertion location, which is
334 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000335 //
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000336 BasicBlock::iterator InsertPos = --BB->end();
337 assert(InsertPos->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000338
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000339#undef CANNOT_SAVE_CCR_ACROSS_CALLS
340#ifdef CANNOT_SAVE_CCR_ACROSS_CALLS
341 //
342 // *** DISABLING THIS BECAUSE SAVING %CCR ACROSS CALLS WORKS NOW.
343 // *** DELETE THIS CODE AFTER SOME TESTING.
344 // *** NOTE: THIS CODE IS BROKEN ANYWAY WHEN THE SETCC IS NOT JUST
345 // *** BEFORE THE BRANCH.
346 // -- Vikram Adve, 7/7/02.
347 //
Vikram S. Adved8893302001-10-28 21:37:25 +0000348 // If the terminator is a conditional branch, insert the trace code just
349 // before the instruction that computes the branch condition (just to
350 // avoid putting a call between the CC-setting instruction and the branch).
351 // Use laterInstrSet to mark instructions that come after the setCC instr
352 // because those cannot be traced at the location we choose.
353 //
Chris Lattner29c14732001-12-14 16:26:05 +0000354 Instruction *SetCC = 0;
355 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
356 if (!Branch->isUnconditional())
357 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
358 if (I->getParent() == BB) {
Chris Lattner7e708292002-06-25 16:13:24 +0000359 InsertPos = SetCC = I; // Back up until we can insert before the setcc
Chris Lattner29c14732001-12-14 16:26:05 +0000360 }
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000361#endif CANNOT_SAVE_CCR_ACROSS_CALLS
Chris Lattner29c14732001-12-14 16:26:05 +0000362
Chris Lattner697954c2002-01-20 22:54:45 +0000363 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000364 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000365 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
366 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000367
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000368 // Insert a print instruction for each instruction preceding InsertPos.
369 // The print instructions must go before InsertPos, so we use the
370 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000371 //
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000372 if (InsertPos != BB->begin()) { // there's at least one instr before InsertPos
373 BasicBlock::iterator II = BB->begin(), IEincl = InsertPos;
374 --IEincl;
375 do { // do from II up to IEincl, inclusive
376 if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
377 assert(valuesStoredInFunction &&
378 "Should not be printing a store instruction at function exit");
379 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
Chris Lattner7e708292002-06-25 16:13:24 +0000380 "reload."+SI->getPointerOperand()->getName());
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000381 InsertPos = ++BB->getInstList().insert(InsertPos, LI);
382 valuesStoredInFunction->push_back(LI);
383 }
384 if (ShouldTraceValue(II))
385 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf,HashPtrToSeqNum);
386 } while (II++ != IEincl);
Chris Lattner29c14732001-12-14 16:26:05 +0000387 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000388}
389
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000390static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf,
391 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000392 // Get an iterator to point to the insertion location
Chris Lattner7e708292002-06-25 16:13:24 +0000393 BasicBlock &BB = M->getEntryNode();
394 BasicBlock::iterator BBI = BB.begin();
Chris Lattner29c14732001-12-14 16:26:05 +0000395
Chris Lattner697954c2002-01-20 22:54:45 +0000396 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000397 WriteAsOperand(OutStr, M, true);
Chris Lattner7e708292002-06-25 16:13:24 +0000398 InsertPrintInst(0, &BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000399 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000400
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000401 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000402 unsigned ArgNo = 0;
Chris Lattner7e708292002-06-25 16:13:24 +0000403 for (Function::aiterator I = M->abegin(), E = M->aend(); I != E; ++I,++ArgNo){
404 InsertVerbosePrintInst(I, &BB, BBI,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000405 " Arg #" + utostr(ArgNo) + ": ", Printf,
406 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000407 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000408}
409
410
Chris Lattner79df7c02002-03-26 18:01:55 +0000411static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000412 Function *Printf,
413 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000414 // Get an iterator to point to the insertion location
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000415 BasicBlock::iterator BBI = --BB->end();
Chris Lattner7e708292002-06-25 16:13:24 +0000416 ReturnInst &Ret = cast<ReturnInst>(BB->back());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000417
Chris Lattner697954c2002-01-20 22:54:45 +0000418 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000419 WriteAsOperand(OutStr, BB->getParent(), true);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000420 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
421 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000422
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000423 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000424 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner7e708292002-06-25 16:13:24 +0000425 InsertPrintInst(Ret.getReturnValue(), BB, BBI, " Returning: ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000426 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000427}
428
429
Chris Lattner79df7c02002-03-26 18:01:55 +0000430bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000431 bool traceFunctionEvents,
432 ExternalFuncs& externalFuncs) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000433 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattner8d9e3772001-10-18 05:28:08 +0000434 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000435
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000436 if (!TraceThisFunction(M))
437 return false;
438
Chris Lattner79df7c02002-03-26 18:01:55 +0000439 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000440 vector<BasicBlock*> exitBlocks;
441
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000442 // Insert code to trace values at function entry
Chris Lattner79df7c02002-03-26 18:01:55 +0000443 if (traceFunctionEvents)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000444 InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc,
445 externalFuncs.HashPtrFunc);
446
447 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000448 if (!DisablePtrHashing)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000449 InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000450
Chris Lattner7e708292002-06-25 16:13:24 +0000451 for (Function::iterator BB = M->begin(); BB != M->end(); ++BB) {
Chris Lattner29c14732001-12-14 16:26:05 +0000452 if (isa<ReturnInst>(BB->getTerminator()))
453 exitBlocks.push_back(BB); // record this as an exit block
454
455 if (traceBasicBlockExits)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000456 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
457 externalFuncs.HashPtrFunc, &valuesStoredInFunction);
458
Chris Lattner2e0769e2002-05-20 21:43:59 +0000459 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000460 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000461 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000462
463 for (unsigned i=0; i < exitBlocks.size(); ++i)
464 {
465 // Insert code to trace values at function exit
466 if (traceFunctionEvents)
467 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
468 externalFuncs.HashPtrFunc);
469
470 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000471 if (!DisablePtrHashing)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000472 InsertReleaseRecordedInst(exitBlocks[i],
473 externalFuncs.ReleaseOnReturnFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000474 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000475
Chris Lattner8d9e3772001-10-18 05:28:08 +0000476 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000477}