public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105728] New: dead store to static var not optimized out
@ 2022-05-25 14:14 felix-gcc at fefe dot de
  2022-05-25 16:21 ` Jan Hubicka
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: felix-gcc at fefe dot de @ 2022-05-25 14:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105728

            Bug ID: 105728
           Summary: dead store to static var not optimized out
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: felix-gcc at fefe dot de
  Target Milestone: ---

Consider this piece of test code:

int dummy1()
{
  static int removeme = 0;
  if (removeme)
  {
      return 0;
  }
  removeme = 1;
  return 0;
}

int dummy2()
{
  static int removeme = 0;
  if (!removeme)
    removeme = 1;
  return 0;
}

int dummy3()
{
  static int removeme = 0;
  removeme = 1;
  return 0;
}

To me, all of these do the same thing and should generate the same code.
As nobody else can see removeme, and we aren't leaking its address, shouldn't
the compiler be able to deduce that all accesses to removeme are
inconsequential and can be removed?

My gcc 11.3 generates a condidion and a store and a return 0 for dummy1, the
same thing for dummy2, but for dummy3 it understands that it only needs to emit
a return 0.

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

* Re: [Bug c/105728] New: dead store to static var not optimized out
  2022-05-25 14:14 [Bug c/105728] New: dead store to static var not optimized out felix-gcc at fefe dot de
@ 2022-05-25 16:21 ` Jan Hubicka
  2022-05-25 16:21 ` [Bug c/105728] " hubicka at kam dot mff.cuni.cz
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Jan Hubicka @ 2022-05-25 16:21 UTC (permalink / raw)
  To: felix-gcc at fefe dot de; +Cc: gcc-bugs

> To me, all of these do the same thing and should generate the same code.
> As nobody else can see removeme, and we aren't leaking its address, shouldn't
> the compiler be able to deduce that all accesses to removeme are
> inconsequential and can be removed?
> 
> My gcc 11.3 generates a condidion and a store and a return 0 for dummy1, the
> same thing for dummy2, but for dummy3 it understands that it only needs to emit
> a return 0.

GCC detects "write olny" variables and that is what matches for dummy3.
I am not 100% sure it is valid to do the optimization in other two cases
since when multiple threads are considered. In any case we lack tracking
of constants stored to global variables which is something ipa-cp can be
extended to.

Honza


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

* [Bug c/105728] dead store to static var not optimized out
  2022-05-25 14:14 [Bug c/105728] New: dead store to static var not optimized out felix-gcc at fefe dot de
  2022-05-25 16:21 ` Jan Hubicka
@ 2022-05-25 16:21 ` hubicka at kam dot mff.cuni.cz
  2022-05-27  6:25 ` [Bug ipa/105728] " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: hubicka at kam dot mff.cuni.cz @ 2022-05-25 16:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105728

--- Comment #1 from hubicka at kam dot mff.cuni.cz ---
> To me, all of these do the same thing and should generate the same code.
> As nobody else can see removeme, and we aren't leaking its address, shouldn't
> the compiler be able to deduce that all accesses to removeme are
> inconsequential and can be removed?
> 
> My gcc 11.3 generates a condidion and a store and a return 0 for dummy1, the
> same thing for dummy2, but for dummy3 it understands that it only needs to emit
> a return 0.

GCC detects "write olny" variables and that is what matches for dummy3.
I am not 100% sure it is valid to do the optimization in other two cases
since when multiple threads are considered. In any case we lack tracking
of constants stored to global variables which is something ipa-cp can be
extended to.

Honza

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

* [Bug ipa/105728] dead store to static var not optimized out
  2022-05-25 14:14 [Bug c/105728] New: dead store to static var not optimized out felix-gcc at fefe dot de
  2022-05-25 16:21 ` Jan Hubicka
  2022-05-25 16:21 ` [Bug c/105728] " hubicka at kam dot mff.cuni.cz
@ 2022-05-27  6:25 ` rguenth at gcc dot gnu.org
  2022-05-27 12:24 ` rguenth at gcc dot gnu.org
  2022-05-27 13:16 ` felix-gcc at fefe dot de
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-27  6:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105728

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |ipa
   Last reconfirmed|                            |2022-05-27
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
the dummy1/dummy2 optimizations could eventually be done by DCE if we knew that
'removeme' is only ever accessed from 'dummy1' / 'dummy2', in that case we
could consider the variable local and since there's no data dependence on it
we could elide it.

Don't we track that already in IPA?  I think local passes cannot rely on
IPA references?  We also need to consider

void foo (int *);
int dummy1()
{
  static int removeme = 0;
  static int *p = &removeme;
  foo (p);
  if (removeme)
  {
      return 0;
  }
  removeme = 1;
  return 0;
}

thus global CTORs (the above will see the init in dummy1(), but not sure
whether we can rely on that?)

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

* [Bug ipa/105728] dead store to static var not optimized out
  2022-05-25 14:14 [Bug c/105728] New: dead store to static var not optimized out felix-gcc at fefe dot de
                   ` (2 preceding siblings ...)
  2022-05-27  6:25 ` [Bug ipa/105728] " rguenth at gcc dot gnu.org
@ 2022-05-27 12:24 ` rguenth at gcc dot gnu.org
  2022-05-27 13:16 ` felix-gcc at fefe dot de
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-27 12:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105728

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Btw, any such optimization would be prone to remove 'debug_cnt' in

void foo()
{
  static int debug_cnt = 0;
  debug_cnt++;
  .. stuff ..
}

because here nothing depends on debug_cnt.  For some printf-style
debugging that might be surprising.  It of course already happens for

void foo()
{
  static bool ever_reached = false;
  ever_reached = true;
}

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

* [Bug ipa/105728] dead store to static var not optimized out
  2022-05-25 14:14 [Bug c/105728] New: dead store to static var not optimized out felix-gcc at fefe dot de
                   ` (3 preceding siblings ...)
  2022-05-27 12:24 ` rguenth at gcc dot gnu.org
@ 2022-05-27 13:16 ` felix-gcc at fefe dot de
  4 siblings, 0 replies; 6+ messages in thread
From: felix-gcc at fefe dot de @ 2022-05-27 13:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105728

--- Comment #4 from felix-gcc at fefe dot de ---
If you do have a printf that references debug_cnt, it wouldn't be removed,
right?

If you expect unreferenced variables to not be optimized out, you can always
compile without optimizer. For local variables even that doesn't help with
clang already.

OTOH we do have attributes "used" and "unused". They could be extended to
variables.

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

end of thread, other threads:[~2022-05-27 13:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-25 14:14 [Bug c/105728] New: dead store to static var not optimized out felix-gcc at fefe dot de
2022-05-25 16:21 ` Jan Hubicka
2022-05-25 16:21 ` [Bug c/105728] " hubicka at kam dot mff.cuni.cz
2022-05-27  6:25 ` [Bug ipa/105728] " rguenth at gcc dot gnu.org
2022-05-27 12:24 ` rguenth at gcc dot gnu.org
2022-05-27 13:16 ` felix-gcc at fefe dot de

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).