public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/114258] New: repeat store operation when copying a union
@ 2024-03-06 17:50 absoler at smail dot nju.edu.cn
  2024-03-06 18:23 ` [Bug middle-end/114258] 2 stores happen when copying from a const union (array) to an union pinskia at gcc dot gnu.org
  2024-03-07  8:15 ` rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: absoler at smail dot nju.edu.cn @ 2024-03-06 17:50 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114258
           Summary: repeat store operation when copying a union
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: absoler at smail dot nju.edu.cn
  Target Milestone: ---

Hi, here's the code and generated binary compiled with gcc-13.2.0 -O2:

```
// https://godbolt.org/z/jsMhcbKnh

union U6 {
   int  f0;
   long long  f1;
   int  f2;
};

union U8 {
   int  f0;
   const long long  f1;
   const long long  f2;
};
union U6 g_13 = {.f0 = 0xE13F5E3DL};
union U8 g_189 = {.f0 = 0x959411BBL};

int d = 0;
int a;
void func_1() {
  const union U6 b[] = {{.f0 = 2}, {.f0 = 2}};
  for (; d;)
    a = 0;
  g_189.f0 &= (g_13 = b[1], 5);
}
```

```
func_1:
 mov    0x2f02(%rip),%eax        # 404028 <d>
 test   %eax,%eax
 je     401130 <func_1+0x10>
 jmp    40112a <func_1+0xa>
 nopl   0x0(%rax)
 movq   $0x0,0x2edd(%rip)        # 404018 <g_13>
 andl   $0x5,0x2ece(%rip)        # 404010 <g_189>
 movl   $0x2,0x2ecc(%rip)        # 404018 <g_13>
 ret
```

we can see that the first write to `g_13` could be removed to improve
performance.

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

* [Bug middle-end/114258] 2 stores happen when copying from a const union (array) to an union
  2024-03-06 17:50 [Bug rtl-optimization/114258] New: repeat store operation when copying a union absoler at smail dot nju.edu.cn
@ 2024-03-06 18:23 ` pinskia at gcc dot gnu.org
  2024-03-07  8:15 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-06 18:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-03-06
          Component|rtl-optimization            |middle-end
           Severity|normal                      |enhancement
            Summary|repeat store operation when |2 stores happen when
                   |copying a union             |copying from a const union
                   |                            |(array) to an union
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase:
```
union U6 {
   short  f0;
   int f1;
};
union U6 g_13;
void func_1() {
  static const union U6 b[] = {{.f0 = 2}, {.f0 = 2}};
  g_13 = b[1];
}
```

It comes from the expansion of the assignment.


For aarch64 we have:
```
;; g_13 = b[1];

(insn 5 4 6 (set (reg:DI 101)
        (high:DI (symbol_ref:DI ("*.LANCHOR0") [flags 0x182])))
"/app/example.cpp":9:8 -1
     (nil))

(insn 6 5 7 (set (reg/f:DI 100)
        (lo_sum:DI (reg:DI 101)
            (symbol_ref:DI ("*.LANCHOR0") [flags 0x182])))
"/app/example.cpp":9:8 -1
     (expr_list:REG_EQUAL (symbol_ref:DI ("*.LANCHOR0") [flags 0x182])
        (nil)))

(insn 7 6 8 (set (mem/c:SI (reg/f:DI 100) [1 g_13D.4435+0 S4 A64])
        (const_int 0 [0])) "/app/example.cpp":9:8 -1
     (nil))

(insn 8 7 9 (set (reg:HI 102)
        (const_int 2 [0x2])) "/app/example.cpp":9:8 -1
     (nil))

(insn 9 8 10 (set (mem/c:HI (reg/f:DI 100) [2 g_13D.4435+0 S2 A64])
        (reg:HI 102)) "/app/example.cpp":9:8 -1
     (nil))

(insn 10 9 11 (set (mem/c:SI (reg/f:DI 100) [1 g_13D.4435+0 S4 A64])
        (const_int 0 [0])) "/app/example.cpp":9:8 -1
     (nil))

(insn 11 10 12 (set (reg:HI 103)
        (const_int 2 [0x2])) "/app/example.cpp":9:8 -1
     (nil))

(insn 12 11 0 (set (mem/c:HI (reg/f:DI 100) [2 g_13D.4435+0 S2 A64])
        (reg:HI 103)) "/app/example.cpp":9:8 -1
     (nil))
```

GCC could do better here in the expansion.

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

* [Bug middle-end/114258] 2 stores happen when copying from a const union (array) to an union
  2024-03-06 17:50 [Bug rtl-optimization/114258] New: repeat store operation when copying a union absoler at smail dot nju.edu.cn
  2024-03-06 18:23 ` [Bug middle-end/114258] 2 stores happen when copying from a const union (array) to an union pinskia at gcc dot gnu.org
@ 2024-03-07  8:15 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-03-07  8:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Huh, very strange RTL we emit for the union assignment.

void func_1(union U6 *a) {
  g_13 = *a;
}

works OK though.

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

end of thread, other threads:[~2024-03-07  8:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 17:50 [Bug rtl-optimization/114258] New: repeat store operation when copying a union absoler at smail dot nju.edu.cn
2024-03-06 18:23 ` [Bug middle-end/114258] 2 stores happen when copying from a const union (array) to an union pinskia at gcc dot gnu.org
2024-03-07  8:15 ` rguenth 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).