https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107050
Bug ID: 107050 Summary: duplicate load of return value when facing multiple branches 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: --- given this code: int g_286 = (-5L); int p; int f = 1; void func_58(); func_31(int c, int d) { if (c) { if (f){ if (d) func_58(); return g_286; } g_286 = 0; } return 0; } func_58() { int arr[30]; p = arr[0]; } when compiled with gcc-12.1.0 (-O1), it will generate: 0000000000401186 <func_58>: 401186: 48 83 ec 10 sub $0x10,%rsp 40118a: 8b 44 24 88 mov -0x78(%rsp),%eax 40118e: 89 05 f4 8c 00 00 mov %eax,0x8cf4(%rip) # 409e88 <p> 401194: 48 83 c4 10 add $0x10,%rsp 401198: c3 retq 0000000000401199 <func_31>: 401199: 89 f8 mov %edi,%eax 40119b: 85 ff test %edi,%edi 40119d: 74 1f je 4011be <func_31+0x25> 40119f: 8b 05 bb 2e 00 00 mov 0x2ebb(%rip),%eax # 404060 <f> 4011a5: 85 c0 test %eax,%eax 4011a7: 75 0b jne 4011b4 <func_31+0x1b> 4011a9: c7 05 b1 2e 00 00 00 movl $0x0,0x2eb1(%rip) # 404064 <g_286> 4011b0: 00 00 00 4011b3: c3 retq 4011b4: 8b 05 aa 2e 00 00 mov 0x2eaa(%rip),%eax # 404064 <g_286> 4011ba: 85 f6 test %esi,%esi 4011bc: 75 01 jne 4011bf <func_31+0x26> 4011be: c3 retq 4011bf: 48 83 ec 08 sub $0x8,%rsp 4011c3: b8 00 00 00 00 mov $0x0,%eax 4011c8: e8 b9 ff ff ff callq 401186 <func_58> 4011cd: 8b 05 91 2e 00 00 mov 0x2e91(%rip),%eax # 404064 <g_286> 4011d3: 48 83 c4 08 add $0x8,%rsp 4011d7: c3 retq we can see in the func_31, compiler choose to load g_286 before judge whether d != 0, and if it's true, %eax will be used and func_58 is called. Before return, g_286 will be loaded again to %eax