This is an archive of the discontinued LLVM Phabricator instance.

[Builtins] Improve the IR emitted for MSVC compatible rotr/rotl builtins to match what the middle and backends understand
ClosedPublic

Authored by craig.topper on May 9 2018, 12:04 PM.

Details

Summary

Currently we emit something like

rotl(x, n) {
n &= bitwidth -1;
return n != 0 ? ((x << n) | (x >> (bitwidth - n)) : x;
}

We use a select to avoid the undefined behavior on the (bitwidth - n) shift.

The middle and backend don't really recognize this as a rotate and end up emitting a cmov or control flow because of the select.

A better pattern is (x << (n & mask)) | (x << (-n & mask)) where mask is bitwidth - 1.

Fixes the main complaint in PR37387. There's still some work to be done if the user writes that sequence directly on a short or char where type promotion rules can prevent it from being recognized. The builtin is emitting direct IR with unpromoted types so that isn't a problem for it.

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.May 9 2018, 12:04 PM
spatel accepted this revision.May 9 2018, 12:52 PM

LGTM - thanks!

This revision is now accepted and ready to land.May 9 2018, 12:52 PM
This revision was automatically updated to reflect the committed changes.
rnk added a subscriber: rnk.May 10 2018, 2:48 PM

Thanks!