https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107051
Bug ID: 107051 Summary: redundant loads when copying a union Product: gcc Version: 12.1.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: --- for this code: union U2 { unsigned f0; char * f1; }; union U2 g_284[2] = {{0UL},{0xC2488F72L}}; int e; void func_1() { union U2 c = {7}; int32_t *d[2]; for (; e;) *d[1] = 0; g_284[0] = c = g_284[1]; } compile it with gcc-12.1.0 -O1, and generate: 0000000000401186 <func_1>: 401186: 83 3d fb 8c 00 00 00 cmpl $0x0,0x8cfb(%rip) # 409e88 <e> 40118d: 74 02 je 401191 <func_1+0xb> 40118f: eb fe jmp 40118f <func_1+0x9> 401191: 8b 15 d1 2e 00 00 mov 0x2ed1(%rip),%edx # 404068 <g_284+0x8> 401197: 48 b8 00 00 00 00 ff movabs $0xffffffff00000000,%rax 40119e: ff ff ff 4011a1: 48 23 05 c0 2e 00 00 and 0x2ec0(%rip),%rax # 404068 <g_284+0x8> 4011a8: 48 09 d0 or %rdx,%rax 4011ab: 48 89 05 ae 2e 00 00 mov %rax,0x2eae(%rip) # 404060 <g_284> 4011b2: c3 retq I don't understand why clearing the low 4 bytes of g_284[1].f1 and then or it with g_284[1].f0, because it should be equal? and for the next example, we can see the both fields of g_303 have been loaded and written to g: union U0 { short f0; int f3; }; union U0 g_303 = {0x9B86L}; union U0 g; int a,b; void func_1() { union U0 d[1] = {1}; for (; a;) for (; b;) ; g = d[0] = g_303; } under gcc-12.1.0 -O1: 0000000000401186 <func_1>: 401186: 83 3d ff 8c 00 00 00 cmpl $0x0,0x8cff(%rip) # 409e8c <a> 40118d: 74 02 je 401191 <func_1+0xb> 40118f: eb fe jmp 40118f <func_1+0x9> 401191: 8b 05 c9 2e 00 00 mov 0x2ec9(%rip),%eax # 404060 <g_303> 401197: 66 8b 05 c2 2e 00 00 mov 0x2ec2(%rip),%ax # 404060 <g_303> 40119e: 89 05 ec 8c 00 00 mov %eax,0x8cec(%rip) # 409e90 <g> 4011a4: c3 retq