<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;
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;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle25
{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">You probably want to do this some time before register allocation, so you don’t have to worry about physical register definitions.<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">Maybe take a look at what ARM does in ARMBaseInstrInfo::optimizeCompareInstr ?<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"> Kosov Pavel <kosov.pavel@huawei.com>
<br>
<b>Sent:</b> Friday, November 22, 2019 3:09 AM<br>
<b>To:</b> Eli Friedman <efriedma@quicinc.com>; LLVM Dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [EXT] 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"><o:p> </o:p></p>
<p class="MsoNormal"><span style="color:#1F497D;mso-fareast-language:ZH-CN">Ok, thank you, I will implement it then.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D;mso-fareast-language:ZH-CN">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 style="color:#1F497D;mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D;mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<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;mso-fareast-language:ZH-CN">From:</span></b><span style="font-size:11.0pt;mso-fareast-language:ZH-CN"> Eli Friedman [<a href="mailto:efriedma@quicinc.com">mailto:efriedma@quicinc.com</a>]
<br>
<b>Sent:</b> Thursday, November 21, 2019 11:55 PM<br>
<b>To:</b> Kosov Pavel <<a href="mailto:kosov.pavel@huawei.com">kosov.pavel@huawei.com</a>>; LLVM Dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><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 style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:ZH-CN">That transform is legal; it’s a missed optimization.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:ZH-CN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:ZH-CN">-Eli<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:ZH-CN"><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;mso-fareast-language:ZH-CN">From:</span></b><span style="font-size:11.0pt;mso-fareast-language:ZH-CN"> llvm-dev <</span><span style="mso-fareast-language:ZH-CN"><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 style="font-size:11.0pt;mso-fareast-language:ZH-CN">>
<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 style="mso-fareast-language:ZH-CN"><a href="mailto:llvm-dev@lists.llvm.org"><span style="font-size:11.0pt">llvm-dev@lists.llvm.org</span></a></span><span style="font-size:11.0pt;mso-fareast-language:ZH-CN"><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 style="mso-fareast-language:ZH-CN"><o:p> </o:p></span></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>
</div>
</body>
</html>