why clang compile local to global



Hello,everyone:
I want to known how to let clang compile my local array to local variables:
I have the code :
int main()
{
int a[3]={1,2,3};

int b=7;
int c=8;
int d=9;
int e=10;
int f=11;
test(b,c,d,e,f,a);
return 0;
}

I use clang command:clang --target=mipsel -emit-llvm -S a.c -o a.ll
The a.ll is:
@main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4

define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%a = alloca [3 x i32], align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
%d = alloca i32, align 4
%e = alloca i32, align 4
%f = alloca i32, align 4
store i32 0, i32* %retval
%0 = bitcast [3 x i32]* %a to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)
store i32 7, i32* %b, align 4
store i32 8, i32* %c, align 4
store i32 9, i32* %d, align 4
store i32 10, i32* %e, align 4
store i32 11, i32* %f, align 4
%1 = load i32, i32* %b, align 4
%2 = load i32, i32* %c, align 4
%3 = load i32, i32* %d, align 4
%4 = load i32, i32* %e, align 4
%5 = load i32, i32* %f, align 4
%arraydecay = getelementptr inbounds [3 x i32], [3 x i32]* %a, i32 0, i32 0
call void @test(i32 signext %1, i32 signext %2, i32 signext %3, i32 signext %4, i32 signext %5, i32* %arraydecay)
ret i32 0;}

I want to known how to compile local array a to a local variable
Thank you








>



|

bg(12-28-15-08-4(12-28-15-09-26).jpg

Hello,everyone:
     I want to known how to let clang compile my local array to local variables:
     I have the code :
    int main()
{
  int a[3]={1,2,3};

        int b=7;
  int c=8;
  int d=9;
  int e=10;
  int f=11;
  test(b,c,d,e,f,a);
  return 0;
}
I use clang command:clang --target=mipsel -emit-llvm -S a.c -o a.ll
The a.ll is:
@main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4
define i32 @main() #0 {
entry:
  %retval = alloca i32, align 4
  %a = alloca [3 x i32], align 4
  %b = alloca i32, align 4
  %c = alloca i32, align 4
  %d = alloca i32, align 4
  %e = alloca i32, align 4
  %f = alloca i32, align 4
  store i32 0, i32* %retval
  %0 = bitcast [3 x i32]* %a to i8*
  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)
  store i32 7, i32* %b, align 4
  store i32 8, i32* %c, align 4
  store i32 9, i32* %d, align 4
  store i32 10, i32* %e, align 4
  store i32 11, i32* %f, align 4
  %1 = load i32, i32* %b, align 4
  %2 = load i32, i32* %c, align 4
  %3 = load i32, i32* %d, align 4
  %4 = load i32, i32* %e, align 4
  %5 = load i32, i32* %f, align 4
  %arraydecay = getelementptr inbounds [3 x i32], [3 x i32]* %a, i32 0, i32 0
  call void @test(i32 signext %1, i32 signext %2, i32 signext %3, i32 signext %4, i32 signext %5, i32* %arraydecay)
  ret i32 0;}
I want to known how to compile local array a to a local variable

Your question is not totally clear: a *is* a “local variable” on the stack, this is the line:

%a = alloca [3 x i32], align 4

It is initialized here:

  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)

using the constant data that is stored in a global:

@main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4

What would you like the code to look like?



Hello,Mehdi
Thanks for your answer,I am writing a llvm backend for my team DSP. The code is a testcase , but when I compile the code and running it ,I can not get the correct result.
In the generating machine code, it loads the array value from a "strange " address not in the stack , but I can not get where the address value comes from , so I look up the .ll file.
With your help, I know variable “a” is really in the stack, the “main.a” variable is constant global, and llvm.memcpy initialize the “a” array , I have misunderstood the .ll file.
Then I see the assemble file , I find
.type $main.a,@object # @main.a
.section .rodata,“a”,@progbits
.align 2
$main.a:
.4byte 1 # 0x1
.4byte 2 # 0x2
.4byte 3 # 0x3
.size $main.a, 12


the “main.a” is in the rotata section
Then I look up my ld script , I do not put the rodata section in the data section ,so the value from the “strange” address is wrong . I finally know why it get the array from the "strange " address.
Now it works perfect.
Thank you for your patient and help ,my English is not very good,Thank you .








刘钰




> From: Mehdi Amini
> Date: 2016-12-28 15:17
> To: liuyu11@ict.ac.cn
> CC: llvm-dev; llvm-dev-request
> Subject: Re: [llvm-dev] why clang compile local to global
>
> > On Dec 27, 2016, at 11:09 PM, liuyu11@ict.ac.cn via llvm-dev llvm-dev@lists.llvm.org wrote:
> >
> > Hello,everyone:
> > I want to known how to let clang compile my local array to local variables:
> > I have the code :
> > int main()
> > {
> > int a[3]={1,2,3};
> >
> > int b=7;
> > int c=8;
> > int d=9;
> > int e=10;
> > int f=11;
> > test(b,c,d,e,f,a);
> > return 0;
> > }
> > I use clang command:clang --target=mipsel -emit-llvm -S a.c -o a.ll
> > The a.ll is:
> > @main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4
> > define i32 @main() #0 {
> > entry:
> > %retval = alloca i32, align 4
> > %a = alloca [3 x i32], align 4
> > %b = alloca i32, align 4
> > %c = alloca i32, align 4
> > %d = alloca i32, align 4
> > %e = alloca i32, align 4
> > %f = alloca i32, align 4
> > store i32 0, i32* %retval
> > %0 = bitcast [3 x i32]* %a to i8*
> > call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)
> > store i32 7, i32* %b, align 4
> > store i32 8, i32* %c, align 4
> > store i32 9, i32* %d, align 4
> > store i32 10, i32* %e, align 4
> > store i32 11, i32* %f, align 4
> > %1 = load i32, i32* %b, align 4
> > %2 = load i32, i32* %c, align 4
> > %3 = load i32, i32* %d, align 4
> > %4 = load i32, i32* %e, align 4
> > %5 = load i32, i32* %f, align 4
> > %arraydecay = getelementptr inbounds [3 x i32], [3 x i32]* %a, i32 0, i32 0
> > call void @test(i32 signext %1, i32 signext %2, i32 signext %3, i32 signext %4, i32 signext %5, i32* %arraydecay)
> > ret i32 0;}
> > I want to known how to compile local array a to a local variable
>
> Your question is not totally clear: a is a “local variable” on the stack, this is the line:
>
> > %a = alloca [3 x i32], align 4
>
> It is initialized here:
>
> > call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)
>
> using the constant data that is stored in a global:
>
> > @main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4
>
> What would you like the code to look like?
>
> —
> Mehdi



|

bg(12-28-15-08-4(12-28-15-09-26).jpg



Hi Mehdi
I want to know how to generate the IR ,that do not use the “main.a” global constant variable, and llvm.memcpy intrinsic , instead of liking other local variable initialization :
store i32 7, i32* %b, align 4
For example :
%element0 = getelementptr inbounds [3 x i32]* %a, i32 0, i32 0
store i32 1, i32* %element0, align 4
%element1 = getelementptr inbounds [3 x i32]* %a, i32 0, i32 1
store i32 2, i32* %element1, align 4
Thank you



> > On Dec 27, 2016, at 11:09 PM, liuyu11@ict.ac.cn via llvm-dev llvm-dev@lists.llvm.org wrote:
> >
> > Hello,everyone:
> > I want to known how to let clang compile my local array to local variables:
> > I have the code :
> > int main()
> > {
> > int a[3]={1,2,3};
> >
> > int b=7;
> > int c=8;
> > int d=9;
> > int e=10;
> > int f=11;
> > test(b,c,d,e,f,a);
> > return 0;
> > }
> > I use clang command:clang --target=mipsel -emit-llvm -S a.c -o a.ll
> > The a.ll is:
> > @main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4
> > define i32 @main() #0 {
> > entry:
> > %retval = alloca i32, align 4
> > %a = alloca [3 x i32], align 4
> > %b = alloca i32, align 4
> > %c = alloca i32, align 4
> > %d = alloca i32, align 4
> > %e = alloca i32, align 4
> > %f = alloca i32, align 4
> > store i32 0, i32* %retval
> > %0 = bitcast [3 x i32]* %a to i8*
> > call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)
> > store i32 7, i32* %b, align 4
> > store i32 8, i32* %c, align 4
> > store i32 9, i32* %d, align 4
> > store i32 10, i32* %e, align 4
> > store i32 11, i32* %f, align 4
> > %1 = load i32, i32* %b, align 4
> > %2 = load i32, i32* %c, align 4
> > %3 = load i32, i32* %d, align 4
> > %4 = load i32, i32* %e, align 4
> > %5 = load i32, i32* %f, align 4
> > %arraydecay = getelementptr inbounds [3 x i32], [3 x i32]* %a, i32 0, i32 0
> > call void @test(i32 signext %1, i32 signext %2, i32 signext %3, i32 signext %4, i32 signext %5, i32* %arraydecay)
> > ret i32 0;}
> > I want to known how to compile local array a to a local variable
>
> Your question is not totally clear: a is a “local variable” on the stack, this is the line:
>
> > %a = alloca [3 x i32], align 4
>
> It is initialized here:
>
> > call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)
>
> using the constant data that is stored in a global:
>
> > @main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4
>
> What would you like the code to look like?
>
> —
> Mehdi



|

bg(12-28-15-08-4(12-28-15-09-26).jpg