From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp01.hesbynett.no (smtp01.hesbynett.no [81.29.32.167]) by sourceware.org (Postfix) with ESMTPS id 923E93858D28 for ; Sun, 23 Jul 2023 16:53:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 923E93858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hesbynett.no Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hesbynett.no Received: from [IPV6:2a01:799:443:8d00:15a9:257a:e051:f77c] (unknown [IPv6:2a01:799:443:8d00:15a9:257a:e051:f77c]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: ma-4381) by smtp01.hesbynett.no (Postfix) with ESMTPSA id 96115209A5; Sun, 23 Jul 2023 18:52:49 +0200 (CEST) Message-ID: Date: Sun, 23 Jul 2023 18:53:30 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: Avoid removing an if (false) statement and its corresponding branch Content-Language: en-GB To: Julian Waters , Gabriel Ravier , gcc-help@gcc.gnu.org References: From: David Brown In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3032.1 required=5.0 tests=BAYES_00,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 23/07/2023 16:27, Julian Waters via Gcc-help wrote: > I just tried it and it does work, even on O3, however I would like to avoid > allocating a variable if possible. It's a shame that gnu::used doesn't > seem to work with if statements (If anyone knows where to look in the > source code do tell me). But thanks for the suggestion nonetheless! > How about: static inline bool False(void) { bool b = false; asm volatile("" : "+r" (b)); return b; } Then use "if (False()) ..." instead of "if (false) ...". The generated overhead is going to be minimal, and no volatile variables are created. It's also quite cool (IMHO) to have 100% portable inline assembly! mvh., David > best regards, > Julian > > On Sun, Jul 23, 2023 at 10:14 PM Gabriel Ravier wrote: > >> On 7/23/23 14:45, Julian Waters via Gcc-help wrote: >>> Hi all, >>> >>> Is there a way to stop gcc from nuking an if (false) statement and the >>> corresponding branch from the compiled code for debugging purposes? >>> Even turning off all optimizations doesn't achieve this >>> >>> best regards, >>> Julian >> >> IMO the simplest way would be to define something like `static const >> volatile bool unoptimizable_false = false;` somewhere and use it in >> place of `false`, when you want to avoid the if statement being >> optimized out. >> >> >