From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 0275C398843A for ; Tue, 20 Jul 2021 14:42:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0275C398843A Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16KEYP0M026709; Tue, 20 Jul 2021 10:42:47 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 39wy71k52n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 10:42:46 -0400 Received: from m0098393.ppops.net (m0098393.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 16KEYoPd028516; Tue, 20 Jul 2021 10:42:46 -0400 Received: from ppma04fra.de.ibm.com (6a.4a.5195.ip4.static.sl-reverse.com [149.81.74.106]) by mx0a-001b2d01.pphosted.com with ESMTP id 39wy71k51n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 10:42:46 -0400 Received: from pps.filterd (ppma04fra.de.ibm.com [127.0.0.1]) by ppma04fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 16KEgh7k016959; Tue, 20 Jul 2021 14:42:43 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma04fra.de.ibm.com with ESMTP id 39upu88sxc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 14:42:43 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 16KEgfaN26870202 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Jul 2021 14:42:41 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2605911C04A; Tue, 20 Jul 2021 14:42:41 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3CD0F11C052; Tue, 20 Jul 2021 14:42:38 +0000 (GMT) Received: from KewenLins-MacBook-Pro.local (unknown [9.197.247.15]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 20 Jul 2021 14:42:37 +0000 (GMT) Subject: Re: [RFC/PATCH] Use range-based for loops for traversing loops To: Jonathan Wakely Cc: Martin Sebor , Richard Biener , Richard Sandiford , Jakub Jelinek , Trevor Saunders , Segher Boessenkool , GCC Patches References: <0a8b77ba-1d54-1eff-b54d-d2cb1e769e09@linux.ibm.com> <211abd8e-6a9b-1a22-dcfc-ede0c49f4223@gmail.com> <8d70f4f8-8855-750e-9b64-e623b46dcada@linux.ibm.com> From: "Kewen.Lin" Message-ID: <0bb41635-7ae0-4ea3-16e9-118c80f17151@linux.ibm.com> Date: Tue, 20 Jul 2021 22:42:36 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: jrAxxFDaJEKlp9Au1iXpJnn3iypjcTbm X-Proofpoint-ORIG-GUID: YHB0t00FD4EHk0Kf16aFI74y3V_Fksuy X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-20_09:2021-07-19, 2021-07-20 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 lowpriorityscore=0 clxscore=1015 suspectscore=0 mlxlogscore=999 spamscore=0 mlxscore=0 malwarescore=0 adultscore=0 phishscore=0 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107200091 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP 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: Tue, 20 Jul 2021 14:42:55 -0000 on 2021/7/20 下午5:49, Jonathan Wakely wrote: > On Tue, 20 Jul 2021 at 09:58, Kewen.Lin 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 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 >> +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