Massive hacks to try to fix subtle logic bugs. I think it's all working now,
[oota-llvm.git] / lib / Transforms / Instrumentation / TraceValues.cpp
1 // $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"
22 #include "llvm/iTerminators.h"
23 #include "llvm/iOther.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/Method.h"
26 #include "llvm/Module.h"
27 #include "llvm/SymbolTable.h"
28 #include <strstream>
29 #include "llvm/Assembly/Writer.h"
30
31
32 const char* const PRINTF = "printVal";
33
34 static inline GlobalVariable *
35 GetStringRef(Module *M, const string &str)
36 {
37   ConstPoolArray *Init = ConstPoolArray::get(str);
38   GlobalVariable *V = new GlobalVariable(Init->getType(), /*Const*/true, Init);
39   M->getGlobalList().push_back(V);
40
41   return V;
42 }
43
44 static inline bool
45 TraceThisOpCode(unsigned opCode)
46 {
47   // Explicitly test for opCodes *not* to trace so that any new opcodes will
48   // be traced by default (VoidTy's are already excluded)
49   // 
50   return (opCode  < Instruction::FirstOtherOp &&
51           opCode != Instruction::Alloca &&
52           opCode != Instruction::PHINode &&
53           opCode != Instruction::Cast);
54 }
55
56
57 static void 
58 FindValuesToTraceInBB(BasicBlock* bb, vector<Value*>& valuesToTraceInBB)
59 {
60   for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II)
61     if ((*II)->getType()->isPrimitiveType() && 
62         (*II)->getType() != Type::VoidTy &&
63         TraceThisOpCode((*II)->getOpcode()))
64       {
65         valuesToTraceInBB.push_back(*II);
66       }
67 }
68
69 // The invocation should be:
70 //       call "printf"(fmt, value).
71 // 
72 static Value *GetPrintMethodForType(Module *Mod, const Type *valueType) {
73   vector<const Type*> ArgTys;
74   ArgTys.reserve(2);
75   ArgTys.push_back(PointerType::get(ArrayType::get(Type::UByteTy)));
76   ArgTys.push_back(valueType);
77   
78   MethodType *printMethodTy = MethodType::get(Type::VoidTy, ArgTys,
79                                               /*isVarArg*/ false);
80   
81   SymbolTable *ST = Mod->getSymbolTableSure();
82   if (Value *V = ST->lookup(PointerType::get(printMethodTy), PRINTF))
83     return V;
84
85   // Create a new method and add it to the module
86   Method *M = new Method(printMethodTy, PRINTF);
87   Mod->getMethodList().push_back(M);
88   return M;
89 }
90
91
92 static Instruction*
93 CreatePrintInstr(Value* val,
94                  const BasicBlock* bb,
95                  Module* module,
96                  unsigned int indent,
97                  bool isMethodExit)
98 {
99   strstream scopeNameString;
100   const Type* valueType = val->getType();
101   
102   assert(valueType->isPrimitiveType() &&
103          valueType->getPrimitiveID() != Type::VoidTyID &&
104          valueType->getPrimitiveID() != Type::TypeTyID &&
105          valueType->getPrimitiveID() != Type::LabelTyID && 
106          "Unsupported type for printing");
107   
108   const Value* scopeToUse = (isMethodExit)? (const Value*) bb->getParent()
109                                           : (const Value*) bb;
110   WriteAsOperand(scopeNameString, scopeToUse) << " : ";
111   WriteAsOperand(scopeNameString, val) << " = "
112                                        << val->getType()->getDescription()
113                                        << ends;
114   string fmtString(indent, ' ');
115   
116   fmtString += " At exit of " + string(isMethodExit ? "Method " : "BB ") +
117     scopeNameString.str();
118   
119   switch(valueType->getPrimitiveID()) {
120   case Type::BoolTyID:
121   case Type::UByteTyID: case Type::UShortTyID:
122   case Type::UIntTyID:  case Type::ULongTyID:
123   case Type::SByteTyID: case Type::ShortTyID:
124   case Type::IntTyID:   case Type::LongTyID:
125     fmtString += " %d\0A";
126     break;
127     
128   case Type::FloatTyID:     case Type::DoubleTyID:
129     fmtString += " %g\0A";
130     break;
131     
132   case Type::PointerTyID:
133     fmtString += " %p\0A";
134     break;
135     
136   default:
137     assert(0 && "Should not get here.  Check the IF expression above");
138     return NULL;
139   }
140   
141   GlobalVariable *fmtVal = GetStringRef(module, fmtString);
142   
143   vector<Value*> paramList;
144   paramList.push_back(fmtVal);
145   paramList.push_back(val);
146
147   return new CallInst(GetPrintMethodForType(module, valueType), paramList);
148 }
149
150
151
152 // 
153 // Insert print instructions at the end of the basic block *bb
154 // for each value in valueVec[].  *bb must postdominate the block
155 // in which the value is computed; this is not checked here.
156 // 
157 static void
158 TraceValuesAtBBExit(const vector<Value*>& valueVec,
159                     BasicBlock* bb,
160                     Module* module,
161                     unsigned int indent,
162                     bool isMethodExit)
163 {
164   // Get an iterator to point to the insertion location
165   // 
166   BasicBlock::InstListType& instList = bb->getInstList();
167   TerminatorInst* termInst = bb->getTerminator(); 
168   BasicBlock::InstListType::iterator here = instList.end()-1;
169   assert((*here)->isTerminator());
170   
171   // Insert a print instruction for each value.
172   // 
173   for (unsigned i=0, N=valueVec.size(); i < N; i++)
174     {
175       Instruction* traceInstr =
176         CreatePrintInstr(valueVec[i], bb, module, indent, isMethodExit);
177       here = instList.insert(here, traceInstr);
178     }
179 }
180
181 static void
182 InsertCodeToShowMethodEntry(BasicBlock* entryBB)
183 {
184 }
185
186 static void
187 InsertCodeToShowMethodExit(BasicBlock* exitBB)
188 {
189 }
190
191
192 bool InsertTraceCode::doInsertTraceCode(Method *M, bool traceBasicBlockExits,
193                                         bool traceMethodExits) {
194   vector<Value*> valuesToTraceInMethod;
195   Module* module = M->getParent();
196   BasicBlock* exitBB = NULL;
197   
198   if (M->isExternal() ||
199       (! traceBasicBlockExits && ! traceMethodExits))
200     return false;
201
202   if (traceMethodExits) {
203     InsertCodeToShowMethodEntry(M->getEntryNode());
204     exitBB = M->getBasicBlocks().front(); //getExitNode();
205   }
206
207   for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) {
208     BasicBlock* bb = *BI;
209
210     vector<Value*> valuesToTraceInBB;
211     FindValuesToTraceInBB(bb, valuesToTraceInBB);
212
213     if (traceBasicBlockExits && bb != exitBB)
214       TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
215                           /*indent*/ 4, /*isMethodExit*/ false);
216
217     if (traceMethodExits) {
218       valuesToTraceInMethod.insert(valuesToTraceInMethod.end(),
219                                    valuesToTraceInBB.begin(),
220                                    valuesToTraceInBB.end());
221     }
222   }
223   
224   if (traceMethodExits) {
225     TraceValuesAtBBExit(valuesToTraceInMethod, exitBB, module,
226                         /*indent*/ 0, /*isMethodExit*/ true);
227     InsertCodeToShowMethodExit(exitBB);
228   }
229   return true;
230 }