Convert to be compatible with lli.
[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 static inline GlobalVariable *
32 GetStringRef(Module *M, const string &str)
33 {
34   ConstPoolArray *Init = ConstPoolArray::get(str);
35   GlobalVariable *V = new GlobalVariable(Init->getType(), /*Const*/true, Init);
36   M->getGlobalList().push_back(V);
37
38   return V;
39 }
40
41 static inline bool
42 TraceThisOpCode(unsigned opCode)
43 {
44   // Explicitly test for opCodes *not* to trace so that any new opcodes will
45   // be traced by default (VoidTy's are already excluded)
46   // 
47   return (opCode  < Instruction::FirstOtherOp &&
48           opCode != Instruction::Alloca &&
49           opCode != Instruction::PHINode &&
50           opCode != Instruction::Cast);
51 }
52
53
54 static void 
55 FindValuesToTraceInBB(BasicBlock* bb, vector<Value*>& valuesToTraceInBB)
56 {
57   for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II)
58     if ((*II)->getType()->isPrimitiveType() && 
59         (*II)->getType() != Type::VoidTy &&
60         TraceThisOpCode((*II)->getOpcode()))
61       {
62         valuesToTraceInBB.push_back(*II);
63       }
64 }
65
66 // The invocation should be:
67 //       call "printVal"(value).
68 // 
69 static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) {
70   MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy),
71                                     /*isVarArg*/ false);
72   
73   SymbolTable *ST = Mod->getSymbolTableSure();
74   if (Value *V = ST->lookup(PointerType::get(MTy), "printVal"))
75     return V;
76
77   // Create a new method and add it to the module
78   Method *M = new Method(MTy, "printVal");
79   Mod->getMethodList().push_back(M);
80   return M;
81 }
82
83
84 static void InsertPrintInsts(Value *Val,
85                              BasicBlock::iterator &BBI,
86                              Module *Mod,
87                              unsigned int indent,
88                              bool isMethodExit) {
89   const Type* ValTy = Val->getType();
90   BasicBlock *BB = (*BBI)->getParent();
91   
92   assert(ValTy->isPrimitiveType() &&
93          ValTy->getPrimitiveID() != Type::VoidTyID &&
94          ValTy->getPrimitiveID() != Type::TypeTyID &&
95          ValTy->getPrimitiveID() != Type::LabelTyID && 
96          "Unsupported type for printing");
97   
98   const Value* scopeToUse = 
99     isMethodExit ? (const Value*)BB->getParent() : (const Value*)BB;
100
101   // Create the marker string...
102   strstream scopeNameString;
103   WriteAsOperand(scopeNameString, scopeToUse) << " : ";
104   WriteAsOperand(scopeNameString, Val) << " = " << ends;
105   string fmtString(indent, ' ');
106   
107   fmtString += string(" At exit of") + scopeNameString.str();
108
109   // Turn the marker string into a global variable...
110   GlobalVariable *fmtVal = GetStringRef(Mod, fmtString);
111   
112   // Insert the first print instruction to print the string flag:
113   Instruction *I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
114                                 vector<Value*>(1, fmtVal));
115   BBI = BB->getInstList().insert(BBI, I)+1;
116
117   // Insert the next print instruction to print the value:
118   I = new CallInst(GetPrintMethodForType(Mod, ValTy),
119                    vector<Value*>(1, Val));
120   BBI = BB->getInstList().insert(BBI, I)+1;
121
122   // Print out a newline
123   fmtVal = GetStringRef(Mod, "\n");
124   I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
125                    vector<Value*>(1, fmtVal));
126   BBI = BB->getInstList().insert(BBI, I)+1;
127 }
128
129
130
131 // 
132 // Insert print instructions at the end of the basic block *bb
133 // for each value in valueVec[].  *bb must postdominate the block
134 // in which the value is computed; this is not checked here.
135 // 
136 static void
137 TraceValuesAtBBExit(const vector<Value*>& valueVec,
138                     BasicBlock* bb,
139                     Module* module,
140                     unsigned int indent,
141                     bool isMethodExit)
142 {
143   // Get an iterator to point to the insertion location
144   // 
145   BasicBlock::InstListType& instList = bb->getInstList();
146   TerminatorInst* termInst = bb->getTerminator(); 
147   BasicBlock::iterator here = instList.end()-1;
148   assert((*here)->isTerminator());
149   
150   // Insert a print instruction for each value.
151   // 
152   for (unsigned i=0, N=valueVec.size(); i < N; i++)
153     InsertPrintInsts(valueVec[i], here, module, indent, isMethodExit);
154 }
155
156 static void
157 InsertCodeToShowMethodEntry(BasicBlock* entryBB)
158 {
159 }
160
161 static void
162 InsertCodeToShowMethodExit(BasicBlock* exitBB)
163 {
164 }
165
166
167 bool InsertTraceCode::doInsertTraceCode(Method *M, bool traceBasicBlockExits,
168                                         bool traceMethodExits) {
169   vector<Value*> valuesToTraceInMethod;
170   Module* module = M->getParent();
171   BasicBlock* exitBB = NULL;
172   
173   if (M->isExternal() ||
174       (! traceBasicBlockExits && ! traceMethodExits))
175     return false;
176
177   if (traceMethodExits) {
178     InsertCodeToShowMethodEntry(M->getEntryNode());
179     exitBB = M->getBasicBlocks().front(); //getExitNode();
180   }
181
182   for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) {
183     BasicBlock* bb = *BI;
184
185     vector<Value*> valuesToTraceInBB;
186     FindValuesToTraceInBB(bb, valuesToTraceInBB);
187
188     if (traceBasicBlockExits && bb != exitBB)
189       TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
190                           /*indent*/ 4, /*isMethodExit*/ false);
191
192     if (traceMethodExits) {
193       valuesToTraceInMethod.insert(valuesToTraceInMethod.end(),
194                                    valuesToTraceInBB.begin(),
195                                    valuesToTraceInBB.end());
196     }
197   }
198
199 #if 0
200   // Disable this code until we have a proper exit node.
201   if (traceMethodExits) {
202     TraceValuesAtBBExit(valuesToTraceInMethod, exitBB, module,
203                         /*indent*/ 0, /*isMethodExit*/ true);
204     InsertCodeToShowMethodExit(exitBB);
205   }
206 #endif
207   return true;
208 }