Files
swift-mirror/SwiftCompilerSources/Sources/Optimizer/DataStructures/BasicBlockWorklist.swift
Erik Eckstein 40200d6544 Swift Optimizer: add BasicBlock utility data structures and rename StackList
* add `BasicBlockSet`
* add `BasicBlockWorklist`
* add `BasicBlockRange`, which defines a range of blocks from a common dominating “begin” block to a set of “end” blocks.
* add  `InstructionRange`, which is similar to `BasicBlockRange`, just on instruction level. It can be used for value lifetime analysis.
* rename `StackList` -> `Stack` and move it to `Optimizer/DataStructures`
* rename `PassContext.passContext` to `PassContext._bridged`
* add notify-functions to PassContext
2022-01-12 15:47:16 +01:00

68 lines
2.4 KiB
Swift

//===--- BasicBlockWorklist.swift - a worklist of basic block -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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 basic blocks in a worklist.
///
/// A `BasicBlockWorklist` is basically a combination of a block array and a block 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 BasicBlockWorklist : CustomStringConvertible, CustomReflectable {
private var worklist: Stack<BasicBlock>
private var pushedBlocks: BasicBlockSet
init(_ context: PassContext) {
self.worklist = Stack(context)
self.pushedBlocks = BasicBlockSet(context)
}
/// Pops the last added block from the worklist or returns nil, if the worklist is empty.
mutating func pop() -> BasicBlock? { return worklist.pop() }
/// Pushes \p block onto the worklist if \p block has never been pushed before.
mutating func pushIfNotVisited(_ block: BasicBlock) {
if !pushedBlocks.contains(block) {
pushedBlocks.insert(block);
worklist.append(block)
}
}
/// Pushes all elements of \p contentsOf which have never been pushed before.
mutating func pushIfNotVisited<S: Sequence>(contentsOf other: S) where S.Element == BasicBlock {
for block in other {
pushIfNotVisited(block)
}
}
/// Returns true if \p block was pushed to the worklist, regardless if it's already popped or not.
func hasBeenPushed(_ block: BasicBlock) -> Bool { pushedBlocks.contains(block) }
var description: String {
"""
worklist: \(worklist)
pushed: \(pushedBlocks)
"""
}
var customMirror: Mirror { Mirror(self, children: []) }
/// TODO: once we have move-only types, make this a real deinit.
mutating func deinitialize() {
pushedBlocks.deinitialize()
worklist.deinitialize()
}
}