GEP closely resembles C array indexing and field selection.
<result> = getelementptr <ty>, <ty>* <ptrval>{, [inrange] <ty> <idx>}*
<result> = getelementptr inbounds <ty>, <ty>* <ptrval>{, [inrange] <ty> <idx>}*
<result> = getelementptr <ty>, <ptr vector> <ptrval>, [inrange] <vector index type> <idx>
Parameter
Return value
Attention: GEP can not be used without load or store instruction. The address calculated must be used afterwards.
struct munger_struct {
int f1;
int f2;
};
void munge(struct munger_struct *P) {
P[0].f1 = P[1].f1 + P[2].f2;
}
...
struct munger_struct Array[3];
...
munge(Array);
P[1].f1 is translated and decomposed to two LLVM instructions
%tmp = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 1, i32 0
%tmp1 = load i32, i32* %tmp
The bc file is as follows:
define void @munge(%struct.munger_struct* %P) {
entry:
%tmp = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 1, i32 0
%tmp1 = load i32, i32* %tmp
%tmp2 = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 2, i32 1
%tmp3 = load i32, i32* %tmp2
%tmp4 = add i32 %tmp3, %tmp1
%tmp5 = getelementptr %struct.munger_struct, %struct.munger_struct* %P, i32 0, i32 0
store i32 %tmp4, i32* %tmp5
ret void
}
Source code