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

            Bug ID: 120479
           Summary: missed opportunity to generate czero.nez
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bonzini at gnu dot org
  Target Milestone: ---
            Target: riscv

Given this code:

void f(unsigned *s, unsigned a0, unsigned a1, unsigned a2, unsigned a3)
{
    unsigned r, c;

    r = s[0] + a0;
    s[0] = r;
    c = r < a0;                        // (1)

    r = s[1] + a1;
    s[1] = r + c;
    c = (r < a1) | (r == -1 ? c : 0);  // (2)

    r = s[2] + a2;
    s[2] = r + c;
    c = (r < a2) | (r == -1 ? c : 0);

    s[3] += a3 + c;
}

with "-O2 -march=rv32g_zicond", GCC generates the following instructions to
compute the expression marked with (2):

        ; c from line (1) in a7; r+1 in t1
        seqz    t1,t1
        and     t3,a7,t1         ; t3 = (t1 == 0) ? 0 : a7
        sltu    a5,a2,a5         ; r<a1 in a5
        or      a5,a5,t3

The seqz+and sequence can be replaced by a single czero.nez instruction:

        ; c from line (1) in a7, r+1 in t1
        czero.nez t3,a7,t1       ; t3 = (t1 != 0) ? a7 : 0
        sltu    a5,a2,a5         ; r<a1 in a5
        or      a5,a5,t3

Reply via email to