<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-GB link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Indeed, that stack space is still being allocated, just at a later time. Previously, it was<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>allocated in code called from the TableGen’d calling convention code, which resulted<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>in the arg reg save area being included in the calculation of alignment for later arguments,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>which is incorrect. I have modified it so that this space is allocated in<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>ARMTargetLowering::LowerFormalArguments, which prevents this problem.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Also, this area was previously allocated above the stack pointer, in the area which<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>should be owned by the callee, and an extra stack pointer adjustment was inserted by<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>the frame lowering code to make space. Now it is allocated below the stack pointer,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>and included in the calculation of the stack frame size, which conceptually makes more<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>sense (though it makes no difference to the generated code).<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Oliver<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Manman Ren [mailto:manman.ren@gmail.com] <br><b>Sent:</b> 04 March 2014 18:05<br><b>To:</b> reviews+D2933+public+f1a32e707ae60633@llvm-reviews.chandlerc.com<br><b>Cc:</b> renato.golin@linaro.org; Oliver Stannard; Tim Northover; llvm-commits@cs.uiuc.edu<br><b>Subject:</b> Re: [PATCH] Correctly align arguments after a byval struct is passed on the stack<o:p></o:p></span></p></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal style='margin-bottom:12.0pt'><o:p> </o:p></p><div><p class=MsoNormal>On Tue, Mar 4, 2014 at 3:36 AM, Oliver Stannard <<a href="mailto:oliver.stannard@arm.com" target="_blank">oliver.stannard@arm.com</a>> wrote:<o:p></o:p></p><p class=MsoNormal>Hi rengolin,<br><br>This patch fixes a bug that would cause an 8-byte-aligned function argument<br>to be consistently misaligned in some situations involving byval arguments.<br>The following function signature is one of these cases:<br><br>%struct12bytes = type { i32, i32, i32 }<br>define void @foo(i32 %a, %struct12bytes* byval %b, i64 %c) {<br><br>When arguments are initially been allocated, %b has 12 bytes reserved on the<br>stack, but this is later adjusted to use registers r1..r3 instead of the<br>stack. However, the 12 bytes are still used when calculating the alignment<br>of %c, causing %c to be allocated 4 bytes above the top of %b, at stack<br>offset 0x10. The stack pointer is moved down 12 bytes at the beginning of<br>the function  to make space for the part of %b which is in registers (all of<br>it in this example), plus another 4 bytes to maintain 8-byte SP alignment.<br>However, the 4 bytes of alignment gap between %b and %c are still present,<br>resulting in %c being loaded from SP+0x14, which is not 8-byte aligned.<br><br>The solution to this is to not allocate stack space for the part of a byval<br>which will be passed in registers.<o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Maybe I didn't get the whole picture, but I think we need to allocate stack space  for<o:p></o:p></p></div><div><p class=MsoNormal>the part of a byval that will be passed in registers, so the part passed in registers<o:p></o:p></p></div><div><p class=MsoNormal>and the part passed on stack will be laid out contiguously on stack.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Thanks,<o:p></o:p></p></div><div><p class=MsoNormal>Manman<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal style='margin-bottom:12.0pt'>This requires some extra work to<br>calculate the location of byval arguments on the stack, especially in the<br>case of multiple small byvals in registers, as previously the stack<br>locations were used to record the location of a byval after storing to the<br>stack.<br><br>This bug can be triggered from C, using a similar test case, but argument b<br>must be larger than 64 bytes to cause clang to emit a byval argument.<br><br>I have put this patch through a night of random PCS testing with no failures.<br><br><a href="http://llvm-reviews.chandlerc.com/D2933" target="_blank">http://llvm-reviews.chandlerc.com/D2933</a><br><br>Files:<br>  include/llvm/CodeGen/CallingConvLower.h<br>  lib/Target/ARM/ARMFrameLowering.cpp<br>  lib/Target/ARM/ARMISelLowering.cpp<br>  lib/Target/ARM/ARMISelLowering.h<br>  lib/Target/ARM/Thumb1FrameLowering.cpp<br>  test/CodeGen/ARM/2013-04-05-Small-ByVal-Structs-PR15293.ll<br>  test/CodeGen/ARM/2013-05-02-AAPCS-ByVal-Structs-C4-C5-VFP.ll<br>  test/CodeGen/ARM/2014-02-21-byval-reg-split-alignment.ll<br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><o:p></o:p></p></blockquote></div><p class=MsoNormal><o:p> </o:p></p></div></div></div></body></html>