<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div>PHIs are lowered by inserting COPYs into each predecessor. Switches only want to generate control flow, nothing else, so the theoretical insertion point for the COPY is above the switch.</div>
<div><br class="">
</div>
<div>It would be possible to do what you suggest by manually splitting the switch up into its constituent compares (producing a binary search tree) with each leaf jumping to the same destination. But in that case each leaf is in a different basic block so you
 don’t have duplicate arcs any more :)</div>
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 25 May 2017, at 16:36, Daniel Berlin <<a href="mailto:dberlin@dberlin.org" class="">dberlin@dberlin.org</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class=""><br class="">
<div class="gmail_extra"><br class="">
<div class="gmail_quote">On Thu, May 25, 2017 at 8:24 AM, James Molloy <span dir="ltr" class="">
<<a href="mailto:James.Molloy@arm.com" target="_blank" class="">James.Molloy@arm.com</a>></span> wrote:<br class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word" class="">[+my non-arm account]
<div class=""><br class="">
</div>
<div class="">Hi,</div>
<div class=""><br class="">
</div>
<div class="">I believe I have a testcase for that too. The key is the word “conflicting” - we can have multiple edges from the same predecessor, but to be well formed all phi values for those arcs must be the same. The verifier checks this, if I recall.<br class="">
</div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">Interesting.  I guess that is because it doesn't know how to map them back to the real edges or something.</div>
<div class=""><br class="">
</div>
<div class="">IE:<br class="">
<div class=""> define i32 @f3(i32 %x) {</div>
<div class=""> bb0:<br class="">
</div>
<div class="">   switch i32 %x, label %bb1 [ </div>
<div class="">i32 0, label %bb2</div>
<div class="">i32 1, label %bb2</div>
<div class="">]</div>
<div class=""> bb1:</div>
<div class="">   br label %bb2</div>
<div class=""> bb2:</div>
<div class="">   %cond = phi i32 [ 0, %bb1 ], [ 0, %bb0 ], [ 1, %bb0 ]</div>
<div class="">   %foo = add i32 %cond, %x</div>
<div class="">   ret i32 %foo</div>
<div class=""> }<br class="">
</div>
</div>
<div class=""> <br class="">
</div>
<div class=""><br class="">
</div>
<div class="">IE %cond = phi i32 [ 0, %bb1 ], [ 0, %bb0 ], [ 1, %bb0 ]</div>
<div class=""><br class="">
</div>
<div class="">should be valid, but you are right, it is not, LLVM requires it to be</div>
<div class="">
<div class=""><br class="">
</div>
<div class=""> %cond = phi i32 [ 0, %bb1 ], [ %x, %bb0 ], [ %x, %bb0 ]</div>
</div>
<div class=""><br class="">
</div>
<div class="">(In the above, i know i could eliminate the add along two paths if i could make a phi node with different values for each of the edges)</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word" class="">
<div class="">I’ll make sure there’s a testcase at least for the full expected behaviour (this pass is off by default at the moment so there’s no panic about trunk miscompiling).</div>
<span class="gmail-HOEnZb"><font color="#888888" class="">
<div class=""><br class="">
</div>
<div class="">James</div>
</font></span>
<div class="">
<div class="gmail-h5">
<div class=""><br class="">
</div>
<div class="">
<div class="">
<blockquote type="cite" class="">
<div class="">On 25 May 2017, at 16:11, Daniel Berlin <<a href="mailto:dberlin@dberlin.org" target="_blank" class="">dberlin@dberlin.org</a>> wrote:</div>
<br class="gmail-m_1444340689649942015Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">(That comment applies mostly to newgvn).
<div class=""><br class="">
</div>
<div class="">In your case, i believe the code is just wrong:<br class="">
<br class="">
</div>
<div class="">
<div class="">/ This assumes the PHI is already well-formed and there aren't conflicting</div>
<div class="">    // incoming values for the same block.</div>
<div class="">    for (auto *B : Blocks)</div>
<div class="">      Values.push_back(PN-><wbr class="">getIncomingValueForBlock(B))</div>
</div>
<div class=""><br class="">
</div>
<div class="">It's 100% completely and definitely possible for a phi to have two values incoming from the same block, actually, regardless of whether it's a self block or not.</div>
<div class="">This happens using switch statements.</div>
<div class="">I believe we even have some testcases in newgvn for multiple edges from same incoming block.</div>
<div class=""><br class="">
</div>
<div class="">So yeah, you have to handle them here.</div>
<div class=""><br class="">
</div>
</div>
<div class="gmail_extra"><br class="">
<div class="gmail_quote">On Thu, May 25, 2017 at 8:08 AM, Daniel Berlin <span dir="ltr" class="">
<<a href="mailto:dberlin@dberlin.org" target="_blank" class="">dberlin@dberlin.org</a>></span> wrote:<br class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr" class="">
<div class="gmail_extra">
<div class="gmail_quote"><span class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<span class=""><br class="">
----------------<br class="">
</span><span class="">dberlin wrote:<br class="">
> I'm curious why you think you need stable sort here as opposed to regular<br class="">
><br class="">
</span>I don't. Changed.<br class="">
<div class="gmail-m_1444340689649942015m_-7927023127036086712HOEnZb">
<div class="gmail-m_1444340689649942015m_-7927023127036086712h5"><br class="">
</div>
</div>
</blockquote>
<div class=""><br class="">
</div>
</span>
<div class="">(i'm offsite today, but someone should test this in newgvn too if i'm right).</div>
<div class=""><br class="">
</div>
<div class="">Do we allow switch statements with multiple edges to ourself?<br class="">
<br class="">
ie</div>
<div class=""><br class="">
</div>
<div class="">bb1:<br class="">
<br class="">
switch <whatever> [</div>
<div class="">i32 0 : label bb1</div>
<div class="">i32 1: label bb1 ]</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">(which, after propagation,  could cause a phi with different operands and the same incoming blocks)</div>
<div class="">If so, either we need stable sorts, or a better ordering of incoming blocks, because the pointer equality we use will not definitely sort them into a consistent order</div>
<div class=""> </div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
<span class="gmail-">IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person,
 use it for any purpose, or store or copy the information in any medium. Thank you.
</span></div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose,
 or store or copy the information in any medium. Thank you.
</body>
</html>