Referencing fields in initializers


For an IR transformation pass I need to get all addresses of fields in an global variable that refer
to global variables in their initializer.

For example, in this code I need the addresses of s.b[1] and s.c[0]:

int foo, bar;

struct S
int a;
int *b[2];
int *c[3];

} s = {1, {0, &foo}, {&bar, 0, 0}};

I tried to achieve this by traversing the initializer and maintaining a path of how I get to the
current position in the data structure. In the example s.b[1] corresponds to path (1, 1) and
s.c[0] with path (2, 0). The paths are then uses as indices for GetElementPtr. So I do
GetElementPtr s 1 1 and GetElementPtr s 2 0. This works in many cases but it crashes
on cases where there are GetElementPtr operators in the tree on the path leading

to the position where I want to take the address (“Invalid GetElementPtrInst indices for type!”).

Does anyone have a suggestion on how I can find the places in initialized global variables where

addresses of global variables are used for initialization?


I have found the issue. I used i64 as indices. For structs it should be an i32. That solved the problem.