From patchwork Fri Jun 1 08:26:27 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dehao Chen X-Patchwork-Id: 162274 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 17BB4B7010 for ; Fri, 1 Jun 2012 18:27:00 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1339144021; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Cc:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=AfPcPO0 q7lM6JaB24d21oElBHEk=; b=d2h4m2vinFMMIP3VV7RWGktJ4CiqF1tZnBaA1w4 CU4S8H21d/wY6IiVcE1tam3PXyCTpMCF3j1ZatVbi9Nm0euWmnZ1JjJFrHSgRiad 3szHzB280XX0UOJqVQ/sjp86sGIXXqN47AvomsdJXLu7fVZ1u6NtC0mXyD4OCFnT anhU= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Cc:Content-Type:X-System-Of-Record:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=X4l8gfF+3ytYZe5hJoUjsSyGy3Z9I1Ev3zW0g2vhL12hT1e2DWUQdm5lFKBXBW GYTN6GZN0IufejZ/U5hfIQg/U40PWwjd42qQ7jummeKXlFiTsiiB9kftBne2G2Q/ c9AiNnYHbuYDUZhdtxLS9RgWduSuiCGYocoAatHqWzwnI=; Received: (qmail 18374 invoked by alias); 1 Jun 2012 08:26:53 -0000 Received: (qmail 18312 invoked by uid 22791); 1 Jun 2012 08:26:43 -0000 X-SWARE-Spam-Status: No, hits=-4.6 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_FD, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-pb0-f47.google.com (HELO mail-pb0-f47.google.com) (209.85.160.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 01 Jun 2012 08:26:28 +0000 Received: by pbbrq2 with SMTP id rq2so2584043pbb.20 for ; Fri, 01 Jun 2012 01:26:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:date:message-id:subject:from:to:cc:content-type :x-system-of-record:x-gm-message-state; bh=KaUIpY4b3/purMyCHnkRZpmRaRJh1vuyhLgSz1gzT9M=; b=K1Uju6tiUVHyX6vZ7Cdsaa90TX8CHwz5TmkLkIj59m1HTuyQFoMLQ+S/k61xUsuobI Qd4diNn0MD28le1F68BnnAVa1ZFrc0MYZktr1DZnPpUllbQrA8gpDvByInqcKkZFW5Th 8M2/NUIy3oQ7oDWnVm9fPI9mW/MY594AtcNWoTNsqzUjd6vuzzPc1/yExcsQEtGp/Uqj Du6v1imPdjSXI5RpTFE8s/ekPI6qWqDYl1YJWJBbSUCryqRr2T49fZwjJT9v7y9pj6rM /0fEyuFsb05PkO7voE1KbG2cPtnQwB1jOQ2qBizK/jCnnYaUNJTRfLkpCMY4inmi8fOe WajA== Received: by 10.68.201.136 with SMTP id ka8mr8046143pbc.124.1338539188173; Fri, 01 Jun 2012 01:26:28 -0700 (PDT) MIME-Version: 1.0 Received: by 10.68.201.136 with SMTP id ka8mr8046110pbc.124.1338539187960; Fri, 01 Jun 2012 01:26:27 -0700 (PDT) Received: by 10.68.204.198 with HTTP; Fri, 1 Jun 2012 01:26:27 -0700 (PDT) Date: Fri, 1 Jun 2012 16:26:27 +0800 Message-ID: Subject: [google] Add options to pattern match function name for hotness attributes From: Dehao Chen To: gcc-patches@gcc.gnu.org Cc: David Li X-System-Of-Record: true X-Gm-Message-State: ALoCoQkIKZ9sYIp/sBBj4oUVg2tpfv2dJyUNoQj9utvFNRSNLBdXxf0RRwsCKPJr6nXax3HThXSRyJYm5dI/NGDZVAkSgiZLCqzBOcEl4x6fFrKwvzkfg6VHN+sbjakdl9s0VtkHTSAmwzIB+gw5d+P3ZOK9NWbrZg== 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 Hi, This patch adds 4 flags to enable user to type in a list of name patterns. Compiler will match the function name with the given patterns, and add "hot", "cold", "likely_hot", "likely_cold" attributes to function declaration. The static branch prediction checks if a basic block contains call to a annotated function, and set the branch probability accordingly. Bootstrapped and passed gcc testsuites. Ok for google branches? Thanks, Dehao gcc/ChangeLog.google-4_6 2012-06-01 Dehao Chen * gcc/cgraph.c (cgraph_match_hotness_by_name): New function. (cgraph_add_hotness_attribute): New function. (cgraph_node): Add hotness attribute to function decl. * gcc/opts.c (common_handle_option): Handle new options. * gcc/predict.c (tree_bb_level_predictions): New prediction. * gcc/predict.def (PRED_LIKELY_COLD_FUNCTION): New prediction. * gcc/common.opt (flag_function_hot_list): New option. (flag_function_cold_list): New option. (flag_function_likely_hot_list): New option. (flag_function_likely_cold_list): New option. Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 188050) +++ gcc/doc/invoke.texi (working copy) @@ -362,7 +362,9 @@ -fdelete-null-pointer-checks -fdse -fdevirtualize -fdse @gol -fearly-inlining -fipa-sra -fexpensive-optimizations -ffast-math @gol -ffinite-math-only -ffloat-store -fexcess-precision=@var{style} @gol --fforward-propagate -ffp-contract=@var{style} -ffunction-sections @gol +-fforward-propagate -ffp-contract=@var{style} @gol +-ffunction-cold-list -ffunction-hot-list -ffunction-likely-cold-list @gol +-ffunction-likely-hot-list -ffunction-sections @gol -fgcse -fgcse-after-reload -fgcse-las -fgcse-lm -fgraphite-identity @gol -fgcse-sm -fif-conversion -fif-conversion2 -findirect-inlining @gol -finline-functions -finline-functions-called-once -finline-limit=@var{n} @gol @@ -8585,6 +8587,22 @@ specify this option and you may have problems with debugging if you specify both this option and @option{-g}. +@item -ffunction-cold-list +@opindex ffunction-cold-list +List of function name patterns that will be applied "cold" attribute. + +@item -ffunction-hot-list +@opindex ffunction-hot-list +List of function name patterns that will be applied "hot" attribute. + +@item -ffunction-likely-cold-list +@opindex ffunction-likely-cold-list +List of function name patterns that will be applied "likely_cold" attribute. + +@item -ffunction-likely-hot-list +@opindex ffunction-likely-hot-list +List of function name patterns that will be applied "likely_hot" attribute. + @item -fbranch-target-load-optimize @opindex fbranch-target-load-optimize Perform branch target register load optimization before prologue / epilogue Index: gcc/cgraph.c =================================================================== --- gcc/cgraph.c (revision 188050) +++ gcc/cgraph.c (working copy) @@ -520,6 +520,70 @@ } } +typedef char *char_pointer; +DEF_VEC_P(char_pointer); +DEF_VEC_ALLOC_P(char_pointer,heap); + +/* Match FNDECL's name with PATTERNS. If matched, add HOTNESS attribute + to FNDECL. + name matches with pattern, iff one of the following conditions satisfy: + 1. strcmp (name, pattern) != 0 + 2. pattern[len - 1] = '*' && strncmp (name, pattern, len - 1) != 0 */ +static bool +cgraph_match_hotness_by_name (tree fndecl, + VEC(char_pointer, heap) *patterns, + const char *hotness) +{ + unsigned i; + char_pointer n; + const char *name = lang_hooks.decl_printable_name(fndecl, 0); + + if (!name) + return false; + + FOR_EACH_VEC_ELT (char_pointer, patterns, i, n) + { + int len = strlen (n); + if ((n[len - 1] == '*' && !strncmp (name, n, len - 1)) + || !strcmp (name, n)) + { + decl_attributes (&fndecl, tree_cons ( + get_identifier (hotness), NULL, NULL), 0); + return true; + } + } + return false; +} + +/* Add hotness attributes to FNDECL. */ +static void +cgraph_add_hotness_attribute (tree fndecl) +{ + if (lookup_attribute ("cold", DECL_ATTRIBUTES (fndecl)) + || lookup_attribute ("hot", DECL_ATTRIBUTES (fndecl)) + || lookup_attribute ("likely_cold", DECL_ATTRIBUTES (fndecl)) + || lookup_attribute ("likely_hot", DECL_ATTRIBUTES (fndecl))) + return; + + if (cgraph_match_hotness_by_name (fndecl, + (VEC(char_pointer, heap) *) flag_function_cold_list, "cold")) + return; + + if (cgraph_match_hotness_by_name (fndecl, + (VEC(char_pointer, heap) *) flag_function_hot_list, "hot")) + return; + + if (cgraph_match_hotness_by_name (fndecl, + (VEC(char_pointer, heap) *) flag_function_likely_cold_list, + "likely_cold")) + return; + + if (cgraph_match_hotness_by_name (fndecl, + (VEC(char_pointer, heap) *) flag_function_likely_hot_list, + "likely_hot")) + return; +} + /* Return cgraph node assigned to DECL. Create new one when needed. */ struct cgraph_node * @@ -554,6 +618,7 @@ node->origin->nested = node; } cgraph_add_assembler_hash_node (node); + cgraph_add_hotness_attribute (decl); return node; } Index: gcc/opts.c =================================================================== --- gcc/opts.c (revision 188050) +++ gcc/opts.c (working copy) @@ -1539,6 +1539,26 @@ (&opts->x_flag_instrument_functions_exclude_files, arg); break; + case OPT_ffunction_hot_list_: + add_comma_separated_to_vector + (&opts->x_flag_function_hot_list, arg); + break; + + case OPT_ffunction_cold_list_: + add_comma_separated_to_vector + (&opts->x_flag_function_cold_list, arg); + break; + + case OPT_ffunction_likely_hot_list_: + add_comma_separated_to_vector + (&opts->x_flag_function_likely_hot_list, arg); + break; + + case OPT_ffunction_likely_cold_list_: + add_comma_separated_to_vector + (&opts->x_flag_function_likely_cold_list, arg); + break; + case OPT_fmessage_length_: pp_set_line_maximum_length (dc->printer, value); break; Index: gcc/predict.c =================================================================== --- gcc/predict.c (revision 188050) +++ gcc/predict.c (working copy) @@ -2070,11 +2070,22 @@ predict_paths_leading_to (bb, PRED_NORETURN, NOT_TAKEN); decl = gimple_call_fndecl (stmt); - if (decl - && lookup_attribute ("cold", - DECL_ATTRIBUTES (decl))) - predict_paths_leading_to (bb, PRED_COLD_FUNCTION, - NOT_TAKEN); + if (decl) + { + tree attr = DECL_ATTRIBUTES (decl); + if (lookup_attribute ("cold", attr)) + predict_paths_leading_to (bb, PRED_COLD_FUNCTION, + NOT_TAKEN); + else if (lookup_attribute ("hot", attr)) + predict_paths_leading_to (bb, PRED_COLD_FUNCTION, + TAKEN); + else if (lookup_attribute ("likely_cold", attr)) + predict_paths_leading_to (bb, PRED_LIKELY_COLD_FUNCTION, + NOT_TAKEN); + else if (lookup_attribute ("likely_hot", attr)) + predict_paths_leading_to (bb, PRED_LIKELY_COLD_FUNCTION, + TAKEN); + } } else if (gimple_code (stmt) == GIMPLE_PREDICT) { Index: gcc/ChangeLog.google-4_6 =================================================================== --- gcc/ChangeLog.google-4_6 (revision 188050) +++ gcc/ChangeLog.google-4_6 (working copy) @@ -1,3 +1,16 @@ +2012-06-01 Dehao Chen + + * gcc/cgraph.c (cgraph_match_hotness_by_name): New function. + (cgraph_add_hotness_attribute): New function. + (cgraph_node): Add hotness attribute to function decl. + * gcc/opts.c (common_handle_option): Handle new options. + * gcc/predict.c (tree_bb_level_predictions): New prediction. + * gcc/predict.def (PRED_LIKELY_COLD_FUNCTION): New prediction. + * gcc/common.opt (flag_function_hot_list): New option. + (flag_function_cold_list): New option. + (flag_function_likely_hot_list): New option. + (flag_function_likely_cold_list): New option. + 2012-05-30 Cary Coutant * gcc/dwarf2out.c (index_location_lists): Don't index location Index: gcc/predict.def =================================================================== --- gcc/predict.def (revision 188050) +++ gcc/predict.def (working copy) @@ -76,6 +76,10 @@ DEF_PREDICTOR (PRED_COLD_FUNCTION, "cold function call", PROB_VERY_LIKELY, PRED_FLAG_FIRST_MATCH) +/* Branch to basic block containing call marked by likely_cold function attribute. */ +DEF_PREDICTOR (PRED_LIKELY_COLD_FUNCTION, "likely cold function call", + HITRATE (75), PRED_FLAG_FIRST_MATCH) + /* Loopback edge is taken. */ DEF_PREDICTOR (PRED_LOOP_BRANCH, "loop branch", HITRATE (86), PRED_FLAG_FIRST_MATCH) Index: gcc/common.opt =================================================================== --- gcc/common.opt (revision 188050) +++ gcc/common.opt (working copy) @@ -92,6 +92,20 @@ Variable void *flag_instrument_functions_exclude_files +; These four are VEC(char_p,heap) * that indicate function hotness. + +Variable +void *flag_function_hot_list + +Variable +void *flag_function_cold_list + +Variable +void *flag_function_likely_hot_list + +Variable +void *flag_function_likely_cold_list + ; Generic structs (e.g. templates not explicitly specialized) ; may not have a compilation unit associated with them, and so ; may need to be treated differently from ordinary structs. @@ -1242,6 +1256,22 @@ Common Report Var(flag_function_sections) Place each function into its own section +ffunction-hot-list= +Common RejectNegative Joined +-ffunction-hot-list=name,... Predict listed functions as hot + +ffunction-cold-list= +Common RejectNegative Joined +-ffunction-cold-list=name,... Predict listed functions as cold + +ffunction-likely-hot-list= +Common RejectNegative Joined +-ffunction-likely-hot-list=name,... Predict listed functions as likely_hot + +ffunction-likely-cold-list= +Common RejectNegative Joined +-ffunction-likely-cold-list=name,... Predict listed functions as likely_cold + fgcda= Common Joined RejectNegative Var(gcov_da_name) Set the gcov data file name.