diff mbox

Mark abort, trap, unreachable and C++ EH and terminations as cold

Message ID 20170609165315.GA77879@kam.mff.cuni.cz
State New
Headers show

Commit Message

Jan Hubicka June 9, 2017, 4:53 p.m. UTC
Hi,
this patch marks the obvious candidates for cold attribute and enables
cold auto-detection on some common coding patterns.

I have bootstrapped/regtested x86_64-linux and plan to commit it at
weekend once performance testers pick up this weeks changes if there
are no complains.

Honza

	* builtin-attrs.def (ATTR_NORETURN_NOTHROW_LEAF_COLD_LIST,
	ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST,
	ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST): New.
	* builtins.def (abort, trap, unreachable): Declare cold.
	* calls.c (flags_from_decl_or_type): Lookup ECF_COLD.
	* tree-core.h (ECF_COLD): New.
	* tree.c (set_call_expr_flags): Handle ECF_COLD.
	(build_common_builtin_nodes): Mark unreachable and abort as cold.

	* class.c (build_vtbl_initializer): Mark dvirt_fn as cold.
	* decl.c (cxx_init_decl_processing, push_throw_library_fn): Likewise.
	(excpet.c): Mark terminate as cold.

	* gcc.dg/predict-14.c: Avoid cold function detection.
	* gcc.target/i386/umod-3.c: Likewise.

Comments

Nathan Sidwell June 12, 2017, 11:52 a.m. UTC | #1
On 06/09/2017 12:53 PM, Jan Hubicka wrote:
> Hi,
> this patch marks the obvious candidates for cold attribute and enables
> cold auto-detection on some common coding patterns.

> 	* class.c (build_vtbl_initializer): Mark dvirt_fn as cold.
> 	* decl.c (cxx_init_decl_processing, push_throw_library_fn): Likewise.
> 	(excpet.c): Mark terminate as cold.


My only complaint about the C++ bits is the 'excpet' typo in the changelog!

nathan
Jan Hubicka June 12, 2017, 2:29 p.m. UTC | #2
> On 06/09/2017 12:53 PM, Jan Hubicka wrote:
> >Hi,
> >this patch marks the obvious candidates for cold attribute and enables
> >cold auto-detection on some common coding patterns.
> 
> >	* class.c (build_vtbl_initializer): Mark dvirt_fn as cold.
> >	* decl.c (cxx_init_decl_processing, push_throw_library_fn): Likewise.
> >	(excpet.c): Mark terminate as cold.
> 
> 
> My only complaint about the C++ bits is the 'excpet' typo in the changelog!

Thank you! I will try to mind to fix it at next commit.
I wonder if there are any other obvious candidates for branch prediction
in C++ FE?

Honza

> 
> nathan
> 
> -- 
> Nathan Sidwell
Nathan Sidwell June 15, 2017, 12:07 p.m. UTC | #3
On 06/12/2017 10:29 AM, Jan Hubicka wrote:

> I wonder if there are any other obvious candidates for branch prediction
> in C++ FE?

Not that I can think of.

I know others have mentioned exit, and you've commented.  It does seem 
to me to be not particularly hot[1], and optimizing speed of exit is 
probably only a concern for 'true' and 'false'.

nathan

[1] Britishism for 'extremely cold'
diff mbox

Patch

Index: builtin-attrs.def
===================================================================
--- builtin-attrs.def	(revision 249057)
+++ builtin-attrs.def	(working copy)
@@ -136,6 +136,8 @@  DEF_ATTR_TREE_LIST (ATTR_NORETURN_NOTHRO
 			ATTR_NULL, ATTR_NOTHROW_LIST)
 DEF_ATTR_TREE_LIST (ATTR_NORETURN_NOTHROW_LEAF_LIST, ATTR_NORETURN,\
 			ATTR_NULL, ATTR_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_NORETURN_NOTHROW_LEAF_COLD_LIST, ATTR_COLD,\
+			ATTR_NULL, ATTR_NORETURN_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_RT_NOTHROW_LEAF_LIST, ATTR_RETURNS_TWICE,\
 			ATTR_NULL, ATTR_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_COLD_NOTHROW_LEAF_LIST, ATTR_COLD,\
@@ -144,6 +146,8 @@  DEF_ATTR_TREE_LIST (ATTR_COLD_NORETURN_N
 			ATTR_NULL, ATTR_NORETURN_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST, ATTR_CONST,\
 			ATTR_NULL, ATTR_NORETURN_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST, ATTR_COLD,\
+			ATTR_NULL, ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_LIST, ATTR_MALLOC,	\
 			ATTR_NULL, ATTR_NOTHROW_LIST)
 DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_LEAF_LIST, ATTR_MALLOC,	\
@@ -387,6 +391,9 @@  DEF_ATTR_TREE_LIST (ATTR_TMPURE_NOTHROW_
 		    ATTR_TM_TMPURE, ATTR_NULL, ATTR_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST,
 		    ATTR_TM_TMPURE, ATTR_NULL, ATTR_NORETURN_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST,
+		    ATTR_COLD, ATTR_NULL,
+		    ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST)
 
 /* Construct a tree for a format_arg attribute.  */
 #define DEF_FORMAT_ARG_ATTRIBUTE(FA)					\
Index: builtins.def
===================================================================
--- builtins.def	(revision 249057)
+++ builtins.def	(working copy)
@@ -775,7 +775,7 @@  DEF_GCC_BUILTIN        (BUILT_IN_UMULL_O
 DEF_GCC_BUILTIN        (BUILT_IN_UMULLL_OVERFLOW, "umulll_overflow", BT_FN_BOOL_ULONGLONG_ULONGLONG_ULONGLONGPTR, ATTR_NOTHROW_NONNULL_LEAF_LIST)
 
 /* Category: miscellaneous builtins.  */
-DEF_LIB_BUILTIN        (BUILT_IN_ABORT, "abort", BT_FN_VOID, ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_ABORT, "abort", BT_FN_VOID, ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST)
 DEF_LIB_BUILTIN        (BUILT_IN_ABS, "abs", BT_FN_INT_INT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_AGGREGATE_INCOMING_ADDRESS, "aggregate_incoming_address", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST)
@@ -881,8 +881,8 @@  DEF_GCC_BUILTIN        (BUILT_IN_SAVEREG
 DEF_GCC_BUILTIN        (BUILT_IN_SETJMP, "setjmp", BT_FN_INT_PTR, ATTR_RT_NOTHROW_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRFMON, "strfmon", BT_FN_SSIZE_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_STRFMON_NOTHROW_3_4)
 DEF_LIB_BUILTIN        (BUILT_IN_STRFTIME, "strftime", BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_TM_PTR, ATTR_FORMAT_STRFTIME_NOTHROW_3_0)
-DEF_GCC_BUILTIN        (BUILT_IN_TRAP, "trap", BT_FN_VOID, ATTR_NORETURN_NOTHROW_LEAF_LIST)
-DEF_GCC_BUILTIN        (BUILT_IN_UNREACHABLE, "unreachable", BT_FN_VOID, ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST)
+DEF_GCC_BUILTIN        (BUILT_IN_TRAP, "trap", BT_FN_VOID, ATTR_NORETURN_NOTHROW_LEAF_COLD_LIST)
+DEF_GCC_BUILTIN        (BUILT_IN_UNREACHABLE, "unreachable", BT_FN_VOID, ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_UNWIND_INIT, "unwind_init", BT_FN_VOID, ATTR_NULL)
 DEF_GCC_BUILTIN        (BUILT_IN_UPDATE_SETJMP_BUF, "update_setjmp_buf", BT_FN_VOID_PTR, ATTR_NULL)
 DEF_GCC_BUILTIN        (BUILT_IN_VA_COPY, "va_copy", BT_FN_VOID_VALIST_REF_VALIST_ARG, ATTR_NOTHROW_LEAF_LIST)
Index: calls.c
===================================================================
--- calls.c	(revision 249057)
+++ calls.c	(working copy)
@@ -821,6 +821,8 @@  flags_from_decl_or_type (const_tree exp)
 	flags |= ECF_NOVOPS;
       if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
 	flags |= ECF_LEAF;
+      if (lookup_attribute ("cold", DECL_ATTRIBUTES (exp)))
+	flags |= ECF_COLD;
 
       if (TREE_NOTHROW (exp))
 	flags |= ECF_NOTHROW;
Index: cp/class.c
===================================================================
--- cp/class.c	(revision 249057)
+++ cp/class.c	(working copy)
@@ -9727,7 +9727,7 @@  build_vtbl_initializer (tree binfo,
 		    dvirt_fn = push_library_fn
 		      (name,
 		       build_function_type_list (void_type_node, NULL_TREE),
-		       NULL_TREE, ECF_NORETURN);
+		       NULL_TREE, ECF_NORETURN | ECF_COLD);
 		}
 	      fn = dvirt_fn;
 	      if (!TARGET_VTABLE_USES_DESCRIPTORS)
Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 249057)
+++ cp/decl.c	(working copy)
@@ -4269,7 +4269,7 @@  cxx_init_decl_processing (void)
 
   abort_fndecl
     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
-			    ECF_NORETURN | ECF_NOTHROW);
+			    ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
 
   /* Perform other language dependent initializations.  */
   init_class_processing ();
@@ -4550,7 +4550,7 @@  push_void_library_fn (tree name, tree pa
 tree
 push_throw_library_fn (tree name, tree type)
 {
-  tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
+  tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
   return fn;
 }
 
Index: cp/except.c
===================================================================
--- cp/except.c	(revision 249057)
+++ cp/except.c	(working copy)
@@ -54,9 +54,10 @@  init_exception_processing (void)
   push_namespace (std_identifier);
   tmp = build_function_type_list (void_type_node, NULL_TREE);
   terminate_fn = build_cp_library_fn_ptr ("terminate", tmp,
-					   ECF_NOTHROW | ECF_NORETURN);
-  TREE_THIS_VOLATILE (terminate_fn) = 1;
-  TREE_NOTHROW (terminate_fn) = 1;
+					   ECF_NOTHROW | ECF_NORETURN
+					   | ECF_COLD);
+  gcc_checking_assert (TREE_THIS_VOLATILE (terminate_fn)
+		       && TREE_NOTHROW (terminate_fn));
   pop_namespace ();
 
   /* void __cxa_call_unexpected(void *); */
Index: testsuite/gcc.dg/predict-14.c
===================================================================
--- testsuite/gcc.dg/predict-14.c	(revision 249057)
+++ testsuite/gcc.dg/predict-14.c	(working copy)
@@ -1,16 +1,18 @@ 
 /* { dg-do compile } */
 /* { dg-options "-O2 -fdump-tree-profile_estimate" } */
 
+void test(void) __attribute__ ((noreturn));
+
 int main(int argc, char **argv)
 {
   switch (argc)
     {
     case 1:
-      __builtin_abort();
+      test();
     case 4:
-      __builtin_abort();
+      test();
     default:
-      __builtin_abort();
+      test();
     }
 
   return 10;
Index: testsuite/gcc.target/i386/umod-3.c
===================================================================
--- testsuite/gcc.target/i386/umod-3.c	(revision 249057)
+++ testsuite/gcc.target/i386/umod-3.c	(working copy)
@@ -1,7 +1,6 @@ 
 /* { dg-do compile } */
 /* { dg-options "-O2 -mtune=atom" } */
 
-extern void abort (void);
 extern void exit (int);
 
 unsigned char cx = 7;
@@ -11,8 +10,8 @@  main ()
 {
   unsigned char cy;
   
-  cy = cx / 6; if (cy != 1) abort ();
-  cy = cx % 6; if (cy != 1) abort ();
+  cy = cx / 6; if (cy != 1) exit (1);
+  cy = cx % 6; if (cy != 1) exit (1);
 
   exit(0);
 }
Index: tree-core.h
===================================================================
--- tree-core.h	(revision 249057)
+++ tree-core.h	(working copy)
@@ -93,6 +93,9 @@  struct die_struct;
 /* Nonzero if this is an indirect call by descriptor.  */
 #define ECF_BY_DESCRIPTOR	  (1 << 14)
 
+/* Nonzero if this is a cold function.  */
+#define ECF_COLD		  (1 << 15)
+
 /* Call argument flags.  */
 /* Nonzero if the argument is not dereferenced recursively, thus only
    directly reachable memory is read or written.  */
Index: tree.c
===================================================================
--- tree.c	(revision 249057)
+++ tree.c	(working copy)
@@ -10625,6 +10625,9 @@  set_call_expr_flags (tree decl, int flag
   if (flags & ECF_LEAF)
     DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
 					NULL, DECL_ATTRIBUTES (decl));
+  if (flags & ECF_COLD)
+    DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("cold"),
+					NULL, DECL_ATTRIBUTES (decl));
   if (flags & ECF_RET1)
     DECL_ATTRIBUTES (decl)
       = tree_cons (get_identifier ("fn spec"),
@@ -10674,11 +10677,11 @@  build_common_builtin_nodes (void)
 			      BUILT_IN_UNREACHABLE,
 			      "__builtin_unreachable",
 			      ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
-			      | ECF_CONST);
+			      | ECF_CONST | ECF_COLD);
       if (!builtin_decl_explicit_p (BUILT_IN_ABORT))
 	local_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT,
 			      "abort",
-			      ECF_LEAF | ECF_NORETURN | ECF_CONST);
+			      ECF_LEAF | ECF_NORETURN | ECF_CONST | ECF_COLD);
     }
 
   if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)