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

            Bug ID: 124009
           Summary: Improve select between -1,1
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: law at gcc dot gnu.org
  Target Milestone: ---

int foo(int x, int y) { return (y < x) ? 1 : -1; }

WHen compiled on riscv64 with -O2 -march=rv64gcbv_zicond we get something like
this:

        slt     a1,a1,a0        # 27    [c=4 l=4]  slt_didi3
        li      a5,2            # 28    [c=4 l=4]  *movdi_64bit/1
        czero.eqz       a0,a5,a1        # 29    [c=4 l=4]  *czero.eqz.didi
        addi    a0,a0,-1        # 17    [c=4 l=4]  *adddi3/1

But it seems like slt to generate 0/1, then shifting that left resulting in
0/2, then subtracting 1 to get -1,1 would be a better sequence.  Obviously
there are generalizations that could be done here as well.

Reply via email to