[llvm-dev] why clang compile local to global

liuyu11@ict.ac.cn via llvm-dev llvm-dev at lists.llvm.org
Wed Dec 28 01:42:29 PST 2016


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, at object         # @main.a
.section .rodata,"a", at 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 at 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 at ict.ac.cn via llvm-dev <llvm-dev at 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
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161228/4690270a/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bg.jpg
Type: image/jpeg
Size: 71938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161228/4690270a/attachment-0001.jpg>


More information about the llvm-dev mailing list