|
| 1 | +//===--- SimplifyMarkDependence.swift -------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 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 | +// Note: this simplification cannot run before dependency diagnostics. |
| 16 | +// See `var isRedundant` below. |
| 17 | + |
| 18 | +extension MarkDependenceInst : OnoneSimplifiable, SILCombineSimplifiable { |
| 19 | + func simplify(_ context: SimplifyContext) { |
| 20 | + if isRedundant || |
| 21 | + // A literal lives forever, so no mark_dependence is needed. |
| 22 | + // This pattern can occur after StringOptimization when a utf8CString of a literal is replace |
| 23 | + // by the string_literal itself. |
| 24 | + value.isLiteral |
| 25 | + { |
| 26 | + replace(with: value, context) |
| 27 | + return |
| 28 | + } |
| 29 | + simplifyBaseOperand(context) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension MarkDependenceAddrInst : OnoneSimplifiable, SILCombineSimplifiable { |
| 34 | + func simplify(_ context: SimplifyContext) { |
| 35 | + if isRedundant { |
| 36 | + context.erase(instruction: self) |
| 37 | + return |
| 38 | + } |
| 39 | + simplifyBaseOperand(context) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +private extension MarkDependenceInstruction { |
| 44 | + var isRedundant: Bool { |
| 45 | + if base.type.isObject && base.type.isTrivial(in: base.parentFunction) { |
| 46 | + // Sometimes due to specialization/builtins, we can get a mark_dependence whose base is a trivial |
| 47 | + // typed object. Trivial values live forever. Therefore the mark_dependence does not have a meaning. |
| 48 | + // Note: the mark_dependence is still needed for lifetime diagnostics. So it's important that this |
| 49 | + // simplification does not run before the lifetime diagnostic pass. |
| 50 | + return true |
| 51 | + } |
| 52 | + // If the value is an address projection from the base the mark_dependence is not needed because the |
| 53 | + // base cannot be destroyed before the accessing the value, anyway. |
| 54 | + if valueOrAddress.type.isAddress, base.type.isAddress, |
| 55 | + // But we still need to keep the mark_dependence for non-escapable types because a non-escapable |
| 56 | + // value can be copied and copies must not outlive the base. |
| 57 | + valueOrAddress.type.isEscapable(in: parentFunction), |
| 58 | + base.accessPath.isEqualOrContains(valueOrAddress.accessPath) |
| 59 | + { |
| 60 | + return true |
| 61 | + } |
| 62 | + return false |
| 63 | + } |
| 64 | + |
| 65 | + func simplifyBaseOperand(_ context: SimplifyContext) { |
| 66 | + /// In OSSA, the `base` is a borrow introducing operand. It is pretty complicated to change the base. |
| 67 | + /// So, for simplicity, we only do this optimization when OSSA is already lowered. |
| 68 | + if parentFunction.hasOwnership { |
| 69 | + return |
| 70 | + } |
| 71 | + // Replace the base operand with the operand of the base value if it's a certain kind of forwarding |
| 72 | + // instruction. |
| 73 | + let rootBase = base.lookThroughEnumAndExistentialRef |
| 74 | + if rootBase != base { |
| 75 | + baseOperand.set(to: rootBase, context) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private extension Value { |
| 81 | + /// True, if this is a literal instruction or a struct of a literal instruction. |
| 82 | + /// What we want to catch here is a `UnsafePointer<Int8>` of a string literal. |
| 83 | + var isLiteral: Bool { |
| 84 | + switch self { |
| 85 | + case let s as StructInst: |
| 86 | + if let singleOperand = s.operands.singleElement { |
| 87 | + return singleOperand.value.isLiteral |
| 88 | + } |
| 89 | + return false |
| 90 | + case is IntegerLiteralInst, is FloatLiteralInst, is StringLiteralInst: |
| 91 | + return true |
| 92 | + default: |
| 93 | + return false |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + var lookThroughEnumAndExistentialRef: Value { |
| 98 | + switch self { |
| 99 | + case let e as EnumInst: |
| 100 | + if let payload = e.payload { |
| 101 | + return payload.lookThroughEnumAndExistentialRef |
| 102 | + } |
| 103 | + return self |
| 104 | + case let ier as InitExistentialRefInst: |
| 105 | + return ier.instance.lookThroughEnumAndExistentialRef |
| 106 | + case let oer as OpenExistentialRefInst: |
| 107 | + return oer.existential.lookThroughEnumAndExistentialRef |
| 108 | + default: |
| 109 | + return self |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments