[llvm-bugs] [Bug 36421] New: Loads done by inlined memcmp() are not cached

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Feb 17 11:44:03 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=36421

            Bug ID: 36421
           Summary: Loads done by inlined memcmp() are not cached
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: dave at znu.io
                CC: llvm-bugs at lists.llvm.org

While trying to create an optimized version of llvm::StringSwitch, I found that
loads done by inlined memcmp() are not cached in a register. What follows is
the distilled test case of my optimized llvm::StringSwitch. Also, given that
"clang -O3 -S -emit-llvm" doesn't inline @memcmp, I assume that this is a
backend problem. Please correct me if I'm wrong. Thanks!

#include <string.h>

unsigned test(const char *string, unsigned len) {
  switch (len) {
  case 6:
    if (memcmp(string, "abcdef", 6) == 0) return 61;
    if (memcmp(string, "ABCDEF", 6) == 0) return 62;
    if (memcmp(string, "aBcDeF", 6) == 0) return 63;
    if (memcmp(string, "AbCdEf", 6) == 0) return 64;
    return 0;
  default:
    return 0;
  }
}

Generates redundant loads:

_Z4testPKcj:                            # @_Z4testPKcj
        .cfi_startproc
# %bb.0:
        xorl    %eax, %eax
        cmpl    $6, %esi
        jne     .LBB0_8
# %bb.1:
        movl    $1684234849, %eax       # imm = 0x64636261
        xorl    (%rdi), %eax
        movzwl  4(%rdi), %ecx
        xorl    $26213, %ecx            # imm = 0x6665
        orl     %eax, %ecx
        je      .LBB0_2
# %bb.3:
        movl    $1145258561, %eax       # imm = 0x44434241
        xorl    (%rdi), %eax
        movzwl  4(%rdi), %ecx
        xorl    $17989, %ecx            # imm = 0x4645
        orl     %eax, %ecx
        je      .LBB0_4
# %bb.5:
        movl    $1147355745, %eax       # imm = 0x44634261
        xorl    (%rdi), %eax
        movzwl  4(%rdi), %ecx
        xorl    $18021, %ecx            # imm = 0x4665
        orl     %eax, %ecx
        je      .LBB0_6
# %bb.7:
        movl    $1682137665, %ecx       # imm = 0x64436241
        xorl    (%rdi), %ecx
        movzwl  4(%rdi), %edx
        xorl    $26181, %edx            # imm = 0x6645
        xorl    %eax, %eax
        orl     %ecx, %edx
        sete    %al
        shll    $6, %eax
.LBB0_8:
        retq
.LBB0_2:
        movl    $61, %eax
        retq
.LBB0_4:
        movl    $62, %eax
        retq
.LBB0_6:
        movl    $63, %eax
        retq

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180217/23203457/attachment-0001.html>


More information about the llvm-bugs mailing list