|
| 1 | +//===--- SimplifyAllocRefDynamic.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 AllocRefDynamicInst : OnoneSimplifyable { |
| 16 | + func simplify(_ context: SimplifyContext) { |
| 17 | + /// Optimize alloc_ref_dynamic of a known type to alloc_ref: |
| 18 | + /// |
| 19 | + /// %3 = metatype SubClass.Type |
| 20 | + /// %4 = upcast %3 : SubClass.Type to BaseClass.Type |
| 21 | + /// %6 = alloc_ref_dynamic [...] %4 : BaseClass.Type, $BaseClass |
| 22 | + /// %8 = (... some use of ...) %6 : $BaseClass |
| 23 | + /// -> |
| 24 | + /// %6 = alloc_ref [...] $SubClass |
| 25 | + /// %7 = upcast %6 : $SubClass to $BaseClass |
| 26 | + /// %8 = (... some use of ...) %7 : $BaseClass |
| 27 | + |
| 28 | + let type: Type |
| 29 | + let emitUpcast: Bool |
| 30 | + if let metatypeInst = metatypeOperand.value as? MetatypeInst { |
| 31 | + type = metatypeInst.type.loweredInstanceTypeOfMetatype(in: parentFunction) |
| 32 | + emitUpcast = false |
| 33 | + } else if let upcastInst = metatypeOperand.value as? UpcastInst, |
| 34 | + let metatypeInst = upcastInst.operands[0].value as? MetatypeInst { |
| 35 | + type = metatypeInst.type.loweredInstanceTypeOfMetatype(in: parentFunction) |
| 36 | + emitUpcast = true |
| 37 | + } else { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + let builder = Builder(before: self, context) |
| 42 | + let newAlloc = builder.createAllocRef(type, isObjC: self.isObjC, canAllocOnStack: self.canAllocOnStack, isBare: false, |
| 43 | + tailAllocatedTypes: self.tailAllocatedTypes, tailAllocatedCounts: Array(self.tailAllocatedCounts.values)) |
| 44 | + |
| 45 | + let result: Value |
| 46 | + if emitUpcast { |
| 47 | + result = builder.createUpcast(from: newAlloc, to: self.type) |
| 48 | + } else { |
| 49 | + result = newAlloc |
| 50 | + } |
| 51 | + uses.replaceAll(with: result, context) |
| 52 | + context.erase(instruction: self) |
| 53 | + } |
| 54 | +} |
0 commit comments