[ADT] Add Bitfield utilities
authorGuillaume Chatelet <gchatelet@google.com>
Mon, 29 Jun 2020 12:48:44 +0000 (12:48 +0000)
committerGuillaume Chatelet <gchatelet@google.com>
Mon, 29 Jun 2020 12:48:44 +0000 (12:48 +0000)
commitb56b467a9a84510bd1c5a573c863cb86c98afbcd
tree8cee92b8c8678bf8467b45f2af453c81ceea9f77
parent874fcd4e8ffc42717a41e57ddcedb4ab63e63854
[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
llvm/include/llvm/ADT/Bitfields.h [new file with mode: 0644]
llvm/unittests/ADT/BitFieldsTest.cpp [new file with mode: 0644]
llvm/unittests/ADT/CMakeLists.txt