This is an archive of the discontinued LLVM Phabricator instance.

[APInt] Add getBitsSetFrom and setBitsFrom to set upper bits starting at a bit
ClosedPublic

Authored by craig.topper on Mar 3 2017, 11:12 PM.

Details

Summary

We currently have methods to set a specified number of low bits, a specified number of high bits, or a range of bits. But looking at some existing code it seems sometimes we want to set the high bits starting from a certain bit. Currently we do this with something like getHighBits(BitWidth, BitWidth - StartBit). Or once we start switching to setHighBits, setHighBits(BitWidth - StartBit) or setHighBits(getBitWidth() - StartBit).

Particularly for the latter case it would be better to have a convenience method like setBitsFrom(StartBit) so we don't need to mention the bit width that's already known to the APInt object.

I considered just making setBits have a default value of UINT_MAX for the hiBit argument and we would internally MIN it with the bit width. So if it wasn't specified it would be treated as bit width. This would require removing the assertion we currently have on the value of hiBit and may not be as readable.

Diff Detail

Repository
rL LLVM

Event Timeline

RKSimon added inline comments.Mar 4 2017, 3:45 PM
include/llvm/ADT/APInt.h
520 ↗(On Diff #90567)

This comment doesn't match the function......

Fix comment pointed out by Simon.

davide accepted this revision.Mar 5 2017, 3:41 PM
This revision is now accepted and ready to land.Mar 5 2017, 3:41 PM
hans accepted this revision.Mar 6 2017, 9:37 AM

lgtm, but I'd suggest adding a simple test for getBitsSetFrom as well.

This revision was automatically updated to reflect the committed changes.