[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 18 21:58:07 PDT 2024


================
@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-    : public ArrayType,
-      private llvm::TrailingObjects<ConstantArrayType, const Expr *> {
+    : public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+    ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+        : Size(Sz), SizeExpr(SE) {}
+    llvm::APInt Size; // Allows us to unique the type.
+    const Expr *SizeExpr;
+  };
+  struct InlineSize {
+    InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+    uint64_t ByteWidth : 4;
+    uint64_t Size : 60;
+  };
+  union {
+    struct InlineSize I;
+    ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-                    const Expr *sz, ArraySizeModifier sm, unsigned tq)
-      : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
-    ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr;
-    if (ConstantArrayTypeBits.HasStoredSizeExpr) {
-      assert(!can.isNull() && "canonical constant array should not have size");
-      *getTrailingObjects<const Expr*>() = sz;
-    }
+  ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz,
+                    ArraySizeModifier SM, unsigned TQ)
+      : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), I(Width / 8, Sz) {
+    ConstantArrayTypeBits.HasExternalSize = false;
+    assert(Sz < 0x0FFFFFFFFFFFFFFF && "Size must fit in 60 bits");
+    assert(Width < 0xFF && "Type width must fit in 8 bits");
----------------
tbaederr wrote:

8 or 4?

https://github.com/llvm/llvm-project/pull/85716


More information about the cfe-commits mailing list