public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct
@ 2021-02-01  8:10 gabravier at gmail dot com
  2021-02-02  7:58 ` [Bug tree-optimization/98908] " rguenth at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: gabravier at gmail dot com @ 2021-02-01  8:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98908
           Summary: Failure to optimize arithmetic involving struct
                    members into operating on the entire struct
           Product: gcc
           Version: 11.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: ---

struct reg {
    uint8_t l;
    uint8_t h;
};

reg f(reg x)
{
    return {(uint8_t)(x.l & 0xFE), (uint8_t)(x.h & 0x80)};
}

This can be optimized to this:

reg f(reg x)
{
    uint16_t tmp = (x.l | x.h << 8) & 0x80FE;
    return {(uint8_t)tmp, (uint8_t)(tmp >> 8)};
}

. This transformation is done by LLVM, but not by GCC.

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
@ 2021-02-02  7:58 ` rguenth at gcc dot gnu.org
  2021-09-03  0:15 ` gabravier at gmail dot com
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-02-02  7:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |53947
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-02-02
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
The vectorizer would do this if there's a V2QImode or generic vect would be
enabled for sth not just word_mode size.  For SLP vectorization it should be
easy to restrict the search space (aka choose a prefered SIMD mode).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53947
[Bug 53947] [meta-bug] vectorizer missed-optimizations

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
  2021-02-02  7:58 ` [Bug tree-optimization/98908] " rguenth at gcc dot gnu.org
@ 2021-09-03  0:15 ` gabravier at gmail dot com
  2021-09-03  0:27 ` pinskia at gcc dot gnu.org
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gabravier at gmail dot com @ 2021-09-03  0:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Gabriel Ravier <gabravier at gmail dot com> ---
This seems to have been fixed in trunk.

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
  2021-02-02  7:58 ` [Bug tree-optimization/98908] " rguenth at gcc dot gnu.org
  2021-09-03  0:15 ` gabravier at gmail dot com
@ 2021-09-03  0:27 ` pinskia at gcc dot gnu.org
  2021-09-03  0:28 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-03  0:27 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |WAITING

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Store merging has been doing this since GCC 8.

  _8 = MEM[(unsigned char *)&x];
  _9 = _8 & 33022;
  MEM[(unsigned char *)&D.2061] = _9;


What I am mssing here?

f:
.LFB0:
        .cfi_startproc
        mov     eax, edi
        and     ax, -32514
        ret

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2021-09-03  0:27 ` pinskia at gcc dot gnu.org
@ 2021-09-03  0:28 ` pinskia at gcc dot gnu.org
  2021-09-03  0:31 ` gabravier at gmail dot com
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-03  0:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The second function is able to be optimized since GCC 9 with bswap producing:
  load_dst_16 = MEM <short unsigned int> [(unsigned char *)&x];

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2021-09-03  0:28 ` pinskia at gcc dot gnu.org
@ 2021-09-03  0:31 ` gabravier at gmail dot com
  2021-09-03  0:41 ` gabravier at gmail dot com
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gabravier at gmail dot com @ 2021-09-03  0:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Gabriel Ravier <gabravier at gmail dot com> ---
It may have been doing it in GCC 8 (and 9, and 10), but it didn't in 11, and
presumably this was also the case in trunk back in February.

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2021-09-03  0:31 ` gabravier at gmail dot com
@ 2021-09-03  0:41 ` gabravier at gmail dot com
  2021-09-03  0:59 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gabravier at gmail dot com @ 2021-09-03  0:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Gabriel Ravier <gabravier at gmail dot com> ---
Also the second example wasn't misoptimized, on the contrary it was the most
reasonable portable function I could write that would work equivalently to the
first *and* that GCC would optimize ideally.

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2021-09-03  0:41 ` gabravier at gmail dot com
@ 2021-09-03  0:59 ` pinskia at gcc dot gnu.org
  2021-09-03  1:09 ` gabravier at gmail dot com
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-03  0:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
   Target Milestone|---                         |9.0
         Resolution|---                         |FIXED

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Gabriel Ravier from comment #6)
> Also the second example wasn't misoptimized, on the contrary it was the most
> reasonable portable function I could write that would work equivalently to
> the first *and* that GCC would optimize ideally.

GCC 7.1.0 produces:
f(reg):
.LFB0:
        .cfi_startproc
        mov     edx, edi
        xor     eax, eax
        mov     ecx, edi
        and     edx, -2
        mov     al, dl
        movzx   edx, ch
        and     edx, -128
        mov     ah, dl
        ret
f1(reg):
.LFB1:
        .cfi_startproc
        and     di, -32514
        xor     eax, eax
        movzx   edx, di
        mov     al, dil
        sar     edx, 8
        mov     ah, dl
        ret

f is your first example and f1 is the second.
As you can see GCC before GCC 8 did neither.
In GCC 8, the second function produces:
  _1 = x.l;
  _2 = (signed short) _1;
  _3 = x.h;
  _4 = (int) _3;
  _5 = _4 << 8;
  _6 = (signed short) _5;
  _7 = _2 | _6;
  _8 = (short unsigned int) _7;
  tmp_14 = _8 & 33022;
  MEM[(unsigned char *)&D.2500] = tmp_14;

And is only opimitized in GCC 9 with bswap producing what I mentioned before

So fixed for GCC 9.

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2021-09-03  0:59 ` pinskia at gcc dot gnu.org
@ 2021-09-03  1:09 ` gabravier at gmail dot com
  2021-09-03  1:11 ` gabravier at gmail dot com
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gabravier at gmail dot com @ 2021-09-03  1:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Gabriel Ravier <gabravier at gmail dot com> ---
Well, fixing a bug filed in 2021 in GCC 9 seems quite hard. Are you confused
about the nature of the bug ? The first example in the description *is* the one
whose optimization the bug is about, and it is the one that didn't optimize
properly in trunk at the time of the bug being filed, back when trunk was GCC
11.0. I'm pretty sure this bug should be considering as fixed for GCC 12
considering it seems to have been fixed somewhere between GCC 11 (which has the
bad code generation) and trunk (which doesn't).

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

* [Bug tree-optimization/98908] Failure to optimize arithmetic involving struct members into operating on the entire struct
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (7 preceding siblings ...)
  2021-09-03  1:09 ` gabravier at gmail dot com
@ 2021-09-03  1:11 ` gabravier at gmail dot com
  2021-09-03  1:31 ` [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3 pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gabravier at gmail dot com @ 2021-09-03  1:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Gabriel Ravier <gabravier at gmail dot com> ---
PS: I had missed at the time that the bug wasn't present in GCC 10/9/8 though,
so perhaps it should be considered as having been a GCC 11 regression that got
fixed in trunk ?

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

* [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (8 preceding siblings ...)
  2021-09-03  1:11 ` gabravier at gmail dot com
@ 2021-09-03  1:31 ` pinskia at gcc dot gnu.org
  2021-09-03  1:38 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-03  1:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Failure to optimize         |[11 Regression] arithmetic
                   |arithmetic involving struct |involving struct members
                   |members into operating on   |into operating on the
                   |the entire struct           |entire struct fails at -O3
   Target Milestone|9.0                         |11.3
             Status|RESOLVED                    |NEW
         Resolution|FIXED                       |---

--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Gabriel Ravier from comment #9)
> PS: I had missed at the time that the bug wasn't present in GCC 10/9/8
> though, so perhaps it should be considered as having been a GCC 11
> regression that got fixed in trunk ?

Oh -O2 is correct but -O3 is not.  I just assumed -O2 because you never said
what option you used.  It is fixed again on the trunk.  So this is still a
regression in GCC 11 series.

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

* [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (9 preceding siblings ...)
  2021-09-03  1:31 ` [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3 pinskia at gcc dot gnu.org
@ 2021-09-03  1:38 ` pinskia at gcc dot gnu.org
  2022-04-21  7:48 ` rguenth at gcc dot gnu.org
  2023-05-29 10:04 ` jakub at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-03  1:38 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |11.2.0
      Known to work|                            |10.3.0, 12.0

--- Comment #11 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
How this was fixed on the trunk was SLP is now able to do:
  _8 = {_1, _3};
  vect__2.4_9 = _8 & { 254, 128 };
Where it only did the store SLP and not the & before.

SLP does it because we know vect-lowering will change it to:
  _7 = VIEW_CONVERT_EXPR<short unsigned int>(_8);
  _6 = _7 & 33022;
  _11 = VIEW_CONVERT_EXPR<vector(2) unsigned char>(_6);

Next time when you file a bug please add what options you are using.

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

* [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (10 preceding siblings ...)
  2021-09-03  1:38 ` pinskia at gcc dot gnu.org
@ 2022-04-21  7:48 ` rguenth at gcc dot gnu.org
  2023-05-29 10:04 ` jakub at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-21  7:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.3                        |11.4

--- Comment #12 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.3 is being released, retargeting bugs to GCC 11.4.

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

* [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3
  2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
                   ` (11 preceding siblings ...)
  2022-04-21  7:48 ` rguenth at gcc dot gnu.org
@ 2023-05-29 10:04 ` jakub at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-29 10:04 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.4                        |11.5

--- Comment #13 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 11.4 is being released, retargeting bugs to GCC 11.5.

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

end of thread, other threads:[~2023-05-29 10:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-01  8:10 [Bug tree-optimization/98908] New: Failure to optimize arithmetic involving struct members into operating on the entire struct gabravier at gmail dot com
2021-02-02  7:58 ` [Bug tree-optimization/98908] " rguenth at gcc dot gnu.org
2021-09-03  0:15 ` gabravier at gmail dot com
2021-09-03  0:27 ` pinskia at gcc dot gnu.org
2021-09-03  0:28 ` pinskia at gcc dot gnu.org
2021-09-03  0:31 ` gabravier at gmail dot com
2021-09-03  0:41 ` gabravier at gmail dot com
2021-09-03  0:59 ` pinskia at gcc dot gnu.org
2021-09-03  1:09 ` gabravier at gmail dot com
2021-09-03  1:11 ` gabravier at gmail dot com
2021-09-03  1:31 ` [Bug tree-optimization/98908] [11 Regression] arithmetic involving struct members into operating on the entire struct fails at -O3 pinskia at gcc dot gnu.org
2021-09-03  1:38 ` pinskia at gcc dot gnu.org
2022-04-21  7:48 ` rguenth at gcc dot gnu.org
2023-05-29 10:04 ` jakub 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).