From patchwork Wed Oct 20 21:10:07 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 68481 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 89225B70E1 for ; Thu, 21 Oct 2010 08:10:19 +1100 (EST) Received: (qmail 16745 invoked by alias); 20 Oct 2010 21:10:17 -0000 Received: (qmail 16723 invoked by uid 22791); 20 Oct 2010 21:10:15 -0000 X-SWARE-Spam-Status: No, hits=-5.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 20 Oct 2010 21:10:10 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9KLA8Qe005056 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 20 Oct 2010 17:10:08 -0400 Received: from anchor.twiddle.home (ovpn-113-50.phx2.redhat.com [10.3.113.50]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id o9KLA7rR029515; Wed, 20 Oct 2010 17:10:07 -0400 Message-ID: <4CBF5AAF.6050103@redhat.com> Date: Wed, 20 Oct 2010 14:10:07 -0700 From: Richard Henderson User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.9) Gecko/20100921 Fedora/3.1.4-1.fc13 Thunderbird/3.1.4 MIME-Version: 1.0 To: GCC Patches CC: Jason Baron , David Daney Subject: [RFC] hot/cold attributes on labels References: <20101019214337.GD2855@redhat.com> In-Reply-To: <20101019214337.GD2855@redhat.com> X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org The motivation for this feature is to inform the compiler how it may wish to layout blocks following an asm goto. Since we can't annotate the asm goto with __builtin_expect, putting some information onto the label decl itself seems a reasonable solution. Note that one needs to use -O2 and not -Os in order to see the effects of this patch, since bb-reorder does nothing when optimizing for size -- despite any explicit option enabling block reordering. This is probably a bug. Of course, a size-sensitive block reordering pass would be Even Cooler. Comments? r~ gcc/ * doc/extend.texi (hot attribute): Document its use on labels. (cold attribute): Likewise. * predict.c (tree_estimate_probability_bb): Look for hot/cold attributes on user labels. * predict.def (PRED_HOT_LABEL, PRED_COLD_LABEL): New. gcc/c-family/ * c-common.c (handle_hot_attribute): Allow LABEL_DECLs. (handle_cold_attribute): Likewise. gcc/testsuite/ * gcc.dg/attr-hotcold-1.c: New. * gcc.dg/tree-ssa/attr-hotcold-2.c: New. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 3716b5b..21345a0 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -5767,7 +5767,8 @@ static tree handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), int ARG_UNUSED (flags), bool *no_add_attrs) { - if (TREE_CODE (*node) == FUNCTION_DECL) + if (TREE_CODE (*node) == FUNCTION_DECL + || TREE_CODE (*node) == LABEL_DECL) { if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) { @@ -5786,6 +5787,7 @@ handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), return NULL_TREE; } + /* Handle a "cold" and attribute; arguments as in struct attribute_spec.handler. */ @@ -5793,7 +5795,8 @@ static tree handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), int ARG_UNUSED (flags), bool *no_add_attrs) { - if (TREE_CODE (*node) == FUNCTION_DECL) + if (TREE_CODE (*node) == FUNCTION_DECL + || TREE_CODE (*node) == LABEL_DECL) { if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) { diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index d39ab48..2cfbe9a 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -3040,33 +3040,53 @@ than 2.96. @item hot @cindex @code{hot} function attribute -The @code{hot} attribute is used to inform the compiler that a function is a -hot spot of the compiled program. The function is optimized more aggressively -and on many target it is placed into special subsection of the text section so -all hot functions appears close together improving locality. +The @code{hot} attribute on a function is used to inform the compiler that +the function is a hot spot of the compiled program. The function is +optimized more aggressively and on many target it is placed into special +subsection of the text section so all hot functions appears close together +improving locality. When profile feedback is available, via @option{-fprofile-use}, hot functions are automatically detected and this attribute is ignored. -The @code{hot} attribute is not implemented in GCC versions earlier -than 4.3. +The @code{hot} attribute on functions is not implemented in GCC versions +earlier than 4.3. + +@cindex @code{hot} label attribute +The @code{hot} attribute on a label is used to inform the compiler that +path following the label are more likely than paths that are not so +annotated. This attribute is used in cases where @code{__builtin_expect} +cannot be used, for instance with computed goto or @code{asm goto}. + +The @code{hot} attribute on labels is not implemented in GCC versions +earlier than 4.6. @item cold @cindex @code{cold} function attribute -The @code{cold} attribute is used to inform the compiler that a function is -unlikely executed. The function is optimized for size rather than speed and on -many targets it is placed into special subsection of the text section so all -cold functions appears close together improving code locality of non-cold parts -of program. The paths leading to call of cold functions within code are marked -as unlikely by the branch prediction mechanism. It is thus useful to mark -functions used to handle unlikely conditions, such as @code{perror}, as cold to -improve optimization of hot functions that do call marked functions in rare -occasions. - -When profile feedback is available, via @option{-fprofile-use}, hot functions +The @code{cold} attribute on functions is used to inform the compiler that +the function is unlikely to be executed. The function is optimized for +size rather than speed and on many targets it is placed into special +subsection of the text section so all cold functions appears close together +improving code locality of non-cold parts of program. The paths leading +to call of cold functions within code are marked as unlikely by the branch +prediction mechanism. It is thus useful to mark functions used to handle +unlikely conditions, such as @code{perror}, as cold to improve optimization +of hot functions that do call marked functions in rare occasions. + +When profile feedback is available, via @option{-fprofile-use}, cold functions are automatically detected and this attribute is ignored. -The @code{cold} attribute is not implemented in GCC versions earlier than 4.3. +The @code{cold} attribute on functions is not implemented in GCC versions +earlier than 4.3. + +@cindex @code{cold} label attribute +The @code{cold} attribute on labels is used to inform the compiler that +the path following the label is unlikely to be executed. This attribute +is used in cases where @code{__builtin_expect} cannot be used, for instance +with computed goto or @code{asm goto}. + +The @code{cold} attribute on labels is not implemented in GCC versions +earlier than 4.6. @item regparm (@var{number}) @cindex @code{regparm} attribute diff --git a/gcc/predict.c b/gcc/predict.c index eb91b87..e03d1dd 100644 --- a/gcc/predict.c +++ b/gcc/predict.c @@ -1627,6 +1627,29 @@ tree_estimate_probability_bb (basic_block bb) FOR_EACH_EDGE (e, ei, bb->succs) { + /* Predict edges to user labels with attributes. */ + if (e->dest != EXIT_BLOCK_PTR) + { + gimple_stmt_iterator gi; + for (gi = gsi_start_bb (e->dest); !gsi_end_p (gi); gsi_next (&gi)) + { + gimple stmt = gsi_stmt (gi); + tree decl; + + if (gimple_code (stmt) != GIMPLE_LABEL) + break; + decl = gimple_label_label (stmt); + if (DECL_ARTIFICIAL (decl)) + continue; + + /* Finally, we have a user-defined label. */ + if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))) + predict_edge_def (e, PRED_COLD_LABEL, NOT_TAKEN); + else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl))) + predict_edge_def (e, PRED_HOT_LABEL, TAKEN); + } + } + /* Predict early returns to be probable, as we've already taken care for error returns and other cases are often used for fast paths through function. diff --git a/gcc/predict.def b/gcc/predict.def index 4b3e87a..73a22eb 100644 --- a/gcc/predict.def +++ b/gcc/predict.def @@ -116,3 +116,10 @@ DEF_PREDICTOR (PRED_NULL_RETURN, "null return", HITRATE (90), 0) /* Branches to a mudflap bounds check are extremely unlikely. */ DEF_PREDICTOR (PRED_MUDFLAP, "mudflap check", PROB_VERY_LIKELY, 0) + +/* Branches to hot labels are likely. */ +DEF_PREDICTOR (PRED_HOT_LABEL, "hot label", HITRATE (85), 0) + +/* Branches to cold labels are extremely unlikely. */ +DEF_PREDICTOR (PRED_COLD_LABEL, "cold label", PROB_VERY_LIKELY, + PRED_FLAG_FIRST_MATCH) diff --git a/gcc/testsuite/gcc.dg/attr-hotcold-1.c b/gcc/testsuite/gcc.dg/attr-hotcold-1.c new file mode 100644 index 0000000..f63a95c --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-hotcold-1.c @@ -0,0 +1,8 @@ +void f(void) +{ + goto A; + A: __attribute__((cold)) + goto B; + B: __attribute__((hot)) + return; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c b/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c new file mode 100644 index 0000000..ae6d383 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-profile-details" } */ + +void g(void); +void h(void); +void f(int x, int y) +{ + if (x) goto A; + if (y) goto B; + return; + + A: __attribute__((cold)) + g(); + return; + + B: __attribute__((hot)) + h(); + return; +} + +/* { dg-final { scan-tree-dump-times "BLOCK 4 freq:1\[^0-9\]" 1 "profile" } } */ + +/* Note: we're attempting to match some number > 6000, i.e. > 60%. + The exact number ought to be tweekable without having to juggle + the testcase around too much. */ +/* { dg-final { scan-tree-dump-times "BLOCK 5 freq:\[6-9\]\[0-9\]\[0-9\]\[0-9\]" 1 "profile" } } */ + +/* { dg-final { cleanup-tree-dump "profile" } } */