From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 5AA5B3861834 for ; Tue, 20 Jul 2021 08:58:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5AA5B3861834 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16K8lAwt095880; Tue, 20 Jul 2021 04:58:20 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 39wu9fg8cm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 04:58:20 -0400 Received: from m0098416.ppops.net (m0098416.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 16K8noQc102294; Tue, 20 Jul 2021 04:58:19 -0400 Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0b-001b2d01.pphosted.com with ESMTP id 39wu9fg8be-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 04:58:19 -0400 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 16K8vMlp016211; Tue, 20 Jul 2021 08:58:17 GMT Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by ppma03ams.nl.ibm.com with ESMTP id 39upu8965q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Jul 2021 08:58:17 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 16K8wFVo31654304 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Jul 2021 08:58:15 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3489811C04A; Tue, 20 Jul 2021 08:58:15 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1E5F811C05C; Tue, 20 Jul 2021 08:58:12 +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 08:58:11 +0000 (GMT) Subject: Re: [RFC/PATCH] Use range-based for loops for traversing loops To: Martin Sebor Cc: Richard Biener , Richard Sandiford , Jakub Jelinek , tbsaunde@tbsaunde.org, Segher Boessenkool , Jonathan Wakely , GCC Patches References: <0a8b77ba-1d54-1eff-b54d-d2cb1e769e09@linux.ibm.com> <211abd8e-6a9b-1a22-dcfc-ede0c49f4223@gmail.com> From: "Kewen.Lin" Message-ID: <8d70f4f8-8855-750e-9b64-e623b46dcada@linux.ibm.com> Date: Tue, 20 Jul 2021 16:58:10 +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: <211abd8e-6a9b-1a22-dcfc-ede0c49f4223@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: RS5dgCyGCrt0qKkzOvbuaky6DfLP4XZV X-Proofpoint-GUID: UqN-M_gkJT_xjgx9dDgJIYm8D44DAoGs X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-20_04:2021-07-19, 2021-07-20 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 mlxscore=0 suspectscore=0 clxscore=1015 adultscore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 malwarescore=0 mlxlogscore=899 impostorscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107200051 X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_MSPIKE_H2, 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 08:58:23 -0000 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 +{ + return t; +} + BR, Kewen