[PATCH] D47428: [InstCombine] PR37603: low bit mask canonicalization

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 27 11:14:31 PDT 2018


lebedev.ri created this revision.
lebedev.ri added reviewers: spatel, craig.topper, RKSimon.

This is PR37603 <https://bugs.llvm.org/show_bug.cgi?id=37603>.

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

When doing bit manipulations, it is quite common to calculate some bit mask,
and apply it to some value via `and`.

The typical C code looks like:

  int mask_signed_add(int nbits) {
      return (1 << nbits) - 1;
  }

which is translated into (with `-O3`)

  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
  }

But there is a second, less readable variant:

  int mask_signed_xor(int nbits) {
      return ~(-(1 << nbits));
  }

which is translated into (with `-O3`)

  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
  }

But now that i have actually looked:
https://godbolt.org/g/VTUDmU
_some_ backend changes will be needed too.
We clearly loose `bzhi` recognition.


Repository:
  rL LLVM

https://reviews.llvm.org/D47428

Files:
  lib/Transforms/InstCombine/InstCombineAddSub.cpp
  test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47428.148761.patch
Type: text/x-patch
Size: 9298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180527/fcb5f72c/attachment.bin>


More information about the llvm-commits mailing list