public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "brian.grayson at sifive dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/99067] New: Missed optimization for induction variable elimination
Date: Wed, 10 Feb 2021 23:49:16 +0000	[thread overview]
Message-ID: <bug-99067-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 99067
           Summary: Missed optimization for induction variable elimination
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: brian.grayson at sifive dot com
  Target Milestone: ---

For RISC-V, this code snippet could eliminate the induction variable for the
loop ending condition.

#include <stdint.h>
int16_t a[1000];
int64_t N = 100;
int64_t found_zero() {
  for (int i = 0; i <= N; i++) {
    if (a[i] == 0) return 1;
  }
  return 0;
}

gcc -O3 for RISC-V generates:

.L8:
  addi  a5,a5,2
  blt a2,a4,.L4
.L3:
  lh  a3,0(a5)
  addi  a4,a4,1  <-- induction variable update that can be eliminated
  bne a3,zero,.L8
  li  a0,1
  ret
.L4:
  li  a0,0
  ret

Is there a reason it doesn't do this transform (written at the C level) to do
pointer comparisons:

  for (int16_t* p = &a[0]; p <= &a[N]; p++) { ... }

That C code is able to remove the extra add instruction:

.L15:
  bgtu  a5,a3,.L12
.L11:
  lh  a4,0(a5)
  addi  a5,a5,2
  bne a4,zero,.L15
  li  a0,1
  ret
.L12:
  li  a0,0
  ret

I verified the same issue occurs in PowerPC and ARM code-gen, so this isn't
target-specific.

             reply	other threads:[~2021-02-10 23:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-10 23:49 brian.grayson at sifive dot com [this message]
2021-02-11  0:48 ` [Bug rtl-optimization/99067] " wilson at gcc dot gnu.org
2021-02-16 15:23 ` [Bug tree-optimization/99067] " amker at gcc dot gnu.org
2021-02-18  2:38 ` amker 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-99067-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).