From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 59F71385736A for ; Tue, 31 May 2022 17:12:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 59F71385736A Received: by mail-pl1-x630.google.com with SMTP id o17so3032179pla.6 for ; Tue, 31 May 2022 10:12:33 -0700 (PDT) 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:references :mime-version:content-disposition:in-reply-to; bh=/QnsIAEU26bEK46ATIuUvYuB/hUmE1seZfGSVcR84aM=; b=Cgxqrx0zQ/eThiaS79+tiXbWbTp6LiyPs7cYdQZto7hCPFWx3bydCw4QzHFuVZxWcd cq0We4+fpcnmh1RYem822n9AHJBmVzGsO1pxyqDCu5y/T5mIepB1ImnP08FPV/vALU+H 7asNGs3/5AAYZVMOxQTpaV1ncJ2jpCoCktUKBC88FBfVdbyXkscH2bShp5knKbkNfrH0 rqsuFLDApNtAqg4Bo0K8LW8KLPRlP0yg4WM4Ty8u0mZ7u+P087YzCWNFxXBHsm5k36h9 ihGQh2SO6451BmTTqT+2KXp9NiHThKpm2GXQucej1Qyl/9yWHnAKNbQjK2iVpQ5U2B7R uPDg== X-Gm-Message-State: AOAM530nLW9Qu8wBLz0mlxgoqEQGd8it92DwDJJX8TBSthAjaLiqsT0I mxEty9/aQusW7P9rB4qgMNw= X-Google-Smtp-Source: ABdhPJzSlW/8WX8bwAqa3j83Nn4gTnbu0IzbVAb5p7B14PrG4avL+pyxma2alc/MnJn8gl9HI3BsZQ== X-Received: by 2002:a17:90a:d3d2:b0:1dd:30bb:6a45 with SMTP id d18-20020a17090ad3d200b001dd30bb6a45mr29768184pjw.206.1654017152074; Tue, 31 May 2022 10:12:32 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.88.122]) by smtp.gmail.com with ESMTPSA id j16-20020a17090a7e9000b001e32824c452sm2113378pjl.40.2022.05.31.10.12.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:12:31 -0700 (PDT) Received: by gnu-tgl-3.localdomain (Postfix, from userid 1000) id 6A5A3C049C; Tue, 31 May 2022 10:12:28 -0700 (PDT) Date: Tue, 31 May 2022 10:12:28 -0700 From: "H.J. Lu" To: Gcc-patches , Richard Biener , richard.sandiford@arm.com Subject: Re: [PATCH v4] DSE: Use the constant store source if possible Message-ID: References: <20220521030120.1977551-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3027.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_ASCII_DIVIDERS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 May 2022 17:12:36 -0000 On Mon, May 30, 2022 at 09:35:43AM +0100, Richard Sandiford wrote: > "H.J. Lu" writes: > > --- > > RTL DSE tracks redundant constant stores within a basic block. When RTL > > loop invariant motion hoists a constant initialization out of the loop > > into a separate basic block, the constant store value becomes unknown > > within the original basic block. When recording store for RTL DSE, check > > if the source register is set only once to a constant by a non-partial > > unconditional load. If yes, record the constant as the constant store > > source. It eliminates unrolled zero stores after memset 0 in a loop > > where a vector register is used as the zero store source. > > > > Extract find_single_def_src from loop-iv.cc and move it to df-core.cc: > > > > 1. Rename to df_find_single_def_src. > > 2. Change the argument to rtx and use rtx_equal_p. > > 3. Return null for partial or conditional defs. > > > > gcc/ > > > > PR rtl-optimization/105638 > > * df-core.cc (df_find_single_def_sr): Moved and renamed from > > find_single_def_src in loop-iv.cc. Change the argument to rtx > > and use rtx_equal_p. Return null for partial or conditional > > defs. > > * df.h (df_find_single_def_src): New prototype. > > * dse.cc (record_store): Use the constant source if the source > > register is set only once. > > * loop-iv.cc (find_single_def_src): Moved to df-core.cc. > > (replace_single_def_regs): Replace find_single_def_src with > > df_find_single_def_src. > > > > gcc/testsuite/ > > > > PR rtl-optimization/105638 > > * g++.target/i386/pr105638.C: New test. > > --- > > gcc/df-core.cc | 44 +++++++++++++++++++++++ > > gcc/df.h | 1 + > > gcc/dse.cc | 14 ++++++++ > > gcc/loop-iv.cc | 45 +----------------------- > > gcc/testsuite/g++.target/i386/pr105638.C | 44 +++++++++++++++++++++++ > > 5 files changed, 104 insertions(+), 44 deletions(-) > > create mode 100644 gcc/testsuite/g++.target/i386/pr105638.C > > > > diff --git a/gcc/df-core.cc b/gcc/df-core.cc > > index a901b84878f..f9b4de8eb7a 100644 > > --- a/gcc/df-core.cc > > +++ b/gcc/df-core.cc > > @@ -2009,6 +2009,50 @@ df_reg_used (rtx_insn *insn, rtx reg) > > return df_find_use (insn, reg) != NULL; > > } > > > > +/* If REG has a single definition, return its known value, otherwise return > > + null. */ > > + > > +rtx > > +df_find_single_def_src (rtx reg) > > +{ > > + rtx src = NULL_RTX; > > + > > + /* Don't look through unbounded number of single definition REG copies, > > + there might be loops for sources with uninitialized variables. */ > > + for (int cnt = 0; cnt < 128; cnt++) > > + { > > + df_ref adef = DF_REG_DEF_CHAIN (REGNO (reg)); > > + if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL > > + || DF_REF_IS_ARTIFICIAL (adef) > > + || (DF_REF_FLAGS (adef) > > + & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))) > > + return NULL_RTX; > > + > > + rtx set = single_set (DF_REF_INSN (adef)); > > + if (set == NULL || !rtx_equal_p (SET_DEST (set), reg)) > > + return NULL_RTX; > > + > > + rtx note = find_reg_equal_equiv_note (DF_REF_INSN (adef)); > > + if (note && function_invariant_p (XEXP (note, 0))) > > + { > > + src = XEXP (note, 0); > > + break; > > + } > > Seems simpler to return this directly, rather than break and then > check function_invariant_p again. Fixed. > > > + src = SET_SRC (set); > > + > > + if (REG_P (src)) > > + { > > + reg = src; > > + continue; > > + } > > + break; > > + } > > + if (!function_invariant_p (src)) > > + return NULL_RTX; > > + > > + return src; > > +} > > + > > > > /*---------------------------------------------------------------------------- > > Debugging and printing functions. > > diff --git a/gcc/df.h b/gcc/df.h > > index bd329205d08..71e249ad20a 100644 > > --- a/gcc/df.h > > +++ b/gcc/df.h > > @@ -991,6 +991,7 @@ extern df_ref df_find_def (rtx_insn *, rtx); > > extern bool df_reg_defined (rtx_insn *, rtx); > > extern df_ref df_find_use (rtx_insn *, rtx); > > extern bool df_reg_used (rtx_insn *, rtx); > > +extern rtx df_find_single_def_src (rtx); > > extern void df_worklist_dataflow (struct dataflow *,bitmap, int *, int); > > extern void df_print_regset (FILE *file, const_bitmap r); > > extern void df_print_word_regset (FILE *file, const_bitmap r); > > diff --git a/gcc/dse.cc b/gcc/dse.cc > > index 30c11cee034..c915266f025 100644 > > --- a/gcc/dse.cc > > +++ b/gcc/dse.cc > > @@ -1508,6 +1508,20 @@ record_store (rtx body, bb_info_t bb_info) > > > > if (tem && CONSTANT_P (tem)) > > const_rhs = tem; > > + else > > + { > > + /* If RHS is set only once to a constant, set CONST_RHS > > + to the constant. */ > > + rtx def_src = df_find_single_def_src (rhs); > > + if (def_src != nullptr && CONSTANT_P (def_src)) > > + { > > + df_ref def = DF_REG_DEF_CHAIN (REGNO (rhs)); > > + rtx_insn *def_insn = DF_REF_INSN (def); > > + rtx def_body = single_set (def_insn); > > + if (rhs == SET_DEST (def_body)) > > + const_rhs = def_src; > > + } > > + } > > This shouldn't be necessary now: we can rely on def_src being correct. > > I.e. I think this can just be: > > rtx def_src = df_find_single_def_src (rhs); > if (def_src != nullptr && CONSTANT_P (def_src)) > const_rhs = def_src; Fixed. > > Thanks, > Richard > Here is the v4 patch. OK for master? Thanks. H.J. --- RTL DSE tracks redundant constant stores within a basic block. When RTL loop invariant motion hoists a constant initialization out of the loop into a separate basic block, the constant store value becomes unknown within the original basic block. When recording store for RTL DSE, check if the source register is set only once to a constant by a non-partial unconditional load. If yes, record the constant as the constant store source. It eliminates unrolled zero stores after memset 0 in a loop where a vector register is used as the zero store source. gcc/ PR rtl-optimization/105638 * df-core.cc (df_find_single_def_src): Moved and renamed from find_single_def_src in loop-iv.cc. Change the argument to rtx and use rtx_equal_p. Return null for partial or conditional defs. * df.h (df_find_single_def_src): New prototype. * dse.cc (record_store): Use the constant source if the source register is set only once. * loop-iv.cc (find_single_def_src): Moved to df-core.cc. (replace_single_def_regs): Replace find_single_def_src with df_find_single_def_src. gcc/testsuite/ PR rtl-optimization/105638 * g++.target/i386/pr105638.C: New test. --- gcc/df-core.cc | 44 +++++++++++++++++++++++ gcc/df.h | 1 + gcc/dse.cc | 8 +++++ gcc/loop-iv.cc | 45 +----------------------- gcc/testsuite/g++.target/i386/pr105638.C | 44 +++++++++++++++++++++++ 5 files changed, 98 insertions(+), 44 deletions(-) create mode 100644 gcc/testsuite/g++.target/i386/pr105638.C diff --git a/gcc/df-core.cc b/gcc/df-core.cc index a901b84878f..e56d9bcf760 100644 --- a/gcc/df-core.cc +++ b/gcc/df-core.cc @@ -2009,6 +2009,50 @@ df_reg_used (rtx_insn *insn, rtx reg) return df_find_use (insn, reg) != NULL; } +/* If REG has a single definition, return its known value, otherwise return + null. */ + +rtx +df_find_single_def_src (rtx reg) +{ + rtx src = NULL_RTX; + + /* Don't look through unbounded number of single definition REG copies, + there might be loops for sources with uninitialized variables. */ + for (int cnt = 0; cnt < 128; cnt++) + { + df_ref adef = DF_REG_DEF_CHAIN (REGNO (reg)); + if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL + || DF_REF_IS_ARTIFICIAL (adef) + || (DF_REF_FLAGS (adef) + & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))) + return NULL_RTX; + + rtx set = single_set (DF_REF_INSN (adef)); + if (set == NULL || !rtx_equal_p (SET_DEST (set), reg)) + return NULL_RTX; + + rtx note = find_reg_equal_equiv_note (DF_REF_INSN (adef)); + if (note && function_invariant_p (XEXP (note, 0))) + { + src = XEXP (note, 0); + return src; + } + src = SET_SRC (set); + + if (REG_P (src)) + { + reg = src; + continue; + } + break; + } + if (!function_invariant_p (src)) + return NULL_RTX; + + return src; +} + /*---------------------------------------------------------------------------- Debugging and printing functions. diff --git a/gcc/df.h b/gcc/df.h index bd329205d08..71e249ad20a 100644 --- a/gcc/df.h +++ b/gcc/df.h @@ -991,6 +991,7 @@ extern df_ref df_find_def (rtx_insn *, rtx); extern bool df_reg_defined (rtx_insn *, rtx); extern df_ref df_find_use (rtx_insn *, rtx); extern bool df_reg_used (rtx_insn *, rtx); +extern rtx df_find_single_def_src (rtx); extern void df_worklist_dataflow (struct dataflow *,bitmap, int *, int); extern void df_print_regset (FILE *file, const_bitmap r); extern void df_print_word_regset (FILE *file, const_bitmap r); diff --git a/gcc/dse.cc b/gcc/dse.cc index 30c11cee034..994c60dc189 100644 --- a/gcc/dse.cc +++ b/gcc/dse.cc @@ -1508,6 +1508,14 @@ record_store (rtx body, bb_info_t bb_info) if (tem && CONSTANT_P (tem)) const_rhs = tem; + else + { + /* If RHS is set only once to a constant, set CONST_RHS + to the constant. */ + rtx def_src = df_find_single_def_src (rhs); + if (def_src != nullptr && CONSTANT_P (def_src)) + const_rhs = def_src; + } } } diff --git a/gcc/loop-iv.cc b/gcc/loop-iv.cc index 0eafe7d2362..d639336445a 100644 --- a/gcc/loop-iv.cc +++ b/gcc/loop-iv.cc @@ -1378,49 +1378,6 @@ simple_rhs_p (rtx rhs) } } -/* If REGNO has a single definition, return its known value, otherwise return - null. */ - -static rtx -find_single_def_src (unsigned int regno) -{ - rtx src = NULL_RTX; - - /* Don't look through unbounded number of single definition REG copies, - there might be loops for sources with uninitialized variables. */ - for (int cnt = 0; cnt < 128; cnt++) - { - df_ref adef = DF_REG_DEF_CHAIN (regno); - if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL - || DF_REF_IS_ARTIFICIAL (adef)) - return NULL_RTX; - - rtx set = single_set (DF_REF_INSN (adef)); - if (set == NULL || !REG_P (SET_DEST (set)) - || REGNO (SET_DEST (set)) != regno) - return NULL_RTX; - - rtx note = find_reg_equal_equiv_note (DF_REF_INSN (adef)); - if (note && function_invariant_p (XEXP (note, 0))) - { - src = XEXP (note, 0); - break; - } - src = SET_SRC (set); - - if (REG_P (src)) - { - regno = REGNO (src); - continue; - } - break; - } - if (!function_invariant_p (src)) - return NULL_RTX; - - return src; -} - /* If any registers in *EXPR that have a single definition, try to replace them with the known-equivalent values. */ @@ -1433,7 +1390,7 @@ replace_single_def_regs (rtx *expr) { rtx x = *iter; if (REG_P (x)) - if (rtx new_x = find_single_def_src (REGNO (x))) + if (rtx new_x = df_find_single_def_src (x)) { *expr = simplify_replace_rtx (*expr, x, new_x); goto repeat; diff --git a/gcc/testsuite/g++.target/i386/pr105638.C b/gcc/testsuite/g++.target/i386/pr105638.C new file mode 100644 index 00000000000..ff40a459de1 --- /dev/null +++ b/gcc/testsuite/g++.target/i386/pr105638.C @@ -0,0 +1,44 @@ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-std=gnu++20 -O2 -march=skylake" } */ +/* { dg-final { scan-assembler-not "vpxor" } } */ + +#include +#include +#include + +class FastBoard { +public: + typedef std::pair movescore_t; + typedef std::tr1::array scoredlist_t; + +protected: + std::vector m_critical; + + int m_boardsize; +}; + +class FastState { +public: + FastBoard board; + + int movenum; +protected: + FastBoard::scoredlist_t scoredmoves; +}; + +class KoState : public FastState { +private: + std::vector ko_hash_history; + std::vector hash_history; +}; + +class GameState : public KoState { +public: + void foo (); +private: + std::vector game_history; +}; + +void GameState::foo() { + game_history.resize(movenum); +} -- 2.36.1