https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113951
Bug ID: 113951 Summary: regression redundant load Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: absoler at smail dot nju.edu.cn Target Milestone: --- Hi, here's the code: ``` 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 S1 { uint64_t f0; uint64_t f1; uint32_t f2; }; /* --- GLOBAL VARIABLES --- */ int32_t g_5 = 0xB779C63CL; struct S1 g_441 = {0x387DADCAC0CA1ED2LL,18446744073709551606UL,4294967295UL}; uint16_t g_558 = 65534UL; int32_t *g_560 = &g_5; struct S1 *g_677 = &g_441; uint32_t g_740 = 0UL; struct S1 g_752 = {0UL,0xD68240D217006E9ALL,4294967295UL}; int f = 1; int c; void func_1() { int32_t b[1]; if (g_5) { int32_t **d = &g_560; if (g_558) { *d = g_560; **d = 0; } else { int32_t e = (int)b; g_677 = &g_441; } } else { uint32_t f[5]; b[0] = (int)f; } if (f) for (; c; c++) g_740--; *g_677 = g_752; } ``` with -O2 flag, gcc-11.2.0 and gcc-13.2.0 with an extra option "-fschedule-insns" can both generate the same following binary code: ``` 0000000000401680 <func_1>: func_1(): /root/myCSmith/test/output2.c:52 401680: mov 0x2a4e(%rip),%ecx # 4040d4 <g_5> /root/myCSmith/test/output2.c:68 401686: mov 0x29fb(%rip),%rax # 404088 <g_677> /root/myCSmith/test/output2.c:52 40168d: test %ecx,%ecx 40168f: je 4016af <func_1+0x2f> /root/myCSmith/test/output2.c:54 401691: cmpw $0x0,0x29ff(%rip) # 404098 <g_558> 401699: je 4016f0 <func_1+0x70> /root/myCSmith/test/output2.c:56 40169b: mov 0x29ee(%rip),%rdx # 404090 <g_560> /root/myCSmith/test/output2.c:68 4016a2: mov 0x29df(%rip),%rax # 404088 <g_677> /root/myCSmith/test/output2.c:56 4016a9: movl $0x0,(%rdx) /root/myCSmith/test/output2.c:65 4016af: mov 0x29ab(%rip),%edx # 404060 <f> 4016b5: test %edx,%edx 4016b7: je 4016d3 <func_1+0x53> /root/myCSmith/test/output2.c:66 (discriminator 1) 4016b9: mov 0x10549(%rip),%edx # 411c08 <c> 4016bf: test %edx,%edx 4016c1: je 4016d3 <func_1+0x53> 4016c3: add %edx,0x10543(%rip) # 411c0c <g_740> 4016c9: movl $0x0,0x10535(%rip) # 411c08 <c> /root/myCSmith/test/output2.c:68 4016d3: movdqa 0x2995(%rip),%xmm0 # 404070 <g_752> 4016db: movups %xmm0,(%rax) 4016de: mov 0x299b(%rip),%rdx # 404080 <g_752+0x10> 4016e5: mov %rdx,0x10(%rax) /root/myCSmith/test/output2.c:69 4016e9: retq 4016ea: nopw 0x0(%rax,%rax,1) /root/myCSmith/test/output2.c:59 4016f0: movq $0x4040a0,0x298d(%rip) # 404088 <g_677> 4016fb: mov $0x4040a0,%eax 401700: jmp 4016af <func_1+0x2f> 401702: data16 nopw %cs:0x0(%rax,%rax,1) 40170d: nopl (%rax) ``` we can see g_677 has been load repeatedly both at 0x401686 and 0x4016a2 at runtime, it's not necessary