<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:"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;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        text-align:justify;
        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:0in;
        margin-bottom:.0001pt;
        font-size:10.5pt;
        font-family:"Calibri",sans-serif;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:11.0pt;
        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;}
span.EmailStyle20
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
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-US" link="#0563C1" vlink="#954F72" style="text-justify-trim:punctuation">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">That transform is legal; it’s a missed optimization.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">-Eli<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" align="left" style="text-align:left"><b><span style="font-size:11.0pt">From:</span></b><span style="font-size:11.0pt"> llvm-dev <llvm-dev-bounces@lists.llvm.org>
<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> llvm-dev@lists.llvm.org<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"><o:p> </o:p></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">Hello! <o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">I noticed that in some cases clang generates sequence of AND+TST instructions:<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">For example:<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">       AND          x3, x2, x1<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">         TST            x2, x1<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">I think these instructions should be merged to one:<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">         ANDS       x3, x2, x1<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">( 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 style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">Is it missing optimization or there could be some negative effect from such merge?<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">Best regards<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">Pavel<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">PS: Code sample (though it may be significantly reduced):<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">(clang -target aarch64 sample.c -S -O2 -o sample.S )
<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">=========================================================================<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">#define NULL ((void*)0)<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">typedef struct {<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    unsigned long * res_in;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    unsigned long * proc;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    <o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    } fd_set_bits;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">fd_set_bits *gv_fds;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">int g_max_i;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">int LOOP_ITERS_COUNT;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">unsigned DEF_MASK;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">__attribute__((noinline)) int do_test(const int max_iters_count,<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">                                        const unsigned long in,
<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">                                        const unsigned long out,
<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">                                        const unsigned long ex,<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">                                        const unsigned long bit_init_val,<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">                                        const unsigned long mask) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    int retval = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    for(int k =0 ; k < max_iters_count; k++)
<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    {<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">        fd_set_bits *fds = gv_fds;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">        for(int j = 0; j < LOOP_ITERS_COUNT; ++j)
<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">        {<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">          if (in) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">            retval++;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">            fds->proc = NULL;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">          }<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">                    
<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">          if (mask & DEF_MASK) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">            fds->proc = NULL;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">          }<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">        }<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">    }<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">         return retval;<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">}<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="mso-fareast-language:ZH-CN">=========================================================================<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
</div>
</div>
</body>
</html>