public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "theodort at inf dot ethz.ch" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-108751-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108751

            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, should
not affect the output (or affect it trivially) cause the compiler to generate
worse code: 

static int a = 0;
static int b = 1;
int main() {
  char c = 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 = 10;
  }
  return 3;
}

compiled with gcc-trunk -Os: 

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 executed.
But if I remove it from the source:

static int a = 0;
static int b = 1;
int main() {
  char c = 0;
  for (;;) {
    if (c)
      break;
    for (; a; a++) {
      if (b)
        continue;
      //else
      // return 2;
    }
    c = 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. 

The same thing happens if the return value is changed:

static int a = 0;
static int b = 1;
int main() {
  char c = 0;
  for (;;) {
    if (c)
      break;
    for (; a; a++) {
      if (b)
        continue;
      else
        return 2;
    }
    c = 10;
  }
  return 1; // changed from return 3
}

gcc-trunk -Os: 

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 = 0;
int main() {
  char c = 0;
  for (;;) {
    if (c)
      break;
    for (; a; a++) {
      if (1) // this was if (b) before
        continue;
      else
        return 2;
    }
    c = 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

             reply	other threads:[~2023-02-10 10:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-10 10:25 theodort at inf dot ethz.ch [this message]
2023-02-10 10:29 ` [Bug tree-optimization/108751] Removing dead code results in worse generated target code " theodort at inf dot ethz.ch
2023-02-10 17:03 ` jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108751-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).