|
| 1 | +//===--- Context.cpp ------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 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 | +#define DEBUG_TYPE "sil-semantic-arc-opts" |
| 14 | + |
| 15 | +#include "Context.h" |
| 16 | +#include "swift/SIL/MemAccessUtils.h" |
| 17 | +#include "swift/SIL/Projection.h" |
| 18 | +#include "llvm/Support/Debug.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | +using namespace swift::semanticarc; |
| 22 | + |
| 23 | +static llvm::cl::opt<bool> |
| 24 | + VerifyAfterTransformOption("sil-semantic-arc-opts-verify-after-transform", |
| 25 | + llvm::cl::Hidden, llvm::cl::init(false)); |
| 26 | + |
| 27 | +void Context::verify() const { |
| 28 | + if (VerifyAfterTransformOption) |
| 29 | + fn.verify(); |
| 30 | +} |
| 31 | + |
| 32 | +//===----------------------------------------------------------------------===// |
| 33 | +// Well Behaved Write Analysis |
| 34 | +//===----------------------------------------------------------------------===// |
| 35 | + |
| 36 | +/// Returns true if we were able to ascertain that either the initialValue has |
| 37 | +/// no write uses or all of the write uses were writes that we could understand. |
| 38 | +bool Context::constructCacheValue( |
| 39 | + SILValue initialValue, |
| 40 | + SmallVectorImpl<Operand *> &wellBehavedWriteAccumulator) { |
| 41 | + SmallVector<Operand *, 8> worklist(initialValue->getUses()); |
| 42 | + |
| 43 | + while (!worklist.empty()) { |
| 44 | + auto *op = worklist.pop_back_val(); |
| 45 | + SILInstruction *user = op->getUser(); |
| 46 | + |
| 47 | + if (Projection::isAddressProjection(user) || |
| 48 | + isa<ProjectBlockStorageInst>(user)) { |
| 49 | + for (SILValue r : user->getResults()) { |
| 50 | + llvm::copy(r->getUses(), std::back_inserter(worklist)); |
| 51 | + } |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + if (auto *oeai = dyn_cast<OpenExistentialAddrInst>(user)) { |
| 56 | + // Mutable access! |
| 57 | + if (oeai->getAccessKind() != OpenedExistentialAccess::Immutable) { |
| 58 | + wellBehavedWriteAccumulator.push_back(op); |
| 59 | + } |
| 60 | + |
| 61 | + // Otherwise, look through it and continue. |
| 62 | + llvm::copy(oeai->getUses(), std::back_inserter(worklist)); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (auto *si = dyn_cast<StoreInst>(user)) { |
| 67 | + // We must be the dest since addresses can not be stored. |
| 68 | + assert(si->getDest() == op->get()); |
| 69 | + wellBehavedWriteAccumulator.push_back(op); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // Add any destroy_addrs to the resultAccumulator. |
| 74 | + if (isa<DestroyAddrInst>(user)) { |
| 75 | + wellBehavedWriteAccumulator.push_back(op); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + // load_borrow and incidental uses are fine as well. |
| 80 | + if (isa<LoadBorrowInst>(user) || isIncidentalUse(user)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + // Look through begin_access and mark them/their end_borrow as users. |
| 85 | + if (auto *bai = dyn_cast<BeginAccessInst>(user)) { |
| 86 | + // If we do not have a read, mark this as a write. Also, insert our |
| 87 | + // end_access as well. |
| 88 | + if (bai->getAccessKind() != SILAccessKind::Read) { |
| 89 | + wellBehavedWriteAccumulator.push_back(op); |
| 90 | + transform(bai->getUsersOfType<EndAccessInst>(), |
| 91 | + std::back_inserter(wellBehavedWriteAccumulator), |
| 92 | + [](EndAccessInst *eai) { return &eai->getAllOperands()[0]; }); |
| 93 | + } |
| 94 | + |
| 95 | + // And then add the users to the worklist and continue. |
| 96 | + llvm::copy(bai->getUses(), std::back_inserter(worklist)); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + // If we have a load, we just need to mark the load [take] as a write. |
| 101 | + if (auto *li = dyn_cast<LoadInst>(user)) { |
| 102 | + if (li->getOwnershipQualifier() == LoadOwnershipQualifier::Take) { |
| 103 | + wellBehavedWriteAccumulator.push_back(op); |
| 104 | + } |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + // If we have a FullApplySite, we need to do per convention/inst logic. |
| 109 | + if (auto fas = FullApplySite::isa(user)) { |
| 110 | + // Begin by seeing if we have an in_guaranteed use. If we do, we are done. |
| 111 | + if (fas.getArgumentConvention(*op) == |
| 112 | + SILArgumentConvention::Indirect_In_Guaranteed) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + // Then see if we have an apply site that is not a coroutine apply |
| 117 | + // site. In such a case, without further analysis, we can treat it like an |
| 118 | + // instantaneous write and validate that it doesn't overlap with our load |
| 119 | + // [copy]. |
| 120 | + if (!fas.beginsCoroutineEvaluation() && |
| 121 | + fas.getArgumentConvention(*op).isInoutConvention()) { |
| 122 | + wellBehavedWriteAccumulator.push_back(op); |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + // Otherwise, be conservative and return that we had a write that we did |
| 127 | + // not understand. |
| 128 | + LLVM_DEBUG(llvm::dbgs() |
| 129 | + << "Function: " << user->getFunction()->getName() << "\n"); |
| 130 | + LLVM_DEBUG(llvm::dbgs() << "Value: " << op->get()); |
| 131 | + LLVM_DEBUG(llvm::dbgs() << "Unhandled apply site!: " << *user); |
| 132 | + |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + // Copy addr that read are just loads. |
| 137 | + if (auto *cai = dyn_cast<CopyAddrInst>(user)) { |
| 138 | + // If our value is the destination, this is a write. |
| 139 | + if (cai->getDest() == op->get()) { |
| 140 | + wellBehavedWriteAccumulator.push_back(op); |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + // Ok, so we are Src by process of elimination. Make sure we are not being |
| 145 | + // taken. |
| 146 | + if (cai->isTakeOfSrc()) { |
| 147 | + wellBehavedWriteAccumulator.push_back(op); |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + // Otherwise, we are safe and can continue. |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + // If we did not recognize the user, just return conservatively that it was |
| 156 | + // written to in a way we did not understand. |
| 157 | + LLVM_DEBUG(llvm::dbgs() |
| 158 | + << "Function: " << user->getFunction()->getName() << "\n"); |
| 159 | + LLVM_DEBUG(llvm::dbgs() << "Value: " << op->get()); |
| 160 | + LLVM_DEBUG(llvm::dbgs() << "Unknown instruction!: " << *user); |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + // Ok, we finished our worklist and this address is not being written to. |
| 165 | + return true; |
| 166 | +} |
0 commit comments