[llvm-dev] RFC: Refactor SubclassData

Ehud Katz via llvm-dev llvm-dev at lists.llvm.org
Mon Dec 23 23:16:16 PST 2019

Hello devs,

Recently I've been working on a bug that can probably be fixed with the
addition of a simple flag to a class descendant of `llvm::Value`.
Without increasing the size of the class, I can just add this flag to
`llvm::Value::SubclassData`. But this is not an easy task!

This is because the offsetes/sizes of the data stored in the
`SubclassData`, are hardcoded literals/enums.
If you change but a single bit in one of those offsets/sizes in a base
class, then all the classes derived, will have to be adjusted accordingly -
which can be very tedious and error-prone.

I offer a small set of macros which will take care of the whole
It will work as follows:

struct A {
  int SubclassData : 14;


struct B : A {
    ADD_SUBCLASS_BITFIELD(int,   5, B1) // A::SubclassData bits [0,5)
ADD_SUBCLASS_BITFIELD(bool,  1, B2) // A::SubclassData bits [5,6)
ADD_SUBCLASS_BITFIELD(short, 6, B3) // A::SubclassData bits [6,12)
 // ADD_SUBCLASS_BITFIELD(int,   6, B4) // A::SubclassData bits [12,18) -
triggers a static_assert, as it exceeds the 14 bits in A::SubclassData

struct C : B {
    ADD_SUBCLASS_BITFIELD(bool, 1, C1) // A::SubclassData bits [12,13)

I would appreciate your thoughts on the matter, before I submit a patch for

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191224/fa3bcd07/attachment.html>

More information about the llvm-dev mailing list