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.

Reply via email to