[llvm-dev] [RFC] Saturating left shift intrinsics

Bevin Hansson via llvm-dev llvm-dev at lists.llvm.org
Wed Jul 8 08:30:54 PDT 2020


Hello,

This is an RFC for adding intrinsics which perform saturating signed/unsigned left shift.
There is currently a patch on Phabricator here:
https://reviews.llvm.org/D83216

The intrinsics are of the form

    i32 @llvm.sshl.sat.i32(i32, i32)
    i32 @llvm.ushl.sat.i32(i32, i32)
    <4 x i32> @llvm.sshl.sat.v4i32(<4 x i32>, <4 x i32>)
    <4 x i32> @llvm.ushl.sat.v4i32(<4 x i32>, <4 x i32>)

and are overloaded with a single type which can be either an integer type or a vector of integers. The value(s) provided in the first operand are shifted left by the amount(s) given by the second operand. As with regular left shift instructions, the second operand is an unsigned quantity and must be less than the integer bitwidth.

If the true result of the operation (given infinite precision) lies outside of the maximum or minimum representable value of the signed/unsigned integer, the result saturates to the maximum or minimum depending on the sign of the shifted value.

These are useful for implementing the Embedded-C fixed-point arithmetic support in Clang, as previously detailed and discussed here:
http://lists.llvm.org/pipermail/llvm-dev/2018-August/125433.html
http://lists.llvm.org/pipermail/cfe-dev/2018-May/058019.html

It is of course possible to emit these shifts as regular IR instructions, but targets which support saturating shifts might have difficulties efficiently selecting native instructions if the optimizer undoes the particular expected instruction pattern that the shifts would be selected on.

/ Bevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200708/1c67fe84/attachment.html>


More information about the llvm-dev mailing list