diff mbox

shrink-wrap: Handle multiple predecessors of prologue

Message ID 20150922161146.GB3544@gate.crashing.org
State New
Headers show

Commit Message

Segher Boessenkool Sept. 22, 2015, 4:11 p.m. UTC
On Tue, Sep 22, 2015 at 05:47:25PM +0200, Bernd Schmidt wrote:
> On 09/21/2015 04:01 PM, Segher Boessenkool wrote:
> >On Mon, Sep 21, 2015 at 01:56:28PM +0200, Bernd Schmidt wrote:
> >>>+  basic_block new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN
> >>>(cfun)->prev_bb);
> >>>+  BB_COPY_PARTITION (new_bb, pro);
> >>[...]
> >>>+  *entry_edge = make_single_succ_edge (new_bb, pro, EDGE_FALLTHRU);
> >>>+  force_nonfallthru (*entry_edge);
> >>>+
> >>>    free_dominance_info (CDI_DOMINATORS);
> >>>  }
> >>
> >>Not sure I like creating a fallthru edge and then calling
> >>force_nonfallthru on it.
> >
> >It's just to create the edge and a branch, and to set all flags and the
> >edge probability etc. correctly for it.  (You have to first make it
> >EDGE_FALLTHRU or force_nonfallthru will complain).  Is there a nicer
> >idiom, or a helper function?  Should there be?
> 
> I tried to come up with something better and failed.

Yeah me too... non-layout RTL mode is very fragile :-(

> So, given that your solution seems to work, the patch is ok.

Thanks!  Here is what I will commit after bootstrap+test (the superfluous
assert removed, and the comment for the code quoted above tweaked a bit).


Segher

---
 gcc/function.c    |   2 +-
 gcc/shrink-wrap.c | 167 ++++++++++++++++++++++++++++++------------------------
 gcc/shrink-wrap.h |   4 +-
 3 files changed, 96 insertions(+), 77 deletions(-)
diff mbox

Patch

diff --git a/gcc/function.c b/gcc/function.c
index bb75b1c..1095465 100644
--- a/gcc/function.c
+++ b/gcc/function.c
@@ -6144,7 +6144,7 @@  thread_prologue_and_epilogue_insns (void)
      prologue/epilogue is emitted only around those parts of the
      function that require it.  */
 
-  try_shrink_wrapping (&entry_edge, orig_entry_edge, &bb_flags, prologue_seq);
+  try_shrink_wrapping (&entry_edge, &bb_flags, prologue_seq);
 
   if (split_prologue_seq != NULL_RTX)
     {
diff --git a/gcc/shrink-wrap.c b/gcc/shrink-wrap.c
index 1387594..94b1228 100644
--- a/gcc/shrink-wrap.c
+++ b/gcc/shrink-wrap.c
@@ -462,6 +462,30 @@  prepare_shrink_wrap (basic_block entry_block)
       }
 }
 
+/* Return whether basic block PRO can get the prologue.  It can not if it
+   has incoming complex edges that need a prologue inserted (we make a new
+   block for the prologue, so those edges would need to be redirected, which
+   does not work).  It also can not if there exist registers live on entry
+   to PRO that are clobbered by the prologue.  */
+
+static bool
+can_get_prologue (basic_block pro, HARD_REG_SET prologue_clobbered)
+{
+  edge e;
+  edge_iterator ei;
+  FOR_EACH_EDGE (e, ei, pro->preds)
+    if (e->flags & (EDGE_COMPLEX | EDGE_CROSSING)
+	&& !dominated_by_p (CDI_DOMINATORS, e->src, pro))
+      return false;
+
+  HARD_REG_SET live;
+  REG_SET_TO_HARD_REG_SET (live, df_get_live_in (pro));
+  if (hard_reg_set_intersect_p (live, prologue_clobbered))
+    return false;
+
+  return true;
+}
+
 /* Return whether we can duplicate basic block BB for shrink wrapping.  We
    cannot if the block cannot be duplicated at all, or if any of its incoming
    edges are complex and come from a block that does not require a prologue
@@ -478,7 +502,7 @@  can_dup_for_shrink_wrapping (basic_block bb, basic_block pro, unsigned max_size)
   edge e;
   edge_iterator ei;
   FOR_EACH_EDGE (e, ei, bb->preds)
-    if (e->flags & EDGE_COMPLEX
+    if (e->flags & (EDGE_COMPLEX | EDGE_CROSSING)
 	&& !dominated_by_p (CDI_DOMINATORS, e->src, pro))
       return false;
 
@@ -577,14 +601,13 @@  fix_fake_fallthrough_edge (edge e)
    (bb 4 is duplicated to 5; the prologue is inserted on the edge 5->3).
 
    ENTRY_EDGE is the edge where the prologue will be placed, possibly
-   changed by this function.  ORIG_ENTRY_EDGE is the edge where it
-   would be placed without shrink-wrapping.  BB_WITH is a bitmap that,
-   if we do shrink-wrap, will on return contain the interesting blocks
-   that run with prologue.  PROLOGUE_SEQ is the prologue we will insert.  */
+   changed by this function.  BB_WITH is a bitmap that, if we do shrink-
+   wrap, will on return contain the interesting blocks that run with
+   prologue.  PROLOGUE_SEQ is the prologue we will insert.  */
 
 void
-try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
-		     bitmap_head *bb_with, rtx_insn *prologue_seq)
+try_shrink_wrapping (edge *entry_edge, bitmap_head *bb_with,
+		     rtx_insn *prologue_seq)
 {
   /* If we cannot shrink-wrap, are told not to shrink-wrap, or it makes
      no sense to shrink-wrap: then do not shrink-wrap!  */
@@ -631,6 +654,9 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
 	IOR_HARD_REG_SET (prologue_used, this_used);
 	note_stores (PATTERN (insn), record_hard_reg_sets, &prologue_clobbered);
       }
+  CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
+  if (frame_pointer_needed)
+    CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
 
   /* Find out what registers are set up by the prologue; any use of these
      cannot happen before the prologue.  */
@@ -695,8 +721,9 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
 	     pro->index);
 
   /* Now see if we can put the prologue at the start of PRO.  Putting it
-     there might require duplicating a block that cannot be duplicated;
-     if so, try again with the immediate dominator of PRO, and so on.
+     there might require duplicating a block that cannot be duplicated,
+     or in some cases we cannot insert the prologue there at all.  If PRO
+     wont't do, try again with the immediate dominator of PRO, and so on.
 
      The blocks that need duplicating are those reachable from PRO but
      not dominated by it.  We keep in BB_WITH a bitmap of the blocks
@@ -714,6 +741,14 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
 
   while (!vec.is_empty () && pro != entry)
     {
+      while (pro != entry && !can_get_prologue (pro, prologue_clobbered))
+	{
+	  pro = get_immediate_dominator (CDI_DOMINATORS, pro);
+
+	  bitmap_set_bit (bb_with, pro->index);
+	  vec.quick_push (pro);
+	}
+
       basic_block bb = vec.pop ();
       if (!can_dup_for_shrink_wrapping (bb, pro, max_grow_size))
 	while (!dominated_by_p (CDI_DOMINATORS, bb, pro))
@@ -746,14 +781,18 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
     {
       calculate_dominance_info (CDI_POST_DOMINATORS);
 
+      basic_block last_ok = pro;
       while (pro != entry)
 	{
 	  basic_block pre = get_immediate_dominator (CDI_DOMINATORS, pro);
-	  if (dominated_by_p (CDI_POST_DOMINATORS, pre, pro))
-	    pro = pre;
-	  else
+	  if (!dominated_by_p (CDI_POST_DOMINATORS, pre, pro))
 	    break;
+
+	  pro = pre;
+	  if (can_get_prologue (pro, prologue_clobbered))
+	    last_ok = pro;
 	}
+      pro = last_ok;
 
       free_dominance_info (CDI_POST_DOMINATORS);
     }
@@ -762,62 +801,26 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
     fprintf (dump_file, "Bumping back to anticipatable blocks, PRO is now %d\n",
 	     pro->index);
 
-  /* If there is more than one predecessor of PRO not dominated by PRO, fail.
-     Also find that single edge that leads to PRO.  */
-
-  bool multi = false;
-  edge the_edge = 0;
-  FOR_EACH_EDGE (e, ei, pro->preds)
-    if (!dominated_by_p (CDI_DOMINATORS, e->src, pro))
-      {
-	if (the_edge)
-	  multi = true;
-	else
-	  the_edge = e;
-      }
-
-  if (multi)
+  if (pro == entry)
     {
-      the_edge = orig_entry_edge;
-
-      if (dump_file)
-	fprintf (dump_file, "More than one candidate edge.\n");
+      free_dominance_info (CDI_DOMINATORS);
+      return;
     }
 
-  if (dump_file)
-    fprintf (dump_file, "Found candidate edge for shrink-wrapping, %d->%d.\n",
-	     the_edge->src->index, the_edge->dest->index);
-
-  *entry_edge = the_edge;
-
   /* Compute what fraction of the frequency and count of the blocks that run
      both with and without prologue are for running with prologue.  This gives
      the correct answer for reducible flow graphs; for irreducible flow graphs
      our profile is messed up beyond repair anyway.  */
 
-  int num = (*entry_edge)->probability;
-  int den = REG_BR_PROB_BASE;
-
-  if (*entry_edge == orig_entry_edge)
-    goto out;
-
-  /* Test whether the prologue is known to clobber any register
-     (other than FP or SP) which are live on the edge.  */
+  gcov_type num = 0;
+  gcov_type den = 0;
 
-  HARD_REG_SET live_on_edge;
-  CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
-  if (frame_pointer_needed)
-    CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
-  REG_SET_TO_HARD_REG_SET (live_on_edge,
-			   df_get_live_in ((*entry_edge)->dest));
-  if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
-    {
-      *entry_edge = orig_entry_edge;
-      if (dump_file)
-	fprintf (dump_file,
-		 "Shrink-wrapping aborted due to clobber.\n");
-      goto out;
-    }
+  FOR_EACH_EDGE (e, ei, pro->preds)
+    if (!dominated_by_p (CDI_DOMINATORS, e->src, pro))
+      {
+	num += EDGE_FREQUENCY (e);
+	den += e->src->frequency;
+      }
 
   /* All is okay, so do it.  */
 
@@ -852,20 +855,6 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
 	dup->count -= bb->count;
       }
 
-  /* Change ENTRY_EDGE, if its src is duplicated.  Do this first, before
-     the redirects have had a chance to create new blocks on the edge we
-     want to use for the prologue, which makes us not find it.  */
-
-  gcc_assert (!dominated_by_p (CDI_DOMINATORS, (*entry_edge)->src, pro));
-
-  if (bitmap_bit_p (bb_with, (*entry_edge)->src->index))
-    {
-      basic_block src = (basic_block) (*entry_edge)->src->aux;
-      FOR_EACH_EDGE (e, ei, src->succs)
-	if (e->dest == pro)
-	  *entry_edge = e;
-    }
-
   /* Now change the edges to point to the copies, where appropriate.  */
 
   FOR_EACH_BB_FN (bb, cfun)
@@ -923,7 +912,37 @@  try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
 	    emit_barrier_after_bb (e->src);
 	  }
 
-out:
+  /* Finally, we want a single edge to put the prologue on.  Make a new
+     block before the PRO block; the edge beteen them is the edge we want.
+     Then redirect those edges into PRO that come from blocks without the
+     prologue, to point to the new block instead.  The new prologue block
+     is put at the end of the insn chain.  */
+
+  basic_block new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
+  BB_COPY_PARTITION (new_bb, pro);
+  if (dump_file)
+    fprintf (dump_file, "Made prologue block %d\n", new_bb->index);
+
+  for (ei = ei_start (pro->preds); (e = ei_safe_edge (ei)); )
+    {
+      if (bitmap_bit_p (bb_with, e->src->index)
+	  || dominated_by_p (CDI_DOMINATORS, e->src, pro))
+	{
+	  ei_next (&ei);
+	  continue;
+	}
+
+      new_bb->count += RDIV (e->src->count * e->probability, REG_BR_PROB_BASE);
+      new_bb->frequency += EDGE_FREQUENCY (e);
+
+      redirect_edge_and_branch_force (e, new_bb);
+      if (dump_file)
+	fprintf (dump_file, "Redirected edge from %d\n", e->src->index);
+    }
+
+  *entry_edge = make_single_succ_edge (new_bb, pro, EDGE_FALLTHRU);
+  force_nonfallthru (*entry_edge);
+
   free_dominance_info (CDI_DOMINATORS);
 }
 
diff --git a/gcc/shrink-wrap.h b/gcc/shrink-wrap.h
index 6819901..527a57a 100644
--- a/gcc/shrink-wrap.h
+++ b/gcc/shrink-wrap.h
@@ -24,8 +24,8 @@  along with GCC; see the file COPYING3.  If not see
 
 /* In shrink-wrap.c.  */
 extern bool requires_stack_frame_p (rtx_insn *, HARD_REG_SET, HARD_REG_SET);
-extern void try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
-				 bitmap_head *bb_flags, rtx_insn *prologue_seq);
+extern void try_shrink_wrapping (edge *entry_edge, bitmap_head *bb_flags,
+				 rtx_insn *prologue_seq);
 extern edge get_unconverted_simple_return (edge, bitmap_head,
 					   vec<edge> *, rtx_insn **);
 extern void convert_to_simple_return (edge entry_edge, edge orig_entry_edge,