|
| 1 | +//===--- ScopedAddressUtils.cpp -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 | +#include "swift/SIL/ScopedAddressUtils.h" |
| 14 | +#include "swift/SIL/OwnershipUtils.h" |
| 15 | +#include "swift/SIL/PrunedLiveness.h" |
| 16 | +#include "swift/SIL/SILArgument.h" |
| 17 | +#include "swift/SIL/SILBuilder.h" |
| 18 | +#include "swift/SIL/SILInstruction.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | + |
| 22 | +void ScopedAddressValueKind::print(llvm::raw_ostream &os) const { |
| 23 | + switch (value) { |
| 24 | + case ScopedAddressValueKind::Invalid: |
| 25 | + llvm_unreachable("Using invalid case?!"); |
| 26 | + case ScopedAddressValueKind::StoreBorrow: |
| 27 | + os << "StoreBorrow"; |
| 28 | + return; |
| 29 | + case ScopedAddressValueKind::BeginAccess: |
| 30 | + os << "BeginAccess"; |
| 31 | + return; |
| 32 | + } |
| 33 | + llvm_unreachable("Covered switch isn't covered?!"); |
| 34 | +} |
| 35 | + |
| 36 | +llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os, |
| 37 | + ScopedAddressValueKind kind) { |
| 38 | + kind.print(os); |
| 39 | + return os; |
| 40 | +} |
| 41 | + |
| 42 | +bool ScopedAddressValue::isScopeEndingUse(Operand *op) const { |
| 43 | + switch (kind) { |
| 44 | + case ScopedAddressValueKind::Invalid: |
| 45 | + llvm_unreachable("Using invalid case?!"); |
| 46 | + case ScopedAddressValueKind::StoreBorrow: { |
| 47 | + if (auto *endBorrow = dyn_cast<EndBorrowInst>(op->getUser())) { |
| 48 | + return endBorrow->getOperand() == value; |
| 49 | + } |
| 50 | + return false; |
| 51 | + } |
| 52 | + case ScopedAddressValueKind::BeginAccess: { |
| 53 | + if (auto *endAccess = dyn_cast<EndAccessInst>(op->getUser())) { |
| 54 | + return endAccess->getOperand() == value; |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +bool ScopedAddressValue::visitScopeEndingUses( |
| 62 | + function_ref<bool(Operand *)> visitor) const { |
| 63 | + switch (kind) { |
| 64 | + case ScopedAddressValueKind::Invalid: |
| 65 | + llvm_unreachable("Using invalid case?!"); |
| 66 | + case ScopedAddressValueKind::StoreBorrow: { |
| 67 | + for (auto *use : value->getUses()) { |
| 68 | + if (isa<EndBorrowInst>(use->getUser())) { |
| 69 | + if (!visitor(use)) |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | + case ScopedAddressValueKind::BeginAccess: { |
| 76 | + for (auto *use : value->getUses()) { |
| 77 | + if (isa<EndAccessInst>(use->getUser())) { |
| 78 | + if (!visitor(use)) |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +bool ScopedAddressValue::computeLiveness(PrunedLiveness &liveness) const { |
| 88 | + auto addressKind = findTransitiveUsesForAddress(value); |
| 89 | + if (addressKind != AddressUseKind::NonEscaping) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + liveness.initializeDefBlock(value->getParentBlock()); |
| 94 | + visitScopeEndingUses([&](Operand *endOp) { |
| 95 | + liveness.updateForUse(endOp->getUser(), /* isLifetimeEnding */ true); |
| 96 | + return true; |
| 97 | + }); |
| 98 | + return true; |
| 99 | +} |
| 100 | + |
| 101 | +void ScopedAddressValue::createScopeEnd(SILBasicBlock::iterator insertPt, |
| 102 | + SILLocation loc) const { |
| 103 | + switch (kind) { |
| 104 | + case ScopedAddressValueKind::StoreBorrow: { |
| 105 | + SILBuilderWithScope(insertPt).createEndBorrow(loc, value); |
| 106 | + return; |
| 107 | + } |
| 108 | + case ScopedAddressValueKind::BeginAccess: { |
| 109 | + SILBuilderWithScope(insertPt).createEndAccess(loc, value, false); |
| 110 | + return; |
| 111 | + } |
| 112 | + case ScopedAddressValueKind::Invalid: |
| 113 | + llvm_unreachable("Using invalid case?!"); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void ScopedAddressValue::endScopeAtLivenessBoundary( |
| 118 | + PrunedLiveness *liveness) const { |
| 119 | + // If no users exist, create scope ending instruction immediately after the |
| 120 | + // scoped address value. |
| 121 | + if (liveness->empty()) { |
| 122 | + createScopeEnd(value->getNextInstruction()->getIterator(), |
| 123 | + RegularLocation::getAutoGeneratedLocation()); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + PrunedLivenessBoundary scopedAddressBoundary; |
| 128 | + scopedAddressBoundary.compute(*liveness); |
| 129 | + // Go over the boundary and create scope ending instructions. |
| 130 | + scopedAddressBoundary.visitInsertionPoints( |
| 131 | + [&](SILBasicBlock::iterator insertPt) { |
| 132 | + createScopeEnd(insertPt, RegularLocation::getAutoGeneratedLocation()); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +bool swift::hasOtherStoreBorrowsInLifetime(StoreBorrowInst *storeBorrow, |
| 137 | + PrunedLiveness *liveness, |
| 138 | + DeadEndBlocks *deadEndBlocks) { |
| 139 | + SmallVector<StoreBorrowInst *, 4> otherStoreBorrows; |
| 140 | + // Collect all other store_borrows to the destination of \p storeBorrow |
| 141 | + for (auto *destUse : storeBorrow->getDest()->getUses()) { |
| 142 | + if (auto *user = dyn_cast<StoreBorrowInst>(destUse->getUser())) { |
| 143 | + if (user == storeBorrow) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + otherStoreBorrows.push_back(user); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + for (auto *otherStoreBorrow : otherStoreBorrows) { |
| 151 | + // Return true, if otherStoreBorrow was in \p storeBorrow's scope |
| 152 | + if (liveness->isWithinBoundaryOfDef(otherStoreBorrow, storeBorrow)) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + return false; |
| 157 | +} |
| 158 | + |
| 159 | +void ScopedAddressValue::print(llvm::raw_ostream &os) const { |
| 160 | + os << "ScopedAddressIntroducingValue:\n" |
| 161 | + "Kind: " |
| 162 | + << kind |
| 163 | + << "\n" |
| 164 | + "Value: " |
| 165 | + << value; |
| 166 | +} |
| 167 | + |
| 168 | +llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os, |
| 169 | + const ScopedAddressValue &value) { |
| 170 | + value.print(os); |
| 171 | + return os; |
| 172 | +} |
0 commit comments