public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ajit Agarwal <aagarwa1@linux.ibm.com>
To: Richard Biener <richard.guenther@gmail.com>,
	Jeff Law <jeffreyalaw@gmail.com>,
	Peter Bergner <bergner@linux.ibm.com>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] tree-ssa-loop-ivopts : Add live analysis in regs used in decision making
Date: Wed, 8 Nov 2023 20:30:23 +0530	[thread overview]
Message-ID: <908bdc21-ea98-436e-9566-01e4d8da9132@linux.ibm.com> (raw)

tree-ssa-loop-ivopts : Add live analysis in regs used in decision making.

Add live anaysis in regs used calculation in decision making of
selecting ivopts candidates.

2023-11-08  Ajit Kumar Agarwal  <aagarwa1@linux.ibm.com>

gcc/ChangeLog:

	* tree-ssa-loop-ivopts.cc (get_regs_used): New function.
	(determine_set_costs): Call to get_regs_used to use live
	analysis.
---
 gcc/tree-ssa-loop-ivopts.cc | 73 +++++++++++++++++++++++++++++++++++--
 1 file changed, 70 insertions(+), 3 deletions(-)

diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
index c3336603778..e02fe7d434b 100644
--- a/gcc/tree-ssa-loop-ivopts.cc
+++ b/gcc/tree-ssa-loop-ivopts.cc
@@ -6160,6 +6160,68 @@ ivopts_estimate_reg_pressure (struct ivopts_data *data, unsigned n_invs,
   return cost + n_cands;
 }
 
+/* Return regs used based on live-in and liveout of given ssa variables.  */
+static unsigned
+get_regs_used (tree ssa_name)
+{
+  unsigned regs_used = 0;
+  gimple *stmt;
+  use_operand_p use;
+  basic_block def_bb = NULL;
+  imm_use_iterator imm_iter;
+
+  stmt = SSA_NAME_DEF_STMT (ssa_name);
+  if (stmt)
+    {
+      def_bb = gimple_bb (stmt);
+      /* Mark defs in liveout bitmap temporarily.  */
+      if (def_bb)
+	regs_used++;
+    }
+  else
+    def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
+
+  /* An undefined local variable does not need to be very alive.  */
+  if (virtual_operand_p (ssa_name)
+      || ssa_undefined_value_p (ssa_name, false))
+    return 0;
+
+  /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
+     add it to the list of live on entry blocks.  */
+  FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
+    {
+      gimple *use_stmt = USE_STMT (use);
+      basic_block add_block = NULL;
+
+      if (gimple_code (use_stmt) == GIMPLE_PHI)
+	{
+	  /* Uses in PHI's are considered to be live at exit of the SRC block
+	     as this is where a copy would be inserted.  Check to see if it is
+	     defined in that block, or whether its live on entry.  */
+	  int index = PHI_ARG_INDEX_FROM_USE (use);
+	  edge e = gimple_phi_arg_edge (as_a <gphi *> (use_stmt), index);
+	  if (e->src != def_bb)
+	    add_block = e->src;
+	}
+      else if (is_gimple_debug (use_stmt))
+	continue;
+      else
+	{
+	  /* If its not defined in this block, its live on entry.  */
+	  basic_block use_bb = gimple_bb (use_stmt);
+	  if (use_bb != def_bb)
+	    add_block = use_bb;
+	}
+
+      /* If there was a live on entry use, increment register used.  */
+      if (add_block)
+	{
+	  regs_used++;
+	}
+    }
+  return regs_used;
+}
+
 /* For each size of the induction variable set determine the penalty.  */
 
 static void
@@ -6200,15 +6262,20 @@ determine_set_costs (struct ivopts_data *data)
       n++;
     }
 
+  unsigned max = 0;
   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
     {
       struct version_info *info = ver_info (data, j);
-
       if (info->inv_id && info->has_nonlin_use)
-	n++;
+	{
+	  tree ssa_name = ssa_name (j);
+	  n = get_regs_used (ssa_name);
+	  if (n >= max)
+	    max = n;
+	}
     }
 
-  data->regs_used = n;
+  data->regs_used = max;
   if (dump_file && (dump_flags & TDF_DETAILS))
     fprintf (dump_file, "  regs_used %d\n", n);
 
-- 
2.39.3



             reply	other threads:[~2023-11-08 15:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-08 15:00 Ajit Agarwal [this message]
2023-11-09 12:51 ` Richard Biener
2023-11-10  6:41   ` Ajit Agarwal
2023-11-10 13:59     ` Richard Biener
2023-11-10 14:31       ` Ajit Agarwal

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=908bdc21-ea98-436e-9566-01e4d8da9132@linux.ibm.com \
    --to=aagarwa1@linux.ibm.com \
    --cc=bergner@linux.ibm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --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).