public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hubicka at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/77689] Missing vectorization lead to huge performance loss
Date: Fri, 28 Jul 2023 11:53:41 +0000	[thread overview]
Message-ID: <bug-77689-4-6YOMOe0DPZ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-77689-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77689

--- Comment #16 from Jan Hubicka <hubicka at gcc dot gnu.org> ---
I am testing the following that makes loop splitting understand when first
iteration is special.

diff --git a/gcc/tree-ssa-loop-split.cc b/gcc/tree-ssa-loop-split.cc
index 70cd0aaefa7..1fd3ee1d1e5 100644
--- a/gcc/tree-ssa-loop-split.cc
+++ b/gcc/tree-ssa-loop-split.cc
@@ -42,6 +42,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-fold.h"
 #include "gimplify-me.h"
 #include "print-tree.h"
+#include "value-query.h"

 /* This file implements two kinds of loop splitting.

@@ -75,7 +76,8 @@ along with GCC; see the file COPYING3.  If not see
    point in *BORDER and the comparison induction variable in IV.  */

 static tree
-split_at_bb_p (class loop *loop, basic_block bb, tree *border, affine_iv *iv)
+split_at_bb_p (class loop *loop, basic_block bb, tree *border, affine_iv *iv,
+              enum tree_code *guard_code)
 {
   gcond *stmt;
   affine_iv iv2;
@@ -87,19 +89,6 @@ split_at_bb_p (class loop *loop, basic_block bb, tree
*border, affine_iv *iv)

   enum tree_code code = gimple_cond_code (stmt);

-  /* Only handle relational comparisons, for equality and non-equality
-     we'd have to split the loop into two loops and a middle statement.  */
-  switch (code)
-    {
-      case LT_EXPR:
-      case LE_EXPR:
-      case GT_EXPR:
-      case GE_EXPR:
-       break;
-      default:
-       return NULL_TREE;
-    }
-
   if (loop_exits_from_bb_p (loop, bb))
     return NULL_TREE;

@@ -129,6 +118,55 @@ split_at_bb_p (class loop *loop, basic_block bb, tree
*border, affine_iv *iv)
   if (!iv->no_overflow)
     return NULL_TREE;

+  /* Only handle relational comparisons, for equality and non-equality
+     we'd have to split the loop into two loops and a middle statement.  */
+  switch (code)
+    {
+      case LT_EXPR:
+      case LE_EXPR:
+      case GT_EXPR:
+      case GE_EXPR:
+       break;
+      case NE_EXPR:
+      case EQ_EXPR:
+       /* If the test check for first iteration, we can handle NE/EQ
+          with only one split loop.  */
+       if (operand_equal_p (iv->base, iv2.base, 0))
+         {
+           if (code == EQ_EXPR)
+             code = !tree_int_cst_sign_bit (iv->step) ? LE_EXPR : GE_EXPR;
+           else
+             code = !tree_int_cst_sign_bit (iv->step) ? GT_EXPR : LT_EXPR;
+           break;
+         }
+       /* Similarly when the test checks for minimal or maximal
+          value range.  */
+       else
+         {
+           int_range<2> r;
+           get_global_range_query ()->range_of_expr (r, op0, stmt);
+           if (!r.varying_p () && !r.undefined_p ())
+             {
+               wide_int val = wi::to_wide (op1);
+               if (known_eq (val, r.lower_bound ()))
+                 {
+                   code = (code == EQ_EXPR) ? LE_EXPR : GT_EXPR;
+                   break;
+                 }
+               else if (known_eq (val, r.upper_bound ()))
+                 {
+                   code = (code == EQ_EXPR) ? GE_EXPR : LT_EXPR;
+                   break;
+                 }
+             }
+         }
+       /* TODO: We can compare with exit condition; it seems that testing for
+          last iteration is common case.  */
+       return NULL_TREE;
+      default:
+       return NULL_TREE;
+    }
+
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Found potential split point: ");
@@ -143,6 +181,7 @@ split_at_bb_p (class loop *loop, basic_block bb, tree
*border, affine_iv *iv)
     }

   *border = iv2.base;
+  *guard_code = code;
   return op0;
 }

@@ -551,7 +590,7 @@ split_loop (class loop *loop1)
     {
       if (!niter.control.no_overflow)
        return false;
-      if (tree_int_cst_sign_bit (niter.control.step) > 0)
+      if (tree_int_cst_sign_bit (niter.control.step))
        niter.cmp = GT_EXPR;
       else
        niter.cmp = LT_EXPR;
@@ -566,8 +605,9 @@ split_loop (class loop *loop1)
     }

   /* Find a splitting opportunity.  */
+  enum tree_code guard_code;
   for (i = 0; i < loop1->num_nodes; i++)
-    if ((guard_iv = split_at_bb_p (loop1, bbs[i], &border, &iv)))
+    if ((guard_iv = split_at_bb_p (loop1, bbs[i], &border, &iv, &guard_code)))
       {
        profile_count entry_count = loop_preheader_edge (loop1)->count ();
        /* Handling opposite steps is not implemented yet.  Neither
@@ -585,7 +625,6 @@ split_loop (class loop *loop1)
        gcond *guard_stmt = as_a<gcond *> (*gsi_last_bb (bbs[i]));
        tree guard_init = PHI_ARG_DEF_FROM_EDGE (phi,
                                                 loop_preheader_edge (loop1));
-       enum tree_code guard_code = gimple_cond_code (guard_stmt);

        /* Loop splitting is implemented by versioning the loop, placing
           the new loop after the old loop, make the first loop iterate

  parent reply	other threads:[~2023-07-28 11:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-77689-4@http.gcc.gnu.org/bugzilla/>
2021-08-25  6:07 ` pinskia at gcc dot gnu.org
2021-08-25  7:38 ` crazylht at gmail dot com
2023-07-28  8:12 ` hubicka at gcc dot gnu.org
2023-07-28 11:53 ` hubicka at gcc dot gnu.org [this message]
2023-07-28 13:11 ` hubicka at gcc dot gnu.org
2023-07-28 14:19 ` cvs-commit at gcc dot gnu.org

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=bug-77689-4-6YOMOe0DPZ@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).