From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E39773858CDA; Tue, 27 Sep 2022 08:53:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E39773858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664268821; bh=9J00qWl35WJH1mhzSjjZrPleXrduCgK1xOFavDAnfmI=; h=From:To:Subject:Date:From; b=W2MmlXWXcNrj6V2HERgOkpEuLnxiuc1MQMglXU9P8PkJPYrTjdOQARYhEaLIKRR/H dQ56BaFbiyCBDxLlYy3MQn+fJYgSyreBggHpk5pC/C3yQn9tiom4X7q/+3irNU0lC+ GtSwuQmuJYUlkWv6gje01j3jVsyQ/R4wrqlwsOuw= From: "absoler at smail dot nju.edu.cn" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/107050] New: duplicate load of return value when facing multiple branches Date: Tue, 27 Sep 2022 08:53:41 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: absoler at smail dot nju.edu.cn X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107050 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 =3D (-5L); int p; int f =3D 1; void func_58(); func_31(int c, int d) { if (c) { if (f){ if (d) func_58(); return g_286; } g_286 =3D 0; } return 0; } func_58() { int arr[30]; p =3D arr[0]; } when compiled with gcc-12.1.0 (-O1), it will generate: 0000000000401186 : 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

401194: 48 83 c4 10 add $0x10,%rsp 401198: c3 retq=20=20=20 0000000000401199 : 401199: 89 f8 mov %edi,%eax 40119b: 85 ff test %edi,%edi 40119d: 74 1f je 4011be 40119f: 8b 05 bb 2e 00 00 mov 0x2ebb(%rip),%eax # 404060 4011a5: 85 c0 test %eax,%eax 4011a7: 75 0b jne 4011b4 4011a9: c7 05 b1 2e 00 00 00 movl $0x0,0x2eb1(%rip) # 404064 4011b0: 00 00 00=20 4011b3: c3 retq=20=20=20 4011b4: 8b 05 aa 2e 00 00 mov 0x2eaa(%rip),%eax # 404064 4011ba: 85 f6 test %esi,%esi 4011bc: 75 01 jne 4011bf 4011be: c3 retq=20=20=20 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 4011cd: 8b 05 91 2e 00 00 mov 0x2e91(%rip),%eax # 404064 4011d3: 48 83 c4 08 add $0x8,%rsp 4011d7: c3 retq=20 we can see in the func_31, compiler choose to load g_286 before judge wheth= er d !=3D 0, and if it's true, %eax will be used and func_58 is called. Before r= eturn, g_286 will be loaded again to %eax=