|
| 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 | + devirtualizeReleaseOfObject(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.mayReleaseOrReadRefCount { |
| 55 | + lastRelease = nil |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +) |
| 61 | + |
| 62 | +/// Devirtualize releases of array buffers. |
| 63 | +private func devirtualizeReleaseOfObject( |
| 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 | + guard root === allocRefInstruction else { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + createDeallocCall(context, allocRefInstruction.type, release, allocRefInstruction) |
| 79 | +} |
| 80 | + |
| 81 | +/// Replaces the release-instruction `release` with an explicit call to |
| 82 | +/// the deallocating destructor of `type` for `object`. |
| 83 | +private func createDeallocCall( |
| 84 | + _ context: PassContext, |
| 85 | + _ type: Type, |
| 86 | + _ release: RefCountingInst, |
| 87 | + _ object: Value |
| 88 | +) { |
| 89 | + guard let dealloc = context.getDestructor(ofClass: type) else { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + let substitutionMap = context.getContextSubstitutionMap(for: type) |
| 94 | + |
| 95 | + let builder = Builder(at: release, location: release.location, context) |
| 96 | + |
| 97 | + var object = object |
| 98 | + if object.type != type { |
| 99 | + object = builder.createUncheckedRefCast(object: object, type: type) |
| 100 | + } |
| 101 | + |
| 102 | + // Do what a release would do before calling the deallocator: set the object |
| 103 | + // in deallocating state, which means set the RC_DEALLOCATING_FLAG flag. |
| 104 | + builder.createSetDeallocating(operand: object, isAtomic: release.isAtomic) |
| 105 | + |
| 106 | + // Create the call to the destructor with the allocated object as self |
| 107 | + // argument. |
| 108 | + let functionRef = builder.createFunctionRef(dealloc) |
| 109 | + |
| 110 | + builder.createApply(function: functionRef, substitutionMap, arguments: [object]) |
| 111 | + context.erase(instruction: release) |
| 112 | +} |
| 113 | + |
| 114 | +private func stripRCIdentityPreservingInsts(_ value: Value) -> Value? { |
| 115 | + switch value { |
| 116 | + // First strip off RC identity preserving casts. |
| 117 | + case let inst as Instruction where inst is UpcastInst || |
| 118 | + inst is UncheckedRefCastInst || |
| 119 | + inst is InitExistentialRefInst || |
| 120 | + inst is OpenExistentialRefInst || |
| 121 | + inst is RefToBridgeObjectInst || |
| 122 | + inst is BridgeObjectToRefInst || |
| 123 | + inst is ConvertFunctionInst || |
| 124 | + inst is UncheckedEnumDataInst: |
| 125 | + return inst.operands[0].value |
| 126 | + |
| 127 | + // Then if we have a struct_extract that is extracting a non-trivial member |
| 128 | + // from a struct with no other non-trivial members, a ref count operation on |
| 129 | + // the struct is equivalent to a ref count operation on the extracted |
| 130 | + // member. Strip off the extract. |
| 131 | + case let sei as StructExtractInst where sei.isFieldOnlyNonTrivialField: |
| 132 | + return sei.operands[0].value |
| 133 | + |
| 134 | + // If we have a struct instruction with only one non-trivial stored field, the |
| 135 | + // only reference count that can be modified is the non-trivial field. Return |
| 136 | + // the non-trivial field. |
| 137 | + case let si as StructInst: |
| 138 | + return si.uniqueNonTrivialFieldValue |
| 139 | + |
| 140 | + // If we have an enum instruction with a payload, strip off the enum to |
| 141 | + // expose the enum's payload. |
| 142 | + case let ei as EnumInst where !ei.operands.isEmpty: |
| 143 | + return ei.operands[0].value |
| 144 | + |
| 145 | + // If we have a tuple_extract that is extracting the only non trivial member |
| 146 | + // of a tuple, a retain_value on the tuple is equivalent to a retain_value on |
| 147 | + // the extracted value. |
| 148 | + case let tei as TupleExtractInst where tei.isEltOnlyNonTrivialElt: |
| 149 | + return tei.operands[0].value |
| 150 | + |
| 151 | + // If we are forming a tuple and the tuple only has one element with reference |
| 152 | + // semantics, a retain_value on the tuple is equivalent to a retain value on |
| 153 | + // the tuple operand. |
| 154 | + case let ti as TupleInst: |
| 155 | + return ti.uniqueNonTrivialElt |
| 156 | + |
| 157 | + default: |
| 158 | + return nil |
| 159 | + } |
| 160 | +} |
0 commit comments