This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Value profile based optimizations, part 2


Hello,

here is the second part of vpt patch; it adds an infrastructure to
read back the profile information and to pass it to optimizers.

Zdenek

	* combine.c (distribute_notes): Cancel REG_VALUE_PROFILE notes.
	* gcov-io.h (GCOV_FIRST_VALUE_COUNTER, GCOV_LAST_VALUE_COUNTER,
	GCOV_N_VALUE_COUNTERS): New.
	* profile.c (compute_value_histograms): New static function.
	(branch_prob): Read back the value histograms.
	* rtl.c (reg_note_name): Add name for REG_VALUE_PROFILE note.
	* rtl.h (enum reg_note): Add REG_VALUE_PROFILE note.
	* value-prof.c: Add comment on reading the profile.
	* value-prof.h (COUNTER_FOR_HIST_TYPE, HIST_TYPE_FOR_COUNTER): New.
	* doc/invoke.texi (-fprofile-values): Document behavior with
	-fbranch-probabilities.

Index: combine.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/combine.c,v
retrieving revision 1.363
diff -c -3 -p -r1.363 combine.c
*** combine.c	13 Jun 2003 22:13:07 -0000	1.363
--- combine.c	26 Jun 2003 10:49:12 -0000
*************** distribute_notes (rtx notes, rtx from_in
*** 12441,12446 ****
--- 12441,12450 ----
  	  place = i3;
  	  break;
  
+ 	case REG_VALUE_PROFILE:
+ 	  /* Just get rid of this note, as it is unused later anyway.  */
+ 	  break;
+ 
  	case REG_VTABLE_REF:
  	  /* ??? Should remain with *a particular* memory load.  Given the
  	     nature of vtable data, the last insn seems relatively safe.  */
Index: gcov-io.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/gcov-io.h,v
retrieving revision 1.37
diff -c -3 -p -r1.37 gcov-io.h
*** gcov-io.h	26 Jun 2003 07:52:09 -0000	1.37
--- gcov-io.h	26 Jun 2003 10:49:12 -0000
*************** typedef HOST_WIDEST_INT gcov_type;
*** 269,281 ****
--- 269,292 ----
  #define GCOV_COUNTER_ARCS 	0  /* Arc transitions.  */
  #define GCOV_COUNTERS_SUMMABLE	1  /* Counters which can be
  				      summaried.  */
+ #define GCOV_FIRST_VALUE_COUNTER 1 /* The first of counters used for value
+ 				      profiling.  They must form a consecutive
+ 				      interval and their order must match
+ 				      the order of HIST_TYPEs in
+ 				      value-prof.h.  */
  #define GCOV_COUNTER_V_INTERVAL	1  /* Histogram of value inside an interval.  */
  #define GCOV_COUNTER_V_POW2	2  /* Histogram of exact power2 logarithm
  				      of a value.  */
  #define GCOV_COUNTER_V_SINGLE	3  /* The most common value of expression.  */
  #define GCOV_COUNTER_V_DELTA	4  /* The most common difference between
  				      consecutive values of expression.  */
+ #define GCOV_LAST_VALUE_COUNTER 4  /* The last of counters used for value
+ 				      profiling.  */
  #define GCOV_COUNTERS		5
+ 
+ /* Number of counters used for value profiling.  */
+ #define GCOV_N_VALUE_COUNTERS \
+   (GCOV_LAST_VALUE_COUNTER - GCOV_FIRST_VALUE_COUNTER + 1)
    
    /* A list of human readable names of the counters */
  #define GCOV_COUNTER_NAMES	{"arcs", "interval", "pow2", "single", "delta"}
Index: profile.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/profile.c,v
retrieving revision 1.122
diff -c -3 -p -r1.122 profile.c
*** profile.c	26 Jun 2003 07:52:09 -0000	1.122
--- profile.c	26 Jun 2003 10:49:12 -0000
*************** static rtx gen_const_delta_profiler (str
*** 114,119 ****
--- 114,120 ----
  static unsigned instrument_edges PARAMS ((struct edge_list *));
  static void instrument_values (unsigned, struct histogram_value *);
  static void compute_branch_probabilities PARAMS ((void));
+ static void compute_value_histograms (unsigned, struct histogram_value *);
  static gcov_type * get_exec_counts PARAMS ((void));
  static basic_block find_group PARAMS ((basic_block));
  static void union_groups PARAMS ((basic_block, basic_block));
*************** compute_branch_probabilities ()
*** 599,604 ****
--- 600,656 ----
    free_aux_for_blocks ();
  }
  
+ /* Load value histograms for N_VALUES values whose description is stored
+    in VALUES array from .da file.  */
+ static void
+ compute_value_histograms (unsigned n_values, struct histogram_value *values)
+ {
+   unsigned i, j, t, any;
+   unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
+   gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
+   gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
+   gcov_type *aact_count;
+  
+   for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
+     n_histogram_counters[t] = 0;
+ 
+   for (i = 0; i < n_values; i++)
+     n_histogram_counters[(int) (values[i].type)] += values[i].n_counters;
+ 
+   any = 0;
+   for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
+     {
+       histogram_counts[t] =
+ 	get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
+ 			     n_histogram_counters[t], &profile_info);
+       if (histogram_counts[t])
+ 	any = 1;
+       act_count[t] = histogram_counts[t];
+     }
+   if (!any)
+     return;
+ 
+   for (i = 0; i < n_values; i++)
+     {
+       rtx hist_list = NULL_RTX;
+       t = (int) (values[i].type);
+ 
+       aact_count = act_count[t];
+       act_count[t] += values[i].n_counters;
+       for (j = values[i].n_counters; j > 0; j--)
+ 	hist_list = alloc_EXPR_LIST (0, GEN_INT (aact_count[j - 1]), hist_list);
+       hist_list = alloc_EXPR_LIST (0, copy_rtx (values[i].value), hist_list);
+       hist_list = alloc_EXPR_LIST (0, GEN_INT (values[i].type), hist_list);
+       REG_NOTES (values[i].insn) =
+ 	      alloc_EXPR_LIST (REG_VALUE_PROFILE, hist_list,
+ 			       REG_NOTES (values[i].insn));
+     }
+ 
+   for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
+     if (histogram_counts[t])
+       free (histogram_counts[t]);
+ }
+ 
  /* Instrument and/or analyze program behavior based on program flow graph.
     In either case, this function builds a flow graph for the function being
     compiled.  The flow graph is stored in BB_GRAPH.
*************** branch_prob ()
*** 884,890 ****
      }
  
    if (flag_branch_probabilities)
!     compute_branch_probabilities ();
  
    /* For each edge not on the spanning tree, add counting code as rtl.  */
    if (profile_arc_flag
--- 936,946 ----
      }
  
    if (flag_branch_probabilities)
!     {
!       compute_branch_probabilities ();
!       if (flag_profile_values)
! 	compute_value_histograms (n_values, values);
!     }
  
    /* For each edge not on the spanning tree, add counting code as rtl.  */
    if (profile_arc_flag
Index: rtl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/rtl.c,v
retrieving revision 1.123
diff -c -3 -p -r1.123 rtl.c
*** rtl.c	20 Feb 2003 08:54:40 -0000	1.123
--- rtl.c	26 Jun 2003 10:49:12 -0000
*************** const char * const reg_note_name[] =
*** 228,234 ****
    "REG_WAS_0", "REG_RETVAL", "REG_LIBCALL", "REG_NONNEG",
    "REG_NO_CONFLICT", "REG_UNUSED", "REG_CC_SETTER", "REG_CC_USER",
    "REG_LABEL", "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
!   "REG_NOALIAS", "REG_SAVE_AREA", "REG_BR_PRED",
    "REG_FRAME_RELATED_EXPR", "REG_EH_CONTEXT", "REG_EH_REGION",
    "REG_SAVE_NOTE", "REG_MAYBE_DEAD", "REG_NORETURN",
    "REG_NON_LOCAL_GOTO", "REG_SETJMP", "REG_ALWAYS_RETURN",
--- 228,234 ----
    "REG_WAS_0", "REG_RETVAL", "REG_LIBCALL", "REG_NONNEG",
    "REG_NO_CONFLICT", "REG_UNUSED", "REG_CC_SETTER", "REG_CC_USER",
    "REG_LABEL", "REG_DEP_ANTI", "REG_DEP_OUTPUT", "REG_BR_PROB",
!   "REG_VALUE_PROFILE", "REG_NOALIAS", "REG_SAVE_AREA", "REG_BR_PRED",
    "REG_FRAME_RELATED_EXPR", "REG_EH_CONTEXT", "REG_EH_REGION",
    "REG_SAVE_NOTE", "REG_MAYBE_DEAD", "REG_NORETURN",
    "REG_NON_LOCAL_GOTO", "REG_SETJMP", "REG_ALWAYS_RETURN",
Index: rtl.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/rtl.h,v
retrieving revision 1.419
diff -c -3 -p -r1.419 rtl.h
*** rtl.h	25 Jun 2003 03:45:13 -0000	1.419
--- rtl.h	26 Jun 2003 10:49:12 -0000
*************** enum reg_note
*** 690,695 ****
--- 690,700 ----
       return.  */
    REG_BR_PROB,
  
+   /* REG_VALUE_PROFILE is attached when the profile is read in to an insn
+      before that the code to profile the value is inserted.  It contains
+      the results of profiling.  */
+   REG_VALUE_PROFILE,
+ 
    /* Attached to a call insn; indicates that the call is malloc-like and
       that the pointer returned cannot alias anything else.  */
    REG_NOALIAS,
Index: value-prof.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/value-prof.c,v
retrieving revision 1.1
diff -c -3 -p -r1.1 value-prof.c
*** value-prof.c	26 Jun 2003 09:38:34 -0000	1.1
--- value-prof.c	26 Jun 2003 10:49:12 -0000
*************** Software Foundation, 59 Temple Place - S
*** 40,48 ****
     insn_values_to_profile function.  This function is called from branch_prob
     in profile.c and the requested values are instrumented by it in the first
     compilation with -fprofile-arcs.  The optimization may then read the
!    gathered data in the second compilation with -fbranch-probablities (the
!    description of an exact way how to do it will be added here once the
!    code responsible for reading of the data is merged).  */
  
  static void insn_values_to_profile (rtx, unsigned *, struct histogram_value **);
  
--- 40,53 ----
     insn_values_to_profile function.  This function is called from branch_prob
     in profile.c and the requested values are instrumented by it in the first
     compilation with -fprofile-arcs.  The optimization may then read the
!    gathered data in the second compilation with -fbranch-probablities.
!    The measured data is appended as REG_VALUE_PROFILE note to the instrumented
!    insn.  The argument to the note consists of an EXPR_LIST where its
!    members have the following meaning (from the first to the last):
!    
!    -- type of information gathered (HIST_TYPE*)
!    -- the expression that is profiled
!    -- list of counters starting from the first one.  */
  
  static void insn_values_to_profile (rtx, unsigned *, struct histogram_value **);
  
Index: value-prof.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/value-prof.h,v
retrieving revision 1.1
diff -c -3 -p -r1.1 value-prof.h
*** value-prof.h	26 Jun 2003 09:38:34 -0000	1.1
--- value-prof.h	26 Jun 2003 10:49:12 -0000
*************** enum hist_type
*** 30,35 ****
--- 30,39 ----
  			   difference between two evaluations of a value.  */
  };
  
+ #define COUNTER_FOR_HIST_TYPE(TYPE) ((int) (TYPE) + GCOV_FIRST_VALUE_COUNTER)
+ #define HIST_TYPE_FOR_COUNTER(COUNTER) \
+   ((enum hist_type) ((COUNTER) - GCOV_FIRST_VALUE_COUNTER))
+ 
  /* The value to measure.  */
  struct histogram_value
  {
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.304
diff -c -3 -p -r1.304 invoke.texi
*** doc/invoke.texi	26 Jun 2003 10:16:31 -0000	1.304
--- doc/invoke.texi	26 Jun 2003 10:49:12 -0000
*************** exactly determine which path is taken mo
*** 4329,4334 ****
--- 4329,4338 ----
  If combined with @option{-fprofile-arcs}, it adds code so that some
  data about values of expressions in the program is gathered.
  
+ With @option{-fbranch-probabilities}, it reads back the data gathered
+ from profiling values of expressions and adds @samp{REG_VALUE_PROFILE}
+ notes to instructions for their later usage in optimizations.
+ 
  @item -fnew-ra
  @opindex fnew-ra
  Use a graph coloring register allocator.  Currently this option is meant


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]