blob: ae356a2f73df1bbfc0f180902d90e8a9840326d5 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- TraceValues.cpp - Value Tracing for debugging ----------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner29c14732001-12-14 16:26:05 +00009//
Chris Lattner649f5dd2002-04-14 06:15:24 +000010// Support for inserting LLVM code to print values at basic block and function
Chris Lattner29c14732001-12-14 16:26:05 +000011// exits.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Advedf1892f2001-10-14 23:18:45 +000014
Chris Lattnerc56d2392003-01-14 22:39:29 +000015#include "llvm/Transforms/Instrumentation.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000017#include "llvm/DerivedTypes.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000019#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000020#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000021#include "llvm/Assembly/Writer.h"
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000022#include "Support/CommandLine.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
Chris Lattner2e0769e2002-05-20 21:43:59 +000024#include <algorithm>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000025#include <sstream>
Chris Lattnerf7703df2004-01-09 06:12:26 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner5ff62e92002-07-22 02:10:13 +000028static cl::opt<bool>
29DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
Chris Lattneraf7ccd92003-04-13 03:50:14 +000030 cl::desc("Disable pointer hashing in the -trace or -tracem "
31 "passes"));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000032
Chris Lattner7b6dd292003-10-27 21:44:09 +000033static cl::list<std::string>
Chris Lattneraf7ccd92003-04-13 03:50:14 +000034TraceFuncNames("tracefunc", cl::desc("Only trace specific functions in the "
35 "-trace or -tracem passes"),
Chris Lattner80e5ed92003-01-13 00:52:14 +000036 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000037
Chris Lattner11982662002-07-23 18:04:15 +000038static void TraceValuesAtBBExit(BasicBlock *BB,
39 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner7b6dd292003-10-27 21:44:09 +000040 std::vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000041
42// We trace a particular function if no functions to trace were specified
43// or if the function is in the specified list.
44//
Chris Lattner11982662002-07-23 18:04:15 +000045inline static bool
Chris Lattner80e5ed92003-01-13 00:52:14 +000046TraceThisFunction(Function &F)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000047{
Chris Lattner80e5ed92003-01-13 00:52:14 +000048 if (TraceFuncNames.empty()) return true;
Chris Lattner2e0769e2002-05-20 21:43:59 +000049
Chris Lattner80e5ed92003-01-13 00:52:14 +000050 return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
51 != TraceFuncNames.end();
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000052}
53
54
Chris Lattnerbd0ef772002-02-26 21:46:54 +000055namespace {
Chris Lattner2e0769e2002-05-20 21:43:59 +000056 struct ExternalFuncs {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000057 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
58 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner7e708292002-06-25 16:13:24 +000059 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000060 };
61
Chris Lattnerf57b8452002-04-27 06:56:12 +000062 class InsertTraceCode : public FunctionPass {
Chris Lattner11982662002-07-23 18:04:15 +000063 protected:
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000064 ExternalFuncs externalFuncs;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000065 public:
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000067 // Add a prototype for runtime functions not already in the program.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000068 //
Chris Lattner7e708292002-06-25 16:13:24 +000069 bool doInitialization(Module &M);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070
71 //--------------------------------------------------------------------------
72 // Function InsertCodeToTraceValues
73 //
Chris Lattner649f5dd2002-04-14 06:15:24 +000074 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner79df7c02002-03-26 18:01:55 +000075 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000076 //
Chris Lattner11982662002-07-23 18:04:15 +000077 bool doit(Function *M);
78
Chris Lattner7b6dd292003-10-27 21:44:09 +000079 virtual void handleBasicBlock(BasicBlock *BB,
80 std::vector<Instruction*> &VI) = 0;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000081
Chris Lattner649f5dd2002-04-14 06:15:24 +000082 // runOnFunction - This method does the work.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000083 //
Chris Lattner11982662002-07-23 18:04:15 +000084 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000085
86 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000087 AU.setPreservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000088 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000089 };
Chris Lattner11982662002-07-23 18:04:15 +000090
91 struct FunctionTracer : public InsertTraceCode {
92 // Ignore basic blocks here...
Chris Lattner7b6dd292003-10-27 21:44:09 +000093 virtual void handleBasicBlock(BasicBlock *BB,
94 std::vector<Instruction*> &VI) {}
Chris Lattner11982662002-07-23 18:04:15 +000095 };
96
97 struct BasicBlockTracer : public InsertTraceCode {
98 // Trace basic blocks here...
Chris Lattner7b6dd292003-10-27 21:44:09 +000099 virtual void handleBasicBlock(BasicBlock *BB,
100 std::vector<Instruction*> &VI) {
Chris Lattner11982662002-07-23 18:04:15 +0000101 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
102 externalFuncs.HashPtrFunc, &VI);
103 }
104 };
105
106 // Register the passes...
Chris Lattnera6275cc2002-07-26 21:12:46 +0000107 RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
108 RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000109} // end anonymous namespace
110
111
Chris Lattnerf7703df2004-01-09 06:12:26 +0000112Pass *llvm::createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner11982662002-07-23 18:04:15 +0000113 return new FunctionTracer();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000114}
115
Chris Lattnerf7703df2004-01-09 06:12:26 +0000116Pass *llvm::createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner11982662002-07-23 18:04:15 +0000117 return new BasicBlockTracer();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000118}
119
Chris Lattner11982662002-07-23 18:04:15 +0000120
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000121// Add a prototype for external functions used by the tracing code.
Chris Lattner29c14732001-12-14 16:26:05 +0000122//
Chris Lattner7e708292002-06-25 16:13:24 +0000123void ExternalFuncs::doInitialization(Module &M) {
Chris Lattner29c14732001-12-14 16:26:05 +0000124 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000125 const FunctionType *MTy =
Chris Lattner7b6dd292003-10-27 21:44:09 +0000126 FunctionType::get(Type::IntTy, std::vector<const Type*>(1, SBP), true);
Chris Lattner7e708292002-06-25 16:13:24 +0000127 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000128
129 // uint (sbyte*)
Chris Lattner5f07c872003-08-31 00:20:36 +0000130 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", Type::UIntTy, SBP,
131 0);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000132
Chris Lattner2e0769e2002-05-20 21:43:59 +0000133 // void (sbyte*)
Chris Lattner5f07c872003-08-31 00:20:36 +0000134 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum",
135 Type::VoidTy, SBP, 0);
136 RecordPtrFunc = M.getOrInsertFunction("RecordPointer",
137 Type::VoidTy, SBP, 0);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000138
Chris Lattner5f07c872003-08-31 00:20:36 +0000139 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", Type::VoidTy, 0);
Chris Lattner7e708292002-06-25 16:13:24 +0000140 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Chris Lattner5f07c872003-08-31 00:20:36 +0000141 Type::VoidTy, 0);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000142}
143
144
145// Add a prototype for external functions used by the tracing code.
146//
Chris Lattner7e708292002-06-25 16:13:24 +0000147bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000148 externalFuncs.doInitialization(M);
Chris Lattner89851072002-03-29 03:43:24 +0000149 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000150}
151
Chris Lattner29c14732001-12-14 16:26:05 +0000152
Chris Lattner7b6dd292003-10-27 21:44:09 +0000153static inline GlobalVariable *getStringRef(Module *M, const std::string &str) {
Chris Lattner29c14732001-12-14 16:26:05 +0000154 // Create a constant internal string reference...
155 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +0000156
157 // Create the global variable and record it in the module
158 // The GV will be renamed to a unique name if needed.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000159 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
160 GlobalValue::InternalLinkage, Init,
Chris Lattner29c14732001-12-14 16:26:05 +0000161 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000162 M->getGlobalList().push_back(GV);
163 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000164}
165
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000166
Chris Lattner29c14732001-12-14 16:26:05 +0000167//
168// Check if this instruction has any uses outside its basic block,
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000169// or if it used by either a Call or Return instruction (ditto).
170// (Values stored to memory within this BB are live at end of BB but are
171// traced at the store instruction, not where they are computed.)
Chris Lattner29c14732001-12-14 16:26:05 +0000172//
173static inline bool LiveAtBBExit(const Instruction* I) {
174 const BasicBlock *BB = I->getParent();
175 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
176 if (const Instruction *UI = dyn_cast<Instruction>(*U))
177 if (UI->getParent() != BB || isa<ReturnInst>(UI))
178 return true;
179
180 return false;
181}
182
183
184static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000185 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000186 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000187 //
Chris Lattner0b16ae22002-10-13 19:39:16 +0000188 return (opCode < Instruction::OtherOpsBegin &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000189 opCode != Instruction::Alloca &&
Chris Lattner3b237fc2003-10-19 21:34:28 +0000190 opCode != Instruction::PHI &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000191 opCode != Instruction::Cast);
192}
193
Chris Lattner29c14732001-12-14 16:26:05 +0000194
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000195// Trace a value computed by an instruction if it is non-void, it is computed
196// by a real computation, not just a copy (see TraceThisOpCode), and
197// -- it is a load instruction: we want to check values read from memory
198// -- or it is live at exit from the basic block (i.e., ignore local temps)
199//
Chris Lattner29c14732001-12-14 16:26:05 +0000200static bool ShouldTraceValue(const Instruction *I) {
201 return
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000202 I->getType() != Type::VoidTy &&
203 TraceThisOpCode(I->getOpcode()) &&
204 (isa<LoadInst>(I) || LiveAtBBExit(I));
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000205}
206
Chris Lattner7b6dd292003-10-27 21:44:09 +0000207static std::string getPrintfCodeFor(const Value *V) {
Chris Lattner29c14732001-12-14 16:26:05 +0000208 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000209 if (V->getType()->isFloatingPoint())
210 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000211 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000212 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000213 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000214 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner65ad3722002-09-03 01:07:35 +0000215 else if (V->getType()->isIntegral())
Chris Lattner29c14732001-12-14 16:26:05 +0000216 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000217
Chris Lattner649f5dd2002-04-14 06:15:24 +0000218 assert(0 && "Illegal value to print out...");
219 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000220}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000221
222
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000223static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Chris Lattner7b6dd292003-10-27 21:44:09 +0000224 std::string Message,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000225 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-12-14 16:26:05 +0000226 // Escape Message by replacing all % characters with %% chars.
Chris Lattner7b6dd292003-10-27 21:44:09 +0000227 std::string Tmp;
Chris Lattner1a33e312002-10-17 16:22:08 +0000228 std::swap(Tmp, Message);
Chris Lattner7b6dd292003-10-27 21:44:09 +0000229 std::string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattner1a33e312002-10-17 16:22:08 +0000230 while (I != Tmp.end()) {
231 Message.append(Tmp.begin(), I);
232 Message += "%%";
233 ++I; // Make sure to erase the % as well...
234 Tmp.erase(Tmp.begin(), I);
235 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattner29c14732001-12-14 16:26:05 +0000236 }
Chris Lattner80e5ed92003-01-13 00:52:14 +0000237 Message += Tmp;
Chris Lattner29c14732001-12-14 16:26:05 +0000238 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000239
Chris Lattner44571632001-10-18 06:03:05 +0000240 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000241 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
242
243 // Turn the format string into an sbyte *
Reid Spencer518310c2004-07-18 00:44:37 +0000244 Constant *GEP=ConstantExpr::getGetElementPtr(fmtVal,
Chris Lattner7b6dd292003-10-27 21:44:09 +0000245 std::vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
Chris Lattner44571632001-10-18 06:03:05 +0000246
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000247 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-05-20 21:43:59 +0000248 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
249 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000250 if (V->getType() != SBP) // Cast pointer to be sbyte*
251 V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000252
Chris Lattner7b6dd292003-10-27 21:44:09 +0000253 std::vector<Value*> HashArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000254 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000255 }
256
Chris Lattner44571632001-10-18 06:03:05 +0000257 // Insert the first print instruction to print the string flag:
Chris Lattner7b6dd292003-10-27 21:44:09 +0000258 std::vector<Value*> PrintArgs;
Chris Lattner29c14732001-12-14 16:26:05 +0000259 PrintArgs.push_back(GEP);
260 if (V) PrintArgs.push_back(V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000261 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattner29c14732001-12-14 16:26:05 +0000262}
263
Chris Lattner44571632001-10-18 06:03:05 +0000264
Chris Lattner29c14732001-12-14 16:26:05 +0000265static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000266 Instruction *InsertBefore,
Chris Lattner7b6dd292003-10-27 21:44:09 +0000267 const std::string &Message, Function *Printf,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000268 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000269 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000270 if (V) WriteAsOperand(OutStr, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000271 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000272 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000273}
274
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000275static void
276InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000277 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000278 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000279
280 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000281 if (V->getType() != SBP) // Cast pointer to be sbyte*
282 V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
283
Chris Lattner7b6dd292003-10-27 21:44:09 +0000284 std::vector<Value*> releaseArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000285 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000286}
287
288static void
289InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000290 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000291 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000292 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000293 if (V->getType() != SBP) // Cast pointer to be sbyte*
294 V = new CastInst(V, SBP, "RP_cast", InsertBefore);
295
Chris Lattner7b6dd292003-10-27 21:44:09 +0000296 std::vector<Value*> releaseArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000297 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
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
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000308 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattnere408e252003-04-23 16:37:45 +0000309 if (FreeInst *FI = dyn_cast<FreeInst>(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000310 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattnere408e252003-04-23 16:37:45 +0000311 else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000312 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000313}
314
Chris Lattner8d9e3772001-10-18 05:28:08 +0000315
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000316// Insert print instructions at the end of basic block BB for each value
317// computed in BB that is live at the end of BB,
318// or that is stored to memory in BB.
319// If the value is stored to memory, we load it back before printing it
Chris Lattner79df7c02002-03-26 18:01:55 +0000320// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000321// for printing at the exit from the function. (Note that in each invocation
322// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000323// store instruction).
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000324//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000325static void TraceValuesAtBBExit(BasicBlock *BB,
326 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner7b6dd292003-10-27 21:44:09 +0000327 std::vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000328 // Get an iterator to point to the insertion location, which is
329 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000330 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000331 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000332
Chris Lattner697954c2002-01-20 22:54:45 +0000333 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000334 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000335 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
336 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000337
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000338 // Insert a print instruction for each instruction preceding InsertPos.
339 // The print instructions must go before InsertPos, so we use the
340 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000341 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000342 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
Chris Lattnere408e252003-04-23 16:37:45 +0000343 if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000344 // Trace the stored value and address
345 InsertVerbosePrintInst(SI->getOperand(0), BB, InsertPos,
346 " (store value) ", Printf, HashPtrToSeqNum);
347 InsertVerbosePrintInst(SI->getOperand(1), BB, InsertPos,
348 " (store addr ) ", Printf, HashPtrToSeqNum);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000349 }
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000350 else if (ShouldTraceValue(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000351 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000352 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000353}
354
Chris Lattner11982662002-07-23 18:04:15 +0000355static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000356 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000357 // Get an iterator to point to the insertion location
Chris Lattner02a3be02003-09-20 14:39:18 +0000358 BasicBlock &BB = F.getEntryBlock();
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000359 Instruction *InsertPos = BB.begin();
Chris Lattner29c14732001-12-14 16:26:05 +0000360
Chris Lattner697954c2002-01-20 22:54:45 +0000361 std::ostringstream OutStr;
Chris Lattner80e5ed92003-01-13 00:52:14 +0000362 WriteAsOperand(OutStr, &F);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000363 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000364 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000365
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000366 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000367 unsigned ArgNo = 0;
Chris Lattner11982662002-07-23 18:04:15 +0000368 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000369 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000370 " Arg #" + utostr(ArgNo) + ": ", Printf,
371 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000372 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000373}
374
375
Chris Lattner79df7c02002-03-26 18:01:55 +0000376static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000377 Function *Printf,
378 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000379 // Get an iterator to point to the insertion location
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000380 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000381
Chris Lattner697954c2002-01-20 22:54:45 +0000382 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000383 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000384 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000385 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000386
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000387 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000388 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000389 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000390 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000391}
392
393
Chris Lattner11982662002-07-23 18:04:15 +0000394bool InsertTraceCode::runOnFunction(Function &F) {
395 if (!TraceThisFunction(F))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000396 return false;
397
Chris Lattner7b6dd292003-10-27 21:44:09 +0000398 std::vector<Instruction*> valuesStoredInFunction;
399 std::vector<BasicBlock*> exitBlocks;
Chris Lattner29c14732001-12-14 16:26:05 +0000400
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000401 // Insert code to trace values at function entry
Chris Lattner11982662002-07-23 18:04:15 +0000402 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
403 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000404
405 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000406 if (!DisablePtrHashing)
Chris Lattner7b6dd292003-10-27 21:44:09 +0000407 new CallInst(externalFuncs.PushOnEntryFunc, std::vector<Value*>(), "",
Chris Lattner02a3be02003-09-20 14:39:18 +0000408 F.getEntryBlock().begin());
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000409
Chris Lattner11982662002-07-23 18:04:15 +0000410 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner29c14732001-12-14 16:26:05 +0000411 if (isa<ReturnInst>(BB->getTerminator()))
412 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner11982662002-07-23 18:04:15 +0000413
414 // Insert trace code if this basic block is interesting...
415 handleBasicBlock(BB, valuesStoredInFunction);
416
Chris Lattner2e0769e2002-05-20 21:43:59 +0000417 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000418 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000419 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000420
Chris Lattner11982662002-07-23 18:04:15 +0000421 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000422 {
423 // Insert code to trace values at function exit
Chris Lattner11982662002-07-23 18:04:15 +0000424 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
425 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000426
427 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000428 if (!DisablePtrHashing)
Chris Lattner7b6dd292003-10-27 21:44:09 +0000429 new CallInst(externalFuncs.ReleaseOnReturnFunc, std::vector<Value*>(),
430 "", exitBlocks[i]->getTerminator());
Chris Lattner29c14732001-12-14 16:26:05 +0000431 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000432
Chris Lattner8d9e3772001-10-18 05:28:08 +0000433 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000434}