From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] tree-optimization/114551 - loop splitting and undefined overflow
Date: Thu, 4 Apr 2024 08:59:00 +0200 (CEST) [thread overview]
Message-ID: <20240404065900.lyMqZpdm8HJVg_BkrN1GAopqEKH3eXkxxeoyK7oHAiw@z> (raw)
When loop splitting hoists a guard computation it needs to make sure
that can be safely evaluated at this place when it was previously
only conditionally evaluated. The following fixes this for the
case of undefined overflow.
Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
PR tree-optimization/114551
* tree-ssa-loop-split.cc (split_loop): If the guard is
only conditionally evaluated rewrite computations with
possibly undefined overflow to unsigned arithmetic.
* gcc.dg/torture/pr114551.c: New testcase.
---
gcc/testsuite/gcc.dg/torture/pr114551.c | 18 ++++++++++++++++++
gcc/tree-ssa-loop-split.cc | 22 ++++++++++++++++++++--
2 files changed, 38 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr114551.c
diff --git a/gcc/testsuite/gcc.dg/torture/pr114551.c b/gcc/testsuite/gcc.dg/torture/pr114551.c
new file mode 100644
index 00000000000..13c15fbc3d9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr114551.c
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+
+int a, b[4], c, d, e, f;
+int main()
+{
+ a--;
+ for (f = 3; f >= 0; f--)
+ {
+ for (e = 0; e < 4; e++)
+ c = 0;
+ for (; c < 4; c++)
+ {
+ d = f && a > 0 && f > (2147483647 - a) ? 0 : b[f];
+ continue;
+ }
+ }
+ return 0;
+}
diff --git a/gcc/tree-ssa-loop-split.cc b/gcc/tree-ssa-loop-split.cc
index c0bb1b71d17..a770ea371a2 100644
--- a/gcc/tree-ssa-loop-split.cc
+++ b/gcc/tree-ssa-loop-split.cc
@@ -653,8 +653,26 @@ split_loop (class loop *loop1)
gimple_seq stmts2;
border = force_gimple_operand (border, &stmts2, true, NULL_TREE);
if (stmts2)
- gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop1),
- stmts2);
+ {
+ /* When the split condition is not always evaluated make sure
+ to rewrite it to defined overflow. */
+ if (!dominated_by_p (CDI_DOMINATORS, exit1->src, bbs[i]))
+ {
+ gimple_stmt_iterator gsi;
+ gsi = gsi_start (stmts2);
+ while (!gsi_end_p (gsi))
+ {
+ gimple *stmt = gsi_stmt (gsi);
+ if (is_gimple_assign (stmt)
+ && arith_code_with_undefined_signed_overflow
+ (gimple_assign_rhs_code (stmt)))
+ rewrite_to_defined_overflow (&gsi);
+ gsi_next (&gsi);
+ }
+ }
+ gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop1),
+ stmts2);
+ }
tree cond = fold_build2 (guard_code, boolean_type_node,
guard_init, border);
if (!initial_true)
--
2.35.3
reply other threads:[~2024-04-04 6:59 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240404065900.lyMqZpdm8HJVg_BkrN1GAopqEKH3eXkxxeoyK7oHAiw@z \
--to=rguenther@suse.de \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).