This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] shl nuw C, %x -> C iff signbit is set on C.
ClosedPublic

Authored by lebedev.ri on Jun 7 2018, 7:40 AM.

Details

Summary

%r = shl nuw i8 C, %x

As per langref:

If the nuw keyword is present, then the shift produces
a poison value if it shifts out any non-zero bits.

Thus, if the sign bit is set on C, then %x can only be 0, which means that %r can only be C.
Or in other words, set sign bit means that the signed value is negative, so the constant is <= 0.

https://rise4fun.com/Alive/WMk
https://rise4fun.com/Alive/udv

Was mentioned in D47428 review.

We already handle the 0 constant, https://godbolt.org/g/UZq1sJ, so this only handles negative constants.

FIXME: could also use KnownBits.

Diff Detail

Repository
rL LLVM

Event Timeline

lebedev.ri created this revision.Jun 7 2018, 7:40 AM
lebedev.ri updated this revision to Diff 150333.Jun 7 2018, 7:51 AM

Actually run ninja check-llvm this time.

spatel accepted this revision.Jun 7 2018, 9:13 AM

LGTM.

Watch out for seemingly unrelated failures though - any time we get smarter about optimizing using these flags, there's a chance that we will expose bugs in other parts of the optimizer.

This revision is now accepted and ready to land.Jun 7 2018, 9:13 AM
lebedev.ri updated this revision to Diff 150351.Jun 7 2018, 9:24 AM

Rebased, NFC.

LGTM.

Watch out for seemingly unrelated failures though - any time we get smarter about optimizing using these flags, there's a chance that we will expose bugs in other parts of the optimizer.

Thank you for the review and a advice!

Posted KnownBits part as follow-up (D47891), but even that isn't sufficient to handle *all* the cases.

This revision was automatically updated to reflect the committed changes.