From f164fd80953f3cffd96a492c8424c83290cd43cc Mon Sep 17 00:00:00 2001
From: Ajit Kumar Agarwal <ajitkum@xilix.com>
Date: Wed, 7 Oct 2015 20:50:40 +0200
Subject: [PATCH] [RFC, Patch]: Optimized changes in the register used inside
 loop for LICM and IVOPTS.

Changes are done in the Loop Invariant(LICM) at RTL level and also the
Induction variable optimization based on SSA representation. The current
logic used in LICM for register used inside the loops is changed. The
Live Out of the loop latch node and the Live in of the destination of
the exit nodes is used to set the Loops Liveness at the exit of the Loop.
The register used is the number of live variables at the exit of the
Loop calculated above.

For Induction variable optimization on tree SSA representation, the register
used logic is based on the number of phi nodes at the loop header to represent
the liveness at the loop.  Current Logic used only the number of phi nodes at
the loop header.  Changes are made to represent the phi operands also live at
the loop. Thus number of phi operands also gets incremented in the number of
registers used.

ChangeLog:
2015-10-09  Ajit Agarwal  <ajitkum@xilinx.com>

	* loop-invariant.c (compute_loop_liveness): New.
	(determine_regs_used): New.
	(find_invariants_to_move): Use of determine_regs_used.
	* tree-ssa-loop-ivopts.c (determine_set_costs): Consider the phi
	arguments for register used.

Signed-off-by:Ajit Agarwal ajitkum@xilinx.com
---
 gcc/loop-invariant.c       | 72 +++++++++++++++++++++++++++++++++++++---------
 gcc/tree-ssa-loop-ivopts.c |  4 +--
 2 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/gcc/loop-invariant.c b/gcc/loop-invariant.c
index 52c8ae8..e4291c9 100644
--- a/gcc/loop-invariant.c
+++ b/gcc/loop-invariant.c
@@ -1413,6 +1413,19 @@ set_move_mark (unsigned invno, int gain)
     }
 }
 
+static int
+determine_regs_used()
+{
+  unsigned int j;
+  unsigned int reg_used = 2;
+  bitmap_iterator bi;
+
+  EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (curr_loop)->regs_live, 0, j, bi)
+    (reg_used) ++;
+
+  return reg_used;
+}
+
 /* Determines which invariants to move.  */
 
 static void
@@ -1433,18 +1446,7 @@ find_invariants_to_move (bool speed, bool call_p)
        registers used; we put some initial bound here to stand for
        induction variables etc.  that we do not detect.  */
     {
-      unsigned int n_regs = DF_REG_SIZE (df);
-
-      regs_used = 2;
-
-      for (i = 0; i < n_regs; i++)
-	{
-	  if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i))
-	    {
-	      /* This is a value that is used but not changed inside loop.  */
-	      regs_used++;
-	    }
-	}
+      regs_used = determine_regs_used();
     }
 
   if (! flag_ira_loop_pressure)
@@ -2055,9 +2057,43 @@ calculate_loop_reg_pressure (void)
     }
 }
 
-
+static void
+calculate_loop_liveness (void)
+{
+  basic_block bb;
+  struct loop *loop;
 
-/* Move the invariants out of the loops.  */
+  FOR_EACH_LOOP (loop, 0)
+    if (loop->aux == NULL)
+      {
+        loop->aux = xcalloc (1, sizeof (struct loop_data));
+        bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
+     }
+
+  FOR_EACH_BB_FN (bb, cfun)
+  {
+    curr_loop = bb->loop_father;
+    if (curr_loop == current_loops->tree_root)
+      continue;
+
+    for (loop = curr_loop;
+         loop != current_loops->tree_root;
+         loop = loop_outer (loop))
+       {
+         int  i;
+         edge e;
+         vec<edge> edges;
+         edges = get_loop_exit_edges (loop);
+         FOR_EACH_VEC_ELT (edges, i, e)
+         {
+           bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_OUT(e->src));
+           bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN(e->dest));
+         }
+      }
+  }
+}
+
+/* Move the invariants  ut of the loops.  */
 
 void
 move_loop_invariants (void)
@@ -2072,6 +2108,14 @@ move_loop_invariants (void)
       calculate_loop_reg_pressure ();
       regstat_free_n_sets_and_refs ();
     }
+  else
+    {
+      df_analyze();
+      regstat_init_n_sets_and_refs ();
+      calculate_loop_liveness();
+      regstat_free_n_sets_and_refs ();
+    }
+
   df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
   /* Process the loops, innermost first.  */
   FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
diff --git a/gcc/tree-ssa-loop-ivopts.c b/gcc/tree-ssa-loop-ivopts.c
index 945d34b..9d94c60 100644
--- a/gcc/tree-ssa-loop-ivopts.c
+++ b/gcc/tree-ssa-loop-ivopts.c
@@ -5736,8 +5736,8 @@ determine_set_costs (struct ivopts_data *data)
 
       if (get_iv (data, op))
 	continue;
-
-      n++;
+      
+      n += gimple_phi_num_args (phi);
     }
 
   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
-- 
1.8.2.1

