[llvm-bugs] [Bug 41216] New: Missing optimization opportunity: non-power-of-two integer loading with fewer movs

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Mar 24 10:23:37 PDT 2019


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

            Bug ID: 41216
           Summary: Missing optimization opportunity: non-power-of-two
                    integer loading with fewer movs
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Keywords: code-quality
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: nok.raven at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Currently non-power-of-two integers loads are done byte-per-byte:

#include <cstdint>

// could be two loads instead of three: (u16 << 8) | u8
std::uint32_t foo_24(unsigned char const* p)
{
    return static_cast<std::uint32_t>(p[0])
        | (static_cast<std::uint32_t>(p[1]) << 8)
        | (static_cast<std::uint32_t>(p[2]) << 16)
        ;
}

// could be two loads instead of five: (u32 << 8) | u8
std::uint64_t foo_40(unsigned char const* p)
{
    return static_cast<std::uint64_t>(p[0])
        | (static_cast<std::uint64_t>(p[1]) << 8)
        | (static_cast<std::uint64_t>(p[2]) << 16)
        | (static_cast<std::uint64_t>(p[3]) << 24)
        | (static_cast<std::uint64_t>(p[4]) << 32)
        ;
}

// could be two loads instead of six: (u32 << 16) | u16
std::uint64_t foo_48(unsigned char const* p)
{
    return static_cast<std::uint64_t>(p[0])
        | (static_cast<std::uint64_t>(p[1]) << 8)
        | (static_cast<std::uint64_t>(p[2]) << 16)
        | (static_cast<std::uint64_t>(p[3]) << 24)
        | (static_cast<std::uint64_t>(p[4]) << 32)
        | (static_cast<std::uint64_t>(p[5]) << 40)
        ;
}

// could be three loads instead of seven: (u32 << 24) | (u16 << 8) | u8
std::uint64_t foo_56(unsigned char const* p)
{
    return static_cast<std::uint64_t>(p[0])
        | (static_cast<std::uint64_t>(p[1]) << 8)
        | (static_cast<std::uint64_t>(p[2]) << 16)
        | (static_cast<std::uint64_t>(p[3]) << 24)
        | (static_cast<std::uint64_t>(p[4]) << 32)
        | (static_cast<std::uint64_t>(p[5]) << 40)
        | (static_cast<std::uint64_t>(p[6]) << 48)
        ;
}

https://godbolt.org/z/Re7dWL

GCC produces better code (however currently it optimizes only 32bit loads
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89809)

-- 
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/20190324/c132603b/attachment.html>


More information about the llvm-bugs mailing list