<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 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{mso-style-priority:99;
mso-style-link:"Plain Text Char";
margin:0cm;
margin-bottom:.0001pt;
font-size:10.5pt;
font-family:"Calibri",sans-serif;}
span.PlainTextChar
{mso-style-name:"Plain Text Char";
mso-style-priority:99;
mso-style-link:"Plain Text";
font-family:"Calibri",sans-serif;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle20
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle21
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle22
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:#1F497D;}
span.EmailStyle23
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@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="ZH-CN" link="#0563C1" vlink="#954F72" style="text-justify-trim:punctuation">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="color:#1F497D">Ok, thank you, I will implement it then.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="color:#1F497D">As far as I see this optimization should be done in AArch64LoadStoreOptimizer, is it right?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" align="left" style="text-align:left"><b><span lang="EN-US" style="font-size:11.0pt">From:</span></b><span lang="EN-US" style="font-size:11.0pt"> Eli Friedman [mailto:efriedma@quicinc.com]
<br>
<b>Sent:</b> Thursday, November 21, 2019 11:55 PM<br>
<b>To:</b> Kosov Pavel <kosov.pavel@huawei.com>; LLVM Dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> RE: [llvm-dev] [ARM] Peephole optimization ( instructions tst + add )<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt">That transform is legal; it’s a missed optimization.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt">-Eli<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" align="left" style="text-align:left"><b><span lang="EN-US" style="font-size:11.0pt">From:</span></b><span lang="EN-US" style="font-size:11.0pt"> llvm-dev <</span><span lang="EN-US"><a href="mailto:llvm-dev-bounces@lists.llvm.org"><span style="font-size:11.0pt">llvm-dev-bounces@lists.llvm.org</span></a></span><span lang="EN-US" style="font-size:11.0pt">>
<b>On Behalf Of </b>Kosov Pavel via llvm-dev<br>
<b>Sent:</b> Thursday, November 21, 2019 2:00 AM<br>
<b>To:</b> </span><span lang="EN-US"><a href="mailto:llvm-dev@lists.llvm.org"><span style="font-size:11.0pt">llvm-dev@lists.llvm.org</span></a></span><span lang="EN-US" style="font-size:11.0pt"><br>
<b>Subject:</b> [EXT] [llvm-dev] [ARM] Peephole optimization ( instructions tst + add )<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Hello! <o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">I noticed that in some cases clang generates sequence of AND+TST instructions:<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">For example:<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> AND x3, x2, x1<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> TST x2, x1<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">I think these instructions should be merged to one:<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> ANDS x3, x2, x1<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">( because TST <Xn>, <Xm> is alias for ANDS XZR, <Xn>, <Xm> -
<a href="https://static.docs.arm.com/ddi0596/a/DDI_0596_ARM_a64_instruction_set_architecture.pdf">
https://static.docs.arm.com/ddi0596/a/DDI_0596_ARM_a64_instruction_set_architecture.pdf</a> )<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Is it missing optimization or there could be some negative effect from such merge?<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Best regards<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Pavel<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">PS: Code sample (though it may be significantly reduced):<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">(clang -target aarch64 sample.c -S -O2 -o sample.S )
<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">=========================================================================<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">#define NULL ((void*)0)<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">typedef struct {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> unsigned long * res_in;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> unsigned long * proc;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> <o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> } fd_set_bits;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">fd_set_bits *gv_fds;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">int g_max_i;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">int LOOP_ITERS_COUNT;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">unsigned DEF_MASK;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">__attribute__((noinline)) int do_test(const int max_iters_count,<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> const unsigned long in,
<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> const unsigned long out,
<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> const unsigned long ex,<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> const unsigned long bit_init_val,<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> const unsigned long mask) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> int retval = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> for(int k =0 ; k < max_iters_count; k++)
<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> fd_set_bits *fds = gv_fds;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> for(int j = 0; j < LOOP_ITERS_COUNT; ++j)
<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> if (in) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> retval++;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> fds->proc = NULL;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> <o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> if (mask & DEF_MASK) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> fds->proc = NULL;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> return retval;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">}<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">=========================================================================<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</div>
</body>
</html>