How to assign to a (int * data) with a alloca

Hello, I running to problem with how to translate the last few 2 line in main.cpp which corresponds to the IR below into LLVM API code, like how do you convert the i32* to i32** so you can do a pointer assignment with the CreateStore() , I see that there is a hidden i32** _ZTW4data() generated, can you explained how this is done in API code?

Basically, the question is what’s the LLVM code for the C code below assigning data to a new alloca.

int * data,
data = (int *) alloca (1024);
 %2 = alloca i8, i64 %switch.select11, align 16
  store i8* %2, i8** bitcast (i32** @data to i8**), align 8, !tbaa !2
  ret i32 0

This is the main.cpp code.

thread_local int* data;

int main(int argc, char *argv[]) {

  int N;

  if (*argv[1]=='1') {
    N = 1;
  } else if (*argv[1]=='3') {
    N = 8;
  }

  data = (int*)alloca(N*sizeof(int));
  data[1] = 0;
}

This is the generated IR

; ModuleID = 'main.bc'
source_filename = "main.cpp"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

$_ZTW4data = comdat any

@data = dso_local thread_local global i32* null, align 8

; Function Attrs: nofree norecurse nounwind uwtable
define dso_local i32 @main(i32 %argc, i8** nocapture readonly %argv) local_unnamed_addr #0 {
entry:
  %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1
  %0 = load i8*, i8** %arrayidx, align 8, !tbaa !2
  %1 = load i8, i8* %0, align 1, !tbaa !6
  %switch.selectcmp = icmp eq i8 %1, 51
  %switch.select = select i1 %switch.selectcmp, i64 32, i64 0
  %switch.selectcmp10 = icmp eq i8 %1, 49
  %switch.select11 = select i1 %switch.selectcmp10, i64 4, i64 %switch.select
  %2 = alloca i8, i64 %switch.select11, align 16
  store i8* %2, i8** bitcast (i32** @data to i8**), align 8, !tbaa !2
  ret i32 0
}

; Function Attrs: uwtable
define weak_odr hidden i32** @_ZTW4data() local_unnamed_addr #1 comdat {
  ret i32** @data
}

attributes #0 = { nofree norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 10.0.1 (https://github.com/llvm/llvm-project.git ef32c611aa214dea855364efd7ba451ec5ec3f74)"}
!2 = !{!3, !3, i64 0}
!3 = !{!"any pointer", !4, i64 0}
!4 = !{!"omnipotent char", !5, i64 0}
!5 = !{!"Simple C++ TBAA"}
!6 = !{!4, !4, i64 0}

Hi!

Sorry, I am not sure what you really try to achieve. Your question seems to imply that you are already able to translate the other C++ statements. Creating a bitcast is not different from the other instructions. You need a Module to emit the instructions to, an IRBuilder pointing to the insertion point into a basic block, and of course the Value from calling alloca. The bitcast then simply requires you to grab the right type:

Module *M = ..;
IRBuilder<> Builder(MM->getContext());
Value *V = ..;
Type *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
Type *Int8PtrPtrTy = Int8PtrTy->getPointerTo();
Value *Casted = Builder.CreateBitCast(V, Int8PtrPtrTy);

If I got your question wrong, and you are really asking about the LLVM API basics, then I would suggest to google for a tutorial about this, or have a look at my book “Learn LLVM 12”, which explains this stuff, because it is really a broad topic.

I am not sure if you are aware that your C++ code is horrible broken.

Regards,
Kai