|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
| 13 | +#include "swift/SIL/SILBuilder.h" |
13 | 14 | #include "swift/SIL/SILOpenedArchetypesTracker.h"
|
14 | 15 |
|
15 | 16 | using namespace swift;
|
16 | 17 |
|
17 | 18 | void SILOpenedArchetypesTracker::addOpenedArchetypeDef(CanArchetypeType archetype,
|
18 | 19 | SILValue Def) {
|
19 | 20 | auto OldDef = getOpenedArchetypeDef(archetype);
|
20 |
| - if (OldDef && isa<GlobalAddrInst>(OldDef)) { |
21 |
| - // It is a forward definition created during deserialization. |
22 |
| - // Replace it with the real definition now. |
23 |
| - OldDef->replaceAllUsesWith(Def); |
24 |
| - OldDef = SILValue(); |
| 21 | + if (OldDef) { |
| 22 | + if (auto *OldI = dyn_cast<GlobalAddrInst>(OldDef)) { |
| 23 | + // It is a forward definition created during deserialization. |
| 24 | + // Replace it with the real definition now. |
| 25 | + OldDef->replaceAllUsesWith(Def); |
| 26 | + // Remove the placeholder instruction. |
| 27 | + OldI->eraseFromParent(); |
| 28 | + OldDef = SILValue(); |
| 29 | + } |
25 | 30 | }
|
26 | 31 | assert(!OldDef &&
|
27 | 32 | "There can be only one definition of an opened archetype");
|
@@ -56,11 +61,22 @@ bool SILOpenedArchetypesTracker::registerUsedOpenedArchetypes(CanType Ty) {
|
56 | 61 | if (getOpenedArchetypeDef(archetypeTy))
|
57 | 62 | return;
|
58 | 63 |
|
59 |
| - auto &SILMod = this->getFunction().getModule(); |
| 64 | + auto *CurF = const_cast<SILFunction *>(&this->getFunction()); |
| 65 | + auto &SILMod = CurF->getModule(); |
60 | 66 | // Create a placeholder representing a forward definition.
|
61 |
| - auto Placeholder = new (SILMod) |
62 |
| - GlobalAddrInst(SILDebugLocation(), |
63 |
| - SILMod.Types.getLoweredType(archetypeTy)); |
| 67 | + // Add the placeholder at the beginning of the entry block. |
| 68 | + SILValue Placeholder; |
| 69 | + if (!CurF->getEntryBlock()->empty()) { |
| 70 | + SILBuilder B(CurF->getEntryBlock()->begin()); |
| 71 | + Placeholder = |
| 72 | + B.createGlobalAddr(ArtificialUnreachableLocation(), |
| 73 | + SILMod.Types.getLoweredType(archetypeTy)); |
| 74 | + } else { |
| 75 | + SILBuilder B(CurF->getEntryBlock()); |
| 76 | + Placeholder = |
| 77 | + B.createGlobalAddr(ArtificialUnreachableLocation(), |
| 78 | + SILMod.Types.getLoweredType(archetypeTy)); |
| 79 | + } |
64 | 80 | // Make it available to SILBuilder, so that instructions using this
|
65 | 81 | // archetype can be constructed.
|
66 | 82 | addOpenedArchetypeDef(archetypeTy, Placeholder);
|
|
0 commit comments