<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">You're right, we would hoist the add to someplace before the notional indirectbr, but that needs to work anyway. After looking more closely at the IR, we would probably hoist into the landingpad instead of into the entry block. Here's the IR I have in my head after optimization and hoisting but before outlining:</div><div class="gmail_quote"><br></div><div class="gmail_quote">define i32 @foo(i32 %a, i32 %b) {</div><div class="gmail_quote">entry:</div><div class="gmail_quote">  invoke void @maybe_throw()</div><div class="gmail_quote">      to label %ret unwind label %lpad</div><div class="gmail_quote">ret:</div><div class="gmail_quote">  ret i32 0 ; The return case</div><div class="gmail_quote"><br></div><div class="gmail_quote">lpad:</div><div class="gmail_quote">  %ehvals = landingpad { i8*, i32 } personality ...</div><div class="gmail_quote">      catch ... ; typeinfo for int</div><div class="gmail_quote">      catch ... ; typeinfo for float<br></div><div class="gmail_quote">  %sum = add i32 %a, %b   ; hoisted out from catch_int and catch_float via CSE or something<br></div><div class="gmail_quote">  ... ; dispatch on the selector, effectively doing a switch</div><div class="gmail_quote"><br></div><div class="gmail_quote">catch_int:</div><div class="gmail_quote">  ret i32 %sum</div><div class="gmail_quote"><br></div><div class="gmail_quote"><div class="gmail_quote">catch_float:</div><div class="gmail_quote">  %s1 = add i32 %sum, 1</div><div class="gmail_quote">  ret i32 %s1</div><div class="gmail_quote"><br></div><div class="gmail_quote">... ; resume</div>}</div><div class="gmail_quote"><br></div><div class="gmail_quote">We have a couple options during EH preparation:</div><div class="gmail_quote">1. Outline the add into a cleanup, store the result into the frameallocation, and access it in the catch handler</div><div class="gmail_quote">2. Sink the add back into the handlers, which we can do because it has no side effects (better)</div><div class="gmail_quote">3. Sink the add past the catch handlers and past the notional indirectbr into the blocks referenced by the handler return result</div><div class="gmail_quote"><br></div><div class="gmail_quote">Number 3 is probably the best, and it would look like:</div><div class="gmail_quote"><br></div><div class="gmail_quote">define i8* @int_handler(i8*, i8*) {</div><div class="gmail_quote">  ret i8* basicblockaddr(@func, %catch_int)<br>}</div><div class="gmail_quote"><div class="gmail_quote">define i8* @float_handler(i8*, i8*) {</div><div class="gmail_quote">  ret i8* basicblockaddr(@func, %catch_float)<br>}</div><div class="gmail_quote"><div class="gmail_quote">define i32 @foo(i32 %a, i32 %b) {</div><div class="gmail_quote">entry:</div><div class="gmail_quote">  invoke void @maybe_throw()</div><div class="gmail_quote">      to label %ret unwind label %lpad</div><div class="gmail_quote">ret:</div><div class="gmail_quote">  ret i32 0 ; The return case</div><div class="gmail_quote"><br></div><div class="gmail_quote">lpad:</div><div class="gmail_quote">  %ehvals = landingpad { i8*, i32 } personality ...</div><div class="gmail_quote">      catch ... ; typeinfo for int</div><div class="gmail_quote">      catch ... ; typeinfo for float<br></div><div class="gmail_quote">  %rejoin_at = call i8* @llvm.eh.actions(... @int_handler, ... @float_handler, ...)</div><div class="gmail_quote">  indirectbr i8* %rejoin_at, [ label %catch_int, label %catch_float ]</div><div class="gmail_quote"><br></div><div class="gmail_quote">catch_int:</div><div class="gmail_quote">  %s1 = add i32 %a, %b<br></div><div class="gmail_quote">  ret i32 %s1</div><div class="gmail_quote"><br></div><div class="gmail_quote"><div class="gmail_quote">catch_float:</div><div class="gmail_quote">  %s2 = add i32 %a, %b<br></div><div class="gmail_quote">  %s3 = add i32 %s2, 1</div><div class="gmail_quote">  ret i32 %s3</div>}</div></div><div class="gmail_quote"><br></div><div>But even if we aren't smart enough to do #3, which could implement #1 by additional outlining. It's ugly though. =/</div><div><br></div><div>In practice, I don't think this situation will occur very often because the catch blocks look like this:</div><div><br></div><div>catch_int:</div><div>  call void @llvm.eh.begincatch(i8* %ehptr)</div><div>  ... ; user code</div><div>  call void @llvm.eh.endcatch()</div><div>  ... ; rejoin normal control</div><div><br></div><div>Anything we can hoist out of "user code" here can usually be sunk back in. If it's not trivial like a hoisted store to an unescaped internal global, then we'll have to emit something that looks like a destructor cleanup.</div></div><div class="gmail_quote"><br></div><div class="gmail_quote">On Thu, Feb 12, 2015 at 7:43 AM, Joseph Tremoulet <span dir="ltr"><<a href="mailto:jotrem@microsoft.com" target="_blank">jotrem@microsoft.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div><span>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">>
</span>We'd have to hoist a + b to somewhere that dominates L1 and L2. I think the only BB in your program that dominates is the entry block<span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u><u></u></span></p>
</span><p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">I don't follow.  What path do you see from entry to either L1 or L2 that doesn't pass through the indirectbr?  In order to reach either L1 or L2, the call to
 maybe_throw() must raise an exception (else we'd return 0 from foo), the exception must be caught by one of the two handlers (else we'd unwind out of foo), and one of the outlined handlers must have executed and returned.  Don't those conditions correspond
 to the path from entry to the indirectbr?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">To clarify, I'm not trying to assert there's a problem; I'm new to LLVM and trying to understand the model.  I've worked before on a compiler that similarly
 used stand-in code to model control flow effected by the runtime/unwinder, and we had these issues with code motion.  Our system was closed enough that we could just have the affected optimizations check for the relevant opcodes (we were doing the equivalent
 of using a special exitlandingpad terminator instead of indirectbr) and avoid pushing code across them; I don't know if a similar approach is appropriate in LLVM, or if there is (or should be) a way to annotate the block to indicate that it's one of these
 cases and new code inserted there would get skipped over at run-time, or if it's ok to just assume that those sort of optimizations don't run after EH Preparation, or what.  Even if I am misunderstanding the shape of the flow graph in my example as you suggest,
 is there no cause for concern that some post-outlining pass might try to insert code into that block?  Like maybe some sort of late profile instrumentation?  It's great if there's not; I'm just trying to understand why not.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">Thanks<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">-Joseph<u></u><u></u></span></p>
<p class="MsoNormal"><a name="14b8006b1c678f53_14b7e75e23d9ebc5__MailEndCompose"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></a></p>
<p class="MsoNormal"><b><span style="font-size:11pt;font-family:Calibri,sans-serif">From:</span></b><span style="font-size:11pt;font-family:Calibri,sans-serif"> Reid Kleckner [mailto:<a href="mailto:rnk@google.com" target="_blank">rnk@google.com</a>]
<br>
<b>Sent:</b> Wednesday, February 11, 2015 5:09 PM<span><br>
<b>To:</b> Joseph Tremoulet<br>
<b>Cc:</b> Kaylor, Andrew; Bataev, Alexey; Reid Kleckner (<a href="mailto:reid@kleckner.net" target="_blank">reid@kleckner.net</a>); LLVM Developers Mailing List<br>
<b>Subject:</b> Re: [LLVMdev] RFC: Native Windows C++ exception handling<u></u><u></u></span></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<p class="MsoNormal">On Wed, Feb 11, 2015 at 1:57 PM, Joseph Tremoulet <<a href="mailto:jotrem@microsoft.com" target="_blank">jotrem@microsoft.com</a>> wrote:<u></u><u></u></p><div><div>
<blockquote style="border-style:none none none solid;border-left-color:rgb(204,204,204);border-left-width:1pt;padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)">Ah, ok.  So if the outliner sees non-dispatch code in the landing pad area, it can find/create somewhere
 to put it and an appropriate eh.actions annotation to get an EH table generated that will ensure it gets executed appropriately at run-time (in this example, perform the add before invoking either handler); is that more or less the idea?  That makes sense,
 thanks.</span><u></u><u></u></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Yep. In the worst case, we could model code before landing pad dispatch as a cleanup handler, but I think the most common transforms are easily undone. Consider your example where a + b gets hoisted before the catch dispatch. Adds have
 no side effects, so we can freely sink them back down into the catch handler once we start outlining.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Things that are hard to move, like loads and stores to unknown memory locations, cannot be hoisted over the llvm.eh.begincatch() call in the first place. It should act as a memory barrier.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)"> </span><u></u><u></u></p>
</div>
<blockquote style="border-style:none none none solid;border-left-color:rgb(204,204,204);border-left-width:1pt;padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)">I have the same question about the post-outlining IR.  To change the example to one where the bait
 won't get outlined, suppose you had</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">int foo(int a, int b) {</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">  try {</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">    try {</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">      maybe_throw();</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">      return 0;</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">    } catch (int) {</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">      // some code here that gets outlined</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">    }</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">    L1:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">    return a + b;</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">  } catch (float) {</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">    // some other code here that also gets outlined</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">  }</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">  L2:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">  return (a + b) + 1;</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:'Courier New';color:black">}</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)">and suppose that nothing gets moved around before outlining.  Then, after outlining, the landingpad
 will be followed by an eh.actions call and then an indirect branch that targets L1 and L2, correct?</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(56,87,35)">Do we need to worry that a late codesize optimization might want to merge the adds by hoisting them
 up above the indirect branch? If that happened, wouldn't it get skipped over if an exception were raised?</span><u></u><u></u></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">We'd have to hoist a + b to somewhere that dominates L1 and L2. I think the only BB in your program that dominates is the entry block. In the IR, the fake 'indirectbr' instruction after the call to @llvm.eh.actions helps keep the CFG conservatively
 correct.<u></u><u></u></p>
</div>
</div></div></div>
</div>
</div>
</div>
</div>

</blockquote></div><br></div></div>