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]

Replace function specific cold attribute handling by profile


Michael,
this patch replaces function specific code setting -Os for cold function by
profile bits.  I've updated whole middle-end and i386 backend to use profile to
drive code size expanding optimizations, so -Os and __attribute__ cold now have
basically same effect.  Missing bits are the RTL cost hooks, I hope to update
them soon.

The profile based code is able to handle testcases like this:

/* { dg-do compile } */
/* { dg-options "-O2" } */
#include <string.h>
static inline
__attribute__ ((cold))
my_cold_memset (void *a, int b,int c)
{
  memset (a,b,c);
}
t(void *a,int b,int c)
{
  if (a)
    my_cold_memset (a,b,c);
}
/* The IF conditional should be predicted as cold and my_cold_memset inlined
   for size expanding memset as rep; stosb.  */
/* { dg-final { scan-assembler "stosb" } } */

Or kernel COLD inline function:
+/* Use this to mark a path cold that isn't a function call
+   Use with care. The code generation advantage isn't large and it is rarely
+   worth it to uglify your code with this. */
+static inline void __cold cold_inline(void) {}
+#define COLD()			cold_inline();
+#endif

Does this seems sane?  If so, I will deal with HOT attribute next.  Here
situation will need little bit more discussion.  I can definitly introduce
probably_hot predicates that would return true only in cases compiler is quite
sure function is hot (that would be with profile feedback or with explicit hot
attribute) and add optimize_*_aggressivly_for_speed predicates acompanying
current optimize_*_for_size/optimize_*_for_speed.

Problem is that -O3 enables optimizations that are more risky in nature, i.e.
they migh or might not be win and we are more lose on debug info.  It is not
clear (at least to me) we want to enable them all for hot function.

Bootstrapping/regtesting i686-linux, will commit it if it seems fine.
Honza

	* target.h (cold_attribute_sets_optimization): Remove.
	* predict.c (PROB_VERY_UNLIKELY): Set to be more compatible with
	HOT/COLD threshold.
	* predict.def (PRED_NORETURN, PRED_COLD_FUNCTION): Use
	PROB_VERY_LIKELY.
	* target-def.h (TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION): Remove.
	* c-common.c (handle_cold_attribute): Do not use function specifics for
	cold attribute.
	* i386.c (TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION): Remove.
Index: target.h
===================================================================
*** target.h	(revision 139800)
--- target.h	(working copy)
*************** struct gcc_target
*** 999,1007 ****
      /* Function to determine if one function can inline another function.  */
      bool (*can_inline_p) (tree, tree);
  
-     /* Whether the cold attribute changes the optimization level.  */
-     bool cold_attribute_sets_optimization;
- 
      /* Whether the hot attribute changes the optimization level.  */
      bool hot_attribute_sets_optimization;
    } target_option;
--- 999,1004 ----
Index: predict.c
===================================================================
*** predict.c	(revision 139801)
--- predict.c	(working copy)
*************** along with GCC; see the file COPYING3.  
*** 66,73 ****
  static sreal real_zero, real_one, real_almost_one, real_br_prob_base,
  	     real_inv_br_prob_base, real_one_half, real_bb_freq_max;
  
! /* Random guesstimation given names.  */
! #define PROB_VERY_UNLIKELY	(REG_BR_PROB_BASE / 100 - 1)
  #define PROB_EVEN		(REG_BR_PROB_BASE / 2)
  #define PROB_VERY_LIKELY	(REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
  #define PROB_ALWAYS		(REG_BR_PROB_BASE)
--- 66,75 ----
  static sreal real_zero, real_one, real_almost_one, real_br_prob_base,
  	     real_inv_br_prob_base, real_one_half, real_bb_freq_max;
  
! /* Random guesstimation given names.  
!    PROV_VERY_UNLIKELY should be small enough so basic block predicted
!    by it gets bellow HOT_BB_FREQUENCY_FRANCTION.  */
! #define PROB_VERY_UNLIKELY	(REG_BR_PROB_BASE / 2000 - 1)
  #define PROB_EVEN		(REG_BR_PROB_BASE / 2)
  #define PROB_VERY_LIKELY	(REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
  #define PROB_ALWAYS		(REG_BR_PROB_BASE)
Index: predict.def
===================================================================
*** predict.def	(revision 139800)
--- predict.def	(working copy)
*************** DEF_PREDICTOR (PRED_LOOP_ITERATIONS_GUES
*** 69,79 ****
  DEF_PREDICTOR (PRED_CONTINUE, "continue", HITRATE (50), 0)
  
  /* Branch to basic block containing call marked by noreturn attribute.  */
! DEF_PREDICTOR (PRED_NORETURN, "noreturn call", HITRATE (99),
  	       PRED_FLAG_FIRST_MATCH)
  
  /* Branch to basic block containing call marked by cold function attribute.  */
! DEF_PREDICTOR (PRED_COLD_FUNCTION, "cold function call", HITRATE (99),
  	       PRED_FLAG_FIRST_MATCH)
  
  /* Loopback edge is taken.  */
--- 69,79 ----
  DEF_PREDICTOR (PRED_CONTINUE, "continue", HITRATE (50), 0)
  
  /* Branch to basic block containing call marked by noreturn attribute.  */
! DEF_PREDICTOR (PRED_NORETURN, "noreturn call", PROB_VERY_LIKELY,
  	       PRED_FLAG_FIRST_MATCH)
  
  /* Branch to basic block containing call marked by cold function attribute.  */
! DEF_PREDICTOR (PRED_COLD_FUNCTION, "cold function call", PROB_VERY_LIKELY,
  	       PRED_FLAG_FIRST_MATCH)
  
  /* Loopback edge is taken.  */
Index: target-def.h
===================================================================
*** target-def.h	(revision 139800)
--- target-def.h	(working copy)
***************
*** 789,798 ****
  #define TARGET_OPTION_CAN_INLINE_P default_target_option_can_inline_p
  #endif
  
- #ifndef TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION
- #define TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION false
- #endif
- 
  #ifndef TARGET_OPTION_HOT_ATTRIBUTE_SETS_OPTIMIZATION
  #define TARGET_OPTION_HOT_ATTRIBUTE_SETS_OPTIMIZATION false
  #endif
--- 789,794 ----
***************
*** 805,811 ****
      TARGET_OPTION_PRINT,			\
      TARGET_OPTION_PRAGMA_PARSE,			\
      TARGET_OPTION_CAN_INLINE_P,			\
-     TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION, \
      TARGET_OPTION_HOT_ATTRIBUTE_SETS_OPTIMIZATION, \
    }
  
--- 801,806 ----
Index: c-common.c
===================================================================
*** c-common.c	(revision 139800)
--- c-common.c	(working copy)
*************** handle_cold_attribute (tree *node, tree 
*** 5314,5343 ****
  	}
        else
  	{
! 	  tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
! 
! 	  /* If we are optimizing, but not optimizing for space, turn on -Os
! 	     optimizations just for this one function.  */
! 	  if (optimize && !optimize_size
! 	      && targetm.target_option.cold_attribute_sets_optimization
! 	      && (!old_opts || old_opts == optimization_default_node))
! 	    {
! 	      /* Create the cold optimization node if needed.  */
! 	      if (!optimization_cold_node)
! 		{
! 		  struct cl_optimization current_options;
! 		  static const char *os_argv[] = { NULL, "-Os", NULL };
! 
! 		  cl_optimization_save (&current_options);
! 		  decode_options (2, os_argv);
! 		  optimization_cold_node = build_optimization_node ();
! 		  cl_optimization_restore (&current_options);
! 		}
! 
! 	      DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
! 		= optimization_cold_node;
! 	    }
! 	  /* Most of the rest of the cold processing is done later with
  	     lookup_attribute.  */
  	}
      }
--- 5314,5320 ----
  	}
        else
  	{
! 	  /* Most of the cold processing is done later with
  	     lookup_attribute.  */
  	}
      }
Index: config/i386/i386.c
===================================================================
*** config/i386/i386.c	(revision 139800)
--- config/i386/i386.c	(working copy)
*************** ix86_enum_va_list (int idx, const char *
*** 29039,29047 ****
  #undef TARGET_OPTION_CAN_INLINE_P
  #define TARGET_OPTION_CAN_INLINE_P ix86_can_inline_p
  
- #undef TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION
- #define TARGET_OPTION_COLD_ATTRIBUTE_SETS_OPTIMIZATION true
- 
  #undef TARGET_OPTION_HOT_ATTRIBUTE_SETS_OPTIMIZATION
  #define TARGET_OPTION_HOT_ATTRIBUTE_SETS_OPTIMIZATION true
  
--- 29039,29044 ----


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