Accessing constant indexes in GetElementPtr

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?

Wojciech

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).
    - You might try "dyn_cast" which will return a valid pointer or 0
      instead of throwing an assertion.
    - Look at the "getNullValue()" method in "Constant". That might be
      useful as well.

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.

--Vikram

Vikram S. Adve wrote:

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.)

That should be _index_ 0, operand 0 is the base pointer itself.

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.)

One minor correction: Operand #0 is the base pointer for the
getelementptr instruction, operand #1 (index #0) is what's being referred
to here...

-Chris

http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/