]> git.ipfire.org Git - thirdparty/gcc.git/commit
Change __builtin_unreachable to __builtin_trap (or infinite loop) if only thing in...
authorAndrew Pinski <quic_apinski@quicinc.com>
Tue, 29 Apr 2025 14:24:08 +0000 (07:24 -0700)
committerAndrew Pinski <quic_apinski@quicinc.com>
Tue, 22 Jul 2025 17:03:01 +0000 (10:03 -0700)
commit31e8896dcd87279be73674e8f2258db26d7a6e1e
tree0831d9e7cd57d48046a5cfd5091eb11aa17caae9
parentbb561b45e490377046b7ce14e8934cc8a81ec6a7
Change __builtin_unreachable to __builtin_trap (or infinite loop) if only thing in function [PR109267]

When we have an empty function, things can go wrong with
cfi_startproc/cfi_endproc and a few other things like exceptions. So if
the only thing the function does is a call to __builtin_unreachable,
let's replace that with a __builtin_trap instead if the target has a trap
instruction.  For targets without a trap instruction defined, replace it
with an infinite loop; this allows not to need for the abort call to happen
but still get the correct behavior of not having two functions at the same
location.

The QOI idea for basic block reorder is recorded as PR 120004.

Changes since v1:
* v2: Move to final gimple cfg cleanup instead of expand and use
      BUILT_IN_UNREACHABLE_TRAP.
* v3: For targets without a trap defined, create an infinite loop.

Bootstrapped and tested on x86_64-linux-gnu.

PR middle-end/109267

gcc/ChangeLog:

* tree-cfgcleanup.cc (execute_cleanup_cfg_post_optimizing): If the first
non debug statement in the first (and only) basic block is a call
to __builtin_unreachable change it to a call to __builtin_trap or an
infinite loop.

gcc/testsuite/ChangeLog:

* lib/target-supports.exp (check_effective_target_trap): New proc.
* g++.dg/missing-return.C: Update testcase for the !trap case.
* gcc.dg/pr109267-1.c: New test.
* gcc.dg/pr109267-2.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/testsuite/g++.dg/missing-return.C
gcc/testsuite/gcc.dg/pr109267-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr109267-2.c [new file with mode: 0644]
gcc/testsuite/lib/target-supports.exp
gcc/tree-cfgcleanup.cc