question about GetElementPtr Instruction

I have a question about GetElementPtr.

Suppose I have an GetElementPtr Instruction GI:

%reg = getelementptr %ST* %s, uint 1, ubyte 2, ubyte 1, uint
5, uint 13

I want to check if this is the reference of a component of a
structure, how can I do that? Should I check which operand of
this instruction is 'ubyte' type? How can I do that in code?

should I use
ubyte *a = dyn_cast<ubyte>GI.getoperand(i) or something else?

Thanks,
xiaodong

I have a question about GetElementPtr.

Suppose I have an GetElementPtr Instruction GI:

%reg = getelementptr %ST* %s, uint 1, ubyte 2, ubyte 1, uint
5, uint 13

I want to check if this is the reference of a component of a
structure, how can I do that? Should I check which operand of
this instruction is 'ubyte' type? How can I do that in code?

Yes, indices of type UByte are indices into structures; type Long are
indices into arrays. Also, structure indices are always constants.

should I use
ubyte *a = dyn_cast<ubyte>GI.getoperand(i) or something else?

You are trying to cast the operand (a Value) to a particular primitive type,
but there is no such thing as class ubyte. Instead:

  if (GI.getoperand(i)->getType() == Type::UByteTy) {
    ConstantUInt* idx = cast<ConstantUInt>(GI.getoperand(i));
    ...
  }

You could also do the following (since array indices are signed and
structure indices are unsigned and always constant, but I don't recommend
it):

  if (ConstantUInt* idx = dyn_cast<ConstantUInt>(GI.getoperand(i))) {
    ...
  }

--Vikram

Dear LLVMdev,

As far as I can tell, what the "ubyte 2" value really means is the
2+1'th member of the structure...regardless of whether that structure's
3rd element actually starts at (pointer to structure) + 3*sizeof(ubyte)
or not. So, is there any semantic connection between "ubyte" as
used in structure accesses and what getelementptr actually interprets
it as? Or is it just called ubyte for fun?

-"Confused in Urbana", Brian

You are correct, ubyte 2 simply means 3rd element of the structure. This
quantity is always constant (it specifies the field number), so we could
have used any signed or unsigned integer type for it instead of ubyte.
UByte means we are limited to at most 256 structure fields but it also makes
the bytecode representation more compact. (To remove this restriction, we
may migrate to UInt in the future and use some kind of Zipf encoding to
avoid increasing bytecode size in the common case.)

There is a routine TargetData::GetIndexedOffset to convert any ptr + index
list to the actual byte offset for a particular target machine. See
llvm/include/Target/TargetData.h.

--Vikram

Hi,

I've another question about getelementptr. So, in the twisted mind of my little
pass, I have a statement like this:

%reg228-mod = getelementptr %struct.SimpleStruct* %N.1, long 0 ; <%struct.SimpleStruct*> [#uses=2]

I'm not sure of the semantics of %a = getelementptr %b, long 0. I want to
think that it is some kind of weird identity (no-op), judging from the operand
type and the result type that is printed out by llvm. In this sense I suppose
it is like saying a = &(b[0]) (== b). I couldn't quite make it out from reading
the language reference.

So I want to go ahead and transform uses of %reg228-mod by replacing them with
uses of %N.1, in this example. Is this right or am I way off?

Thanks for any help,

-Brian

That's correct,
  X = getElementPtr <Ty>* ptr, long 0
is a NOP. It does exactly what you said: X = &b[0].

  X = getElementPtr <Ty>* ptr
is a NOP too.

--Vikram