From: "Kewen.Lin" <linkw@linux.ibm.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: Martin Sebor <msebor@gmail.com>,
Richard Biener <richard.guenther@gmail.com>,
Richard Sandiford <richard.sandiford@arm.com>,
Jakub Jelinek <jakub@redhat.com>,
Trevor Saunders <tbsaunde@tbsaunde.org>,
Segher Boessenkool <segher@kernel.crashing.org>,
GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [RFC/PATCH] Use range-based for loops for traversing loops
Date: Tue, 20 Jul 2021 22:42:36 +0800 [thread overview]
Message-ID: <0bb41635-7ae0-4ea3-16e9-118c80f17151@linux.ibm.com> (raw)
In-Reply-To: <CAH6eHdR4dz_WHjO7h_EKMM=s_spSiz-Sgn1Kq8=dD-qhs=PQ=A@mail.gmail.com>
on 2021/7/20 下午5:49, Jonathan Wakely wrote:
> On Tue, 20 Jul 2021 at 09:58, Kewen.Lin <linkw@linux.ibm.com> wrote:
>>
>> on 2021/7/19 下午11:59, Martin Sebor wrote:
>>> On 7/19/21 12:20 AM, Kewen.Lin wrote:
>>>> Hi,
>>>>
>>>> This patch follows Martin's suggestion here[1], to support
>>>> range-based for loops for traversing loops, analogously to
>>>> the patch for vec[2].
>>>>
>>>> Bootstrapped and regtested on powerpc64le-linux-gnu P9,
>>>> x86_64-redhat-linux and aarch64-linux-gnu, also
>>>> bootstrapped on ppc64le P9 with bootstrap-O3 config.
>>>>
>>>> Any comments are appreciated.
>>>
>>> Thanks for this nice cleanup! Just a few suggestions:
>>>
>>> I would recommend against introducing new macros unless they
>>> offer a significant advantage over alternatives (for the two
>>> macros the patch adds I don't think they do).
>>>
>>> If improving const-correctness is one of our a goals
>>> the loops_list iterator type would need to a corresponding
>>> const_iterator type, and const overloads of the begin()
>>> and end() member functions.
>>>
>>> Rather than introducing more instances of the loop_p typedef
>>> I'd suggest to use loop *. It has at least two advantages:
>>> it's clearer (it's obvious it refers to a pointer), and lends
>>> itself more readily to making code const-correct by declaring
>>> the control variable const: for (const class loop *loop: ...)
>>> while avoiding the mistake of using const loop_p loop to
>>> declare a pointer to a const loop.
>>>
>>
>> Thanks for the suggestions, Martin! Will update them in V2.
>>
>> With some experiments, I noticed that even provided const_iterator
>> like:
>>
>> iterator
>> begin ()
>> {
>> return iterator (*this, 0);
>> }
>>
>> + const_iterator
>> + begin () const
>> + {
>> + return const_iterator (*this, 0);
>> + }
>>
>> for (const class loop *loop: ...) will still use iterator instead
>> of const_iterator pair. We have to make the code look like:
>>
>> const auto& const_loops = loops_list (...);
>> for (const class loop *loop: const_loops)
>>
>> or
>> template<typename T> constexpr const T &as_const(T &t) noexcept { return t; }
>> for (const class loop *loop: as_const(loops_list...))
>>
>> Does it look good to add below as_const along with loops_list in cfgloop.h?
>>
>> +/* Provide the functionality of std::as_const to support range-based for
>> + to use const iterator. (We can't use std::as_const itself because it's
>> + a C++17 feature.) */
>> +template <typename T>
>> +constexpr const T &
>> +as_const (T &t) noexcept
>
> The noexcept is not needed because GCC is built -fno-exceptions. For
> consistency with all the other code that doesn't use noexcept, it
> should probably not be there.
>
Thanks for pointing out! Fixed it in v2.
>> +{
>> + return t;
>> +}
>> +
>
> That's one option. Another option (which could coexist with as_const)
> is to add cbegin() and cend() members, which are not overloaded for
> const and non-const, and so always return a const_iterator:
>
> const_iterator cbegin () const { return const_iterator (*this, 0); }
> iterator begin () const { return cbegin(); }
>
> And similarly for `end () const` and `cend () const`.
>
Thanks for the suggestion. As you pointed out in the later reply, the
range-based for loop doesn't use cbegin and cend, so I didn't add them
in v2.
BR,
Kewen
next prev parent reply other threads:[~2021-07-20 14:42 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-19 6:20 Kewen.Lin
2021-07-19 6:26 ` Andrew Pinski
2021-07-20 8:56 ` Kewen.Lin
2021-07-19 14:08 ` Jonathan Wakely
2021-07-20 8:56 ` Kewen.Lin
2021-07-19 14:34 ` Richard Biener
2021-07-20 8:57 ` Kewen.Lin
2021-07-19 15:59 ` Martin Sebor
2021-07-20 8:58 ` Kewen.Lin
2021-07-20 9:49 ` Jonathan Wakely
2021-07-20 9:50 ` Jonathan Wakely
2021-07-20 14:42 ` Kewen.Lin [this message]
2021-07-20 14:36 ` [PATCH v2] " Kewen.Lin
2021-07-22 12:56 ` Richard Biener
2021-07-22 12:56 ` Richard Biener
2021-07-23 8:41 ` [PATCH] Make loops_list support an optional loop_p root Kewen.Lin
2021-07-23 16:26 ` Martin Sebor
2021-07-27 2:25 ` Kewen.Lin
2021-07-29 8:01 ` Richard Biener
2021-07-30 5:20 ` [PATCH v2] " Kewen.Lin
2021-08-03 12:08 ` Richard Biener
2021-08-04 2:36 ` [PATCH v3] " Kewen.Lin
2021-08-04 10:01 ` Richard Biener
2021-08-04 10:47 ` Kewen.Lin
2021-08-04 12:04 ` Richard Biener
2021-08-05 8:50 ` Kewen.Lin
2021-07-23 8:35 ` [PATCH v3] Use range-based for loops for traversing loops Kewen.Lin
2021-07-23 16:10 ` Martin Sebor
2021-07-27 2:10 ` [PATCH v4] " Kewen.Lin
2021-07-29 7:48 ` Richard Biener
2021-07-30 7:18 ` Thomas Schwinge
2021-07-30 7:58 ` Kewen.Lin
2021-11-24 14:24 ` Reduce scope of a few 'class loop *loop' variables (was: [PATCH v4] Use range-based for loops for traversing loops) Thomas Schwinge
2021-11-24 16:58 ` Martin Jambor
2021-11-24 19:44 ` Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0bb41635-7ae0-4ea3-16e9-118c80f17151@linux.ibm.com \
--to=linkw@linux.ibm.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=jwakely.gcc@gmail.com \
--cc=msebor@gmail.com \
--cc=richard.guenther@gmail.com \
--cc=richard.sandiford@arm.com \
--cc=segher@kernel.crashing.org \
--cc=tbsaunde@tbsaunde.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).