[llvm-bugs] [Bug 39793] New: Byte swap idioms for only part of a wider integer value produce suboptimal code

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 26 11:41:48 PST 2018


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

            Bug ID: 39793
           Summary: Byte swap idioms for only part of a wider integer
                    value produce suboptimal code
           Product: libraries
           Version: 7.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Transformation Utilities
          Assignee: unassignedbugs at nondot.org
          Reporter: fabiang at radgametools.com
                CC: llvm-bugs at lists.llvm.org

Reduced from a bug reported on llvm-dev.

-

#include <stdint.h>

uint64_t f(uint64_t x)
{
    return ((x & 0xff000000ul) >> 3 * 8)
         | ((x & 0x00ff0000ul) >>     8)
         | ((x & 0x0000ff00ul) <<     8)
         | ((x & 0x000000fful) << 3 * 8);
}

uint64_t g(uint64_t x)
{
    uint32_t y = (uint32_t)x;
    return ((y & 0xff000000u) >> 3 * 8)
         | ((y & 0x00ff0000u) >>     8)
         | ((y & 0x0000ff00u) <<     8)
         | ((y & 0x000000ffu) << 3 * 8);
}

-

compiled for target x86_64-linux-gnu at -O2 results in this IR:

-

; Function Attrs: norecurse nounwind readnone uwtable
define dso_local i64 @f(i64) local_unnamed_addr #0 {
  %2 = lshr i64 %0, 24
  %3 = and i64 %2, 255
  %4 = lshr i64 %0, 8
  %5 = and i64 %4, 65280
  %6 = or i64 %3, %5
  %7 = shl i64 %0, 8
  %8 = and i64 %7, 16711680
  %9 = or i64 %6, %8
  %10 = shl i64 %0, 24
  %11 = and i64 %10, 4278190080
  %12 = or i64 %9, %11
  ret i64 %12
}

; Function Attrs: nounwind readnone uwtable
define dso_local i64 @g(i64) local_unnamed_addr #1 {
  %2 = trunc i64 %0 to i32
  %3 = tail call i32 @llvm.bswap.i32(i32 %2)
  %4 = zext i32 %3 to i64
  ret i64 %4
}

-

Ideally, the output for f should match g.

This would require matching the byte reverse patterns for a narrower sub-type
as well, and inserting trunc/zext around it (as in the code for g). Not sure
how complicated that ends up being.

-- 
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/20181126/28097a2f/attachment.html>


More information about the llvm-bugs mailing list