|
| 1 | +//===--- SimplifyRetainReleaseValue.swift ---------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | +extension RetainValueInst : Simplifyable, SILCombineSimplifyable { |
| 16 | + func simplify(_ context: SimplifyContext) { |
| 17 | + |
| 18 | + // Remove pairs of |
| 19 | + // ``` |
| 20 | + // release_value %0 // the release is before the retain! |
| 21 | + // retain_value %0 |
| 22 | + // ``` |
| 23 | + // which sometimes the ARC optimizations cannot do. |
| 24 | + // |
| 25 | + if optimizeReleaseRetainPair(context) { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + // Replace |
| 30 | + // ``` |
| 31 | + // %1 = enum #E.A, %0 |
| 32 | + // retain_value %1 |
| 33 | + // ``` |
| 34 | + // with |
| 35 | + // ``` |
| 36 | + // %1 = enum #E.A, %0 // maybe dead |
| 37 | + // retain_value %0 |
| 38 | + // ``` |
| 39 | + replaceOperandWithPayloadOfEnum(context) |
| 40 | + |
| 41 | + // Remove if the operand is trivial (but not necessarily its type), e.g. |
| 42 | + // ``` |
| 43 | + // %1 = value_to_bridge_object %0 : $UInt64 |
| 44 | + // retain_value %1 : $Builtin.BridgeObject |
| 45 | + // ``` |
| 46 | + if removeIfOperandIsTrivial(context) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // Replace e.g. |
| 51 | + // ``` |
| 52 | + // retain_value %1 : $SomeClass |
| 53 | + // ``` |
| 54 | + // with |
| 55 | + // ``` |
| 56 | + // strong_retain %1 : $SomeClass |
| 57 | + // ``` |
| 58 | + replaceWithStrongOrUnownedRetain(context) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +extension ReleaseValueInst : Simplifyable, SILCombineSimplifyable { |
| 63 | + func simplify(_ context: SimplifyContext) { |
| 64 | + |
| 65 | + // Replace |
| 66 | + // ``` |
| 67 | + // %1 = enum #E.A, %0 |
| 68 | + // release_value %1 |
| 69 | + // ``` |
| 70 | + // with |
| 71 | + // ``` |
| 72 | + // %1 = enum #E.A, %0 // maybe dead |
| 73 | + // release_value %0 |
| 74 | + // ``` |
| 75 | + replaceOperandWithPayloadOfEnum(context) |
| 76 | + |
| 77 | + // Remove if the operand is trivial (but not necessarily its type), e.g. |
| 78 | + // ``` |
| 79 | + // %1 = value_to_bridge_object %0 : $UInt64 |
| 80 | + // release_value %1 : $Builtin.BridgeObject |
| 81 | + // ``` |
| 82 | + if removeIfOperandIsTrivial(context) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Replace e.g. |
| 87 | + // ``` |
| 88 | + // release_value %1 : $SomeClass |
| 89 | + // ``` |
| 90 | + // with |
| 91 | + // ``` |
| 92 | + // release_value %1 : $SomeClass |
| 93 | + // ``` |
| 94 | + replaceWithStrongOrUnownedRelease(context) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +private extension RetainValueInst { |
| 99 | + func optimizeReleaseRetainPair(_ context: SimplifyContext) -> Bool { |
| 100 | + if let prevInst = self.previous, |
| 101 | + let release = prevInst as? ReleaseValueInst, |
| 102 | + release.value == self.value { |
| 103 | + context.erase(instruction: release) |
| 104 | + context.erase(instruction: self) |
| 105 | + return true |
| 106 | + } |
| 107 | + return false |
| 108 | + } |
| 109 | + |
| 110 | + func replaceWithStrongOrUnownedRetain(_ context: SimplifyContext) { |
| 111 | + if value.type.isReferenceCounted(in: parentFunction) { |
| 112 | + let builder = Builder(before: self, context) |
| 113 | + if value.type.isUnownedStorageType { |
| 114 | + builder.createUnownedRetain(operand: value) |
| 115 | + } else { |
| 116 | + builder.createStrongRetain(operand: value) |
| 117 | + } |
| 118 | + context.erase(instruction: self) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +private extension ReleaseValueInst { |
| 124 | + func replaceWithStrongOrUnownedRelease(_ context: SimplifyContext) { |
| 125 | + if value.type.isReferenceCounted(in: parentFunction) { |
| 126 | + let builder = Builder(before: self, context) |
| 127 | + if value.type.isUnownedStorageType { |
| 128 | + builder.createUnownedRelease(operand: value) |
| 129 | + } else { |
| 130 | + builder.createStrongRelease(operand: value) |
| 131 | + } |
| 132 | + context.erase(instruction: self) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +private extension UnaryInstruction { |
| 138 | + func replaceOperandWithPayloadOfEnum(_ context: SimplifyContext) { |
| 139 | + if let e = operand.value as? EnumInst, |
| 140 | + !e.type.isValueTypeWithDeinit, |
| 141 | + let payload = e.payload { |
| 142 | + operand.set(to: payload, context) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + func removeIfOperandIsTrivial(_ context: SimplifyContext) -> Bool { |
| 147 | + if operand.value.isTrivial(context) { |
| 148 | + context.erase(instruction: self) |
| 149 | + return true |
| 150 | + } |
| 151 | + return false |
| 152 | + } |
| 153 | +} |
0 commit comments