This is an archive of the discontinued LLVM Phabricator instance.

[KnownBits] Add fast-path for shl with unknown shift amount
ClosedPublic

Authored by nikic on May 26 2023, 1:14 AM.

Details

Summary

We currently don't call into KnownBits::shl from ValueTracking if the shift amount is unknown. If we do try to do so, we get significant compile-time regressions, because evaluating all 64 shift amounts if quite expensive, and mostly pointless in this case. Add a fast-path for the case where the shift amount is the full [0, BitWidth-1] range. This primarily requires a more accurate estimate of the max shift amount, to avoid taking the fast-path in too many cases.

Diff Detail

Event Timeline

nikic created this revision.May 26 2023, 1:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2023, 1:14 AM
nikic requested review of this revision.May 26 2023, 1:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2023, 1:14 AM
foad accepted this revision.May 26 2023, 1:32 AM

Looks nice.

llvm/lib/Support/KnownBits.cpp
183–194

Do we still need all this logic now that you have determined the max shift amount in advance?

215–217

Since power-of-2 is the normal case it might be faster to handle it without constructing another APInt.

This revision is now accepted and ready to land.May 26 2023, 1:32 AM
nikic updated this revision to Diff 526022.May 26 2023, 4:12 AM

Remove redundant handling for poison cases, use extractBitsAsZExtValue.

nikic marked 2 inline comments as done.May 26 2023, 4:13 AM
nikic added inline comments.
llvm/lib/Support/KnownBits.cpp
183–194

Indeed, we can drop the entire poison handling here now.

215–217

Done. I've put it in a function, because I expect we'll want to adjust the other shift operators to match...

nikic updated this revision to Diff 526024.May 26 2023, 4:14 AM
nikic marked 2 inline comments as done.

Drop unnecessary braces.

foad accepted this revision.May 26 2023, 4:18 AM

Even nicer!

This revision was landed with ongoing or failed builds.May 26 2023, 4:57 AM
This revision was automatically updated to reflect the committed changes.