blob: dcca54d57b13367e7c2920b5ff7cd8e313b69660 [file] [log] [blame]
Vikram S. Advedf1892f2001-10-14 23:18:45 +00001// $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. Adve631b9a32001-10-18 18:16:11 +000022#include "llvm/iMemory.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000023#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 Lattner8d9e3772001-10-18 05:28:08 +000029#include "llvm/Assembly/Writer.h"
Vikram S. Adved8893302001-10-28 21:37:25 +000030#include "llvm/Support/HashExtras.h"
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +000031#include "llvm/Support/StringExtras.h"
Vikram S. Adved8893302001-10-28 21:37:25 +000032#include <hash_set>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000033#include <sstream>
Vikram S. Advebedb00d2001-10-18 13:49:22 +000034
Vikram S. Adved8893302001-10-28 21:37:25 +000035
36static const char*
37PrintMethodNameForType(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. Adved8893302001-10-28 21:37:25 +000064 default:
65 assert(0 && "Unsupported type for printing");
66 return NULL;
67 }
68}
69
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000070static inline GlobalVariable *GetStringRef(Module *M, const string &str) {
71 ConstPoolArray *Init = ConstPoolArray::get(str);
72 GlobalVariable *GV = new GlobalVariable(Init->getType(), /*Const*/true, Init);
73 M->getGlobalList().push_back(GV);
74 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000075}
76
Vikram S. Advebedb00d2001-10-18 13:49:22 +000077
Chris Lattnerf84b9bc2001-10-15 13:07:21 +000078static inline bool
Vikram S. Advedf1892f2001-10-14 23:18:45 +000079TraceThisOpCode(unsigned opCode)
80{
81 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +000082 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +000083 //
84 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +000085 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +000086 opCode != Instruction::PHINode &&
87 opCode != Instruction::Cast);
88}
89
Vikram S. Adve631b9a32001-10-18 18:16:11 +000090//
Vikram S. Adved8893302001-10-28 21:37:25 +000091// Check if this instruction has any uses outside its basic block,
92// or if it used by either a Call or Return instruction.
Vikram S. Adve631b9a32001-10-18 18:16:11 +000093//
94static inline bool
95LiveAtBBExit(Instruction* I)
96{
97 BasicBlock* bb = I->getParent();
98 bool isLive = false;
99 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
100 {
101 const Instruction* userI = dyn_cast<Instruction>(*U);
Vikram S. Adved8893302001-10-28 21:37:25 +0000102 if (userI == NULL
103 || userI->getParent() != bb
104 || userI->getOpcode() == Instruction::Call
105 || userI->getOpcode() == Instruction::Ret)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000106 isLive = true;
107 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000108 return isLive;
109}
110
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000111
Chris Lattner8d9e3772001-10-18 05:28:08 +0000112static void
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000113FindValuesToTraceInBB(BasicBlock* bb, vector<Instruction*>& valuesToTraceInBB)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000114{
115 for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000116 if ((*II)->getOpcode() == Instruction::Store
117 || (LiveAtBBExit(*II) &&
118 (*II)->getType()->isPrimitiveType() &&
119 (*II)->getType() != Type::VoidTy &&
120 TraceThisOpCode((*II)->getOpcode())))
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000121 {
122 valuesToTraceInBB.push_back(*II);
123 }
124}
125
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000126//
127// Let's save this code for future use; it has been tested and works:
128//
129// The signatures of the printf methods supported are:
130// int printf(ubyte*, ubyte*, ubyte*, ubyte*, int intValue)
131// int printf(ubyte*, ubyte*, ubyte*, ubyte*, unsigned uintValue)
132// int printf(ubyte*, ubyte*, ubyte*, ubyte*, float floatValue)
133// int printf(ubyte*, ubyte*, ubyte*, ubyte*, double doubleValue)
134// int printf(ubyte*, ubyte*, ubyte*, ubyte*, char* stringValue)
135// int printf(ubyte*, ubyte*, ubyte*, ubyte*, void* ptrValue)
136//
137// The invocation should be:
138// call "printf"(fmt, bbName, valueName, valueTypeName, value).
139//
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000140Value *GetPrintfMethodForType(Module* module, const Type* valueType)
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000141{
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000142 PointerType *ubytePtrTy = PointerType::get(ArrayType::get(Type::UByteTy));
143 vector<const Type*> argTypesVec(4, ubytePtrTy);
144 argTypesVec.push_back(valueType);
145
146 MethodType *printMethodTy = MethodType::get(Type::IntTy, argTypesVec,
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000147 /*isVarArg*/ false);
148
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000149 SymbolTable *ST = module->getSymbolTable();
150 if (Value *Meth = ST->lookup(PointerType::get(printMethodTy), "printf"))
151 return Meth;
152
153 // Create a new method and add it to the module
154 Method *printMethod = new Method(printMethodTy, "printf");
155 module->getMethodList().push_back(printMethod);
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000156
157 return printMethod;
158}
159
160
161Instruction*
162CreatePrintfInstr(Value* val,
163 const BasicBlock* bb,
164 Module* module,
165 unsigned int indent,
166 bool isMethodExit)
167{
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000168 ostringstream fmtString, scopeNameString, valNameString;
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000169 vector<Value*> paramList;
170 const Type* valueType = val->getType();
Vikram S. Adved8893302001-10-28 21:37:25 +0000171 Method* printMethod = cast<Method>(GetPrintfMethodForType(module,valueType));
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000172
173 if (! valueType->isPrimitiveType() ||
174 valueType->getPrimitiveID() == Type::VoidTyID ||
175 valueType->getPrimitiveID() == Type::TypeTyID ||
176 valueType->getPrimitiveID() == Type::LabelTyID)
177 {
178 assert(0 && "Unsupported type for printing");
179 return NULL;
180 }
181
182 const Value* scopeToUse = (isMethodExit)? (const Value*) bb->getParent()
183 : (const Value*) bb;
184 if (scopeToUse->hasName())
185 scopeNameString << scopeToUse->getName() << ends;
186 else
187 scopeNameString << scopeToUse << ends;
188
189 if (val->hasName())
190 valNameString << val->getName() << ends;
191 else
192 valNameString << val << ends;
193
194 for (unsigned i=0; i < indent; i++)
195 fmtString << " ";
196
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000197 fmtString << " AT EXIT OF "
198 << ((isMethodExit)? "METHOD " : "BB ")
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000199 << "%s : val %s = %s ";
200
201 GlobalVariable* scopeNameVal = GetStringRef(module, scopeNameString.str());
202 GlobalVariable* valNameVal = GetStringRef(module,valNameString.str());
203 GlobalVariable* typeNameVal = GetStringRef(module,
204 val->getType()->getDescription().c_str());
205
206 switch(valueType->getPrimitiveID())
207 {
208 case Type::BoolTyID:
209 case Type::UByteTyID: case Type::UShortTyID:
210 case Type::UIntTyID: case Type::ULongTyID:
211 case Type::SByteTyID: case Type::ShortTyID:
212 case Type::IntTyID: case Type::LongTyID:
Vikram S. Advec426c632001-10-28 22:44:02 +0000213 fmtString << " %d\n";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000214 break;
215
216 case Type::FloatTyID: case Type::DoubleTyID:
Vikram S. Advec426c632001-10-28 22:44:02 +0000217 fmtString << " %g\n";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000218 break;
219
220 case Type::PointerTyID:
Vikram S. Advec426c632001-10-28 22:44:02 +0000221 fmtString << " %p\n";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000222 break;
223
224 default:
225 assert(0 && "Should not get here. Check the IF expression above");
226 return NULL;
227 }
228
229 fmtString << ends;
230 GlobalVariable* fmtVal = GetStringRef(module, fmtString.str());
231
232 paramList.push_back(fmtVal);
233 paramList.push_back(scopeNameVal);
234 paramList.push_back(valNameVal);
235 paramList.push_back(typeNameVal);
236 paramList.push_back(val);
237
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000238 return new CallInst(printMethod, paramList);
239}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000240
241
Chris Lattner8d9e3772001-10-18 05:28:08 +0000242// The invocation should be:
Vikram S. Adved8893302001-10-28 21:37:25 +0000243// call "printString"([ubyte*] or [sbyte*] or ubyte* or sbyte*).
244// call "printLong"(long)
245// call "printInt"(int) ...
Chris Lattner8d9e3772001-10-18 05:28:08 +0000246//
Chris Lattner44571632001-10-18 06:03:05 +0000247static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) {
248 MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy),
249 /*isVarArg*/ false);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000250
Vikram S. Adved8893302001-10-28 21:37:25 +0000251 const char* printMethodName = PrintMethodNameForType(VTy);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000252 SymbolTable *ST = Mod->getSymbolTableSure();
Vikram S. Adved8893302001-10-28 21:37:25 +0000253 if (Value *V = ST->lookup(PointerType::get(MTy), printMethodName))
Chris Lattner8d9e3772001-10-18 05:28:08 +0000254 return V;
255
256 // Create a new method and add it to the module
Vikram S. Adved8893302001-10-28 21:37:25 +0000257 Method *M = new Method(MTy, printMethodName);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000258 Mod->getMethodList().push_back(M);
259 return M;
260}
261
262
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000263static void
264InsertPrintInsts(Value *Val,
265 BasicBlock* BB,
266 BasicBlock::iterator &BBI,
267 Module *Mod,
268 unsigned int indent,
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000269 bool isMethodExit,
270 bool isMethodEntry = false)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000271{
Chris Lattner44571632001-10-18 06:03:05 +0000272 const Type* ValTy = Val->getType();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000273
Chris Lattner111bd012001-10-29 17:27:38 +0000274 assert((ValTy->isPrimitiveType() || isa<PointerType>(ValTy)) &&
275 ValTy != Type::VoidTy && ValTy != Type::TypeTy &&
276 ValTy != Type::LabelTy && "Unsupported type for printing");
Chris Lattner8d9e3772001-10-18 05:28:08 +0000277
Chris Lattner44571632001-10-18 06:03:05 +0000278 const Value* scopeToUse =
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000279 (isMethodExit || isMethodEntry)? (const Value*)BB->getParent() : (const Value*)BB;
280
Chris Lattner44571632001-10-18 06:03:05 +0000281 // Create the marker string...
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000282 ostringstream scopeNameString;
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000283 if (isMethodExit || isMethodEntry)
284 scopeNameString << " METHOD ";
285 else
286 scopeNameString << " BASIC BLOCK ";
287
288 scopeNameString << ((scopeToUse->hasName())
289 ? scopeToUse->getName().c_str()
290 : itostr((int) scopeToUse).c_str())
291 << " : ";
292
Vikram S. Adved8893302001-10-28 21:37:25 +0000293 WriteAsOperand(scopeNameString, Val) << " = ";
Chris Lattner8d9e3772001-10-18 05:28:08 +0000294
Vikram S. Adved8893302001-10-28 21:37:25 +0000295 string fmtString(indent, ' ');
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000296 if (isMethodEntry)
297 fmtString += string(" AT ENTRY OF") + scopeNameString.str();
298 else
299 fmtString += string(" AT EXIT OF") + scopeNameString.str();
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000300
Chris Lattner44571632001-10-18 06:03:05 +0000301 // Turn the marker string into a global variable...
302 GlobalVariable *fmtVal = GetStringRef(Mod, fmtString);
303
304 // Insert the first print instruction to print the string flag:
305 Instruction *I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
306 vector<Value*>(1, fmtVal));
307 BBI = BB->getInstList().insert(BBI, I)+1;
308
309 // Insert the next print instruction to print the value:
310 I = new CallInst(GetPrintMethodForType(Mod, ValTy),
311 vector<Value*>(1, Val));
312 BBI = BB->getInstList().insert(BBI, I)+1;
313
314 // Print out a newline
Vikram S. Advec426c632001-10-28 22:44:02 +0000315 fmtVal = GetStringRef(Mod, "\n");
Chris Lattner44571632001-10-18 06:03:05 +0000316 I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
317 vector<Value*>(1, fmtVal));
318 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000319}
320
321
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000322static LoadInst*
Chris Lattner7c54b4a2001-11-26 17:00:43 +0000323InsertLoadInst(StoreInst *SI,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000324 BasicBlock *bb,
325 BasicBlock::iterator &BBI)
326{
Chris Lattner7c54b4a2001-11-26 17:00:43 +0000327 LoadInst* loadInst = new LoadInst(SI->getPointerOperand(), SI->copyIndices());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000328 BBI = bb->getInstList().insert(BBI, loadInst) + 1;
329 return loadInst;
330}
331
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000332
333//
334// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000335// for each value in valueVec[] that is live at the end of that basic block,
336// or that is stored to memory in this basic block.
337// If the value is stored to memory, we load it back before printing
338// We also return all such loaded values in the vector valuesStoredInMethod
339// for printing at the exit from the method. (Note that in each invocation
340// of the method, this will only get the last value stored for each static
341// store instruction).
342// *bb must be the block in which the value is computed;
343// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000344//
345static void
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000346TraceValuesAtBBExit(const vector<Instruction*>& valueVec,
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000347 BasicBlock* bb,
348 Module* module,
349 unsigned int indent,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000350 bool isMethodExit,
351 vector<Instruction*>* valuesStoredInMethod)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000352{
Vikram S. Adved8893302001-10-28 21:37:25 +0000353 // Get an iterator to point to the insertion location, which is
354 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000355 //
356 BasicBlock::InstListType& instList = bb->getInstList();
Chris Lattner44571632001-10-18 06:03:05 +0000357 BasicBlock::iterator here = instList.end()-1;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000358 assert((*here)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000359
Vikram S. Adved8893302001-10-28 21:37:25 +0000360 // If the terminator is a conditional branch, insert the trace code just
361 // before the instruction that computes the branch condition (just to
362 // avoid putting a call between the CC-setting instruction and the branch).
363 // Use laterInstrSet to mark instructions that come after the setCC instr
364 // because those cannot be traced at the location we choose.
365 //
366 hash_set<Instruction*> laterInstrSet;
367 if (BranchInst* brInst = dyn_cast<BranchInst>(*here))
368 if (! brInst->isUnconditional())
369 if (Instruction* setCC = dyn_cast<Instruction>(brInst->getCondition()))
370 if (setCC->getParent() == bb)
371 {
372 while ((*here) != setCC && here != instList.begin())
373 {
374 --here;
375 laterInstrSet.insert(*here);
376 }
377 assert((*here) == setCC && "Missed the setCC instruction?");
378 laterInstrSet.insert(*here);
379 }
380
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000381 // Insert a print instruction for each value.
382 //
383 for (unsigned i=0, N=valueVec.size(); i < N; i++)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000384 {
385 Instruction* I = valueVec[i];
386 if (I->getOpcode() == Instruction::Store)
387 {
388 assert(valuesStoredInMethod != NULL &&
389 "Should not be printing a store instruction at method exit");
390 I = InsertLoadInst((StoreInst*) I, bb, here);
391 valuesStoredInMethod->push_back(I);
392 }
Vikram S. Adved8893302001-10-28 21:37:25 +0000393 if (laterInstrSet.find(I) == laterInstrSet.end())
394 InsertPrintInsts(I, bb, here, module, indent, isMethodExit);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000395 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000396}
397
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000398
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000399
400static Instruction*
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000401CreateMethodTraceInst(Method* method,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000402 unsigned int indent,
403 const string& msg)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000404{
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000405 string fmtString(indent, ' ');
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000406 // ostringstream methodNameString;
407 // WriteAsOperand(methodNameString, method);
408 // fmtString += msg + methodNameString.str() + '\n';
409 if (method->hasName())
410 fmtString += msg + method->getName().c_str() + '\n';
411 else
412 fmtString += msg + itostr((int) method) + '\n';
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000413
414 GlobalVariable *fmtVal = GetStringRef(method->getParent(), fmtString);
415 Instruction *printInst =
416 new CallInst(GetPrintMethodForType(method->getParent(), fmtVal->getType()),
417 vector<Value*>(1, fmtVal));
418
419 return printInst;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000420}
421
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000422
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000423static inline void
424InsertCodeToShowMethodEntry(Method* method,
425 BasicBlock* entryBB,
426 unsigned int indent)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000427{
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000428 // Get an iterator to point to the insertion location
429 BasicBlock::InstListType& instList = entryBB->getInstList();
430 BasicBlock::iterator here = instList.begin();
431
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000432 Instruction *printInst = CreateMethodTraceInst(method, indent,
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000433 "ENTERING METHOD ");
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000434 here = entryBB->getInstList().insert(here, printInst) + 1;
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000435
436 // Now print all the incoming arguments
437 const Method::ArgumentListType& argList = method->getArgumentList();
438 for (Method::ArgumentListType::const_iterator
439 I=argList.begin(), E=argList.end(); I != E; ++I)
440 {
441 InsertPrintInsts((*I), entryBB, here, method->getParent(),
442 indent, /*isMethodExit*/false, /*isMethodEntry*/true);
443 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000444}
445
446
447static inline void
448InsertCodeToShowMethodExit(Method* method,
449 BasicBlock* exitBB,
450 unsigned int indent)
451{
452 // Get an iterator to point to the insertion location
453 BasicBlock::InstListType& instList = exitBB->getInstList();
454 BasicBlock::iterator here = instList.end()-1;
455 assert((*here)->isTerminator());
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000456 assert(isa<ReturnInst>(*here));
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000457
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000458 Instruction *printInst = CreateMethodTraceInst(method, indent,
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000459 "LEAVING METHOD ");
460 here = exitBB->getInstList().insert(here, printInst) + 1;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000461
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000462 // print the return value, if any
463 if (method->getReturnType() != Type::VoidTy)
464 InsertPrintInsts(cast<ReturnInst>(exitBB->getTerminator())->getReturnValue(),
465 exitBB, here, method->getParent(),
466 indent, /*isMethodExit*/true, /*isMethodEntry*/false);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000467}
468
469
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000470//************************** External Functions ****************************/
471
472
473bool
474InsertTraceCode::doInsertTraceCode(Method *M,
475 bool traceBasicBlockExits,
476 bool traceMethodExits)
477{
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000478 vector<Instruction*> valuesStoredInMethod;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000479 Module* module = M->getParent();
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000480 vector<BasicBlock*> exitBlocks;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000481
Chris Lattner8d9e3772001-10-18 05:28:08 +0000482 if (M->isExternal() ||
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000483 (! traceBasicBlockExits && ! traceMethodExits))
Chris Lattner8d9e3772001-10-18 05:28:08 +0000484 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000485
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000486 if (traceMethodExits)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000487 InsertCodeToShowMethodEntry(M, M->getEntryNode(), /*indent*/ 0);
488
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000489 for (Method::iterator BI = M->begin(); BI != M->end(); ++BI)
490 {
491 BasicBlock* bb = *BI;
492 bool isExitBlock = false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000493 vector<Instruction*> valuesToTraceInBB;
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000494
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000495 FindValuesToTraceInBB(bb, valuesToTraceInBB);
496
497 if (bb->succ_begin() == bb->succ_end())
498 { // record this as an exit block
499 exitBlocks.push_back(bb);
500 isExitBlock = true;
501 }
502
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000503 if (traceBasicBlockExits)
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000504 TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000505 /*indent*/ 4, /*isMethodExit*/ false,
506 &valuesStoredInMethod);
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000507 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000508
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000509 if (traceMethodExits)
510 for (unsigned i=0; i < exitBlocks.size(); ++i)
511 {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000512 TraceValuesAtBBExit(valuesStoredInMethod, exitBlocks[i], module,
513 /*indent*/ 0, /*isMethodExit*/ true,
514 /*valuesStoredInMethod*/ NULL);
515 InsertCodeToShowMethodExit(M, exitBlocks[i], /*indent*/ 0);
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000516 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000517
Chris Lattner8d9e3772001-10-18 05:28:08 +0000518 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000519}