<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=utf-8"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@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=FR link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>+CC llvm-commits<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Arnaud A. de Grandmaison [mailto:arnaud.degrandmaison@arm.com] <br><b>Sent:</b> 09 September 2015 23:37<br><b>To:</b> 'David Majnemer'<br><b>Cc:</b> Jakub Kuderski<br><b>Subject:</b> RE: [llvm] r246997 - There is a trunc(lshr (zext A), Cst) optimization in InstCombineCasts that<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>The whole point of the patch is that we know which bits are actually sign bits. I think the line<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>+      return CastInst::CreateIntegerCast(Shift, CI.getType(), false);<br>should instead be<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>    </span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>return CastInst::CreateIntegerCast(Shift, CI.getType(), true);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>because we otherwise loose the sign info.This is also what the comment describing the transformation says we want to do.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I missed that when reviewing the patch. The attached patch fix the issue (and adds David’s testcase).<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Does that look reasonable to you David ?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Cheers,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Arnaud<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> llvm-commits [<a href="mailto:llvm-commits-bounces@lists.llvm.org">mailto:llvm-commits-bounces@lists.llvm.org</a>] <b>On Behalf Of </b>David Majnemer via llvm-commits<br><b>Sent:</b> 09 September 2015 22:22<br><b>To:</b> Jakub Kuderski<br><b>Cc:</b> llvm-commits<br><b>Subject:</b> Re: [llvm] r246997 - There is a trunc(lshr (zext A), Cst) optimization in InstCombineCasts that<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>This patch wrongly transforms:<o:p></o:p></p><div><div><p class=MsoNormal>define i16 @f(i8 %V) {<o:p></o:p></p></div><div><p class=MsoNormal>  %conv = sext i8 %V to i32<o:p></o:p></p></div><div><p class=MsoNormal>  %shr.1.i = lshr i32 %conv, 1<o:p></o:p></p></div><div><p class=MsoNormal>  %conv.i = trunc i32 %shr.1.i to i16<o:p></o:p></p></div><div><p class=MsoNormal>  ret i16 %conv.i<o:p></o:p></p></div><div><p class=MsoNormal>}<o:p></o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>into:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><p class=MsoNormal>define i16 @f(i8 %V) {<o:p></o:p></p></div><div><p class=MsoNormal>  %shr.1.i = ashr i8 %V, 1<o:p></o:p></p></div><div><p class=MsoNormal>  %conv.i = zext i8 %shr.1.i to i16<o:p></o:p></p></div><div><p class=MsoNormal>  ret i16 %conv.i<o:p></o:p></p></div><div><p class=MsoNormal>}<o:p></o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>If %V is 0x80, %conv.i should be 0xFFC0 but your transform will make it 0x00C0.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>This is the root cause of PR24763 and has been reverted in r247180.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>On Tue, Sep 8, 2015 at 3:03 AM, Jakub Kuderski via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<o:p></o:p></p><p class=MsoNormal>Author: kuhar<br>Date: Tue Sep  8 05:03:17 2015<br>New Revision: 246997<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=246997&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=246997&view=rev</a><br>Log:<br>There is a trunc(lshr (zext A), Cst) optimization in InstCombineCasts that<br>removes cast by performing the lshr on smaller types. However, currently there<br>is no trunc(lshr (sext A), Cst) variant.<br>This patch add such optimization by transforming trunc(lshr (sext A), Cst)<br>to ashr A, Cst.<br><br>Differential Revision: <a href="http://reviews.llvm.org/D12520" target="_blank">http://reviews.llvm.org/D12520</a><br><br>Modified:<br>    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp<br>    llvm/trunk/test/Transforms/InstCombine/cast.ll<br><br>Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=246997&r1=246996&r2=246997&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=246997&r1=246996&r2=246997&view=diff</a><br>==============================================================================<br>--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)<br>+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Tue Sep  8 05:03:17 2015<br>@@ -499,6 +499,26 @@ Instruction *InstCombiner::visitTrunc(Tr<br>     return CastInst::CreateIntegerCast(Shift, CI.getType(), false);<br>   }<br><br>+  // Transform trunc(lshr (sext A), Cst) to ashr A, Cst to eliminate type<br>+  // conversion.<br>+  // It works because bits coming from sign extension have the same value as<br>+  // sign bit of the original value; performing ashr instead of lshr<br>+  // generates bits of the same value as the sign bit.<br>+  if (Src->hasOneUse() &&<br>+      match(Src, m_LShr(m_SExt(m_Value(A)), m_ConstantInt(Cst))) &&<br>+      cast<Instruction>(Src)->getOperand(0)->hasOneUse()) {<br>+    const unsigned ASize = A->getType()->getPrimitiveSizeInBits();<br>+    // This optimization can be only performed when zero bits generated by<br>+    // the original lshr aren't pulled into the value after truncation, so we<br>+    // can only shift by values smaller then the size of destination type (in<br>+    // bits).<br>+    if (Cst->getValue().ult(ASize)) {<br>+      Value *Shift = Builder->CreateAShr(A, Cst->getZExtValue());<br>+      Shift->takeName(Src);<br>+      return CastInst::CreateIntegerCast(Shift, CI.getType(), false);<br>+    }<br>+  }<br>+<br>   // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest<br>   // type isn't non-native.<br>   if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) &&<br><br>Modified: llvm/trunk/test/Transforms/InstCombine/cast.ll<br>URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast.ll?rev=246997&r1=246996&r2=246997&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast.ll?rev=246997&r1=246996&r2=246997&view=diff</a><br>==============================================================================<br>--- llvm/trunk/test/Transforms/InstCombine/cast.ll (original)<br>+++ llvm/trunk/test/Transforms/InstCombine/cast.ll Tue Sep  8 05:03:17 2015<br>@@ -1062,6 +1062,43 @@ define i8 @test85(i32 %a) {<br> ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8<br> }<br><br>+define i16 @test86(i16 %v) {<br>+  %a = sext i16 %v to i32<br>+  %s = ashr i32 %a, 4<br>+  %t = trunc i32 %s to i16<br>+  ret i16 %t<br>+<br>+; CHECK-LABEL: @test86(<br>+; CHECK:  [[ASHR:%.*]] = ashr i16 %v, 4<br>+; CHECK-NEXT: ret i16 [[ASHR]]<br>+}<br>+<br>+define i16 @test87(i16 %v) {<br>+  %c = sext i16 %v to i32<br>+  %m = mul nsw i32 %c, 16<br>+  %a = ashr i32 %m, 16<br>+  %t = trunc i32 %a to i16<br>+  ret i16 %t<br>+<br>+; CHECK-LABEL: @test87(<br>+; CHECK:  [[ASHR:%.*]] = ashr i16 %v, 12<br>+; CHECK-NEXT: ret i16 [[ASHR]]<br>+}<br>+<br>+define i16 @test88(i16 %v) {<br>+  %a = sext i16 %v to i32<br>+  %s = ashr i32 %a, 18<br>+  %t = trunc i32 %s to i16<br>+  ret i16 %t<br>+<br>+; Do not optimize to ashr i16 (shift by 18)<br>+; CHECK-LABEL: @test88(<br>+; CHECK: [[SEXT:%.*]] = sext i16 %v to i32<br>+; CHECK-NEXT: [[ASHR:%.*]] = ashr i32 [[SEXT]], 18<br>+; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ASHR]] to i16<br>+; CHECK-NEXT: ret i16 [[TRUNC]]<br>+}<br>+<br> ; Overflow on a float to int or int to float conversion is undefined (PR21130).<br><br> define i8 @overflow_fptosi() {<br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p></div></div></div></div></body></html>