public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bina2374 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/95685] New: Loop invariants can't be moved out of the loop
Date: Mon, 15 Jun 2020 15:47:01 +0000	[thread overview]
Message-ID: <bug-95685-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 95685
           Summary: Loop invariants can't be moved out of the loop
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bina2374 at gmail dot com
                CC: kito at gcc dot gnu.org, wilson at gcc dot gnu.org
  Target Milestone: ---
            Target: riscv32-unknown-elf

Command line: bin/riscv64-unknown-elf-gcc -march=rv32imafc -mabi=ilp32f -O2
-funroll-loops bar.c -S

==========
 C Source
==========
unsigned short bar(unsigned char data, unsigned short crc) {
  unsigned char i = 0;

  for (i = 0; i < 3; i++) {
    data >>= 1;
    if ((data & 1) == 1)
      crc ^= 0x2001;
  }
  return crc;
}

=========
 GCC asm
=========
bar:
        srli    a4,a0,1
        andi    t0,a4,1
        mv      a5,a0
        mv      a0,a1
        beq     t0,zero,.L2
        li      t1,8192     #
        addi    t2,t1,1     # 0x2001
        xor     a0,a1,t2
.L2:
        srli    a1,a5,2
        andi    a2,a1,1
        beq     a2,zero,.L3
        li      a3,8192     #
        addi    a6,a3,1     # 0x2001
        xor     a0,a0,a6
.L3:
        srli    a7,a5,3
        andi    t3,a7,1
        beq     t3,zero,.L4
        li      t4,8192     #
        addi    t5,t4,1     # 0x2001
        xor     a0,a0,t5
.L4:
        ret

If I compile it without -funroll-loops, loop invariant code motion works:
bar:
        li      a2,8192     #
        mv      a4,a0
        li      a5,3
        mv      a0,a1
        addi    a2,a2,1     # 0x2001
.L3:
        srli    a4,a4,1
        addi    a5,a5,-1
        andi    a3,a4,1
        andi    a5,a5,0xff
        beq     a3,zero,.L2
        xor     a0,a0,a2
.L2:
        bne     a5,zero,.L3
        ret

I guess the problem is the order of optimization passes.
Because cunroll pass makes the loop no longer a loop, loop2_invariant can't
work on it.

=======================
 Ideal Code Generation 
=======================
bar:
        srli    a4,a0,1
        andi    t0,a4,1
        mv      a5,a0
        mv      a0,a1
        li      t1,8192     #
        addi    t2,t1,1     # t2 = 0x2001
        beq     t0,zero,.L2
        xor     a0,a1,t2
.L2:
        srli    a1,a5,2
        andi    a2,a1,1
        beq     a2,zero,.L3
        xor     a0,a0,t2    # Use t2
.L3:
        srli    a7,a5,3
        andi    t3,a7,1
        beq     t3,zero,.L4
        xor     a0,a0,t2    # Use t2
.L4:
        ret

             reply	other threads:[~2020-06-15 15:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-15 15:47 bina2374 at gmail dot com [this message]
2020-06-16  0:31 ` [Bug tree-optimization/95685] " wilson at gcc dot gnu.org
2020-06-16  6:36 ` rguenth at gcc dot gnu.org
2020-06-16  6:37 ` [Bug rtl-optimization/95685] " rguenth at gcc dot gnu.org
2021-05-30 22:31 ` pinskia 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-95685-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).