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

            Bug ID: 78809
           Summary: Inline strcmp with small constant strings
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wilco at gcc dot gnu.org
  Target Milestone: ---

GCC currently doesn't optimize str(n)cmp of small constant strings (besides the
empty string).  Such cases can be inlined to avoid the overhead of calling
strcmp - benchmarking shows that this is ~2-3x faster.  GLIBC does this
optimization for strings up to size 3 in the headers, however it is better to
implement this in GCC.

int t1(const char *s) { return __builtin_strcmp (s, "a"); }
int t2(const char *s) { return strcmp (s, "a"); }

Currently generates for AArch64 with -O2:

t1:
        adrp    x1, .LC2
        add     x1, x1, :lo12:.LC2
        b       strcmp

t2:
        ldrb    w2, [x0]
        mov     w1, 97
        subs    w1, w1, w2
        bne     .L17
        ldrb    w1, [x0, 1]
        neg     w1, w1
.L17:
        neg     w0, w1
        ret

GCC should be able to generate:

t2:
        ldrb    w2, [x0]
        subs    w1, w2, 97
        bne     .L17
        ldrb    w1, [x0, 1]
.L17:
        ret

Reply via email to