blob: c778ca8b0dfc57255c2539ce6eb5a1448f2611ca [file] [log] [blame]
Chris Lattner29c14732001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
3// Support for inserting LLVM code to print values at basic block and method
4// 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 Lattnere9bb2df2001-12-03 22:26:30 +000010#include "llvm/ConstantVals.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/Method.h"
16#include "llvm/Module.h"
17#include "llvm/SymbolTable.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000018#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/StringExtras.h"
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000020#include <sstream>
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::vector;
22using std::string;
Vikram S. Adved8893302001-10-28 21:37:25 +000023
Chris Lattner29c14732001-12-14 16:26:05 +000024// Add a prototype for printf if it is not already in the program.
25//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000026bool InsertTraceCode::doInitialization(Module *M) {
Chris Lattner29c14732001-12-14 16:26:05 +000027 SymbolTable *ST = M->getSymbolTable();
28 const Type *SBP = PointerType::get(Type::SByteTy);
29 const MethodType *MTy =
30 MethodType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Vikram S. Adved8893302001-10-28 21:37:25 +000031
Chris Lattner29c14732001-12-14 16:26:05 +000032 if (Value *Meth = ST->lookup(PointerType::get(MTy), "printf")) {
33 PrintfMeth = cast<Method>(Meth);
34 return false;
35 }
36
37 // Create a new method and add it to the module
38 PrintfMeth = new Method(MTy, false, "printf");
39 M->getMethodList().push_back(PrintfMeth);
40 return true;
Vikram S. Adved8893302001-10-28 21:37:25 +000041}
42
Chris Lattner29c14732001-12-14 16:26:05 +000043
44static inline GlobalVariable *getStringRef(Module *M, const string &str) {
45 // Create a constant internal string reference...
46 Constant *Init = ConstantArray::get(str);
47 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
48 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000049 M->getGlobalList().push_back(GV);
50 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000051}
52
Vikram S. Advebedb00d2001-10-18 13:49:22 +000053
Chris Lattner29c14732001-12-14 16:26:05 +000054//
55// Check if this instruction has any uses outside its basic block,
56// or if it used by either a Call or Return instruction.
57//
58static inline bool LiveAtBBExit(const Instruction* I) {
59 const BasicBlock *BB = I->getParent();
60 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
61 if (const Instruction *UI = dyn_cast<Instruction>(*U))
62 if (UI->getParent() != BB || isa<ReturnInst>(UI))
63 return true;
64
65 return false;
66}
67
68
69static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +000070 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +000071 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +000072 //
73 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +000074 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +000075 opCode != Instruction::PHINode &&
76 opCode != Instruction::Cast);
77}
78
Chris Lattner29c14732001-12-14 16:26:05 +000079
80static bool ShouldTraceValue(const Instruction *I) {
81 return
82 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
83 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +000084}
85
Chris Lattner29c14732001-12-14 16:26:05 +000086static string getPrintfCodeFor(const Value *V) {
87 if (V == 0) return "";
88 switch (V->getType()->getPrimitiveID()) {
89 case Type::BoolTyID:
90 case Type::UByteTyID: case Type::UShortTyID:
91 case Type::UIntTyID: case Type::ULongTyID:
92 case Type::SByteTyID: case Type::ShortTyID:
93 case Type::IntTyID: case Type::LongTyID:
94 return "%d";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000095
Chris Lattner29c14732001-12-14 16:26:05 +000096 case Type::FloatTyID: case Type::DoubleTyID:
97 return "%g";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000098
Chris Lattner29c14732001-12-14 16:26:05 +000099 case Type::LabelTyID: case Type::PointerTyID:
100 return "%p";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000101
Chris Lattner29c14732001-12-14 16:26:05 +0000102 default:
103 assert(0 && "Illegal value to print out...");
104 return "";
105 }
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000106}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000107
108
Chris Lattner29c14732001-12-14 16:26:05 +0000109static void InsertPrintInst(Value *V, BasicBlock *BB, BasicBlock::iterator &BBI,
110 string Message, Method *Printf) {
111 // Escape Message by replacing all % characters with %% chars.
112 unsigned Offset = 0;
113 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner29c14732001-12-14 16:26:05 +0000114 Message.replace(Offset, 2, "%%");
115 Offset += 2; // Skip over the new %'s
116 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000117
Chris Lattner29c14732001-12-14 16:26:05 +0000118 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000119
Chris Lattner44571632001-10-18 06:03:05 +0000120 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000121 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
122
123 // Turn the format string into an sbyte *
124 Instruction *GEP =
125 new GetElementPtrInst(fmtVal,
126 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
127 "trstr");
128 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner44571632001-10-18 06:03:05 +0000129
130 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000131 vector<Value*> PrintArgs;
132 PrintArgs.push_back(GEP);
133 if (V) PrintArgs.push_back(V);
134 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner44571632001-10-18 06:03:05 +0000135 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner29c14732001-12-14 16:26:05 +0000136}
137
Chris Lattner44571632001-10-18 06:03:05 +0000138
Chris Lattner29c14732001-12-14 16:26:05 +0000139static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
140 BasicBlock::iterator &BBI,
141 const string &Message, Method *Printf) {
Chris Lattner697954c2002-01-20 22:54:45 +0000142 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000143 if (V) WriteAsOperand(OutStr, V);
144 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", Printf);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000145}
146
147
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000148// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000149// for each value in valueVec[] that is live at the end of that basic block,
150// or that is stored to memory in this basic block.
151// If the value is stored to memory, we load it back before printing
152// We also return all such loaded values in the vector valuesStoredInMethod
153// for printing at the exit from the method. (Note that in each invocation
154// of the method, this will only get the last value stored for each static
155// store instruction).
156// *bb must be the block in which the value is computed;
157// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000158//
Chris Lattner29c14732001-12-14 16:26:05 +0000159static void TraceValuesAtBBExit(BasicBlock *BB, Method *Printf,
160 vector<Instruction*> *valuesStoredInMethod) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000161 // Get an iterator to point to the insertion location, which is
162 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000163 //
Chris Lattner29c14732001-12-14 16:26:05 +0000164 BasicBlock::iterator InsertPos = BB->end()-1;
165 assert((*InsertPos)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000166
Vikram S. Adved8893302001-10-28 21:37:25 +0000167 // If the terminator is a conditional branch, insert the trace code just
168 // before the instruction that computes the branch condition (just to
169 // avoid putting a call between the CC-setting instruction and the branch).
170 // Use laterInstrSet to mark instructions that come after the setCC instr
171 // because those cannot be traced at the location we choose.
172 //
Chris Lattner29c14732001-12-14 16:26:05 +0000173 Instruction *SetCC = 0;
174 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
175 if (!Branch->isUnconditional())
176 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
177 if (I->getParent() == BB) {
178 SetCC = I;
179 while (*InsertPos != SetCC)
180 --InsertPos; // Back up until we can insert before the setcc
181 }
182
183 // Copy all of the instructions into a vector to avoid problems with Setcc
184 const vector<Instruction*> Insts(BB->begin(), InsertPos);
185
Chris Lattner697954c2002-01-20 22:54:45 +0000186 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000187 WriteAsOperand(OutStr, BB, false);
188 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), Printf);
189
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000190 // Insert a print instruction for each value.
191 //
Chris Lattner29c14732001-12-14 16:26:05 +0000192 for (vector<Instruction*>::const_iterator II = Insts.begin(),
193 IE = Insts.end(); II != IE; ++II) {
194 Instruction *I = *II;
195 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
196 assert(valuesStoredInMethod &&
197 "Should not be printing a store instruction at method exit");
198 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
199 "reload");
200 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
201 valuesStoredInMethod->push_back(LI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000202 }
Chris Lattner29c14732001-12-14 16:26:05 +0000203 if (ShouldTraceValue(I))
204 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf);
205 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000206}
207
Chris Lattner29c14732001-12-14 16:26:05 +0000208static inline void InsertCodeToShowMethodEntry(Method *M, Method *Printf) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000209 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000210 BasicBlock *BB = M->getEntryNode();
211 BasicBlock::iterator BBI = BB->begin();
212
Chris Lattner697954c2002-01-20 22:54:45 +0000213 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000214 WriteAsOperand(OutStr, M, true);
215 InsertPrintInst(0, BB, BBI, "ENTERING METHOD: " + OutStr.str(), Printf);
216
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000217 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000218 const Method::ArgumentListType &argList = M->getArgumentList();
219 unsigned ArgNo = 0;
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000220 for (Method::ArgumentListType::const_iterator
Chris Lattner29c14732001-12-14 16:26:05 +0000221 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
222 InsertVerbosePrintInst(*I, BB, BBI,
223 " Arg #" + utostr(ArgNo), Printf);
224 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000225}
226
227
Chris Lattner29c14732001-12-14 16:26:05 +0000228static inline void InsertCodeToShowMethodExit(BasicBlock *BB, Method *Printf) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000229 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000230 BasicBlock::iterator BBI = BB->end()-1;
231 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000232
Chris Lattner697954c2002-01-20 22:54:45 +0000233 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000234 WriteAsOperand(OutStr, BB->getParent(), true);
235 InsertPrintInst(0, BB, BBI, "LEAVING METHOD: " + OutStr.str(), Printf);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000236
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000237 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000238 if (BB->getParent()->getReturnType() != Type::VoidTy)
239 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", Printf);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000240}
241
242
Chris Lattner29c14732001-12-14 16:26:05 +0000243bool InsertTraceCode::doit(Method *M, bool traceBasicBlockExits,
244 bool traceMethodEvents, Method *Printf) {
245 if (M->isExternal() || (!traceBasicBlockExits && !traceMethodEvents))
Chris Lattner8d9e3772001-10-18 05:28:08 +0000246 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000247
Chris Lattner29c14732001-12-14 16:26:05 +0000248 vector<Instruction*> valuesStoredInMethod;
249 vector<BasicBlock*> exitBlocks;
250
Chris Lattner29c14732001-12-14 16:26:05 +0000251 if (traceMethodEvents)
252 InsertCodeToShowMethodEntry(M, Printf);
253
254 for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) {
255 BasicBlock *BB = *BI;
256 if (isa<ReturnInst>(BB->getTerminator()))
257 exitBlocks.push_back(BB); // record this as an exit block
258
259 if (traceBasicBlockExits)
260 TraceValuesAtBBExit(BB, Printf, &valuesStoredInMethod);
261 }
262
263 if (traceMethodEvents)
264 for (unsigned i=0; i < exitBlocks.size(); ++i) {
265#if 0
266 TraceValuesAtBBExit(valuesStoredInMethod, exitBlocks[i], module,
267 /*indent*/ 0, /*isMethodExit*/ true,
268 /*valuesStoredInMethod*/ NULL);
269#endif
270 InsertCodeToShowMethodExit(exitBlocks[i], Printf);
271 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000272
Chris Lattner8d9e3772001-10-18 05:28:08 +0000273 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000274}