Skip to content

Commit ca7facd

Browse files
committed
SIL: add some use-list APIs
* `users`: maps from a sequence of operands to a sequence of instructions * `users(ofType:)` : as `users`, but filters users of a certain instruction type * `Value.users`: = `Value.uses.users`
1 parent 1545e01 commit ca7facd

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private func rewriteUses(of startValue: Value, _ context: FunctionPassContext) {
454454

455455
private extension InstructionWorklist {
456456
mutating func pushIfNotVisited(usersOf value: Value) {
457-
pushIfNotVisited(contentsOf: value.uses.lazy.map { $0.instruction })
457+
pushIfNotVisited(contentsOf: value.users)
458458
}
459459
}
460460

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ extension Value {
236236
-> Bool {
237237
var users = InstructionSet(context)
238238
defer { users.deinitialize() }
239-
uses.lazy.map({ $0.instruction }).forEach { users.insert($0) }
239+
users.insert(contentsOf: self.users)
240240

241241
var worklist = InstructionWorklist(context)
242242
defer { worklist.deinitialize() }

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,10 @@ final public class AllocStackInst : SingleValueInstruction, Allocation, DebugVar
12481248
public var debugVariable: DebugVariable {
12491249
return bridged.AllocStack_getVarInfo()
12501250
}
1251+
1252+
public var deallocations: LazyMapSequence<LazyFilterSequence<UseList>, Instruction> {
1253+
uses.users(ofType: DeallocStackInst.self)
1254+
}
12511255
}
12521256

12531257
final public class AllocVectorInst : SingleValueInstruction, Allocation, UnaryInstruction {
@@ -1317,7 +1321,7 @@ extension Instruction {
13171321
/// Return the sequence of use points of any instruction.
13181322
public var endInstructions: EndInstructions {
13191323
if let scopedInst = self as? ScopedInstruction {
1320-
return .scoped(scopedInst.endOperands.map({ $0.instruction }))
1324+
return .scoped(scopedInst.endOperands.users)
13211325
}
13221326
return .single(self)
13231327
}
@@ -1366,6 +1370,10 @@ final public class StoreBorrowInst : SingleValueInstruction, StoringInstruction,
13661370
}
13671371
return dest as! AllocStackInst
13681372
}
1373+
1374+
public var endBorrows: LazyMapSequence<LazyFilterSequence<UseList>, Instruction> {
1375+
uses.users(ofType: EndBorrowInst.self)
1376+
}
13691377
}
13701378

13711379
final public class BeginAccessInst : SingleValueInstruction, UnaryInstruction {

SwiftCompilerSources/Sources/SIL/Operand.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ extension Sequence where Element == Operand {
169169
public var endingLifetime: LazyFilterSequence<Self> {
170170
return self.lazy.filter { $0.endsLifetime }
171171
}
172+
173+
public var users: LazyMapSequence<Self, Instruction> {
174+
return self.lazy.map { $0.instruction }
175+
}
176+
177+
// This intentinally returns a Sequence of `Instruction` and not a Sequence of `I` to be able to use
178+
// it as argument to `InstructionSet.insert(contentsOf:)`.
179+
public func users<I: Instruction>(ofType: I.Type) -> LazyMapSequence<LazyFilterSequence<Self>, Instruction> {
180+
self.lazy.filter{ $0.instruction is I }.users
181+
}
182+
}
183+
184+
extension Value {
185+
public var users: LazyMapSequence<UseList, Instruction> { uses.users }
172186
}
173187

174188
extension Operand {

0 commit comments

Comments
 (0)