[LLVMdev] question about GetElementPtr Instruction

Vikram S. Adve vadve at cs.uiuc.edu
Wed Sep 25 21:27:01 PDT 2002


> 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




More information about the llvm-dev mailing list