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 154 155 156 157 158 159 160 161
|
//===--- SimplifyDestructure.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 DestructureTupleInst : OnoneSimplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
// If the tuple is trivial, replace
// ```
// (%1, %2) = destructure_tuple %t
// ```
// ->
// ```
// %1 = tuple_extract %t, 0
// %2 = tuple_extract %t, 1
// ```
// This canonicalization helps other optimizations to e.g. CSE tuple_extracts.
//
if replaceWithTupleExtract(context) {
return
}
// Eliminate the redundant instruction pair
// ```
// %t = tuple (%0, %1, %2)
// (%3, %4, %5) = destructure_tuple %t
// ```
// and replace the results %3, %4, %5 with %0, %1, %2, respectively
//
if let tuple = self.tuple as? TupleInst {
tryReplaceConstructDestructPair(construct: tuple, destruct: self, context)
}
}
private func replaceWithTupleExtract(_ context: SimplifyContext) -> Bool {
guard self.tuple.type.isTrivial(in: parentFunction) else {
return false
}
let builder = Builder(before: self, context)
for (elementIdx, result) in results.enumerated() {
let elementValue = builder.createTupleExtract(tuple: self.tuple, elementIndex: elementIdx)
result.uses.replaceAll(with: elementValue, context)
}
context.erase(instruction: self)
return true
}
}
extension DestructureStructInst : OnoneSimplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
// If the struct is trivial, replace
// ```
// (%1, %2) = destructure_struct %s
// ```
// ->
// ```
// %1 = struct_extract %s, #S.field0
// %2 = struct_extract %s, #S.field1
// ```
// This canonicalization helps other optimizations to e.g. CSE tuple_extracts.
//
if replaceWithStructExtract(context) {
return
}
switch self.struct {
case let str as StructInst:
// Eliminate the redundant instruction pair
// ```
// %s = struct (%0, %1, %2)
// (%3, %4, %5) = destructure_struct %s
// ```
// and replace the results %3, %4, %5 with %0, %1, %2, respectively
//
tryReplaceConstructDestructPair(construct: str, destruct: self, context)
case let copy as CopyValueInst:
// Similar to the pattern above, but with a copy_value:
// Replace
// ```
// %s = struct (%0, %1, %2)
// %c = copy_value %s // can also be a chain of multiple copies
// (%3, %4, %5) = destructure_struct %c
// ```
// with
// ```
// %c0 = copy_value %0
// %c1 = copy_value %1
// %c2 = copy_value %2
// %s = struct (%0, %1, %2) // keep the original struct
// ```
// and replace the results %3, %4, %5 with %c0, %c1, %c2, respectively.
//
// This transformation has the advantage that we can do it even if the `struct` instruction
// has other uses than the `copy_value`.
//
if copy.uses.singleUse?.instruction == self,
let structInst = copy.fromValue.lookThroughCopy as? StructInst,
structInst.parentBlock == self.parentBlock
{
for (result, operand) in zip(self.results, structInst.operands) {
if operand.value.type.isTrivial(in: parentFunction) {
result.uses.replaceAll(with: operand.value, context)
} else {
let builder = Builder(before: structInst, context)
let copiedOperand = builder.createCopyValue(operand: operand.value)
result.uses.replaceAll(with: copiedOperand, context)
}
}
context.erase(instruction: self)
context.erase(instruction: copy)
}
default:
break
}
}
private func replaceWithStructExtract(_ context: SimplifyContext) -> Bool {
guard self.struct.type.isTrivial(in: parentFunction) else {
return false
}
let builder = Builder(before: self, context)
for (fieldIdx, result) in results.enumerated() {
let fieldValue = builder.createStructExtract(struct: self.struct, fieldIndex: fieldIdx)
result.uses.replaceAll(with: fieldValue, context)
}
context.erase(instruction: self)
return true
}
}
private func tryReplaceConstructDestructPair(construct: SingleValueInstruction,
destruct: MultipleValueInstruction,
_ context: SimplifyContext) {
let singleUse = context.preserveDebugInfo ? construct.uses.singleUse : construct.uses.ignoreDebugUses.singleUse
let canEraseFirst = singleUse?.instruction == destruct
if !canEraseFirst && construct.parentFunction.hasOwnership && construct.ownership == .owned {
// We cannot add more uses to this tuple without inserting a copy.
return
}
for (result, operand) in zip(destruct.results, construct.operands) {
result.uses.replaceAll(with: operand.value, context)
}
if canEraseFirst {
context.erase(instructionIncludingDebugUses: destruct)
}
}
|