public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/99997] New: Missed optimisation with -Os
@ 2021-04-09 14:03 dhowells at redhat dot com
  2021-04-09 14:13 ` [Bug c/99997] " matthew at wil dot cx
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: dhowells at redhat dot com @ 2021-04-09 14:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99997
           Summary: Missed optimisation with -Os
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dhowells at redhat dot com
  Target Milestone: ---

Created attachment 50538
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50538&action=edit
Test source

Using the Fedora 33 x86_64 compiler:
gcc version 10.2.1 20201125 (Red Hat 10.2.1-9) (GCC) 

Building the following (see also attached file):

typedef _Bool bool;
#define __always_inline inline __attribute__((__always_inline__))
enum { PG_head = 16 };
struct page {
        unsigned long flags;
        unsigned long compound_head;    /* Bit zero is set */
};
static inline bool constant_test_bit(int nr, const void *addr)
{
        const unsigned int *p = (const unsigned int *)addr;
        return ((1UL << (nr & 31)) & (p[nr >> 5])) != 0;
}
static __always_inline bool PageTail(struct page *page)
{
        return page->compound_head & 1;
}
static __always_inline bool PageCompound(struct page *page)
{
        return constant_test_bit(PG_head, &page->flags) || PageTail(page);
}
bool PageTransCompound(struct page *page)
{
        return PageCompound(page);
}

with "gcc -Os" I get the following assembly:

PageTransCompound:
.LFB3:
        .cfi_startproc
        movl    (%rdi), %edx
        movl    $1, %eax
        btl     $16, %edx
        jc      .L2
        movq    8(%rdi), %rax
        andl    $1, %eax
.L2:
        andl    $1, %eax
        ret
        .cfi_endproc

There are two consecutive identical ANDL instructions, one of which is
superfluous.  The compile could eliminate the one that's immediately prior to
the .L2 instruction.

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

* [Bug c/99997] Missed optimisation with -Os
  2021-04-09 14:03 [Bug c/99997] New: Missed optimisation with -Os dhowells at redhat dot com
@ 2021-04-09 14:13 ` matthew at wil dot cx
  2021-04-12  7:48 ` [Bug tree-optimization/99997] " rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: matthew at wil dot cx @ 2021-04-09 14:13 UTC (permalink / raw)
  To: gcc-bugs

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

Matthew Wilcox <matthew at wil dot cx> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |matthew at wil dot cx

--- Comment #1 from Matthew Wilcox <matthew at wil dot cx> ---
Actually, it should eliminate the one _after_ the L2 label.  It just moved the
constant 1 into %eax and doesn't need to limit it again.

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

* [Bug tree-optimization/99997] Missed optimisation with -Os
  2021-04-09 14:03 [Bug c/99997] New: Missed optimisation with -Os dhowells at redhat dot com
  2021-04-09 14:13 ` [Bug c/99997] " matthew at wil dot cx
@ 2021-04-12  7:48 ` rguenth at gcc dot gnu.org
  2021-08-03 22:37 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-12  7:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
          Component|c                           |tree-optimization
   Last reconfirmed|                            |2021-04-12
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
We expand from

  _10 = (_Bool) _9;
  if (_10 != 0)
    goto <bb 4>; [50.00%]
  else
    goto <bb 3>; [50.00%]
;;    succ:       4
;;                3

;;   basic block 3, loop depth 0
;;    pred:       2
  _3 = page_2(D)->compound_head;
  _4 = (_Bool) _3;
  _11 = (int) _4;
;;    succ:       4

;;   basic block 4, loop depth 0
;;    pred:       2
;;                3
  # iftmp.0_5 = PHI <1(2), _11(3)>
  _6 = (_Bool) iftmp.0_5;
  return _6;

and what's obvious to see is that there are excessive conversions on 3->4.
There's the opportunity to constant fold the _6 = (_Bool) iftmp.0_5 conversion
on 2->4 by adjusting the type of iftmp.0_5 pushing another required conversion
on the 3->4 edge which should nicely combine with the conversions already
present there.  At -O2 PRE performs this, at -O1 the same issue is present.

Note the & 1 truncations are inserted by RTL expansion and since we lack
partial DCE / sink on RTL it doesn't get optimized.

I'll look into the GIMPLE simplification.

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

* [Bug tree-optimization/99997] Missed optimisation with -Os
  2021-04-09 14:03 [Bug c/99997] New: Missed optimisation with -Os dhowells at redhat dot com
  2021-04-09 14:13 ` [Bug c/99997] " matthew at wil dot cx
  2021-04-12  7:48 ` [Bug tree-optimization/99997] " rguenth at gcc dot gnu.org
@ 2021-08-03 22:37 ` pinskia at gcc dot gnu.org
  2022-02-02  1:07 ` pinskia at gcc dot gnu.org
  2023-05-24  0:27 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-03 22:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|rguenth at gcc dot gnu.org         |pinskia at gcc dot gnu.org

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm,  phi-opt should have done it:

  if (_10 != 0)
    goto <bb 4>; [50.00%]
  else
    goto <bb 3>; [50.00%]

  <bb 3> [local count: 536870913]:
  _3 = page_2(D)->compound_head;
  _4 = (_Bool) _3;
  _11 = (int) _4;

  <bb 4> [local count: 1073741824]:
  # iftmp.0_5 = PHI <1(2), _11(3)>

We have code to move the conversion in phiopt but we are not doing it for some
reason:
Take:
bool f(int tf, long unsigned int r)
{
  int t = 1;
  if (tf)
    t = ((r&1)!=0);
  return t;
}
bool f2(int tf, long unsigned int r)
{
  int t = 1;
  bool tt = ((r&1)!=0);
  if (tf)
    t = tt;
  return t;
}
These two functions should produce 100% the same code but they don't.
Mine.

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

* [Bug tree-optimization/99997] Missed optimisation with -Os
  2021-04-09 14:03 [Bug c/99997] New: Missed optimisation with -Os dhowells at redhat dot com
                   ` (2 preceding siblings ...)
  2021-08-03 22:37 ` pinskia at gcc dot gnu.org
@ 2022-02-02  1:07 ` pinskia at gcc dot gnu.org
  2023-05-24  0:27 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-02  1:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=104339,
                   |                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=71016

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Part of the fix is PR 104339 but the code which was added to fix PR 71016 is
what is really causing the issue. We need to figure out how to improve the code
that handles the fix for PR 71016.

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

* [Bug tree-optimization/99997] Missed optimisation with -Os
  2021-04-09 14:03 [Bug c/99997] New: Missed optimisation with -Os dhowells at redhat dot com
                   ` (3 preceding siblings ...)
  2022-02-02  1:07 ` pinskia at gcc dot gnu.org
@ 2023-05-24  0:27 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-24  0:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I suspect if we do something about:
  # iftmp.0_8 = PHI <0(3), 1(4)>
  _9 = (_Bool) iftmp.0_8;


First things might just work. Let me look into that. Plus there might be an
expand issue ...

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

end of thread, other threads:[~2023-05-24  0:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-09 14:03 [Bug c/99997] New: Missed optimisation with -Os dhowells at redhat dot com
2021-04-09 14:13 ` [Bug c/99997] " matthew at wil dot cx
2021-04-12  7:48 ` [Bug tree-optimization/99997] " rguenth at gcc dot gnu.org
2021-08-03 22:37 ` pinskia at gcc dot gnu.org
2022-02-02  1:07 ` pinskia at gcc dot gnu.org
2023-05-24  0:27 ` 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).