[llvm-bugs] [Bug 32119] New: XORing of all bytes of a multi-word integer can be simplified

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Mar 2 07:29:10 PST 2017


http://bugs.llvm.org/show_bug.cgi?id=32119

            Bug ID: 32119
           Summary: XORing of all bytes of a multi-word integer can be
                    simplified
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: andreybokhanko at gmail.com
                CC: llvm-bugs at lists.llvm.org

$ cat test2.c
#include <inttypes.h>

uint8_t foo1(uint32_t val) {
  return ((val >> 24) & 0xFF) ^ ((val >> 16) & 0xFF) ^ ((val >> 8) & 0xFF) ^
(val & 0xFF);
}

uint8_t foo2(uint32_t val) {
  uint32_t t = ((val >> 16) & 0xFFFF) ^ (val & 0xFFFF);
  uint8_t res = ((t >> 8) & 0xFF) ^ (t & 0xFF);
  return res;
}

$ mrk3-cc test2.c -S -emit-llvm -Os

$ cat test2.ll
...
; Function Attrs: norecurse nounwind optsize readnone
define zeroext i8 @foo1(i32 %val) #0 {
entry:
  %shr = lshr i32 %val, 24
  %shr1 = lshr i32 %val, 16
  %shr3 = lshr i32 %val, 8
  %xor = xor i32 %shr1, %val
  %xor5 = xor i32 %xor, %shr
  %xor7 = xor i32 %xor5, %shr3
  %conv = trunc i32 %xor7 to i8
  ret i8 %conv
}

; Function Attrs: norecurse nounwind optsize readnone
define zeroext i8 @foo2(i32 %val) #0 {
entry:
  %shr = lshr i32 %val, 16
  %and1 = and i32 %val, 65535
  %xor = xor i32 %shr, %and1
  %shr2 = lshr i32 %xor, 8
  %xor5 = xor i32 %shr2, %xor
  %conv = trunc i32 %xor5 to i8
  ret i8 %conv
}


foo1 and foo2 are equivalent -- foo2 just implements XORing of all bytes of a
multi-word integer more efficiently.

Resulting number of instructions: 7 for foo1 (3 shr, 3 xor, 1 trunc); 6 for
foo2 (2 shr, 1 and, 2 xor). foo2 can be optimized further by removing "and"
instruction, which is not really required.

This probably belongs to InstCombiner? (Sorry, can't find specific category for
InstCombiner.)

Andrey

-- 
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/20170302/c393367c/attachment.html>


More information about the llvm-bugs mailing list