Hi!

Eric mentioned we have a code trying to divide by zero intentionally
in gcc (since r0-241 !).
But, it clearly doesn't do what it wanted (which I think is raise
SIGFPE if the target normally raises it upon division by zero)
since r7-4470-g606f928d3805614, because we replace the division instruction
by __builtin_trap (), which does raise some signal, but quite different,
and then sure, r12-6924-gc2b610e7c6c89fd optimizes away even that
__builtin_trap ().

So I think on the libgcc side we should just hide the fact we are dividing
by zero from the optimizers, but it raises the question about what Ada
actually mandates and wants for such cases.  Because in 7+ it can end up
with a different signal and supposedly different exception at least.

Ok for trunk if this passes bootstrap/regtest?

2022-02-03  Jakub Jelinek  <ja...@redhat.com>

        * libgcc2.c (__udivmoddi4): Add optimization barriers to actually
        ensure division by zero.

--- libgcc/libgcc2.c.jj 2022-01-11 23:11:23.810270199 +0100
+++ libgcc/libgcc2.c    2022-02-03 09:24:14.513682731 +0100
@@ -1022,8 +1022,13 @@ __udivmoddi4 (UDWtype n, UDWtype d, UDWt
        {
          /* qq = NN / 0d */
 
-         if (d0 == 0)
-           d0 = 1 / d0;        /* Divide intentionally by zero.  */
+         if (__builtin_expect (d0 == 0, 0))
+           {
+             UWtype one = 1;
+             __asm ("" : "+g" (one));
+             __asm ("" : "+g" (d0));
+             d0 = one / d0;    /* Divide intentionally by zero.  */
+           }
 
          udiv_qrnnd (q1, n1, 0, n1, d0);
          udiv_qrnnd (q0, n0, n1, n0, d0);
@@ -1068,8 +1073,13 @@ __udivmoddi4 (UDWtype n, UDWtype d, UDWt
        {
          /* qq = NN / 0d */
 
-         if (d0 == 0)
-           d0 = 1 / d0;        /* Divide intentionally by zero.  */
+         if (__builtin_expect (d0 == 0, 0))
+           {
+             UWtype one = 1;
+             __asm ("" : "+g" (one));
+             __asm ("" : "+g" (d0));
+             d0 = one / d0;    /* Divide intentionally by zero.  */
+           }
 
          count_leading_zeros (bm, d0);
 


        Jakub

Reply via email to