<div dir="ltr">I have a WIP implementation here: <a href="https://reviews.llvm.org/D69868">https://reviews.llvm.org/D69868</a>. It only supports use of returned values on the "normal" path. Use on the abnormal path is very complex and may not be needed. If people clamor for it, then we can always add it later after further review.<div><br></div><div>PTAL!</div><div>-bw</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 2, 2019 at 3:35 PM Finkel, Hal J. <<a href="mailto:hfinkel@anl.gov">hfinkel@anl.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div bgcolor="#FFFFFF">
<div>On 7/2/19 12:42 AM, Bill Wendling wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">On Mon, Jul 1, 2019 at 6:25 PM Finkel, Hal J. <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>> wrote:<br>
</div>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div bgcolor="#FFFFFF">
<p>On 7/1/19 1:38 PM, Bill Wendling via llvm-dev wrote:<br>
</p>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">On Fri, Jun 28, 2019 at 3:35 PM James Y Knight <<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.com</a>> wrote:<br>
</div>
<div class="gmail_quote">
<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">
<div dir="ltr">On Fri, Jun 28, 2019 at 5:53 PM Bill Wendling <<a href="mailto:isanbard@gmail.com" target="_blank">isanbard@gmail.com</a>> wrote:<br>
</div>
<div class="gmail_quote">
<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">
<div dir="ltr">On Fri, Jun 28, 2019 at 1:48 PM James Y Knight <<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.com</a>> wrote:<br>
</div>
<div class="gmail_quote">
<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">
<div dir="ltr">On Fri, Jun 28, 2019 at 3:00 PM Bill Wendling <<a href="mailto:isanbard@gmail.com" target="_blank">isanbard@gmail.com</a>> wrote:<br>
</div>
<div class="gmail_quote">
<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">
<div dir="ltr">On Thu, Jun 27, 2019 at 1:44 PM Bill Wendling <<a href="mailto:isanbard@gmail.com" target="_blank">isanbard@gmail.com</a>> wrote:<br>
</div>
<div class="gmail_quote">
<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">
<div dir="ltr">On Thu, Jun 27, 2019 at 1:29 PM James Y Knight <<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.com</a>> wrote:<br>
</div>
<div class="gmail_quote">
<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">I think this is fine, except that it stops at the point where things actually start to get interesting and tricky.
<div><br>
</div>
<div>How will you actually handle the flow of values from the callbr into the error blocks? A callbr can specify requirements on where its outputs live. So, what if two callbr, in different branches of code, specify _different_ constraints for the same output,
 and list the same block as a possible error successor? How can the resulting phi be codegened?</div>
<div><br>
</div>
</div>
</blockquote>
<div>This is where I fall back on the statement about how "the programmer knows what they're doing". Perhaps I'm being too cavalier here? My concern, if you want to call it that, is that we don't be too restrictive on the new behavior. For example, the "asm
 goto" may set a register to an error value (made up on the spot; may not be a common use). But, if there's no real reason to have the value be valid on the abnormal path, then sure we can declare that it's not valid on the abnormal path.</div>
<div><br>
</div>
</div>
</div>
</blockquote>
<div>I think I should explain my "programmer knows what they're doing" statement a bit better. I'm specifically referring to inline asm here. The more general "callbr" case may still need to be considered (see Reid's reply).</div>
<div><br>
</div>
<div>When a programmer uses inline asm, they're implicitly telling the compiler that they *do* know what they're doing  (I know this is common knowledge, but I wanted to reiterate it.). In particular, either they need to reference an instruction not readily
 available from the compiler (e.g. "cpuid") or the compiler isn't able to give them the needed performance in a critical section. I'm extending this sentiment to callbr with output constraints. Let's take your example below and write it as "normal" asm statements
 one on each branch of an if-then-else (please ignore any syntax errors):</div>
<div><br>
</div>
<div>if:</div>
<div>  br i1 %cmp, label %true, label %false</div>
<div><br>
</div>
<div>true:</div>
<div>
<div>  %0 = call { i32, i32 } asm sideeffect "poetry $0, $1", "={r8},={r9}" ()<br>
</div>
<div>  br label %end</div>
<div><br>
</div>
<div>false:<br>
</div>
<div>  %1 = call { i32, i32 } asm sideeffect "poetry2 $0, $1", "={r10},={r11}" ()</div>
<div>  br label %end</div>
<div><br>
</div>
<div>end:</div>
<div>  %vals = phi { i32, i32 } [ %0, %true ], [ %1, %false ]</div>
</div>
<div><br>
</div>
<div>How is this handled in codegen? Is it an error or does the back-end handle it? Whatever's done today for "normal" inline asm is what I *think* should be the behavior for the inline asm callbr variant. If this doesn't seem sensible (and I realize that I
 may be thinking of an "in a perfect world" scenario), then we'll need to come up with a more sensible solution which may be to disallow the values on the error block until we can think of a better way to handle them.</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>This example is no problem, because instructions can be emitted between what's emitted by "call asm" and the end of the block (be it a fallthrough, or a jump instruction. What gets emitted there is a move of the output register to another location -- either
 a register or to the stack. And therefore at the beginning of the "end" block, "%vals" is always in a consistent location, no matter how you got to that block.</div>
<div><br>
</div>
<div>But in the callbr case, there is not a location at which those moves can be emitted, after the callbr, before the jump to "error".</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>I see what you mean. Let's say we create a pseudo-instruction (similar to landingpad, et al) that needs to be lowered by the backend in a reasonable manner. The EH stuff has an external process/library that performs the actual unwinding and which sets
 the values accordingly. We won't have this.</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div> </div>
<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">
<div class="gmail_quote">
<div>What we could do instead is split the edges and insert the copy-to-<where ever> statements there.
</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>Exactly -- except that doing that is potentially an invalid transform, because the address is being used as a value, not simply a jump target. The label list is just a list of _possible_ jump targets, changing those won't actually affect anything. You'd
 instead need to change the blockaddress constant, but in the general case you don't know where that address came from -- (and it may therefore be required that you have the same address for two separate callbr instructions).</div>
<div><br>
</div>
<div>I guess this kinda touches on some of the same issues as in the other discussion about the handling of the blockaddress in callbr and inlining, etc...</div>
<div><br>
</div>
<div>I wonder if we could put some validity restrictions on the IR structure, rather than trying to fix things up after the fact by attempting to split blocks. E.g., we could state that it's invalid to have a phi which uses the value defined by a callbr, if
 it's conditioned on that same block as predecessor.  That is: it's valid to use _other_ values defined in the block ending in callbr, because they can be moved prior to the callbr. It's also valid to use the value defined by the callbr in a phi conditioned
 on some other intermediate block as predecessor, because then any required moves can happen in the intermediate block.</div>
<div><br>
</div>
<div>I believe such an IR restriction should be sufficient to make it possible to emit valid code from the IR in all cases, but I'm a bit afraid of how badly adding such odd edge-cases might screw up the rest of the compiler and optimizer.</div>
</div>
</div>
</blockquote>
<div>
<div dir="ltr">
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><br>
</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><font face="arial, sans-serif">I want to revisit this. </font></span>Here are the situations we're confronted with:</p>
<ol>
<li>The goto-target can be jumped to by 1 callbr instruction,</li><li>The goto-target can be jumped to by N callbr instructions, which don't need a PHI node, and</li><li>The goto-target can be jumped to by N callbr instructions, which *do* need a PHI node.</li></ol>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><font face="arial, sans-serif">I'm going to plug the instruction I created out of thin air a few emails back, but better explain (I'm using an instruction instead of an intrinsic because
 we want that instruction to be right after all non-PHI instructions in the goto-target block). I'm _not_ suggesting we need this instruction. It's just for demonstration purposes.</font></span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><font face="arial, sans-serif"><br>
</font></span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><font face="arial, sans-serif">Situations (1) and (2) don't encounter an problem. Any value used in the goto-target can be handled by inserting the code to extract that value in the
 goto-target block:</font></span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><font face="arial, sans-serif"><br>
</font></span></p>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">bb1:</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">  %x.bb1 = callbr i32 asm sideeffect "...", "=r,X"(i32
</span><b style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">%x</b><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, i8* blockaddress(@bar, %goto.target))</span><br>
</p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><span>          </span>to label %fallthrough1
 [label %goto.target]</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">fallthrough1:</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">bb2:</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">  %y.bb2 = callbr i32 asm sideeffect "...", "=r,X"(i32
</span><b style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">%y</b><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, i8* blockaddress(@bar, %goto.target))</span><br>
</p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><span>          </span>to label %fallthrough2
 [label %goto.target]</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">fallthrough2:</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<br>
</p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">goto.target:</span></p>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
  %x.goto = <extract value from %x.bb1></p>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
  %y.goto = <extract value from %y.bb2></p>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
  ... <uses of %x.goto and %y.goto> ...</p>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<br>
</p>
</div>
</div>
</div>
</div>
</blockquote>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><font face="arial, sans-serif">This leaves situation (3), which is far more complex as we've seen. To reiterate, the issue here is that we need to extract the values returned by callbr.
 This would typically be done by using a PHI node, but llvm may want to split critical edges or push the calculation back to the predecessor block, which won't work with the callbr asm, because it could branch out of the asm at any point thus skipping the extraction.
 So we can't use PHI nodes for these values. There are three classes of solutions to this:</font></span></p>
<ol>
<li>Don't allow the values to be used in goto-targets, or</li><li>Allow them, but with significant restrictions, or</li><li>Allow them without using PHI nodes.</li></ol>
<div>Each has its benefits and drawbacks. As I've stated before, I think that (1) is too restrictive, but if we can't come up with a good solution, it may be our only option. Solution (2) could be a good compromise. However, I want to propose a potential solution
 to (3).</div>
<div><br>
The core of my proposal is to replace the PHI node with code that will replicate its behavior without code lowering trying to modify the CFG (at least not in ways that may invalidate the asm). Here is example code:</div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)"><br>
</span></p>
</div>
</div>
</div>
</div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)">bb1:</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(34,34,34)">  </span><span style="font-variant-ligatures:no-common-ligatures">store i8* blockaddress(@bar, %bb1), i8** %src</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<span style="font-variant-ligatures:no-common-ligatures">  %x.bb1 = callbr i32 asm sideeffect "...", "=r,X"(i32 %x, i8* blockaddress(@bar, %goto.target))</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><span>          </span>to label %fallthrough1 [label
 %goto.target]</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">fallthrough1:</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">bb2:</span></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures">store i8* blockaddress(@bar, %bb2), i8** %src</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  %x.bb2 = callbr i32 asm sideeffect "...", "=r,X"(i32 %x, i8* blockaddress(@bar, %goto.target))</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><span>          </span>to label %fallthrough2 [label
 %goto.target]</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">fallthrough2:</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<br>
</p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">goto.target:</span></p>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
  %x1 = indirectval i8** %src, i32 [%x.bb1, %bb1], [%x.bb2, %bb2]</p>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
  <extract values from %x1></p>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;color:rgb(0,0,0)">
  ...</p>
</div>
</div>
</div>
</div>
</blockquote>
<font color="#000000">
<div style="font-family:Inconsolata"><br>
</div>
</font>
<div><font color="#000000"><font face="arial,
                        sans-serif">This can be lowered to this:</font><br>
</font>
<div class="gmail_quote">
<div><br>
</div>
</div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">bb1:</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures">store i8* blockaddress(@bar, %bb1), i8** %src</span></p>
</div>
</div>
</div>
</div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">  %x.bb1 = callbr i32 asm sideeffect "...", "=r,X"(i32 %x, i8* blockaddress(@bar, %error))</span><br>
</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><span>          </span>to label %fallthrough1 [label
 %goto.target]</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">fallthrough1:</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">bb2:</span></p>
</div>
</div>
<div class="gmail_quote">
<div class="gmail_quote">
<div>
<div>
<div class="gmail_quote">
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px">
<span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures">store i8* blockaddress(@bar, %bb2), i8** %src</span></p>
</div>
</div>
</div>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  %x.bb2 = callbr i32 asm sideeffect "...", "=r,X"(i32 %x, i8* blockaddress(@bar, %error))</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><span>          </span>to label %fallthrough2 [label
 %goto.target]</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">fallthrough2:</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ...</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<br>
</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">goto.target:</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  %src1 = load i8**, i8*** </span><span style="font-variant-ligatures:no-common-ligatures">@src</span><br>
</p>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  %<a href="http://src.bb" target="_blank">src.bb</a> = load i8*, i8** %src1</p>
</div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  switch i64 %<a href="http://src.bb" target="_blank">src.bb</a>, label %goto.target.body [ ; or if-then-else
 blocks</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">      i64 ptrtoint i8* blockaddress(@bar, %bb1) to i64, label %goto.target.bb1</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">      i64 ptrtoint i8* blockaddress(@bar, %bb2) to i64, label %goto.target.bb2</span><span style="font-variant-ligatures:no-common-ligatures"> 
  </span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">  ]</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures"><br>
</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<span style="font-variant-ligatures:no-common-ligatures">goto.target.bb1:</span></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  %x1 = <extract value from %x.bb1></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  br label %goto.target.body</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<br>
</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
goto.target.bb2:</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  %x2 = <extract value from %x.bb2></p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  br label %goto.target.body</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<br>
</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
goto.target.body:</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  %x.merge = phi i64 [%x1, label %goto.target.bb1], [%x1, label %goto.target.bb2]</p>
</div>
</div>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
  ...</p>
</div>
</div>
</blockquote>
<div class="gmail_quote">
<div>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;font-family:Inconsolata;margin:0px;color:rgb(0,0,0)">
<br>
</p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px;color:rgb(0,0,0)">
<font face="arial, sans-serif">With this, we don't change any values used by the callbr instructions, and the return values are extracted correctly. This has the unsavory issue of using stores and loads, but this may be the price we need to pay.</font></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px;color:rgb(0,0,0)">
<font face="arial, sans-serif"><br>
</font></p>
<p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal;margin:0px;color:rgb(0,0,0)">
<font face="arial, sans-serif">Thoughts?</font></p>
</div>
</div>
</div>
</div>
</blockquote>
<p><br>
</p>
<p>The non-fallthrough blocks can have other predecessors, right? If so, I imagine that you need to also do the following:</p>
</div>
</blockquote>
<div>Good point! <br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div bgcolor="#FFFFFF">
<p> 1. Store zero (or -1 or some other distinguishable value) into the %src alloca in the entry block.</p>
</div>
</blockquote>
<div>It should be a null value, as that's not a valid block address. Then again, if we use the "switch" instruction the default branch should suffice. We will probably want to reset the value after the callbr values are extracted.</div>
</div>
</div>
</blockquote>
<p><br>
</p>
<p>Actually, I'm not sure this works because the code in "<extract value from %x.bb1>" must be dominated by %x.bb1, and in this case it's not.<br>
</p>
<p><br>
</p>
<blockquote type="cite">
<div dir="ltr">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div bgcolor="#FFFFFF">
<p> 2. Store this same distinguishable value into the %src alloca after the "value extraction" is performed.</p>
<p> 3. Include this distinguishable value in the switch statement.</p>
<p>While Clang does not normally insert phi nodes, in this case perhaps the problem is self-contained enough for this to be reasonable. However, I'm not sure that this is worthwhile. This is a performance feature generally, and if the user really wants to use
 these outputs, are they going to want the extra expense of the branches and jump tables and all of the rest of it? Maybe in the common case the extraction blocks will be trivial and get merged, but the default case will still be problematic?</p>
</div>
</blockquote>
<div>There are ways to avoid the branches, et al, mostly by writing the code in the form of situations (1) and (2) by using lead-in blocks:</div>
<div><br>
</div>
</div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div><span style="font-family:Inconsolata;font-variant-ligatures:no-common-ligatures">true_branch:</span></div>
</div>
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata">  goto body;</span></div>
</div>
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata"><br>
</span></div>
</div>
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata">false_branch:</span></div>
</div>
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata">  goto body;</span></div>
</div>
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata"><br>
</span></div>
</div>
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata">body:</span></div>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div class="gmail_quote">
<div><span style="font-variant-ligatures:no-common-ligatures;font-family:Inconsolata">  <use of common values here></span></div>
</div>
</blockquote>
</div>
</blockquote>
<p><br>
</p>
<p>One specific concern is that if it turns out that the non-fallthrough-block is not reachable except via callbrs, then you'd want the optimizer to be able to eliminate the checks for the "null value" case.</p>
<p> -Hal<br>
</p>
<p><br>
</p>
<blockquote type="cite">
<div dir="ltr">
<div class="gmail_quote">
<div><br>
</div>
<div>-bw </div>
</div>
</div>
</blockquote>
<pre cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</div>

</blockquote></div>