<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - WinEHPrepare fails on __try / __finally inside __try / __except"
   href="https://llvm.org/bugs/show_bug.cgi?id=24773">24773</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>WinEHPrepare fails on __try / __finally inside __try / __except
          </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>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rnk@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=14864" name="attach_14864" title="IR">attachment 14864</a> <a href="attachment.cgi?id=14864&action=edit" title="IR">[details]</a></span>
IR

Two things go wrong starting from this C++ test case (IR attached):

extern "C" int printf(const char *, ...);
int do_div(int a, int b) { return a / b; }
int main() {
  int r = 0;
  __try {
    __try {
      r = do_div(1, 0);
    } __finally {
      printf("finally\n");
    }
  } __except (1) {
    printf("caught\n");
  }
  printf("1 / 0: %d\n", r);
  return 0;
}

First, having a single-BB catch funclet seems to confuse WinEHPrepare. It ends
up cloning the subsequent blocks and transforms the "implausible" ret as
unreachable.

Second, if I run -O1 on the IR, demotion fails like this:
$ opt -O1 -winehprepare t.ll -S -o -
Instruction does not dominate all uses!
  %call.wineh.reload.wineh.reload = load i32, i32*
%call.wineh.reload.wineh.spillslot
  %call4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8],
[11 x i8]* @"\01??_C@_0L@HIGBIMNM@1?5?1?50?3?5?$CFd?6?$AA@", i32 0, i32 0), i32
%call.wineh.reload.wineh.reload)
LLVM ERROR: Broken function found, compilation aborted!</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>