This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Add APInt::isInvertOf
AbandonedPublic

Authored by serge-sans-paille on Oct 20 2022, 2:24 AM.

Details

Summary

The pattern is frequent enough to have its own implementation, which is
both more explicit and (slightly) faster than the default idiom.

Also use it to implement a faster llvm::KnownBits::isConstant() that doesn't
rely on popcnt.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2022, 2:24 AM
serge-sans-paille requested review of this revision.Oct 20 2022, 2:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2022, 2:24 AM
nikic added a comment.Oct 21 2022, 3:10 AM

Do we get a benefit is we change only the KnownBits::isConstant implementation to do Zero == ~One?

In terms of readability, I find X == ~Y clearer than X.isInvertOf(Y), so I personally wouldn't do this without some noticeable impact. (Though I'm not particularly opposed either.)

llvm/include/llvm/ADT/APInt.h
931

I think this will cause shift UB for zero-width APInts (yes, those are a thing now ...)

Do we get a benefit is we change only the KnownBits::isConstant implementation to do Zero == ~One?

I've checked and the answer is no (at least as long as popcnt is available. (One | Zero).countPopulation() == getBitWidth() also is an alternative but it's not significantly faster.

In terms of readability, I find X == ~Y clearer than X.isInvertOf(Y), so I personally wouldn't do this without some noticeable impact. (Though I'm not particularly opposed either.)

ok, let's forget about it!

llvm/include/llvm/ADT/APInt.h
931

indeed

serge-sans-paille abandoned this revision.Oct 24 2022, 2:40 AM