Message ID | alpine.DEB.2.20.2203031440390.11552@tpp.orcam.me.uk |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a98:d5ce:0:b0:178:cc93:bf7d with SMTP id g14csp1965125eik; Mon, 18 Jul 2022 09:49:03 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t+giO/kKraH59n+/+iGl9J7Tff5CRUGggwxxkXB5YdPXZ6OCbEIUIAdcq3V/jn7Gu9Qrch X-Received: by 2002:a17:906:8479:b0:72b:83cf:4cf3 with SMTP id hx25-20020a170906847900b0072b83cf4cf3mr26209532ejc.90.1658162943721; Mon, 18 Jul 2022 09:49:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658162943; cv=none; d=google.com; s=arc-20160816; b=G4Is04AACqN8NN/k9qprdc6bVtNTr7FRTNP4sIupxr8LbtDWN8KHJ3WUb9v5s9QENc BXYMATrUEeZ/91Uj0NeND7QAaD4z3APUgYj9o2o09fDaOqXoD9pAVqx5oa7Pv44wy1Bd szVKNpaBF86eKs99r1PIsYmg6S7/omQM8DzdROJ4weKj7ILkPl0s0jYx4l9zDoqxTlWd aCgAcaHAoBWp54sK4jJg9ezNfaJbrdDmFZbegknh3yo1qmnG8oUcmjha27NgscqNwOmP 10Xn1EHR9oUw7zFxZJg/3CPIJ2PRuNO2oj12d0/+M7i8LDAZnxEVi8W0ojJh3p5P4tHF GAFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :message-id:subject:to:from:date:dkim-signature:dmarc-filter :delivered-to; bh=pmziUXKv+xK10P8VQGUsLwZqshoHfGkumGrWJBTbrHg=; b=fnlmfwwNGHrvI2YtXOCtbNhavNkoBJLExYGoEu9Yg0D9ckh6f6Rd0zaBplLrCkSPqT sQpiIXY+VQoNQTEE0AlNbWkZAqWmSInRhQoAY4aXdyNOWsVkeLvlaqVBNr2fjk7oDyzr UMHqky8icGvhfGGud3nCScn9u8szAKlKj/gphaVnlaLk+t3i602wTqzEqmpAcG0Ambo/ 6G2K21tol7+E71b22PPINksNwQcI7jJ9SMcYQqpnEY1UzkWMTH59rWTqYnlh8a9mfbf2 Bjjwddi4204DrQAAmu6NzHf2QPdd1lzNtNuQ2xKv3qZTw1eU1ZxM4jnOomjk0TuaWWwc DsfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@embecosm.com header.s=google header.b="YB/IsaQN"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gn13-20020a1709070d0d00b0072b3cde0ce8si17162374ejc.471.2022.07.18.09.49.03 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 09:49:03 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@embecosm.com header.s=google header.b="YB/IsaQN"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D90503852771 for <ouuuleilei@gmail.com>; Mon, 18 Jul 2022 16:48:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 1CD393858D28 for <gcc-patches@gcc.gnu.org>; Mon, 18 Jul 2022 16:48:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1CD393858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-lf1-x12d.google.com with SMTP id u13so20369071lfn.5 for <gcc-patches@gcc.gnu.org>; Mon, 18 Jul 2022 09:48:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=pmziUXKv+xK10P8VQGUsLwZqshoHfGkumGrWJBTbrHg=; b=YB/IsaQNq5QIv7Kfh7GAi7p7xWGqDhSh0WFfa1EMnr8WHRFtyriVWh+LoBRb9zkEyk fvYo09Dih0SNIGbXAq1d8IxJl6XHEx8gv2PVFZ/o8TSp2MlJCU+Cm2kIqL/y9ZSnJNJU geTAyoZ+b7dLiiwIU1mXNZwPhVJSRy2ZhPdokcrQw3BklHg/XHU80p9ok2hiG9yuq9K1 bCfNU1vHlmPvvb/0Yn3R+Di33yuD07Niql1jW8KW+ZaXcQ3bnrahs4H77hnrcoz2Hu0q BBf020dIbWRrznE7edVgU56mPVa/iFfLgrSudnEd0nqBRwI9s7pS9V1D9AjCZ3dBzetc rzRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=pmziUXKv+xK10P8VQGUsLwZqshoHfGkumGrWJBTbrHg=; b=16nSmzNBYScPMMqph5ZeWDDjihJiNb1LkAmz/rjmoCoDBhJf6RvR+C19o6Ko8mgMCy 2b++kK2cxz2++6tmphnbzA8WosYU52K8T4bU39OpCzLrwyAly9athZRzQGbrRwLIFDaL xF6pj8FNoDVFsqeknJVo+8LakOdt/RmBbx5iSaEUkXzUzn7pSw1WNl6SvF/ICGYqBUY/ GeTvmSzCwGh24uGP0LWOOLxCBMp4IgjVIv1xvxtSX6yPWOjAeva+/jtuVxXuXZkgrCJa Fxtay1F0wORp87P6sUFivRv06WWPzBlUlD2TX6aFn8N+xB6KcsQaOuAcRRbdBQ6fVj63 lV2Q== X-Gm-Message-State: AJIora/F5mnBxQksyXf65LcsUq6l9koCPSOnIndkG7bj/+/bBy+JT2GJ 1GLE++PnePbjlGsSucVhTdofwTwXOYJvGw== X-Received: by 2002:a05:6512:74:b0:488:a062:281d with SMTP id i20-20020a056512007400b00488a062281dmr14859393lfo.43.1658162912521; Mon, 18 Jul 2022 09:48:32 -0700 (PDT) Received: from [192.168.219.3] ([78.8.192.131]) by smtp.gmail.com with ESMTPSA id q27-20020a05651232bb00b00489de5012d4sm2674568lfe.301.2022.07.18.09.48.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Jul 2022 09:48:31 -0700 (PDT) Date: Mon, 18 Jul 2022 17:48:24 +0100 (BST) From: "Maciej W. Rozycki" <macro@embecosm.com> To: gcc-patches@gcc.gnu.org Subject: [PATCH] RISC-V: Add RTX costs for `if_then_else' expressions Message-ID: <alpine.DEB.2.20.2203031440390.11552@tpp.orcam.me.uk> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Cc: Kito Cheng <kito.cheng@gmail.com>, Andrew Waterman <andrew@sifive.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1738709866632357987?= X-GMAIL-MSGID: =?utf-8?q?1738709866632357987?= |
Series |
RISC-V: Add RTX costs for `if_then_else' expressions
|
|
Commit Message
Maciej W. Rozycki
July 18, 2022, 4:48 p.m. UTC
Fix a performance regression from commit 391500af1932 ("Do not ignore costs of jump insns in combine."), a part of the m68k series for MODE_CC conversion (<https://gcc.gnu.org/ml/gcc-patches/2019-11/msg01028.html>), observed in soft-fp code in libgcc used by some of the embench-iot benchmarks. The immediate origin of the regression is the middle end, which in the absence of cost information from the backend estimates the cost of an RTL expression by assuming a single machine instruction for each of the expression's subexpression. So for `if_then_else', which takes 3 operands, the estimated cost is 3 instructions (i.e. 12 units) even though a branch instruction evaluates it in a single machine cycle (ignoring the cost of actually taking the branch of course, which is handled elsewhere). Consequently an insn sequence like: (insn 595 594 596 43 (set (reg:DI 305) (lshiftrt:DI (reg/v:DI 160 [ R_f ]) (const_int 55 [0x37]))) ".../libgcc/soft-fp/adddf3.c":46:3 216 {lshrdi3} (nil)) (insn 596 595 597 43 (set (reg:DI 304) (and:DI (reg:DI 305) (const_int 1 [0x1]))) ".../libgcc/soft-fp/adddf3.c":46:3 109 {anddi3} (expr_list:REG_DEAD (reg:DI 305) (nil))) (jump_insn 597 596 598 43 (set (pc) (if_then_else (eq (reg:DI 304) (const_int 0 [0])) (label_ref:DI 1644) (pc))) ".../libgcc/soft-fp/adddf3.c":46:3 237 {*branchdi} (expr_list:REG_DEAD (reg:DI 304) (int_list:REG_BR_PROB 536870916 (nil))) -> 1644) does not (anymore, as from the commit referred) get combined into: (note 595 594 596 43 NOTE_INSN_DELETED) (note 596 595 597 43 NOTE_INSN_DELETED) (jump_insn 597 596 598 43 (parallel [ (set (pc) (if_then_else (eq (zero_extract:DI (reg/v:DI 160 [ R_f ]) (const_int 1 [0x1]) (const_int 55 [0x37])) (const_int 0 [0])) (label_ref:DI 1644) (pc))) (clobber (scratch:DI)) ]) ".../libgcc/soft-fp/adddf3.c":46:3 243 {*branch_on_bitdi} (int_list:REG_BR_PROB 536870916 (nil)) -> 1644) This is because the new cost is incorrectly calculated as 28 units while the cost of the original 3 instructions was 24: rejecting combination of insns 595, 596 and 597 original costs 4 + 4 + 16 = 24 replacement cost 28 Before the commit referred the cost of jump instruction was ignored and considered 0 (i.e. unknown) and a sequence of instructions of a known cost used to win: allowing combination of insns 595, 596 and 597 original costs 4 + 4 + 0 = 0 replacement cost 28 Add the missing costs for the 3 variants of `if_then_else' expressions we currently define in the backend. With the fix in place the cost of this particular `if_then_else' pattern is 2 instructions or 8 units (because of the shift operation) and therefore the ultimate cost of the original 3 RTL insns will work out at 16 units (4 + 4 + 8), however the replacement single RTL insn will cost 8 units only. gcc/ * config/riscv/riscv.cc (riscv_rtx_costs) <IF_THEN_ELSE>: New case. --- gcc/config/riscv/riscv.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) gcc-riscv-rtx-costs-if-then-else.diff
Comments
Hi Maciej: LGTM, thanks for modeling this in cost model! On Tue, Jul 19, 2022 at 12:48 AM Maciej W. Rozycki <macro@embecosm.com> wrote: > > Fix a performance regression from commit 391500af1932 ("Do not ignore > costs of jump insns in combine."), a part of the m68k series for MODE_CC > conversion (<https://gcc.gnu.org/ml/gcc-patches/2019-11/msg01028.html>), > observed in soft-fp code in libgcc used by some of the embench-iot > benchmarks. > > The immediate origin of the regression is the middle end, which in the > absence of cost information from the backend estimates the cost of an > RTL expression by assuming a single machine instruction for each of the > expression's subexpression. > > So for `if_then_else', which takes 3 operands, the estimated cost is 3 > instructions (i.e. 12 units) even though a branch instruction evaluates > it in a single machine cycle (ignoring the cost of actually taking the > branch of course, which is handled elsewhere). Consequently an insn > sequence like: > > (insn 595 594 596 43 (set (reg:DI 305) > (lshiftrt:DI (reg/v:DI 160 [ R_f ]) > (const_int 55 [0x37]))) ".../libgcc/soft-fp/adddf3.c":46:3 216 {lshrdi3} > (nil)) > (insn 596 595 597 43 (set (reg:DI 304) > (and:DI (reg:DI 305) > (const_int 1 [0x1]))) ".../libgcc/soft-fp/adddf3.c":46:3 109 {anddi3} > (expr_list:REG_DEAD (reg:DI 305) > (nil))) > (jump_insn 597 596 598 43 (set (pc) > (if_then_else (eq (reg:DI 304) > (const_int 0 [0])) > (label_ref:DI 1644) > (pc))) ".../libgcc/soft-fp/adddf3.c":46:3 237 {*branchdi} > (expr_list:REG_DEAD (reg:DI 304) > (int_list:REG_BR_PROB 536870916 (nil))) > -> 1644) > > does not (anymore, as from the commit referred) get combined into: > > (note 595 594 596 43 NOTE_INSN_DELETED) > (note 596 595 597 43 NOTE_INSN_DELETED) > (jump_insn 597 596 598 43 (parallel [ > (set (pc) > (if_then_else (eq (zero_extract:DI (reg/v:DI 160 [ R_f ]) > (const_int 1 [0x1]) > (const_int 55 [0x37])) > (const_int 0 [0])) > (label_ref:DI 1644) > (pc))) > (clobber (scratch:DI)) > ]) ".../libgcc/soft-fp/adddf3.c":46:3 243 {*branch_on_bitdi} > (int_list:REG_BR_PROB 536870916 (nil)) > -> 1644) > > This is because the new cost is incorrectly calculated as 28 units while > the cost of the original 3 instructions was 24: > > rejecting combination of insns 595, 596 and 597 > original costs 4 + 4 + 16 = 24 > replacement cost 28 > > Before the commit referred the cost of jump instruction was ignored and > considered 0 (i.e. unknown) and a sequence of instructions of a known > cost used to win: > > allowing combination of insns 595, 596 and 597 > original costs 4 + 4 + 0 = 0 > replacement cost 28 > > Add the missing costs for the 3 variants of `if_then_else' expressions > we currently define in the backend. > > With the fix in place the cost of this particular `if_then_else' pattern > is 2 instructions or 8 units (because of the shift operation) and > therefore the ultimate cost of the original 3 RTL insns will work out at > 16 units (4 + 4 + 8), however the replacement single RTL insn will cost > 8 units only. > > gcc/ > * config/riscv/riscv.cc (riscv_rtx_costs) <IF_THEN_ELSE>: New > case. > --- > gcc/config/riscv/riscv.cc | 27 +++++++++++++++++++++++++++ > 1 file changed, 27 insertions(+) > > gcc-riscv-rtx-costs-if-then-else.diff > Index: gcc/gcc/config/riscv/riscv.cc > =================================================================== > --- gcc.orig/gcc/config/riscv/riscv.cc > +++ gcc/gcc/config/riscv/riscv.cc > @@ -1853,6 +1853,33 @@ riscv_rtx_costs (rtx x, machine_mode mod > /* Otherwise use the default handling. */ > return false; > > + case IF_THEN_ELSE: > + if (TARGET_SFB_ALU > + && register_operand (XEXP (x, 1), mode) > + && sfb_alu_operand (XEXP (x, 2), mode) > + && comparison_operator (XEXP (x, 0), VOIDmode)) > + { > + /* For predicated conditional-move operations we assume the cost > + of a single instruction even though there are actually two. */ > + *total = COSTS_N_INSNS (1); > + return true; > + } > + else if (LABEL_REF_P (XEXP (x, 1)) && XEXP (x, 2) == pc_rtx) > + { > + if (equality_operator (XEXP (x, 0), mode) > + && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT) > + { > + *total = COSTS_N_INSNS (SINGLE_SHIFT_COST + 1); > + return true; > + } > + if (order_operator (XEXP (x, 0), mode)) > + { > + *total = COSTS_N_INSNS (1); > + return true; > + } > + } > + return false; > + > case NOT: > *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1); > return false;
On Thu, 21 Jul 2022, Kito Cheng wrote:
> LGTM, thanks for modeling this in cost model!
Patch applied now, thank you for your review.
Maciej
Index: gcc/gcc/config/riscv/riscv.cc =================================================================== --- gcc.orig/gcc/config/riscv/riscv.cc +++ gcc/gcc/config/riscv/riscv.cc @@ -1853,6 +1853,33 @@ riscv_rtx_costs (rtx x, machine_mode mod /* Otherwise use the default handling. */ return false; + case IF_THEN_ELSE: + if (TARGET_SFB_ALU + && register_operand (XEXP (x, 1), mode) + && sfb_alu_operand (XEXP (x, 2), mode) + && comparison_operator (XEXP (x, 0), VOIDmode)) + { + /* For predicated conditional-move operations we assume the cost + of a single instruction even though there are actually two. */ + *total = COSTS_N_INSNS (1); + return true; + } + else if (LABEL_REF_P (XEXP (x, 1)) && XEXP (x, 2) == pc_rtx) + { + if (equality_operator (XEXP (x, 0), mode) + && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT) + { + *total = COSTS_N_INSNS (SINGLE_SHIFT_COST + 1); + return true; + } + if (order_operator (XEXP (x, 0), mode)) + { + *total = COSTS_N_INSNS (1); + return true; + } + } + return false; + case NOT: *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1); return false;