[llvm-bugs] [Bug 37603] New: Missed canonicalization for bit mask calculation

via llvm-bugs llvm-bugs at lists.llvm.org
Sun May 27 07:50:10 PDT 2018


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

            Bug ID: 37603
           Summary: Missed canonicalization for bit mask calculation
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: lebedev.ri at gmail.com
                CC: llvm-bugs at lists.llvm.org

https://godbolt.org/g/VCMNpS
https://rise4fun.com/Alive/idM

int mask_signed_add(int nbits) {
    return (1 << nbits) - 1;
}
unsigned mask_unsigned_add(unsigned nbits) {
    return (1 << nbits) - 1;
}
int mask_signed_xor(int nbits) {
    return ~(-(1 << nbits));
}
unsigned mask_unsigned_xor(unsigned nbits) {
    return ~(-(1 << nbits));
}

define dso_local i32 @mask_signed_add(int)(i32) local_unnamed_addr #0 {
  %2 = shl i32 1, %0
  %3 = add nsw i32 %2, -1
  ret i32 %3
}
define dso_local i32 @mask_unsigned_add(unsigned int)(i32) local_unnamed_addr
#0 {
  %2 = shl i32 1, %0
  %3 = add nsw i32 %2, -1
  ret i32 %3
}
define dso_local i32 @mask_signed_xor(int)(i32) local_unnamed_addr #0 {
  %2 = shl i32 -1, %0
  %3 = xor i32 %2, -1
  ret i32 %3
}
define dso_local i32 @mask_unsigned_xor(unsigned int)(i32) local_unnamed_addr
#0 {
  %2 = shl i32 -1, %0
  %3 = xor i32 %2, -1
  ret i32 %3
}

I think we really would prefer the second variant with `xor`,
because if we calculated mask, it is quite likely that
we will use it in `and` next, and then it can be fused into `andn`..

Thoughs?

-- 
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/20180527/53b948e6/attachment.html>


More information about the llvm-bugs mailing list