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

            Bug ID: 128423
           Summary: Missed simplification/reassociation for RISC-V
           Product: gcc
           Version: 17.0
            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: ---

Compile with -O2 -march=rv64gcbv:

typedef unsigned long uintptr_t;
int f(char *d, char *m, char *b)
{
  return ((uintptr_t)d - (uintptr_t)b - 2 > 4) & ((uintptr_t)d - (uintptr_t)m -
2 > 4);
}
int g(char *d, char *m, char *b, int *x)
{
  if (((uintptr_t)d - (uintptr_t)b - 2 > 4) & ((uintptr_t)d - (uintptr_t)m - 2
> 4))
    return x[0];
  return x[1];
}


In both cases we can improve by reassociating and doing the subtraction just
once. Something like this:

Before, f:

sub     a1,a0,a1
sub     a0,a0,a2
addi    t1,a0,-2
addi    t0,a1,-2
minu    t2,t1,t0
sltiu   a2,t2,5
xori    a0,a2,1
ret

After, f:

addi    t0,a0,-2
sub     a1,t0,a1
sub     a2,t0,a2
minu    a1,a1,a2
sltiu   a1,a1,5
xori    a0,a1,1
ret

Reply via email to