From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2BFB83858D28; Fri, 10 Feb 2023 10:25:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2BFB83858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676024705; bh=8gZUCEoucHuQ7Z2zdMb1dkFhbSkrjhTaOy0pYpUKgEc=; h=From:To:Subject:Date:From; b=FUJaxQbUc4fhftppHQZa/ruSvhb35VKyoiKrGVEP+TfAyE8fS2t8xxiv0SfwLXgPz jM1e4gPVF7GQ9v8s89c7mluC3aZxGY3pcRTt62lRjtW4xwIM3DUnL3uCQyKK/QCMEK l228JyiPPzqy9rIrBAcEQRCXWaTWzv98ZgRYYv0Q= From: "theodort at inf dot ethz.ch" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/108751] New: Removing dead code results in worse optimization at -Os Date: Fri, 10 Feb 2023 10:25:04 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: theodort at inf dot ethz.ch 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=3D108751 Bug ID: 108751 Summary: Removing dead code results in worse optimization at -Os Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: theodort at inf dot ethz.ch Target Milestone: --- I found this case where slight changes in the program that, in theory, shou= ld not affect the output (or affect it trivially) cause the compiler to genera= te worse code:=20 static int a =3D 0; static int b =3D 1; int main() { char c =3D 0; for (;;) { if (c) break; for (; a; a++) { // a is 0, this loop is dead if (b) // this is always true continue; else return 2; // this program will never return 2 } c =3D 10; } return 3; } compiled with gcc-trunk -Os:=20 main: .L2: movl a(%rip), %eax testl %eax, %eax je .L6 incl %eax movl %eax, a(%rip) jmp .L2 .L6: movl $3, %eax ret Clearly, the compiler has figured out that "return 2;" will never be execut= ed. But if I remove it from the source: static int a =3D 0; static int b =3D 1; int main() { char c =3D 0; for (;;) { if (c) break; for (; a; a++) { if (b) continue; //else // return 2; } c =3D 10; } return 3; } and compile with gcc-trunk -Os again: main: movl a(%rip), %eax xorl %edx, %edx .L2: testl %eax, %eax jne .L4 testb %dl, %dl je .L7 xorl %eax, %eax movl %eax, a(%rip) jmp .L7 .L4: incl %eax movb $1, %dl jmp .L2 .L7: movl $3, %eax ret the generated code is worse.=20 The same thing happens if the return value is changed: static int a =3D 0; static int b =3D 1; int main() { char c =3D 0; for (;;) { if (c) break; for (; a; a++) { if (b) continue; else return 2; } c =3D 10; } return 1; // changed from return 3 } gcc-trunk -Os:=20 main: movl a(%rip), %eax xorl %edx, %edx .L2: testl %eax, %eax jne .L4 testb %dl, %dl je .L7 xorl %eax, %eax movl %eax, a(%rip) jmp .L7 .L4: incl %eax movb $1, %dl jmp .L2 .L7: movl $1, %eax ret and if we constant propagate b: static int a =3D 0; int main() { char c =3D 0; for (;;) { if (c) break; for (; a; a++) { if (1) // this was if (b) before continue; else return 2; } c =3D 10; } return 1; } gcc-trunk -Os: main: movl a(%rip), %eax xorl %edx, %edx .L2: testl %eax, %eax jne .L12 testb %dl, %dl je .L7 xorl %eax, %eax movl %eax, a(%rip) jmp .L7 .L12: incl %eax movb $1, %dl jmp .L2 .L7: movl $1, %eax ret=