SwiftCompilerSources: add the InstructionRange.insert(borrowScopeOf:) utility

Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
This commit is contained in:
Erik Eckstein
2023-11-27 15:31:07 +01:00
parent e4d227a5fb
commit 9eb5da4a0f

View File

@@ -532,3 +532,25 @@ extension GlobalVariable {
}
}
}
extension InstructionRange {
/// Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
mutating func insert(borrowScopeOf borrow: BorrowIntroducingInstruction, _ context: some Context) {
var worklist = ValueWorklist(context)
defer { worklist.deinitialize() }
worklist.pushIfNotVisited(borrow)
while let value = worklist.pop() {
for use in value.uses {
switch use.instruction {
case let endBorrow as EndBorrowInst:
self.insert(endBorrow)
case let branch as BranchInst:
worklist.pushIfNotVisited(branch.getArgument(for: use))
default:
break
}
}
}
}
}