Mutating the elements of a ConstantArray

Hello,

I need to append something to the global "llvm.global_ctors". This
variable may or may not already be declared within the current module.

If I lookup the global variable, I see that it supports a
getOperand(i) and setOperand(i,c), but does not support any way that I
can enlarge that array to add a new record.

Any suggestions?

Hi Nick,

You actually have to create a new ConstantArray with the desired elements, then remove the existing llvm.global_ctors constand and create a new one to replace it.

Take a look at the GlobalOpt pass for examples of how to do this sort of thing.

-Chris

Thanks,

Just one question more: why does Constant::getVectorElements()
operate on a SmallVector<T>, while ConstantArray::get() operate on a
std::vector<T> ? What is the distinction between these uses?

Thanks!
Nick

SmallVector is optimized for the case when the vector is small, to not go to the heap. This is the "small string optimization" for vectors. More details here:
http://llvm.org/docs/ProgrammersManual.html#datastructure

As far API design goes, we're in a mixed state. I'd strongly prefer to get rid of std::vector from the various interfaces, f.e. creating a constant array currently requires passing in an std::vector. For these sorts of interfaces, we should migrate to passing in a "Constant *const* / unsigned" pair. This allows use with a C array, a SmallVector, std::vector, or any other container with sequential storage.

For the "get data" APIs, we should migrate to returning in a SmallVectorImpl<t>& like Constant::getVectorElements() does.

-Chris