Skip to content

Commit 9eb5da4

Browse files
committed
SwiftCompilerSources: add the InstructionRange.insert(borrowScopeOf:) utility
Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
1 parent e4d227a commit 9eb5da4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,25 @@ extension GlobalVariable {
532532
}
533533
}
534534
}
535+
536+
extension InstructionRange {
537+
/// Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
538+
mutating func insert(borrowScopeOf borrow: BorrowIntroducingInstruction, _ context: some Context) {
539+
var worklist = ValueWorklist(context)
540+
defer { worklist.deinitialize() }
541+
542+
worklist.pushIfNotVisited(borrow)
543+
while let value = worklist.pop() {
544+
for use in value.uses {
545+
switch use.instruction {
546+
case let endBorrow as EndBorrowInst:
547+
self.insert(endBorrow)
548+
case let branch as BranchInst:
549+
worklist.pushIfNotVisited(branch.getArgument(for: use))
550+
default:
551+
break
552+
}
553+
}
554+
}
555+
}
556+
}

0 commit comments

Comments
 (0)