<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns="http://www.w3.org/TR/REC-html40" 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"><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META content="MSHTML 6.00.6001.18023" name=GENERATOR>
<STYLE>@font-face {
font-family: Cambria Math;
}
@font-face {
font-family: Calibri;
}
@page Section1 {size: 612.0pt 792.0pt; margin: 72.0pt 72.0pt 72.0pt 72.0pt; }
P.MsoNormal {
FONT-SIZE: 11pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Calibri","sans-serif"
}
LI.MsoNormal {
FONT-SIZE: 11pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Calibri","sans-serif"
}
DIV.MsoNormal {
FONT-SIZE: 11pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Calibri","sans-serif"
}
A:link {
COLOR: blue; TEXT-DECORATION: underline; mso-style-priority: 99
}
SPAN.MsoHyperlink {
COLOR: blue; TEXT-DECORATION: underline; mso-style-priority: 99
}
A:visited {
COLOR: purple; TEXT-DECORATION: underline; mso-style-priority: 99
}
SPAN.MsoHyperlinkFollowed {
COLOR: purple; TEXT-DECORATION: underline; mso-style-priority: 99
}
SPAN.EmailStyle17 {
COLOR: windowtext; FONT-FAMILY: "Calibri","sans-serif"; mso-style-type: personal-compose
}
.MsoChpDefault {
mso-style-type: export-only
}
DIV.Section1 {
page: Section1
}
</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 vLink=purple link=blue>
<DIV><SPAN class=916494612-27052008><FONT face=Arial color=#0000ff
size=2>Nicolas:</FONT></SPAN></DIV>
<DIV><SPAN class=916494612-27052008><FONT face=Arial color=#0000ff size=2>The
LLVM mail server is being slow, so I'm direct e-mailing my comment. The
generated code is IEEE correct...use CreateFCmpUEQ for unordered or equal, which
should generate your hand written version....I'm assuming that your next e-mail
(min/max) is probably the same thing.</FONT></SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV></DIV>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left><FONT
face=Tahoma size=2>-----Original Message-----<BR><B>From:</B>
llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu] <B>On Behalf
Of </B>Nicolas Capens<BR><B>Sent:</B> Tuesday, May 27, 2008 11:10
AM<BR><B>To:</B> 'LLVM Developers Mailing List'<BR><B>Subject:</B> [LLVMdev]
Float compare-for-equality and select
optimizationopportunity<BR><BR></FONT></DIV>
<DIV class=Section1>
<P class=MsoNormal>Hi all,<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>I’m trying to generate code containing an ordered float
compare for equality, and select. The resulting code however has an unordered
compare and some Boolean logic that I think could be eliminated. In C syntax
the code looks like this:<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">float x,
y;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">int a, b,
c<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">if(x == y) //
Rotate the integers<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">{<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
int t;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p> </o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
t = a;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
a = b;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
b = c;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
c = t;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">}</SPAN><o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>This is the resulting x86 assembly code:<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">movss
xmm0,dword ptr [ecx+4] <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN lang=NL-BE
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">ucomiss
xmm0,dword ptr [ecx+8] <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN lang=NL-BE
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">sete
al <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">setnp
dl <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">test
dl,al <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">mov
edx,edi <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmovne
edx,ecx <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmovne
ecx,esi <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmovne
esi,edi</SPAN><o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>While I’m pleasantly surprised that my branch does get
turned into several select operations as intended (cmov - conditional move –
in x86), I’m confused why it uses the ucomiss instruction (unordered compare
and set flags). I only used IRBuilder::CreateFCmpOEQ. It also appears to
invert the conditional, for no clear reason. I think it could be rewritten as
follows:<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">movss
xmm0,dword ptr [ecx+4] <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">comiss
xmm0,dword ptr [ecx+8] <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">mov
edx,edi <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmove
edx,ecx <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmove
ecx,esi <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmove
esi,edi</SPAN><o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>Compared to the original C syntax code this looks pretty
straightforward. Curiously, when I replace the compare-for-equality with
something like a less-than, it does generate such compact code (using <SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">comiss</SPAN>
and <SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmova</SPAN>).
And the not-equal case looks like this:<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">movss
xmm0,dword ptr [ecx+4] <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">ucomiss
xmm0,dword ptr [ecx+8] <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">mov
esi,ecx <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmove
esi,edx <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmovne
ecx,eax <o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36pt"><SPAN
style="FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: 'Courier New'">cmove
edx,eax</SPAN><o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>So this generates compact code but with an unordered
compare.<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>Anyway, it looks like the compare-for-equality case in
particular is missing an optimization opportunity. It’s no big deal to me but
I thought someone here might be interested…<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>Cheers,<o:p></o:p></P>
<P class=MsoNormal><o:p> </o:p></P>
<P class=MsoNormal>Nicolas
Capens<o:p></o:p></P></DIV></BLOCKQUOTE></BODY></HTML>