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"
31 static inline GlobalVariable *
32 GetStringRef(Module *M, const string &str)
34 ConstPoolArray *Init = ConstPoolArray::get(str);
35 GlobalVariable *V = new GlobalVariable(Init->getType(), /*Const*/true, Init);
36 M->getGlobalList().push_back(V);
42 TraceThisOpCode(unsigned opCode)
44 // Explicitly test for opCodes *not* to trace so that any new opcodes will
45 // be traced by default (VoidTy's are already excluded)
47 return (opCode < Instruction::FirstOtherOp &&
48 opCode != Instruction::Alloca &&
49 opCode != Instruction::PHINode &&
50 opCode != Instruction::Cast);
55 FindValuesToTraceInBB(BasicBlock* bb, vector<Value*>& valuesToTraceInBB)
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()))
62 valuesToTraceInBB.push_back(*II);
66 // The invocation should be:
67 // call "printVal"(value).
69 static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) {
70 MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy),
73 SymbolTable *ST = Mod->getSymbolTableSure();
74 if (Value *V = ST->lookup(PointerType::get(MTy), "printVal"))
77 // Create a new method and add it to the module
78 Method *M = new Method(MTy, "printVal");
79 Mod->getMethodList().push_back(M);
84 static void InsertPrintInsts(Value *Val,
85 BasicBlock::iterator &BBI,
89 const Type* ValTy = Val->getType();
90 BasicBlock *BB = (*BBI)->getParent();
92 assert(ValTy->isPrimitiveType() &&
93 ValTy->getPrimitiveID() != Type::VoidTyID &&
94 ValTy->getPrimitiveID() != Type::TypeTyID &&
95 ValTy->getPrimitiveID() != Type::LabelTyID &&
96 "Unsupported type for printing");
98 const Value* scopeToUse =
99 isMethodExit ? (const Value*)BB->getParent() : (const Value*)BB;
101 // Create the marker string...
102 strstream scopeNameString;
103 WriteAsOperand(scopeNameString, scopeToUse) << " : ";
104 WriteAsOperand(scopeNameString, Val) << " = " << ends;
105 string fmtString(indent, ' ');
107 fmtString += string(" At exit of") + scopeNameString.str();
109 // Turn the marker string into a global variable...
110 GlobalVariable *fmtVal = GetStringRef(Mod, fmtString);
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;
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;
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;
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.
137 TraceValuesAtBBExit(const vector<Value*>& valueVec,
143 // Get an iterator to point to the insertion location
145 BasicBlock::InstListType& instList = bb->getInstList();
146 TerminatorInst* termInst = bb->getTerminator();
147 BasicBlock::iterator here = instList.end()-1;
148 assert((*here)->isTerminator());
150 // Insert a print instruction for each value.
152 for (unsigned i=0, N=valueVec.size(); i < N; i++)
153 InsertPrintInsts(valueVec[i], here, module, indent, isMethodExit);
157 InsertCodeToShowMethodEntry(BasicBlock* entryBB)
162 InsertCodeToShowMethodExit(BasicBlock* exitBB)
167 bool InsertTraceCode::doInsertTraceCode(Method *M, bool traceBasicBlockExits,
168 bool traceMethodExits) {
169 vector<Value*> valuesToTraceInMethod;
170 Module* module = M->getParent();
171 BasicBlock* exitBB = NULL;
173 if (M->isExternal() ||
174 (! traceBasicBlockExits && ! traceMethodExits))
177 if (traceMethodExits) {
178 InsertCodeToShowMethodEntry(M->getEntryNode());
179 exitBB = M->getBasicBlocks().front(); //getExitNode();
182 for (Method::iterator BI = M->begin(); BI != M->end(); ++BI) {
183 BasicBlock* bb = *BI;
185 vector<Value*> valuesToTraceInBB;
186 FindValuesToTraceInBB(bb, valuesToTraceInBB);
188 if (traceBasicBlockExits && bb != exitBB)
189 TraceValuesAtBBExit(valuesToTraceInBB, bb, module,
190 /*indent*/ 4, /*isMethodExit*/ false);
192 if (traceMethodExits) {
193 valuesToTraceInMethod.insert(valuesToTraceInMethod.end(),
194 valuesToTraceInBB.begin(),
195 valuesToTraceInBB.end());
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);