From patchwork Mon Jun 17 07:17:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takayuki 'January June' Suwa X-Patchwork-Id: 1948516 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=yahoo.co.jp header.i=@yahoo.co.jp header.a=rsa-sha256 header.s=yj20110701 header.b=bQqd1Wvo; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W2h6g64Tkz20Ws for ; Mon, 17 Jun 2024 17:17:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D0C963857011 for ; Mon, 17 Jun 2024 07:17:43 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from omggw7017-vm0.mail.djm.yahoo.co.jp (omggw7017-vm0.mail.djm.yahoo.co.jp [183.79.55.10]) by sourceware.org (Postfix) with ESMTPS id F05423858CDA for ; Mon, 17 Jun 2024 07:17:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F05423858CDA Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=yahoo.co.jp Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=yahoo.co.jp ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F05423858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=183.79.55.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718608645; cv=none; b=KHqTXiuHQEm/lh5LP5xKyHkbBc2s6xGQQaRn9FYRreJzMsOllNinSJ72F98oo89VGqeoj/9hBc3glaQKuBMEziOzNnlk54m1IdbIPFCDf6z6Z67MuY6v2UnK3IIisiMyesAbEovLVP/mPWygumuxwsPwHkbnda1iOXLvWQENbDM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718608645; c=relaxed/simple; bh=qZyHKAhjkiliHGZRKcj6Y1HtL2rxtBp40vO2jBp+ocY=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=FKKt5O/YHa2BHMaRX0Fh2Gx7XVk5JIqaHiBnd91cgMlaVQgShFb+mwWi+1gTDZ3UmwN8tYb8I0BbAXI5EZfodPhE8hycKXhnEat+SPbFc4XpvJG3LOR9Ps+loWKeNj97w97JM/lUN/mGl5SkVssOgedlvXDwh01PCsbfnID19Vo= ARC-Authentication-Results: i=1; server2.sourceware.org X-YMail-OSG: WjQ33U8VM1mZzja4MrngwNkdcIs6EWm9ewL22T3HJs3A_ffEFlbAVNySQOU4g6x LgPHsTb08A3kF2eIwdXxvMbVg8SSrF3J440oHOkaFjSz3uJG1Zd1N19mOU06R2mTYndyTy2uZ5x8 mMUwCdgkBTciOOzLsdG91a9uw7Sw_jwGAVtarRAP79wbGhnTpNDeh9O44pmWdJup3LQdmBvESCqc Woz2VIKTYS81YzwadQC23tqgtuBjpyhTQfFukFfjRuQ2QFXGsuAO6y1TYCmow1LFOGqBN9thgljJ efcKVpmFkOcWcvnN5mO8rrLr1pe_enseFnXoZAeSE.qCowpMwctuPUfe2.O8F0Wma6oArxmNGXsL zIMVh8VhwaSq.mAaPEO6hNJ7lHl__Rp0OlgmaOWKplXV5YICP_MTmg0fTuRF5MMYXWxCyZPGSgSh cFjuv0v9tnu2EqR6P7d0h2ZgReLc5XnKw5h5r8IomNRm.saBYGrL2PMZSrje_aJ4qALoHdfOD0zi A9cKexYtPRFj8dce7lEkug2BuWwqaY_VCmR22IKSU3wTR6vqU20uu9tiBqPQ11kR.VKajFGsA_2v kG_ceeksjWjbLtt.WyCdk1JHh_Ra0VLMU1lg7mvsfo3eKq98bH1CbkhnGFxhVXepwM2uAu3Ch_a. 61sxvKgf99wHM5WnmMPAnZ_lXd7k0xzLaddgmOcQJpVOqQDnjFQUtNxYgB9wtM_zSyJOTaAddaZ8 tZQOaB0Y_bkCQXZJ20uNe1c0FgsyvhgUBczvWnhI819iswvqk4vGcJq8SLhsp8jU9v5Iu2SB9JV7 DjyTyBRoa9oDj3_L2xNDocb9iAS9RXWf24qVkuHPL293lI5qGbau3Ge_RLFvs48coTFxYdSeasCK fgGeGhJLNl7RM9cZCVUykF.NZSzITZCgdSKp0MPwnUkQUhtxpiiYERR5GLFJxQZqJbnUrajpNs9j T9EyatmhRzNPkbESxRkltC9Q2tmN_cywlmdC1zNfCbZViJQ_XvJ0JksP6MFHoSQa6h0TwBfhOKtZ 1_yWQYN8eYF7ZrmraYdebLvq.ytG2SfdoqjirmuCL3MsQsSNvr9Xhn3.CZUrlLicxBFphA4BMb82 9FMSNiZmzqNFogxFkgFE- Received: from sonicgw.mail.yahoo.co.jp by sonicconh5003.mail.kks.yahoo.co.jp with HTTP; Mon, 17 Jun 2024 07:17:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1718608635; s=yj20110701; d=yahoo.co.jp; h=References:Content-Transfer-Encoding:Content-Type:Subject:From:Cc:To:MIME-Version:Date:Message-ID; bh=dvCa1IuAygGr+neN1X7XBe4qAUI8sgfk4DGS+XSrdxc=; b=bQqd1WvorCmzvwIirrlpB2jfNRI+LPn8L2Qk2O6Ez0F9U8uhamc0SvQS6u7/EYks suCYkMQ4hsktbS8cjT2FXv1Yk3XA7CXtNChBUNy4H71HzjD/cFrdPoKbOGmEhBg9Bxj vCSRuLXEsSMl6Mx+2rit2aBk9Mwl4/zOqtUBDCt0= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=yj20110701; d=yahoo.co.jp; h=Message-ID:Date:MIME-Version:Cc:From:Content-Type:Content-Transfer-Encoding:References; b=F/foxgyzqviGVFlIkfFD1ZGTDaDdJ/qcB69p90Xo5j2TREjceCFLFBAZYTzb7bkM E8gYv2Vi8zY72M3SBOWKZtR8iUOpHUANqow4wBqJN0ctCOl30X0b/9hywICNvcS5nj8 j8RkUo92P+X4cm+S7rPUeaqu8fvPTPWnXqpFB9Xw=; Received: by smtphe5003.mail.kks.ynwp.yahoo.co.jp (YJ Hermes SMTP Server) with ESMTPA ID 54bdb71d4a49240123971d8967990e60; Mon, 17 Jun 2024 16:17:16 +0900 (JST) Message-ID: <6e7205ee-53b8-4d29-8704-c37d90f96146@yahoo.co.jp> Date: Mon, 17 Jun 2024 16:17:15 +0900 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: gcc-patches@gcc.gnu.org Cc: Max Filippov From: Takayuki 'January June' Suwa Subject: [PATCH] xtensa: constantsynth: Reforge to fix some non-fatal issues References: <6e7205ee-53b8-4d29-8704-c37d90f96146.ref@yahoo.co.jp> X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org The previous constant synthesis logic had some issues that were non-fatal but worth considering: - It didn't work with DFmode literals, because those were cast to SImode rather SFmode when splitting into two natural-width words by split_double(). - It didn't work with large literals when TARGET_AUTO_LITPOOLS was enabled, because those were relaxed MOVI immediates rather references to literal pool entries, - It didn't take into account that when literals with the same RTL representation are pooled multiple times within a function, those entries are shared (especially important when optimizing for size). This patch addresses the above issues by making appropriate tweaks to the constant synthesis logic. gcc/ChangeLog: * config/xtensa/xtensa-protos.h (xtensa_constantsynth): Change the second argument from HOST_WIDE_INT to rtx. * config/xtensa/xtensa.cc (#include): Add "context.h" and "pass_manager.h". (machine_function): Add a new hash_map field "litpool_usage". (xtensa_constantsynth): Make "src" (the second operand) accept RTX literal instead of its value, and treat both bare and pooled SI/SFmode literals equally by bit-exact canonicalization into CONST_INT RTX internally. And then, make avoid synthesis if such multiple identical canonicalized literals are found in same function when optimizing for size. Finally, for literals where synthesis is not possible or has been avoided, re-emit "move" RTXes with canonicalized ones to increase the chances of sharing literal pool entries. * config/xtensa/xtensa.md (split patterns for constant synthesis): Change to simply invoke xtensa_constantsynth() as mentioned above, and add new patterns for when TARGET_AUTO_LITPOOLS is enabled. --- gcc/config/xtensa/xtensa-protos.h | 2 +- gcc/config/xtensa/xtensa.cc | 75 ++++++++++++++++++++++++++++--- gcc/config/xtensa/xtensa.md | 56 ++++++++++++++--------- 3 files changed, 103 insertions(+), 30 deletions(-) diff --git a/gcc/config/xtensa/xtensa-protos.h b/gcc/config/xtensa/xtensa-protos.h index 77553b0453f..8f645e87de9 100644 --- a/gcc/config/xtensa/xtensa-protos.h +++ b/gcc/config/xtensa/xtensa-protos.h @@ -44,7 +44,7 @@ extern int xtensa_expand_scc (rtx *, machine_mode); extern int xtensa_expand_block_move (rtx *); extern int xtensa_expand_block_set (rtx *); extern void xtensa_split_operand_pair (rtx *, machine_mode); -extern int xtensa_constantsynth (rtx, HOST_WIDE_INT); +extern int xtensa_constantsynth (rtx, rtx); extern int xtensa_emit_move_sequence (rtx *, machine_mode); extern rtx xtensa_copy_incoming_a7 (rtx); extern void xtensa_expand_nonlocal_goto (rtx *); diff --git a/gcc/config/xtensa/xtensa.cc b/gcc/config/xtensa/xtensa.cc index 45dc1be3ff5..0f05c6635b3 100644 --- a/gcc/config/xtensa/xtensa.cc +++ b/gcc/config/xtensa/xtensa.cc @@ -58,6 +58,8 @@ along with GCC; see the file COPYING3. If not see #include "insn-attr.h" #include "tree-pass.h" #include "print-rtl.h" +#include "context.h" +#include "pass_manager.h" #include /* This file should be included last. */ @@ -107,6 +109,7 @@ struct GTY(()) machine_function bool inhibit_logues_a1_adjusts; rtx last_logues_a9_content; HARD_REG_SET eliminated_callee_saved; + hash_map *litpool_usage; }; static void xtensa_option_override (void); @@ -1104,7 +1107,7 @@ xtensa_split_operand_pair (rtx operands[4], machine_mode mode) } -/* Try to emit insns to load srcval (that cannot fit into signed 12-bit) +/* Try to emit insns to load src (either naked or pooled SI/SF constant) into dst with synthesizing a such constant value from a sequence of load-immediate / arithmetic ones, instead of a L32R instruction (plus a constant in litpool). */ @@ -1190,11 +1193,67 @@ xtensa_constantsynth_rtx_ADDSUBX (rtx reg, HOST_WIDE_INT imm) } int -xtensa_constantsynth (rtx dst, HOST_WIDE_INT srcval) +xtensa_constantsynth (rtx dst, rtx src) { + HOST_WIDE_INT srcval; + static opt_pass *pass_rtl_split2; + int *pv; + + /* Derefer if src is litpool entry, and get integer constant value. */ + src = avoid_constant_pool_reference (src); + if (CONST_INT_P (src)) + srcval = INTVAL (src); + else if (CONST_DOUBLE_P (src) && GET_MODE (src) == SFmode) + { + long l; + + REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (src), l); + srcval = (int32_t)l, src = GEN_INT (srcval); + } + else + return 0; + + /* Force dst as SImode. */ + gcc_assert (REG_P (dst)); + if (GET_MODE (dst) != SImode) + dst = gen_rtx_REG (SImode, REGNO (dst)); + + if (optimize_size) + { + /* During the first split pass after register allocation (rtl-split2), + record the occurrence of integer src value and do nothing. */ + if (!pass_rtl_split2) + pass_rtl_split2 = g->get_passes ()->get_pass_by_name ("rtl-split2"); + if (current_pass == pass_rtl_split2) + { + if (!cfun->machine->litpool_usage) + cfun->machine->litpool_usage = new hash_map (); + if ((pv = cfun->machine->litpool_usage->get (src))) + ++*pv; + else + cfun->machine->litpool_usage->put (src, 1); + return 0; + } + + /* If two or more identical integer constants appear in the function, + the code size can be reduced by re-emitting a "move" (load from an + either litpool entry or relaxed immediate) instruction in SImode + to increase the chances that the litpool entry will be shared. */ + if (cfun->machine->litpool_usage + && (pv = cfun->machine->litpool_usage->get (src)) + && *pv > 1) + { + emit_move_insn (dst, src); + return 1; + } + } + /* No need for synthesizing for what fits into MOVI instruction. */ if (xtensa_simm12b (srcval)) - return 0; + { + emit_move_insn (dst, src); + return 1; + } /* 2-insns substitution. */ if ((optimize_size || (optimize && xtensa_extra_l32r_costs >= 1)) @@ -1240,9 +1299,6 @@ xtensa_constantsynth (rtx dst, HOST_WIDE_INT srcval) emit_insn (gen_rotlsi3 (dst, dst, GEN_INT (shift))); return 1; } - } - for (shift = 1; shift < 12; ++shift) - { v = (int32_t)(((uint32_t)srcval << shift) | ((uint32_t)srcval >> (32 - shift))); if (xtensa_simm12b(v)) @@ -1255,7 +1311,12 @@ xtensa_constantsynth (rtx dst, HOST_WIDE_INT srcval) } } - return 0; + /* If cannot synthesize the value and also cannot fit into MOVI instruc- + tion, re-emit a "move" (load from an either litpool entry or relaxed + immediate) instruction in SImode in order to increase the chances that + the litpool entry will be shared. */ + emit_move_insn (dst, src); + return 1; } diff --git a/gcc/config/xtensa/xtensa.md b/gcc/config/xtensa/xtensa.md index ef826b63e44..8709ef6d7a7 100644 --- a/gcc/config/xtensa/xtensa.md +++ b/gcc/config/xtensa/xtensa.md @@ -1285,17 +1285,25 @@ (define_split [(set (match_operand:SHI 0 "register_operand") (match_operand:SHI 1 "constantpool_operand"))] - "! optimize_debug && reload_completed" + "!optimize_debug && reload_completed" [(const_int 0)] { - rtx x = avoid_constant_pool_reference (operands[1]), dst = operands[0]; - if (! CONST_INT_P (x)) - FAIL; - if (mode == HImode) - dst = gen_rtx_REG (SImode, REGNO (dst)); - if (! xtensa_constantsynth (dst, INTVAL (x))) - emit_move_insn (dst, x); - DONE; + if (xtensa_constantsynth (operands[0], operands[1])) + DONE; + FAIL; +}) + +(define_split + [(set (match_operand:SHI 0 "register_operand") + (match_operand:SHI 1 "const_int_operand"))] + "!optimize_debug && reload_completed + && !TARGET_CONST16 && TARGET_AUTO_LITPOOLS + && ! xtensa_simm12b (INTVAL (operands[1]))" + [(const_int 0)] +{ + if (xtensa_constantsynth (operands[0], operands[1])) + DONE; + FAIL; }) ;; 16-bit Integer moves @@ -1503,21 +1511,25 @@ (define_split [(set (match_operand:SF 0 "register_operand") - (match_operand:SF 1 "constantpool_operand"))] - "! optimize_debug && reload_completed" + (match_operand 1 "constantpool_operand"))] + "!optimize_debug && reload_completed" [(const_int 0)] { - rtx x = avoid_constant_pool_reference (operands[1]); - long l; - HOST_WIDE_INT value; - if (! CONST_DOUBLE_P (x) || GET_MODE (x) != SFmode) - FAIL; - REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l); - x = gen_rtx_REG (SImode, REGNO (operands[0])); - value = (int32_t)l; - if (! xtensa_constantsynth (x, value)) - emit_move_insn (x, GEN_INT (value)); - DONE; + if (xtensa_constantsynth (operands[0], operands[1])) + DONE; + FAIL; +}) + +(define_split + [(set (match_operand:SF 0 "register_operand") + (match_operand 1 "const_double_operand"))] + "!optimize_debug && reload_completed + && !TARGET_CONST16 && TARGET_AUTO_LITPOOLS" + [(const_int 0)] +{ + if (xtensa_constantsynth (operands[0], operands[1])) + DONE; + FAIL; }) ;; 64-bit floating point moves