HomePhabricator

[ADT] Add Bitfield utilities

Authored by gchatelet on Jun 29 2020, 5:48 AM.

Description

[ADT] Add Bitfield utilities

Context:

There are places in LLVM where we need to pack typed fields into opaque values.
For instance, the XXXInst classes in llvm/include/llvm/IR/Instructions.h that extract informations from Value::SubclassData via getSubclassDataFromInstruction().
The bit twiddling is done manually: this impairs readability and prevent consistent handling of out of range values (e.g. https://github.com/llvm/llvm-project/blob/435b458ad0a4630e6126246a6865748104ccad06/llvm/include/llvm/IR/Instructions.h#L564)
More importantly, the bit pattern is scattered throughout the implementation making it hard to pack additionnal fields or check for overlapping bits.

Design decisions:

The Bitfield structs are to be declared together so it is clear which bits are used or not.
The code is designed with simplicity in mind, hence a few limitations:

  • Storage is limited to a single integer,
  • Enum values have to be unsigned,
  • Storage type has to be unsigned,
  • There are no automatic detection of overlapping fields (packed bitfield declaration should help though),
  • The interface is C like so storage needs to be passed in everytime (code is simpler and lifetime considerations more obvious)

RFC: http://lists.llvm.org/pipermail/llvm-dev/2020-June/142196.html

Differential Revision: https://reviews.llvm.org/D81580

Details

Committed
gchateletJun 29 2020, 5:48 AM
Differential Revision
D81580: [ADT] Add Bitfield utilities - design #1
Parents
rG874fcd4e8ffc: Add intrinsic helper function
Branches
Unknown
Tags
Unknown