[llvm-bugs] [Bug 51568] New: Better codegen for truncated bswap

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Aug 20 15:14:14 PDT 2021


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

            Bug ID: 51568
           Summary: Better codegen for truncated bswap
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

unsigned int swap_ull(unsigned long long value)
{
    return ((value & 0x00000000000000ffull) << 56)
            | ((value & 0x000000000000ff00ull) << 40)
            | ((value & 0x0000000000ff0000ull) << 24)
            | ((value & 0x00000000ff000000ull) << 8)
            | ((value & 0x000000ff00000000ull) >> 8)
            | ((value & 0x0000ff0000000000ull) >> 24)
            | ((value & 0x00ff000000000000ull) >> 40)
            | ((value & 0xff00000000000000ull) >> 56);
}

unsigned int swap_ull_builtin(unsigned long long value)
{
    return __builtin_bswap64(value);
}



Trunk -O3:
swap_ull(unsigned long long):                           # @swap_ull(unsigned
long long)
        movabs  rax, 72057589742960640
        and     rax, rdi
        bswap   rax
        shr     rdi, 56
        or      eax, edi
        ret
swap_ull_builtin(unsigned long long):                  #
@swap_ull_builtin(unsigned long long)
        mov     rax, rdi
        bswap   rax
        ret

ICC -O3:
swap_ull(unsigned long long):
        bswap     rdi                                           #10.51
        mov       eax, edi                                      #10.51
        ret                                                     #10.51
swap_ull_builtin(unsigned long long):
        bswap     rdi                                           #15.12
        mov       eax, edi                                      #15.12
        ret                                                     #15.12


unsigned int swap_ull(unsigned long long value)
{
    return ((value & 0x00000000000000ffull) << 56)
            | ((value & 0x000000000000ff00ull) << 40)
            | ((value & 0x0000000000ff0000ull) << 24)
            | ((value & 0x00000000ff000000ull) << 8)
            | ((value & 0x000000ff00000000ull) >> 8)
            | ((value & 0x0000ff0000000000ull) >> 24)
            | ((value & 0x00ff000000000000ull) >> 40)
            | ((value & 0xff00000000000000ull) >> 56);
}

unsigned int swap_ull_builtin(unsigned long long value)
{
    return __builtin_bswap64(value);
}


define i32 @src(i64 %0) {
%1:
  %2 = and i64 %0, 72057594037927935
  %3 = bswap i64 %2
  %4 = lshr i64 %0, 56
  %5 = or i64 %3, %4
  %6 = trunc i64 %5 to i32
  ret i32 %6
}
=>
define i32 @tgt(i64 %0) {
%1:
  %2 = bswap i64 %0
  %3 = trunc i64 %2 to i32
  ret i32 %3
}
Transformation seems to be correct!
https://alive2.llvm.org/ce/z/loQjOQ

-- 
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/20210820/3a6e09bb/attachment.html>


More information about the llvm-bugs mailing list