From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id E0F523858D39 for ; Wed, 2 Mar 2022 07:50:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E0F523858D39 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 1C9381F37C; Wed, 2 Mar 2022 07:50:54 +0000 (UTC) Received: from murzim.suse.de (murzim.suse.de [10.160.4.192]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 15A3FA3B83; Wed, 2 Mar 2022 07:50:54 +0000 (UTC) Date: Wed, 2 Mar 2022 08:50:53 +0100 (CET) From: Richard Biener To: "H.J. Lu" cc: gcc-patches@gcc.gnu.org, Jakub Jelinek Subject: Re: [PATCH] Use C++ RAII to clear currently_expanding_gimple_stmt In-Reply-To: <20220301165523.869939-1-hjl.tools@gmail.com> Message-ID: References: <20220301165523.869939-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Wed, 02 Mar 2022 07:50:56 -0000 On Tue, 1 Mar 2022, H.J. Lu wrote: > Use C++ RAII with make_temp_override in the C++ FE to automatically clear > currently_expanding_gimple_stmt. Hmm, the temp-override isn't really a good fit since we only want to have the cleanup part and that should always "restore" to nullptr. Richard. > gcc/ > > PR middle-end/104721 > * cfgexpand.cc (expand_gimple_basic_block): Use make_temp_override > to clear currently_expanding_gimple_stmt automatically. > * tree.h (temp_override): Moved from cp/cp-tree.h. > (type_identity_t): Likewise. > make_temp_override(): Likewise. > > gcc/cp/ > > PR middle-end/104721 > * cp-tree.h (temp_override): Moved to ../tree.h. > (type_identity_t): Likewise. > make_temp_override(): Likewise. > --- > gcc/cfgexpand.cc | 15 +++++--------- > gcc/cp/cp-tree.h | 51 ------------------------------------------------ > gcc/tree.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 56 insertions(+), 61 deletions(-) > > diff --git a/gcc/cfgexpand.cc b/gcc/cfgexpand.cc > index 87536ec7ccd..3d656113df5 100644 > --- a/gcc/cfgexpand.cc > +++ b/gcc/cfgexpand.cc > @@ -5817,6 +5817,9 @@ expand_gimple_basic_block (basic_block bb, bool disable_tail_calls) > if (note) > NOTE_BASIC_BLOCK (note) = bb; > > + auto cleanup = make_temp_override (currently_expanding_gimple_stmt, > + nullptr); > + > for (; !gsi_end_p (gsi); gsi_next (&gsi)) > { > basic_block new_bb; > @@ -5927,10 +5930,7 @@ expand_gimple_basic_block (basic_block bb, bool disable_tail_calls) > { > new_bb = expand_gimple_cond (bb, as_a (stmt)); > if (new_bb) > - { > - currently_expanding_gimple_stmt = NULL; > - return new_bb; > - } > + return new_bb; > } > else if (is_gimple_debug (stmt)) > { > @@ -6052,10 +6052,7 @@ expand_gimple_basic_block (basic_block bb, bool disable_tail_calls) > if (can_fallthru) > bb = new_bb; > else > - { > - currently_expanding_gimple_stmt = NULL; > - return new_bb; > - } > + return new_bb; > } > } > else > @@ -6078,8 +6075,6 @@ expand_gimple_basic_block (basic_block bb, bool disable_tail_calls) > } > } > > - currently_expanding_gimple_stmt = NULL; > - > /* Expand implicit goto and convert goto_locus. */ > FOR_EACH_EDGE (e, ei, bb->succs) > { > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index 80994e94793..0a55588dc5d 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -1961,57 +1961,6 @@ public: > } > }; > > -/* RAII sentinel that saves the value of a variable, optionally > - overrides it right away, and restores its value when the sentinel > - id destructed. */ > - > -template > -class temp_override > -{ > - T& overridden_variable; > - T saved_value; > -public: > - temp_override(T& var) : overridden_variable (var), saved_value (var) {} > - temp_override(T& var, T overrider) > - : overridden_variable (var), saved_value (var) > - { > - overridden_variable = overrider; > - } > - ~temp_override() { overridden_variable = saved_value; } > -}; > - > -/* Wrapping a template parameter in type_identity_t hides it from template > - argument deduction. */ > -#if __cpp_lib_type_identity > -using std::type_identity_t; > -#else > -template > -struct type_identity { typedef T type; }; > -template > -using type_identity_t = typename type_identity::type; > -#endif > - > -/* Object generator function for temp_override, so you don't need to write the > - type of the object as a template argument. > - > - Use as auto x = make_temp_override (flag); */ > - > -template > -inline temp_override > -make_temp_override (T& var) > -{ > - return { var }; > -} > - > -/* Likewise, but use as auto x = make_temp_override (flag, value); */ > - > -template > -inline temp_override > -make_temp_override (T& var, type_identity_t overrider) > -{ > - return { var, overrider }; > -} > - > /* The cached class binding level, from the most recently exited > class, or NULL if none. */ > > diff --git a/gcc/tree.h b/gcc/tree.h > index 36ceed57064..32d610474d2 100644 > --- a/gcc/tree.h > +++ b/gcc/tree.h > @@ -6581,4 +6581,55 @@ extern tree get_attr_nonstring_decl (tree, tree * = NULL); > > extern int get_target_clone_attr_len (tree); > > +/* RAII sentinel that saves the value of a variable, optionally > + overrides it right away, and restores its value when the sentinel > + id destructed. */ > + > +template > +class temp_override > +{ > + T& overridden_variable; > + T saved_value; > +public: > + temp_override(T& var) : overridden_variable (var), saved_value (var) {} > + temp_override(T& var, T overrider) > + : overridden_variable (var), saved_value (var) > + { > + overridden_variable = overrider; > + } > + ~temp_override() { overridden_variable = saved_value; } > +}; > + > +/* Wrapping a template parameter in type_identity_t hides it from template > + argument deduction. */ > +#if __cpp_lib_type_identity > +using std::type_identity_t; > +#else > +template > +struct type_identity { typedef T type; }; > +template > +using type_identity_t = typename type_identity::type; > +#endif > + > +/* Object generator function for temp_override, so you don't need to write the > + type of the object as a template argument. > + > + Use as auto x = make_temp_override (flag); */ > + > +template > +inline temp_override > +make_temp_override (T& var) > +{ > + return { var }; > +} > + > +/* Likewise, but use as auto x = make_temp_override (flag, value); */ > + > +template > +inline temp_override > +make_temp_override (T& var, type_identity_t overrider) > +{ > + return { var, overrider }; > +} > + > #endif /* GCC_TREE_H */ > -- Richard Biener SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany; GF: Ivo Totev; HRB 36809 (AG Nuernberg)