public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Avoid removing an if (false) statement and its corresponding branch
@ 2023-07-23 12:45 Julian Waters
  2023-07-23 14:14 ` Gabriel Ravier
  0 siblings, 1 reply; 5+ messages in thread
From: Julian Waters @ 2023-07-23 12:45 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 226 bytes --]

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Avoid removing an if (false) statement and its corresponding branch
  2023-07-23 12:45 Avoid removing an if (false) statement and its corresponding branch Julian Waters
@ 2023-07-23 14:14 ` Gabriel Ravier
  2023-07-23 14:27   ` Julian Waters
  0 siblings, 1 reply; 5+ messages in thread
From: Gabriel Ravier @ 2023-07-23 14:14 UTC (permalink / raw)
  To: Julian Waters, gcc-help

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Avoid removing an if (false) statement and its corresponding branch
  2023-07-23 14:14 ` Gabriel Ravier
@ 2023-07-23 14:27   ` Julian Waters
  2023-07-23 16:53     ` David Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Julian Waters @ 2023-07-23 14:27 UTC (permalink / raw)
  To: Gabriel Ravier, gcc-help

[-- Attachment #1: Type: text/plain, Size: 954 bytes --]

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!

best regards,
Julian

On Sun, Jul 23, 2023 at 10:14 PM Gabriel Ravier <gabravier@gmail.com> 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.
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Avoid removing an if (false) statement and its corresponding branch
  2023-07-23 14:27   ` Julian Waters
@ 2023-07-23 16:53     ` David Brown
  2023-07-25  5:52       ` Julian Waters
  0 siblings, 1 reply; 5+ messages in thread
From: David Brown @ 2023-07-23 16:53 UTC (permalink / raw)
  To: Julian Waters, Gabriel Ravier, gcc-help

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 <gabravier@gmail.com> 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.
>>
>>
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Avoid removing an if (false) statement and its corresponding branch
  2023-07-23 16:53     ` David Brown
@ 2023-07-25  5:52       ` Julian Waters
  0 siblings, 0 replies; 5+ messages in thread
From: Julian Waters @ 2023-07-25  5:52 UTC (permalink / raw)
  To: David Brown, gcc-help

[-- Attachment #1: Type: text/plain, Size: 1696 bytes --]

Thanks for the reply, but the solution was right there all along...

if (volatile bool _ = false)

On Mon, Jul 24, 2023 at 12:53 AM David Brown <david.brown@hesbynett.no>
wrote:

> 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 <gabravier@gmail.com>
> 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.
> >>
> >>
> >
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-25  5:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-23 12:45 Avoid removing an if (false) statement and its corresponding branch Julian Waters
2023-07-23 14:14 ` Gabriel Ravier
2023-07-23 14:27   ` Julian Waters
2023-07-23 16:53     ` David Brown
2023-07-25  5:52       ` Julian Waters

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).