mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-19 11:02:59 +02:00
Use separate functions for printing values of each type.
Put trace code before condition-generating instruction in basic blocks that end in a conditional branch. llvm-svn: 1002
This commit is contained in:
parent
0f4d41632f
commit
767c1505bc
@ -27,8 +27,47 @@
|
|||||||
#include "llvm/Module.h"
|
#include "llvm/Module.h"
|
||||||
#include "llvm/SymbolTable.h"
|
#include "llvm/SymbolTable.h"
|
||||||
#include "llvm/Assembly/Writer.h"
|
#include "llvm/Assembly/Writer.h"
|
||||||
|
#include "llvm/Support/HashExtras.h"
|
||||||
|
#include <hash_set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
const string PRINT_FUNC_NAME = "printVal";
|
||||||
|
|
||||||
|
static const char*
|
||||||
|
PrintMethodNameForType(const Type* type)
|
||||||
|
{
|
||||||
|
if (PointerType* pty = dyn_cast<PointerType>(type))
|
||||||
|
{
|
||||||
|
const Type* elemTy;
|
||||||
|
if (ArrayType* aty = dyn_cast<ArrayType>(pty->getValueType()))
|
||||||
|
elemTy = aty->getElementType();
|
||||||
|
else
|
||||||
|
elemTy = pty->getValueType();
|
||||||
|
if (elemTy == Type::SByteTy || elemTy == Type::UByteTy)
|
||||||
|
return "printString";
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (type->getPrimitiveID())
|
||||||
|
{
|
||||||
|
case Type::BoolTyID: return "printBool";
|
||||||
|
case Type::UByteTyID: return "printUByte";
|
||||||
|
case Type::SByteTyID: return "printSByte";
|
||||||
|
case Type::UShortTyID: return "printUShort";
|
||||||
|
case Type::ShortTyID: return "printShort";
|
||||||
|
case Type::UIntTyID: return "printUInt";
|
||||||
|
case Type::IntTyID: return "printInt";
|
||||||
|
case Type::ULongTyID: return "printULong";
|
||||||
|
case Type::LongTyID: return "printLong";
|
||||||
|
case Type::FloatTyID: return "printFloat";
|
||||||
|
case Type::DoubleTyID: return "printDouble";
|
||||||
|
case Type::PointerTyID: return "printPointer";
|
||||||
|
case Type::MethodTyID: return "printPointer";
|
||||||
|
default:
|
||||||
|
assert(0 && "Unsupported type for printing");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static inline GlobalVariable *GetStringRef(Module *M, const string &str) {
|
static inline GlobalVariable *GetStringRef(Module *M, const string &str) {
|
||||||
ConstPoolArray *Init = ConstPoolArray::get(str);
|
ConstPoolArray *Init = ConstPoolArray::get(str);
|
||||||
GlobalVariable *GV = new GlobalVariable(Init->getType(), /*Const*/true, Init);
|
GlobalVariable *GV = new GlobalVariable(Init->getType(), /*Const*/true, Init);
|
||||||
@ -50,7 +89,8 @@ TraceThisOpCode(unsigned opCode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check if this instruction has any uses outside its basic block
|
// Check if this instruction has any uses outside its basic block,
|
||||||
|
// or if it used by either a Call or Return instruction.
|
||||||
//
|
//
|
||||||
static inline bool
|
static inline bool
|
||||||
LiveAtBBExit(Instruction* I)
|
LiveAtBBExit(Instruction* I)
|
||||||
@ -60,10 +100,12 @@ LiveAtBBExit(Instruction* I)
|
|||||||
for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
|
for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
|
||||||
{
|
{
|
||||||
const Instruction* userI = dyn_cast<Instruction>(*U);
|
const Instruction* userI = dyn_cast<Instruction>(*U);
|
||||||
if (userI == NULL || userI->getParent() != bb)
|
if (userI == NULL
|
||||||
|
|| userI->getParent() != bb
|
||||||
|
|| userI->getOpcode() == Instruction::Call
|
||||||
|
|| userI->getOpcode() == Instruction::Ret)
|
||||||
isLive = true;
|
isLive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isLive;
|
return isLive;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +124,6 @@ FindValuesToTraceInBB(BasicBlock* bb, vector<Instruction*>& valuesToTraceInBB)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0 // Code is disabled for now
|
|
||||||
//
|
//
|
||||||
// Let's save this code for future use; it has been tested and works:
|
// Let's save this code for future use; it has been tested and works:
|
||||||
//
|
//
|
||||||
@ -128,7 +169,7 @@ CreatePrintfInstr(Value* val,
|
|||||||
ostringstream fmtString, scopeNameString, valNameString;
|
ostringstream fmtString, scopeNameString, valNameString;
|
||||||
vector<Value*> paramList;
|
vector<Value*> paramList;
|
||||||
const Type* valueType = val->getType();
|
const Type* valueType = val->getType();
|
||||||
Method* printMethod = GetPrintfMethodForType(module, valueType);
|
Method* printMethod = cast<Method>(GetPrintfMethodForType(module,valueType));
|
||||||
|
|
||||||
if (! valueType->isPrimitiveType() ||
|
if (! valueType->isPrimitiveType() ||
|
||||||
valueType->getPrimitiveID() == Type::VoidTyID ||
|
valueType->getPrimitiveID() == Type::VoidTyID ||
|
||||||
@ -170,15 +211,15 @@ CreatePrintfInstr(Value* val,
|
|||||||
case Type::UIntTyID: case Type::ULongTyID:
|
case Type::UIntTyID: case Type::ULongTyID:
|
||||||
case Type::SByteTyID: case Type::ShortTyID:
|
case Type::SByteTyID: case Type::ShortTyID:
|
||||||
case Type::IntTyID: case Type::LongTyID:
|
case Type::IntTyID: case Type::LongTyID:
|
||||||
fmtString << " %d\0A";
|
fmtString << " %d\\n";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Type::FloatTyID: case Type::DoubleTyID:
|
case Type::FloatTyID: case Type::DoubleTyID:
|
||||||
fmtString << " %g\0A";
|
fmtString << " %g\\n";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Type::PointerTyID:
|
case Type::PointerTyID:
|
||||||
fmtString << " %p\0A";
|
fmtString << " %p\\n";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -197,22 +238,24 @@ CreatePrintfInstr(Value* val,
|
|||||||
|
|
||||||
return new CallInst(printMethod, paramList);
|
return new CallInst(printMethod, paramList);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
// The invocation should be:
|
// The invocation should be:
|
||||||
// call "printVal"(value).
|
// call "printString"([ubyte*] or [sbyte*] or ubyte* or sbyte*).
|
||||||
|
// call "printLong"(long)
|
||||||
|
// call "printInt"(int) ...
|
||||||
//
|
//
|
||||||
static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) {
|
static Value *GetPrintMethodForType(Module *Mod, const Type *VTy) {
|
||||||
MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy),
|
MethodType *MTy = MethodType::get(Type::VoidTy, vector<const Type*>(1, VTy),
|
||||||
/*isVarArg*/ false);
|
/*isVarArg*/ false);
|
||||||
|
|
||||||
|
const char* printMethodName = PrintMethodNameForType(VTy);
|
||||||
SymbolTable *ST = Mod->getSymbolTableSure();
|
SymbolTable *ST = Mod->getSymbolTableSure();
|
||||||
if (Value *V = ST->lookup(PointerType::get(MTy), "printVal"))
|
if (Value *V = ST->lookup(PointerType::get(MTy), printMethodName))
|
||||||
return V;
|
return V;
|
||||||
|
|
||||||
// Create a new method and add it to the module
|
// Create a new method and add it to the module
|
||||||
Method *M = new Method(MTy, "printVal");
|
Method *M = new Method(MTy, printMethodName);
|
||||||
Mod->getMethodList().push_back(M);
|
Mod->getMethodList().push_back(M);
|
||||||
return M;
|
return M;
|
||||||
}
|
}
|
||||||
@ -240,9 +283,9 @@ InsertPrintInsts(Value *Val,
|
|||||||
// Create the marker string...
|
// Create the marker string...
|
||||||
ostringstream scopeNameString;
|
ostringstream scopeNameString;
|
||||||
WriteAsOperand(scopeNameString, scopeToUse) << " : ";
|
WriteAsOperand(scopeNameString, scopeToUse) << " : ";
|
||||||
WriteAsOperand(scopeNameString, Val) << " = " << ends;
|
WriteAsOperand(scopeNameString, Val) << " = ";
|
||||||
string fmtString(indent, ' ');
|
|
||||||
|
|
||||||
|
string fmtString(indent, ' ');
|
||||||
fmtString += string(" At exit of") + scopeNameString.str();
|
fmtString += string(" At exit of") + scopeNameString.str();
|
||||||
|
|
||||||
// Turn the marker string into a global variable...
|
// Turn the marker string into a global variable...
|
||||||
@ -259,7 +302,7 @@ InsertPrintInsts(Value *Val,
|
|||||||
BBI = BB->getInstList().insert(BBI, I)+1;
|
BBI = BB->getInstList().insert(BBI, I)+1;
|
||||||
|
|
||||||
// Print out a newline
|
// Print out a newline
|
||||||
fmtVal = GetStringRef(Mod, "\n");
|
fmtVal = GetStringRef(Mod, "\\n");
|
||||||
I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
|
I = new CallInst(GetPrintMethodForType(Mod, fmtVal->getType()),
|
||||||
vector<Value*>(1, fmtVal));
|
vector<Value*>(1, fmtVal));
|
||||||
BBI = BB->getInstList().insert(BBI, I)+1;
|
BBI = BB->getInstList().insert(BBI, I)+1;
|
||||||
@ -298,12 +341,34 @@ TraceValuesAtBBExit(const vector<Instruction*>& valueVec,
|
|||||||
bool isMethodExit,
|
bool isMethodExit,
|
||||||
vector<Instruction*>* valuesStoredInMethod)
|
vector<Instruction*>* valuesStoredInMethod)
|
||||||
{
|
{
|
||||||
// Get an iterator to point to the insertion location
|
// Get an iterator to point to the insertion location, which is
|
||||||
|
// just before the terminator instruction.
|
||||||
//
|
//
|
||||||
BasicBlock::InstListType& instList = bb->getInstList();
|
BasicBlock::InstListType& instList = bb->getInstList();
|
||||||
BasicBlock::iterator here = instList.end()-1;
|
BasicBlock::iterator here = instList.end()-1;
|
||||||
assert((*here)->isTerminator());
|
assert((*here)->isTerminator());
|
||||||
|
|
||||||
|
// If the terminator is a conditional branch, insert the trace code just
|
||||||
|
// before the instruction that computes the branch condition (just to
|
||||||
|
// avoid putting a call between the CC-setting instruction and the branch).
|
||||||
|
// Use laterInstrSet to mark instructions that come after the setCC instr
|
||||||
|
// because those cannot be traced at the location we choose.
|
||||||
|
//
|
||||||
|
hash_set<Instruction*> laterInstrSet;
|
||||||
|
if (BranchInst* brInst = dyn_cast<BranchInst>(*here))
|
||||||
|
if (! brInst->isUnconditional())
|
||||||
|
if (Instruction* setCC = dyn_cast<Instruction>(brInst->getCondition()))
|
||||||
|
if (setCC->getParent() == bb)
|
||||||
|
{
|
||||||
|
while ((*here) != setCC && here != instList.begin())
|
||||||
|
{
|
||||||
|
--here;
|
||||||
|
laterInstrSet.insert(*here);
|
||||||
|
}
|
||||||
|
assert((*here) == setCC && "Missed the setCC instruction?");
|
||||||
|
laterInstrSet.insert(*here);
|
||||||
|
}
|
||||||
|
|
||||||
// Insert a print instruction for each value.
|
// Insert a print instruction for each value.
|
||||||
//
|
//
|
||||||
for (unsigned i=0, N=valueVec.size(); i < N; i++)
|
for (unsigned i=0, N=valueVec.size(); i < N; i++)
|
||||||
@ -316,7 +381,8 @@ TraceValuesAtBBExit(const vector<Instruction*>& valueVec,
|
|||||||
I = InsertLoadInst((StoreInst*) I, bb, here);
|
I = InsertLoadInst((StoreInst*) I, bb, here);
|
||||||
valuesStoredInMethod->push_back(I);
|
valuesStoredInMethod->push_back(I);
|
||||||
}
|
}
|
||||||
InsertPrintInsts(I, bb, here, module, indent, isMethodExit);
|
if (laterInstrSet.find(I) == laterInstrSet.end())
|
||||||
|
InsertPrintInsts(I, bb, here, module, indent, isMethodExit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,8 +395,8 @@ CreateMethodTraceInst(Method* method,
|
|||||||
{
|
{
|
||||||
string fmtString(indent, ' ');
|
string fmtString(indent, ' ');
|
||||||
ostringstream methodNameString;
|
ostringstream methodNameString;
|
||||||
WriteAsOperand(methodNameString, method) << ends;
|
WriteAsOperand(methodNameString, method);
|
||||||
fmtString += msg + methodNameString.str();
|
fmtString += msg + methodNameString.str() + "\\n";
|
||||||
|
|
||||||
GlobalVariable *fmtVal = GetStringRef(method->getParent(), fmtString);
|
GlobalVariable *fmtVal = GetStringRef(method->getParent(), fmtString);
|
||||||
Instruction *printInst =
|
Instruction *printInst =
|
||||||
@ -387,9 +453,10 @@ InsertTraceCode::doInsertTraceCode(Method *M,
|
|||||||
vector<BasicBlock*> exitBlocks;
|
vector<BasicBlock*> exitBlocks;
|
||||||
|
|
||||||
if (M->isExternal() ||
|
if (M->isExternal() ||
|
||||||
|
(M->hasName() && M->getName() == PRINT_FUNC_NAME) ||
|
||||||
(! traceBasicBlockExits && ! traceMethodExits))
|
(! traceBasicBlockExits && ! traceMethodExits))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (traceMethodExits)
|
if (traceMethodExits)
|
||||||
InsertCodeToShowMethodEntry(M, M->getEntryNode(), /*indent*/ 0);
|
InsertCodeToShowMethodEntry(M, M->getEntryNode(), /*indent*/ 0);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user