forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicBlock.swift
235 lines (190 loc) · 6.61 KB
/
BasicBlock.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//===--- BasicBlock.swift - Defines the BasicBlock class ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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 Basic
import SILBridging
@_semantics("arc.immortal")
final public class BasicBlock : CustomStringConvertible, HasShortDescription, Hashable {
public var next: BasicBlock? { bridged.getNext().block }
public var previous: BasicBlock? { bridged.getPrevious().block }
public var parentFunction: Function { bridged.getFunction().function }
public var description: String {
return String(taking: bridged.getDebugDescription())
}
public var shortDescription: String { name }
public var arguments: ArgumentArray { ArgumentArray(block: self) }
public var instructions: InstructionList {
InstructionList(first: bridged.getFirstInst().instruction)
}
public var terminator: TermInst {
bridged.getLastInst().instruction as! TermInst
}
public var successors: SuccessorArray { terminator.successors }
public var predecessors: PredecessorList {
PredecessorList(startAt: bridged.getFirstPred())
}
public var singlePredecessor: BasicBlock? {
var preds = predecessors
if let p = preds.next() {
if preds.next() == nil {
return p
}
}
return nil
}
public var hasSinglePredecessor: Bool { singlePredecessor != nil }
public var singleSuccessor: BasicBlock? {
successors.count == 1 ? successors[0] : nil
}
/// All function exiting blocks except for ones with an `unreachable` terminator,
/// not immediately preceded by an apply of a no-return function.
public var isReachableExitBlock: Bool {
switch terminator {
case let termInst where termInst.isFunctionExiting:
return true
case is UnreachableInst:
if let instBeforeUnreachable = terminator.previous,
let ai = instBeforeUnreachable as? ApplyInst,
ai.isCalleeNoReturn && !ai.isCalleeTrapNoReturn
{
return true
}
return false
default:
return false
}
}
/// The index of the basic block in its function.
/// This has O(n) complexity. Only use it for debugging
public var index: Int {
for (idx, block) in parentFunction.blocks.enumerated() {
if block == self { return idx }
}
fatalError()
}
public var name: String { "bb\(index)" }
public static func == (lhs: BasicBlock, rhs: BasicBlock) -> Bool { lhs === rhs }
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public var bridged: BridgedBasicBlock { BridgedBasicBlock(SwiftObject(self)) }
}
/// The list of instructions in a BasicBlock.
///
/// It's allowed to delete the current, next or any other instructions while
/// iterating over the instruction list.
public struct InstructionList : CollectionLikeSequence, IteratorProtocol {
private var currentInstruction: Instruction?
public init(first: Instruction?) { currentInstruction = first }
public mutating func next() -> Instruction? {
if var inst = currentInstruction {
while inst.isDeleted {
guard let nextInst = inst.next else {
return nil
}
inst = nextInst
}
currentInstruction = inst.next
return inst
}
return nil
}
public var first: Instruction? { currentInstruction }
public var last: Instruction? { reversed().first }
public func reversed() -> ReverseInstructionList {
if let inst = currentInstruction {
let lastInst = inst.bridged.getLastInstOfParent().instruction
return ReverseInstructionList(first: lastInst)
}
return ReverseInstructionList(first: nil)
}
}
/// The list of instructions in a BasicBlock in reverse order.
///
/// It's allowed to delete the current, next or any other instructions while
/// iterating over the instruction list.
public struct ReverseInstructionList : CollectionLikeSequence, IteratorProtocol {
private var currentInstruction: Instruction?
public init(first: Instruction?) { currentInstruction = first }
public mutating func next() -> Instruction? {
if var inst = currentInstruction {
while inst.isDeleted {
guard let nextInst = inst.previous else {
return nil
}
inst = nextInst
}
currentInstruction = inst.previous
return inst
}
return nil
}
public var first: Instruction? { currentInstruction }
}
public struct ArgumentArray : RandomAccessCollection {
fileprivate let block: BasicBlock
public var startIndex: Int { return 0 }
public var endIndex: Int { block.bridged.getNumArguments() }
public subscript(_ index: Int) -> Argument {
block.bridged.getArgument(index).argument
}
}
public struct SuccessorArray : RandomAccessCollection, FormattedLikeArray {
private let base: OptionalBridgedSuccessor
public let count: Int
init(base: OptionalBridgedSuccessor, count: Int) {
self.base = base
self.count = count
}
public var startIndex: Int { return 0 }
public var endIndex: Int { return count }
public subscript(_ index: Int) -> BasicBlock {
assert(index >= startIndex && index < endIndex)
return base.advancedBy(index).getTargetBlock().block
}
}
public struct PredecessorList : CollectionLikeSequence, IteratorProtocol {
private var currentSucc: OptionalBridgedSuccessor
public init(startAt: OptionalBridgedSuccessor) { currentSucc = startAt }
public mutating func next() -> BasicBlock? {
if let succ = currentSucc.successor {
currentSucc = succ.getNext()
return succ.getContainingInst().instruction.parentBlock
}
return nil
}
}
// Bridging utilities
extension BridgedBasicBlock {
public var block: BasicBlock { obj.getAs(BasicBlock.self) }
public var optional: OptionalBridgedBasicBlock {
OptionalBridgedBasicBlock(obj: self.obj)
}
}
extension OptionalBridgedBasicBlock {
public var block: BasicBlock? { obj.getAs(BasicBlock.self) }
public static var none: OptionalBridgedBasicBlock {
OptionalBridgedBasicBlock(obj: nil)
}
}
extension Optional where Wrapped == BasicBlock {
public var bridged: OptionalBridgedBasicBlock {
OptionalBridgedBasicBlock(obj: self?.bridged.obj)
}
}
extension OptionalBridgedSuccessor {
var successor: BridgedSuccessor? {
if let succ = succ {
return BridgedSuccessor(succ: succ)
}
return nil
}
}