For this C code,<div><div><br></div><div>struct s {</div><div> int a[40];</div><div>};</div><div><br></div><div>void g(struct s a) {</div><div> a.a[0] = 4;</div><div>}</div><div><br></div><div>void f() {</div><div> struct s a;</div>
<div> g(a);</div><div>}</div><div><br></div><div>clang generates llvm IR:</div><div><br></div><div><div>define void @g(%struct.s* byval %a) nounwind {</div><div>entry:</div><div> %tmp = getelementptr inbounds %struct.s* %a, i32 0, i32 0 ; <[40 x i32]*> [#uses=1]</div>
<div> %arraydecay = getelementptr inbounds [40 x i32]* %tmp, i32 0, i32 0 ; <i32*> [#uses=1]</div><div> %arrayidx = getelementptr inbounds i32* %arraydecay, i64 0 ; <i32*> [#uses=1]</div><div> store i32 4, i32* %arrayidx</div>
<div> ret void</div><div>}</div><div><br></div><div>define void @f() nounwind {</div><div>entry:</div><div> %a = alloca %struct.s, align 4 ; <%struct.s*> [#uses=1]</div><div> %agg.tmp = alloca %struct.s ; <%struct.s*> [#uses=2]</div>
<div> %tmp = bitcast %struct.s* %agg.tmp to i8* ; <i8*> [#uses=1]</div><div> %tmp1 = bitcast %struct.s* %a to i8* ; <i8*> [#uses=1]</div><div> call void @llvm.memcpy.i64(i8* %tmp, i8* %tmp1, i64 160, i32 4)</div>
<div> call void @g(%struct.s* byval %agg.tmp)</div><div> ret void</div><div>}</div><div><br></div><div>Since we have already alloca'ed a temporary struct.s %agg.tmp, why is there still a 'byval' in g's parameter? The consequence of this is when assembly code is generated, we end up with allocating 3 structs on the stack:</div>
<div><br></div><div><div>f:</div><div>.Leh_func_begin2:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>pushq<span class="Apple-tab-span" style="white-space:pre"> </span>%rbp</div><div>.Llabel3:</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>movq<span class="Apple-tab-span" style="white-space:pre"> </span>%rsp, %rbp</div><div>.Llabel4:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>subq<span class="Apple-tab-span" style="white-space:pre"> </span>$496, %rsp</div>
<div><br></div><div><br></div><div>Could somebody explain the rationale behind this behavior? Thanks.</div><div><br></div></div></div></div>