Files
swift-mirror/SwiftCompilerSources/Sources/Optimizer/Utilities/SSAUpdater.swift
Michael Gottesman 11f0ff6e32 [sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
For years, optimizer engineers have been hitting a common bug caused by passes
assuming all SILValues have a parent function only to be surprised by SILUndef.
Generally we see SILUndef not that often so we see this come up later in
testing. This patch eliminates that problem by making SILUndef uniqued at the
function level instead of the module level. This ensures that it makes sense for
SILUndef to have a parent function, eliminating this possibility since we can
define an API to get its parent function.

rdar://123484595
2024-02-27 13:14:47 -08:00

54 lines
1.9 KiB
Swift

//===--- SSAUpdater.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
import OptimizerBridging
/// Utility for updating SSA for a set of SIL instructions defined in multiple blocks.
struct SSAUpdater<Context: MutatingContext> {
let context: Context
init(function: Function, type: Type, ownership: Ownership,
_ context: Context) {
self.context = context
context._bridged.SSAUpdater_initialize(function.bridged, type.bridged,
ownership._bridged)
}
mutating func addAvailableValue(_ value: Value, in block: BasicBlock) {
context._bridged.SSAUpdater_addAvailableValue(block.bridged, value.bridged)
}
/// Construct SSA for a value that is live at the *end* of a basic block.
mutating func getValue(atEndOf block: BasicBlock) -> Value {
context.notifyInstructionsChanged()
return context._bridged.SSAUpdater_getValueAtEndOfBlock(block.bridged).value
}
/// Construct SSA for a value that is live in the *middle* of a block.
/// This handles the case where the use is before a definition of the value in the same block.
///
/// bb1:
/// %1 = def
/// br bb2
/// bb2:
/// = use(?)
/// %2 = def
/// cond_br bb2, bb3
///
/// In this case we need to insert a phi argument in bb2, merging %1 and %2.
mutating func getValue(inMiddleOf block: BasicBlock) -> Value {
context.notifyInstructionsChanged()
return context._bridged.SSAUpdater_getValueInMiddleOfBlock(block.bridged).value
}
}