From patchwork Fri Sep 13 17:28:12 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Mi X-Patchwork-Id: 274815 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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 279E22C00C7 for ; Sat, 14 Sep 2013 03:28:55 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=SyUVgQiPzn3G76Lvzr hidS6KBeElPcA0Or4o7ky5AbK+xvWD9mowDCuJw8j039SrWcxNxL/J93f9osUvZy RVOUHETQ7C6qsIGQVOdh1EIoHLGFW3K21b9RlkYeKPIaOSHVw93gDzC5eXTpzHG9 2ZW1z5F95d/7Jpvk7etK/mnHo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=j7JUfWWk7Dva9EIGX6kx0eM4 2Bc=; b=NDRL3Y6NAcPMnvfexd4FfjagsDP5pCheCdYS4hdL5V7IcqmTJMXgMPnK xS5KuYS87iC7SxUZuNstjZJ27bz0ss+cAtpQYf3X1m5YiQQoMuEmK3UOMKReQWcd UE/KanykJMVlPdS5WdpLncskn9rs8niikfuwxQfgG4qM4GV8DaU= Received: (qmail 4353 invoked by alias); 13 Sep 2013 17:28:45 -0000 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 Received: (qmail 4338 invoked by uid 89); 13 Sep 2013 17:28:44 -0000 Received: from mail-ie0-f171.google.com (HELO mail-ie0-f171.google.com) (209.85.223.171) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 13 Sep 2013 17:28:44 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, KHOP_THREADED, NO_RELAYS autolearn=ham version=3.3.2 X-HELO: mail-ie0-f171.google.com Received: by mail-ie0-f171.google.com with SMTP id at1so3252997iec.30 for ; Fri, 13 Sep 2013 10:28:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=MBXMZ01X6vhg8sg6a/bTQeMyvamyh9Q/93aEX7EJKQw=; b=SQ5rc/4+Dyfjln0NxMKXEmLBsJXju3TnDJ+6zXsyQZn2wOn+Y480LVNR57GiYxYsGN lrZ5UlrC/++ZPwTrcBZvc0aoCMgio6Dpy8AlgM2nGbGX5HwLwh/aRgtjy1+QLA1qjyue SOcHXSBrEQhljPO4+rQUY5empvazfmZmd8O23pIar9YmSRmNsy6Z1bPg5g1d4Ng3qMJn 9gJOUbTUNYHUeSl/vyLCoZQmUPIbTfsIgitHZhDcdu0mgwCZVf/6c6M3NkN5AoQJRx7N 001DsM2zZHCiFITDP2qcrB56lxwcb0gp/YJ7TfvHDQvSBSGAH2ER93bi+7BxiUSpR9GU sSFg== X-Gm-Message-State: ALoCoQnpsOBTXYOu9PeWGiTOyBtwFHPr17tY9hO+RoOlVS4QXEzJRplggpiOoHPZHWltAOaEez6pwKM4Yv67wKKwOh7n/t5mCj6p78LmcuetsR45aAtIRrky0N76aOhv+/RwvNW17wG80nPuO2fPlgaHqGdF+XtlaxGXAxaj+VRPnmBvNG052hasIQs2rbp+OTHg2QbZplUIIhDo7ZLz4STbbDH2t/BONA== MIME-Version: 1.0 X-Received: by 10.50.178.167 with SMTP id cz7mr1717492igc.7.1379093292425; Fri, 13 Sep 2013 10:28:12 -0700 (PDT) Received: by 10.64.35.193 with HTTP; Fri, 13 Sep 2013 10:28:12 -0700 (PDT) In-Reply-To: References: Date: Fri, 13 Sep 2013 10:28:12 -0700 Message-ID: Subject: Re: Fwd: [PATCH] Scheduling result adjustment to enable macro-fusion From: Wei Mi To: Alexander Monakov Cc: Steven Bosscher , GCC Patches , David Li > Thanks. At this point you need feedback from x86 and scheduler maintainers. > I would recommend you to resubmit the patch with a Changelog text, and with > the text of the patch inline in the email (your last mail has the patch as a > binary attachment, which makes it harder to review and respond to). Please > mention if the updated patch passes bootstrap and regtest. Thanks! Here is the new patch. bootstrap and regression pass. ok for trunk? 2013-09-13 Wei Mi * sched-rgn.c (add_branch_dependences): Keep insns in a SCHED_GROUP at the end of bb to remain their locations. * config/i386/x86-tune.def (DEF_TUNE): Add m_COREI7 for X86_TUNE_FUSE_CMP_AND_BRANCH. * config/i386/i386.c (ix86_macro_fusion_p): New Function. (ix86_macro_fusion_pair_p): Ditto. * doc/tm.texi.in: Generated. * doc/tm.texi: Ditto. * target.def: Add two hooks: macro_fusion_p and macro_fusion_pair_p. * haifa-sched.c (try_group_insn): New function. (group_insns_for_macro_fusion): New function. (sched_init): Call group_insns_for_macro_fusion. Index: sched-rgn.c =================================================================== --- sched-rgn.c (revision 201963) +++ sched-rgn.c (working copy) @@ -2443,6 +2443,8 @@ add_branch_dependences (rtx head, rtx ta cc0 setters remain at the end because they can't be moved away from their cc0 user. + Predecessors of SCHED_GROUP_P instructions at the end remain at the end. + COND_EXEC insns cannot be moved past a branch (see e.g. PR17808). Insns setting TARGET_CLASS_LIKELY_SPILLED_P registers (usually return @@ -2465,7 +2467,8 @@ add_branch_dependences (rtx head, rtx ta #endif || (!reload_completed && sets_likely_spilled (PATTERN (insn))))) - || NOTE_P (insn)) + || NOTE_P (insn) + || (last != 0 && SCHED_GROUP_P (last))) { if (!NOTE_P (insn)) { Index: config/i386/x86-tune.def =================================================================== --- config/i386/x86-tune.def (revision 201963) +++ config/i386/x86-tune.def (working copy) @@ -196,7 +196,8 @@ DEF_TUNE (X86_TUNE_USE_VECTOR_CONVERTS, /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction with a subsequent conditional jump instruction into a single compare-and-branch uop. */ -DEF_TUNE (X86_TUNE_FUSE_CMP_AND_BRANCH, "fuse_cmp_and_branch", m_BDVER) +DEF_TUNE (X86_TUNE_FUSE_CMP_AND_BRANCH, "fuse_cmp_and_branch", + m_COREI7 | m_BDVER) /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag will impact LEA instruction selection. */ DEF_TUNE (X86_TUNE_OPT_AGU, "opt_agu", m_ATOM | m_SLM) Index: config/i386/i386.c =================================================================== --- config/i386/i386.c (revision 201963) +++ config/i386/i386.c (working copy) @@ -24850,6 +24850,99 @@ ia32_multipass_dfa_lookahead (void) } } +/* Return true if target platform supports macro-fusion. */ + +static bool +ix86_macro_fusion_p () +{ + if (TARGET_FUSE_CMP_AND_BRANCH) + return true; + else + return false; +} + +/* Check whether current microarchitecture support macro fusion + for insn pair "CONDGEN + CONDJMP". Refer to + "Intel Architectures Optimization Reference Manual". */ + +static bool +ix86_macro_fusion_pair_p (rtx condgen, rtx condjmp) +{ + rtx src; + if (!strcmp (ix86_tune_string, "corei7")) + { + /* For Nehalem. */ + rtx single_set = single_set (condgen); + /* Nehalem doesn't support macro-fusion for add/sub+jmp. */ + if (single_set == NULL_RTX) + return false; + + src = SET_SRC (single_set); + if (GET_CODE (src) != COMPARE) + return false; + + /* Nehalem doesn't support macro-fusion for cmp/test MEM-IMM + insn pattern. */ + if ((MEM_P (XEXP (src, 0)) + && CONST_INT_P (XEXP (src, 1))) + || (MEM_P (XEXP (src, 1)) + && CONST_INT_P (XEXP (src, 0)))) + return false; + + /* Nehalem doesn't support macro-fusion for add/sub/dec/inc + jmp. */ + if (get_attr_type (condgen) != TYPE_TEST + && get_attr_type (condgen) != TYPE_ICMP) + return false; + return true; + } + else if (!strcmp (ix86_tune_string, "corei7-avx")) + { + /* For Sandybridge. */ + enum rtx_code ccode; + rtx compare_set = NULL_RTX, test_if, cond; + rtx single_set = single_set (condgen); + if (single_set != NULL_RTX) + compare_set = single_set; + else + { + int i; + rtx pat = PATTERN (condgen); + for (i = 0; i < XVECLEN (pat, 0); i++) + if (GET_CODE (XVECEXP (pat, 0, i)) == SET + && GET_CODE (SET_SRC (XVECEXP (pat, 0, i))) == COMPARE) + compare_set = XVECEXP (pat, 0, i); + } + + if (compare_set == NULL_RTX) + return false; + src = SET_SRC (compare_set); + if (GET_CODE (src) != COMPARE) + return false; + + /* Sandybridge doesn't support macro-fusion for cmp/test MEM-IMM + insn pattern. */ + if ((MEM_P (XEXP (src, 0)) + && CONST_INT_P (XEXP (src, 1))) + || (MEM_P (XEXP (src, 1)) + && CONST_INT_P (XEXP (src, 0)))) + return false; + + /* Sandybridge doesn't support macro-fusion for inc/dec + + unsigned comparison jmp. */ + test_if = SET_SRC (pc_set (condjmp)); + cond = XEXP (test_if, 0); + ccode = GET_CODE (cond); + if (get_attr_type (condgen) == TYPE_INCDEC + && (ccode == GEU + || ccode == GTU + || ccode == LEU + || ccode == LTU)) + return false; + return true; + } + return false; +} + /* Try to reorder ready list to take advantage of Atom pipelined IMUL execution. It is applied if (1) IMUL instruction is on the top of list; @@ -42982,6 +43075,10 @@ ix86_memmodel_check (unsigned HOST_WIDE_ #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \ ia32_multipass_dfa_lookahead +#undef TARGET_SCHED_MACRO_FUSION_P +#define TARGET_SCHED_MACRO_FUSION_P ix86_macro_fusion_p +#undef TARGET_SCHED_MACRO_FUSION_PAIR_P +#define TARGET_SCHED_MACRO_FUSION_PAIR_P ix86_macro_fusion_pair_p #undef TARGET_FUNCTION_OK_FOR_SIBCALL #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall Index: doc/tm.texi.in =================================================================== --- doc/tm.texi.in (revision 201963) +++ doc/tm.texi.in (working copy) @@ -4940,6 +4940,10 @@ them: try the first ones in this list fi @hook TARGET_SCHED_REORDER2 +@hook TARGET_SCHED_MACRO_FUSION_P + +@hook TARGET_SCHED_MACRO_FUSION_PAIR_P + @hook TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK @hook TARGET_SCHED_INIT Index: doc/tm.texi =================================================================== --- doc/tm.texi (revision 201963) +++ doc/tm.texi (working copy) @@ -6553,6 +6553,17 @@ scheduling one insn causes other insns t cycle. These other insns can then be taken into account properly. @end deftypefn +@deftypefn {Target Hook} bool TARGET_SCHED_MACRO_FUSION_P (void) +This hook is used to check whether target platform supports macro fusion. +@end deftypefn + +@deftypefn {Target Hook} bool TARGET_SCHED_MACRO_FUSION_PAIR_P (rtx @var{condgen}, rtx @var{condjmp}) +This hook is used to check whether two insns could be macro fused for +target microarchitecture. If this hook returns true for the given insn pair +(@var{condgen} and @var{condjmp}), scheduler will put them into a sched +group, and they will not be scheduled apart. +@end deftypefn + @deftypefn {Target Hook} void TARGET_SCHED_DEPENDENCIES_EVALUATION_HOOK (rtx @var{head}, rtx @var{tail}) This hook is called after evaluation forward dependencies of insns in chain given by two parameter values (@var{head} and @var{tail} Index: target.def =================================================================== --- target.def (revision 201963) +++ target.def (working copy) @@ -1041,6 +1041,19 @@ scheduling one insn causes other insns t cycle. These other insns can then be taken into account properly.", int, (FILE *file, int verbose, rtx *ready, int *n_readyp, int clock), NULL) +DEFHOOK +(macro_fusion_p, + "This hook is used to check whether target platform supports macro fusion.", + bool, (void), NULL) + +DEFHOOK +(macro_fusion_pair_p, + "This hook is used to check whether two insns could be macro fused for\n\ +target microarchitecture. If this hook returns true for the given insn pair\n\ +(@var{condgen} and @var{condjmp}), scheduler will put them into a sched\n\ +group, and they will not be scheduled apart.", + bool, (rtx condgen, rtx condjmp), NULL) + /* The following member value is a pointer to a function called after evaluation forward dependencies of insns in chain given by two parameter values (head and tail correspondingly). */ Index: haifa-sched.c =================================================================== --- haifa-sched.c (revision 201963) +++ haifa-sched.c (working copy) @@ -6519,6 +6519,44 @@ setup_sched_dump (void) ? stderr : dump_file); } +static void +try_group_insn (rtx insn) +{ + unsigned int condreg1, condreg2; + rtx cc_reg_1; + rtx prev; + + targetm.fixed_condition_code_regs (&condreg1, &condreg2); + cc_reg_1 = gen_rtx_REG (CCmode, condreg1); + prev = prev_nonnote_nondebug_insn (insn); + if (!any_condjump_p (insn) + || !reg_referenced_p (cc_reg_1, PATTERN (insn)) + || !prev + || !modified_in_p (cc_reg_1, prev)) + return; + + /* Different microarchitectures support macro fusions for different + combinations of insn pairs. */ + if (!targetm.sched.macro_fusion_pair_p + || !targetm.sched.macro_fusion_pair_p (prev, insn)) + return; + + SCHED_GROUP_P (insn) = 1; +} + +/* If the last cond jump and the cond register defining insn are consecutive + before scheduling, we want them to be in a schedule group. This is good + for performance on microarchitectures supporting macro-fusion. */ + +static void +group_insns_for_macro_fusion () +{ + basic_block bb; + + FOR_EACH_BB (bb) + try_group_insn (BB_END (bb)); +} + /* Initialize some global state for the scheduler. This function works with the common data shared between all the schedulers. It is called from the scheduler specific initialization routine. */ @@ -6645,6 +6683,11 @@ sched_init (void) } curr_state = xmalloc (dfa_state_size); + + /* Group compare and branch insns for macro-fusion. */ + if (targetm.sched.macro_fusion_p + && targetm.sched.macro_fusion_p ()) + group_insns_for_macro_fusion (); } static void haifa_init_only_bb (basic_block, basic_block);