<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.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.PlainTextChar
{mso-style-name:"Plain Text Char";
mso-style-priority:99;
mso-style-link:"Plain Text";
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
/* Page Definitions */
@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="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>
</body>
</html>