static const function locals

Hi all

I'm trying to write an optimization to create constant initializers for static const locals where they are structures, i.e., on this test

struct S {
  int i;
  S(int v) : i(v) { }
};

int foo(int i) {
  static const S arr[4] = {0,1,2,3};
  return arr[i].i;
}

i'm going to change the IR so that instead of a series of stores guarded by __cxa_guard functions, we just have

@_ZZ3fooiE3arr = constant global [4 x %struct.S] [%struct.S { i32 0}, %struct.S { i32 1}, %struct.S { i32 2}, %struct.S { i32 3}]

However, the global has been given a zero initializer which makes doing this change difficult without more analysis than I think is necessary.

So, finally, my question is, can we put an undef initializer on this variable instead of a zero initializer? I don't know the C++ spec so i'm not sure if i'm missing something.

Thanks,
Pete

No; see [stmt.dcl]p4.

-Eli

Hi all

I'm trying to write an optimization to create constant initializers for static const locals where they are structures, i.e., on this test

struct S {
       int i;
       S(int v) : i(v) { }
};

int foo(int i) {
       static const S arr[4] = {0,1,2,3};
       return arr[i].i;
}

i'm going to change the IR so that instead of a series of stores guarded by __cxa_guard functions, we just have

@_ZZ3fooiE3arr = constant global [4 x %struct.S] [%struct.S { i32 0}, %struct.S { i32 1}, %struct.S { i32 2}, %struct.S { i32 3}]

However, the global has been given a zero initializer which makes doing this change difficult without more analysis than I think is necessary.

So, finally, my question is, can we put an undef initializer on this variable instead of a zero initializer? I don't know the C++ spec so i'm not sure if i'm missing something.

No; see [stmt.dcl]p4.

D'oh. More involved analysis it is then. Thanks Eli.

Pete