public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114030] New: redundant load of inline function's arguments
@ 2024-02-21 12:54 absoler at smail dot nju.edu.cn
  2024-02-21 23:22 ` [Bug tree-optimization/114030] redundant load due to unions and different size loads pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: absoler at smail dot nju.edu.cn @ 2024-02-21 12:54 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114030
           Summary: redundant load of inline function's arguments
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: absoler at smail dot nju.edu.cn
  Target Milestone: ---

hi, here's the code. Redundant load will be introduced compiled with gcc-13.2.0
-O3.
```
// https://godbolt.org/z/3oGWq6anq

union U0 {
   int  f0;
   long long  f1;
   int  f2;
   const unsigned char  f3;
   char  f4;
};

union U2 {
   char  f0;
   unsigned short  f1;
   unsigned short  f2;
};

/* --- GLOBAL VARIABLES --- */
int g_3 = 0xD86E52D8L;
union U0 g_34 = {-1L};
union U2 g_35 = {0L};
int g_49 = 0xDC590CB2L;
int g_54[6][5] =
{{0L,0L,0L,0L,0L},{0x6833E1ABL,(-2L),0x6833E1ABL,(-2L),0x6833E1ABL},{0L,0L,0L,0L,0L},{0x6833E1ABL,(-2L),0x6833E1ABL,(-2L),0x6833E1ABL},{0L,0L,0L,0L,0L},{0x6833E1ABL,(-2L),0x6833E1ABL,(-2L),0x6833E1ABL}};
union U2 g_81 = {0L};

/* --- FORWARD DECLARATIONS --- */
static char(safe_rshift_func_int8_t_s_s)(char left, int right) {  return     
((left < 0) || (((int)right) < 0) || (((int)right) >= 32))          ? ((left)) 
        :          (left >> ((int)right));}
static unsigned char(safe_lshift_func_uint8_t_u_u)(unsigned char left, unsigned
int right) {  return      ((((unsigned int)right) >= 32) ||       (left >
((255) >> ((unsigned int)right))))          ? ((left))          :         
(left << ((unsigned int)right));}
static char(safe_lshift_func_int8_t_s_s)(char left, int right) {  return     
((left < 0) || (((int)right) < 0) || (((int)right) >= 32) ||       (left >
((127) >> ((int)right))))          ? ((left))          :          (left <<
((int)right));}
void func_1(void);
void func_31(union U0  p_32, union U2  p_33);
int  func_42(union U0  p_43, int * p_44);


void func_1() {
  func_31(g_34, g_35);
}

void func_31(union U0 g, union U2 h) {
  int *i = &g_3;
  g_34.f2 = 1;
  *i = func_42((safe_rshift_func_int8_t_s_s(0, g.f4), g), &g_49);
}
int func_42(union U0 j, int *k) {
  unsigned l_51 = 4294967295UL;
  int *l = &g_3;
  *l = g_54[4][0] |=
     
~safe_lshift_func_uint8_t_u_u(safe_lshift_func_int8_t_s_s((0x7BD129AC07F4C733LL
^ l_51), 6), j.f3);
  return j.f0;
}
```

compiled binary is:

```
00000000004015b0 <func_1>:
func_1():
/root/myCSmith/test/output2.c:57
  4015b0:       movzbl 0x2b49(%rip),%ecx        # 404100 <g_34>  # not
necessary
  4015b7:       mov    0x2b43(%rip),%edx        # 404100 <g_34>
safe_lshift_func_uint8_t_u_u():
/root/myCSmith/test/output2.c:62
  4015bd:       mov    $0xffffff33,%eax
func_31():
/root/myCSmith/test/output2.c:62
  4015c2:       movl   $0x1,0x2b34(%rip)        # 404100 <g_34>
safe_lshift_func_uint8_t_u_u():
/root/myCSmith/test/output2.c:49
  4015cc:       cmp    $0x1f,%cl
  4015cf:       ja     4015ec <func_1+0x3c>
/root/myCSmith/test/output2.c:49 (discriminator 1)
  4015d1:       mov    $0xff,%esi
  4015d6:       sar    %cl,%esi
  4015d8:       cmp    $0xcb,%esi
  4015de:       jle    4015ec <func_1+0x3c>
/root/myCSmith/test/output2.c:49 (discriminator 3)
  4015e0:       mov    $0xcc,%eax
  4015e5:       shl    %cl,%eax
func_42():
/root/myCSmith/test/output2.c:69 (discriminator 2)
  4015e7:       movzbl %al,%eax
  4015ea:       not    %eax
/root/myCSmith/test/output2.c:68
  4015ec:       or     %eax,0x2ade(%rip)        # 4040d0 <g_54+0x50>
func_31():
/root/myCSmith/test/output2.c:63 (discriminator 2)
  4015f2:       mov    %edx,0x2b10(%rip)        # 404108 <g_3>
func_1():
/root/myCSmith/test/output2.c:58
  4015f8:       retq   
  4015f9:       nopl   0x0(%rax)
```

the load at address 0x4015b0 is redundant.

This behavior is regression with gcc-13.2.0 and a necessary flag -O3, and can
be triggered with gcc-8.2.0 with -O2 flag.

if compiled with gcc-13.2.0 -O2, there won't be such a load operation.

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

* [Bug tree-optimization/114030] redundant load due to unions and different size loads
  2024-02-21 12:54 [Bug tree-optimization/114030] New: redundant load of inline function's arguments absoler at smail dot nju.edu.cn
@ 2024-02-21 23:22 ` pinskia at gcc dot gnu.org
  2024-02-22 10:41 ` [Bug middle-end/114030] " rguenth at gcc dot gnu.org
  2024-03-31  3:08 ` absoler at smail dot nju.edu.cn
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-21 23:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
            Summary|redundant load of inline    |redundant load due to
                   |function's arguments        |unions and different size
                   |                            |loads
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-02-21
           Severity|normal                      |enhancement

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase:
```
union U0 {
   int  f2;
   char  f4;
};

int g_3;
union U0 g_34 = {-1L};
char func_1() {
  int t11 = g_34.f2;
  char t1 = g_34.f4;
  g_3 = t11;
  return t1;
}
```

This is just due to unions. I am not sure if this is important to optimize
really since techincally the original code is undefined by the C/C++ standard
(though GCC does an extension which makes it defined). Unions used in this way
is not used much either.

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

* [Bug middle-end/114030] redundant load due to unions and different size loads
  2024-02-21 12:54 [Bug tree-optimization/114030] New: redundant load of inline function's arguments absoler at smail dot nju.edu.cn
  2024-02-21 23:22 ` [Bug tree-optimization/114030] redundant load due to unions and different size loads pinskia at gcc dot gnu.org
@ 2024-02-22 10:41 ` rguenth at gcc dot gnu.org
  2024-03-31  3:08 ` absoler at smail dot nju.edu.cn
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-02-22 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|tree-optimization           |middle-end

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
FRE only tries to optimize same sized "punned" loads, it doesn't try to handle
truncation.  At -O2 it looks like some missed SRA (some inlining only happens
at IPA time with -O2).

In the end it's maybe better suited for RTL optimization.

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

* [Bug middle-end/114030] redundant load due to unions and different size loads
  2024-02-21 12:54 [Bug tree-optimization/114030] New: redundant load of inline function's arguments absoler at smail dot nju.edu.cn
  2024-02-21 23:22 ` [Bug tree-optimization/114030] redundant load due to unions and different size loads pinskia at gcc dot gnu.org
  2024-02-22 10:41 ` [Bug middle-end/114030] " rguenth at gcc dot gnu.org
@ 2024-03-31  3:08 ` absoler at smail dot nju.edu.cn
  2 siblings, 0 replies; 4+ messages in thread
From: absoler at smail dot nju.edu.cn @ 2024-03-31  3:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from absoler at smail dot nju.edu.cn ---
(In reply to Andrew Pinski from comment #1)
> Reduced testcase:
> ```
> union U0 {
>    int  f2;
>    char  f4;
> };
> 
> int g_3;
> union U0 g_34 = {-1L};
> char func_1() {
>   int t11 = g_34.f2;
>   char t1 = g_34.f4;
>   g_3 = t11;
>   return t1;
> }
> ```
> 
> This is just due to unions. I am not sure if this is important to optimize
> really since techincally the original code is undefined by the C/C++
> standard (though GCC does an extension which makes it defined). Unions used
> in this way is not used much either.

reading different field of a union seems defined in C11 standard (maybe not in
C++11).

"If the member used to read the contents of a union object is not the same as
the member last used to store a value in the object, the appropriate part of
the object representation of the value is reinterpreted as an object
representation in the new type as described in 6.2.6 (a process sometimes
called ‘type punning’). This might be a trap representation."

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

end of thread, other threads:[~2024-03-31  3:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-21 12:54 [Bug tree-optimization/114030] New: redundant load of inline function's arguments absoler at smail dot nju.edu.cn
2024-02-21 23:22 ` [Bug tree-optimization/114030] redundant load due to unions and different size loads pinskia at gcc dot gnu.org
2024-02-22 10:41 ` [Bug middle-end/114030] " rguenth at gcc dot gnu.org
2024-03-31  3:08 ` absoler at smail dot nju.edu.cn

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).