[InstCombine] canonicalize rotate patterns with cmp/select
authorSanjay Patel <spatel@rotateright.com>
Tue, 13 Nov 2018 22:47:24 +0000 (22:47 +0000)
committerSanjay Patel <spatel@rotateright.com>
Tue, 13 Nov 2018 22:47:24 +0000 (22:47 +0000)
commitf8f12272e80fd37f6ee48b7ffef9d7296dcd2e33
tree259dcf63ddac790b771d072158e2470d6744dd8c
parente19dc6137fadf44aac0385357e3169350a2061c0
[InstCombine] canonicalize rotate patterns with cmp/select

The cmp+branch variant of this pattern is shown in:
https://bugs.llvm.org/show_bug.cgi?id=34924
...and as discussed there, we probably can't transform
that without a rotate intrinsic. We do have that now
via funnel shift, but we're not quite ready to
canonicalize IR to that form yet. The case with 'select'
should already be transformed though, so that's this patch.

The sequence with negation followed by masking is what we
use in the backend and partly in clang (though that part
should be updated).

https://rise4fun.com/Alive/TplC
  %cmp = icmp eq i32 %shamt, 0
  %sub = sub i32 32, %shamt
  %shr = lshr i32 %x, %shamt
  %shl = shl i32 %x, %sub
  %or = or i32 %shr, %shl
  %r = select i1 %cmp, i32 %x, i32 %or
  =>
  %neg = sub i32 0, %shamt
  %masked = and i32 %shamt, 31
  %maskedneg = and i32 %neg, 31
  %shl2 = lshr i32 %x, %masked
  %shr2 = shl i32 %x, %maskedneg
  %r = or i32 %shl2, %shr2

llvm-svn: 346807
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/test/Transforms/InstCombine/rotate.ll