blob: 7f7321dbf06dfa74b37b06f58dd2650f2618eb9b [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"
Chris Lattner42a41272002-04-09 18:37:46 +000015#include "llvm/BasicBlock.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000016#include "llvm/Function.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000017#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000019#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
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 Lattnerbd0ef772002-02-26 21:46:54 +000025namespace {
26 class InsertTraceCode : public MethodPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000027 bool TraceBasicBlockExits, TraceFunctionExits;
28 Function *PrintfFunc;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029 public:
Chris Lattner79df7c02002-03-26 18:01:55 +000030 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattnerbd0ef772002-02-26 21:46:54 +000031 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner79df7c02002-03-26 18:01:55 +000032 TraceFunctionExits(traceFunctionExits) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000033
34 // Add a prototype for printf if it is not already in the program.
35 //
36 bool doInitialization(Module *M);
37
38 //--------------------------------------------------------------------------
39 // Function InsertCodeToTraceValues
40 //
41 // Inserts tracing code for all live values at basic block and/or method
Chris Lattner79df7c02002-03-26 18:01:55 +000042 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000043 //
Chris Lattner79df7c02002-03-26 18:01:55 +000044 static bool doit(Function *M, bool traceBasicBlockExits,
45 bool traceFunctionExits, Function *Printf);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000046
47 // runOnMethod - This method does the work. Always successful.
48 //
Chris Lattner79df7c02002-03-26 18:01:55 +000049 bool runOnMethod(Function *F) {
50 return doit(F, TraceBasicBlockExits, TraceFunctionExits, PrintfFunc);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000051 }
52 };
53} // end anonymous namespace
54
55
56Pass *createTraceValuesPassForMethod() { // Just trace methods
57 return new InsertTraceCode(false, true);
58}
59
60Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and methods
61 return new InsertTraceCode(true, true);
62}
63
64
65
66
Chris Lattner29c14732001-12-14 16:26:05 +000067// Add a prototype for printf if it is not already in the program.
68//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000069bool InsertTraceCode::doInitialization(Module *M) {
Chris Lattner29c14732001-12-14 16:26:05 +000070 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +000071 const FunctionType *MTy =
72 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Vikram S. Adved8893302001-10-28 21:37:25 +000073
Chris Lattner89851072002-03-29 03:43:24 +000074 PrintfFunc = M->getOrInsertFunction("printf", MTy);
75 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +000076}
77
Chris Lattner29c14732001-12-14 16:26:05 +000078
79static inline GlobalVariable *getStringRef(Module *M, const string &str) {
80 // Create a constant internal string reference...
81 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +000082
83 // Create the global variable and record it in the module
84 // The GV will be renamed to a unique name if needed.
Chris Lattner29c14732001-12-14 16:26:05 +000085 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
86 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000087 M->getGlobalList().push_back(GV);
88 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000089}
90
Vikram S. Advebedb00d2001-10-18 13:49:22 +000091
Chris Lattner29c14732001-12-14 16:26:05 +000092//
93// Check if this instruction has any uses outside its basic block,
94// or if it used by either a Call or Return instruction.
95//
96static inline bool LiveAtBBExit(const Instruction* I) {
97 const BasicBlock *BB = I->getParent();
98 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
99 if (const Instruction *UI = dyn_cast<Instruction>(*U))
100 if (UI->getParent() != BB || isa<ReturnInst>(UI))
101 return true;
102
103 return false;
104}
105
106
107static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000108 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000109 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000110 //
111 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000112 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000113 opCode != Instruction::PHINode &&
114 opCode != Instruction::Cast);
115}
116
Chris Lattner29c14732001-12-14 16:26:05 +0000117
118static bool ShouldTraceValue(const Instruction *I) {
119 return
120 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
121 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000122}
123
Chris Lattner29c14732001-12-14 16:26:05 +0000124static string getPrintfCodeFor(const Value *V) {
125 if (V == 0) return "";
126 switch (V->getType()->getPrimitiveID()) {
127 case Type::BoolTyID:
128 case Type::UByteTyID: case Type::UShortTyID:
129 case Type::UIntTyID: case Type::ULongTyID:
130 case Type::SByteTyID: case Type::ShortTyID:
131 case Type::IntTyID: case Type::LongTyID:
132 return "%d";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000133
Chris Lattner29c14732001-12-14 16:26:05 +0000134 case Type::FloatTyID: case Type::DoubleTyID:
135 return "%g";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000136
Chris Lattner29c14732001-12-14 16:26:05 +0000137 case Type::LabelTyID: case Type::PointerTyID:
138 return "%p";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000139
Chris Lattner29c14732001-12-14 16:26:05 +0000140 default:
141 assert(0 && "Illegal value to print out...");
142 return "";
143 }
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000144}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000145
146
Chris Lattner29c14732001-12-14 16:26:05 +0000147static void InsertPrintInst(Value *V, BasicBlock *BB, BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 string Message, Function *Printf) {
Chris Lattner29c14732001-12-14 16:26:05 +0000149 // Escape Message by replacing all % characters with %% chars.
150 unsigned Offset = 0;
151 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner29c14732001-12-14 16:26:05 +0000152 Message.replace(Offset, 2, "%%");
153 Offset += 2; // Skip over the new %'s
154 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000155
Chris Lattner29c14732001-12-14 16:26:05 +0000156 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000157
Chris Lattner44571632001-10-18 06:03:05 +0000158 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000159 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
160
161 // Turn the format string into an sbyte *
162 Instruction *GEP =
163 new GetElementPtrInst(fmtVal,
164 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
165 "trstr");
166 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner44571632001-10-18 06:03:05 +0000167
168 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000169 vector<Value*> PrintArgs;
170 PrintArgs.push_back(GEP);
171 if (V) PrintArgs.push_back(V);
172 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner44571632001-10-18 06:03:05 +0000173 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner29c14732001-12-14 16:26:05 +0000174}
175
Chris Lattner44571632001-10-18 06:03:05 +0000176
Chris Lattner29c14732001-12-14 16:26:05 +0000177static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
178 BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000179 const string &Message, Function *Printf) {
Chris Lattner697954c2002-01-20 22:54:45 +0000180 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000181 if (V) WriteAsOperand(OutStr, V);
182 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", Printf);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000183}
184
185
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000186// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000187// for each value in valueVec[] that is live at the end of that basic block,
188// or that is stored to memory in this basic block.
189// If the value is stored to memory, we load it back before printing
Chris Lattner79df7c02002-03-26 18:01:55 +0000190// We also return all such loaded values in the vector valuesStoredInFunction
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000191// for printing at the exit from the method. (Note that in each invocation
192// of the method, this will only get the last value stored for each static
193// store instruction).
194// *bb must be the block in which the value is computed;
195// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000196//
Chris Lattner79df7c02002-03-26 18:01:55 +0000197static void TraceValuesAtBBExit(BasicBlock *BB, Function *Printf,
198 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000199 // Get an iterator to point to the insertion location, which is
200 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000201 //
Chris Lattner29c14732001-12-14 16:26:05 +0000202 BasicBlock::iterator InsertPos = BB->end()-1;
203 assert((*InsertPos)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000204
Vikram S. Adved8893302001-10-28 21:37:25 +0000205 // If the terminator is a conditional branch, insert the trace code just
206 // before the instruction that computes the branch condition (just to
207 // avoid putting a call between the CC-setting instruction and the branch).
208 // Use laterInstrSet to mark instructions that come after the setCC instr
209 // because those cannot be traced at the location we choose.
210 //
Chris Lattner29c14732001-12-14 16:26:05 +0000211 Instruction *SetCC = 0;
212 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
213 if (!Branch->isUnconditional())
214 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
215 if (I->getParent() == BB) {
216 SetCC = I;
217 while (*InsertPos != SetCC)
218 --InsertPos; // Back up until we can insert before the setcc
219 }
220
221 // Copy all of the instructions into a vector to avoid problems with Setcc
222 const vector<Instruction*> Insts(BB->begin(), InsertPos);
223
Chris Lattner697954c2002-01-20 22:54:45 +0000224 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000225 WriteAsOperand(OutStr, BB, false);
226 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), Printf);
227
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000228 // Insert a print instruction for each value.
229 //
Chris Lattner29c14732001-12-14 16:26:05 +0000230 for (vector<Instruction*>::const_iterator II = Insts.begin(),
231 IE = Insts.end(); II != IE; ++II) {
232 Instruction *I = *II;
233 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000234 assert(valuesStoredInFunction &&
Chris Lattner29c14732001-12-14 16:26:05 +0000235 "Should not be printing a store instruction at method exit");
236 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
237 "reload");
238 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner79df7c02002-03-26 18:01:55 +0000239 valuesStoredInFunction->push_back(LI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000240 }
Chris Lattner29c14732001-12-14 16:26:05 +0000241 if (ShouldTraceValue(I))
242 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf);
243 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000244}
245
Chris Lattner79df7c02002-03-26 18:01:55 +0000246static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000247 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000248 BasicBlock *BB = M->getEntryNode();
249 BasicBlock::iterator BBI = BB->begin();
250
Chris Lattner697954c2002-01-20 22:54:45 +0000251 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000252 WriteAsOperand(OutStr, M, true);
253 InsertPrintInst(0, BB, BBI, "ENTERING METHOD: " + OutStr.str(), Printf);
254
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000255 // Now print all the incoming arguments
Chris Lattner79df7c02002-03-26 18:01:55 +0000256 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattner29c14732001-12-14 16:26:05 +0000257 unsigned ArgNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000258 for (Function::ArgumentListType::const_iterator
Chris Lattner29c14732001-12-14 16:26:05 +0000259 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
260 InsertVerbosePrintInst(*I, BB, BBI,
261 " Arg #" + utostr(ArgNo), Printf);
262 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000263}
264
265
Chris Lattner79df7c02002-03-26 18:01:55 +0000266static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
267 Function *Printf) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000268 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000269 BasicBlock::iterator BBI = BB->end()-1;
270 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000271
Chris Lattner697954c2002-01-20 22:54:45 +0000272 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000273 WriteAsOperand(OutStr, BB->getParent(), true);
274 InsertPrintInst(0, BB, BBI, "LEAVING METHOD: " + OutStr.str(), Printf);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000275
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000276 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000277 if (BB->getParent()->getReturnType() != Type::VoidTy)
278 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", Printf);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000279}
280
281
Chris Lattner79df7c02002-03-26 18:01:55 +0000282bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
283 bool traceFunctionEvents, Function *Printf) {
284 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattner8d9e3772001-10-18 05:28:08 +0000285 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000286
Chris Lattner79df7c02002-03-26 18:01:55 +0000287 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000288 vector<BasicBlock*> exitBlocks;
289
Chris Lattner79df7c02002-03-26 18:01:55 +0000290 if (traceFunctionEvents)
291 InsertCodeToShowFunctionEntry(M, Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000292
Chris Lattner79df7c02002-03-26 18:01:55 +0000293 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattner29c14732001-12-14 16:26:05 +0000294 BasicBlock *BB = *BI;
295 if (isa<ReturnInst>(BB->getTerminator()))
296 exitBlocks.push_back(BB); // record this as an exit block
297
298 if (traceBasicBlockExits)
Chris Lattner79df7c02002-03-26 18:01:55 +0000299 TraceValuesAtBBExit(BB, Printf, &valuesStoredInFunction);
Chris Lattner29c14732001-12-14 16:26:05 +0000300 }
301
Chris Lattner79df7c02002-03-26 18:01:55 +0000302 if (traceFunctionEvents)
Chris Lattner29c14732001-12-14 16:26:05 +0000303 for (unsigned i=0; i < exitBlocks.size(); ++i) {
304#if 0
Chris Lattner79df7c02002-03-26 18:01:55 +0000305 TraceValuesAtBBExit(valuesStoredInFunction, exitBlocks[i], module,
306 /*indent*/ 0, /*isFunctionExit*/ true,
307 /*valuesStoredInFunction*/ NULL);
Chris Lattner29c14732001-12-14 16:26:05 +0000308#endif
Chris Lattner79df7c02002-03-26 18:01:55 +0000309 InsertCodeToShowFunctionExit(exitBlocks[i], Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000310 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000311
Chris Lattner8d9e3772001-10-18 05:28:08 +0000312 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000313}