From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mengyan1223.wang (mengyan1223.wang [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id C2C903858D3C for ; Sun, 23 Jan 2022 09:12:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2C903858D3C Received: from [IPv6:240e:358:11fa:8200:dc73:854d:832e:4] (unknown [IPv6:240e:358:11fa:8200:dc73:854d:832e:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384)) (Client did not present a certificate) (Authenticated sender: xry111@mengyan1223.wang) by mengyan1223.wang (Postfix) with ESMTPSA id 44AE165CD5; Sun, 23 Jan 2022 04:12:10 -0500 (EST) Message-ID: <7107aa063c391c0c3dd9e1b9641f7e5a5df9aa31.camel@mengyan1223.wang> Subject: Re: Pure/const function not getting executed as the first operand to logical OR ( || ) (C++) From: Xi Ruoyao To: Vishal Subramanyam , "gcc-help@gcc.gnu.org" Date: Sun, 23 Jan 2022 17:12:05 +0800 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3030.1 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, JMQ_SPF_NEUTRAL, 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:12:16 -0000 On Sun, 2022-01-23 at 08:37 +0000, Vishal Subramanyam via Gcc-help wrote: > When compiling the following C++ snippet with -O0, I'm getting a > Floating Point Exception on my Ubuntu 21.10, g++ 11.2.0, x86-64. > > #include > > int func(int n, int r) > { >     return n % r; > } > int main() > { >     const int n = 15, r = 0; >     if (r == 0 || func(n, r)) >         std::cout << "YES" << std::endl; >     else >         std::cout << "NO" << std::endl; >     if (func(n, r) || r == 0) >         std::cout << "YES" << std::endl; >     else >         std::cout << "NO" << std::endl; >     return 0; > } > > With -O0, I'm getting a Floating Point error in the second if > condition since it involves executing the "func" function where > division by zero occurs. > > YES > Floating point exception (core dumped) > > Now, with -O1 optimization, this is the output. > > YES > YES > > The "func" call has been optimized away. I checked the assembly code > output with -S and verified the same. Now, I would like to know what > was the optimization (the optimization option) that led to this > happening. When I tried compiling the same code with -O0 but with an > additional "-fipa-pure-const", I'm getting the same output as the O1 > program with two YESes. I can reproduce this behavior, but I have some doubt on this: I remember someone told me that -f* options had no effect with -O0. > But when I try compiling with both the -O1 and -fno-ipa-pure-const, I > am not getting the Floating Point Exception. If -fipa-pure-const is > responsible for the optimization that removes the function call, > shouldn't the function call occur with -fno-ipa-pure-const. I have > tried various combinations of options related to dead code elimination > and branch probabilities, but can't figure out what's happening? You need -fno-inline. If the function call is inlined completely, then the compiler can find and remove dead code without any inter-procedural analysis. > g++ t.cc -O1 -fno-inline -fno-ipa-pure-const && ./a.out > YES > [1] 4151 floating point exception (core dumped) ./a.out -- Xi Ruoyao School of Aerospace Science and Technology, Xidian University