|
| 1 | +//===--- Existential.h - Existential related Analyses. -------*- C++ //-*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_SILOPTIMIZER_UTILS_EXISTENTIAL_H |
| 14 | +#define SWIFT_SILOPTIMIZER_UTILS_EXISTENTIAL_H |
| 15 | + |
| 16 | +#include "swift/AST/SubstitutionMap.h" |
| 17 | +#include "swift/SIL/SILInstruction.h" |
| 18 | + |
| 19 | +namespace swift { |
| 20 | + |
| 21 | +/// Find InitExistential from global_addr and copy_addr. |
| 22 | +SILValue findInitExistentialFromGlobalAddr(GlobalAddrInst *GAI, |
| 23 | + CopyAddrInst *CAI); |
| 24 | + |
| 25 | +/// Returns the address of an object with which the stack location \p ASI is |
| 26 | +/// initialized. This is either a init_existential_addr or the destination of a |
| 27 | +/// copy_addr. Returns a null value if the address does not dominate the |
| 28 | +/// alloc_stack user \p ASIUser. |
| 29 | +/// If the value is copied from another stack location, \p isCopied is set to |
| 30 | +/// true. |
| 31 | +SILValue getAddressOfStackInit(AllocStackInst *ASI, SILInstruction *ASIUser, |
| 32 | + bool &isCopied); |
| 33 | + |
| 34 | +/// Find the init_existential, which could be used to determine a concrete |
| 35 | +/// type of the value used by \p openedUse. |
| 36 | +/// If the value is copied from another stack location, \p isCopied is set to |
| 37 | +/// true. |
| 38 | +/// |
| 39 | +/// FIXME: replace all uses of this with ConcreteExistentialInfo. |
| 40 | +SILInstruction *findInitExistential(Operand &openedUse, |
| 41 | + ArchetypeType *&OpenedArchetype, |
| 42 | + SILValue &OpenedArchetypeDef, |
| 43 | + bool &isCopied); |
| 44 | + |
| 45 | +/// Record conformance and concrete type info derived from an init_existential |
| 46 | +/// value that is reopened before it's use. This is useful for finding the |
| 47 | +/// concrete type of an apply's self argument. For example, an important pattern |
| 48 | +/// for a class existential is: |
| 49 | +/// |
| 50 | +/// %e = init_existential_ref %c : $C : $C, $P & Q |
| 51 | +/// %o = open_existential_ref %e : $P & Q to $@opened("PQ") P & Q |
| 52 | +/// %r = apply %f<@opened("PQ") P & Q>(%o) |
| 53 | +/// : $@convention(method) <τ_0_0 where τ_0_0 : P, τ_0_0 : Q> |
| 54 | +/// (@guaranteed τ_0_0) -> @owned τ_0_0 |
| 55 | +struct ConcreteExistentialInfo { |
| 56 | + // The opened type passed as self. `$@opened("PQ")` above. |
| 57 | + // This is also the replacement type of the method's Self type. |
| 58 | + ArchetypeType *OpenedArchetype = nullptr; |
| 59 | + // The definition of the OpenedArchetype. |
| 60 | + SILValue OpenedArchetypeDef; |
| 61 | + // True if the openedValue is copied from another stack location |
| 62 | + bool isCopied; |
| 63 | + // The init_existential instruction that produces the opened existential. |
| 64 | + SILInstruction *InitExistential = nullptr; |
| 65 | + // The existential type of the self argument before it is opened, |
| 66 | + // produced by an init_existential. |
| 67 | + CanType ExistentialType; |
| 68 | + // The conformances required by the init_existential. `$C : $P & $Q` above. |
| 69 | + ArrayRef<ProtocolConformanceRef> ExistentialConformances; |
| 70 | + // The concrete type of self from the init_existential. `$C` above. |
| 71 | + CanType ConcreteType; |
| 72 | + // When ConcreteType is itself an opened existential, record the type |
| 73 | + // definition. May be nullptr for a valid AppliedConcreteType. |
| 74 | + SingleValueInstruction *ConcreteTypeDef = nullptr; |
| 75 | + // The Substitution map derived from the init_existential. |
| 76 | + // This maps a single generic parameter to the replacement ConcreteType |
| 77 | + // and includes the full list of existential conformances. |
| 78 | + // signature: <P & Q>, replacement: $C : conformances: [$P, $Q] |
| 79 | + SubstitutionMap ExistentialSubs; |
| 80 | + // The value of concrete type used to initialize the existential. `%c` above. |
| 81 | + SILValue ConcreteValue; |
| 82 | + |
| 83 | + // Search for a recognized pattern in which the given value is an opened |
| 84 | + // existential that was previously initialized to a concrete type. |
| 85 | + // Constructs a valid ConcreteExistentialInfo object if successfull. |
| 86 | + ConcreteExistentialInfo(Operand &openedUse); |
| 87 | + |
| 88 | + ConcreteExistentialInfo(ConcreteExistentialInfo &) = delete; |
| 89 | + |
| 90 | + bool isValid() const { |
| 91 | + return OpenedArchetype && OpenedArchetypeDef && InitExistential |
| 92 | + && ConcreteType && !ExistentialSubs.empty() && ConcreteValue; |
| 93 | + } |
| 94 | + |
| 95 | + // Do a conformance lookup on ConcreteType with the given requirement, P. If P |
| 96 | + // is satisfiable based on the existential's conformance, return the new |
| 97 | + // conformance on P. Otherwise return None. |
| 98 | + Optional<ProtocolConformanceRef> |
| 99 | + lookupExistentialConformance(ProtocolDecl *P) const { |
| 100 | + CanType selfTy = P->getSelfInterfaceType()->getCanonicalType(); |
| 101 | + return ExistentialSubs.lookupConformance(selfTy, P); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} // end namespace swift |
| 106 | + |
| 107 | +#endif |
0 commit comments