2 //***************************************************************************
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.
12 // 10/11/01 - Vikram Adve - Created
13 //**************************************************************************/
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"
29 #include "llvm/Assembly/Writer.h"
32 const char* const PRINTF = "printVal";
34 static inline GlobalVariable *
35 GetStringRef(Module *M, const string &str)
37 ConstPoolArray *Init = ConstPoolArray::get(str);
38 GlobalVariable *V = new GlobalVariable(Init->getType(), /*Const*/true, Init);
39 M->getGlobalList().push_back(V);
45 TraceThisOpCode(unsigned opCode)
47 // Explicitly test for opCodes *not* to trace so that any new opcodes will
48 // be traced by default (VoidTy's are already excluded)
50 return (opCode < Instruction::FirstOtherOp &&
51 opCode != Instruction::Alloca &&
52 opCode != Instruction::PHINode &&
53 opCode != Instruction::Cast);
58 FindValuesToTraceInBB(BasicBlock* bb, vector<Value*>& valuesToTraceInBB)
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()))
65 valuesToTraceInBB.push_back(*II);
69 // The invocation should be:
70 // call "printf"(fmt, value).
72 static Value *GetPrintMethodForType(Module *Mod, const Type *valueType) {
73 vector<const Type*> ArgTys;
75 ArgTys.push_back(PointerType::get(ArrayType::get(Type::UByteTy)));
76 ArgTys.push_back(valueType);
78 MethodType *printMethodTy = MethodType::get(Type::VoidTy, ArgTys,
81 SymbolTable *ST = Mod->getSymbolTableSure();
82 if (Value *V = ST->lookup(PointerType::get(printMethodTy), PRINTF))
85 // Create a new method and add it to the module
86 Method *M = new Method(printMethodTy, PRINTF);
87 Mod->getMethodList().push_back(M);
93 CreatePrintInstr(Value* val,
99 strstream scopeNameString;
100 const Type* valueType = val->getType();
102 assert(valueType->isPrimitiveType() &&
103 valueType->getPrimitiveID() != Type::VoidTyID &&
104 valueType->getPrimitiveID() != Type::TypeTyID &&
105 valueType->getPrimitiveID() != Type::LabelTyID &&
106 "Unsupported type for printing");
108 const Value* scopeToUse = (isMethodExit)? (const Value*) bb->getParent()
110 WriteAsOperand(scopeNameString, scopeToUse) << " : ";
111 WriteAsOperand(scopeNameString, val) << " = "
112 << val->getType()->getDescription()
114 string fmtString(indent, ' ');
116 fmtString += " At exit of " + string(isMethodExit ? "Method " : "BB ") +
117 scopeNameString.str();
119 switch(valueType->getPrimitiveID()) {
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";
128 case Type::FloatTyID: case Type::DoubleTyID:
129 fmtString += " %g\0A";
132 case Type::PointerTyID:
133 fmtString += " %p\0A";
137 assert(0 && "Should not get here. Check the IF expression above");
141 GlobalVariable *fmtVal = GetStringRef(module, fmtString);
143 vector<Value*> paramList;
144 paramList.push_back(fmtVal);
145 paramList.push_back(val);
147 return new CallInst(GetPrintMethodForType(module, valueType), paramList);
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.
158 TraceValuesAtBBExit(const vector<Value*>& valueVec,
164 // Get an iterator to point to the insertion location
166 BasicBlock::InstListType& instList = bb->getInstList();
167 TerminatorInst* termInst = bb->getTerminator();
168 BasicBlock::InstListType::iterator here = instList.end()-1;
169 assert((*here)->isTerminator());
171 // Insert a print instruction for each value.
173 for (unsigned i=0, N=valueVec.size(); i < N; i++)
175 Instruction* traceInstr =
176 CreatePrintInstr(valueVec[i], bb, module, indent, isMethodExit);
177 here = instList.insert(here, traceInstr);
182 InsertCodeToShowMethodEntry(BasicBlock* entryBB)
187 InsertCodeToShowMethodExit(BasicBlock* exitBB)
192 bool InsertTraceCode::doInsertTraceCode(Method *M, bool traceBasicBlockExits,
193 bool traceMethodExits) {
194 vector<Value*> valuesToTraceInMethod;
195 Module* module = M->getParent();
196 BasicBlock* exitBB = NULL;
198 if (M->isExternal() ||
199 (! traceBasicBlockExits && ! traceMethodExits))
202 if (traceMethodExits) {
203 InsertCodeToShowMethodEntry(M->getEntryNode());
204 exitBB = M->getBasicBlocks().front(); //getExitNode();
207 for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) {
208 BasicBlock* bb = *BI;
210 vector<Value*> valuesToTraceInBB;
211 FindValuesToTraceInBB(bb, valuesToTraceInBB);
213 if (traceBasicBlockExits && bb != exitBB)
214 TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
215 /*indent*/ 4, /*isMethodExit*/ false);
217 if (traceMethodExits) {
218 valuesToTraceInMethod.insert(valuesToTraceInMethod.end(),
219 valuesToTraceInBB.begin(),
220 valuesToTraceInBB.end());
224 if (traceMethodExits) {
225 TraceValuesAtBBExit(valuesToTraceInMethod, exitBB, module,
226 /*indent*/ 0, /*isMethodExit*/ true);
227 InsertCodeToShowMethodExit(exitBB);