Use $bits()
from IEEE 1800-2012 § 20.6.2 Expression size system function:
The
$bits
system function returns the number of bits required to hold an expression as a bit stream.
Direct example from the LRM:
... Given the declaration:
typedef struct { logic valid; bit [8:1] data; } MyType;
the expression
$bits(MyType)
shall return 9, the number of data bits needed by a variable of type MyType.