[LLVMdev] Accessing constant indexes in GetElementPtr

Vikram S. Adve vadve at cs.uiuc.edu
Mon Sep 23 07:35:01 PDT 2002

> Also sprach Wojciech Stryjewski:
> } Ok. Let's say I have a GetElementPtrInst that is used to access
> structure
> } elements. In this case the indexes will be constants and I want
> to get the
> } constant values as an a native C int.
> }
> } Now I can iterate over all the indexes with idx_begin(). I can cast
> } all of the indexes to a ConstantIntegral. However, I cannot cast the
> } indexes to a ConstantSInt or ConstantUInt (cast<> throws an
> assertion) and
> } these two classes are the ones that have the getValue() method I need.
> }
> } Am I missing something here?
> }
> I'm not quite sure what particular problem you're having, but a few
> things to remember...
>     - Not all of the operands of getelementptr are constants. (In
>       particular, Operand(0) isn't).

Bill is right -- not all operands are necessarily constant.  Basically, any
index into an array may not be a constant.  Any index into a structure must
be a constant of type UByte, and so it should cast to a ConstantUInt.
(Operand 0 is not a special case, but it is always an array index so it
sometimes may not be a constant.  Very often, though, it is the constant 0.)

anyway, indexVal->getType() == Type::UByteTy should tell you if you have a
structure index or not.


More information about the llvm-dev mailing list