public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/114391] New: catch() and immediate throw; could be optimized to noop
@ 2024-03-19  8:18 antoshkka at gmail dot com
  2024-03-19 12:00 ` [Bug middle-end/114391] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: antoshkka at gmail dot com @ 2024-03-19  8:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114391

            Bug ID: 114391
           Summary: catch() and immediate throw; could be optimized to
                    noop
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the example:

void foo();

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


At the moment, the compiler at -O2 generates the assembly:

test():
  sub rsp, 24
  call foo()
  add rsp, 24
  ret
  mov rdi, rax
  jmp .L2

test() [clone .cold]:
.L2:
  call __cxa_begin_catch
  call __cxa_rethrow
  mov QWORD PTR [rsp+8], rax
  call __cxa_end_catch
  mov rdi, QWORD PTR [rsp+8]
  call _Unwind_Resume


However, an optimal assembly is:

test():
  jmp foo()


Please, add an optimization that removes catch() + immediate throw.


The sample code could be often met in release builds, due to some invariants
checks or debug logging are removed depending on NDEBUG:

void test() {
    try {
        foo();
    } catch (...) {
#ifdef NDEBUG
        std::cerr << "Unhandled exception!" << std::endl <<
            boost::current_exception_diagnostic_information();
#endif
        throw;
    }
}  


Godbolt playground: https://godbolt.org/z/qdG91cMe1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/114391] catch() and immediate throw; could be optimized to noop
  2024-03-19  8:18 [Bug middle-end/114391] New: catch() and immediate throw; could be optimized to noop antoshkka at gmail dot com
@ 2024-03-19 12:00 ` rguenth at gcc dot gnu.org
  2024-03-19 13:06 ` antoshkka at gmail dot com
  2024-03-19 22:20 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-03-19 12:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114391

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-03-19
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Is there something to optimize when foo() cannot be tail-called?  IMO that
would be best done separately.  Consider

int foo();

int test() {
    try {
        return foo() + 1;
    } catch (...) {
        throw;
    }
}

wouldn't it be valid to turn this into

int test() {
  return foo () + 1;
}

so the requirement is that the try {} catch (...) { throw; } is the
outermost EH handling region, nothing more?  And then we can elide it?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/114391] catch() and immediate throw; could be optimized to noop
  2024-03-19  8:18 [Bug middle-end/114391] New: catch() and immediate throw; could be optimized to noop antoshkka at gmail dot com
  2024-03-19 12:00 ` [Bug middle-end/114391] " rguenth at gcc dot gnu.org
@ 2024-03-19 13:06 ` antoshkka at gmail dot com
  2024-03-19 22:20 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: antoshkka at gmail dot com @ 2024-03-19 13:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114391

--- Comment #2 from Antony Polukhin <antoshkka at gmail dot com> ---
> Is there something to optimize when foo() cannot be tail-called?

Yes. Just `catch (...) { throw; }`, no more restrictions. I do not even think,
that it should be the outer most EH region:


void foo();
void bar();

void test() {
    try {
        foo();
    } catch (...) {
        throw;
    }
    bar();
}


is fine to optimize to just


void test() {
    foo();
    bar();
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/114391] catch() and immediate throw; could be optimized to noop
  2024-03-19  8:18 [Bug middle-end/114391] New: catch() and immediate throw; could be optimized to noop antoshkka at gmail dot com
  2024-03-19 12:00 ` [Bug middle-end/114391] " rguenth at gcc dot gnu.org
  2024-03-19 13:06 ` antoshkka at gmail dot com
@ 2024-03-19 22:20 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-19 22:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114391

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
                 CC|                            |pinskia at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-19 22:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-19  8:18 [Bug middle-end/114391] New: catch() and immediate throw; could be optimized to noop antoshkka at gmail dot com
2024-03-19 12:00 ` [Bug middle-end/114391] " rguenth at gcc dot gnu.org
2024-03-19 13:06 ` antoshkka at gmail dot com
2024-03-19 22:20 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).