-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathExistential.cpp
474 lines (431 loc) · 18.1 KB
/
Existential.cpp
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
//===--- Existential.cpp - Functions analyzing existentials. -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
#include "swift/SILOptimizer/Utils/Existential.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/LocalArchetypeRequirementCollector.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "llvm/ADT/SmallPtrSet.h"
using namespace swift;
/// Determine InitExistential from global_addr.
/// %3 = global_addr @$P : $*SomeP
/// %4 = init_existential_addr %3 : $*SomeP, $SomeC
/// %5 = alloc_ref $SomeC
/// store %5 to %4 : $*SomeC
/// %8 = alloc_stack $SomeP
/// copy_addr %3 to [init] %8 : $*SomeP
/// %10 = apply %9(%3) : $@convention(thin) (@in_guaranteed SomeP)
/// Assumptions: Insn is a direct user of GAI (e.g., copy_addr or
/// apply pattern shown above) and that a valid init_existential_addr
/// value is returned only if it can prove that the value it
/// initializes is the same value at the use point.
static InitExistentialAddrInst *
findInitExistentialFromGlobalAddr(GlobalAddrInst *GAI, SILInstruction *Insn) {
/// Check for a single InitExistential usage for GAI and
/// a simple dominance check: both InitExistential and Insn are in
/// the same basic block and only one InitExistential
/// occurs between GAI and Insn.
llvm::SmallPtrSet<SILInstruction *, 8> IEUses;
for (auto *Use : GAI->getUses()) {
if (auto *InitExistential =
dyn_cast<InitExistentialAddrInst>(Use->getUser())) {
IEUses.insert(InitExistential);
}
}
/// No InitExistential found in the basic block.
if (IEUses.empty())
return nullptr;
/// Walk backwards from Insn instruction till the beginning of the basic block
/// looking for an InitExistential.
InitExistentialAddrInst *SingleIE = nullptr;
for (auto II = Insn->getIterator().getReverse(),
IE = Insn->getParent()->rend();
II != IE; ++II) {
if (!IEUses.count(&*II))
continue;
if (SingleIE)
return nullptr;
SingleIE = cast<InitExistentialAddrInst>(&*II);
}
return SingleIE;
}
/// Returns the instruction that initializes the given stack address. This is
/// currently either a init_existential_addr, unconditional_checked_cast_addr,
/// store, or copy_addr (if the instruction initializing the source of the copy
/// cannot be determined). Returns nullptr if the initializer does not dominate
/// the alloc_stack user \p ASIUser. If the value is copied from another stack
/// location, \p isCopied is set to true.
///
/// allocStackAddr may either itself be an AllocStackInst or an
/// InitEnumDataAddrInst that projects the value of an AllocStackInst.
static SILInstruction *getStackInitInst(SILValue allocStackAddr,
SILInstruction *ASIUser,
bool &isCopied) {
SILInstruction *SingleWrite = nullptr;
// Check that this alloc_stack is initialized only once.
for (auto Use : allocStackAddr->getUses()) {
auto *User = Use->getUser();
// Ignore instructions which don't write to the stack location.
// Also ignore ASIUser (only kicks in if ASIUser is the original apply).
if (isa<DeallocStackInst>(User) ||
DebugValueInst::hasAddrVal(User) ||
isa<DestroyAddrInst>(User) || isa<WitnessMethodInst>(User) ||
isa<DeinitExistentialAddrInst>(User) ||
OpenExistentialAddrInst::isRead(User) || User == ASIUser) {
continue;
}
if (auto *CAI = dyn_cast<CopyAddrInst>(User)) {
if (CAI->getDest() == allocStackAddr) {
if (SingleWrite)
return nullptr;
SingleWrite = CAI;
isCopied = true;
}
continue;
}
// An unconditional_checked_cast_addr also copies a value into this addr.
if (auto *UCCAI = dyn_cast<UnconditionalCheckedCastAddrInst>(User)) {
if (UCCAI->getDest() == allocStackAddr) {
if (SingleWrite)
return nullptr;
SingleWrite = UCCAI;
isCopied = true;
}
continue;
}
if (auto *store = dyn_cast<StoreInst>(User)) {
if (store->getDest() == allocStackAddr) {
if (SingleWrite)
return nullptr;
SingleWrite = store;
}
continue;
}
if (isa<InitExistentialAddrInst>(User)) {
if (SingleWrite)
return nullptr;
SingleWrite = User;
continue;
}
if (isa<ApplyInst>(User) || isa<TryApplyInst>(User)) {
// Ignore function calls which do not write to the stack location.
auto Conv = FullApplySite(User).getArgumentConvention(*Use);
if (Conv != SILArgumentConvention::Indirect_In &&
Conv != SILArgumentConvention::Indirect_In_Guaranteed)
return nullptr;
continue;
}
// Bail if there is any unknown (and potentially writing) instruction.
return nullptr;
}
if (!SingleWrite)
return nullptr;
// A very simple dominance check. As ASI is an operand of ASIUser,
// SingleWrite dominates ASIUser if it is in the same block as ASI or
// ASIUser. (SingleWrite can't occur after ASIUser because the address would
// be uninitialized on use).
//
// If allocStack holds an Optional, then ASI is an InitEnumDataAddrInst
// projection and not strictly an operand of ASIUser. We rely on the guarantee
// that this InitEnumDataAddrInst must occur before the InjectEnumAddrInst
// that was the source of the existential address.
SILBasicBlock *BB = SingleWrite->getParent();
if (BB != allocStackAddr->getParentBlock() && BB != ASIUser->getParent())
return nullptr;
if (auto *store = dyn_cast<StoreInst>(SingleWrite))
return store;
if (auto *IE = dyn_cast<InitExistentialAddrInst>(SingleWrite))
return IE;
if (auto *UCCA = dyn_cast<UnconditionalCheckedCastAddrInst>(SingleWrite)) {
assert(isCopied && "isCopied not set for a unconditional_checked_cast_addr");
return UCCA;
}
auto *CAI = cast<CopyAddrInst>(SingleWrite);
assert(isCopied && "isCopied not set for a copy_addr");
// Attempt to recurse to find a concrete type.
if (auto *ASI = dyn_cast<AllocStackInst>(CAI->getSrc()))
return getStackInitInst(ASI, CAI, isCopied);
// Peek through a stack location holding an Enum.
// %stack_adr = alloc_stack
// %data_adr = init_enum_data_addr %stk_adr
// %enum_adr = inject_enum_addr %stack_adr
// %copy_src = unchecked_take_enum_data_addr %enum_adr
// Replace %copy_src with %data_adr and recurse.
//
// TODO: a general Optional elimination sil-combine could
// supersede this check.
if (auto *UTEDAI = dyn_cast<UncheckedTakeEnumDataAddrInst>(CAI->getSrc())) {
if (InitEnumDataAddrInst *IEDAI = findInitAddressForTrivialEnum(UTEDAI))
return getStackInitInst(IEDAI, CAI, isCopied);
}
// Check if the CAISrc is a global_addr.
if (auto *GAI = dyn_cast<GlobalAddrInst>(CAI->getSrc()))
return findInitExistentialFromGlobalAddr(GAI, CAI);
// If the source of the copy cannot be determined, return the copy itself
// because the caller may have special handling for the source address.
return CAI;
}
/// Check if the given operand originates from a recognized OpenArchetype
/// instruction. If so, return the Opened, otherwise return nullptr.
OpenedArchetypeInfo::OpenedArchetypeInfo(Operand &use) {
SILValue openedVal = use.get();
SILInstruction *user = use.getUser();
if (auto *instance = dyn_cast<AllocStackInst>(openedVal)) {
// Handle:
// %opened = open_existential_addr
// %instance = alloc $opened
// <copy|store> %opened to %stack
// <opened_use> %instance
if (auto *initI = getStackInitInst(instance, user, isOpenedValueCopied)) {
// init_existential_addr isn't handled here because it isn't considered an
// "opened" archtype. init_existential_addr should be handled by
// ConcreteExistentialInfo.
if (auto *CAI = dyn_cast<CopyAddrInst>(initI))
openedVal = CAI->getSrc();
if (auto *store = dyn_cast<StoreInst>(initI))
openedVal = store->getSrc();
}
}
if (auto *Open = dyn_cast<OpenExistentialAddrInst>(openedVal)) {
OpenedArchetype = Open->getType().castTo<OpenedArchetypeType>();
OpenedArchetypeValue = Open;
ExistentialValue = Open->getOperand();
return;
}
if (auto *Open = dyn_cast<OpenExistentialRefInst>(openedVal)) {
OpenedArchetype = Open->getType().castTo<OpenedArchetypeType>();
OpenedArchetypeValue = Open;
ExistentialValue = Open->getOperand();
return;
}
if (auto *Open = dyn_cast<OpenExistentialMetatypeInst>(openedVal)) {
auto Ty = Open->getType().getASTType();
while (auto Metatype = dyn_cast<MetatypeType>(Ty))
Ty = Metatype.getInstanceType();
OpenedArchetype = cast<OpenedArchetypeType>(Ty);
OpenedArchetypeValue = Open;
ExistentialValue = Open->getOperand();
}
}
/// Initialize ExistentialSubs from the given conformance list, using the
/// already initialized ExistentialType and ConcreteType.
void ConcreteExistentialInfo::initializeSubstitutionMap(
ArrayRef<ProtocolConformanceRef> ExistentialConformances, SILModule *M) {
// Construct a single-generic-parameter substitution map directly to the
// ConcreteType with this existential's full list of conformances.
//
// NOTE: LocalArchetypeRequirementCollector generates the signature for passing an
// opened existential as a generic parameter. No opened archetypes are
// actually involved here--the API is only used as a convenient way to create
// a substitution map. Since opened archetypes have different conformances
// than their corresponding existential, ExistentialConformances needs to be
// filtered when using it with this (phony) generic signature.
auto &ctx = M->getASTContext();
LocalArchetypeRequirementCollector collector(ctx, CanGenericSignature());
collector.addOpenedExistential(ExistentialType);
auto ExistentialSig = buildGenericSignature(
ctx, collector.OuterSig, collector.Params, collector.Requirements,
/*allowInverses=*/true).getCanonicalSignature();
ExistentialSubs = SubstitutionMap::get(
ExistentialSig, [&](SubstitutableType *type) { return ConcreteType; },
[&](CanType /*depType*/, Type /*replaceType*/,
ProtocolDecl *proto) -> ProtocolConformanceRef {
// Directly providing ExistentialConformances to the SubstitutionMap will
// fail because of the mismatch between opened archetype conformance and
// existential value conformance. Instead, provide a conformance lookup
// function that pulls only the necessary conformances out of
// ExistentialConformances. This assumes that existential conformances
// are a superset of opened archetype conformances.
auto iter =
llvm::find_if(ExistentialConformances,
[&](const ProtocolConformanceRef &conformance) {
return conformance.getRequirement() == proto;
});
assert(iter != ExistentialConformances.end() && "missing conformance");
return *iter;
});
assert(isValid());
}
/// If the ConcreteType is an opened existential, also initialize
/// ConcreteTypeDef to the definition of that type.
void ConcreteExistentialInfo::initializeConcreteTypeDef(
SILInstruction *typeConversionInst) {
if (!isa<OpenedArchetypeType>(ConcreteType))
return;
assert(isValid());
// If the concrete type is another existential, we're "forwarding" an
// opened existential type, so we must keep track of the original
// defining instruction.
if (!typeConversionInst->getTypeDependentOperands().empty()) {
ConcreteTypeDef = cast<SingleValueInstruction>(
typeConversionInst->getTypeDependentOperands()[0].get());
return;
}
auto typeOperand =
cast<InitExistentialMetatypeInst>(typeConversionInst)->getOperand();
assert(typeOperand->getType().hasOpenedExistential()
&& "init_existential is supposed to have a typedef operand");
ConcreteTypeDef = cast<SingleValueInstruction>(typeOperand);
}
/// Construct this ConcreteExistentialInfo based on the given existential use.
///
/// Finds the init_existential, or an address with concrete type used to
/// initialize the given \p openedUse. If the value is copied
/// from another stack location, \p isCopied is set to true.
///
/// If successful, ConcreteExistentialInfo will be valid upon return, with the
/// following fields assigned:
/// - ExistentialType
/// - isCopied
/// - ConcreteType
/// - ConcreteValue
/// - ConcreteTypeDef
/// - ExistentialSubs
ConcreteExistentialInfo::ConcreteExistentialInfo(SILValue existential,
SILInstruction *user) {
if (existential->getType().isAddress()) {
auto *ASI = dyn_cast<AllocStackInst>(existential);
if (!ASI)
return;
SILInstruction *stackInit =
getStackInitInst(ASI, user, isConcreteValueCopied);
if (!stackInit)
return;
if (auto *IE = dyn_cast<InitExistentialAddrInst>(stackInit)) {
ExistentialType = IE->getOperand()->getType().getASTType();
ConcreteType = IE->getFormalConcreteType();
ConcreteValue = IE;
initializeSubstitutionMap(IE->getConformances(), &IE->getModule());
initializeConcreteTypeDef(IE);
return;
}
// TODO: Once we have a way to introduce more constrained archetypes, handle
// any unconditional_checked_cast that wasn't already statically eliminated.
//
// Unexpected stack write.
return;
}
if (auto *IER = dyn_cast<InitExistentialRefInst>(existential)) {
ExistentialType = IER->getType().getASTType();
ConcreteType = IER->getFormalConcreteType();
ConcreteValue = IER->getOperand();
initializeSubstitutionMap(IER->getConformances(), &IER->getModule());
initializeConcreteTypeDef(IER);
return;
}
if (auto *IEM = dyn_cast<InitExistentialMetatypeInst>(existential)) {
ExistentialType = IEM->getType().getASTType();
ConcreteValue = IEM->getOperand();
ConcreteType = ConcreteValue->getType().getASTType();
while (auto InstanceType =
dyn_cast<ExistentialMetatypeType>(ExistentialType)) {
ExistentialType = InstanceType.getInstanceType();
ConcreteType = cast<MetatypeType>(ConcreteType).getInstanceType();
}
initializeSubstitutionMap(IEM->getConformances(), &IEM->getModule());
initializeConcreteTypeDef(IEM);
return;
}
// Unrecognized opened existential producer.
return;
}
/// Initialize a ConcreteExistentialInfo based on a concrete type and protocol
/// declaration that has already been computed via whole module type
/// inference. A cast instruction will be introduced to produce the concrete
/// value from the opened value.
///
/// The simpler constructor taking only the existential value is preferred
/// because it generates simpler SIL and does not require an extra
/// cast. However, if that constructor fails to produce a valid
/// ConcreteExistentialInfo, this constructor may succeed because it doesn't
/// needs to rediscover the whole-module inferred ConcreteTypeCandidate.
ConcreteExistentialInfo::ConcreteExistentialInfo(SILValue existential,
SILInstruction *user,
CanType ConcreteTypeCandidate,
ProtocolDecl *Protocol) {
SILModule *M = existential->getModule();
// We have the open_existential; we still need the conformance.
auto ConformanceRef = checkConformance(ConcreteTypeCandidate, Protocol);
if (ConformanceRef.isInvalid())
return;
// Assert that the conformance is complete.
auto *ConcreteConformance = ConformanceRef.getConcrete();
assert(ConcreteConformance->isComplete());
ConcreteType = ConcreteTypeCandidate;
// There is no ConcreteValue in this case.
/// Determine the ExistentialConformances and SubstitutionMap.
ExistentialType = Protocol->getDeclaredInterfaceType()->getCanonicalType();
initializeSubstitutionMap(ProtocolConformanceRef(ConcreteConformance), M);
assert(isValid());
}
ConcreteOpenedExistentialInfo::ConcreteOpenedExistentialInfo(Operand &use)
: OAI(use) {
if (!OAI.isValid())
return;
CEI.emplace(OAI.ExistentialValue, OAI.OpenedArchetypeValue);
if (!CEI->isValid()) {
CEI.reset();
return;
}
CEI->isConcreteValueCopied |= OAI.isOpenedValueCopied;
}
ConcreteOpenedExistentialInfo::ConcreteOpenedExistentialInfo(
Operand &use, CanType concreteType, ProtocolDecl *protocol)
: OAI(use) {
if (!OAI.isValid())
return;
CEI.emplace(OAI.ExistentialValue, OAI.OpenedArchetypeValue, concreteType,
protocol);
if (!CEI->isValid()) {
CEI.reset();
return;
}
CEI->isConcreteValueCopied |= OAI.isOpenedValueCopied;
}
void LLVM_ATTRIBUTE_USED OpenedArchetypeInfo::dump() const {
if (!isValid()) {
llvm::dbgs() << "invalid OpenedArchetypeInfo\n";
return;
}
llvm::dbgs() << "OpendArchetype: ";
OpenedArchetype->dump(llvm::dbgs());
llvm::dbgs() << "OpendArchetypeValue: ";
OpenedArchetypeValue->dump();
llvm::dbgs() << (isOpenedValueCopied ? "copied " : "") << "ExistentialValue: ";
ExistentialValue->dump();
}
void LLVM_ATTRIBUTE_USED ConcreteExistentialInfo::dump() const {
llvm::dbgs() << "ExistentialType: ";
ExistentialType->dump(llvm::dbgs());
llvm::dbgs() << "ConcreteType: ";
ConcreteType->dump(llvm::dbgs());
llvm::dbgs() << (isConcreteValueCopied ? "copied " : "") << "ConcreteValue: ";
ConcreteValue->dump();
if (ConcreteTypeDef) {
llvm::dbgs() << "ConcreteTypeDef: ";
ConcreteTypeDef->dump();
}
ExistentialSubs.dump(llvm::dbgs());
llvm::dbgs() << '\n';
}
void LLVM_ATTRIBUTE_USED ConcreteOpenedExistentialInfo::dump() const {
OAI.dump();
if (CEI) {
CEI->dump();
} else {
llvm::dbgs() << "no ConcreteExistentialInfo\n";
}
}