<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><style>body { line-height: 1.5; }blockquote { margin-top: 0px; margin-bottom: 0px; margin-left: 0.5em; }body { font-size: 10.5pt; font-family: 微软雅黑; color: rgb(0, 0, 0); line-height: 1.5; }</style></head><body>
<table width="99.99%" height="100%" style="padding: 10px; background-color: transparent;" border="0" cellpadding="0" cellspacing="0" background="cid:_Foxmail.1@f85b5281-5f86-815f-6bd3-5ba9d779c53d">
<tbody><tr>
<td valign="top" style="width:100%;height:100%;">
<div style="">
<div><span></span>Hello,<span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;">Mehdi</span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> 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.</span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> 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. </span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> 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.</span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> Then I see the assemble file , I find </span></div><span style="font-size: 14px; background-color: rgba(0, 0, 0, 0);"><span style="color: rgb(0, 0, 0); white-space: pre;"> </span><font color="#3366ff">.type<span style="white-space: pre;"> </span>$main.a,@object # @main.a<br><span style="white-space: pre;"> </span>.section<span style="white-space: pre;"> </span>.rodata,"a",@progbits</font><br><span style="color: rgb(0, 0, 0); white-space: pre;"> </span>.align<span style="color: rgb(0, 0, 0); white-space: pre;"> </span>2<br>$main.a:<br><span style="color: rgb(0, 0, 0); white-space: pre;"> </span>.4byte<span style="color: rgb(0, 0, 0); white-space: pre;"> </span>1 # 0x1<br><span style="color: rgb(0, 0, 0); white-space: pre;"> </span>.4byte<span style="color: rgb(0, 0, 0); white-space: pre;"> </span>2 # 0x2<br><span style="color: rgb(0, 0, 0); white-space: pre;"> </span>.4byte<span style="color: rgb(0, 0, 0); white-space: pre;"> </span>3 # 0x3<br><span style="color: rgb(0, 0, 0); white-space: pre;"> </span>.size<span style="color: rgb(0, 0, 0); white-space: pre;"> </span>$main.a, 12<br><br></span><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> the "main.a" is in the rotata section </span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> 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.</span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> Now it works perfect. </span></div><div><span style="background-color: transparent; font-size: 10.5pt; line-height: 1.5;"> Thank you for your patient and help ,my English is not very good,Thank you .</span></div>
<div><br></div><hr style="width: 210px; height: 1px;" color="#b5c4df" size="1" align="left">
<div><span><div style="margin: 10px; font-size: 10pt;"><div style="font-size: 29px;"><font face="华文行楷">刘钰</font></div></div></span></div>
<blockquote style="margin-top: 0px; margin-bottom: 0px; margin-left: 0.5em;"><div> </div><div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm"><div style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px; FONT-SIZE: 12px;FONT-FAMILY:tahoma;COLOR:#000000; BACKGROUND: #efefef; PADDING-BOTTOM: 8px; PADDING-TOP: 8px"><div><b>From:</b> <a href="mailto:mehdi.amini@apple.com">Mehdi Amini</a></div><div><b>Date:</b> 2016-12-28 15:17</div><div><b>To:</b> <a href="mailto:liuyu11@ict.ac.cn">liuyu11@ict.ac.cn</a></div><div><b>CC:</b> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev</a>; <a href="mailto:llvm-dev-request@lists.llvm.org">llvm-dev-request</a></div><div><b>Subject:</b> Re: [llvm-dev] why clang compile local to global</div></div></div><div><div> </div>
<div>> On Dec 27, 2016, at 11:09 PM, liuyu11@ict.ac.cn via llvm-dev <llvm-dev@lists.llvm.org> wrote:</div>
<div>> </div>
<div>> Hello,everyone:</div>
<div>> I want to known how to let clang compile my local array to local variables:</div>
<div>> I have the code :</div>
<div>> int main()</div>
<div>> {</div>
<div>> int a[3]={1,2,3};</div>
<div>> </div>
<div>> int b=7;</div>
<div>> int c=8;</div>
<div>> int d=9;</div>
<div>> int e=10;</div>
<div>> int f=11;</div>
<div>> test(b,c,d,e,f,a);</div>
<div>> return 0;</div>
<div>> }</div>
<div>> I use clang command:clang --target=mipsel -emit-llvm -S a.c -o a.ll</div>
<div>> The a.ll is:</div>
<div>> @main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4</div>
<div>> define i32 @main() #0 {</div>
<div>> entry:</div>
<div>> %retval = alloca i32, align 4</div>
<div>> %a = alloca [3 x i32], align 4</div>
<div>> %b = alloca i32, align 4</div>
<div>> %c = alloca i32, align 4</div>
<div>> %d = alloca i32, align 4</div>
<div>> %e = alloca i32, align 4</div>
<div>> %f = alloca i32, align 4</div>
<div>> store i32 0, i32* %retval</div>
<div>> %0 = bitcast [3 x i32]* %a to i8*</div>
<div>> call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)</div>
<div>> store i32 7, i32* %b, align 4</div>
<div>> store i32 8, i32* %c, align 4</div>
<div>> store i32 9, i32* %d, align 4</div>
<div>> store i32 10, i32* %e, align 4</div>
<div>> store i32 11, i32* %f, align 4</div>
<div>> %1 = load i32, i32* %b, align 4</div>
<div>> %2 = load i32, i32* %c, align 4</div>
<div>> %3 = load i32, i32* %d, align 4</div>
<div>> %4 = load i32, i32* %e, align 4</div>
<div>> %5 = load i32, i32* %f, align 4</div>
<div>> %arraydecay = getelementptr inbounds [3 x i32], [3 x i32]* %a, i32 0, i32 0</div>
<div>> call void @test(i32 signext %1, i32 signext %2, i32 signext %3, i32 signext %4, i32 signext %5, i32* %arraydecay)</div>
<div>> ret i32 0;}</div>
<div>> I want to known how to compile local array a to a local variable</div>
<div> </div>
<div>Your question is not totally clear: a *is* a “local variable” on the stack, this is the line:</div>
<div> </div>
<div>> %a = alloca [3 x i32], align 4</div>
<div> </div>
<div>It is initialized here:</div>
<div> </div>
<div>> call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([3 x i32]* @main.a to i8*), i32 12, i32 4, i1 false)</div>
<div> </div>
<div>using the constant data that is stored in a global:</div>
<div> </div>
<div>> @main.a = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 3], align 4</div>
<div> </div>
<div>What would you like the code to look like?</div>
<div> </div>
<div>— </div>
<div>Mehdi</div>
<div> </div>
</div></blockquote>
<div></div></div>
</td>
</tr>
</tbody></table>
</body></html>