public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-8635] internal-fn: Temporarily disable flag_trapv during .{ADD, SUB, MUL}_OVERFLOW etc. expansion [PR114753]
@ 2024-04-21  4:09 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2024-04-21  4:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:10f689596ad1633f4f5de1852c8f82a993fe948e

commit r13-8635-g10f689596ad1633f4f5de1852c8f82a993fe948e
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 18 09:45:14 2024 +0200

    internal-fn: Temporarily disable flag_trapv during .{ADD,SUB,MUL}_OVERFLOW etc. expansion [PR114753]
    
    __builtin_{add,sub,mul}_overflow{,_p} builtins are well defined
    for all inputs even for -ftrapv, and the -fsanitize=signed-integer-overflow
    ifns shouldn't abort in libgcc but emit the desired ubsan diagnostics
    or abort depending on -fsanitize* setting regardless of -ftrapv.
    The expansion of these internal functions uses expand_expr* in various
    places (e.g. MULT_EXPR at least in 2 spots), so temporarily disabling
    flag_trapv in all those spots would be hard.
    The following patch disables it around the bodies of 3 functions
    which can do the expand_expr calls.
    If it was in the C++ FE, I'd use some RAII sentinel, but I don't think
    we have one in the middle-end.
    
    2024-04-18  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/114753
            * internal-fn.cc (expand_mul_overflow): Save flag_trapv and
            temporarily clear it for the duration of the function, then
            restore previous value.
            (expand_vector_ubsan_overflow): Likewise.
            (expand_arith_overflow): Likewise.
    
            * gcc.dg/pr114753.c: New test.
    
    (cherry picked from commit 6c152c9db3b5b9d43e12846fb7a44977c0b65fc2)

Diff:
---
 gcc/internal-fn.cc              | 19 +++++++++++++++++++
 gcc/testsuite/gcc.dg/pr114753.c | 14 ++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index 6e81dc05e0e..c8943e6cb9e 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -1482,7 +1482,11 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
   rtx target = NULL_RTX;
   signop sign;
   enum insn_code icode;
+  int save_flag_trapv = flag_trapv;
 
+  /* We don't want any __mulv?i3 etc. calls from the expansion of
+     these internal functions, so disable -ftrapv temporarily.  */
+  flag_trapv = 0;
   done_label = gen_label_rtx ();
   do_error = gen_label_rtx ();
 
@@ -2324,6 +2328,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
       else
 	expand_arith_overflow_result_store (lhs, target, mode, res);
     }
+  flag_trapv = save_flag_trapv;
 }
 
 /* Expand UBSAN_CHECK_* internal function if it has vector operands.  */
@@ -2344,7 +2349,11 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
   rtx resvr = NULL_RTX;
   unsigned HOST_WIDE_INT const_cnt = 0;
   bool use_loop_p = (!cnt.is_constant (&const_cnt) || const_cnt > 4);
+  int save_flag_trapv = flag_trapv;
 
+  /* We don't want any __mulv?i3 etc. calls from the expansion of
+     these internal functions, so disable -ftrapv temporarily.  */
+  flag_trapv = 0;
   if (lhs)
     {
       optab op;
@@ -2474,6 +2483,7 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
     }
   else if (resvr)
     emit_move_insn (lhsr, resvr);
+  flag_trapv = save_flag_trapv;
 }
 
 /* Expand UBSAN_CHECK_ADD call STMT.  */
@@ -2552,7 +2562,11 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
   prec0 = MIN (prec0, pr);
   pr = get_min_precision (arg1, uns1_p ? UNSIGNED : SIGNED);
   prec1 = MIN (prec1, pr);
+  int save_flag_trapv = flag_trapv;
 
+  /* We don't want any __mulv?i3 etc. calls from the expansion of
+     these internal functions, so disable -ftrapv temporarily.  */
+  flag_trapv = 0;
   /* If uns0_p && uns1_p, precop is minimum needed precision
      of unsigned type to hold the exact result, otherwise
      precop is minimum needed precision of signed type to
@@ -2593,6 +2607,7 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
 	  ops.location = loc;
 	  rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
 	  expand_arith_overflow_result_store (lhs, target, mode, tem);
+	  flag_trapv = save_flag_trapv;
 	  return;
 	}
 
@@ -2616,16 +2631,19 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
 	      if (integer_zerop (arg0) && !unsr_p)
 		{
 		  expand_neg_overflow (loc, lhs, arg1, false, NULL);
+		  flag_trapv = save_flag_trapv;
 		  return;
 		}
 	      /* FALLTHRU */
 	    case PLUS_EXPR:
 	      expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p,
 				      unsr_p, unsr_p, false, NULL);
+	      flag_trapv = save_flag_trapv;
 	      return;
 	    case MULT_EXPR:
 	      expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
 				   unsr_p, unsr_p, false, NULL);
+	      flag_trapv = save_flag_trapv;
 	      return;
 	    default:
 	      gcc_unreachable ();
@@ -2671,6 +2689,7 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
 	  else
 	    expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
 				 uns0_p, uns1_p, false, NULL);
+	  flag_trapv = save_flag_trapv;
 	  return;
 	}
 
diff --git a/gcc/testsuite/gcc.dg/pr114753.c b/gcc/testsuite/gcc.dg/pr114753.c
new file mode 100644
index 00000000000..117f2c36d3e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr114753.c
@@ -0,0 +1,14 @@
+/* PR middle-end/114753 */
+/* { dg-do run } */
+/* { dg-options "-O2 -ftrapv" } */
+
+int
+main ()
+{
+  volatile long long i = __LONG_LONG_MAX__;
+  volatile long long j = 2;
+  long long k;
+  if (!__builtin_mul_overflow (i, j, &k) || k != -2LL)
+    __builtin_abort ();
+  return 0;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-21  4:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-21  4:09 [gcc r13-8635] internal-fn: Temporarily disable flag_trapv during .{ADD, SUB, MUL}_OVERFLOW etc. expansion [PR114753] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).