<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 - Optimize away exception allocation and throws handled by catch"
   href="https://bugs.llvm.org/show_bug.cgi?id=35052">35052</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimize away exception allocation and throws handled by catch
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

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

        <tr>
          <th>Hardware</th>
          <td>All
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>antoshkka@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Following function

void foo() {
    try {
        throw 1;
    } catch(...) {}
}

produces the following assembly at -O2:

foo():                                # @foo()
        push    rax
        mov     edi, 4
        call    __cxa_allocate_exception
        mov     dword ptr [rax], 1
        mov     esi, offset typeinfo for int
        xor     edx, edx
        mov     rdi, rax
        call    __cxa_throw
        mov     rdi, rax
        call    __cxa_begin_catch
        pop     rax
        jmp     __cxa_end_catch 

This is suboptimal as the exception is catched and ignored. Optimal assembly
would be:

foo():
        ret

For exception classes with user provided constructors and destructors that have
side effect, leave the calls to constructor and destructor but do no actually
throw the exception:

foo():
        call user_exception()
        call ~user_exception()
        rep ret


The `catch(...){}` expression used widely in C++ code, optimizing it would
improve binary size and performance a lot.

Optimization must happen after the inlining. Some realistic code examples were
the optimization could be very useful if triggered after inlining:

// Destructors
bar::~bar() {
    try {
        cleanup_some_dangerous_resources();
    } catch(...) {}
}

// Logging
#define SafeLog(X) try { g_logger << X << std::endl; } catch (...){}

// Poor Try* functions
bool try_parse(const char* text) {
    try {
        parse_text(text); // throws
    } catch (...) {
        return false;
    }

    return true;
}</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>