public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/113985] New: redundant copy of return values at O0
@ 2024-02-19  3:00 absoler at smail dot nju.edu.cn
  2024-02-19  3:01 ` [Bug rtl-optimization/113985] " xry111 at gcc dot gnu.org
  2024-02-19  3:15 ` [Bug middle-end/113985] expansion of `*struct = call();` could be improved pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: absoler at smail dot nju.edu.cn @ 2024-02-19  3:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113985
           Summary: redundant copy of return values at O0
           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, I found with -O0 flag, there are redundant operations on return values of
structure type.

```
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int int16_t;
typedef unsigned short int uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed long int int64_t;
typedef unsigned long int uint64_t;

struct S0 {
   int32_t  f0;
   uint8_t  f1;
   int32_t  f2;
};

struct S0 g_91 = {0x6732C952L,0x6BL,6L};
int64_t g_205[9] =
{0L,0L,0x47DC22DBA5B9078ELL,0L,0L,0x47DC22DBA5B9078ELL,0L,0L,0x47DC22DBA5B9078ELL};

void  func_1(void);
struct S0  func_2();


void func_1() {
  struct S0 a;
  struct S0 *b = &g_91;
  *b = func_2(0, a, a.f1, g_205[0]);
}
struct S0 func_2() { return g_91;}
```

the disassembly:
```
0000000000401186 <func_1>:
func_1():
/root/loadtest0/test/output2.c:44
  401186:       push   %rbp
  401187:       mov    %rsp,%rbp
  40118a:       sub    $0x20,%rsp
/root/loadtest0/test/output2.c:46
  40118e:       movq   $0x404080,-0x8(%rbp)
/root/loadtest0/test/output2.c:47
  401196:       mov    0x2f03(%rip),%rsi        # 4040a0 <g_205>
  40119d:       movzbl -0x10(%rbp),%eax
  4011a1:       movzbl %al,%ecx
  4011a4:       mov    -0x14(%rbp),%rdx
  4011a8:       mov    -0xc(%rbp),%eax
  4011ab:       mov    %rsi,%r8
  4011ae:       mov    %rdx,%rsi
  4011b1:       mov    %eax,%edx
  4011b3:       mov    $0x0,%edi
  4011b8:       mov    $0x0,%eax
  4011bd:       callq  4011d7 <func_2>
  4011c2:       mov    -0x8(%rbp),%rcx
  4011c6:       mov    %rax,(%rcx)
  4011c9:       mov    0x8(%rcx),%eax // redundant
  4011cc:       and    $0x0,%eax
  4011cf:       or     %edx,%eax
  4011d1:       mov    %eax,0x8(%rcx)
/root/loadtest0/test/output2.c:48
  4011d4:       nop
  4011d5:       leaveq 
  4011d6:       retq   
```

we can see that it's unnecessary to load `0x8(%rcx)` into %eax, because it will
be cleared in the next instruction

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

* [Bug rtl-optimization/113985] redundant copy of return values at O0
  2024-02-19  3:00 [Bug rtl-optimization/113985] New: redundant copy of return values at O0 absoler at smail dot nju.edu.cn
@ 2024-02-19  3:01 ` xry111 at gcc dot gnu.org
  2024-02-19  3:15 ` [Bug middle-end/113985] expansion of `*struct = call();` could be improved pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-02-19  3:01 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
It does not make sense to disable the optimization and complain the code is not
optimized.

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

* [Bug middle-end/113985] expansion of `*struct = call();` could be improved
  2024-02-19  3:00 [Bug rtl-optimization/113985] New: redundant copy of return values at O0 absoler at smail dot nju.edu.cn
  2024-02-19  3:01 ` [Bug rtl-optimization/113985] " xry111 at gcc dot gnu.org
@ 2024-02-19  3:15 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-19  3:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |NEW
           Keywords|                            |internal-improvement
     Ever confirmed|0                           |1
          Component|rtl-optimization            |middle-end
           Severity|normal                      |enhancement
            Summary|redundant copy of return    |expansion of `*struct =
                   |values at O0                |call();` could be improved
         Resolution|INVALID                     |---
   Last reconfirmed|                            |2024-02-19

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Just for reference this comes from the expansion of *b = call.

What is happening is it is loading the original variable from *b and then
ioring the new value in. This should be improved but not because of the -O0
case but rather it would be better for expansion reasons and better initial
RTL.

Not we normally don't care about -O0 code size/generation but in this case, it
just that it would help improve compile time slightly.

I have not looked fully into what produces the tmp = (a&0) just yet though.
But the initial RTL (even at -O2) is:
```
(insn 28 27 29 (set (reg:SI 112)
        (mem/c:SI (reg/f:DI 110) [1 g_91D.2780+8 S4 A64]))
"/app/example.cpp":27:8 -1
     (nil))

(insn 29 28 30 (parallel [
            (set (reg:SI 113)
                (and:SI (reg:SI 112)
                    (const_int 0 [0])))
            (clobber (reg:CC 17 flags))
        ]) "/app/example.cpp":27:8 -1
     (nil))

(insn 30 29 31 (parallel [
            (set (reg:SI 114)
                (ior:SI (reg:SI 113)
                    (reg:SI 111)))
            (clobber (reg:CC 17 flags))
        ]) "/app/example.cpp":27:8 -1
     (nil))

```

Which seems like it definitely could be improved.

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

end of thread, other threads:[~2024-02-19  3:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-19  3:00 [Bug rtl-optimization/113985] New: redundant copy of return values at O0 absoler at smail dot nju.edu.cn
2024-02-19  3:01 ` [Bug rtl-optimization/113985] " xry111 at gcc dot gnu.org
2024-02-19  3:15 ` [Bug middle-end/113985] expansion of `*struct = call();` could be improved 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).