<div dir="ltr"><div><div>Hi Björn and Daniel,<br><br></div>FoldPHIArgOpIntoPHI() is for unary ops (casts), but it calls FoldPHIArgBinOpIntoPHI() for binops which takes care of almost everything else?<br><br></div>My minimal patch idea is to ease the restriction in ShouldChangeType because i1 is special. I tried the patch below, and it works on the example...and nothing in 'make check' failed. :)<br><br>Index: lib/Transforms/InstCombine/InstructionCombining.cpp<br>===================================================================<br>--- lib/Transforms/InstCombine/InstructionCombining.cpp    (revision 293485)<br>+++ lib/Transforms/InstCombine/InstructionCombining.cpp    (working copy)<br>@@ -88,12 +88,12 @@<br> <br> /// Return true if it is desirable to convert an integer computation from a<br> /// given bit width to a new bit width.<br>-/// We don't want to convert from a legal to an illegal type for example or from<br>-/// a smaller to a larger illegal type.<br>+/// We don't want to convert from a legal to an illegal type or from a smaller<br>+/// to a larger illegal type. Width of '1' is always treated as a legal type.<br> bool InstCombiner::ShouldChangeType(unsigned FromWidth,<br>                                     unsigned ToWidth) const {<br>-  bool FromLegal = DL.isLegalInteger(FromWidth);<br>-  bool ToLegal = DL.isLegalInteger(ToWidth);<br>+  bool FromLegal = FromWidth == 1 ? true : DL.isLegalInteger(FromWidth);<br>+  bool ToLegal = ToWidth == 1 ? true : DL.isLegalInteger(ToWidth);<br> <br>   // If this is a legal integer from type, and the result would be an illegal<br>   // type, don't do the transformation.<br>@@ -110,7 +110,7 @@<br> <br> /// Return true if it is desirable to convert a computation from 'From' to 'To'.<br> /// We don't want to convert from a legal to an illegal type for example or from<br>-/// a smaller to a larger illegal type.<br>+/// a smaller to a larger illegal type. i1 is always treated as a legal type.<br> bool InstCombiner::ShouldChangeType(Type *From, Type *To) const {<br>   assert(From->isIntegerTy() && To->isIntegerTy());<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 30, 2017 at 1:22 PM, Björn Steinbrink <span dir="ltr"><<a href="mailto:bsteinbr@gmail.com" target="_blank">bsteinbr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hi Sanjay,<br><br></div>unfortunately that patch does not help in my case. Here's the IR that fails to get fully optimized:<br><br>    target datalayout = "e-m:e-i64:64-f80:128-n8:16:<wbr>32:64-S128"<br>    target triple = "x86_64-unknown-linux-gnu"<br><br>    define fastcc zeroext i1 @testfunc(i8** noalias nocapture readonly dereferenceable(8)) unnamed_addr {<br>    entry-block:<br>      %1 = load i8*, i8** %0, align 8<br>      %2 = icmp ne i8* %1, null<br>      %.mux = zext i1 %2 to i8<br>      br i1 %2, label %bb10, label %bb15<br><br>    bb10:                         <wbr>                    ; preds = %entry-block<br>      %3 = load i8, i8* %1, align 1<br>      %4 = icmp eq i8 %3, 42<br>      %.1 = zext i1 %4 to i8<br>      br label %bb15<br><br>    bb15:                         <wbr>                    ; preds = %entry-block, %bb10<br>      %_0.1 = phi i8 [ %.mux, %entry-block ], [ %.1, %bb10 ]<br>      %5 = icmp ne i8 %_0.1, 0<br>      ret i1 %5<br>    }<br><br></div>The zext instructions should be folded into the phi, and then the new zext gets removed along with the icmp instruction at the end.<span class="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Björn<br></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-01-30 20:20 GMT+01:00 Sanjay Patel <span dir="ltr"><<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>I'm looking at a similar problem in:<br><a href="https://reviews.llvm.org/D28625" target="_blank">https://reviews.llvm.org/D2862<wbr>5</a><br><br></div>Does that patch make any difference on the cases that you are looking at?<br><br></div>Instead of avoiding ShouldChangeType with zext as a special-case opcode, it might be better to treat i1 as a special-case type. There's no way to avoid i1 in IR, so we might as well allow transforming to that type?<br><br>I'm not sure yet, but there's a chance that change might induce problems (infinite loops) with this:<br><a href="https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L374" target="_blank">https://github.com/llvm-mirror<wbr>/llvm/blob/master/lib/Transfor<wbr>ms/InstCombine/InstCombineSimp<wbr>lifyDemanded.cpp#L374</a><br><div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="m_7329589995694897273h5">On Sun, Jan 29, 2017 at 3:09 PM, Björn Steinbrink via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="m_7329589995694897273h5"><div dir="ltr"><div><div><div><div>Hi,<br><br></div>AFAICT there are two places where zext instructions may get folded into PHI nodes. One is FoldPHIArgZextsIntoPHI and the other is the more generic FoldPHIArgOpIntoPHI. Now, the former only handles PHIs with more than 2 incoming values, while the latter only handles casts where the source type is legal.<br><br>This means that for an PHI node with two incoming i8 values, both resulting from `zext i1 * to i8` instructions, both of these functions will refuse to actually fold the zext into the PHI, while the same operation would be performed if there were three or more arms. We noticed this because we saw a optimization regression when a function got specialized and the PHI node only had two incoming values left.<br><br></div>Since I'm not fully aware of any implications this might have, I wonder what is the right way to fix this? Looking at FoldPHIArgZextsIntoPHI, it seems that making the check for `ShouldChangeType` in FoldPHIArgOpIntoPHI conditional on the cast instruction not being a zext instruction. Does that sound right, or am I missing something here?<br><br></div>Thanks<span class="m_7329589995694897273m_-3943333840047646804HOEnZb"><font color="#888888"><br></font></span></div><span class="m_7329589995694897273m_-3943333840047646804HOEnZb"><font color="#888888">Björn<br></font></span></div>
<br></div></div>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>