Files
Erik Eckstein 9aff288be4 Optimizer: re-implement the pointer_to_address SILCombine peephole optimizations in swift
Which consists of
* removing redundant `address_to_pointer`-`pointer_to_address` pairs
* optimize `index_raw_pointer` of a manually computed stride to `index_addr`
* remove or increase the alignment based on a "assumeAlignment" builtin

This is a big code cleanup but also has some functional differences for the `address_to_pointer`-`pointer_to_address` pair removal:

* It's not done if the resulting SIL would result in a (detectable) use-after-dealloc_stack memory lifetime failure.
* It's not done if `copy_value`s must be inserted or borrow-scopes must be extended to comply with ownership rules (this was the task of the OwnershipRAUWHelper).

Inserting copies is bad anyway.
Extending borrow-scopes would only be required if the original lifetime of the pointer extends a borrow scope - which shouldn't happen in save code. Therefore this is a very rare case which is not worth handling.
2024-12-21 08:28:22 +01:00

98 lines
3.0 KiB
Swift

//===--- Worklist.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
/// A utility for processing entities in a worklist.
///
/// A `Worklist` is basically a combination of a stack and a set.
/// It can be used for typical worklist-processing algorithms.
///
/// This type should be a move-only type, but unfortunately we don't have move-only
/// types yet. Therefore it's needed to call `deinitialize()` explicitly to
/// destruct this data structure, e.g. in a `defer {}` block.
struct Worklist<Set: IntrusiveSet> : CustomStringConvertible, NoReflectionChildren {
typealias Element = Set.Element
private var worklist: Stack<Element>
private var pushedElements: Set
init(_ context: some Context) {
self.worklist = Stack(context)
self.pushedElements = Set(context)
}
mutating func pop() -> Element? { return worklist.pop() }
/// Pop and allow the popped element to be pushed again to the worklist.
mutating func popAndForget() -> Element? {
if let element = worklist.pop() {
pushedElements.erase(element)
return element
}
return nil
}
mutating func pushIfNotVisited(_ element: Element) {
if pushedElements.insert(element) {
worklist.append(element)
}
}
mutating func pushIfNotVisited<S: Sequence>(contentsOf other: S) where S.Element == Element {
for element in other {
pushIfNotVisited(element)
}
}
/// Returns true if \p element was pushed to the worklist, regardless if it's already popped or not.
func hasBeenPushed(_ element: Element) -> Bool { pushedElements.contains(element) }
var isEmpty: Bool { worklist.isEmpty }
var description: String {
"""
worklist: \(worklist)
pushed: \(pushedElements)
"""
}
/// TODO: once we have move-only types, make this a real deinit.
mutating func deinitialize() {
pushedElements.deinitialize()
worklist.deinitialize()
}
}
typealias BasicBlockWorklist = Worklist<BasicBlockSet>
typealias InstructionWorklist = Worklist<InstructionSet>
typealias SpecificInstructionWorklist<InstType: Instruction> = Worklist<SpecificInstructionSet<InstType>>
typealias ValueWorklist = Worklist<ValueSet>
typealias OperandWorklist = Worklist<OperandSet>
extension InstructionWorklist {
mutating func pushPredecessors(of inst: Instruction, ignoring ignoreInst: Instruction) {
if let prev = inst.previous {
if prev != ignoreInst {
pushIfNotVisited(prev)
}
} else {
for predBlock in inst.parentBlock.predecessors {
let termInst = predBlock.terminator
if termInst != ignoreInst {
pushIfNotVisited(termInst)
}
}
}
}
}