[llvm-bugs] [Bug 35052] New: Optimize away exception allocation and throws handled by catch

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 24 01:36:08 PDT 2017


            Bug ID: 35052
           Summary: Optimize away exception allocation and throws handled
                    by catch
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: antoshkka at gmail.com
                CC: llvm-bugs at lists.llvm.org

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:


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:

        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 {
    } 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;

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171024/c8db4348/attachment.html>

More information about the llvm-bugs mailing list