public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101326] New: std::optional returns forced through stack
@ 2021-07-05 16:37 tnfchris at gcc dot gnu.org
  2021-07-05 16:45 ` [Bug middle-end/101326] " tnfchris at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: tnfchris at gcc dot gnu.org @ 2021-07-05 16:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101326
           Summary: std::optional returns forced through stack
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tnfchris at gcc dot gnu.org
  Target Milestone: ---

The simple example

#include <optional>

std::optional<long> foo() {
    return 0;
}

shows suboptimal codegen at -O3 -std=c++17.

The values seem to be forced through the stack.  On AArch64 we get

foo():
        sub     sp, sp, #16
        mov     w0, 1
        strb    w0, [sp, 8]
        mov     x0, 0
        ldr     x1, [sp, 8]
        add     sp, sp, 16
        ret

instead of (what clang gives)

foo():                                // @foo()
        mov     w1, #1
        mov     x0, xzr
        ret

On x86 it's the same:

foo():
        mov     QWORD PTR [rsp-24], 0
        mov     rax, QWORD PTR [rsp-24]
        mov     BYTE PTR [rsp-16], 1
        mov     rdx, QWORD PTR [rsp-16]
        ret

vs

foo():                                // @foo()
        mov     w1, #1
        mov     x0, xzr
        ret

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

* [Bug middle-end/101326] std::optional returns forced through stack
  2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
@ 2021-07-05 16:45 ` tnfchris at gcc dot gnu.org
  2021-07-05 17:07 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: tnfchris at gcc dot gnu.org @ 2021-07-05 16:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Tamar Christina <tnfchris at gcc dot gnu.org> ---
last example for x86 should have been

foo():                                # @foo()
        xor     eax, eax
        mov     dl, 1
        ret

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

* [Bug middle-end/101326] std::optional returns forced through stack
  2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
  2021-07-05 16:45 ` [Bug middle-end/101326] " tnfchris at gcc dot gnu.org
@ 2021-07-05 17:07 ` pinskia at gcc dot gnu.org
  2021-07-06  6:38 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-05 17:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-07-05
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Take:
struct g {
  unsigned long b;
};

struct a
{
  struct g g1;
  unsigned char c;
};

static inline void h(struct g *a)
{
  a->b = 0;
}

static inline void j(struct a *a)
{
  h(&a->g1);
  a->c = 1;
}

struct a f(void)
{
  struct a a;
  j(&a);
  return a;
}

---- CUT ----
It works, I suspect in the case of std::optional, std::optional is marked as
BLKmode rather than TImode as the struct is marked as "ADDRESSABLE".

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

* [Bug middle-end/101326] std::optional returns forced through stack
  2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
  2021-07-05 16:45 ` [Bug middle-end/101326] " tnfchris at gcc dot gnu.org
  2021-07-05 17:07 ` pinskia at gcc dot gnu.org
@ 2021-07-06  6:38 ` rguenth at gcc dot gnu.org
  2021-07-06  7:40 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-07-06  6:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think the issue is simply that RTL expansion forces the object to memory
since that is what GIMPLE does:

struct optional foo ()
{
  struct optional D.12374;

  <bb 2> [local count: 1073741824]:
  MEM <long int> [(struct optional *)&D.12374] = 0;
  MEM <unsigned char> [(struct optional *)&D.12374 + 8B] = 1;
  return D.12374;
}

and yes, if it would have had TImode we could expand it to a register pair
but it has BLKmode because it's TYPE_NEEDS_CONSTRUCTING(?), the type
isn't TREE_ADDRESSABLE for me.

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

* [Bug middle-end/101326] std::optional returns forced through stack
  2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-07-06  6:38 ` rguenth at gcc dot gnu.org
@ 2021-07-06  7:40 ` pinskia at gcc dot gnu.org
  2023-04-26  9:46 ` pinskia at gcc dot gnu.org
  2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-06  7:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #3)
> and yes, if it would have had TImode we could expand it to a register pair
> but it has BLKmode because it's TYPE_NEEDS_CONSTRUCTING(?), the type
> isn't TREE_ADDRESSABLE for me.

Yes TYPE_NEEDS_CONSTRUCTING, I had thought they were the same bit but I am
wrong.

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

* [Bug middle-end/101326] std::optional returns forced through stack
  2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-07-06  7:40 ` pinskia at gcc dot gnu.org
@ 2023-04-26  9:46 ` pinskia at gcc dot gnu.org
  2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-26  9:46 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |david at westcontrol dot com

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 109631 has been marked as a duplicate of this bug. ***

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

* [Bug middle-end/101326] std::optional returns forced through stack
  2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-04-26  9:46 ` pinskia at gcc dot gnu.org
@ 2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-12 21:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup

*** This bug has been marked as a duplicate of bug 95405 ***

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

end of thread, other threads:[~2023-07-12 21:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-05 16:37 [Bug c++/101326] New: std::optional returns forced through stack tnfchris at gcc dot gnu.org
2021-07-05 16:45 ` [Bug middle-end/101326] " tnfchris at gcc dot gnu.org
2021-07-05 17:07 ` pinskia at gcc dot gnu.org
2021-07-06  6:38 ` rguenth at gcc dot gnu.org
2021-07-06  7:40 ` pinskia at gcc dot gnu.org
2023-04-26  9:46 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` 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).