From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from IND01-MA1-obe.outbound.protection.outlook.com (mail-ma1ind01olkn0153.outbound.protection.outlook.com [104.47.100.153]) by sourceware.org (Postfix) with ESMTPS id C2C063858D3C for ; Sun, 23 Jan 2022 09:58:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2C063858D3C ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BZFWwywZNX2d0hqIpwvv3KKcVOXUz4D4SLBIW8TvruIv5WA/5aaFLlJQMAMQa6pLC1BWG7mmBDhVCyF8Iz6Tvf5ez2xtKcX5QNDDaeXIRbakAcdiOlLea3uUFDfKIoZ4NtLHX2ykYLJPvjHd0l2v5+0D3jQofCxeqybnS9v1C+nV4qWX7585gGjjXAQW6ehjCmxAsUtDBVzy2IfsHHQOamJ1D8NKLvGIGJwFjE/Js7zbZiik6gRCRSBTzzt/ZLldpbSsfl5TunGlNv0pMXx3ztRbrH9PL2+SYDmFs70PpuW4Mwo1Kh0mtDox3q/+9jb/GSI4AoHZYVcPyM9XkNU3+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hK5L2fBvBQv2rW17L96hn9BFfsSNU+UxsgheOsgbDrM=; b=Z5tyHScVd6Ik6B2fqEJJ0zBFctjEYS2CJAJ8Z0CsztIuel/pp0fIZzgZkLQv7VRuqRQ5uiiCiz7VcyL0bkwA+KxyMEb5zbIAzvMUh0TCHRN0Xoa5j7m1Usow30D0xUgz49RaTo35s+1wSyW0UvU++2Bsrye/Tpfx6oPtSkFQJJ++FBhDPkmV6/uf7fzRqk2aCWWU2SPF+BANCwwJAsE9JYQffkqf6DQpMMY9cCugxxnzW0DCEUwSSDtCzf5GrFYo6/mcYfqAs/5qtLI92ngvz+jxadQu7avbuPpctrnTyCM7A0oEdGge4Wh92WJYfzijXxbJEJvl25xOxWWY3k0e2g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from MA1PR0101MB1559.INDPRD01.PROD.OUTLOOK.COM (52.134.142.141) by PN2PR01MB5285.INDPRD01.PROD.OUTLOOK.COM (10.141.17.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.7; Sun, 23 Jan 2022 09:58:32 +0000 Received: from MA1PR0101MB1559.INDPRD01.PROD.OUTLOOK.COM ([fe80::10ef:c976:293:fbb7]) by MA1PR0101MB1559.INDPRD01.PROD.OUTLOOK.COM ([fe80::10ef:c976:293:fbb7%10]) with mapi id 15.20.4909.017; Sun, 23 Jan 2022 09:58:32 +0000 From: Vishal Subramanyam To: "gcc-help@gcc.gnu.org" Subject: Fw: Pure/const function not getting executed as the first operand to logical OR ( || ) (C++) Thread-Topic: Pure/const function not getting executed as the first operand to logical OR ( || ) (C++) Thread-Index: AQHYEDFsnpbDIN0L3Em2YYRQSo5zuqxwUjGAgAALf7WAAAFBjQ== Date: Sun, 23 Jan 2022 09:58:32 +0000 Message-ID: References: <7107aa063c391c0c3dd9e1b9641f7e5a5df9aa31.camel@mengyan1223.wang> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: suggested_attachment_session_id: cf111e9e-cbe9-c640-4c05-1c2bc94f1273 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [x+6qkuSfmgKXEgePpOLb2vDdGqbpb4G3] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: d2d2f162-ca1d-49cd-08c7-08d9de56ea29 x-ms-traffictypediagnostic: PN2PR01MB5285:EE_ x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: gFSOo7LRvCFooUllr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pHAAMAYIjjI9XRjwf5KuM8fZ3D39ZumwKgKlASQdITvIa+p8nHLqUbUR86iB6kekURSOnycl8Unn4iqDGVAWhPFOiNIgccQvUaR7oUsMwIqHvb5cFsTyt4dZBPS+91KunHRLAAr6sUL5/ISzcrIQKyGH5fv+LkxN2MOZL9Sbs563wArPY3r5Mu6UN/m2z9oArZWyLDWXjjV8UZjn4CIX+z9geXubfEbiPcSnusgFU7hD1DUD6fRFbUuc2TBDmujYJdVh6nshaTgJjR+lda6HvgOVOTUKyhrE8nvBvl8uEcSWmDKPfL/7MKm40s6VWz49EYbaIDuISBmICitpxmpSG/5VmMZbZrV5qWceZCTxamjwkLWJLMFfp6JPdn35LtT17iXwtxschDHhznM0XjnRoDz5oM26m5O5NfZ1dxX96BMxP3HDBZU5xDz8n178v/DVFtr3p4G3FL+ki8c35V7ern17yWqY9XH8obQMJa7dMir/s7zVXEkb6IhSup8W Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MA1PR0101MB1559.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: d2d2f162-ca1d-49cd-08c7-08d9de56ea29 X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Jan 2022 09:58:32.5966 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PN2PR01MB5285 X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no 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-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Jan 2022 09:58:39 -0000 =0A= =0A= ________________________________________=0A= From: Vishal Subramanyam =0A= Sent: Sunday, January 23, 2022 9:56 AM=0A= To: Xi Ruoyao=0A= Subject: Re: Pure/const function not getting executed as the first operand = to logical OR ( || ) (C++)=0A= =0A= This still doesn't explain why an O1 level optimization can violate the sta= ndard by not evaluating the first operand.=0A= What optimization is my code triggering with -fno-inline? My function clear= ly has a return value, so how did the compiler=0A= decide that the return value doesn't matter?=0A= =0A= Thanks,=0A= Vishal=0A= =0A= ________________________________________=0A= From: Xi Ruoyao =0A= Sent: Sunday, January 23, 2022 9:12 AM=0A= To: Vishal Subramanyam; gcc-help@gcc.gnu.org=0A= Subject: Re: Pure/const function not getting executed as the first operand = to logical OR ( || ) (C++)=0A= =0A= On Sun, 2022-01-23 at 08:37 +0000, Vishal Subramanyam via Gcc-help=0A= wrote:=0A= > When compiling the following C++ snippet with -O0, I'm getting a=0A= > Floating Point Exception on my Ubuntu 21.10, g++ 11.2.0, x86-64.=0A= >=0A= > #include =0A= >=0A= > int func(int n, int r)=0A= > {=0A= > return n % r;=0A= > }=0A= > int main()=0A= > {=0A= > const int n =3D 15, r =3D 0;=0A= > if (r =3D=3D 0 || func(n, r))=0A= > std::cout << "YES" << std::endl;=0A= > else=0A= > std::cout << "NO" << std::endl;=0A= > if (func(n, r) || r =3D=3D 0)=0A= > std::cout << "YES" << std::endl;=0A= > else=0A= > std::cout << "NO" << std::endl;=0A= > return 0;=0A= > }=0A= >=0A= > With -O0, I'm getting a Floating Point error in the second if=0A= > condition since it involves executing the "func" function where=0A= > division by zero occurs.=0A= >=0A= > YES=0A= > Floating point exception (core dumped)=0A= >=0A= > Now, with -O1 optimization, this is the output.=0A= >=0A= > YES=0A= > YES=0A= >=0A= > The "func" call has been optimized away. I checked the assembly code=0A= > output with -S and verified the same. Now, I would like to know what=0A= > was the optimization (the optimization option) that led to this=0A= > happening. When I tried compiling the same code with -O0 but with an=0A= > additional "-fipa-pure-const", I'm getting the same output as the O1=0A= > program with two YESes.=0A= =0A= I can reproduce this behavior, but I have some doubt on this: I remember=0A= someone told me that -f* options had no effect with -O0.=0A= =0A= > But when I try compiling with both the -O1 and -fno-ipa-pure-const, I=0A= > am not getting the Floating Point Exception. If -fipa-pure-const is=0A= > responsible for the optimization that removes the function call,=0A= > shouldn't the function call occur with -fno-ipa-pure-const. I have=0A= > tried various combinations of options related to dead code elimination=0A= > and branch probabilities, but can't figure out what's happening?=0A= =0A= You need -fno-inline. If the function call is inlined completely, then=0A= the compiler can find and remove dead code without any inter-procedural=0A= analysis.=0A= =0A= > g++ t.cc -O1 -fno-inline -fno-ipa-pure-const && ./a.out=0A= > YES=0A= > [1] 4151 floating point exception (core dumped) ./a.out=0A= --=0A= Xi Ruoyao =0A= School of Aerospace Science and Technology, Xidian University=0A=