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> 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-- gaeke at uiuc.edu
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> -----Original Message----- > From: llvmdev-admin at cs.uiuc.edu [mailto:llvmdev-admin at cs.uiuc.edu]On > Behalf Of Brian R. Gaeke > Sent: Thursday, September 26, 2002 1:55 AM > To: llvmdev at cs.uiuc.edu > Subject: Re: [LLVMdev] question about GetElementPtr Instruction > > > > 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 > > > 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 > > -- > gaeke at uiuc.edu > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev >
Apparently Analagous Threads
- [LLVMdev] Another question about GetElementPtr Instruction
- [LLVMdev] question about GetElementPtr Instruction
- [LLVMdev] question about GetElementPtr Instruction
- [LLVMdev] Two important changes to the getelementptr instruction
- [LLVMdev] Two important changes to the getelementptr instruction