public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "juzhe.zhong at rivai dot ai" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/113134] gcc does not version loops with early break conditions that don't have side-effects
Date: Fri, 02 Feb 2024 08:49:41 +0000	[thread overview]
Message-ID: <bug-113134-4-Ko3IlCQzio@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-113134-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #22 from JuzheZhong <juzhe.zhong at rivai dot ai> ---
I have done this following experiment.


diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
index bf017137260..8c36cc63d3b 100644
--- a/gcc/tree-ssa-loop-ivcanon.cc
+++ b/gcc/tree-ssa-loop-ivcanon.cc
@@ -1260,6 +1260,39 @@ canonicalize_loop_induction_variables (class loop *loop,
          may_be_zero = false;
        }

+      if (!exit)
+       {
+         auto_vec<edge> exits = get_loop_exit_edges (loop);
+         exit = exits[0];
+         class tree_niter_desc desc1;
+         class tree_niter_desc desc2;
+         if (number_of_iterations_exit (loop, exits[0], &desc1, false)
+             && number_of_iterations_exit (loop, exits[1], &desc2, false))
+           {
+             niter = fold_build2 (MIN_EXPR, unsigned_type_node, desc1.niter,
+                                  desc2.niter);
+             create_canonical_iv (loop, exit, niter);
+             gcond *cond_stmt;
+             class nb_iter_bound *elt;
+             for (elt = loop->bounds; elt; elt = elt->next)
+               {
+                 if (elt->is_exit
+                     && !wi::ltu_p (loop->nb_iterations_upper_bound,
+                                    elt->bound))
+                   {
+                     cond_stmt = as_a <gcond *> (elt->stmt);
+                     break;
+                   }
+               }
+             if (exits[1]->flags & EDGE_TRUE_VALUE)
+               gimple_cond_make_false (cond_stmt);
+             else
+               gimple_cond_make_true (cond_stmt);
+             update_stmt (cond_stmt);
+             return false;
+           }
+       }
+

I know the check is wrong just for experiment, Then:

  <bb 2> [local count: 69202658]:
  _21 = (unsigned int) N_13(D);
  _22 = MIN_EXPR <_21, 1001>;    ---- > Use MIN_EXPR as the check.
  _23 = _22 + 1;
  goto <bb 5>; [100.00%]

  <bb 3> [local count: 1014686025]:
  _1 = (long unsigned int) i_9;
  _2 = _1 * 4;
  _3 = a_14(D) + _2;
  _4 = *_3;
  _5 = b_15(D) + _2;
  _6 = *_5;
  _7 = c_16(D) + _2;
  _8 = _4 + _6;
  *_7 = _8;
  if (0 != 0)
    goto <bb 6>; [1.00%]
  else
    goto <bb 4>; [99.00%]

  <bb 4> [local count: 1004539166]:
  i_18 = i_9 + 1;

  <bb 5> [local count: 1073741824]:
  # i_9 = PHI <0(2), i_18(4)>
  # ivtmp_19 = PHI <_23(2), ivtmp_20(4)>
  ivtmp_20 = ivtmp_19 - 1;
  if (ivtmp_20 != 0)
    goto <bb 3>; [94.50%]
  else
    goto <bb 6>; [5.50%]

  <bb 6> [local count: 69202658]:
  return;

Then it can vectorize.

I am not sure whether it is the right place to put the codes.

      parent reply	other threads:[~2024-02-02  8:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-25 11:29 [Bug c/113134] New: Middle end early break vectorization: Fail to vectorize a simple early break code juzhe.zhong at rivai dot ai
2023-12-25 12:35 ` [Bug c/113134] " tnfchris at gcc dot gnu.org
2023-12-27 15:21 ` [Bug c/113134] gcc does not version loops with side-effect early breaks tnfchris at gcc dot gnu.org
2023-12-28  1:21 ` [Bug c/113134] gcc does not version loops with early break conditions that don't have side-effects juzhe.zhong at rivai dot ai
2023-12-28  3:48 ` tnfchris at gcc dot gnu.org
2023-12-28  3:55 ` juzhe.zhong at rivai dot ai
2023-12-28  4:02 ` tnfchris at gcc dot gnu.org
2023-12-28  4:05 ` tnfchris at gcc dot gnu.org
2023-12-28  4:23 ` juzhe.zhong at rivai dot ai
2023-12-28  4:30 ` tnfchris at gcc dot gnu.org
2023-12-28  4:35 ` juzhe.zhong at rivai dot ai
2023-12-28  4:45 ` tnfchris at gcc dot gnu.org
2023-12-28  4:46 ` juzhe.zhong at rivai dot ai
2023-12-28  4:49 ` tnfchris at gcc dot gnu.org
2023-12-28  4:51 ` juzhe.zhong at rivai dot ai
2023-12-28  4:53 ` tnfchris at gcc dot gnu.org
2023-12-28  5:08 ` tnfchris at gcc dot gnu.org
2023-12-28  9:11 ` juzhe.zhong at rivai dot ai
2023-12-28 21:20 ` [Bug tree-optimization/113134] " pinskia at gcc dot gnu.org
2024-01-08  8:11 ` rguenth at gcc dot gnu.org
2024-01-31 11:48 ` juzhe.zhong at rivai dot ai
2024-01-31 12:04 ` rguenth at gcc dot gnu.org
2024-02-02  3:38 ` juzhe.zhong at rivai dot ai
2024-02-02  8:49 ` juzhe.zhong at rivai dot ai [this message]

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-113134-4-Ko3IlCQzio@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).