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