Files
swift-mirror/SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyAllocRefDynamic.swift
Kuba Mracek c7a5569c4f [embedded] Add support for ManagedBuffer to Embedded Swift
This makes ManagedBuffer available and usable in Embedded Swift, by:

- Removing an internal consistency check from ManagedBuffer that relies on metatypes.
- Making the .create() API transparent (to hoist the metatype to the callee).
- Adding a AllocRefDynamicInst simplification to convert `alloc_ref_dynamic` to `alloc_ref`, which removes a metatype use.
- Adding tests for the above.
2024-09-23 13:32:50 -07:00

46 lines
1.9 KiB
Swift

//===--- SimplifyAllocRefDynamic.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 AllocRefDynamicInst : OnoneSimplifyable {
func simplify(_ context: SimplifyContext) {
/// Optimize alloc_ref_dynamic of a known type to alloc_ref:
///
/// %3 = metatype SubClass.Type
/// %4 = upcast %3 : SubClass.Type to BaseClass.Type
/// %6 = alloc_ref_dynamic [...] %4 : BaseClass.Type, $BaseClass
/// %8 = (... some use of ...) %6 : $BaseClass
/// ->
/// %6 = alloc_ref [...] $SubClass
/// %7 = upcast %6 : $SubClass to $BaseClass
/// %8 = (... some use of ...) %7 : $BaseClass
let type: Type
if let metatypeInst = operands[1].value as? MetatypeInst {
type = metatypeInst.type.loweredInstanceTypeOfMetatype(in: parentFunction)
} else if let upcastInst = operands[1].value as? UpcastInst,
let metatypeInst = upcastInst.operands[0].value as? MetatypeInst {
type = metatypeInst.type.loweredInstanceTypeOfMetatype(in: parentFunction)
} else {
return
}
let builder = Builder(before: self, context)
let newAlloc = builder.createAllocRef(type, isObjC: self.isObjC, canAllocOnStack: self.canAllocOnStack, isBare: false,
tailAllocatedTypes: self.tailAllocatedTypes, tailAllocatedCounts: Array(self.tailAllocatedCounts.values))
let newCast = builder.createUpcast(from: newAlloc, to: self.type)
uses.replaceAll(with: newCast, context)
context.erase(instruction: self)
}
}