aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/Instrumentation/TraceValues.cpp
blob: d4d99efeae8c88418b7aa1cde807612cd9c52fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// $Id$
//***************************************************************************
// File:
//	TraceValues.cpp
// 
// Purpose:
//      Support for inserting LLVM code to print values at basic block
//      and method exits.  Also exports functions to create a call
//      "printf" instruction with one of the signatures listed below.
// 
// History:
//	10/11/01	 -  Vikram Adve  -  Created
//**************************************************************************/


#include "llvm/Transforms/Instrumentation/TraceValues.h"
#include "llvm/GlobalVariable.h"
#include "llvm/ConstPoolVals.h"
#include "llvm/Type.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instruction.h"
#include "llvm/iTerminators.h"
#include "llvm/iOther.h"
#include "llvm/BasicBlock.h"
#include "llvm/Method.h"
#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include <strstream>
#include "llvm/Assembly/Writer.h"


const char* const PRINTF = "printVal";

static inline GlobalVariable *
GetStringRef(Module *M, const string &str)
{
  ConstPoolArray *Init = ConstPoolArray::get(str);
  GlobalVariable *V = new GlobalVariable(Init->getType(), /*Const*/true, Init);
  M->getGlobalList().push_back(V);

  return V;
}

static inline bool
TraceThisOpCode(unsigned opCode)
{
  // Explicitly test for opCodes *not* to trace so that any new opcodes will
  // be traced by default (VoidTy's are already excluded)
  // 
  return (opCode  < Instruction::FirstOtherOp &&
          opCode != Instruction::Alloca &&
          opCode != Instruction::PHINode &&
          opCode != Instruction::Cast);
}


static void 
FindValuesToTraceInBB(BasicBlock* bb, vector<Value*>& valuesToTraceInBB)
{
  for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II)
    if ((*II)->getType()->isPrimitiveType() && 
        (*II)->getType() != Type::VoidTy &&
        TraceThisOpCode((*II)->getOpcode()))
      {
        valuesToTraceInBB.push_back(*II);
      }
}

// The invocation should be:
//       call "printf"(fmt, value).
// 
static Value *GetPrintMethodForType(Module *Mod, const Type *valueType) {
  vector<const Type*> ArgTys;
  ArgTys.reserve(2);
  ArgTys.push_back(PointerType::get(ArrayType::get(Type::UByteTy)));
  ArgTys.push_back(valueType);
  
  MethodType *printMethodTy = MethodType::get(Type::VoidTy, ArgTys,
                                              /*isVarArg*/ false);
  
  SymbolTable *ST = Mod->getSymbolTableSure();
  if (Value *V = ST->lookup(PointerType::get(printMethodTy), PRINTF))
    return V;

  // Create a new method and add it to the module
  Method *M = new Method(printMethodTy, PRINTF);
  Mod->getMethodList().push_back(M);
  return M;
}


static Instruction*
CreatePrintInstr(Value* val,
                 const BasicBlock* bb,
                 Module* module,
                 unsigned int indent,
                 bool isMethodExit)
{
  strstream scopeNameString;
  const Type* valueType = val->getType();
  
  assert(valueType->isPrimitiveType() &&
         valueType->getPrimitiveID() != Type::VoidTyID &&
         valueType->getPrimitiveID() != Type::TypeTyID &&
         valueType->getPrimitiveID() != Type::LabelTyID && 
         "Unsupported type for printing");
  
  const Value* scopeToUse = (isMethodExit)? (const Value*) bb->getParent()
                                          : (const Value*) bb;
  WriteAsOperand(scopeNameString, scopeToUse) << " : ";
  WriteAsOperand(scopeNameString, val) << " = "
                                       << val->getType()->getDescription()
                                       << ends;
  string fmtString(indent, ' ');
  
  fmtString += " At exit of " + string(isMethodExit ? "Method " : "BB ") +
    scopeNameString.str();
  
  switch(valueType->getPrimitiveID()) {
  case Type::BoolTyID:
  case Type::UByteTyID: case Type::UShortTyID:
  case Type::UIntTyID:  case Type::ULongTyID:
  case Type::SByteTyID: case Type::ShortTyID:
  case Type::IntTyID:   case Type::LongTyID:
    fmtString += " %d\0A";
    break;
    
  case Type::FloatTyID:     case Type::DoubleTyID:
    fmtString += " %g\0A";
    break;
    
  case Type::PointerTyID:
    fmtString += " %p\0A";
    break;
    
  default:
    assert(0 && "Should not get here.  Check the IF expression above");
    return NULL;
  }
  
  GlobalVariable *fmtVal = GetStringRef(module, fmtString);
  
  vector<Value*> paramList;
  paramList.push_back(fmtVal);
  paramList.push_back(val);

  return new CallInst(GetPrintMethodForType(module, valueType), paramList);
}



// 
// Insert print instructions at the end of the basic block *bb
// for each value in valueVec[].  *bb must postdominate the block
// in which the value is computed; this is not checked here.
// 
static void
TraceValuesAtBBExit(const vector<Value*>& valueVec,
                    BasicBlock* bb,
                    Module* module,
                    unsigned int indent,
                    bool isMethodExit)
{
  // Get an iterator to point to the insertion location
  // 
  BasicBlock::InstListType& instList = bb->getInstList();
  TerminatorInst* termInst = bb->getTerminator(); 
  BasicBlock::InstListType::iterator here = instList.end()-1;
  assert((*here)->isTerminator());
  
  // Insert a print instruction for each value.
  // 
  for (unsigned i=0, N=valueVec.size(); i < N; i++)
    {
      Instruction* traceInstr =
        CreatePrintInstr(valueVec[i], bb, module, indent, isMethodExit);
      here = instList.insert(here, traceInstr);
    }
}

static void
InsertCodeToShowMethodEntry(BasicBlock* entryBB)
{
}

static void
InsertCodeToShowMethodExit(BasicBlock* exitBB)
{
}


bool InsertTraceCode::doInsertTraceCode(Method *M, bool traceBasicBlockExits,
                                        bool traceMethodExits) {
  vector<Value*> valuesToTraceInMethod;
  Module* module = M->getParent();
  BasicBlock* exitBB = NULL;
  
  if (M->isExternal() ||
      (! traceBasicBlockExits && ! traceMethodExits))
    return false;

  if (traceMethodExits) {
    InsertCodeToShowMethodEntry(M->getEntryNode());
    exitBB = M->getBasicBlocks().front(); //getExitNode();
  }

  for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) {
    BasicBlock* bb = *BI;

    vector<Value*> valuesToTraceInBB;
    FindValuesToTraceInBB(bb, valuesToTraceInBB);

    if (traceBasicBlockExits && bb != exitBB)
      TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
                          /*indent*/ 4, /*isMethodExit*/ false);

    if (traceMethodExits) {
      valuesToTraceInMethod.insert(valuesToTraceInMethod.end(),
                                   valuesToTraceInBB.begin(),
                                   valuesToTraceInBB.end());
    }
  }
  
  if (traceMethodExits) {
    TraceValuesAtBBExit(valuesToTraceInMethod, exitBB, module,
                        /*indent*/ 0, /*isMethodExit*/ true);
    InsertCodeToShowMethodExit(exitBB);
  }
  return true;
}