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

            Bug ID: 127306
           Summary: RISC-V bclr_lowest_set_bit pass produces wrong code
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: uwu at icenowy dot me
  Target Milestone: ---

Created attachment 65547
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65547&action=edit
A minimal reproducer

With Zbb/Zbs extensions enabled and an optimization level higher than
(including) O1, GCC 16 produces wrong code for the program attached. (0xffff
should be the expected result)

```
icenowy@edelgard [ tmp ] $ /opt/or-cross/a23/bin/riscv64-openruyi-linux-gcc
gcc-riscv-bclr-miscompile.c -O2 -march=rv64gc
icenowy@edelgard [ tmp ] $ qemu-riscv64-static -L /opt/or-cross/a23/sysroot
./a.out 
0xffff
icenowy@edelgard [ tmp ] $ /opt/or-cross/a23/bin/riscv64-openruyi-linux-gcc
gcc-riscv-bclr-miscompile.c -O2 -march=rv64gc_zbb_zbs
icenowy@edelgard [ tmp ] $ qemu-riscv64-static -L /opt/or-cross/a23/sysroot
./a.out 
0x2aab
icenowy@edelgard [ tmp ] ! /opt/or-cross/a23/bin/riscv64-openruyi-linux-gcc
gcc-riscv-bclr-miscompile.c -O0 -march=rv64gc_zbb_zbs
icenowy@edelgard [ tmp ] $ qemu-riscv64-static -L /opt/or-cross/a23/sysroot
./a.out 
0xffff
icenowy@edelgard [ tmp ] $ gcc gcc-riscv-bclr-miscompile.c
icenowy@edelgard [ tmp ] $ ./a.out 
0xffff
```

Investigation shows that disabling the `bclr_lowest_set_bit` pass will fix this
issue, which indicates that this pass is faulty.

Reply via email to