[cfg-branch] function sections

Jan Hubicka jh@suse.cz
Thu Dec 20 13:22:00 GMT 2001


Hi,
not quite function splitting yet, but should save some cache and reduce branch distances.

Honza

Thu Dec 20 22:10:25 CET 2001 Jan Hubicka  <jh@suse.cz>
	* function.h (function): Add new filed function_frequency.
	* predict.c (compute_function_frequency): New function.
	(estimate_probability): Call it.
	* varasm.c (in_section, unlikely_executed_text_section,
	hot_text_section): New static function.
	(function_section): Use them.
	* elfos.h (HOT_TEXT_SECTION_ASM_OP,
	UNLIKELY_EXECUTED_TEXT_SECITON_ASM_OP): New.
Index: function.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/function.h,v
retrieving revision 1.71.2.1
diff -c -3 -p -r1.71.2.1 function.h
*** function.h	2001/12/14 23:08:12	1.71.2.1
--- function.h	2001/12/20 21:08:57
*************** struct function
*** 472,477 ****
--- 472,483 ----
  
    /* Nonzero if code to initialize arg_pointer_save_area has been emited.  */
    unsigned int arg_pointer_save_area_init : 1;
+ 
+   enum function_frequency {
+     FUNCTION_FREQUENCY_COLD,
+     FUNCTION_FREQUENCY_HOT,
+     FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
+   } function_frequency;
  };
  
  /* The function currently being compiled.  */
Index: predict.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/predict.c,v
retrieving revision 1.44.2.14
diff -c -3 -p -r1.44.2.14 predict.c
*** predict.c	2001/12/14 23:08:22	1.44.2.14
--- predict.c	2001/12/20 21:08:57
*************** static void process_note_predictions	 PA
*** 71,76 ****
--- 78,84 ----
  static void process_note_prediction	 PARAMS ((basic_block, int *, int *,
                                                    sbitmap *, int, int));
  static bool last_basic_block_p           PARAMS ((basic_block));
+ static void compute_function_frequency	 PARAMS ((void));
  
  /* Information we hold about each branch predictor.
     Filled using information from predict.def.  */
*************** estimate_probability (loops_info)
*** 554,559 ****
--- 599,605 ----
    sbitmap_vector_free (dominators);
  
    estimate_bb_frequencies (loops_info);
+   compute_function_frequency ();
  }
  
  /* __builtin_expect dropped tokens into the insn stream describing
*************** estimate_bb_frequencies (loops)
*** 1159,1162 ****
--- 1205,1229 ----
  
    free_aux_for_blocks ();
    free_aux_for_edges ();
+ }
+ 
+ /* Decide whether function is hot, cold or unlikely executed.  */
+ static void
+ compute_function_frequency ()
+ {
+   int i;
+   if (!flag_branch_probabilities)
+     return;
+   cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
+   for (i = 0; i < n_basic_blocks; i++)
+     {
+       basic_block bb = BASIC_BLOCK (i);
+       if (maybe_hot_bb_p (bb))
+ 	{
+ 	  cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
+ 	  return;
+ 	}
+       if (!probably_never_executed_bb_p (bb))
+ 	cfun->function_frequency = FUNCTION_FREQUENCY_COLD;
+     }
  }
Index: varasm.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/varasm.c,v
retrieving revision 1.226.2.3
diff -c -3 -p -r1.226.2.3 varasm.c
*** varasm.c	2001/12/14 23:08:45	1.226.2.3
--- varasm.c	2001/12/20 21:08:59
*************** static int const_str_htab_eq		PARAMS ((c
*** 190,197 ****
  static void const_str_htab_del		PARAMS ((void *));
  static void asm_emit_uninitialised	PARAMS ((tree, const char*, int, int));
  static void resolve_unique_section	PARAMS ((tree, int));
  
! static enum in_section { no_section, in_text, in_data, in_named
  #ifdef BSS_SECTION_ASM_OP
    , in_bss
  #endif
--- 190,200 ----
  static void const_str_htab_del		PARAMS ((void *));
  static void asm_emit_uninitialised	PARAMS ((tree, const char*, int, int));
  static void resolve_unique_section	PARAMS ((tree, int));
+ static void unlikely_executed_text_section PARAMS ((void));
+ static void hot_text_section		PARAMS ((void));
  
! static enum in_section { no_section, in_text, in_text_hot, in_text_unlikely,
!   in_data, in_named
  #ifdef BSS_SECTION_ASM_OP
    , in_bss
  #endif
*************** text_section ()
*** 248,253 ****
--- 251,296 ----
      }
  }
  
+ /* Tell assembler to switch to text section.  */
+ 
+ static void
+ hot_text_section ()
+ {
+   if (in_section != in_text_hot)
+     {
+ #ifdef HOT_TEXT_SECTION
+       HOT_TEXT_SECTION ();
+       in_section = in_text_het;
+ #else
+ #ifdef HOT_TEXT_SECTION_ASM_OP
+       fprintf (asm_out_file, "%s\n", HOT_TEXT_SECTION_ASM_OP);
+       in_section = in_text_hot;
+ #else
+       text_section ();
+ #endif
+ #endif
+     }
+ }
+ 
+ static void
+ unlikely_executed_text_section ()
+ {
+   if (in_section != in_text_unlikely)
+     {
+ #ifdef UNLIKELY_EXECUTED_TEXT_SECTION
+       UNLIKELY_EXECUTED_TEXT_SECTION ();
+       in_section = in_text_unlikely;
+ #else
+ #ifdef UNLIKELY_EXECUTED_TEXT_SECTION_ASM_OP
+       fprintf (asm_out_file, "%s\n", UNLIKELY_EXECUTED_TEXT_SECTION_ASM_OP);
+       in_section = in_text_unlikely;
+ #else
+       text_section ();
+ #endif
+ #endif
+     }
+ }
+ 
  /* Tell assembler to switch to data section.  */
  
  void
*************** function_section (decl)
*** 562,567 ****
--- 605,625 ----
    if (decl != NULL_TREE
        && DECL_SECTION_NAME (decl) != NULL_TREE)
      named_section (decl, (char *) 0, 0);
+   else if (decl == current_function_decl)
+     {
+       switch (cfun->function_frequency)
+ 	{
+ 	  case FUNCTION_FREQUENCY_HOT:
+ 	    hot_text_section ();
+ 	    break;
+ 	  case FUNCTION_FREQUENCY_COLD:
+ 	    text_section ();
+ 	    break;
+ 	  case FUNCTION_FREQUENCY_UNLIKELY_EXECUTED:
+ 	    unlikely_executed_text_section ();
+ 	    break;
+ 	}
+     }
    else
      text_section ();
  }



More information about the Gcc-patches mailing list