[LLVMdev] Initialising global Array

Slobodan Pejic pejic at ualberta.ca
Thu Jun 18 11:06:26 PDT 2009


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);
>
>   

I ran the following two functions in a ModulePass.  The Initializer 
initialization is the difference.


static void andiTest1(Module &M) {
  int NumEdges = 4;
  const ArrayType *ATy = ArrayType::get(Type::Int32Ty, NumEdges);

  std::vector<Constant*> Initializer; Initializer.reserve(NumEdges);
  APInt zero(32,0);      Constant* zeroc = ConstantInt::get(zero);
  APInt minusone(32,-1); Constant* minusonec = ConstantInt::get(minusone);

  GlobalVariable *Counters = new GlobalVariable(ATy, false,
      GlobalValue::InternalLinkage, Constant::getNullValue(ATy),
      "OptimalEdgeProfCounters1", &M);

  Initializer[0] = zeroc;
  Initializer[1] = minusonec;
  Initializer[2] = minusonec;
  Initializer[3] = zeroc;

  Constant *init = llvm::ConstantArray::get(ATy, Initializer);
  Counters->setInitializer(init);
  DOUT << "Initializer:: \n";
  for( int i = 0; i < Initializer.size(); ++i ) {
    DOUT << "Initializer["<<i<<"]: " << Initializer[i] << "\n";
  }
}

static void andiTest2(Module &M) {
  int NumEdges = 4;
  const ArrayType *ATy = ArrayType::get(Type::Int32Ty, NumEdges);

  std::vector<Constant*> Initializer = std::vector<Constant*>(NumEdges);
  APInt zero(32,0);      Constant* zeroc = ConstantInt::get(zero);
  APInt minusone(32,-1); Constant* minusonec = ConstantInt::get(minusone);

  GlobalVariable *Counters = new GlobalVariable(ATy, false,
      GlobalValue::InternalLinkage, Constant::getNullValue(ATy),
      "OptimalEdgeProfCounters2", &M);

  Initializer[0] = zeroc;
  Initializer[1] = minusonec;
  Initializer[2] = minusonec;
  Initializer[3] = zeroc;

  Constant *init = llvm::ConstantArray::get(ATy, Initializer);
  Counters->setInitializer(init);
  DOUT << "Initializer:: \n";
  for( int i = 0; i < Initializer.size(); ++i ) {
    DOUT << "Initializer["<<i<<"]: " << Initializer[i] << "\n";
  }
}



More information about the llvm-dev mailing list