[LLVMdev] Inserting trace information during opt transformations

Chris Lattner sabre at nondot.org
Sun Aug 19 15:14:09 PDT 2007


On Aug 17, 2007, at 3:50 PM, Sarah Thompson wrote:

> Hi all,
>
> In several of the transformations I'm working on I need to embed the
> logical equivalent of puts/printf/etc. calls inline with code. I am
> having some difficulties getting the transformation code right. As I
> understand it, I should be creating a ConstantArray based on the  
> string
> (in this case I'm taking an instruction, 'disassembling' it to an
> ostringstream then turning it into a constant array):
>
> 	std::ostringstream instr;
> 	(*wp)->print(instr);
> 	Constant *itext = ConstantArray::get(instr.str());
>
> This bit seems OK. I am calling a function in my run time system with
> the signature
>
>     void mcp_trace_instruction(const char *)
>
> by first declaring it inside doInitialization():
>
> 	virtual bool doInitialization(Module &M)
> 	{
> 		std::vector<const Type*> args;
> 		args.push_back(PointerType::get(Type::Int8Ty));
> 		
> 		FunctionType *FT = FunctionType::get(Type::VoidTy, args, false);
> 		TraceFunc = M.getOrInsertFunction("mcp_trace_instruction", FT);
>
> 		return true;		
> 	}
>
> then inserting a suitable CallInst instruction
>
> 	new CallInst(TraceFunc, gepinst, "", *wp);
>
>
> Looking at disassembly output from similar code, I can't just pass the
> constant array directly to the call because I need a GetElementPtrInst
> conversion. This is the thing I'm having trouble with. My current
> attempt is:
>
>             GetElementPtrInst *gepinst = new GetElementPtrInst(itext,
>             	ConstantInt::get(Type::Int32Ty, 0),
>             	ConstantInt::get(Type::Int32Ty, 0),
>             	"", *wp);

Right.  The problem here is that you need to create a GlobalVariable  
to hold the constant array.  Something like this should work:

itext = new GlobalVariable(itext->getType(), true,  
GlobalValue::InternalLinkage, itext, "", &M);

-Chris



More information about the llvm-dev mailing list