This is an archive of the discontinued LLVM Phabricator instance.

[Support] Add KnownBits::countMaxSignedBits(). Make KnownBits::countMinSignBits() always return at least 1.
ClosedPublic

Authored by craig.topper on Jan 2 2022, 2:05 PM.

Details

Summary

Even if we don't have any known bits, we can assume that there is
1 at least 1 sign bit. This is consistent with ComputeNumSignBits
which always returns at least.

Add KnownBits::countMaxSignedBits() which computes the number of
bits needed to represent all signed values with those known bits.

Split from D116469.

Diff Detail

Event Timeline

craig.topper created this revision.Jan 2 2022, 2:05 PM
craig.topper requested review of this revision.Jan 2 2022, 2:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 2 2022, 2:05 PM

Add KnownBits::countMinSignBits()

craig.topper retitled this revision from [Support] Make KnownBits::countMinSignBits() always return at least 1. to [Support] Add KnownBits::countMaxSignedBits(). Make KnownBits::countMinSignBits() always return at least 1..Jan 2 2022, 9:06 PM
craig.topper edited the summary of this revision. (Show Details)
lebedev.ri accepted this revision.Jan 2 2022, 11:19 PM

This LG, thank you.

This revision is now accepted and ready to land.Jan 2 2022, 11:19 PM
This revision was landed with ongoing or failed builds.Jan 2 2022, 11:31 PM
This revision was automatically updated to reflect the committed changes.
spatel added inline comments.Jan 3 2022, 8:21 AM
llvm/include/llvm/Support/KnownBits.h
256–258

The name doesn't read clearly to me. Does this seem better?

/// Returns the maximum number of bits needed to represent all possible
/// signed values with these known bits. This is the inverse of the minimum 
/// number of known sign bits. Examples for bitwidth 5:
/// 110?? --> 4
/// 0000? --> 2 
unsigned countMaxSignificantBits() const {

I thought this was what countMaxActiveBits() returns, so we should put a comment on that too to avoid confusion.
The "significant" terminology would be similar to a change in D20275.

craig.topper added inline comments.Jan 3 2022, 9:43 AM
llvm/include/llvm/Support/KnownBits.h
256–258

countMaxActiveBits() treats it as an unsigned number. It treats each 1 in a negative number as significant and ignores all leading zeros on a postive number.

I agree the name countMaxSignedBits() isn't great. It's partially derived from APInt::getMinSignedBits(). Maybe we should start the rename there and lose the "Min"?

spatel added inline comments.Jan 3 2022, 10:39 AM
llvm/include/llvm/Support/KnownBits.h
256–258

Yes, there's a lot of mismatched naming. There's similar functionality in ConstantRange too.
Starting with APInt sounds good. So we have getNumSignBits() there, and then getMinSignedBits() is derived from that.
If we rename the latter to getSignedBits(), that could easily be confused with getNumSignBits().
You know where I'm leaning. :)
"getSignificantBits()" or "getNumSignificantBits()" ?

craig.topper added inline comments.Jan 3 2022, 10:56 AM
llvm/include/llvm/Support/KnownBits.h
256–258

Added to the other renames in D116522

foad added inline comments.Jan 4 2022, 1:26 AM
llvm/include/llvm/Support/KnownBits.h
256–258

You know where I'm leaning. :)
"getSignificantBits()" or "getNumSignificantBits()" ?

I already commented in D116522 but I really don't like that we ended up with APInt::getActiveBits vs APInt::getSignificantBits for the unsigned vs signed version of the same thing.

How about getNumBitsSigned (trying to avoid confusion with "sign bits") vs getNumBitsUnsigned?