Fix profile use in computed goto duplication

Jan Hubicka hubicka@ucw.cz
Tue Jun 9 15:03:00 GMT 2009


Hi,
this is patch I made looking into PR 39283.  There is obvious bug in
test testing whether code should be duplicated, since it is
!optimize_for_size_p.  Interestingly it combines with another problem
that computed goto blocks tends to have very many incomming edges where
all are cold.
This patch fixes both problem that funilly enough results in no-op on
the testcase itself (there is other problem, see the PR itself), but
should help elsewhere.

Bootstrapped/regtested i686-linux, will commit it later today if there
are no complains.

	* bb-reorder.c (duplicate_computed_gotos): Always duplicate when
	computed goto is same size as unconditional jump; fix use of profile.
Index: bb-reorder.c
===================================================================
--- bb-reorder.c	(revision 148296)
+++ bb-reorder.c	(working copy)
@@ -1994,6 +1994,7 @@ duplicate_computed_gotos (void)
 {
   basic_block bb, new_bb;
   bitmap candidates;
+  bitmap tiny_candidates;
   int max_size;
 
   if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1)
@@ -2009,6 +2010,7 @@ duplicate_computed_gotos (void)
 
   max_size = uncond_jump_length * PARAM_VALUE (PARAM_MAX_GOTO_DUPLICATION_INSNS);
   candidates = BITMAP_ALLOC (NULL);
+  tiny_candidates = BITMAP_ALLOC (NULL);
 
   /* Look for blocks that end in a computed jump, and see if such blocks
      are suitable for unfactoring.  If a block is a candidate for unfactoring,
@@ -2053,6 +2055,8 @@ duplicate_computed_gotos (void)
 	continue;
 
       bitmap_set_bit (candidates, bb->index);
+      if (size <= uncond_jump_length)
+        bitmap_set_bit (tiny_candidates, bb->index);
     }
 
   /* Nothing to do if there is no computed jump here.  */
@@ -2076,12 +2080,25 @@ duplicate_computed_gotos (void)
 	  || single_pred_p (single_succ (bb)))
 	continue;
 
-      if (!optimize_bb_for_size_p (bb))
-	continue;
-
       /* The successor block has to be a duplication candidate.  */
       if (!bitmap_bit_p (candidates, single_succ (bb)->index))
 	continue;
+      /* When conditional jump is smaller than size of unconditional jump,
+         we can always duplicate.  */
+      if (bitmap_bit_p (tiny_candidates, single_succ (bb)->index))
+	;
+      /* For bytecode interpreters it is very common for computed goto
+         to be reached by very many blocks.  Because they get predicted
+         evenly, we hardly every get any hot predecestors and duplication
+	 does not happen. When profile is guessed, be more aggressive and
+	 duplicate always when the computed goto itself is considered hot.  */
+      else if (profile_status != PROFILE_READ)
+	{
+          if (optimize_bb_for_size_p (single_succ (bb)))
+	    continue;
+	}
+      else if (optimize_bb_for_size_p (bb))
+	continue;
 
       new_bb = duplicate_block (single_succ (bb), single_succ_edge (bb), bb);
       new_bb->aux = bb->aux;
@@ -2093,6 +2110,7 @@ done:
   cfg_layout_finalize ();
 
   BITMAP_FREE (candidates);
+  BITMAP_FREE (tiny_candidates);
   return 0;
 }
 



More information about the Gcc-patches mailing list