|
| 1 | +//===--- LoopTree.swift ---------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 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 | +import OptimizerBridging |
| 15 | + |
| 16 | +/// Describes top level loops. |
| 17 | +struct LoopTree { |
| 18 | + fileprivate let bridged: BridgedLoopTree |
| 19 | + |
| 20 | + let loops: TopLevelLoopArray |
| 21 | + |
| 22 | + init(bridged: BridgedLoopTree, context: FunctionPassContext) { |
| 23 | + self.bridged = bridged |
| 24 | + self.loops = TopLevelLoopArray(bridged) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Describes a loop with its children. |
| 29 | +struct Loop { |
| 30 | + private let bridged: BridgedLoop |
| 31 | + |
| 32 | + let innerLoops: LoopArray |
| 33 | + let loopBlocks: LoopBlocks |
| 34 | + |
| 35 | + var exitingAndLatchBlocks: some Sequence<BasicBlock> { |
| 36 | + return header.predecessors.lazy |
| 37 | + .filter { predecessor in |
| 38 | + contains(block: predecessor) && !isLoopExiting(loopBlock: predecessor) |
| 39 | + } + exitingBlocks |
| 40 | + } |
| 41 | + |
| 42 | + /// Exit blocks of the loop. |
| 43 | + /// |
| 44 | + /// - Note: Some exit blocks will be duplicated if the loop has critical edges. |
| 45 | + var exitBlocks: some Sequence<BasicBlock> { |
| 46 | + return loopBlocks.lazy |
| 47 | + .flatMap(\.successors) |
| 48 | + .filter { !contains(block: $0) } |
| 49 | + } |
| 50 | + |
| 51 | + var exitingBlocks: some Sequence<BasicBlock> { |
| 52 | + return loopBlocks.lazy |
| 53 | + .filter { isLoopExiting(loopBlock: $0) } |
| 54 | + } |
| 55 | + |
| 56 | + init(bridged: BridgedLoop) { |
| 57 | + self.bridged = bridged |
| 58 | + self.innerLoops = LoopArray(bridged) |
| 59 | + self.loopBlocks = LoopBlocks(bridged) |
| 60 | + } |
| 61 | + |
| 62 | + var preheader: BasicBlock? { |
| 63 | + bridged.getPreheader().block |
| 64 | + } |
| 65 | + |
| 66 | + var header: BasicBlock { |
| 67 | + bridged.getHeader().block |
| 68 | + } |
| 69 | + |
| 70 | + /// Returns `true` if the loop has exactly one exit block. |
| 71 | + var hasSingleExitBlock: Bool { |
| 72 | + return exitBlocks.singleElement != nil |
| 73 | + } |
| 74 | + |
| 75 | + var hasNoExitBlocks: Bool { |
| 76 | + return exitBlocks.isEmpty |
| 77 | + } |
| 78 | + |
| 79 | + private func isLoopExiting(loopBlock: BasicBlock) -> Bool { |
| 80 | + return loopBlock.successors.contains { !contains(block: $0) } |
| 81 | + } |
| 82 | + |
| 83 | + func getBlocksThatDominateAllExitingAndLatchBlocks(_ context: FunctionPassContext) -> [BasicBlock] { |
| 84 | + var result: [BasicBlock] = [] |
| 85 | + var cachedExitingAndLatchBlocks = Stack<BasicBlock>(context) |
| 86 | + var workList = BasicBlockWorklist(context) |
| 87 | + defer { |
| 88 | + cachedExitingAndLatchBlocks.deinitialize() |
| 89 | + workList.deinitialize() |
| 90 | + } |
| 91 | + |
| 92 | + cachedExitingAndLatchBlocks.append(contentsOf: exitingAndLatchBlocks) |
| 93 | + workList.pushIfNotVisited(header) |
| 94 | + |
| 95 | + while let block = workList.pop() { |
| 96 | + guard cachedExitingAndLatchBlocks.allSatisfy({ exitBlock in |
| 97 | + return block.dominates(exitBlock, context.dominatorTree) |
| 98 | + }) else { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + result.append(block) |
| 103 | + |
| 104 | + workList.pushIfNotVisited(contentsOf: context.dominatorTree.getChildren(of: block)) |
| 105 | + } |
| 106 | + |
| 107 | + return result |
| 108 | + } |
| 109 | + |
| 110 | + func contains(block: BasicBlock) -> Bool { |
| 111 | + return bridged.contains(block.bridged) |
| 112 | + } |
| 113 | + |
| 114 | + func splitCriticalExitingAndBackEdges(_ context: FunctionPassContext) { |
| 115 | + for exitingOrLatchBlock in exitingAndLatchBlocks { |
| 116 | + for (index, succesor) in exitingOrLatchBlock.successors.enumerated() where !contains(block: succesor) { |
| 117 | + splitCriticalEdge( |
| 118 | + from: exitingOrLatchBlock.terminator.parentBlock, |
| 119 | + toEdgeIndex: index, |
| 120 | + dominatorTree: context.dominatorTree, |
| 121 | + loopTree: context.loopTree, |
| 122 | + context |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +struct TopLevelLoopArray: BridgedRandomAccessCollection { |
| 130 | + private let bridgedLoopTree: BridgedLoopTree |
| 131 | + |
| 132 | + let count: Int |
| 133 | + |
| 134 | + var startIndex: Int { return 0 } |
| 135 | + var endIndex: Int { return count } |
| 136 | + |
| 137 | + init(_ bridgedLoopTree: BridgedLoopTree) { |
| 138 | + self.bridgedLoopTree = bridgedLoopTree |
| 139 | + self.count = bridgedLoopTree.getTopLevelLoopCount() |
| 140 | + } |
| 141 | + |
| 142 | + subscript(_ index: Int) -> Loop { |
| 143 | + assert(index >= startIndex && index < endIndex) |
| 144 | + return Loop(bridged: bridgedLoopTree.getLoop(index)) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +struct LoopArray: BridgedRandomAccessCollection { |
| 149 | + private let bridgedLoop: BridgedLoop |
| 150 | + |
| 151 | + let count: Int |
| 152 | + |
| 153 | + var startIndex: Int { return 0 } |
| 154 | + var endIndex: Int { return count } |
| 155 | + |
| 156 | + init(_ bridgedLoop: BridgedLoop) { |
| 157 | + self.bridgedLoop = bridgedLoop |
| 158 | + self.count = bridgedLoop.getInnerLoopCount() |
| 159 | + } |
| 160 | + |
| 161 | + subscript(_ index: Int) -> Loop { |
| 162 | + assert(index >= startIndex && index < endIndex) |
| 163 | + return Loop(bridged: bridgedLoop.getInnerLoop(index)) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +struct LoopBlocks: BridgedRandomAccessCollection { |
| 168 | + private let bridgedLoop: BridgedLoop |
| 169 | + |
| 170 | + let count: Int |
| 171 | + |
| 172 | + var startIndex: Int { return 0 } |
| 173 | + var endIndex: Int { return count } |
| 174 | + |
| 175 | + init(_ bridgedLoop: BridgedLoop) { |
| 176 | + self.bridgedLoop = bridgedLoop |
| 177 | + self.count = bridgedLoop.getBasicBlockCount() |
| 178 | + } |
| 179 | + |
| 180 | + subscript(_ index: Int) -> BasicBlock { |
| 181 | + assert(index >= startIndex && index < endIndex) |
| 182 | + return bridgedLoop.getBasicBlock(index).block |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func splitEdge( |
| 187 | + from block: BasicBlock, |
| 188 | + toEdgeIndex: Int, |
| 189 | + dominatorTree: DominatorTree, |
| 190 | + loopTree: LoopTree, |
| 191 | + _ context: some MutatingContext |
| 192 | +) -> BasicBlock { |
| 193 | + let result = loopTree.bridged.splitEdge(block.bridged, toEdgeIndex, dominatorTree.bridged).block |
| 194 | + |
| 195 | + context.notifyBranchesChanged() |
| 196 | + return result |
| 197 | +} |
| 198 | + |
| 199 | +/// If the specified edge is critical, the function returns inserted block. Otherwise returns `nil`. |
| 200 | +@discardableResult |
| 201 | +func splitCriticalEdge( |
| 202 | + from block: BasicBlock, |
| 203 | + toEdgeIndex: Int, |
| 204 | + dominatorTree: DominatorTree, |
| 205 | + loopTree: LoopTree, |
| 206 | + _ context: some MutatingContext |
| 207 | +) -> BasicBlock? { |
| 208 | + guard block.isCriticalEdge(edgeIndex: toEdgeIndex) else { |
| 209 | + return nil |
| 210 | + } |
| 211 | + |
| 212 | + return splitEdge(from: block, toEdgeIndex: toEdgeIndex, dominatorTree: dominatorTree, loopTree: loopTree, context) |
| 213 | +} |
0 commit comments