[llvm-bugs] [Bug 37098] New: Bitfield optimization opportunity

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Apr 11 20:53:21 PDT 2018


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

            Bug ID: 37098
           Summary: Bitfield optimization opportunity
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: matze at braunis.de
                CC: llvm-bugs at lists.llvm.org

This program:

typedef struct {
    unsigned a : 1;
    unsigned b : 1;
    unsigned c : 1;
    unsigned d : 1;
} S;

unsigned overlap(S a, S b) {
    return (
        (a.a & b.a) |
        (a.b & b.b) |
        (a.c & b.c) |
        (a.d & b.d)
    ) != 0;
}

in llvm IR:

define i32 @overlap(i32 %arg, i32 %arg1) {
  %tmp = and i32 %arg, 1
  %tmp2 = and i32 %tmp, %arg1
  %tmp3 = lshr i32 %arg, 1
  %tmp4 = lshr i32 %arg1, 1
  %tmp5 = and i32 %tmp3, 1
  %tmp6 = and i32 %tmp5, %tmp4
  %tmp7 = or i32 %tmp6, %tmp2
  %tmp8 = lshr i32 %arg, 2
  %tmp9 = lshr i32 %arg1, 2
  %tmp10 = and i32 %tmp8, 1
  %tmp11 = and i32 %tmp10, %tmp9
  %tmp12 = or i32 %tmp7, %tmp11
  %tmp13 = lshr i32 %arg, 3
  %tmp14 = lshr i32 %arg1, 3
  %tmp15 = and i32 %tmp13, 1
  %tmp16 = and i32 %tmp15, %tmp14
  %tmp17 = or i32 %tmp12, %tmp16
  ret i32 %tmp17
}

could be optimized to:

define i32 @quux(i32 %arg, i32 %arg1) {
  %bits0 = and i32 %arg, 15
  %and = and i32 %bits0, %arg1
  %cmp = icmp ne i32 %and, 0
  %res = zext i1 %cmp to i32
  ret i32 %res
}

but llvm cannot figure this out at the moment. Maybe there are some
instcombines missing?

-- 
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/20180412/ccc734f9/attachment-0001.html>


More information about the llvm-bugs mailing list