loongarch: use -mno-check-zero-division as the default for optimized code
authorXi Ruoyao <xry111@xry111.site>
Sat, 2 Jul 2022 08:16:44 +0000 (16:16 +0800)
committerXi Ruoyao <xry111@xry111.site>
Mon, 4 Jul 2022 06:30:47 +0000 (14:30 +0800)
commitd6bedcfcefccfbec0e258e797c393f42ab0882ce
treebb4ee837b675b1e3f0189c923efaeb77b52ab260
parent554fa149a0b31e822c2f02925173a1d2ec58a678
loongarch: use -mno-check-zero-division as the default for optimized code

Integer division by zero is undefined behavior anyway, and there are
already many platforms where neither the GCC port and the hardware do
anything to trap on division by zero.  So any portable program shall not
rely on SIGFPE on division by zero, in both theory and practice.  As the
result, there is no real reason to cost two additional instructions just
for the trap on division by zero with a new ISA.

One remaining reason to trap on division by zero may be debugging,
especially while -fsanitize=integer-divide-by-zero is not implemented
for LoongArch yet.  To make debugging easier, keep -mcheck-zero-division
as the default for -O0 and -Og, but use -mno-check-zero-division as the
default for all other optimization levels.

Backport this behavior change for 12.2, so 12.1 will be the only release
with a different default.

Co-authored-by: Lulu Cheng <chenglulu@loongson.cn>
gcc/ChangeLog:

* config/loongarch/loongarch.cc (loongarch_check_zero_div_p):
New static function.
(loongarch_idiv_insns): Use loongarch_check_zero_div_p instead
of TARGET_CHECK_ZERO_DIV.
(loongarch_output_division): Likewise.
* common/config/loongarch/loongarch-common.cc
(TARGET_DEFAULT_TARGET_FLAGS): Remove unneeded hook.
* doc/invoke.texi: Update to match the new behavior.

gcc/testsuite/ChangeLog:

* gcc.c-torture/execute/20101011-1.c (dg-additional-options):
add -mcheck-zero-division for LoongArch targets.

(cherry picked from commit f150dc1bd11802b70277f0fa209f2d23695a1095)
gcc/common/config/loongarch/loongarch-common.cc
gcc/config/loongarch/loongarch.cc
gcc/doc/invoke.texi
gcc/testsuite/gcc.c-torture/execute/20101011-1.c