<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:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
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;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
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"><div class=WordSection1><p class=MsoNormal>Hi, All:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I have a testcase which produced incorrect result, it’s caused by the combination of nsw flag and ExtLdPromotion, I am leaning to say Clang set nsw flag incorrectly, but please let me know if I was wrong.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Here is the reduced testcase:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>long long foo(int *a)<o:p></o:p></p><p class=MsoNormal>{<o:p></o:p></p><p class=MsoNormal>long long c;<o:p></o:p></p><p class=MsoNormal>c = *a * 1405;<o:p></o:p></p><p class=MsoNormal>return c;<o:p></o:p></p><p class=MsoNormal>}<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Clang emitted the following IR (It is done by EmitMUL() in CGExprScalar.cpp, while CGF.getLangOpts().getSignedOverflowBehavior()=LangOptions::SOB_Undefined and CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)=false):<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>; Function Attrs: nounwind readonly<o:p></o:p></p><p class=MsoNormal>define i64 @foo(i32* nocapture readonly %a) #0 {<o:p></o:p></p><p class=MsoNormal>entry:<o:p></o:p></p><p class=MsoNormal> %0 = load i32* %a, align 4, !tbaa !1<o:p></o:p></p><p class=MsoNormal> %mul = mul nsw i32 %0, 1405<o:p></o:p></p><p class=MsoNormal> %conv = sext i32 %mul to i64<o:p></o:p></p><p class=MsoNormal> ret i64 %conv<o:p></o:p></p><p class=MsoNormal>}<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Question 1: Is it reasonable to say “mul” is “No Signed Wrap” ? *a *1405 could overflow though.<o:p></o:p></p><p class=MsoNormal>Question 2: Why SignedOverflowBehavior is set to LangOptions::SOB_Undefined by default?<o:p></o:p></p><p class=MsoNormal>Question 3: What about CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Because of nsw, and with Quentin patch (git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233753 91177308-0d34-0410-b5e6-96231b3b80d8), ExtLdPromotion() in CodeGenPrepare.cpp is able promote the sext to the following:<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'>define i64 @foo(i32* nocapture readonly %a) #0 {<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'>entry:<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'> %0 = load i32* %a, align 4, !tbaa !1<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'> %conv = sext i32 %0 to i64<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'> %mul = mul nsw i64 %conv, 1405<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'> ret i64 %mul<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'>}<o:p></o:p></p><p class=MsoNormal style='text-indent:9.6pt'><o:p> </o:p></p><p class=MsoNormal>This promotion itself looks fine to me if nsw is true, and the final code becomes:<o:p></o:p></p><p class=MsoNormal> ldrsw x8, [x0]<o:p></o:p></p><p class=MsoNormal> movz w9, #0x57d<o:p></o:p></p><p class=MsoNormal> mul x0, x8, x9<o:p></o:p></p><p class=MsoNormal> ret<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The results is different from a 32-bit mul then sext, at least for my testcase.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Without nsw, ExtLdPromotion() didn’t change anything, and the result is correct.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Any thoughts would be helpful.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Regards<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Lawrence Hu<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal style='text-indent:9.6pt'><o:p> </o:p></p><p class=MsoNormal style='text-indent:9.6pt'><o:p> </o:p></p><p class=MsoNormal style='text-indent:9.6pt'><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>