How do I get the result of an instruction?

Hi all,

I probably have a stupid question but I could not find out so please help me out here. I can use getOperand to get the operands of an instruction. Similarly, How do I get the result of an instruction?

Thanks,
Bhavani

bhavani krishnan <bhavi63@yahoo.com> writes:

I probably have a stupid question but I could not find out so please
help me out here. I can use getOperand to get the operands of an
instruction. Similarly, How do I get the result of an instruction?

The result of the instruction is the instruction itself. Whenever you
need to use the result of the instruction, pass the instruction as an
argument.

bhavani krishnan wrote:

Hi all,

I probably have a stupid question but I could not find out so please help me out here. I can use getOperand to get the operands of an instruction. Similarly, How do I get the result of an instruction?
  

The instruction itself is its result.

For example, if I wanted to generate code that does a getlemeneptr instruction (GEP) and then uses the result of the GEP as the pointer to a load instruction, I would do the following:

Instruction * GEP = new GetElementPtrInst (Value, ....);
Instruction * LI = new LoadInst (GEP, "name", ...);

-- John T.

Thanks!

How do I cast the result to a string? I basically need to store the results of some instructions in a file.

Thanks,
Bhavani

If you're confused about how the IR works, http://llvm.org/demo/ can
be useful. The question you're asking doesn't really seem to make
sense.

-Eli

Hi,

I am writing an optimization pass where I need to instrument the code such that I need to store the results of some instructions in file. Using llc -march=cpp option I figured out how to add a function(say writeToFile) which takes char* parameter and writes to file. Now, I need put in a CallInst which calls writeToFile passing the Instruction result as parameter. How do I do this?

So, in my optimization pass...
Func *myprint = makewriteToFile() //creates a function which writes to file
for (Function::iterator i = func->begin(), e = func->end(); i != e; ++i)
{
        blk=i;
        for (BasicBlock::iterator j = blk->begin(), k = blk->end(); j != k; ++j){
            Instruction *inst = j;
            //if inst satisfies my condition, write reults to file
            CallInst *CallPrint = CallInst::Create(myprint, ???, "", j);
            CallPrint->setTailCall(true);
       }
}
What do I put in the ???. How do I cast Instruction *inst into char * which can be passed into the function?

Hope this is clear.
Thanks,
Bhavani

Well, you can do something like the following:
define i32 @f() nounwind {
entry:
  %x = alloca i32
  %resulttoprint = call i32 (...)* @a() nounwind
;start instrumentation
  store i32 %resulttoprint, i32* %x, align 4
  %x1 = bitcast i32* %x to i8*
  call void @print(i8* %x1) nounwind
;end instrumentation
  ret i32 %resulttoprint
}

That said, you might need to do something that's aware of the type of
the result; printing a value in human-readable form requires calling
something like printf.

-Eli