Skip to content

Commit 2c84de3

Browse files
committed
Optimizer: add a few "lookThrough" utilities
* move `Value.lookThoughOwnershipInstructions` from the ObjCBridgingOptimization pass to OptUtils * add `lookThroughBorrow` and `lookThroughCopy`
1 parent 9f33d3e commit 2c84de3

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private func optimizeOptionalBridging(forArgumentOf block: BasicBlock,
8484
}
8585

8686
// Check for the first ObjC -> swift bridging operation.
87-
let swiftValue = lookThroughOwnershipInsts(swiftValueSwitch.enumOp)
87+
let swiftValue = swiftValueSwitch.enumOp.lookThoughOwnershipInstructions
8888
guard let originalObjCValueSwitch = isOptionalBridging(of: swiftValue, isBridging: isBridgeToSwiftCall) else {
8989
return true
9090
}
@@ -126,7 +126,7 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
126126
return true
127127
}
128128

129-
let swiftValue = lookThroughOwnershipInsts(bridgeToObjcCall.arguments[0])
129+
let swiftValue = bridgeToObjcCall.arguments[0].lookThoughOwnershipInstructions
130130

131131
// Handle the first case: the ObjC -> swift bridging operation is optional and the swift -> ObjC
132132
// bridging is within a test for Optional.some, e.g.
@@ -245,18 +245,6 @@ private func removeBridgingCodeInPredecessors(of block: BasicBlock, _ context: F
245245
}
246246
}
247247

248-
private func lookThroughOwnershipInsts(_ value: Value) -> Value {
249-
// Looks like it's sufficient to support begin_borrow and copy_value for now.
250-
// TODO: add move_value if needed.
251-
if let bbi = value as? BeginBorrowInst {
252-
return bbi.borrowedValue
253-
}
254-
if let cvi = value as? CopyValueInst {
255-
return cvi.fromValue
256-
}
257-
return value
258-
}
259-
260248
/// Checks for an optional bridging `switch_enum` diamond.
261249
///
262250
/// ```

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ extension Value {
1818
uses.lazy.filter { !($0.instruction is DebugValueInst) }
1919
}
2020

21+
var lookThroughBorrow: Value {
22+
if let beginBorrow = self as? BeginBorrowInst {
23+
return beginBorrow.borrowedValue.lookThroughBorrow
24+
}
25+
return self
26+
}
27+
28+
var lookThroughCopy: Value {
29+
if let copy = self as? CopyValueInst {
30+
return copy.fromValue.lookThroughCopy
31+
}
32+
return self
33+
}
34+
35+
var lookThoughOwnershipInstructions: Value {
36+
switch self {
37+
case let beginBorrow as BeginBorrowInst:
38+
return beginBorrow.borrowedValue.lookThoughOwnershipInstructions
39+
case let copy as CopyValueInst:
40+
return copy.fromValue.lookThoughOwnershipInstructions
41+
case let move as MoveValueInst:
42+
return move.fromValue.lookThoughOwnershipInstructions
43+
default:
44+
return self
45+
}
46+
}
47+
2148
/// Walks over all fields of an aggregate and checks if a reference count
2249
/// operation for this value is required. This differs from a simple `Type.isTrivial`
2350
/// check, because it treats a value_to_bridge_object instruction as "trivial".

0 commit comments

Comments
 (0)