Printing Function Arguments

I am processing the LLVM instructions and right now I am at the 'call'
instruction.
For now I just want to print the argument type.

For example in the following:

  %0 = tail call i32 (...)* @__FFF (i32 8) nounwind; <i32> [#uses=1]

I need to get access to 'i32' and '8' separately.

I do:

CallInst *CI = dyn_cast<CallInst>(I);
Value *v = CI->getOperand(1)

I can get the type via v->getType() and that returns 'i32', which is good.

But I also need to get the value '8'.

I can get it via v->getValueID(), but in the documentation it says not to
use that function.

Any other ideas what is the proper way to access the value '8' ?

thanks

ivtm wrote:

I am processing the LLVM instructions and right now I am at the 'call'
instruction.
For now I just want to print the argument type.

For example in the following:

  %0 = tail call i32 (...)* @__FFF (i32 8) nounwind; <i32> [#uses=1]

I need to get access to 'i32' and '8' separately.

I do:

CallInst *CI = dyn_cast<CallInst>(I); Value *v = CI->getOperand(1)

I can get the type via v->getType() and that returns 'i32', which is good.

But I also need to get the value '8'.

I can get it via v->getValueID(), but in the documentation it says not to
use that function.

Heh. No, getValueID() returns '8' as the "ConstantIntVal" enum, not the actual argument number.

Any other ideas what is the proper way to access the value '8' ?

It's a ConstantInt, so cast it and retrieve the APInt then pull out the number. Here:

   unsigned val;
   if (ConstantInt *CI = dyn_cast<ConstantInt>(v)) {
     val = CI->getValue()->getSExtValue();
   }

Nick

Hi Nick,

Thanks, that seemed to work.

Nick Lewycky wrote:

Another question, I need to get the "%0" below in the:

%0 = tail call i32 (...)* @__FFF (i32 8) nounwind; <i32> [#uses=1]

that is, the return register.

I am wondering in general, where should I look in the llvm codebase for
parsing instructions ?

I am looking at existing passes and also the header files like Function.h,
etc to see what methods they have, but it takes a while to figure out the
basic methods...

ivtm wrote:

ivtm <martinaide1@yahoo.com> writes:

Another question, I need to get the "%0" below in the:

%0 = tail call i32 (...)* @__FFF (i32 8) nounwind; <i32> [#uses=1]

that is, the return register.

What information do you want, exactly?

In your example, %0 is the CallInst. So if you have

CallInst *ci = CallInst::Create(...

then use `ci' whenever you want to use %0.

I am wondering in general, where should I look in the llvm codebase for
parsing instructions ?

I am looking at existing passes and also the header files like Function.h,
etc to see what methods they have, but it takes a while to figure out the
basic methods...

This is a bit better than looking at header files:

http://llvm.org/doxygen/classes.html

ivtm wrote:

Another question, I need to get the "%0" below in the:

%0 = tail call i32 (...)* @__FFF (i32 8) nounwind; <i32> [#uses=1]

that is, the return register.

I am wondering in general, where should I look in the llvm codebase for
parsing instructions ?

The parser is in lib/AsmParser/.

Nick

Hey Oscar,

I want to extract information from the instruction.

Think writing a simple interpreter.

I already have the CallInst instance (described above in the message).

Via ci->getOperand(1) say I can get the 'i32 8' parameter and I can get the
'i32' and '8' separately as Nick described.

But I need to extract the %0 from the CallInst instance somehow. I am not
sure what the exact method to call is.

I need that btw, for every instruction that writes, e.g. loads, adds, muls,
etc...There has got to be a generic method for that.

I am looking at the existing LLVM passes to figure out how they extract
stuff, but since many are only computing the small relevant info that they
need, they do not always need to extract all the components of an
instruction. (I've looked at the interpreter too, but it is doing other
stuff).

Anyway..concretely, I need to extract the return register for an
instruction.

Óscar Fuentes wrote:

Anyway,

I need to parse some 20 LLVM instructions now and the Doxygen does not
really help me.

Is there some documents or file that one can look at on parsing various
instructions ?

thanks

ivtm wrote:

ivtm wrote:

Hey Oscar,

I want to extract information from the instruction.

Think writing a simple interpreter.

I already have the CallInst instance (described above in the message).

Via ci->getOperand(1) say I can get the 'i32 8' parameter and I can get the
'i32' and '8' separately as Nick described.

But I need to extract the %0 from the CallInst instance somehow. I am not
sure what the exact method to call is.

I need that btw, for every instruction that writes, e.g. loads, adds, muls,
etc...There has got to be a generic method for that.

This is a common misunderstanding. When you look at something like:

   %a = add i32 %x, %y

it's common to think "Well, I've got the Instruction* which is on the right hand side, how do I get the %a on the left hand side"? The answer is that the whole thing is actually:

   %I->getName() = I->getOpcode() I->op_begin()..I->op_end()

There is only the Instruction *I. It _is_ the value that it produces. There is no register it's being stored into, that Instruction* is itself the definition of the register!

Note that registers are immutable (being a static single assignment form) and that we have an infinite register set.

Nick

Hi Nick,

Perhaps I am confused. What is the best way to extract information from
instructions ?

Is it via the, say:

for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
    ....

I am not sure what happens next, e.g. to the variable 'i', you should know
what part of the instruction this is and cast it to the necessary type.

For example, I am parsing the 'inttoptr' instruction.

In that case the loop iterates only once, but then, I want to extract the
values
from which I am casting, _and_ the value to which I am casting. Somehow I
need to
extract the contents of the 'i'.

For that, there are functions like getSrcTy() and getDstTy()
I found them again by browsing the header files (which is not right :)...
say for the inttoptr instruction.

Btw, also the cout << I->getName() returns an empty string for some reason.
I tried I->getName().c_str(), but still the same.

Some simple example, say of parsing 'load' or 'add' would be great....

Also, I assume when you say registers are immutable, you mean within the
same instruction instance.
I can have a loop that updates the same register.

thanks for your help....I just need to parse successfully few instructions
and I will be done, but right now I am struggling.

Nick Lewycky wrote:

Hi Nick,

I parsed your message again carefully and did some experiments.

I guess the:

for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
{
}

iterates over the operands of the instruction "I", which are as you said,
*other* instructions.

But if I want to get other information about the instruction, say the type
of the operands,
then I still need to figure out how to do that, e.g. via the I->getSrcTy()
methods for say CastInst's instructions.

My goal with this exercise was to write a small interpreter for the SSA IR,
so I still need to associate some state with all the Instructions, for
example, if I have a loop in the SSA, then the concrete values of the
virtual registers (represented as Instruction * here) can change.

Nick Lewycky wrote:

ivtm wrote:

Hi Nick,

I parsed your message again carefully and did some experiments.

I guess the:

for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
{
}

iterates over the operands of the instruction "I", which are as you said,
*other* instructions.

But if I want to get other information about the instruction, say the type
of the operands,
then I still need to figure out how to do that, e.g. via the I->getSrcTy()
methods for say CastInst's instructions.

Kinda? If the cast is from, say i8* to i32, "TheCastInst->getType()" returns i32 "and TheCastInst->getSrcTy()" will return i8*, that is to say, it will run "TheCastInst->getOperand(0)->getType()".

My goal with this exercise was to write a small interpreter for the SSA IR,
so I still need to associate some state with all the Instructions, for
example, if I have a loop in the SSA, then the concrete values of the
virtual registers (represented as Instruction * here) can change.

Given 'User::op_iterator i' you can state:

   Value *V = i;
   if (CastInst *CI = dyn_cast<CastInst>(V))
     ...use CI->getSrcTy()...

BasicBlock::iterator and Value::use_iterator work the same way, just turn the iterator into a Value* and use dyn_cast to cast your way up to find out what kind of value it really is.

To find out the type of any Value (including Instructions), you'd call the getType() method on it. Just in case you haven't seen the doxygen for it yet, this is where you start:

   http://llvm.org/doxygen/classllvm_1_1Value.html

It's got a clickable graph of every possible value type and each page has the complete listing of the methods you can call. If you're in a more specific class like "CastInst" it will only show you methods that were added in CastInst, not ones that it inherited from the base class. If you're in that situation, you can see all of them by clicking the link labelled "List of all members."

Nick

It's legal for an instruction to have an empty name. If it has a name,
you get %the_name in the output assembly. If it doesn't, you get %0,
%1, %2, etc. in sequence.