[llvm-bugs] [Bug 48970] New: Unaligned read followed by bswap generates suboptimal code

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jan 30 12:41:52 PST 2021


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

            Bug ID: 48970
           Summary: Unaligned read followed by bswap generates suboptimal
                    code
           Product: libraries
           Version: 11.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: RISC-V
          Assignee: unassignedbugs at nondot.org
          Reporter: eduardosanchezmunoz at gmail.com
                CC: asb at lowrisc.org, llvm-bugs at lists.llvm.org

On RISC-V, an unaligned read followed by a bswap produces suboptimal code.

Given the following IR:

declare i16 @llvm.bswap.i16(i16)
define i16 @read(i16* %p) {
start:
  %v = load i16, i16* %p, align 1
  ret i16 %v
}
define i16 @read_swap(i16* %p) {
start:
  %v = load i16, i16* %p, align 1
  %v2 = tail call i16 @llvm.bswap.i16(i16 %v)
  ret i16 %v2
}

compiled with llc -mtriple=riscv64-unknown-linux-gnu -O3

it produces the following assembly:

read:
        lb      a1, 1(a0)
        lbu     a0, 0(a0)
        slli    a1, a1, 8
        or      a0, a0, a1
        ret
read_swap:
        lb      a1, 1(a0)
        lbu     a0, 0(a0)
        slli    a1, a1, 8
        or      a0, a0, a1
        slli    a1, a0, 40
        addi    a2, zero, 255
        slli    a2, a2, 48
        and     a1, a1, a2
        slli    a0, a0, 56
        or      a0, a0, a1
        srli    a0, a0, 48
        ret

The code for read is generated as expected. However, the code for read_swap can
be simplified to:

read_swap:
        lb      a1, 0(a0)
        lbu     a0, 1(a0)
        slli    a1, a1, 8
        or      a0, a0, a1
        ret

-- 
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/20210130/07f9fe1a/attachment.html>


More information about the llvm-bugs mailing list