Skip to content

Commit 7deec66

Browse files
committed
Optimizer: add two utilities in OptUtils.swift
* `MultipleValueInstruction.replace(with:)` * `eraseIfDead(functions:)`
1 parent 9bd85c6 commit 7deec66

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ extension SingleValueInstruction {
349349
}
350350
}
351351

352+
extension MultipleValueInstruction {
353+
/// Replaces all uses with the result of `replacement` and then erases the instruction.
354+
func replace(with replacement: MultipleValueInstruction, _ context: some MutatingContext) {
355+
for (origResult, newResult) in zip(self.results, replacement.results) {
356+
origResult.uses.replaceAll(with: newResult, context)
357+
}
358+
context.erase(instruction: self)
359+
}
360+
}
361+
352362
extension Instruction {
353363
var isTriviallyDead: Bool {
354364
if results.contains(where: { !$0.uses.isEmpty }) {
@@ -1004,3 +1014,20 @@ extension StoreInst: CopyLikeInstruction {
10041014
private var load: LoadInst { source as! LoadInst }
10051015
}
10061016

1017+
func eraseIfDead(functions: [Function], _ context: ModulePassContext) {
1018+
var toDelete = functions
1019+
while true {
1020+
var remaining = [Function]()
1021+
for fn in toDelete {
1022+
if !fn.isPossiblyUsedExternally && !fn.isReferencedInModule {
1023+
context.erase(function: fn)
1024+
} else {
1025+
remaining.append(fn)
1026+
}
1027+
}
1028+
if remaining.count == toDelete.count {
1029+
return
1030+
}
1031+
toDelete = remaining
1032+
}
1033+
}

0 commit comments

Comments
 (0)