public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94836] New: Failure to optimize condition based on known value of static variable
@ 2020-04-28 23:56 gabravier at gmail dot com
  2020-04-29  7:12 ` [Bug tree-optimization/94836] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gabravier at gmail dot com @ 2020-04-28 23:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94836
           Summary: Failure to optimize condition based on known value of
                    static variable
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

int f(int x)
{
    static int s;

    if (s)
        s = x;

    return s;
}

This can be optimized to `return 0`. This transformation is done by LLVM, but
not by GCC

LLVM -O3 outputs :

f(int): # @f(int)
  xor eax, eax
  ret

GCC -O3 outputs :

f(int):
  mov eax, DWORD PTR f(int)::s[rip]
  test eax, eax
  je .L1
  mov DWORD PTR f(int)::s[rip], edi
  mov eax, edi
.L1:
  ret

(see also https://godbolt.org/z/FzpjCb)

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

* [Bug tree-optimization/94836] Failure to optimize condition based on known value of static variable
  2020-04-28 23:56 [Bug tree-optimization/94836] New: Failure to optimize condition based on known value of static variable gabravier at gmail dot com
@ 2020-04-29  7:12 ` rguenth at gcc dot gnu.org
  2020-04-30  8:09 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-29  7:12 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
   Last reconfirmed|                            |2020-04-29
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  We do have varpool->used_by_single_function which we could use
to optimize this during VN.

Let me have a looksee.

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

* [Bug tree-optimization/94836] Failure to optimize condition based on known value of static variable
  2020-04-28 23:56 [Bug tree-optimization/94836] New: Failure to optimize condition based on known value of static variable gabravier at gmail dot com
  2020-04-29  7:12 ` [Bug tree-optimization/94836] " rguenth at gcc dot gnu.org
@ 2020-04-30  8:09 ` rguenth at gcc dot gnu.org
  2020-04-30  8:17 ` rguenth at gcc dot gnu.org
  2021-08-30  5:02 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-30  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
OK, so it's not that easy to do.  Consider

int f(int x)
{
    static int s;

    if (!s)
        s = x;

    return s;
}

which we cannot optimize in this way.  VN would need to work in a way
assigning the value 0 to 's' optimistically and if it eventually
arrives at s = x it would need to invalidate that optimistic assumption
and iterate.  That is it would need to see the function as

int f(int x)
{
    static int s;
 # s = PHI <0, s'>

    if (!s)
        s = x;

    return s;
 s' = s;
 goto start;
}

with the goto of course not explicit.  I suppose rather than integrating
the feature into their value-numbering scheme compilers implement special
purpose optimization (IIRC this pattern appears in SPEC).

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

* [Bug tree-optimization/94836] Failure to optimize condition based on known value of static variable
  2020-04-28 23:56 [Bug tree-optimization/94836] New: Failure to optimize condition based on known value of static variable gabravier at gmail dot com
  2020-04-29  7:12 ` [Bug tree-optimization/94836] " rguenth at gcc dot gnu.org
  2020-04-30  8:09 ` rguenth at gcc dot gnu.org
@ 2020-04-30  8:17 ` rguenth at gcc dot gnu.org
  2021-08-30  5:02 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-30  8:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|rguenth at gcc dot gnu.org         |unassigned at gcc dot gnu.org
             Status|ASSIGNED                    |NEW
                 CC|                            |rguenth at gcc dot gnu.org

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

* [Bug tree-optimization/94836] Failure to optimize condition based on known value of static variable
  2020-04-28 23:56 [Bug tree-optimization/94836] New: Failure to optimize condition based on known value of static variable gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-30  8:17 ` rguenth at gcc dot gnu.org
@ 2021-08-30  5:02 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-30  5:02 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |missed-optimization

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

end of thread, other threads:[~2021-08-30  5:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-28 23:56 [Bug tree-optimization/94836] New: Failure to optimize condition based on known value of static variable gabravier at gmail dot com
2020-04-29  7:12 ` [Bug tree-optimization/94836] " rguenth at gcc dot gnu.org
2020-04-30  8:09 ` rguenth at gcc dot gnu.org
2020-04-30  8:17 ` rguenth at gcc dot gnu.org
2021-08-30  5:02 ` pinskia at gcc dot gnu.org

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