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

            Bug ID: 125765
           Summary: `a ? CST0 : (CST0|CST1)` can be handled better for
                    riscv
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---
            Target: riscv

Take:
```
int src(int c, int b, int a) {
  int t = (a ? 0xff10 : 0xff00);
  return t;
}


int tgt(int c, int b, int a) {
  int t = (a ? 0x30 : 0);
  t |= 0xff00;
  return t;
}
```

Compiling this at -O1 -march=rv64gcb_zicond, you will see tgt produces:
```
        li      a5,48
        czero.eqz       a0,a5,a2
        li      a5,65536
        addi    a5,a5,-256
        or      a0,a0,a5
        ret
```
Which looks to be the best code.
But src produces:
```
        li      a0,65536
        addi    a0,a0,-240
        czero.eqz       a0,a0,a2
        li      a5,65536
        addi    a5,a5,-256
        czero.nez       a2,a5,a2
        add     a0,a2,a0
        ret
```
Which has much worse code.

Note the reason for -O1 rather than -O2 is because pre on the gimple level will
cause tgt to change to src.

I am not sure if this shows up in spec or not but it is always good to improve
this.

Reply via email to