]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end: teach gimple_could_trap_p to handle __builtin calls [PR122103]
authorTamar Christina <tamar.christina@arm.com>
Mon, 5 Jan 2026 20:53:21 +0000 (20:53 +0000)
committerTamar Christina <tamar.christina@arm.com>
Mon, 5 Jan 2026 20:53:21 +0000 (20:53 +0000)
The following testcase

void f (float *__restrict c, int *__restrict d, int n)
{
    for (int i = 0; i < n; i++)
    {
      if (d[i] > 1000)
        c[i] = __builtin_sqrtf (c[i]);
    }
}

compiled with -O3 -march=armv9-a -fno-math-errno -ftrapping-math

vectorizes as:

f:
        cmp     w2, 0
        ble     .L1
        mov     x3, 0
        mov     w4, 1000
        whilelo p7.s, wzr, w2
        mov     z31.s, w4
        ptrue   p6.b, all
.L3:
        ld1w    z0.s, p7/z, [x1, x3, lsl 2]
        cmpgt   p7.s, p7/z, z0.s, z31.s
        ld1w    z30.s, p7/z, [x0, x3, lsl 2]
        fsqrt   z30.s, p6/m, z30.s
        st1w    z30.s, p7, [x0, x3, lsl 2]
        incw    x3
        whilelo p7.s, w3, w2
        b.any   .L3
.L1:
        ret

which is incorrect, fsqrt can raise FE exceptions and so should be masked on p7
as the inactive lanes can trigger incorrect FE errors as the code in the PR
demonstrates.

In GCC 13 this was partially addressed for instruction that got lowered to IFNs
through r13-5979-gb9c78605039f839f3c79ad8fca4f60ea9a5654ed but it never
addressed __builtin_math_fns.  Assuming the direction of travel in PR96373 is
still valid this extends the support.

While ERRNO trapping is controlled through flags, it looks like for trapping
math the calls and IFNs are not marked specifically.  Instead in
gimple_could_trap_p_1 through operation_could_trap_p we default to all floating
point operation could trap if flag_trapping_math.

This extends gimple_could_trap_p_1 to do the same for __builtin_math_fns but
exclude instructions that the standard says can't raise FEs.

gcc/ChangeLog:

PR tree-optimization/122103
* gimple.cc (gimple_could_trap_p_1): Handle __builtin_ calls.

gcc/gimple.cc

index ca783b002e284aae84416b72d5581739930dc4a1..c791e04124314bab166a9f9d9eb77950b474e7c1 100644 (file)
@@ -2406,14 +2406,32 @@ gimple_could_trap_p_1 (const gimple *s, bool include_mem, bool include_stores)
       return gimple_asm_volatile_p (as_a <const gasm *> (s));
 
     case GIMPLE_CALL:
-      if (gimple_call_internal_p (s))
-       return false;
-      t = gimple_call_fndecl (s);
-      /* Assume that indirect and calls to weak functions may trap.  */
-      if (!t || !DECL_P (t) || DECL_WEAK (t))
-       return true;
-      return false;
+      {
+       if (gimple_call_internal_p (s))
+         return false;
+       t = gimple_call_fndecl (s);
+       /* Assume that indirect and calls to weak functions may trap.  */
+       if (!t || !DECL_P (t) || DECL_WEAK (t))
+         return true;
 
+       /* Any floating point builtin operation could trap.  */
+       if (gimple_call_builtin_p (s))
+         {
+           if (fndecl_built_in_p (t, BUILT_IN_NORMAL))
+             switch (DECL_FUNCTION_CODE (t))
+               {
+               CASE_FLT_FN (BUILT_IN_COPYSIGN):
+               CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
+                 return false;
+               default:
+                 break;
+               }
+           tree type = TREE_TYPE (gimple_call_fntype (s));
+           bool fp_operation = FLOAT_TYPE_P (type);
+           return fp_operation && flag_trapping_math;
+         }
+       return false;
+      }
     case GIMPLE_ASSIGN:
       op = gimple_assign_rhs_code (s);