Skip to content

Commit ad9dafc

Browse files
committed
Swift SIL: add Value.definingBlock
and re-factor `Value.definingInstruction`
1 parent 3b43da9 commit ad9dafc

File tree

6 files changed

+40
-48
lines changed

6 files changed

+40
-48
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/StackPromotion.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ private extension EscapeInfo {
207207
var dominatingBlock = value.block
208208

209209
_ = isEscaping(object: value, visitUse: { op, _, _ in
210-
if let defBlock = op.value.definingBlock, defBlock.dominates(dominatingBlock, domTree) {
210+
let defBlock = op.value.definingBlock
211+
if defBlock.dominates(dominatingBlock, domTree) {
211212
dominatingBlock = defBlock
212213
}
213214
return .continueWalking
@@ -230,25 +231,25 @@ private extension EscapeInfo {
230231
outerBlockRange.insert(user.block)
231232

232233
let val = op.value
233-
if let defBlock = val.definingBlock {
234-
// Also insert the operand's definition. Otherwise we would miss allocation
235-
// instructions (for which the `visitUse` closure is not called).
236-
outerBlockRange.insert(defBlock)
237-
238-
// We need to explicitly add predecessor blocks of phi-arguments becaues they
239-
// are not necesesarily visited by `EscapeInfo.walkDown`.
240-
// This is important for the special case where there is a back-edge from the
241-
// inner range to the inner rage's begin-block:
242-
//
243-
// bb0: // <- need to be in the outer range
244-
// br bb1(%some_init_val)
245-
// bb1(%arg):
246-
// %k = alloc_ref $Klass // innerInstRange.begin
247-
// cond_br bb2, bb1(%k) // back-edge to bb1 == innerInstRange.blockRange.begin
248-
//
249-
if val is BlockArgument {
250-
outerBlockRange.insert(contentsOf: defBlock.predecessors)
251-
}
234+
let defBlock = val.definingBlock
235+
236+
// Also insert the operand's definition. Otherwise we would miss allocation
237+
// instructions (for which the `visitUse` closure is not called).
238+
outerBlockRange.insert(defBlock)
239+
240+
// We need to explicitly add predecessor blocks of phi-arguments becaues they
241+
// are not necesesarily visited by `EscapeInfo.walkDown`.
242+
// This is important for the special case where there is a back-edge from the
243+
// inner range to the inner rage's begin-block:
244+
//
245+
// bb0: // <- need to be in the outer range
246+
// br bb1(%some_init_val)
247+
// bb1(%arg):
248+
// %k = alloc_ref $Klass // innerInstRange.begin
249+
// cond_br bb2, bb1(%k) // back-edge to bb1 == innerInstRange.blockRange.begin
250+
//
251+
if val is BlockArgument {
252+
outerBlockRange.insert(contentsOf: defBlock.predecessors)
252253
}
253254
return .continueWalking
254255
})

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import SILBridging
1818
/// Maps to both, SILPhiArgument and SILFunctionArgument.
1919
public class Argument : Value, Equatable {
2020
public var definingInstruction: Instruction? { nil }
21+
public var definingBlock: BasicBlock { block }
2122

2223
public var block: BasicBlock {
2324
return SILArgument_getParent(bridged).block

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ extension OptionalBridgedInstruction {
135135

136136
public class SingleValueInstruction : Instruction, Value {
137137
final public var definingInstruction: Instruction? { self }
138+
final public var definingBlock: BasicBlock { block }
138139

139140
fileprivate final override var resultCount: Int { 1 }
140141
fileprivate final override func getResult(index: Int) -> Value { self }
@@ -151,6 +152,7 @@ public final class MultipleValueInstructionResult : Value {
151152
}
152153

153154
public var definingInstruction: Instruction? { instruction }
155+
public var definingBlock: BasicBlock { instruction.block }
154156

155157
public var index: Int { MultiValueInstResult_getIndex(bridged) }
156158

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ public protocol Value : AnyObject, CustomStringConvertible {
1717
var uses: UseList { get }
1818
var type: Type { get }
1919
var ownership: Ownership { get }
20+
21+
/// The instruction which defines the value.
22+
///
23+
/// This is nil if the value is not defined by an instruction, e.g. an `Argument`.
2024
var definingInstruction: Instruction? { get }
25+
26+
/// The block where the value is defined.
27+
///
28+
/// It's not legal to get the definingBlock of an `Undef` value.
29+
var definingBlock: BasicBlock { get }
2130
}
2231

2332
public enum Ownership {
@@ -80,32 +89,10 @@ extension Value {
8089
UseList(SILValue_firstUse(bridged))
8190
}
8291

83-
public var function: Function {
84-
SILNode_getFunction(bridgedNode).function
85-
}
92+
public var function: Function { definingBlock.function }
8693

8794
public var type: Type { SILValue_getType(bridged).type }
8895

89-
public var definingInstruction: Instruction? {
90-
switch self {
91-
case let inst as SingleValueInstruction:
92-
return inst
93-
case let mvi as MultipleValueInstructionResult:
94-
return mvi.instruction
95-
default:
96-
return nil
97-
}
98-
}
99-
100-
public var definingBlock: BasicBlock? {
101-
if let inst = definingInstruction {
102-
return inst.block
103-
}
104-
if let arg = self as? Argument {
105-
return arg.block
106-
}
107-
return nil
108-
}
10996
public var ownership: Ownership { SILValue_getOwnership(bridged).ownership }
11097

11198
public var hashable: HashableValue { ObjectIdentifier(self) }
@@ -154,10 +141,16 @@ extension BridgedValue {
154141

155142
final class Undef : Value {
156143
public var definingInstruction: Instruction? { nil }
144+
public var definingBlock: BasicBlock {
145+
fatalError("undef has no defining block")
146+
}
157147
}
158148

159149
final class PlaceholderValue : Value {
160150
public var definingInstruction: Instruction? { nil }
151+
public var definingBlock: BasicBlock {
152+
fatalError("PlaceholderValue has no defining block")
153+
}
161154
}
162155

163156
extension OptionalBridgedValue {

include/swift/SIL/SILBridging.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ BridgedInstruction Operand_getUser(BridgedOperand);
229229
SwiftInt Operand_isTypeDependent(BridgedOperand);
230230

231231
std::string SILNode_debugDescription(BridgedNode node);
232-
BridgedFunction SILNode_getFunction(BridgedNode node);
233232
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
234233
BridgedType SILValue_getType(BridgedValue value);
235234
BridgedOwnership SILValue_getOwnership(BridgedValue value);

lib/SIL/Utils/SILBridging.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,6 @@ std::string SILNode_debugDescription(BridgedNode node) {
334334
return str;
335335
}
336336

337-
BridgedFunction SILNode_getFunction(BridgedNode node) {
338-
return {castToSILNode(node)->getFunction()};
339-
}
340-
341337
static Operand *castToOperand(BridgedOperand operand) {
342338
return const_cast<Operand *>(static_cast<const Operand *>(operand.op));
343339
}

0 commit comments

Comments
 (0)