Removing constants from a constant array?

Hi,

I’m currently writing a LLVM pass and would like to modify a constant array based on some basic analysis.

Is there any way to simply remove entries (`llvm::Constant`) from an array (`llvm::ConstantArray`)? Replacing all uses with an undefined value doesn’t work as the array is terminated by an undefined value, subsequent elements thus are ignored at runtime. I’d rather avoid re-creating the entire array from scratch and replace the existing array with it but I’m afraid there’s not many other options.

Is there any solution I’m not aware of yet?

Thanks,
Nicolai

Hi,

I’m currently writing a LLVM pass and would like to modify a constant array based on some basic analysis.

Is there any way to simply remove entries (`llvm::Constant`) from an array (`llvm::ConstantArray`)? Replacing all uses with an undefined value doesn’t work as the array is terminated by an undefined value, subsequent elements thus are ignored at runtime. I’d rather avoid re-creating the entire array from scratch and replace the existing array with it but I’m afraid there’s not many other options.

Is there any solution I’m not aware of yet?

In many cases, the only way to "change" a value using the LLVM C++ API is to create a new value and replace uses of the old value with the new value. I haven't looked, but I would not be surprised if this is the only way to "modify" the number of elements in a ConstantArray.

I can see how removing a ConstantArray element or creating a new ConstantArray with fewer elements would be a bit of a pain: you would need to change the ConstantArray, all the GEPs for the constant array, and potentially handle code that casts the ConstantArray to another type.

What are you trying to achieve be "removing" an element from a ConstantArray? Perhaps there is an easier way to do what you're trying to do.

Regards,

John Criswell

I can access the array through a GlobalVariable and set the variable's initializer, creating a new array should be doable but I’d still rather avoid doing so if possible.

The array contains pairs of strings and function pointers and is used by an interpreter and I’d like to remove some of them to limit the interpreter’s functionality.

If I replace the pair with an undefined value pair (i.e. both values are NULL), the interpreter stops scanning the array and further pairs are ignored (I can’t change that behavior). Removing only the string or function pointer doesn’t work either, the interpreter checks if either of the values is NULL and throws a runtime error.

Replacing the function pointer with a pointer to an empty function works but I’d rather erase the pairs entirely.
I’ll probably try re-building the array and replacing the existing one and see if that works.

Thanks,
Nicolai