The Often Misunderstood GEP Instruction

Introduction

This document seeks to dispel the mystery and confusion surrounding LLVM’sGetElementPtr (GEP) instruction.Questions about the wily GEP instruction are probably the most frequentlyoccurring questions once a developer gets down to coding with LLVM. Here we layout the sources of confusion and show that the GEP instruction is really quitesimple.

Address Computation

When people are first confronted with the GEP instruction, they tend to relateit to known concepts from other programming paradigms, most notably C arrayindexing and field selection. GEP closely resembles C array indexing and fieldselection, however it is a little different and this leads to the followingquestions.

What is the first index of the GEP instruction?

Quick answer: The index stepping through the second operand.

The confusion with the first index usually arises from thinking about theGetElementPtr instruction as if it was a C index operator. They aren’t thesame. For example, when we write, in “C”:

  1. AType *Foo;
  2. ...
  3. X = &Foo->F;

it is natural to think that there is only one index, the selection of the fieldF. However, in this example, Foo is a pointer. That pointermust be indexed explicitly in LLVM. C, on the other hand, indices through ittransparently. To arrive at the same address location as the C code, you wouldprovide the GEP instruction with two index operands. The first operand indexesthrough the pointer; the second operand indexes the field F of thestructure, just as if you wrote:

  1. X = &Foo[0].F;

Sometimes this question gets rephrased as:

Why is it okay to index through the first pointer, but subsequent pointerswon’t be dereferenced?

The answer is simply because memory does not have to be accessed to perform thecomputation. The second operand to the GEP instruction must be a value of apointer type. The value of the pointer is provided directly to the GEPinstruction as an operand without any need for accessing memory. It must,therefore be indexed and requires an index operand. Consider this example:

  1. struct munger_struct {
  2. int f1;
  3. int f2;
  4. };
  5. void munge(struct munger_struct *P) {
  6. P[0].f1 = P[1].f1 + P[2].f2;
  7. }
  8. ...
  9. struct munger_struct Array[3];
  10. ...
  11. munge(Array);

In this “C” example, the front end compiler (Clang) will generate three GEPinstructions for the three indices through “P” in the assignment statement. Thefunction argument P will be the second operand of each of these GEPinstructions. The third operand indexes through that pointer. The fourthoperand will be the field offset into the struct munger_struct type, foreither the f1 or f2 field. So, in LLVM assembly the munge functionlooks like:

  1. define void @munge(%struct.munger_struct* %P) {
  2. entry:
  3. %tmp = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 1, i32 0
  4. %tmp1 = load i32, i32* %tmp
  5. %tmp2 = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 2, i32 1
  6. %tmp3 = load i32, i32* %tmp2
  7. %tmp4 = add i32 %tmp3, %tmp1
  8. %tmp5 = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 0, i32 0
  9. store i32 %tmp4, i32* %tmp5
  10. ret void
  11. }

In each case the second operand is the pointer through which the GEP instructionstarts. The same is true whether the second operand is an argument, allocatedmemory, or a global variable.

To make this clear, let’s consider a more obtuse example:

  1. %MyVar = uninitialized global i32
  2. ...
  3. %idx1 = getelementptr i32, i32* %MyVar, i64 0
  4. %idx2 = getelementptr i32, i32* %MyVar, i64 1
  5. %idx3 = getelementptr i32, i32* %MyVar, i64 2

These GEP instructions are simply making address computations from the baseaddress of MyVar. They compute, as follows (using C syntax):

  1. idx1 = (char*) &MyVar + 0
  2. idx2 = (char*) &MyVar + 4
  3. idx3 = (char*) &MyVar + 8

Since the type i32 is known to be four bytes long, the indices 0, 1 and 2translate into memory offsets of 0, 4, and 8, respectively. No memory isaccessed to make these computations because the address of %MyVar is passeddirectly to the GEP instructions.

The obtuse part of this example is in the cases of %idx2 and %idx3. Theyresult in the computation of addresses that point to memory past the end of the%MyVar global, which is only one i32 long, not three i32s long.While this is legal in LLVM, it is inadvisable because any load or store withthe pointer that results from these GEP instructions would produce undefinedresults.

Why is the extra 0 index required?

Quick answer: there are no superfluous indices.

This question arises most often when the GEP instruction is applied to a globalvariable which is always a pointer type. For example, consider this:

  1. %MyStruct = uninitialized global { float*, i32 }
  2. ...
  3. %idx = getelementptr { float*, i32 }, { float*, i32 }* %MyStruct, i64 0, i32 1

The GEP above yields an i32* by indexing the i32 typed field of thestructure %MyStruct. When people first look at it, they wonder why the i640 index is needed. However, a closer inspection of how globals and GEPs workreveals the need. Becoming aware of the following facts will dispel theconfusion:

  • The type of %MyStruct is not { float, i32 } but rather { float,i32 }*. That is, %MyStruct is a pointer to a structure containing apointer to a float and an i32.
  • Point #1 is evidenced by noticing the type of the second operand of the GEPinstruction (%MyStruct) which is { float, i32 }.
  • The first index, i64 0 is required to step over the global variable%MyStruct. Since the second argument to the GEP instruction must alwaysbe a value of pointer type, the first index steps through that pointer. Avalue of 0 means 0 elements offset from that pointer.
  • The second index, i32 1 selects the second field of the structure (thei32).

What is dereferenced by GEP?

Quick answer: nothing.

The GetElementPtr instruction dereferences nothing. That is, it doesn’t accessmemory in any way. That’s what the Load and Store instructions are for. GEP isonly involved in the computation of addresses. For example, consider this:

  1. %MyVar = uninitialized global { [40 x i32 ]* }
  2. ...
  3. %idx = getelementptr { [40 x i32]* }, { [40 x i32]* }* %MyVar, i64 0, i32 0, i64 0, i64 17

In this example, we have a global variable, %MyVar that is a pointer to astructure containing a pointer to an array of 40 ints. The GEP instruction seemsto be accessing the 18th integer of the structure’s array of ints. However, thisis actually an illegal GEP instruction. It won’t compile. The reason is that thepointer in the structure must be dereferenced in order to index into thearray of 40 ints. Since the GEP instruction never accesses memory, it isillegal.

In order to access the 18th integer in the array, you would need to do thefollowing:

  1. %idx = getelementptr { [40 x i32]* }, { [40 x i32]* }* %, i64 0, i32 0
  2. %arr = load [40 x i32]*, [40 x i32]** %idx
  3. %idx = getelementptr [40 x i32], [40 x i32]* %arr, i64 0, i64 17

In this case, we have to load the pointer in the structure with a loadinstruction before we can index into the array. If the example was changed to:

  1. %MyVar = uninitialized global { [40 x i32 ] }
  2. ...
  3. %idx = getelementptr { [40 x i32] }, { [40 x i32] }*, i64 0, i32 0, i64 17

then everything works fine. In this case, the structure does not contain apointer and the GEP instruction can index through the global variable, into thefirst field of the structure and access the 18th i32 in the array there.

Why don’t GEP x,0,0,1 and GEP x,1 alias?

Quick Answer: They compute different address locations.

If you look at the first indices in these GEP instructions you find that theyare different (0 and 1), therefore the address computation diverges with thatindex. Consider this example:

  1. %MyVar = global { [10 x i32] }
  2. %idx1 = getelementptr { [10 x i32] }, { [10 x i32] }* %MyVar, i64 0, i32 0, i64 1
  3. %idx2 = getelementptr { [10 x i32] }, { [10 x i32] }* %MyVar, i64 1

In this example, idx1 computes the address of the second integer in thearray that is in the structure in %MyVar, that is MyVar+4. The type ofidx1 is i32. However, idx2 computes the address of _the next_structure after %MyVar. The type of idx2 is { [10 x i32] } and itsvalue is equivalent to MyVar + 40 because it indexes past the ten 4-byteintegers in MyVar. Obviously, in such a situation, the pointers don’talias.

Why do GEP x,1,0,0 and GEP x,1 alias?

Quick Answer: They compute the same address location.

These two GEP instructions will compute the same address because indexingthrough the 0th element does not change the address. However, it does change thetype. Consider this example:

  1. %MyVar = global { [10 x i32] }
  2. %idx1 = getelementptr { [10 x i32] }, { [10 x i32] }* %MyVar, i64 1, i32 0, i64 0
  3. %idx2 = getelementptr { [10 x i32] }, { [10 x i32] }* %MyVar, i64 1

In this example, the value of %idx1 is %MyVar+40 and its type isi32. The value of %idx2 is also MyVar+40 but its type is { [10 xi32] }.

Can GEP index into vector elements?

This hasn’t always been forcefully disallowed, though it’s not recommended. Itleads to awkward special cases in the optimizers, and fundamental inconsistencyin the IR. In the future, it will probably be outright disallowed.

What effect do address spaces have on GEPs?

None, except that the address space qualifier on the second operand pointer typealways matches the address space qualifier on the result type.

How is GEP different from ptrtoint, arithmetic, and inttoptr?

It’s very similar; there are only subtle differences.

With ptrtoint, you have to pick an integer type. One approach is to pick i64;this is safe on everything LLVM supports (LLVM internally assumes pointers arenever wider than 64 bits in many places), and the optimizer will actually narrowthe i64 arithmetic down to the actual pointer size on targets which don’tsupport 64-bit arithmetic in most cases. However, there are some cases where itdoesn’t do this. With GEP you can avoid this problem.

Also, GEP carries additional pointer aliasing rules. It’s invalid to take a GEPfrom one object, address into a different separately allocated object, anddereference it. IR producers (front-ends) must follow this rule, and consumers(optimizers, specifically alias analysis) benefit from being able to rely onit. See the Rules section for more information.

And, GEP is more concise in common cases.

However, for the underlying integer computation implied, there is nodifference.

I’m writing a backend for a target which needs custom lowering for GEP. How do I do this?

You don’t. The integer computation implied by a GEP is target-independent.Typically what you’ll need to do is make your backend pattern-match expressionstrees involving ADD, MUL, etc., which are what GEP is lowered into. This has theadvantage of letting your code work correctly in more cases.

GEP does use target-dependent parameters for the size and layout of data types,which targets can customize.

If you require support for addressing units which are not 8 bits, you’ll need tofix a lot of code in the backend, with GEP lowering being only a small piece ofthe overall picture.

How does VLA addressing work with GEPs?

GEPs don’t natively support VLAs. LLVM’s type system is entirely static, and GEPaddress computations are guided by an LLVM type.

VLA indices can be implemented as linearized indices. For example, an expressionlike X[a][b][c], must be effectively lowered into a form likeX[am+bn+c], so that it appears to the GEP as a single-dimensional arrayreference.

This means if you want to write an analysis which understands array indices andyou want to support VLAs, your code will have to be prepared to reverse-engineerthe linearization. One way to solve this problem is to use the ScalarEvolutionlibrary, which always presents VLA and non-VLA indexing in the same manner.

Rules

What happens if an array index is out of bounds?

There are two senses in which an array index can be out of bounds.

First, there’s the array type which comes from the (static) type of the firstoperand to the GEP. Indices greater than the number of elements in thecorresponding static array type are valid. There is no problem with out ofbounds indices in this sense. Indexing into an array only depends on the size ofthe array element, not the number of elements.

A common example of how this is used is arrays where the size is not known.It’s common to use array types with zero length to represent these. The factthat the static type says there are zero elements is irrelevant; it’s perfectlyvalid to compute arbitrary element indices, as the computation only depends onthe size of the array element, not the number of elements. Note that zero-sizedarrays are not a special case here.

This sense is unconnected with inbounds keyword. The inbounds keyword isdesigned to describe low-level pointer arithmetic overflow conditions, ratherthan high-level array indexing rules.

Analysis passes which wish to understand array indexing should not assume thatthe static array type bounds are respected.

The second sense of being out of bounds is computing an address that’s beyondthe actual underlying allocated object.

With the inbounds keyword, the result value of the GEP is undefined if theaddress is outside the actual underlying allocated object and not the addressone-past-the-end.

Without the inbounds keyword, there are no restrictions on computingout-of-bounds addresses. Obviously, performing a load or a store requires anaddress of allocated and sufficiently aligned memory. But the GEP itself is onlyconcerned with computing addresses.

Can array indices be negative?

Yes. This is basically a special case of array indices being out of bounds.

Can I compare two values computed with GEPs?

Yes. If both addresses are within the same allocated object, orone-past-the-end, you’ll get the comparison result you expect. If either isoutside of it, integer arithmetic wrapping may occur, so the comparison may notbe meaningful.

Can I do GEP with a different pointer type than the type of the underlying object?

Yes. There are no restrictions on bitcasting a pointer value to an arbitrarypointer type. The types in a GEP serve only to define the parameters for theunderlying integer computation. They need not correspond with the actual type ofthe underlying object.

Furthermore, loads and stores don’t have to use the same types as the type ofthe underlying object. Types in this context serve only to specify memory sizeand alignment. Beyond that there are merely a hint to the optimizer indicatinghow the value will likely be used.

Can I cast an object’s address to integer and add it to null?

You can compute an address that way, but if you use GEP to do the add, you can’tuse that pointer to actually access the object, unless the object is managedoutside of LLVM.

The underlying integer computation is sufficiently defined; null has a definedvalue — zero — and you can add whatever value you want to it.

However, it’s invalid to access (load from or store to) an LLVM-aware objectwith such a pointer. This includes GlobalVariables, Allocas, and objectspointed to by noalias pointers.

If you really need this functionality, you can do the arithmetic with explicitinteger instructions, and use inttoptr to convert the result to an address. Mostof GEP’s special aliasing rules do not apply to pointers computed from ptrtoint,arithmetic, and inttoptr sequences.

Can I compute the distance between two objects, and add that value to one address to compute the other address?

As with arithmetic on null, you can use GEP to compute an address that way, butyou can’t use that pointer to actually access the object if you do, unless theobject is managed outside of LLVM.

Also as above, ptrtoint and inttoptr provide an alternative way to do this whichdo not have this restriction.

Can I do type-based alias analysis on LLVM IR?

You can’t do type-based alias analysis using LLVM’s built-in type system,because LLVM has no restrictions on mixing types in addressing, loads or stores.

LLVM’s type-based alias analysis pass uses metadata to describe a different typesystem (such as the C type system), and performs type-based aliasing on top ofthat. Further details are in thelanguage reference.

What happens if a GEP computation overflows?

If the GEP lacks the inbounds keyword, the value is the result fromevaluating the implied two’s complement integer computation. However, sincethere’s no guarantee of where an object will be allocated in the address space,such values have limited meaning.

If the GEP has the inbounds keyword, the result value is undefined (a “trapvalue”) if the GEP overflows (i.e. wraps around the end of the address space).

As such, there are some ramifications of this for inbounds GEPs: scales impliedby array/vector/pointer indices are always known to be “nsw” since they aresigned values that are scaled by the element size. These values are alsoallowed to be negative (e.g. “gep i32 *%P, i32 -1”) but the pointer itselfis logically treated as an unsigned value. This means that GEPs have anasymmetric relation between the pointer base (which is treated as unsigned) andthe offset applied to it (which is treated as signed). The result of theadditions within the offset calculation cannot have signed overflow, but whenapplied to the base pointer, there can be signed overflow.

How can I tell if my front-end is following the rules?

There is currently no checker for the getelementptr rules. Currently, the onlyway to do this is to manually check each place in your front-end whereGetElementPtr operators are created.

It’s not possible to write a checker which could find all rule violationsstatically. It would be possible to write a checker which works by instrumentingthe code with dynamic checks though. Alternatively, it would be possible towrite a static checker which catches a subset of possible problems. However, nosuch checker exists today.

Rationale

Why is GEP designed this way?

The design of GEP has the following goals, in rough unofficial order ofpriority:

  • Support C, C-like languages, and languages which can be conceptually loweredinto C (this covers a lot).
  • Support optimizations such as those that are common in C compilers. Inparticular, GEP is a cornerstone of LLVM’s pointer aliasingmodel.
  • Provide a consistent method for computing addresses so that addresscomputations don’t need to be a part of load and store instructions in the IR.
  • Support non-C-like languages, to the extent that it doesn’t interfere withother goals.
  • Minimize target-specific information in the IR.

Why do struct member indices always use i32?

The specific type i32 is probably just a historical artifact, however it’s wideenough for all practical purposes, so there’s been no need to change it. Itdoesn’t necessarily imply i32 address arithmetic; it’s just an identifier whichidentifies a field in a struct. Requiring that all struct indices be the samereduces the range of possibilities for cases where two GEPs are effectively thesame but have distinct operand types.

What’s an uglygep?

Some LLVM optimizers operate on GEPs by internally lowering them into moreprimitive integer expressions, which allows them to be combined with otherinteger expressions and/or split into multiple separate integer expressions. Ifthey’ve made non-trivial changes, translating back into LLVM IR can involvereverse-engineering the structure of the addressing in order to fit it into thestatic type of the original first operand. It isn’t always possibly to fullyreconstruct this structure; sometimes the underlying addressing doesn’tcorrespond with the static type at all. In such cases the optimizer instead willemit a GEP with the base pointer casted to a simple address-unit pointer, usingthe name “uglygep”. This isn’t pretty, but it’s just as valid, and it’ssufficient to preserve the pointer aliasing guarantees that GEP provides.

Summary

In summary, here’s some things to always remember about the GetElementPtrinstruction:

  • The GEP instruction never accesses memory, it only provides pointercomputations.
  • The second operand to the GEP instruction is always a pointer and it must beindexed.
  • There are no superfluous indices for the GEP instruction.
  • Trailing zero indices are superfluous for pointer aliasing, but not for thetypes of the pointers.
  • Leading zero indices are not superfluous for pointer aliasing nor the typesof the pointers.