[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:59:44 PST 2016


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 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/ce42309d/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/ce42309d/attachment-0001.jpg>


More information about the llvm-dev mailing list