16
16
#include " swift/SIL/SILDebugScope.h"
17
17
#include " swift/SIL/SILFunction.h"
18
18
#include " swift/SIL/SILModule.h"
19
+ #include " swift/SIL/SILOpenedArchetypesTracker.h"
19
20
#include " llvm/ADT/PointerUnion.h"
20
21
#include " llvm/ADT/StringExtras.h"
21
22
@@ -40,6 +41,16 @@ class SILBuilder {
40
41
// / recorded in this list.
41
42
SmallVectorImpl<SILInstruction *> *InsertedInstrs = nullptr ;
42
43
44
+ // / An immutable view on the set of available opened archetypes.
45
+ // / It is passed down to SILInstruction constructors and create
46
+ // / methods.
47
+ SILOpenedArchetypesState OpenedArchetypes;
48
+
49
+ // / Maps opened archetypes to their definitions. If provided,
50
+ // / can be used by the builder. It is supposed to be used
51
+ // / only by SILGen or SIL deserializers.
52
+ SILOpenedArchetypesTracker *OpenedArchetypesTracker = nullptr ;
53
+
43
54
public:
44
55
SILBuilder (SILFunction &F) : F(F), BB(0 ) {}
45
56
@@ -75,6 +86,19 @@ class SILBuilder {
75
86
return F.getModule ().getTypeLowering (T);
76
87
}
77
88
89
+ void setOpenedArchetypesTracker (SILOpenedArchetypesTracker *Tracker) {
90
+ this ->OpenedArchetypesTracker = Tracker;
91
+ this ->OpenedArchetypes .setOpenedArchetypesTracker (OpenedArchetypesTracker);
92
+ }
93
+
94
+ SILOpenedArchetypesTracker *getOpenedArchetypesTracker () const {
95
+ return OpenedArchetypesTracker;
96
+ }
97
+
98
+ SILOpenedArchetypesState &getOpenedArchetypes () {
99
+ return OpenedArchetypes;
100
+ }
101
+
78
102
void setCurrentDebugScope (const SILDebugScope *DS) { CurDebugScope = DS; }
79
103
const SILDebugScope *getCurrentDebugScope () const { return CurDebugScope; }
80
104
@@ -214,54 +238,57 @@ class SILBuilder {
214
238
SILDebugVariable Var = SILDebugVariable()) {
215
239
Loc.markAsPrologue ();
216
240
return insert (AllocStackInst::create (getSILDebugLocation (Loc),
217
- elementType, F, Var));
241
+ elementType, F, OpenedArchetypes,
242
+ Var));
218
243
}
219
244
220
245
AllocRefInst *createAllocRef (SILLocation Loc, SILType elementType, bool objc,
221
246
bool canAllocOnStack) {
222
247
// AllocRefInsts expand to function calls and can therefore not be
223
248
// counted towards the function prologue.
224
249
assert (!Loc.isInPrologue ());
225
- return insert (new (F.getModule ()) AllocRefInst (
226
- getSILDebugLocation (Loc), elementType, F, objc, canAllocOnStack));
250
+ return insert (AllocRefInst::create (getSILDebugLocation (Loc), elementType, F,
251
+ objc, canAllocOnStack,
252
+ OpenedArchetypes));
227
253
}
228
254
229
255
AllocRefDynamicInst *createAllocRefDynamic (SILLocation Loc, SILValue operand,
230
256
SILType type, bool objc) {
231
257
// AllocRefDynamicInsts expand to function calls and can therefore
232
258
// not be counted towards the function prologue.
233
259
assert (!Loc.isInPrologue ());
234
- return insert (new (F. getModule ()) AllocRefDynamicInst (
235
- getSILDebugLocation (Loc), operand, type, objc));
260
+ return insert (AllocRefDynamicInst::create ( getSILDebugLocation (Loc), operand,
261
+ type, objc, F, OpenedArchetypes ));
236
262
}
237
263
238
264
AllocValueBufferInst *
239
265
createAllocValueBuffer (SILLocation Loc, SILType valueType, SILValue operand) {
240
- return insert (new (F. getModule ()) AllocValueBufferInst (
241
- getSILDebugLocation (Loc), valueType, operand));
266
+ return insert (AllocValueBufferInst::create (
267
+ getSILDebugLocation (Loc), valueType, operand, F, OpenedArchetypes ));
242
268
}
243
269
244
270
AllocBoxInst *createAllocBox (SILLocation Loc, SILType ElementType,
245
271
SILDebugVariable Var = SILDebugVariable()) {
246
272
Loc.markAsPrologue ();
247
- return insert (
248
- AllocBoxInst::create ( getSILDebugLocation (Loc), ElementType, F , Var));
273
+ return insert (AllocBoxInst::create ( getSILDebugLocation (Loc), ElementType, F,
274
+ OpenedArchetypes , Var));
249
275
}
250
276
251
277
AllocExistentialBoxInst *
252
278
createAllocExistentialBox (SILLocation Loc, SILType ExistentialType,
253
279
CanType ConcreteType,
254
280
ArrayRef<ProtocolConformanceRef> Conformances) {
255
281
return insert (AllocExistentialBoxInst::create (
256
- getSILDebugLocation (Loc), ExistentialType, ConcreteType,
257
- Conformances, &F ));
282
+ getSILDebugLocation (Loc), ExistentialType, ConcreteType, Conformances,
283
+ &F, OpenedArchetypes ));
258
284
}
259
285
260
286
ApplyInst *createApply (SILLocation Loc, SILValue Fn, SILType SubstFnTy,
261
287
SILType Result, ArrayRef<Substitution> Subs,
262
288
ArrayRef<SILValue> Args, bool isNonThrowing) {
263
289
return insert (ApplyInst::create (getSILDebugLocation (Loc), Fn, SubstFnTy,
264
- Result, Subs, Args, isNonThrowing, F));
290
+ Result, Subs, Args, isNonThrowing, F,
291
+ OpenedArchetypes));
265
292
}
266
293
267
294
ApplyInst *createApply (SILLocation Loc, SILValue Fn, ArrayRef<SILValue> Args,
@@ -278,16 +305,18 @@ class SILBuilder {
278
305
SILBasicBlock *errorBB) {
279
306
return insertTerminator (TryApplyInst::create (getSILDebugLocation (Loc),
280
307
fn, substFnTy, subs, args,
281
- normalBB, errorBB, F));
308
+ normalBB, errorBB, F,
309
+ OpenedArchetypes));
282
310
}
283
311
284
312
PartialApplyInst *createPartialApply (SILLocation Loc, SILValue Fn,
285
313
SILType SubstFnTy,
286
314
ArrayRef<Substitution> Subs,
287
315
ArrayRef<SILValue> Args,
288
316
SILType ClosureTy) {
289
- return insert (PartialApplyInst::create (
290
- getSILDebugLocation (Loc), Fn, SubstFnTy, Subs, Args, ClosureTy, F));
317
+ return insert (PartialApplyInst::create (getSILDebugLocation (Loc), Fn,
318
+ SubstFnTy, Subs, Args, ClosureTy, F,
319
+ OpenedArchetypes));
291
320
}
292
321
293
322
BuiltinInst *createBuiltin (SILLocation Loc, Identifier Name, SILType ResultTy,
@@ -526,8 +555,8 @@ class SILBuilder {
526
555
527
556
UncheckedRefCastInst *createUncheckedRefCast (SILLocation Loc, SILValue Op,
528
557
SILType Ty) {
529
- return insert (new (F. getModule ()) UncheckedRefCastInst (
530
- getSILDebugLocation (Loc), Op, Ty ));
558
+ return insert (UncheckedRefCastInst::create ( getSILDebugLocation (Loc), Op, Ty,
559
+ F, OpenedArchetypes ));
531
560
}
532
561
533
562
UncheckedRefCastAddrInst *
@@ -539,20 +568,20 @@ class SILBuilder {
539
568
540
569
UncheckedAddrCastInst *createUncheckedAddrCast (SILLocation Loc, SILValue Op,
541
570
SILType Ty) {
542
- return insert (new (F. getModule ()) UncheckedAddrCastInst (
543
- getSILDebugLocation (Loc), Op, Ty ));
571
+ return insert (UncheckedAddrCastInst::create ( getSILDebugLocation (Loc), Op,
572
+ Ty, F, OpenedArchetypes ));
544
573
}
545
574
546
575
UncheckedTrivialBitCastInst *
547
576
createUncheckedTrivialBitCast (SILLocation Loc, SILValue Op, SILType Ty) {
548
- return insert (new (F. getModule ()) UncheckedTrivialBitCastInst (
549
- getSILDebugLocation (Loc), Op, Ty));
577
+ return insert (UncheckedTrivialBitCastInst::create (
578
+ getSILDebugLocation (Loc), Op, Ty, F, OpenedArchetypes ));
550
579
}
551
580
552
581
UncheckedBitwiseCastInst *
553
582
createUncheckedBitwiseCast (SILLocation Loc, SILValue Op, SILType Ty) {
554
- return insert (new (F. getModule ()) UncheckedBitwiseCastInst (
555
- getSILDebugLocation (Loc), Op, Ty ));
583
+ return insert (UncheckedBitwiseCastInst::create ( getSILDebugLocation (Loc), Op,
584
+ Ty, F, OpenedArchetypes ));
556
585
}
557
586
558
587
RefToBridgeObjectInst *createRefToBridgeObject (SILLocation Loc, SILValue Ref,
@@ -648,8 +677,8 @@ class SILBuilder {
648
677
649
678
UnconditionalCheckedCastInst *
650
679
createUnconditionalCheckedCast (SILLocation Loc, SILValue op, SILType destTy) {
651
- return insert (new (F. getModule ()) UnconditionalCheckedCastInst (
652
- getSILDebugLocation (Loc), op, destTy));
680
+ return insert (UnconditionalCheckedCastInst::create (
681
+ getSILDebugLocation (Loc), op, destTy, F, OpenedArchetypes ));
653
682
}
654
683
655
684
UnconditionalCheckedCastAddrInst *createUnconditionalCheckedCastAddr (
@@ -896,11 +925,10 @@ class SILBuilder {
896
925
WitnessMethodInst *createWitnessMethod (SILLocation Loc, CanType LookupTy,
897
926
ProtocolConformanceRef Conformance,
898
927
SILDeclRef Member, SILType MethodTy,
899
- SILValue OptionalOpenedExistential,
900
928
bool Volatile = false ) {
901
929
return insert (WitnessMethodInst::create (
902
930
getSILDebugLocation (Loc), LookupTy, Conformance, Member, MethodTy,
903
- &F, OptionalOpenedExistential , Volatile));
931
+ &F, OpenedArchetypes , Volatile));
904
932
}
905
933
906
934
DynamicMethodInst *createDynamicMethod (SILLocation Loc, SILValue Operand,
@@ -912,27 +940,39 @@ class SILBuilder {
912
940
913
941
OpenExistentialAddrInst *
914
942
createOpenExistentialAddr (SILLocation Loc, SILValue Operand, SILType SelfTy) {
915
- return insert (new (F.getModule ()) OpenExistentialAddrInst (
943
+ auto *I = insert (new (F.getModule ()) OpenExistentialAddrInst (
916
944
getSILDebugLocation (Loc), Operand, SelfTy));
945
+ if (OpenedArchetypesTracker)
946
+ OpenedArchetypesTracker->registerOpenedArchetypes (I);
947
+ return I;
917
948
}
918
949
919
950
OpenExistentialMetatypeInst *createOpenExistentialMetatype (SILLocation Loc,
920
951
SILValue operand,
921
952
SILType selfTy) {
922
- return insert (new (F.getModule ()) OpenExistentialMetatypeInst (
953
+ auto *I = insert (new (F.getModule ()) OpenExistentialMetatypeInst (
923
954
getSILDebugLocation (Loc), operand, selfTy));
955
+ if (OpenedArchetypesTracker)
956
+ OpenedArchetypesTracker->registerOpenedArchetypes (I);
957
+ return I;
924
958
}
925
959
926
960
OpenExistentialRefInst *
927
961
createOpenExistentialRef (SILLocation Loc, SILValue Operand, SILType Ty) {
928
- return insert (new (F.getModule ()) OpenExistentialRefInst (
962
+ auto *I = insert (new (F.getModule ()) OpenExistentialRefInst (
929
963
getSILDebugLocation (Loc), Operand, Ty));
964
+ if (OpenedArchetypesTracker)
965
+ OpenedArchetypesTracker->registerOpenedArchetypes (I);
966
+ return I;
930
967
}
931
968
932
969
OpenExistentialBoxInst *
933
970
createOpenExistentialBox (SILLocation Loc, SILValue Operand, SILType Ty) {
934
- return insert (new (F.getModule ()) OpenExistentialBoxInst (
971
+ auto *I = insert (new (F.getModule ()) OpenExistentialBoxInst (
935
972
getSILDebugLocation (Loc), Operand, Ty));
973
+ if (OpenedArchetypesTracker)
974
+ OpenedArchetypesTracker->registerOpenedArchetypes (I);
975
+ return I;
936
976
}
937
977
938
978
InitExistentialAddrInst *
@@ -942,25 +982,25 @@ class SILBuilder {
942
982
ArrayRef<ProtocolConformanceRef> Conformances) {
943
983
return insert (InitExistentialAddrInst::create (
944
984
getSILDebugLocation (Loc), Existential, FormalConcreteType,
945
- LoweredConcreteType, Conformances, &F));
985
+ LoweredConcreteType, Conformances, &F, OpenedArchetypes ));
946
986
}
947
987
948
988
InitExistentialMetatypeInst *
949
989
createInitExistentialMetatype (SILLocation Loc, SILValue metatype,
950
990
SILType existentialType,
951
991
ArrayRef<ProtocolConformanceRef> conformances) {
952
992
return insert (InitExistentialMetatypeInst::create (
953
- getSILDebugLocation (Loc), existentialType, metatype, conformances,
954
- &F ));
993
+ getSILDebugLocation (Loc), existentialType, metatype, conformances, &F,
994
+ OpenedArchetypes ));
955
995
}
956
996
957
997
InitExistentialRefInst *
958
998
createInitExistentialRef (SILLocation Loc, SILType ExistentialType,
959
999
CanType FormalConcreteType, SILValue Concrete,
960
1000
ArrayRef<ProtocolConformanceRef> Conformances) {
961
1001
return insert (InitExistentialRefInst::create (
962
- getSILDebugLocation (Loc), ExistentialType, FormalConcreteType,
963
- Concrete, Conformances, &F));
1002
+ getSILDebugLocation (Loc), ExistentialType, FormalConcreteType, Concrete,
1003
+ Conformances, &F, OpenedArchetypes ));
964
1004
}
965
1005
966
1006
DeinitExistentialAddrInst *createDeinitExistentialAddr (SILLocation Loc,
@@ -992,8 +1032,8 @@ class SILBuilder {
992
1032
}
993
1033
994
1034
MetatypeInst *createMetatype (SILLocation Loc, SILType Metatype) {
995
- return insert (new (F. getModule ())
996
- MetatypeInst ( getSILDebugLocation (Loc), Metatype ));
1035
+ return insert (MetatypeInst::create ( getSILDebugLocation (Loc), Metatype,
1036
+ &F, OpenedArchetypes ));
997
1037
}
998
1038
999
1039
ObjCMetatypeToObjectInst *
@@ -1306,9 +1346,9 @@ class SILBuilder {
1306
1346
SILValue op, SILType destTy,
1307
1347
SILBasicBlock *successBB,
1308
1348
SILBasicBlock *failureBB) {
1309
- return insertTerminator (new (F. getModule ()) CheckedCastBranchInst (
1310
- getSILDebugLocation (Loc), isExact, op, destTy, successBB,
1311
- failureBB ));
1349
+ return insertTerminator (CheckedCastBranchInst::create (
1350
+ getSILDebugLocation (Loc), isExact, op, destTy, successBB, failureBB, F,
1351
+ OpenedArchetypes ));
1312
1352
}
1313
1353
1314
1354
CheckedCastAddrBranchInst *
0 commit comments