public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: Richard Biener <richard.guenther@gmail.com>
Cc: David Malcolm <dmalcolm@redhat.com>,
	Jakub Jelinek <jakub@redhat.com>,
	kazu@gcc.gnu.org, GCC Patches <gcc-patches@gcc.gnu.org>,
	Jan Hubicka <hubicka@ucw.cz>
Subject: Re: [PATCH] Add if-chain to switch conversion pass.
Date: Fri, 25 Sep 2020 16:05:52 +0200	[thread overview]
Message-ID: <f7117014-aec2-4278-4997-bda8dbc879d7@suse.cz> (raw)
In-Reply-To: <CAFiYyc1C3NrDHze17YHn5pwWjRuodryLf8UxMmVTApqM=MRkgg@mail.gmail.com>

On 9/24/20 2:41 PM, Richard Biener wrote:
> On Wed, Sep 2, 2020 at 1:53 PM Martin Liška <mliska@suse.cz> wrote:
>>
>> On 9/1/20 4:50 PM, David Malcolm wrote:
>>> Hope this is constructive
>>> Dave
>>
>> Thank you David. All of them very very useful!
>>
>> There's updated version of the patch.

Hey.

What a juicy patch review!

> 
> I noticed several functions without a function-level comment.

Yep, but several of them are documented in a class declaration. Anyway, I will
improve for the next time.

> 
> -  cluster (tree case_label_expr, basic_block case_bb, profile_probability prob,
> -          profile_probability subtree_prob);
> +  inline cluster (tree case_label_expr, basic_block case_bb,
> +                 profile_probability prob, profile_probability subtree_prob);
> 
> I thought we generally leave this to the compiler ...
> 
> +@item -fconvert-if-to-switch
> +@opindex fconvert-if-to-switch
> +Perform conversion of an if cascade into a switch statement.
> +Do so if the switch can be later transformed using a jump table
> +or a bit test.  The transformation can help to produce faster code for
> +the switch statement.  This flag is enabled by default
> +at @option{-O2} and higher.
> 
> this mentions we do this only when we later can convert the
> switch again but both passes (we still have two :/) have
> independent guards.

Yes, we have the option for jump tables (-jump-tables), but we miss one for a bit-test.
Moreover, as mentioned in the cover email, one can see it beneficial to convert a if-chain
to switch as the expansion (without any BT and JT) can benefit from balanced tree.

> 
> +  /* For now, just wipe the dominator information.  */
> +  free_dominance_info (CDI_DOMINATORS);
> 
> could at least be conditional on the vop renaming condition...
> 
> +  if (!all_candidates.is_empty ())
> +    mark_virtual_operands_for_renaming (fun);

Yep.

> 
> +      if (bitmap_bit_p (*visited_bbs, bb->index))
> +       break;
> +      bitmap_set_bit (*visited_bbs, bb->index);
> 
> since you are using a bitmap and not a sbitmap (why?)
> you can combine those into

New to me, thanks.

> 
>     if (!bitmap_set_bit (*visited_bbs, bb->index))
>      break;
> 
> +      /* Current we support following patterns (situations):
> +
> +        1) if condition with equal operation:
> +
> ...
> 
> did you see whether using
> 
>     register_edge_assert_for (lhs, true_edge, code, lhs, rhs, asserts);
> 
> works equally well?  It fills the 'asserts' vector with relations
> derived from 'lhs'.  There's also
> vr_values::extract_range_for_var_from_comparison_expr
> to compute the case_range

Good point! I must admit that my patch doesn't properly handle negative conditions:

   if (argc != 11111)
   {
     if (argc == 1)
       global = 222;
     ...
   }

which can VRP correctly identify as anti-range:
int ~[11111, 11111]  EQUIVALENCES: { argc_8(D) } (1 elements)$1 = void

I have question about OR and AND conditions:

   <bb 2> :
   _1 = aChar_8(D) == 1;
   _2 = aChar_8(D) == 10;
   _3 = _1 | _2;
   if (_3 != 0)
     goto <bb 6>; [INV]
   else
     goto <bb 3>; [INV]

   <bb 2> :
   _1 = aChar_8(D) != 1;
   _2 = aChar_8(D) != 10;
   _3 = _1 & _2;
   if (_3 != 0)
     goto <bb 6>; [INV]
   else
     goto <bb 3>; [INV]

Can I somehow get that from VRP (as I ask register_edge_assert_for only for LHS
of a condition)?

> 
> +      /* If it's not the first condition, then we need a BB without
> +        any statements.  */
> +      if (!first)
> +       {
> +         unsigned stmt_count = 0;
> +         for (gimple_stmt_iterator gsi = gsi_start_nondebug_bb (bb);
> +              !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
> +           ++stmt_count;
> +
> +         if (stmt_count - visited_stmt_count != 0)
> +           break;
> 
> hmm, OK, this might be a bit iffy to get correct then, still it's a lot
> of pattern maching code that is there elsewhere already.
> ifcombine simply hoists any stmts without side-effects up the
> dominator tree and thus only requires BBs without side-effects
> (IIRC there's a predicate fn for that).

Yes, I completely miss support for code hoisting (expect first BB where we put gswitch).
If I'm correct hoisting should be possible where case destination should be a new BB
that will contain original statements and then it will jump to a case destination block.

> 
> +      /* Prevent loosing information for a PHI node where 2 edges will
> +        be folded into one.  Note that we must do the same also for false_edge
> +        (for last BB in a if-elseif chain).  */
> +      if (!chain->record_phi_arguments (true_edge)
> +         || !chain->record_phi_arguments (false_edge))
> 
> I don't really get this - looking at record_phi_arguments it seems
> we're requiring that all edges into the same PHI from inside the case
> (irrespective of from which case label) have the same value for the
> PHI arg?

I guess so, I'll refresh the functionality.

> 
> +             if (arg != *v)
> +               return false;
> 
> should use operand_equal_p at least, REAL_CSTs are for example
> not shared tree nodes.  I'll also notice that if record_phi_arguments
> fails we still may have altered its hash-map even though the particular
> edge will not participate in the current chain, so it will affect other
> chains ending in the same BB.  Overall this looks a bit too conservative
> (and random, based on visiting order).

Oh, yes, it's not properly cleared once we bail out for a particular chain.

> 
> +    expanded_location loc
> +    = expand_location (gimple_location (chain->m_first_condition));
> +      if (dump_file)
> +       {
> +         fprintf (dump_file, "Condition chain (at %s:%d) with %d conditions "
> +                  "(%d BBs) transformed into a switch statement.\n",
> +                  loc.file, loc.line, total_case_values,
> +                  chain->m_entries.length ());
> 
> Use dump_printf_loc and you can pass a gimple * stmt as location.
> 
> +      /* Follow if-elseif-elseif chain.  */
> +      bb = false_edge->dest;
> 
> so that means the code doesn't handle a tree, right?  But what
> makes us sure the chain doesn't continue on the true_edge instead,
> guess this degenerate tree isn't handled either.

As mentioned earlier, I didn't consider VAR != CST type of conditions that
makes it more complicated.

> 
> I was thinking on whether doing the switch discovery in a reverse
> CFG walk, recording for each BB what case_range(s) it represents
> for a particular variable(s) so when visiting a dominator you
> can quickly figure what's the relevant children (true, false or both).

Sounds promising. Note that right now we do not support overlapping cases like:

   if (5 <= argc && argc <= 10)
     foo ();
   else if (6 <= argc && argc <= 100)
     foo ();

So I'm wondering if we can support 2 children?

> It would also make the matching a BB-local operation where you'd
> do the case_label discovery based on the single-pred BBs gimple-cond.

Can you please describe more how will the walk work?

> 
> +  output = bit_test_cluster::find_bit_tests (filtered_clusters);
> +  r = output.length () < filtered_clusters.length ();
> +  if (r)
> +    dump_clusters (&output, "BT can be built");
> 
> so as of the very above comment - this might be guarded with
> flag_tree_switch_conversion?

flag_tree_switch_conversion isn't connected to the if-chain pass (yet).

> 
> As mentioned previously I would have liked to see if-to-switch
> integrated with switch-conversion, having separate passes is
> somewhat awkward (for example the redundant and possibly
> expensive find_bit_tests).

Well, the CFG transformation for BT and JT is not trivial and I would like
to go in the first iteration through gswitch statements.
I have a massive speed up for the find_bit_tests/find_jump_tables.

> 
> +         /* Move all statements from the BB to the BB with gswitch.  */
> +         auto_vec<gimple *> stmts;
> +         for (gimple_stmt_iterator gsi = gsi_start_bb (entry.m_bb);
> +              !gsi_end_p (gsi); gsi_next (&gsi))
> +           {
> +             gimple *stmt = gsi_stmt (gsi);
> +             if (gimple_code (stmt) != GIMPLE_COND)
> +               stmts.safe_push (stmt);
> +           }
> +
> +         for (unsigned i = 0; i < stmts.length (); i++)
> +           {
> +             gimple_stmt_iterator gsi_from = gsi_for_stmt (stmts[i]);
> +             gsi_move_before (&gsi_from, &gsi);
> +           }
> 
> so you are already hoisting all stmts ...

As mentioned, it's not supported right now. This moves all these kind of "temp" statements:

   _1 = aChar_8(D) == 1;
   _2 = aChar_8(D) == 10;
   _3 = _1 | _2;

Martin

> 
> +      make_edge (first_cond.m_bb, case_bb, 0);
> 
> and if this doesn't create a new edge you need equivalent PHI
> args in the case_bb.  To remove this restriction you "only"
> have to add a forwarder.  Sth like
> 
>     edge e = make_edge (...);
>     if (!e)
>       {
>          bb = create_basic_block ();
>          make_edge (first_cond.m_bb, bb, 0);
>          e = make_edge (bb, case_bb, 0);
>       }
>    fill PHI arg of 'e' from original value (no need to create the hash-map then)
> 
> Richard.
> 
> 
>> Martin


  reply	other threads:[~2020-09-25 14:05 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-04 14:23 Martin Liška
2019-11-04 14:49 ` Jakub Jelinek
2019-11-05 12:38   ` Richard Biener
2019-11-06 21:03     ` Bernhard Reutner-Fischer
2019-11-14  9:44       ` Martin Liška
2019-11-14 12:35         ` Bernhard Reutner-Fischer
2019-11-14  9:41     ` Martin Liška
2019-11-14 10:48       ` Richard Biener
2019-11-15 13:56         ` Martin Liška
2020-09-01 11:47           ` Martin Liška
2020-09-01 14:50             ` David Malcolm
2020-09-02 11:53               ` Martin Liška
2020-09-21  8:55                 ` Martin Liška
2020-09-24 12:41                 ` Richard Biener
2020-09-25 14:05                   ` Martin Liška [this message]
2020-09-29  8:46                     ` Richard Biener
2020-10-02 13:26                       ` Martin Liška
2020-10-02 14:19                         ` Andrew MacLeod
2020-10-06 12:09                           ` Martin Liška
2020-10-06 12:56                             ` Andrew MacLeod
2020-10-06 13:09                               ` Martin Liška
2020-10-06 13:23                                 ` Andrew MacLeod
2020-10-06 13:41                                 ` Richard Biener
2020-10-02 13:23                   ` Martin Liška
2020-10-06  7:47                     ` Richard Biener
2020-10-06 13:48                       ` Martin Liška
2020-10-06 14:12                         ` Jakub Jelinek
2020-10-12 12:39                           ` Martin Liška
2020-10-12 13:00                             ` Jakub Jelinek
2020-10-14 18:09                             ` Andrew MacLeod
2020-10-07  8:00                         ` Richard Biener
2020-10-12 12:44                           ` Martin Liška
2020-10-12 13:01                             ` Martin Liška
2020-10-15 12:38                               ` Richard Biener
2020-10-16 14:04                             ` [PATCH v2] " Martin Liška
2020-11-06 12:31                               ` Richard Biener
2020-11-09 12:26                                 ` Martin Liška
2020-11-16 12:21                                   ` Richard Biener
2020-11-18 12:25                                     ` Martin Liška
2020-11-19 14:46                                       ` Richard Biener
2020-11-20  8:57                                         ` Martin Liška
2020-11-20 14:37                                           ` Richard Biener
2020-11-27 15:07                                             ` Martin Liška
2020-12-01 10:34                                               ` Richard Biener
2020-12-01 13:57                                                 ` [PATCH] if-to-switch: Support chain with 2 BBs Martin Liška
2020-12-01 22:14                                                   ` Jeff Law
2019-11-13 11:32   ` [PATCH] Add if-chain to switch conversion pass Martin Liška
2019-11-13 16:14     ` Michael Matz
2019-11-14 10:07       ` Martin Liška

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=f7117014-aec2-4278-4997-bda8dbc879d7@suse.cz \
    --to=mliska@suse.cz \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hubicka@ucw.cz \
    --cc=jakub@redhat.com \
    --cc=kazu@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /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).