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>
Sun, 3 Jul 2022 03:03:26 +0000 (11:03 +0800)
commitf150dc1bd11802b70277f0fa209f2d23695a1095
tree9133d8fb81d95414f60ea7a3065c96e4d97bd7fd
parentb5c3dd3df381896b09fa76b52cd7a49b9b24afb3
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.

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.
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