Search code examples
c++compiler-constructionglobal-variablesllvm

Painlessly change type of a GlobalVariable


In my IR I have a string global constant:

@.str = private unnamed_addr constant [6 x i8] c"Hello\00", align 1

This constant is used in various places across the module. I need to extend its initializer by few bytes. There is a GlobalVariable method to change it (setInitializer()), but it doesn't work, because the new initializer has different size and, thus, type.

It isn't possible to do str->RAUW(newStr) because of the same difference in types. However, my code doesn't use that array size information, so it should be safe to just "hack" the type change.

Is there an easy way to do this without rewriting my whole IR?


Solution

  • I haven't found any way to do this in simple way, but the negative answer is also an answer.

    I ended up carefully rewriting the whole module. PHIInsts were particulary nasty.