|
| 1 | +//===--- ReleaseDevirtualizer.swift - Devirtualizes release-instructions --===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 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 | +import SIL |
| 14 | + |
| 15 | +/// Devirtualizes release instructions which are known to destruct the object. |
| 16 | +/// |
| 17 | +/// This means, it replaces a sequence of |
| 18 | +/// %x = alloc_ref [stack] $X |
| 19 | +/// ... |
| 20 | +/// strong_release %x |
| 21 | +/// dealloc_stack_ref %x |
| 22 | +/// with |
| 23 | +/// %x = alloc_ref [stack] $X |
| 24 | +/// ... |
| 25 | +/// set_deallocating %x |
| 26 | +/// %d = function_ref @dealloc_of_X |
| 27 | +/// %a = apply %d(%x) |
| 28 | +/// dealloc_stack_ref %x |
| 29 | +/// |
| 30 | +/// The optimization is only done for stack promoted objects because they are |
| 31 | +/// known to have no associated objects (which are not explicitly released |
| 32 | +/// in the deinit method). |
| 33 | +let releaseDevirtualizerPass = FunctionPass( |
| 34 | + name: "release-devirtualizer", { function, context in |
| 35 | + for block in function.blocks { |
| 36 | + // The last `release_value`` or `strong_release`` instruction before the |
| 37 | + // deallocation. |
| 38 | + var lastRelease: RefCountingInst? |
| 39 | + |
| 40 | + for instruction in block.instructions { |
| 41 | + if let release = lastRelease { |
| 42 | + // We only do the optimization for stack promoted object, because for |
| 43 | + // these we know that they don't have associated objects, which are |
| 44 | + // _not_ released by the deinit method. |
| 45 | + if let deallocStackRef = instruction as? DeallocStackRefInst { |
| 46 | + tryDevirtualizeReleaseOfObject(context, release, deallocStackRef) |
| 47 | + lastRelease = nil |
| 48 | + continue |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if instruction is ReleaseValueInst || instruction is StrongReleaseInst { |
| 53 | + lastRelease = instruction as? RefCountingInst |
| 54 | + } else if instruction.mayRelease { |
| 55 | + lastRelease = nil |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +) |
| 61 | + |
| 62 | +/// Tries to de-virtualize the final release of a stack-promoted object. |
| 63 | +private func tryDevirtualizeReleaseOfObject( |
| 64 | + _ context: PassContext, |
| 65 | + _ release: RefCountingInst, |
| 66 | + _ deallocStackRef: DeallocStackRefInst |
| 67 | +) { |
| 68 | + let allocRefInstruction = deallocStackRef.allocRef |
| 69 | + var root = release.operands[0].value |
| 70 | + while let newRoot = stripRCIdentityPreservingInsts(root) { |
| 71 | + root = newRoot |
| 72 | + } |
| 73 | + |
| 74 | + if root != allocRefInstruction { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + let type = allocRefInstruction.type |
| 79 | + |
| 80 | + guard let dealloc = context.getDestructor(ofClass: type) else { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + let builder = Builder(at: release, location: release.location, context) |
| 85 | + |
| 86 | + var object: Value = allocRefInstruction |
| 87 | + if object.type != type { |
| 88 | + object = builder.createUncheckedRefCast(object: object, type: type) |
| 89 | + } |
| 90 | + |
| 91 | + // Do what a release would do before calling the deallocator: set the object |
| 92 | + // in deallocating state, which means set the RC_DEALLOCATING_FLAG flag. |
| 93 | + builder.createSetDeallocating(operand: object, isAtomic: release.isAtomic) |
| 94 | + |
| 95 | + // Create the call to the destructor with the allocated object as self |
| 96 | + // argument. |
| 97 | + let functionRef = builder.createFunctionRef(dealloc) |
| 98 | + |
| 99 | + let substitutionMap = context.getContextSubstitutionMap(for: type) |
| 100 | + builder.createApply(function: functionRef, substitutionMap, arguments: [object]) |
| 101 | + context.erase(instruction: release) |
| 102 | +} |
| 103 | + |
| 104 | +private func stripRCIdentityPreservingInsts(_ value: Value) -> Value? { |
| 105 | + guard let inst = value as? Instruction else { return nil } |
| 106 | + |
| 107 | + switch inst { |
| 108 | + // First strip off RC identity preserving casts. |
| 109 | + case is UpcastInst, |
| 110 | + is UncheckedRefCastInst, |
| 111 | + is InitExistentialRefInst, |
| 112 | + is OpenExistentialRefInst, |
| 113 | + is RefToBridgeObjectInst, |
| 114 | + is BridgeObjectToRefInst, |
| 115 | + is ConvertFunctionInst, |
| 116 | + is UncheckedEnumDataInst: |
| 117 | + return inst.operands[0].value |
| 118 | + |
| 119 | + // Then if we have a struct_extract that is extracting a non-trivial member |
| 120 | + // from a struct with no other non-trivial members, a ref count operation on |
| 121 | + // the struct is equivalent to a ref count operation on the extracted |
| 122 | + // member. Strip off the extract. |
| 123 | + case let sei as StructExtractInst where sei.isFieldOnlyNonTrivialField: |
| 124 | + return sei.operand |
| 125 | + |
| 126 | + // If we have a struct or tuple instruction with only one non-trivial operand, the |
| 127 | + // only reference count that can be modified is the non-trivial operand. Return |
| 128 | + // the non-trivial operand. |
| 129 | + case is StructInst, is TupleInst: |
| 130 | + return inst.uniqueNonTrivialOperand |
| 131 | + |
| 132 | + // If we have an enum instruction with a payload, strip off the enum to |
| 133 | + // expose the enum's payload. |
| 134 | + case let ei as EnumInst where !ei.operands.isEmpty: |
| 135 | + return ei.operand |
| 136 | + |
| 137 | + // If we have a tuple_extract that is extracting the only non trivial member |
| 138 | + // of a tuple, a retain_value on the tuple is equivalent to a retain_value on |
| 139 | + // the extracted value. |
| 140 | + case let tei as TupleExtractInst where tei.isEltOnlyNonTrivialElt: |
| 141 | + return tei.operand |
| 142 | + |
| 143 | + default: |
| 144 | + return nil |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +private extension Instruction { |
| 149 | + /// Search the operands of this tuple for a unique non-trivial elt. If we find |
| 150 | + /// it, return it. Otherwise return `nil`. |
| 151 | + var uniqueNonTrivialOperand: Value? { |
| 152 | + var candidateElt: Value? |
| 153 | + let function = self.function |
| 154 | + |
| 155 | + for op in operands { |
| 156 | + if !op.value.type.isTrivial(in: function) { |
| 157 | + if candidateElt == nil { |
| 158 | + candidateElt = op.value |
| 159 | + continue |
| 160 | + } |
| 161 | + |
| 162 | + // Otherwise, we have two values that are non-trivial. Bail. |
| 163 | + return nil |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return candidateElt |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +private extension TupleExtractInst { |
| 172 | + var isEltOnlyNonTrivialElt: Bool { |
| 173 | + let function = self.function |
| 174 | + |
| 175 | + if type.isTrivial(in: function) { |
| 176 | + return false |
| 177 | + } |
| 178 | + |
| 179 | + let opType = operand.type |
| 180 | + |
| 181 | + var nonTrivialEltsCount = 0 |
| 182 | + for elt in opType.tupleElements { |
| 183 | + if elt.isTrivial(in: function) { |
| 184 | + nonTrivialEltsCount += 1 |
| 185 | + } |
| 186 | + |
| 187 | + if nonTrivialEltsCount > 1 { |
| 188 | + return false |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return true |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +private extension StructExtractInst { |
| 197 | + var isFieldOnlyNonTrivialField: Bool { |
| 198 | + let function = self.function |
| 199 | + |
| 200 | + if type.isTrivial(in: function) { |
| 201 | + return false |
| 202 | + } |
| 203 | + |
| 204 | + let structType = operand.type |
| 205 | + |
| 206 | + var nonTrivialFieldsCount = 0 |
| 207 | + for field in structType.getStructFields(in: function) { |
| 208 | + if field.isTrivial(in: function) { |
| 209 | + nonTrivialFieldsCount += 1 |
| 210 | + } |
| 211 | + |
| 212 | + if nonTrivialFieldsCount > 1 { |
| 213 | + return false |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return true |
| 218 | + } |
| 219 | +} |
0 commit comments