aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Sayle <roger@nextmovesoftware.com>2024-06-24 15:34:03 +0100
committerRoger Sayle <roger@nextmovesoftware.com>2024-06-24 15:35:37 +0100
commitd8b05aef77443e1d3d8f3f5d2c56ac49a503fee3 (patch)
tree697c9fbc1dfb1152f4b917110d7792386eb12142
parentc43c74f6ec795a586388de7abfdd20a0040f6f16 (diff)
downloadgcc-d8b05aef77443e1d3d8f3f5d2c56ac49a503fee3.zip
gcc-d8b05aef77443e1d3d8f3f5d2c56ac49a503fee3.tar.gz
gcc-d8b05aef77443e1d3d8f3f5d2c56ac49a503fee3.tar.bz2
PR tree-optimization/113673: Avoid load merging when potentially trapping.
This patch fixes PR tree-optimization/113673, a P2 ice-on-valid regression caused by load merging of (ptr[0]<<8)+ptr[1] when -ftrapv has been specified. When the operator is | or ^ this is safe, but for addition of signed integer types, a trap may be generated/required, so merging this idiom into a single non-trapping instruction is inappropriate, confusing the compiler by transforming a basic block with an exception edge into one without. This revision implements Richard Biener's feedback to add an early check for stmt_can_throw_internal (cfun, stmt) to prevent transforming in the presence of any statement that could trap, not just overflow on addition. The one other tweak included in this patch is to mark the local function find_bswap_or_nop_load as static ensuring that it isn't called from outside this file, and guaranteeing that it is dominated by stmt_can_throw_internal checking. 2024-06-24 Roger Sayle <roger@nextmovesoftware.com> Richard Biener <rguenther@suse.de> gcc/ChangeLog PR tree-optimization/113673 * gimple-ssa-store-merging.cc (find_bswap_or_nop_load): Make static. (find_bswap_or_nop_1): Avoid transformations (load merging) when stmt_can_throw_internal indicates that a statement can trap. gcc/testsuite/ChangeLog PR tree-optimization/113673 * g++.dg/pr113673.C: New test case.
-rw-r--r--gcc/gimple-ssa-store-merging.cc6
-rw-r--r--gcc/testsuite/g++.dg/pr113673.C14
2 files changed, 18 insertions, 2 deletions
diff --git a/gcc/gimple-ssa-store-merging.cc b/gcc/gimple-ssa-store-merging.cc
index cb0cb5f..7dba4a7 100644
--- a/gcc/gimple-ssa-store-merging.cc
+++ b/gcc/gimple-ssa-store-merging.cc
@@ -363,7 +363,7 @@ init_symbolic_number (struct symbolic_number *n, tree src)
the answer. If so, REF is that memory source and the base of the memory area
accessed and the offset of the access from that base are recorded in N. */
-bool
+static bool
find_bswap_or_nop_load (gimple *stmt, tree ref, struct symbolic_number *n)
{
/* Leaf node is an array or component ref. Memorize its base and
@@ -610,7 +610,9 @@ find_bswap_or_nop_1 (gimple *stmt, struct symbolic_number *n, int limit)
gimple *rhs1_stmt, *rhs2_stmt, *source_stmt1;
enum gimple_rhs_class rhs_class;
- if (!limit || !is_gimple_assign (stmt))
+ if (!limit
+ || !is_gimple_assign (stmt)
+ || stmt_can_throw_internal (cfun, stmt))
return NULL;
rhs1 = gimple_assign_rhs1 (stmt);
diff --git a/gcc/testsuite/g++.dg/pr113673.C b/gcc/testsuite/g++.dg/pr113673.C
new file mode 100644
index 0000000..1148977
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr113673.C
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fnon-call-exceptions -ftrapv" } */
+
+struct s { ~s(); };
+void
+h (unsigned char *data, int c)
+{
+ s a1;
+ while (c)
+ {
+ int m = *data++ << 8;
+ m += *data++;
+ }
+}