Module Name:    src
Committed By:   rin
Date:           Sat Oct  7 12:05:36 UTC 2023

Modified Files:
        src/external/gpl3/gcc.old/dist/gcc: rtlanal.c target.def targhooks.c
            targhooks.h
        src/external/gpl3/gcc.old/dist/gcc/config/vax: vax.c
        src/external/gpl3/gcc.old/dist/gcc/doc: tm.texi tm.texi.in

Log Message:
gcc.old: PR port-vax/57646 patch provided by Kalvis Duckmanton [13/21]

Bitfield instructions will generate a reserved operand fault if the
operands are not reasonable (size > 32, position > 31 and size not
zero and field in a register). GCC generates code to test for these
conditions but in certain circumstances, the optimiser may decide
that a bitfield extraction instruction is invariant and move it
ahead of the instructions testing its arguments.

Introduce a new target hook to indicate to GCC that a bitfield
instruction may trap and update may_trap_p_1()

XXXRO: Although this patch includes diffs outside gcc/config/vax,
NFC for !TARGET_BITFIELD_MAY_TRAP_P, i.e., other than vax.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/external/gpl3/gcc.old/dist/gcc/rtlanal.c
cvs rdiff -u -r1.9 -r1.10 src/external/gpl3/gcc.old/dist/gcc/target.def
cvs rdiff -u -r1.11 -r1.12 src/external/gpl3/gcc.old/dist/gcc/targhooks.c \
    src/external/gpl3/gcc.old/dist/gcc/targhooks.h
cvs rdiff -u -r1.16 -r1.17 \
    src/external/gpl3/gcc.old/dist/gcc/config/vax/vax.c
cvs rdiff -u -r1.11 -r1.12 src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi
cvs rdiff -u -r1.9 -r1.10 src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi.in

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/gpl3/gcc.old/dist/gcc/rtlanal.c
diff -u src/external/gpl3/gcc.old/dist/gcc/rtlanal.c:1.13 src/external/gpl3/gcc.old/dist/gcc/rtlanal.c:1.14
--- src/external/gpl3/gcc.old/dist/gcc/rtlanal.c:1.13	Mon Feb 20 02:11:07 2023
+++ src/external/gpl3/gcc.old/dist/gcc/rtlanal.c	Sat Oct  7 12:05:35 2023
@@ -2953,6 +2953,15 @@ may_trap_p_1 (const_rtx x, unsigned flag
       /* These operations don't trap even with floating point.  */
       break;
 
+    case SIGN_EXTRACT:
+      if (targetm.have_extv ())
+	return targetm.bitfield_may_trap_p (x, flags);
+      break;
+    case ZERO_EXTRACT:
+      if (targetm.have_extzv ())
+	return targetm.bitfield_may_trap_p (x, flags);
+      break;
+
     default:
       /* Any floating arithmetic may trap.  */
       if (FLOAT_MODE_P (GET_MODE (x)) && flag_trapping_math)

Index: src/external/gpl3/gcc.old/dist/gcc/target.def
diff -u src/external/gpl3/gcc.old/dist/gcc/target.def:1.9 src/external/gpl3/gcc.old/dist/gcc/target.def:1.10
--- src/external/gpl3/gcc.old/dist/gcc/target.def:1.9	Mon Feb 20 02:11:07 2023
+++ src/external/gpl3/gcc.old/dist/gcc/target.def	Sat Oct  7 12:05:35 2023
@@ -3919,6 +3919,20 @@ passed along.",
  int, (const_rtx x, unsigned flags),
  default_unspec_may_trap_p)
 
+/* Return nonzero if evaluating SIGN_EXTRACT X or ZERO_EXTRACT X might
+   cause a trap.  FLAGS has the same meaning as in rtlanal.c:
+   may_trap_p_1. */
+DEFHOOK
+(bitfield_may_trap_p,
+ "This target hook returns nonzero if @var{x}, an @code{sign_extract} or\n\
+@code{zero_extract} operation, might cause a trap.  Targets can use\n\
+this hook to enhance precision of analysis for @code{sign_extract} and\n\
+@code{zero_extract} operations.  You may call @code{may_trap_p_1}\n\
+to analyze inner elements of @var{x} in which case @var{flags} should be\n\
+passed along.",
+ int, (const_rtx x, unsigned flags),
+ default_bitfield_may_trap_p)
+
 /* Given a register, this hook should return a parallel of registers
    to represent where to find the register pieces.  Define this hook
    if the register and its mode are represented in Dwarf in

Index: src/external/gpl3/gcc.old/dist/gcc/targhooks.c
diff -u src/external/gpl3/gcc.old/dist/gcc/targhooks.c:1.11 src/external/gpl3/gcc.old/dist/gcc/targhooks.c:1.12
--- src/external/gpl3/gcc.old/dist/gcc/targhooks.c:1.11	Mon Feb 20 02:11:07 2023
+++ src/external/gpl3/gcc.old/dist/gcc/targhooks.c	Sat Oct  7 12:05:35 2023
@@ -126,6 +126,12 @@ default_unspec_may_trap_p (const_rtx x, 
   return 0;
 }
 
+int
+default_bitfield_may_trap_p (const_rtx x, unsigned flags)
+{
+  return 0;
+}
+
 machine_mode
 default_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
 			       machine_mode mode,
Index: src/external/gpl3/gcc.old/dist/gcc/targhooks.h
diff -u src/external/gpl3/gcc.old/dist/gcc/targhooks.h:1.11 src/external/gpl3/gcc.old/dist/gcc/targhooks.h:1.12
--- src/external/gpl3/gcc.old/dist/gcc/targhooks.h:1.11	Mon Feb 20 02:11:07 2023
+++ src/external/gpl3/gcc.old/dist/gcc/targhooks.h	Sat Oct  7 12:05:35 2023
@@ -29,6 +29,7 @@ extern bool default_legitimize_address_d
 extern bool default_const_not_ok_for_debug_p (rtx);
 
 extern int default_unspec_may_trap_p (const_rtx, unsigned);
+extern int default_bitfield_may_trap_p (const_rtx, unsigned);
 extern machine_mode default_promote_function_mode (const_tree, machine_mode,
 							int *, const_tree, int);
 extern machine_mode default_promote_function_mode_always_promote

Index: src/external/gpl3/gcc.old/dist/gcc/config/vax/vax.c
diff -u src/external/gpl3/gcc.old/dist/gcc/config/vax/vax.c:1.16 src/external/gpl3/gcc.old/dist/gcc/config/vax/vax.c:1.17
--- src/external/gpl3/gcc.old/dist/gcc/config/vax/vax.c:1.16	Sat Oct  7 12:02:03 2023
+++ src/external/gpl3/gcc.old/dist/gcc/config/vax/vax.c	Sat Oct  7 12:05:35 2023
@@ -63,6 +63,7 @@ static void vax_trampoline_init (rtx, tr
 static poly_int64 vax_return_pops_args (tree, tree, poly_int64);
 static bool vax_mode_dependent_address_p (const_rtx, addr_space_t);
 static HOST_WIDE_INT vax_starting_frame_offset (void);
+static int vax_bitfield_may_trap_p (const_rtx, unsigned);
 
 /* Initialize the GCC target structure.  */
 #undef TARGET_ASM_ALIGNED_HI_OP
@@ -136,6 +137,9 @@ vax_elf_binds_local_p (const_tree exp)
 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
 
+#undef TARGET_BITFIELD_MAY_TRAP_P
+#define TARGET_BITFIELD_MAY_TRAP_P vax_bitfield_may_trap_p
+
 struct gcc_target targetm = TARGET_INITIALIZER;
 
 /* Set global variables as needed for the options enabled.  */
@@ -2497,3 +2501,34 @@ vax_decomposed_dimode_operand_p (rtx lo,
 
   return rtx_equal_p(lo, hi) && lo_offset + 4 == hi_offset;
 }
+
+/* Return 1 if a bitfield instruction (extv/extzv) may trap */
+static int
+vax_bitfield_may_trap_p (const_rtx x, unsigned flags)
+{
+  /* per the VARM
+   * Bitfield instructions may trap if
+   * size (arg1) GTRU 32
+   * size (arg1) NEQ 0, pos (arg 2) GTRU 31 and the field is in a register
+   * i.e. REG_P(operands[0]) is true
+   *
+   * GCC can only determine that a bitfield instruction will not trap
+   * if the size and position arguments are constants; if they aren't,
+   * the instruction must be assumed to trap.
+   */
+  rtx field = XEXP (x, 0);
+  rtx size = XEXP (x, 1);
+  rtx pos = XEXP (x, 2);
+  int retval = 0;
+
+  if (!CONST_INT_P (size) || !CONST_INT_P (pos))
+    retval = 1;
+  else if (INTVAL (size) < 0 || INTVAL (size) > GET_MODE_BITSIZE ( SImode ))
+    retval = 1;
+  else if (REG_P (field) && INTVAL (size) != 0
+	   && (INTVAL (pos) < 0 || INTVAL (pos) >= GET_MODE_BITSIZE ( SImode )))
+    retval = 1;
+  else
+    retval = 0;
+  return retval;
+}

Index: src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi
diff -u src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi:1.11 src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi:1.12
--- src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi:1.11	Mon Feb 20 02:11:24 2023
+++ src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi	Sat Oct  7 12:05:36 2023
@@ -11803,6 +11803,15 @@ to analyze inner elements of @var{x} in 
 passed along.
 @end deftypefn
 
+@deftypefn {Target Hook} int TARGET_BITFIELD_MAY_TRAP_P (const_rtx @var{x}, unsigned @var{flags})
+This target hook returns nonzero if @var{x}, an @code{sign_extract} or
+@code{zero_extract} operation, might cause a trap.  Targets can use
+this hook to enhance precision of analysis for @code{sign_extract} and
+@code{zero_extract} operations.  You may call @code{may_trap_p_1}
+to analyze inner elements of @var{x} in which case @var{flags} should be
+passed along.
+@end deftypefn
+
 @deftypefn {Target Hook} void TARGET_SET_CURRENT_FUNCTION (tree @var{decl})
 The compiler invokes this hook whenever it changes its current function
 context (@code{cfun}).  You can define this function if

Index: src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi.in
diff -u src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi.in:1.9 src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi.in:1.10
--- src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi.in:1.9	Mon Feb 20 02:11:24 2023
+++ src/external/gpl3/gcc.old/dist/gcc/doc/tm.texi.in	Sat Oct  7 12:05:36 2023
@@ -7979,6 +7979,8 @@ to by @var{ce_info}.
 
 @hook TARGET_UNSPEC_MAY_TRAP_P
 
+@hook TARGET_BITFIELD_MAY_TRAP_P
+
 @hook TARGET_SET_CURRENT_FUNCTION
 
 @defmac TARGET_OBJECT_SUFFIX

Reply via email to