blob: 98589a457cb3fe85b616ce6f97b9bb73cd24825c [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"
31#include <hash_set>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000032#include <sstream>
Vikram S. Advebedb00d2001-10-18 13:49:22 +000033
Vikram S. Adved8893302001-10-28 21:37:25 +000034const string PRINT_FUNC_NAME = "printVal";
35
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";
64 case Type::MethodTyID: return "printPointer";
65 default:
66 assert(0 && "Unsupported type for printing");
67 return NULL;
68 }
69}
70
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000071static inline GlobalVariable *GetStringRef(Module *M, const string &str) {
72 ConstPoolArray *Init = ConstPoolArray::get(str);
73 GlobalVariable *GV = new GlobalVariable(Init->getType(), /*Const*/true, Init);
74 M->getGlobalList().push_back(GV);
75 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000076}
77
Vikram S. Advebedb00d2001-10-18 13:49:22 +000078
Chris Lattnerf84b9bc2001-10-15 13:07:21 +000079static inline bool
Vikram S. Advedf1892f2001-10-14 23:18:45 +000080TraceThisOpCode(unsigned opCode)
81{
82 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +000083 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +000084 //
85 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +000086 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +000087 opCode != Instruction::PHINode &&
88 opCode != Instruction::Cast);
89}
90
Vikram S. Adve631b9a32001-10-18 18:16:11 +000091//
Vikram S. Adved8893302001-10-28 21:37:25 +000092// Check if this instruction has any uses outside its basic block,
93// or if it used by either a Call or Return instruction.
Vikram S. Adve631b9a32001-10-18 18:16:11 +000094//
95static inline bool
96LiveAtBBExit(Instruction* I)
97{
98 BasicBlock* bb = I->getParent();
99 bool isLive = false;
100 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
101 {
102 const Instruction* userI = dyn_cast<Instruction>(*U);
Vikram S. Adved8893302001-10-28 21:37:25 +0000103 if (userI == NULL
104 || userI->getParent() != bb
105 || userI->getOpcode() == Instruction::Call
106 || userI->getOpcode() == Instruction::Ret)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000107 isLive = true;
108 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000109 return isLive;
110}
111
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000112
Chris Lattner8d9e3772001-10-18 05:28:08 +0000113static void
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000114FindValuesToTraceInBB(BasicBlock* bb, vector<Instruction*>& valuesToTraceInBB)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000115{
116 for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000117 if ((*II)->getOpcode() == Instruction::Store
118 || (LiveAtBBExit(*II) &&
119 (*II)->getType()->isPrimitiveType() &&
120 (*II)->getType() != Type::VoidTy &&
121 TraceThisOpCode((*II)->getOpcode())))
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000122 {
123 valuesToTraceInBB.push_back(*II);
124 }
125}
126
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000127//
128// Let's save this code for future use; it has been tested and works:
129//
130// The signatures of the printf methods supported are:
131// int printf(ubyte*, ubyte*, ubyte*, ubyte*, int intValue)
132// int printf(ubyte*, ubyte*, ubyte*, ubyte*, unsigned uintValue)
133// int printf(ubyte*, ubyte*, ubyte*, ubyte*, float floatValue)
134// int printf(ubyte*, ubyte*, ubyte*, ubyte*, double doubleValue)
135// int printf(ubyte*, ubyte*, ubyte*, ubyte*, char* stringValue)
136// int printf(ubyte*, ubyte*, ubyte*, ubyte*, void* ptrValue)
137//
138// The invocation should be:
139// call "printf"(fmt, bbName, valueName, valueTypeName, value).
140//
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000141Value *GetPrintfMethodForType(Module* module, const Type* valueType)
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000142{
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000143 PointerType *ubytePtrTy = PointerType::get(ArrayType::get(Type::UByteTy));
144 vector<const Type*> argTypesVec(4, ubytePtrTy);
145 argTypesVec.push_back(valueType);
146
147 MethodType *printMethodTy = MethodType::get(Type::IntTy, argTypesVec,
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000148 /*isVarArg*/ false);
149
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000150 SymbolTable *ST = module->getSymbolTable();
151 if (Value *Meth = ST->lookup(PointerType::get(printMethodTy), "printf"))
152 return Meth;
153
154 // Create a new method and add it to the module
155 Method *printMethod = new Method(printMethodTy, "printf");
156 module->getMethodList().push_back(printMethod);
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000157
158 return printMethod;
159}
160
161
162Instruction*
163CreatePrintfInstr(Value* val,
164 const BasicBlock* bb,
165 Module* module,
166 unsigned int indent,
167 bool isMethodExit)
168{
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000169 ostringstream fmtString, scopeNameString, valNameString;
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000170 vector<Value*> paramList;
171 const Type* valueType = val->getType();
Vikram S. Adved8893302001-10-28 21:37:25 +0000172 Method* printMethod = cast<Method>(GetPrintfMethodForType(module,valueType));
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000173
174 if (! valueType->isPrimitiveType() ||
175 valueType->getPrimitiveID() == Type::VoidTyID ||
176 valueType->getPrimitiveID() == Type::TypeTyID ||
177 valueType->getPrimitiveID() == Type::LabelTyID)
178 {
179 assert(0 && "Unsupported type for printing");
180 return NULL;
181 }
182
183 const Value* scopeToUse = (isMethodExit)? (const Value*) bb->getParent()
184 : (const Value*) bb;
185 if (scopeToUse->hasName())
186 scopeNameString << scopeToUse->getName() << ends;
187 else
188 scopeNameString << scopeToUse << ends;
189
190 if (val->hasName())
191 valNameString << val->getName() << ends;
192 else
193 valNameString << val << ends;
194
195 for (unsigned i=0; i < indent; i++)
196 fmtString << " ";
197
198 fmtString << " At exit of "
199 << ((isMethodExit)? "Method " : "BB ")
200 << "%s : val %s = %s ";
201
202 GlobalVariable* scopeNameVal = GetStringRef(module, scopeNameString.str());
203 GlobalVariable* valNameVal = GetStringRef(module,valNameString.str());
204 GlobalVariable* typeNameVal = GetStringRef(module,
205 val->getType()->getDescription().c_str());
206
207 switch(valueType->getPrimitiveID())
208 {
209 case Type::BoolTyID:
210 case Type::UByteTyID: case Type::UShortTyID:
211 case Type::UIntTyID: case Type::ULongTyID:
212 case Type::SByteTyID: case Type::ShortTyID:
213 case Type::IntTyID: case Type::LongTyID:
Vikram S. Adved8893302001-10-28 21:37:25 +0000214 fmtString << " %d\\n";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000215 break;
216
217 case Type::FloatTyID: case Type::DoubleTyID:
Vikram S. Adved8893302001-10-28 21:37:25 +0000218 fmtString << " %g\\n";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000219 break;
220
221 case Type::PointerTyID:
Vikram S. Adved8893302001-10-28 21:37:25 +0000222 fmtString << " %p\\n";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000223 break;
224
225 default:
226 assert(0 && "Should not get here. Check the IF expression above");
227 return NULL;
228 }
229
230 fmtString << ends;
231 GlobalVariable* fmtVal = GetStringRef(module, fmtString.str());
232
233 paramList.push_back(fmtVal);
234 paramList.push_back(scopeNameVal);
235 paramList.push_back(valNameVal);
236 paramList.push_back(typeNameVal);
237 paramList.push_back(val);
238
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000239 return new CallInst(printMethod, paramList);
240}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000241
242
Chris Lattner8d9e3772001-10-18 05:28:08 +0000243// The invocation should be:
Vikram S. Adved8893302001-10-28 21:37:25 +0000244// call "printString"([ubyte*] or [sbyte*] or ubyte* or sbyte*).
245// call "printLong"(long)
246// call "printInt"(int) ...
Chris Lattner8d9e3772001-10-18 05:28:08 +0000247//
Chris Lattner44571632001-10-18 06:03:05 +0000248static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) {
249 MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy),
250 /*isVarArg*/ false);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000251
Vikram S. Adved8893302001-10-28 21:37:25 +0000252 const char* printMethodName = PrintMethodNameForType(VTy);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000253 SymbolTable *ST = Mod->getSymbolTableSure();
Vikram S. Adved8893302001-10-28 21:37:25 +0000254 if (Value *V = ST->lookup(PointerType::get(MTy), printMethodName))
Chris Lattner8d9e3772001-10-18 05:28:08 +0000255 return V;
256
257 // Create a new method and add it to the module
Vikram S. Adved8893302001-10-28 21:37:25 +0000258 Method *M = new Method(MTy, printMethodName);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000259 Mod->getMethodList().push_back(M);
260 return M;
261}
262
263
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000264static void
265InsertPrintInsts(Value *Val,
266 BasicBlock* BB,
267 BasicBlock::iterator &BBI,
268 Module *Mod,
269 unsigned int indent,
270 bool isMethodExit)
271{
Chris Lattner44571632001-10-18 06:03:05 +0000272 const Type* ValTy = Val->getType();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000273
Chris Lattner44571632001-10-18 06:03:05 +0000274 assert(ValTy->isPrimitiveType() &&
275 ValTy->getPrimitiveID() != Type::VoidTyID &&
276 ValTy->getPrimitiveID() != Type::TypeTyID &&
277 ValTy->getPrimitiveID() != Type::LabelTyID &&
Chris Lattner8d9e3772001-10-18 05:28:08 +0000278 "Unsupported type for printing");
279
Chris Lattner44571632001-10-18 06:03:05 +0000280 const Value* scopeToUse =
281 isMethodExit ? (const Value*)BB->getParent() : (const Value*)BB;
282
283 // Create the marker string...
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000284 ostringstream scopeNameString;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000285 WriteAsOperand(scopeNameString, scopeToUse) << " : ";
Vikram S. Adved8893302001-10-28 21:37:25 +0000286 WriteAsOperand(scopeNameString, Val) << " = ";
Chris Lattner8d9e3772001-10-18 05:28:08 +0000287
Vikram S. Adved8893302001-10-28 21:37:25 +0000288 string fmtString(indent, ' ');
Chris Lattner44571632001-10-18 06:03:05 +0000289 fmtString += string(" At exit of") + scopeNameString.str();
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000290
Chris Lattner44571632001-10-18 06:03:05 +0000291 // Turn the marker string into a global variable...
292 GlobalVariable *fmtVal = GetStringRef(Mod, fmtString);
293
294 // Insert the first print instruction to print the string flag:
295 Instruction *I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
296 vector<Value*>(1, fmtVal));
297 BBI = BB->getInstList().insert(BBI, I)+1;
298
299 // Insert the next print instruction to print the value:
300 I = new CallInst(GetPrintMethodForType(Mod, ValTy),
301 vector<Value*>(1, Val));
302 BBI = BB->getInstList().insert(BBI, I)+1;
303
304 // Print out a newline
Vikram S. Adved8893302001-10-28 21:37:25 +0000305 fmtVal = GetStringRef(Mod, "\\n");
Chris Lattner44571632001-10-18 06:03:05 +0000306 I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
307 vector<Value*>(1, fmtVal));
308 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000309}
310
311
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000312static LoadInst*
313InsertLoadInst(StoreInst* storeInst,
314 BasicBlock *bb,
315 BasicBlock::iterator &BBI)
316{
317 LoadInst* loadInst = new LoadInst(storeInst->getPtrOperand(),
318 storeInst->getIndexVec());
319 BBI = bb->getInstList().insert(BBI, loadInst) + 1;
320 return loadInst;
321}
322
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000323
324//
325// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000326// for each value in valueVec[] that is live at the end of that basic block,
327// or that is stored to memory in this basic block.
328// If the value is stored to memory, we load it back before printing
329// We also return all such loaded values in the vector valuesStoredInMethod
330// for printing at the exit from the method. (Note that in each invocation
331// of the method, this will only get the last value stored for each static
332// store instruction).
333// *bb must be the block in which the value is computed;
334// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000335//
336static void
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000337TraceValuesAtBBExit(const vector<Instruction*>& valueVec,
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000338 BasicBlock* bb,
339 Module* module,
340 unsigned int indent,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000341 bool isMethodExit,
342 vector<Instruction*>* valuesStoredInMethod)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000343{
Vikram S. Adved8893302001-10-28 21:37:25 +0000344 // Get an iterator to point to the insertion location, which is
345 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000346 //
347 BasicBlock::InstListType& instList = bb->getInstList();
Chris Lattner44571632001-10-18 06:03:05 +0000348 BasicBlock::iterator here = instList.end()-1;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000349 assert((*here)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000350
Vikram S. Adved8893302001-10-28 21:37:25 +0000351 // If the terminator is a conditional branch, insert the trace code just
352 // before the instruction that computes the branch condition (just to
353 // avoid putting a call between the CC-setting instruction and the branch).
354 // Use laterInstrSet to mark instructions that come after the setCC instr
355 // because those cannot be traced at the location we choose.
356 //
357 hash_set<Instruction*> laterInstrSet;
358 if (BranchInst* brInst = dyn_cast<BranchInst>(*here))
359 if (! brInst->isUnconditional())
360 if (Instruction* setCC = dyn_cast<Instruction>(brInst->getCondition()))
361 if (setCC->getParent() == bb)
362 {
363 while ((*here) != setCC && here != instList.begin())
364 {
365 --here;
366 laterInstrSet.insert(*here);
367 }
368 assert((*here) == setCC && "Missed the setCC instruction?");
369 laterInstrSet.insert(*here);
370 }
371
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000372 // Insert a print instruction for each value.
373 //
374 for (unsigned i=0, N=valueVec.size(); i < N; i++)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000375 {
376 Instruction* I = valueVec[i];
377 if (I->getOpcode() == Instruction::Store)
378 {
379 assert(valuesStoredInMethod != NULL &&
380 "Should not be printing a store instruction at method exit");
381 I = InsertLoadInst((StoreInst*) I, bb, here);
382 valuesStoredInMethod->push_back(I);
383 }
Vikram S. Adved8893302001-10-28 21:37:25 +0000384 if (laterInstrSet.find(I) == laterInstrSet.end())
385 InsertPrintInsts(I, bb, here, module, indent, isMethodExit);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000386 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000387}
388
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000389
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000390
391static Instruction*
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000392CreateMethodTraceInst(Method* method,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000393 unsigned int indent,
394 const string& msg)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000395{
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000396 string fmtString(indent, ' ');
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000397 ostringstream methodNameString;
Vikram S. Adved8893302001-10-28 21:37:25 +0000398 WriteAsOperand(methodNameString, method);
399 fmtString += msg + methodNameString.str() + "\\n";
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000400
401 GlobalVariable *fmtVal = GetStringRef(method->getParent(), fmtString);
402 Instruction *printInst =
403 new CallInst(GetPrintMethodForType(method->getParent(), fmtVal->getType()),
404 vector<Value*>(1, fmtVal));
405
406 return printInst;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000407}
408
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000409
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000410static inline void
411InsertCodeToShowMethodEntry(Method* method,
412 BasicBlock* entryBB,
413 unsigned int indent)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000414{
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000415 // Get an iterator to point to the insertion location
416 BasicBlock::InstListType& instList = entryBB->getInstList();
417 BasicBlock::iterator here = instList.begin();
418
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000419 Instruction *printInst = CreateMethodTraceInst(method, indent,
420 "Entering Method");
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000421
422 here = entryBB->getInstList().insert(here, printInst) + 1;
423}
424
425
426static inline void
427InsertCodeToShowMethodExit(Method* method,
428 BasicBlock* exitBB,
429 unsigned int indent)
430{
431 // Get an iterator to point to the insertion location
432 BasicBlock::InstListType& instList = exitBB->getInstList();
433 BasicBlock::iterator here = instList.end()-1;
434 assert((*here)->isTerminator());
435
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000436 Instruction *printInst = CreateMethodTraceInst(method, indent,
437 "Leaving Method");
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000438
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000439 exitBB->getInstList().insert(here, printInst) + 1;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000440}
441
442
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000443//************************** External Functions ****************************/
444
445
446bool
447InsertTraceCode::doInsertTraceCode(Method *M,
448 bool traceBasicBlockExits,
449 bool traceMethodExits)
450{
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000451 vector<Instruction*> valuesStoredInMethod;
Chris Lattner8d9e3772001-10-18 05:28:08 +0000452 Module* module = M->getParent();
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000453 vector<BasicBlock*> exitBlocks;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000454
Chris Lattner8d9e3772001-10-18 05:28:08 +0000455 if (M->isExternal() ||
Vikram S. Adved8893302001-10-28 21:37:25 +0000456 (M->hasName() && M->getName() == PRINT_FUNC_NAME) ||
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000457 (! traceBasicBlockExits && ! traceMethodExits))
Chris Lattner8d9e3772001-10-18 05:28:08 +0000458 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000459
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000460 if (traceMethodExits)
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000461 InsertCodeToShowMethodEntry(M, M->getEntryNode(), /*indent*/ 0);
462
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000463 for (Method::iterator BI = M->begin(); BI != M->end(); ++BI)
464 {
465 BasicBlock* bb = *BI;
466 bool isExitBlock = false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000467 vector<Instruction*> valuesToTraceInBB;
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000468
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000469 FindValuesToTraceInBB(bb, valuesToTraceInBB);
470
471 if (bb->succ_begin() == bb->succ_end())
472 { // record this as an exit block
473 exitBlocks.push_back(bb);
474 isExitBlock = true;
475 }
476
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000477 if (traceBasicBlockExits)
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000478 TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000479 /*indent*/ 4, /*isMethodExit*/ false,
480 &valuesStoredInMethod);
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000481 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000482
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000483 if (traceMethodExits)
484 for (unsigned i=0; i < exitBlocks.size(); ++i)
485 {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000486 TraceValuesAtBBExit(valuesStoredInMethod, exitBlocks[i], module,
487 /*indent*/ 0, /*isMethodExit*/ true,
488 /*valuesStoredInMethod*/ NULL);
489 InsertCodeToShowMethodExit(M, exitBlocks[i], /*indent*/ 0);
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000490 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000491
Chris Lattner8d9e3772001-10-18 05:28:08 +0000492 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000493}