<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class="">Hi,</div>
<div class=""><br class="">
</div>
<div class="">Firstly, overall I think perfection shouldn't stand in the way of progress and that GVNSink is a good improvement on the really bad bisimulation algorithm in SimplifyCFG. Do you have any objections to it going in-tree and I can work on improving/rewriting
 the algorithm there, after I remove the old SimplifyCFG algorithm?</div>
<div class=""><br class="">
</div>
<div class="">I'm afraid I don't fully follow you. What compounds this is that you've referred to this (inverted or post value numbering) being in the literature somewhere, but I've totally failed to find it. Do you have any starting pointers?</div>
<div class=""><br class="">
</div>
<div class="">Consider the following example, which is one of the simple motivating testcases for the creation of GVNSink (it doesn't show all the features of GVNSink by a long way, but it does illustrate a problem):</div>
<div class=""><br class="">
</div>
<div class=""><font face="Menlo" class="">bb1:</font></div>
<div class=""><font face="Menlo" class="">  x1 = load %p</font></div>
<div class=""><font face="Menlo" class="">  x2 = add x1, %g</font></div>
<div class=""><font face="Menlo" class="">  x3 = add x2, 5</font></div>
<div class=""><font face="Menlo" class="">  x4 = add x3, %g2</font></div>
<div class=""><font face="Menlo" class="">  store x4, %p</font></div>
<div class=""><font face="Menlo" class="">  goto bb3</font></div>
<div class=""><font face="Menlo" class=""><br class="">
</font></div>
<div class=""><font face="Menlo" class="">bb2:</font></div>
<div class=""><font face="Menlo" class="">  y1 = load %p</font></div>
<div class=""><font face="Menlo" class="">  y2 = add y1, %g</font></div>
<div class=""><font face="Menlo" class="">  y3 = add y2, 42</font></div>
<div class=""><font face="Menlo" class="">  y4 = add y3, %g2</font></div>
<div class=""><font face="Menlo" class="">  store y4, %p</font></div>
<div class=""><font face="Menlo" class="">  goto bb3</font></div>
<div class=""><br class="">
</div>
<div class="">In this example all instructions can be sunk to bb3 with just one phi(5, 42). I'm trying to understand how your suggestion would work. You said:</div>
<div class=""><br class="">
</div>
<div class="">"(in particular, you will create a phi for any value with two different leaders at a join point you must pass to sink it. If you calculate this properly, and in topo order of expressions, such that you process operands before things that use them,
 you can calculate the number of phis you would insert in O(N) time overall)"</div>
<div class=""><br class="">
</div>
<div class="">My difficulty is that I can't think of a value numbering function that wouldn't diverge at {x3, y3}. Consider a "normal" value numbering in which VN[x] == VN[y] iff congruent(x, y):</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><font face="Menlo" class="">bb1:</font></div>
<div class=""><font face="Menlo" class="">  x1 = load %p     [VN=1]</font></div>
<div class=""><font face="Menlo" class="">  x2 = add x1, %g  [VN=2]</font></div>
<div class=""><font face="Menlo" class="">  x3 = add x2, 5   [VN=3]</font></div>
<div class=""><font face="Menlo" class="">  x4 = add x3, %g2 [VN=4]</font></div>
<div class=""><font face="Menlo" class="">  store x4, %p     [VN=5]</font></div>
<div class=""><font face="Menlo" class="">  goto bb3</font></div>
<div class=""><font face="Menlo" class=""><br class="">
</font></div>
<div class=""><font face="Menlo" class="">bb2:</font></div>
<div class=""><font face="Menlo" class="">  y1 = load %p     [VN=1]</font></div>
<div class=""><font face="Menlo" class="">  y2 = add y1, %g  [VN=2]</font></div>
<div class=""><font face="Menlo" class="">  y3 = add y2, 42  [VN=6]  <-- 42 != 5, so VN[y3] != VN[x3]</font></div>
<div class=""><font face="Menlo" class="">  y4 = add y3, %g2 [VN=7]</font></div>
<div class=""><font face="Menlo" class="">  store y4, %p     [VN=8]  <-- value numbers have diverged</font></div>
<div class=""><font face="Menlo" class="">  goto bb3</font></div>
<div class=""><br class="">
</div>
<div class="">Now consider an inverse value numbering in which VN[x] == VN[y] => x or y's uses can be replaced with phi(x, y):</div>
<div class=""><br class="">
</div>
<div class=""><font face="Menlo" class="">bb1:</font></div>
<div class=""><font face="Menlo" class="">  x1 = load %p     [VN=7]  <-- value numbers have diverged</font></div>
<div class=""><font face="Menlo" class="">  x2 = add x1, %g  [VN=6]  <-- x2 cannot be used the same as y2; different constants get added</font></div>
<div class=""><font face="Menlo" class="">  x3 = add x2, 5   [VN=3]  <-- x3 is used the same as y3</font></div>
<div class=""><font face="Menlo" class="">  x4 = add x3, %g2 [VN=2]</font></div>
<div class=""><font face="Menlo" class="">  store x4, %p     [VN=1]</font></div>
<div class=""><font face="Menlo" class="">  goto bb3</font></div>
<div class=""><font face="Menlo" class=""><br class="">
</font></div>
<div class=""><font face="Menlo" class="">bb2:</font></div>
<div class=""><font face="Menlo" class="">  y1 = load %p     [VN=5]</font></div>
<div class=""><font face="Menlo" class="">  y2 = add y1, %g  [VN=4]</font></div>
<div class=""><font face="Menlo" class="">  y3 = add y2, 42  [VN=3]</font></div>
<div class=""><font face="Menlo" class="">  y4 = add y3, %g2 [VN=2]</font></div>
<div class=""><font face="Menlo" class="">  store y4, %p     [VN=1]</font></div>
<div class=""><font face="Menlo" class="">  goto bb3</font></div>
<div class=""><br class="">
</div>
<div class="">Both value numberings notice the similarity of only half the sinkable instructions.</div>
<div class=""><br class="">
</div>
<div class="">The way I get around this in GVNSink is to define VN[x] as an *optimistic* function that only takes into account the opcodes and number of uses. This allows it to number x2 and y2 the same, even though they could not actually replace each other.
 The value numbering is used purely as a tool to narrow the search space - bisimulation (or n-simulation!) takes over after that to ensure validity.</div>
<div class=""><br class="">
</div>
<div class="">The way I'm thinking about this problem, there is surely no such value numbering function that will indicate when a PHI is needed and will not diverge at that point. Therefore, I must be thinking about the problem differently (wrongly) to you.</div>
<div class=""><br class="">
</div>
<div class="">Could you please enlighten me or maybe point me at papers?</div>
<div class=""><br class="">
</div>
<div class="">Cheers, and apologies for the slowness in picking this up.</div>
<div class=""><br class="">
</div>
<div class="">James</div>
<div>
<blockquote type="cite" class="">
<div class="">On 22 May 2017, at 17:46, 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 Mon, May 22, 2017 at 8:41 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br class="">
<br class="">
Thanks for the quick reply; this is a very helpful and constructive discussion :)<br class="">
</blockquote>
<div class=""><br class="">
</div>
<div class="">Happy to help :)</div>
<div class=""> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br class="">
The oversimplification and repetition below is for my own understanding and so that I can better understand your replies, as you know this subject far better than I.<br class="">
<span class=""><br class="">
A: store 1<br class="">
B: store 2<br class="">
C: store 3<br class="">
</span>D:<br class="">
<br class="">
I understand that in a classical sinking algorithm we'd want to walk backwards:<br class="">
  1) Sink 'store 3' from C to D<br class="">
  2) Sink 'store 2' from B to D<br class="">
  3) Sink 'store 1' from A to D<br class="">
<br class="">
A key to this working is that each instruction, when considered, was sunk as far as it could be. If we had added the restriction that an instruction could only be sunk to its immediate successor block, that algorithm would have taken three iterations to reach
 a fixpoint.<br class="">
</blockquote>
<div class="">Yes</div>
<div class=""> <br class="">
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br class="">
GVNSink can (at the moment at least) only sink instructions from a block into its immediate successor. This is what allows it to model accurately enough how many PHIs it is going to create and whether sinking is actually worthwhile (and how much sinking)</blockquote>
<div class=""><br class="">
</div>
<div class="">How much is the former, and how much is the later?</div>
<div class=""><br class="">
</div>
<div class="">The number of phis you can create is calculable ahead of time in O(N) time for all operands, and related the availability of values</div>
<div class="">(in particular, you will create a phi for any value with two different leaders at a join point you must pass to sink it. If you calculate this properly, and in topo order of expressions, such that you process operands before things that use them,
 you can calculate the number of phis you would insert in O(N) time overall)</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
. I don't believe it would be able to handle all the cases it currently does if it didn't have this restriction (but would be able to handle other cases that it currently can't, so I see that very much as an enhancement I'd like to add later).<br class="">
</blockquote>
<div class=""><br class="">
</div>
<div class="">I actually don't buy this, fwiw.</div>
<div class="">It's entirely possible to calculate all the hypothetical insertion points ahead of time, and figure out which make sense from a cost perspective.</div>
<div class="">There are sinking (and PRE) algorithms that do this.<br class="">
You could do exactly the same cases you do now.</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Now, whether you want that complexity is a different question :)</div>
<div class=""><br class="">
</div>
</div>
</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>