[LLVMdev] Initialising global Array
Dan Gohman
gohman at apple.com
Tue Jul 7 10:06:17 PDT 2009
On Jun 18, 2009, at 6:22 AM, Andreas Neustifter wrote:
> Hi,
>
> I try to create a array that has a nonzero initialiser:
>
> What i do is, first create the array type.
>
>
>> const ArrayType *ATy = ArrayType::get(Type::Int32Ty, NumEdges);
>>
>
> Then create some constant values for the initializer.
>
>
>> std::vector<Constant*> Initializer; Initializer.reserve(NumEdges);
It looks like you may be using reserve when resize is needed.
>>
>> APInt zero(32,0); Constant* zeroc = ConstantInt::get(zero);
>>
>> APInt minusone(32,-1); Constant* minusonec = ConstantInt::get
>> (minusone);
>>
>
> Create the global array, there is no initializer yet.
>
>
>> GlobalVariable *Counters =
>>
>> new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
>>
>> Constant::getNullValue(ATy),
>> "OptimalEdgeProfCounters", &M);
>>
>
> Then exactly "NumEdges" constants are pushed to the "Initializer":
>
>
>> Initializer[i] = zeroc;
If Initializer hasn't been resized, this access will be
out of bounds.
Dan
More information about the llvm-dev
mailing list