File: TypeLayout.h

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (764 lines) | stat: -rw-r--r-- 31,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//===------------- TypeLayout.h ---------------  Type layouts ---*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_IRGEN_TYPE_LAYOUT_H
#define SWIFT_IRGEN_TYPE_LAYOUT_H

#include "FixedTypeInfo.h"
#include "TypeInfo.h"
#include "swift/SIL/SILType.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/Debug.h"

namespace swift {
namespace irgen {

class EnumTypeLayoutEntry;
class LayoutStringBuilder;

enum class TypeLayoutEntryKind : uint8_t {
  Empty,
  Scalar,
  Archetype,
  AlignedGroup,
  Resilient,
  Enum,
  TypeInfoBased,
};

enum class ScalarKind : uint8_t {
  TriviallyDestroyable,
  Immovable,
  ErrorReference,
  NativeStrongReference,
  NativeUnownedReference,
  NativeWeakReference,
  UnknownReference,
  UnknownUnownedReference,
  UnknownWeakReference,
  BlockReference,
  BridgeReference,
  ObjCReference,
  BlockStorage,
  ThickFunc,
  ExistentialReference,
  CustomReference,
};

/// Convert a ReferenceCounting into the appropriate Scalar reference
ScalarKind refcountingToScalarKind(ReferenceCounting refCounting);

class TypeLayoutEntry {
protected:
  /// Memoize the value of layoutString()
  /// None -> Not yet computed
  /// Optional(nullptr) -> No layout string
  /// Optional(Constant*) -> Layout string
  mutable std::optional<llvm::Constant *> _layoutString;

public:
  TypeLayoutEntryKind kind;
  uint8_t hasArchetypeField : 1;
  uint8_t hasResilientField : 1;
  uint8_t hasDependentResilientField : 1;

  TypeLayoutEntry()
      : kind(TypeLayoutEntryKind::Empty), hasArchetypeField(false),
        hasResilientField(false), hasDependentResilientField(false) {}

  TypeLayoutEntry(TypeLayoutEntryKind kind)
      : kind(kind), hasArchetypeField(false), hasResilientField(false),
        hasDependentResilientField(false) {}

  virtual ~TypeLayoutEntry();

  virtual void computeProperties();

  bool containsResilientField() const;
  bool containsArchetypeField() const;
  bool containsDependentResilientField() const;


  bool isEmpty() const { return kind == TypeLayoutEntryKind::Empty; }

  TypeLayoutEntryKind getKind() const { return kind; }

  virtual llvm::Value *alignmentMask(IRGenFunction &IGF) const;
  virtual llvm::Value *size(IRGenFunction &IGF) const;

  /// Return the size of the type if known statically
  virtual std::optional<Size> fixedSize(IRGenModule &IGM) const;

  /// Return if the type and its subtypes are trivially destructible.
  virtual bool isTriviallyDestroyable() const;
  virtual bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                                   unsigned index) const;
  virtual bool isSingleRetainablePointer() const;

  /// Return if the size of the type is known statically
  virtual bool isFixedSize(IRGenModule &IGM) const;

  /// Return the alignment of the type if known statically
  virtual std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const;

  /// Return the number of extra inhabitants if known statically
  virtual std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const;
  virtual llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const;
  virtual llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const;
  virtual llvm::Constant *layoutString(IRGenModule &IGM,
                                       GenericSignature genericSig) const;
  virtual bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                              GenericSignature genericSig) const;

  virtual void destroy(IRGenFunction &IGF, Address addr) const;

  void assign(IRGenFunction &IGF, Address dest, Address src,
              IsTake_t isTake) const;
  void initialize(IRGenFunction &IGF, Address dest, Address src,
                  IsTake_t isTake) const;

  virtual void assignWithCopy(IRGenFunction &IGF, Address dest,
                              Address src) const;
  virtual void assignWithTake(IRGenFunction &IGF, Address dest,
                              Address src) const;

  virtual void initWithCopy(IRGenFunction &IGF, Address dest,
                              Address src) const;
  virtual void initWithTake(IRGenFunction &IGF, Address dest,
                            Address src) const;

  /// Returns a pointer to the object (T*) inside of the buffer.
  virtual llvm::Value *initBufferWithCopyOfBuffer(IRGenFunction &IGF,
                                                  Address dest,
                                                  Address src) const;

  virtual llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                               llvm::Value *numEmptyCases,
                                               Address addr) const;

  virtual void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                         llvm::Value *numEmptyCases,
                                         Address enumAddr) const;

  const EnumTypeLayoutEntry *getAsEnum() const;

  bool isAlignedGroup() const;

  virtual std::optional<const FixedTypeInfo *> getFixedTypeInfo() const;

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD virtual void dump() const {
    assert(isEmpty() && "Missing subclass implementation?");
    llvm::dbgs() << "{empty}";
  }
#endif
protected:
  llvm::Value *
  getEnumTagSinglePayloadGeneric(IRGenFunction &IGF, Address addr,
                                 llvm::Value *numEmptyCases,
                                 llvm::function_ref<llvm::Value *(Address addr)>
                                     getExtraInhabitantIndexFun) const;

  void storeEnumTagSinglePayloadGeneric(
      IRGenFunction &IGF, llvm::Value *tag, llvm::Value *numEmptyCases,
      Address addr,
      llvm::function_ref<void(Address addr, llvm::Value *tag)>
          storeExtraInhabitantIndexFun) const;

  void gatherProperties(TypeLayoutEntry *fromEntry);
};

class ScalarTypeLayoutEntry : public TypeLayoutEntry,
                              public llvm::FoldingSetNode {
public:
  const FixedTypeInfo &typeInfo;
  SILType representative;

  ScalarKind scalarKind;

  ScalarTypeLayoutEntry(const FixedTypeInfo &ti, SILType representative,
                        ScalarKind scalarKind)
      : TypeLayoutEntry(TypeLayoutEntryKind::Scalar), typeInfo(ti),
        representative(representative), scalarKind(scalarKind) {}

  ~ScalarTypeLayoutEntry();

  void computeProperties() override;

  // Support for FoldingSet.
  void Profile(llvm::FoldingSetNodeID &id) const;
  static void Profile(llvm::FoldingSetNodeID &ID, const TypeInfo &ti,
                      SILType ty);

  llvm::Value *alignmentMask(IRGenFunction &IGF) const override;
  llvm::Value *size(IRGenFunction &IGF) const override;
  std::optional<Size> fixedSize(IRGenModule &IGM) const override;
  bool isFixedSize(IRGenModule &IGM) const override;
  std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const override;
  std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const override;
  bool isTriviallyDestroyable() const override;
  bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                           unsigned index) const override;
  bool isSingleRetainablePointer() const override;
  llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const override;
  llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const override;
  llvm::Type *getStorageType(IRGenFunction &IGF) const;
  llvm::Constant *layoutString(IRGenModule &IGM,
                               GenericSignature genericSig) const override;
  bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                      GenericSignature genericSig) const override;

  void destroy(IRGenFunction &IGF, Address addr) const override;

  void assignWithCopy(IRGenFunction &IGF, Address dest,
                      Address src) const override;
  void assignWithTake(IRGenFunction &IGF, Address dest,
                      Address src) const override;

  void initWithCopy(IRGenFunction &IGF, Address dest,
                    Address src) const override;
  void initWithTake(IRGenFunction &IGF, Address dest,
                    Address src) const override;

  llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                       llvm::Value *numEmptyCases,
                                       Address addr) const override;

  void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                 llvm::Value *numEmptyCases,
                                 Address enumAddr) const override;

  static bool classof(const TypeLayoutEntry *entry);

  std::optional<const FixedTypeInfo *> getFixedTypeInfo() const override;

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const override;
#endif

};

class ArchetypeLayoutEntry : public TypeLayoutEntry,
                             public llvm::FoldingSetNode {
  SILType archetype;

public:
  ArchetypeLayoutEntry(SILType archetype)
      : TypeLayoutEntry(TypeLayoutEntryKind::Archetype), archetype(archetype) {}

  ~ArchetypeLayoutEntry();

  void computeProperties() override;

  // Support for FoldingSet.
  void Profile(llvm::FoldingSetNodeID &id) const;
  static void Profile(llvm::FoldingSetNodeID &ID, SILType archetype);

  llvm::Value *alignmentMask(IRGenFunction &IGF) const override;
  llvm::Value *size(IRGenFunction &IGF) const override;
  std::optional<Size> fixedSize(IRGenModule &IGM) const override;
  bool isFixedSize(IRGenModule &IGM) const override;
  std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const override;
  std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const override;
  bool isTriviallyDestroyable() const override;
  bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                           unsigned index) const override;
  bool isSingleRetainablePointer() const override;
  llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const override;
  llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const override;
  llvm::Constant *layoutString(IRGenModule &IGM,
                               GenericSignature genericSig) const override;
  bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                      GenericSignature genericSig) const override;

  void destroy(IRGenFunction &IGF, Address addr) const override;

  void assignWithCopy(IRGenFunction &IGF, Address dest,
                      Address src) const override;
  void assignWithTake(IRGenFunction &IGF, Address dest,
                      Address src) const override;

  void initWithCopy(IRGenFunction &IGF, Address dest,
                    Address src) const override;
  void initWithTake(IRGenFunction &IGF, Address dest,
                    Address src) const override;

  llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                       llvm::Value *numEmptyCases,
                                       Address addr) const override;

  void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                 llvm::Value *numEmptyCases,
                                 Address enumAddr) const override;

  static bool classof(const TypeLayoutEntry *entry);

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const override;
#endif
};

class ResilientTypeLayoutEntry : public TypeLayoutEntry,
                                 public llvm::FoldingSetNode {
  SILType ty;

public:
  ResilientTypeLayoutEntry(SILType ty)
      : TypeLayoutEntry(TypeLayoutEntryKind::Resilient), ty(ty) {}

  ~ResilientTypeLayoutEntry();

  void computeProperties() override;

  // Support for FoldingSet.
  void Profile(llvm::FoldingSetNodeID &id) const;
  static void Profile(llvm::FoldingSetNodeID &ID, SILType ty);

  llvm::Value *alignmentMask(IRGenFunction &IGF) const override;
  llvm::Value *size(IRGenFunction &IGF) const override;
  std::optional<Size> fixedSize(IRGenModule &IGM) const override;
  bool isFixedSize(IRGenModule &IGM) const override;
  std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const override;
  std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const override;
  bool isTriviallyDestroyable() const override;
  bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                           unsigned index) const override;
  bool isSingleRetainablePointer() const override;
  llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const override;
  llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const override;
  llvm::Constant *layoutString(IRGenModule &IGM,
                               GenericSignature genericSig) const override;
  bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                      GenericSignature genericSig) const override;

  void destroy(IRGenFunction &IGF, Address addr) const override;

  void assignWithCopy(IRGenFunction &IGF, Address dest,
                      Address src) const override;
  void assignWithTake(IRGenFunction &IGF, Address dest,
                      Address src) const override;

  void initWithCopy(IRGenFunction &IGF, Address dest,
                    Address src) const override;
  void initWithTake(IRGenFunction &IGF, Address dest,
                    Address src) const override;

  llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                       llvm::Value *numEmptyCases,
                                       Address addr) const override;

  void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                 llvm::Value *numEmptyCases,
                                 Address enumAddr) const override;

  static bool classof(const TypeLayoutEntry *entry);

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const override;
#endif
};

class AlignedGroupEntry : public TypeLayoutEntry, public llvm::FoldingSetNode {
  std::vector<TypeLayoutEntry *> entries;
  SILType ty;
  Alignment::int_type minimumAlignment;

  std::optional<const FixedTypeInfo *> fixedTypeInfo;

public:
  AlignedGroupEntry(const std::vector<TypeLayoutEntry *> &entries, SILType ty,
                    Alignment::int_type minimumAlignment,
                    std::optional<const FixedTypeInfo *> fixedTypeInfo)
      : TypeLayoutEntry(TypeLayoutEntryKind::AlignedGroup), entries(entries),
        ty(ty), minimumAlignment(minimumAlignment),
        fixedTypeInfo(fixedTypeInfo) {}

  ~AlignedGroupEntry();

  void computeProperties() override;

  // Support for FoldingSet.
  void Profile(llvm::FoldingSetNodeID &id) const;
  static void Profile(llvm::FoldingSetNodeID &ID,
                      const std::vector<TypeLayoutEntry *> &entries,
                      Alignment::int_type minimumAlignment);

  llvm::Value *alignmentMask(IRGenFunction &IGF) const override;
  llvm::Value *size(IRGenFunction &IGF) const override;
  std::optional<Size> fixedSize(IRGenModule &IGM) const override;
  bool isFixedSize(IRGenModule &IGM) const override;
  std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const override;
  std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const override;
  bool isTriviallyDestroyable() const override;
  bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                           unsigned index) const override;
  bool isSingleRetainablePointer() const override;
  llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const override;
  llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const override;
  llvm::Constant *layoutString(IRGenModule &IGM,
                               GenericSignature genericSig) const override;
  bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                      GenericSignature genericSig) const override;

  void destroy(IRGenFunction &IGF, Address addr) const override;

  void assignWithCopy(IRGenFunction &IGF, Address dest,
                      Address src) const override;
  void assignWithTake(IRGenFunction &IGF, Address dest,
                      Address src) const override;

  void initWithCopy(IRGenFunction &IGF, Address dest,
                    Address src) const override;
  void initWithTake(IRGenFunction &IGF, Address dest,
                    Address src) const override;

  llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                       llvm::Value *numEmptyCases,
                                       Address addr) const override;

  void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                 llvm::Value *numEmptyCases,
                                 Address enumAddr) const override;

  static bool classof(const TypeLayoutEntry *entry);

  std::optional<const FixedTypeInfo *> getFixedTypeInfo() const override;

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const override;
#endif

private:
  /// Memoize the value of fixedSize()
  /// None -> Not yet computed
  /// Optional(None) -> Not fixed size
  /// Optional(Size) -> Fixed Size
  mutable std::optional<std::optional<Size>> _fixedSize = std::nullopt;
  /// Memoize the value of fixedAlignment()
  /// None -> Not yet computed
  /// Optional(None) -> Not fixed Alignment
  /// Optional(Alignment) -> Fixed Alignment
  mutable std::optional<std::optional<Alignment>> _fixedAlignment =
      std::nullopt;

  /// Memoize the value of fixedXICount()
  /// None -> Not yet computed
  /// Optional(None) -> Not fixed xi count
  /// Optional(Count) -> Fixed XICount
  mutable std::optional<std::optional<uint32_t>> _fixedXICount = std::nullopt;

  llvm::Value *withExtraInhabitantProvidingEntry(
      IRGenFunction &IGF, Address addr, llvm::Type *returnType,
      llvm::function_ref<llvm::Value *(TypeLayoutEntry *,
                                       Address /*entry addr*/,
                                       llvm::Value * /*entry xi count*/)>
          entryFun) const;
  void
  withEachEntry(IRGenFunction &IGF, Address dest, Address src,
                llvm::function_ref<void(TypeLayoutEntry *entry,
                                        Address entryDest, Address entrySrc)>
                    entryFun) const;
};

class EnumTypeLayoutEntry : public TypeLayoutEntry,
                            public llvm::FoldingSetNode {
public:
  /// More efficient value semantics implementations for certain enum layouts.
  enum CopyDestroyStrategy {
    /// No special behavior.
    Normal,
    /// The payload is trivially destructible, so copying is bitwise (if
    /// allowed), and destruction is a noop.
    TriviallyDestroyable,
    /// The payload is a single reference-counted value, and we have
    /// a single no-payload case which uses the null extra inhabitant, so
    /// copy and destroy can pass through to retain and release entry
    /// points.
    NullableRefcounted,
    /// The payload's value witnesses can handle the extra inhabitants we use
    /// for no-payload tags, so we can forward all our calls to them.
    ForwardToPayload,
  };

  unsigned numEmptyCases;
  unsigned minimumAlignment;
  std::vector<TypeLayoutEntry *> cases;
  SILType ty;
  std::optional<const FixedTypeInfo *> fixedTypeInfo;

  EnumTypeLayoutEntry(unsigned numEmptyCases,
                      const std::vector<TypeLayoutEntry *> &cases, SILType ty,
                      std::optional<const FixedTypeInfo *> fixedTypeInfo,
                      Alignment::int_type minimumAlignment,
                      std::optional<Size> fixedSize)
      : TypeLayoutEntry(TypeLayoutEntryKind::Enum),
        numEmptyCases(numEmptyCases), minimumAlignment(minimumAlignment),
        cases(cases), ty(ty), fixedTypeInfo(fixedTypeInfo) {
    if (fixedSize) {
      _fixedSize = fixedSize;
    }
  }

  ~EnumTypeLayoutEntry();

  void computeProperties() override;

  // Support for FoldingSet.
  void Profile(llvm::FoldingSetNodeID &id) const;
  static void Profile(llvm::FoldingSetNodeID &ID, unsigned numEmptyCases,
                      const std::vector<TypeLayoutEntry *> &cases);

  llvm::Value *alignmentMask(IRGenFunction &IGF) const override;
  llvm::Value *size(IRGenFunction &IGF) const override;
  std::optional<Size> fixedSize(IRGenModule &IGM) const override;
  bool isFixedSize(IRGenModule &IGM) const override;
  std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const override;
  std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const override;
  bool isTriviallyDestroyable() const override;
  bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                           unsigned index) const override;
  bool isSingleRetainablePointer() const override;
  CopyDestroyStrategy copyDestroyKind(IRGenModule &IGM) const;
  llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const override;
  llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const override;
  llvm::Constant *layoutString(IRGenModule &IGM,
                               GenericSignature genericSig) const override;
  bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                      GenericSignature genericSig) const override;

  void destroy(IRGenFunction &IGF, Address addr) const override;

  void assignWithCopy(IRGenFunction &IGF, Address dest,
                      Address src) const override;
  void assignWithTake(IRGenFunction &IGF, Address dest,
                      Address src) const override;

  void initWithCopy(IRGenFunction &IGF, Address dest,
                    Address src) const override;
  void initWithTake(IRGenFunction &IGF, Address dest,
                    Address src) const override;

  llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                       llvm::Value *numEmptyCases,
                                       Address addr) const override;

  void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                 llvm::Value *numEmptyCases,
                                 Address enumAddr) const override;

  llvm::Value *getEnumTag(IRGenFunction &IGF, Address enumAddr) const;

  void destructiveProjectEnumData(IRGenFunction &IGF, Address enumAddr) const;

  void destructiveInjectEnumTag(IRGenFunction &IGF, llvm::Value *tag,
                                Address enumAddr) const;

  bool isMultiPayloadEnum() const;
  bool isSingleton() const;

  std::optional<const FixedTypeInfo *> getFixedTypeInfo() const override;

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const override;
#endif

private:
  /// Memoize the value of fixedSize()
  /// None -> Not yet computed
  /// Optional(None) -> Not fixed size
  /// Optional(Size) -> Fixed Size
  mutable std::optional<std::optional<Size>> _fixedSize = std::nullopt;
  /// Memoize the value of fixedAlignment()
  /// None -> Not yet computed
  /// Optional(None) -> Not fixed Alignment
  /// Optional(Alignment) -> Fixed Alignment
  mutable std::optional<std::optional<Alignment>> _fixedAlignment =
      std::nullopt;

  /// Memoize the value of fixedXICount()
  /// None -> Not yet computed
  /// Optional(None) -> Not fixed xi count
  /// Optional(Count) -> Fixed XICount
  mutable std::optional<std::optional<uint32_t>> _fixedXICount = std::nullopt;

  llvm::Value *maxPayloadSize(IRGenFunction &IGF) const;
  llvm::BasicBlock *testSinglePayloadEnumContainsPayload(IRGenFunction &IGF,
                                                         Address addr) const;

  void initializeSinglePayloadEnum(IRGenFunction &IGF, Address dest,
                                   Address src, IsTake_t isTake) const;
  void assignSinglePayloadEnum(IRGenFunction &IGF, Address dest, Address src,
                               IsTake_t isTake) const;

  void initializeMultiPayloadEnum(IRGenFunction &IGF, Address dest,
                                   Address src, IsTake_t isTake) const;
  void assignMultiPayloadEnum(IRGenFunction &IGF, Address dest, Address src,
                              IsTake_t isTake) const;

  std::pair<Address, llvm::Value *>
  getMultiPayloadEnumTagByteAddrAndNumBytes(IRGenFunction &IGF,
                                            Address addr) const;

  llvm::Value *
  getEnumTagSinglePayloadForSinglePayloadEnum(IRGenFunction &IGF, Address addr,
                                              llvm::Value *numEmptyCases) const;
  void storeEnumTagSinglePayloadForSinglePayloadEnum(IRGenFunction &IGF,
                                                     llvm::Value *tag,
                                                     llvm::Value *numEmptyCases,
                                                     Address enumAddr) const;
  llvm::Value *
  getEnumTagSinglePayloadForMultiPayloadEnum(IRGenFunction &IGF, Address addr,
                                             llvm::Value *numEmptyCases) const;
  void storeEnumTagSinglePayloadForMultiPayloadEnum(IRGenFunction &IGF,
                                                    llvm::Value *tag,
                                                    llvm::Value *numEmptyCases,
                                                    Address enumAddr) const;
  llvm::Value *getEnumTagMultipayload(IRGenFunction &IGF,
                                      Address enumAddr) const;

  void storeEnumTagMultipayload(IRGenFunction &IGF, llvm::Value *tag,
                                Address enumAddr) const;

  /// Store a value to the enum's tag bytes.
  void storeMultiPayloadTag(IRGenFunction &IGF, llvm::Value *value,
                            Address enumAddr) const;
  /// Store a value to the enum's payload bytes.
  void storeMultiPayloadValue(IRGenFunction &IGF, llvm::Value *value,
                              Address enumAddr) const;

  void destroyMultiPayloadEnum(IRGenFunction &IGF, Address enumAddr) const;
  void destroySinglePayloadEnum(IRGenFunction &IGF, Address enumAddr) const;

  void multiPayloadEnumForPayloadAndEmptyCases(
      IRGenFunction &IGF, Address addr,
      llvm::function_ref<void(TypeLayoutEntry *payload, llvm::Value *tagIndex)>
          payloadFunction,
      llvm::function_ref<void()> noPayloadFunction) const;

  bool buildSinglePayloadRefCountString(IRGenModule &IGM,
                                        LayoutStringBuilder &B,
                                        GenericSignature genericSig) const;

  bool buildMultiPayloadRefCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                                       GenericSignature genericSig) const;

  static bool classof(const TypeLayoutEntry *entry);
};

/// TypeLayouts that defer to the existing typeinfo infrastructure in cases that
/// type layouts don't have the functionality implemented yet (e.g. multi enum
/// extra inhabitants).
class TypeInfoBasedTypeLayoutEntry : public TypeLayoutEntry,
                                     public llvm::FoldingSetNode {
public:
  const FixedTypeInfo &typeInfo;
  SILType representative;

  TypeInfoBasedTypeLayoutEntry(const FixedTypeInfo &ti, SILType representative)
      : TypeLayoutEntry(TypeLayoutEntryKind::TypeInfoBased), typeInfo(ti),
        representative(representative) {}

  ~TypeInfoBasedTypeLayoutEntry();

  void computeProperties() override;

  // Support for FoldingSet.
  void Profile(llvm::FoldingSetNodeID &id) const;
  static void Profile(llvm::FoldingSetNodeID &ID, const TypeInfo &ti,
                      SILType ty);

  llvm::Value *alignmentMask(IRGenFunction &IGF) const override;
  llvm::Value *size(IRGenFunction &IGF) const override;
  llvm::Value *extraInhabitantCount(IRGenFunction &IGF) const override;
  bool isTriviallyDestroyable() const override;
  bool canValueWitnessExtraInhabitantsUpTo(IRGenModule &IGM,
                                           unsigned index) const override;
  bool isSingleRetainablePointer() const override;
  std::optional<Size> fixedSize(IRGenModule &IGM) const override;
  bool isFixedSize(IRGenModule &IGM) const override;
  std::optional<Alignment> fixedAlignment(IRGenModule &IGM) const override;
  std::optional<uint32_t> fixedXICount(IRGenModule &IGM) const override;
  llvm::Value *isBitwiseTakable(IRGenFunction &IGF) const override;
  llvm::Type *getStorageType(IRGenFunction &IGF) const;

  void destroy(IRGenFunction &IGF, Address addr) const override;

  void assignWithCopy(IRGenFunction &IGF, Address dest,
                      Address src) const override;
  void assignWithTake(IRGenFunction &IGF, Address dest,
                      Address src) const override;

  void initWithCopy(IRGenFunction &IGF, Address dest,
                    Address src) const override;
  void initWithTake(IRGenFunction &IGF, Address dest,
                    Address src) const override;

  llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
                                       llvm::Value *numEmptyCases,
                                       Address addr) const override;

  void storeEnumTagSinglePayload(IRGenFunction &IGF, llvm::Value *tag,
                                 llvm::Value *numEmptyCases,
                                 Address enumAddr) const override;

  llvm::Constant *layoutString(IRGenModule &IGM,
                               GenericSignature genericSig) const override;
  bool refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
                      GenericSignature genericSig) const override;

  std::optional<const FixedTypeInfo *> getFixedTypeInfo() const override;

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const override;
#endif
};

class TypeLayoutCache {
  llvm::BumpPtrAllocator bumpAllocator;

  llvm::FoldingSet<ScalarTypeLayoutEntry> scalarEntries;
  llvm::FoldingSet<ArchetypeLayoutEntry> archetypeEntries;
  llvm::FoldingSet<AlignedGroupEntry> alignedGroupEntries;
  llvm::FoldingSet<EnumTypeLayoutEntry> enumEntries;
  llvm::FoldingSet<ResilientTypeLayoutEntry> resilientEntries;
  llvm::FoldingSet<TypeInfoBasedTypeLayoutEntry> typeInfoBasedEntries;

  TypeLayoutEntry emptyEntry;
public:
  ~TypeLayoutCache();
  ScalarTypeLayoutEntry *getOrCreateScalarEntry(const TypeInfo &ti,
                                                SILType representative,
                                                ScalarKind kind);

  ArchetypeLayoutEntry *getOrCreateArchetypeEntry(SILType archetype);

  AlignedGroupEntry *
  getOrCreateAlignedGroupEntry(const std::vector<TypeLayoutEntry *> &entries,
                               SILType ty, Alignment::int_type minimumAlignment,
                               const TypeInfo &ti);

  EnumTypeLayoutEntry *
  getOrCreateEnumEntry(unsigned numEmptyCase,
                       const std::vector<TypeLayoutEntry *> &nonEmptyCases,
                       SILType ty, const TypeInfo &ti);

  TypeInfoBasedTypeLayoutEntry *
  getOrCreateTypeInfoBasedEntry(const TypeInfo &ti, SILType representative);

  ResilientTypeLayoutEntry *getOrCreateResilientEntry(SILType ty);

  TypeLayoutEntry *getEmptyEntry();
};

} // namespace irgen
} // namespace swift
#endif