know if individual LLVM's Instruction has a result, and how to obtain it?

Most LLVM IR instructions have a result field, according to the Language Reference.

I want to know, for all LLVM Instructions, is there an easy and consistent way to know if the current Inst has a result field?
And if yes, what is the best way to obtain it?

E.g.:

  <result> = add <ty> <op1>, <op2>          <i>; yields {ty}:result

</i>

All ADD instruction will have a result field, regardless of whether its result is used.

I checked the source code for Instruction.h/.cpp, getOperand(int) is for obtaining the operands only. There is no dedicated method to obtain the result.
{I was expecting something like Instruction->getResult().}

Instruction * Inst;

Value * Res = Inst;

Is the above the right way to do the work?

For an other example:

 <result> = [tail] call [[cconv](http://llvm.org/docs/LangRef.html#callingconv)] [[ret attrs](http://llvm.org/docs/LangRef.html#paramattrs)] <ty> [<fnty>*] <fnptrval>(<function args>) [[fn attrs](http://llvm.org/docs/LangRef.html#fnattrs)]

According to the LLVM Language reference, CallInst should always have a result.
But for void bar(int) types, there will be no return. So the generated IR will be similar to:

  call void @bar(i32 %2) nounwind

instead of

  %3 = call void @bar(i32 %2) nounwind

How can I handle this or similar case?

Are there special handling for certain Instructions? (E.g. LoadInst, StoreInst, etc.)

Thank you very much

Chuck

Hi Chuck,

Most LLVM IR instructions have a result field, according to the Language Reference.

I want to know, for all LLVM Instructions, is there an easy and consistent way
to know if the current Inst has a result field?
And if yes, what is the best way to obtain it?

the instruction is the result! So everywhere you though you needed to use
Instruction->getResult(), just use Instruction. Thus your question about
whether there is a result field doesn't make sense. The closest thing is
probably: the instruction does not have void type.

Ciao, Duncan.

Duncan,

I think your answer.
I think also makes sense for void return type function calls, as its result(s) will never be used.

Thank you again

Chuck

Duncan,

Thank you for the answer.
I think it also makes sense for void return type function calls, as its result(s) will never be used.

Chuck