0
votes

Is there a way to extract or insert an element at an index from a value instead of an int? I would like to be able to use something like ExtractElementInst but for arrays instead of vectors.

Right now I am doing

mBuilder.CreateExtractValue(refArray, index)

But, I need to be able to pass it a Value * because I would like to be able to access array elements at the index of the value of a variable. For example:

array[i]

With the above code, I am limited to:

array[0]
1

1 Answers

1
votes

To perform address arithmetic with arbitrary, non-constant indices, you need to use the getelementptr instruction, which is created using the CreateGEP function.

Note that getelementptr needs its argument to be a pointer, so you'll want to directly use the global variable or alloca that holds the array, rather than reading it first. This also means that you need a zero as the first index to follow the pointer. So all in all, the generated code to read array[i] should look something like this:

@array = global [3 x i32] [1,2,3]

define void f() {
  %i = ; calculuate the index here
  %array_i_ptr = getelementptr [3 x i32], [3 x i32]* @array, i32 0, i32 %i
  %array_i_value = load i32, i32* array_i_ptr
  ; do something with %array_i_value
  ret
}

Here I assumed array was a global variable. For a local variable, you'd use alloca instead. For a dynamically allocated array, you'd have a call to malloc, no array types and a getelementptr instruction with only one index (because we'd be working with a pointer to int, rather than pointer to array to int, so the zero index isn't necessary).