public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-6745] ifcvt: Improve costs handling for noce_convert_multiple.
@ 2022-01-19 17:38 Robin Dapp
  0 siblings, 0 replies; only message in thread
From: Robin Dapp @ 2022-01-19 17:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:46346d8d76c24bd07a30cb2c367e34601efabfef

commit r12-6745-g46346d8d76c24bd07a30cb2c367e34601efabfef
Author: Robin Dapp <rdapp@linux.ibm.com>
Date:   Wed Nov 27 13:46:17 2019 +0100

    ifcvt: Improve costs handling for noce_convert_multiple.
    
    When noce_convert_multiple is called the original costs are not yet
    initialized.  Therefore, up to now, costs were only ever unfairly
    compared against COSTS_N_INSNS (2).  This would lead to
    default_noce_conversion_profitable_p () rejecting all but the most
    contrived of sequences.
    
    This patch temporarily initializes the original costs by counting
    adding costs for all sets inside the then_bb.
    
    gcc/ChangeLog:
    
            * ifcvt.cc (bb_ok_for_noce_convert_multiple_sets): Estimate insns costs.
            (noce_process_if_block): Use potential costs.

Diff:
---
 gcc/ifcvt.cc | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/gcc/ifcvt.cc b/gcc/ifcvt.cc
index c696563918d..27e42944b8c 100644
--- a/gcc/ifcvt.cc
+++ b/gcc/ifcvt.cc
@@ -3414,14 +3414,17 @@ noce_convert_multiple_sets (struct noce_if_info *if_info)
    (SET (REG) (REG)) insns suitable for conversion to a series
    of conditional moves.  Also check that we have more than one set
    (other routines can handle a single set better than we would), and
-   fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets.  */
+   fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets.  While going
+   through the insns store the sum of their potential costs in COST.  */
 
 static bool
-bb_ok_for_noce_convert_multiple_sets (basic_block test_bb)
+bb_ok_for_noce_convert_multiple_sets (basic_block test_bb, unsigned *cost)
 {
   rtx_insn *insn;
   unsigned count = 0;
   unsigned param = param_max_rtl_if_conversion_insns;
+  bool speed_p = optimize_bb_for_speed_p (test_bb);
+  unsigned potential_cost = 0;
 
   FOR_BB_INSNS (test_bb, insn)
     {
@@ -3457,9 +3460,13 @@ bb_ok_for_noce_convert_multiple_sets (basic_block test_bb)
       if (!can_conditionally_move_p (GET_MODE (dest)))
 	return false;
 
+      potential_cost += insn_cost (insn, speed_p);
+
       count++;
     }
 
+  *cost += potential_cost;
+
   /* If we would only put out one conditional move, the other strategies
      this pass tries are better optimized and will be more appropriate.
      Some targets want to strictly limit the number of conditional moves
@@ -3507,11 +3514,24 @@ noce_process_if_block (struct noce_if_info *if_info)
      to calculate a value for x.
      ??? For future expansion, further expand the "multiple X" rules.  */
 
-  /* First look for multiple SETS.  */
+  /* First look for multiple SETS.  The original costs already include
+     a base cost of COSTS_N_INSNS (2): one instruction for the compare
+     (which we will be needing either way) and one instruction for the
+     branch.  When comparing costs we want to use the branch instruction
+     cost and the sets vs. the cmovs generated here.  Therefore subtract
+     the costs of the compare before checking.
+     ??? Actually, instead of the branch instruction costs we might want
+     to use COSTS_N_INSNS (BRANCH_COST ()) as in other places.  */
+
+  unsigned potential_cost = if_info->original_cost - COSTS_N_INSNS (1);
+  unsigned old_cost = if_info->original_cost;
   if (!else_bb
       && HAVE_conditional_move
-      && bb_ok_for_noce_convert_multiple_sets (then_bb))
+      && bb_ok_for_noce_convert_multiple_sets (then_bb, &potential_cost))
     {
+      /* Temporarily set the original costs to what we estimated so
+	 we can determine if the transformation is worth it.  */
+      if_info->original_cost = potential_cost;
       if (noce_convert_multiple_sets (if_info))
 	{
 	  if (dump_file && if_info->transform_name)
@@ -3519,6 +3539,9 @@ noce_process_if_block (struct noce_if_info *if_info)
 		     if_info->transform_name);
 	  return TRUE;
 	}
+
+      /* Restore the original costs.  */
+      if_info->original_cost = old_cost;
     }
 
   bool speed_p = optimize_bb_for_speed_p (test_bb);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-19 17:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-19 17:38 [gcc r12-6745] ifcvt: Improve costs handling for noce_convert_multiple Robin Dapp

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).