<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:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ligatures:standardcontextual;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
mso-fareast-language:EN-US;}
@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-AU" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hello,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Commit 6b2e4fa4 (main) entitled “* arm: "OpCmp2OpS" moved to Pass 2 so it doesn't conflict with AND; CMP -> TST optimisation” by Gareth from Feb 11 2024 produces incorrect assembler in certain cases.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><a href="https://gitlab.com/freepascal.org/fpc/source/-/commit/6b2e4fa4133a496c1c3f89e3c71fffbdd7c192fb">https://gitlab.com/freepascal.org/fpc/source/-/commit/6b2e4fa4133a496c1c3f89e3c71fffbdd7c192fb</a><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">This piece of code:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">function CPUMaskCount(CPUMask:LongWord):LongWord;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">var<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Count:LongWord;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">begin<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">{}<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Result:=0;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> for Count:=CPU_ID_0 to CPU_ID_MAX do<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> begin<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> if (CPUMask and (1 shl Count)) <> 0 then<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> begin<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> Inc(Result);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> end;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> end;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">end;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">when compiled with FPC prior to commit 6b2e4fa4 produces the following working assembler:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">00020528 <GLOBALCONFIG_$$_CPUMASKCOUNT$LONGWORD$$LONGWORD>:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20528: e1a01000 mov r1, r0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 2052c: e3a00000 mov r0, #0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20530: e3a02000 mov r2, #0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20534: e3a03001 mov r3, #1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20538: e0113213 ands r3, r1, r3, lsl r2<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 2053c: 12800001 addne r0, r0, #1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20540: e2822001 add r2, r2, #1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20544: e352001f cmp r2, #31<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20548: 9afffff9 bls 20534 <GLOBALCONFIG_$$_CPUMASKCOUNT$LONGWORD$$LONGWORD+0xc><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 2054c: e12fff1e bx lr<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">But when compiled with FPC after commit 6b2e4fa4 it produces this assembler which doesn’t work:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">00020528 <GLOBALCONFIG_$$_CPUMASKCOUNT$LONGWORD$$LONGWORD>:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20528: e1a01000 mov r1, r0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 2052c: e3a00000 mov r0, #0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20530: e3a02000 mov r2, #0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20534: e3a03001 mov r3, #1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20538: e1110003 tst r1, r3<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 2053c: 12800001 addne r0, r0, #1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20540: e2822001 add r2, r2, #1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20544: e352001f cmp r2, #31<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 20548: 9afffff9 bls 20534 <GLOBALCONFIG_$$_CPUMASKCOUNT$LONGWORD$$LONGWORD+0xc><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> 2054c: e12fff1e bx lr<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">You can see that the difference is the lack of lsl r2 on the end of the TST instruction which means that the shl on the original code is not being performed and the test is therefore invalid.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Similar code sequences in multiple other places produce the same result with the lsl suffix missing from the TST instruction.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Please let me know if you need any further information.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Garry Wood.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</body>
</html>