<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - ARC optimizations interact badly with catchswitch"
   href="https://bugs.llvm.org/show_bug.cgi?id=37332">37332</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>ARC optimizations interact badly with catchswitch
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>smeenai@fb.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>ahatanak@gmail.com, compnerd@compnerd.org, llvm-bugs@lists.llvm.org, rjmccall@apple.com, rnk@google.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20260" name="attach_20260" title="Reduced IR">attachment 20260</a> <a href="attachment.cgi?id=20260&action=edit" title="Reduced IR">[details]</a></span>
Reduced IR

If you run the attached IR with opt -objc-arc, the verifier will complain
because ARC optimizations attempted to insert an objc_release before a
catchswitch, but a catchswitch must be the only non-PHI instruction in its
basic block. Specifically, the ARC optimizations were attempting to duplicate
an objc_release across both edges of an invoke (presumably to shorten a
lifetime), so they wanted an insertion point that was guaranteed to be reached
from the unwind edge of the invoke; unfortunately, with the funclet-style
exception instructions, there's no such guaranteed insertion point in the
existing CFG in the general case.

One way I see of fixing this is splitting the invoke edge if it goes to a
catchswitch and inserting a cleanuppad to hold the release. E.g. in my
particular example the transformed IR would be:

  %call = invoke i8* @f(i8* %p, i8* %q)
            to label %invoke.cont unwind label %ehcleanup

ehcleanup:
  %tmp = cleanuppad within none []
  tail call void @objc_release(i8* %p) [ "funclet"(token %tmp) ]
  cleanupret from %tmp to label %catch.dispatch

%catch.dispatch:
  ...

I think this is the "right" thing to do in some sense, but it also involves
changing the CFG, which is probably going to be fraught with pain. I admittedly
haven't looked too much into how much work it would be to keep the analysis
correct in the face of a CFG change, but I'm guessing there's a reason the
existing transform explicitly avoids splitting edges :) (See
<a href="https://reviews.llvm.org/diffusion/L/browse/llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp;331493$262-272">https://reviews.llvm.org/diffusion/L/browse/llvm/trunk/lib/Transforms/ObjCARC/PtrState.cpp;331493$262-272</a>.)

The other option is to just bail out of the objc_release movement if there's a
catchswitch in the way. Unfortunately, I can't figure out where to do that
check. Marking SetCFGHazardAfflicted inside
BottomUpPtrState::HandlePotentialUse doesn't seem to do the trick; I think the
CFGHazardAfflicted state somehow gets lost at some point during merging. I can
check for the presence of a catchswitch inside ObjCARCOpt::CheckForCFGHazards,
but that seems overly conservative, since we only care if the catchswitch is a
potential insertion point. Any help on that front would be very appreciated.

(I'm also not sure why the retain/release for %p isn't just elided entirely in
this particular example. I have a separate reduction for that, and I'll file a
different bug.)</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>