llvm.org GIT mirror llvm / b905867
Merge from mainline. Improve the description on the getelementptr instruction. It should now better define what the instruction does. This also makes it clear that getelementptr can index into a vector type. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_24@57702 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 10 years ago
1 changed file(s) with 27 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
33083308
33093309
Syntax:
33103310

                  
                
3311 <result> = getelementptr <ty>* <ptrval>{, <ty> <idx>}*
3311 <result> = getelementptr <pty>* <ptrval>{, <ty> <idx>}*
33123312
33133313
33143314
Overview:
33153315
33163316

33173317 The 'getelementptr' instruction is used to get the address of a
3318 subelement of an aggregate data structure.

3319
3320
Arguments:
3321
3322

This instruction takes a list of integer operands that indicate what

3323 elements of the aggregate object to index to. The actual types of the arguments
3324 provided depend on the type of the first pointer argument. The
3325 'getelementptr' instruction is used to index down through the type
3326 levels of a structure or to a specific index in an array. When indexing into a
3327 structure, only i32 integer constants are allowed. When indexing
3328 into an array or pointer, only integers of 32 or 64 bits are allowed; 32-bit
3329 values will be sign extended to 64-bits if required.

3318 subelement of an aggregate data structure. It performs address calculation only
3319 and does not access memory.

3320
3321
Arguments:
3322
3323

The first argument is always a pointer, and forms the basis of the

3324 calculation. The remaining arguments are indices, that indicate which of the
3325 elements of the aggregate object are indexed. The interpretation of each index
3326 is dependent on the type being indexed into. The first index always indexes the
3327 pointer value given as the first argument, the second index indexes a value of
3328 the type pointed to (not necessarily the value directly pointed to, since the
3329 first index can be non-zero), etc. The first type indexed into must be a pointer
3330 value, subsequent types can be arrays, vectors and structs. Note that subsequent
3331 types being indexed into can never be pointers, since that would require loading
3332 the pointer before continuing calculation.

3333
3334

The type of each index argument depends on the type it is indexing into.

3335 When indexing into a (packed) structure, only i32 integer
3336 constants are allowed. When indexing into an array, pointer or vector,
3337 only integers of 32 or 64 bits are allowed (also non-constants). 32-bit values
3338 will be sign extended to 64-bits if required.

33303339
33313340

For example, let's consider a C code fragment and how it gets

33323341 compiled to LLVM:

33673376
33683377
Semantics:
33693378
3370

The index types specified for the 'getelementptr' instruction depend

3371 on the pointer type that is being indexed into. Pointer
3372 and array types can use a 32-bit or 64-bit
3373 integer type but the value will always be sign extended
3374 to 64-bits. Structure and packed
3375 structure types require i32 constants.

3376
33773379

In the example above, the first index is indexing into the '%ST*'

33783380 type, which is a pointer, yielding a '%ST' = '{ i32, double, %RT
33793381 }' type, a structure. The second index indexes into the third element of
34133415
34143416

                  
                
34153417 ; yields [12 x i8]*:aptr
3416 %aptr = getelementptr {i32, [12 x i8]}* %sptr, i64 0, i32 1
3418 %aptr = getelementptr {i32, [12 x i8]}* %saptr, i64 0, i32 1
3419 ; yields i8*:vptr
3420 %vptr = getelementptr {i32, <2 x i8>}* %svptr, i64 0, i32 1, i32 1
3421 ; yields i8*:eptr
3422 %eptr = getelementptr [12 x i8]* %aptr, i64 0, i32 1
34173423
34183424
34193425