This borrows as much as possible from the SDAG version of the code (originally added with D27129 and since updated with big endian support).
In IR, we can test more easily for correctness than we did in the original patch. I'm using the simplest cases that I could find for InstSimplify - we computeKnownBits on variable shift amounts to see if they are zero or in range, so I used a shuffle to push constant elements into a vector.
The motivating x86 example from https://llvm.org/PR50123 is also here. We computeKnownBits in the caller code, but we only check if the shift amount is in range. That could be enhanced to catch the 2nd x86 test - if the shift amount is known too big, the result is 0.
Alive2 understands the datalayout and agrees that the tests here are correct - example:
https://alive2.llvm.org/ce/z/KZJFMZ
Can you use APInt::insertBits here ?