Skip to content

Commit cd7c533

Browse files
committed
Optimizer: add SingleValueInstruction.replace(with:) and use it throughout the optimizer
It replaces all uses and then erases the instruction.
1 parent ca7facd commit cd7c533

13 files changed

+41
-58
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocVectorLowering.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ private func createOutlinedGlobal(
250250
let globalAddr = builder.createGlobalAddr(global: outlinedGlobal, dependencyToken: nil)
251251
let rawVectorPointer = builder.createAddressToPointer(address: globalAddr, pointerType: allocVectorBuiltin.type,
252252
needStackProtection: false)
253-
allocVectorBuiltin.uses.replaceAll(with: rawVectorPointer, context)
254-
context.erase(instruction: allocVectorBuiltin)
253+
allocVectorBuiltin.replace(with: rawVectorPointer, context)
255254
}
256255

257256
private func createStackAllocatedVector(
@@ -266,8 +265,7 @@ private func createStackAllocatedVector(
266265
let rawVectorPointer = builder.createAddressToPointer(address: allocVec, pointerType: allocVectorBuiltin.type,
267266
needStackProtection: true)
268267

269-
allocVectorBuiltin.uses.replaceAll(with: rawVectorPointer, context)
270-
context.erase(instruction: allocVectorBuiltin)
268+
allocVectorBuiltin.replace(with: rawVectorPointer, context)
271269

272270
for endInst in liverange.ends {
273271
let builder = Builder(after: endInst, context)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ private func rewriteApplyInstruction(using specializedCallee: Function, callSite
301301
}
302302
}
303303

304-
oldApply.uses.replaceAll(with: newApply, context)
305-
context.erase(instruction: oldApply)
304+
oldApply.replace(with: newApply, context)
306305
}
307306

308307
// ===================== Utility functions and extensions ===================== //
@@ -1381,4 +1380,4 @@ let rewrittenCallerBodyTest = FunctionTest("closure_specialize_rewritten_caller_
13811380
print("Rewritten caller body for: \(function.name):")
13821381
print("\(function)\n")
13831382
}
1384-
}
1383+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CopyToBorrowOptimization.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ private extension LoadInst {
260260
private func remove(copy: CopyValueInst, collectedUses: Uses, liverange: InstructionRange) {
261261
let context = collectedUses.context
262262
replaceMoveWithBorrow(of: copy, replacedBy: copy.fromValue, liverange: liverange, collectedUses: collectedUses)
263-
copy.uses.replaceAll(with: copy.fromValue, context)
264-
context.erase(instruction: copy)
263+
copy.replace(with: copy.fromValue, context)
265264

266265
for forwardingUse in collectedUses.forwardingUses {
267266
forwardingUse.changeOwnership(from: .owned, to: .guaranteed, context)
@@ -301,8 +300,7 @@ private func replaceMoveWithBorrow(
301300
isLexical: moveInst.isLexical,
302301
hasPointerEscape: moveInst.hasPointerEscape,
303302
isFromVarDecl: moveInst.isFromVarDecl)
304-
moveInst.uses.replaceAll(with: bbi, context)
305-
context.erase(instruction: moveInst)
303+
moveInst.replace(with: bbi, context)
306304
createEndBorrows(for: bbi, atEndOf: liverange, collectedUses: collectedUses)
307305
}
308306

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
152152
let replacement = builder.createUncheckedEnumData(enum: optionalReplacement,
153153
caseIndex: someCase,
154154
resultType: bridgeToObjcCall.type)
155-
bridgeToObjcCall.uses.replaceAll(with: replacement, context)
156-
context.erase(instruction: bridgeToObjcCall)
155+
bridgeToObjcCall.replace(with: replacement, context)
157156
return true
158157
}
159158

@@ -219,8 +218,7 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
219218

220219
// Now replace the bridged value with the original value in the destination block.
221220
let replacement = s.makeAvailable(in: bridgeToObjcCall.parentBlock, context)
222-
bridgeToObjcCall.uses.replaceAll(with: replacement, context)
223-
context.erase(instruction: bridgeToObjcCall)
221+
bridgeToObjcCall.replace(with: replacement, context)
224222
return true
225223
}
226224

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,7 @@ private func replace(object allocRef: AllocRefInstBase,
413413
}
414414

415415
rewriteUses(of: allocRef, context)
416-
allocRef.uses.replaceAll(with: globalValue, context)
417-
context.erase(instruction: allocRef)
416+
allocRef.replace(with: globalValue, context)
418417
return globalValue
419418
}
420419

@@ -431,13 +430,11 @@ private func rewriteUses(of startValue: Value, _ context: FunctionPassContext) {
431430
if !beginDealloc.parentFunction.hasOwnership {
432431
builder.createStrongRelease(operand: beginDealloc.reference)
433432
}
434-
beginDealloc.uses.replaceAll(with: beginDealloc.reference, context)
435-
context.erase(instruction: beginDealloc)
433+
beginDealloc.replace(with: beginDealloc.reference, context)
436434
case is EndCOWMutationInst, is EndInitLetRefInst, is MoveValueInst:
437435
let svi = inst as! SingleValueInstruction
438436
worklist.pushIfNotVisited(usersOf: svi)
439-
svi.uses.replaceAll(with: svi.operands[0].value, context)
440-
context.erase(instruction: svi)
437+
svi.replace(with: svi.operands[0].value, context)
441438
case let upCast as UpcastInst:
442439
worklist.pushIfNotVisited(usersOf: upCast)
443440
case let refCast as UncheckedRefCastInst:
@@ -571,8 +568,7 @@ private func replace(findStringCall: ApplyInst,
571568
findStringCall.arguments[1],
572569
cacheAddr])
573570

574-
findStringCall.uses.replaceAll(with: newCall, context)
575-
context.erase(instruction: findStringCall)
571+
findStringCall.replace(with: newCall, context)
576572
}
577573

578574
private extension GlobalValueInst {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ private func replace(load: LoadInst, with availableValues: [AvailableValue], _ c
279279
//
280280
newValue = ssaUpdater.getValue(inMiddleOf: load.parentBlock)
281281
}
282-
load.uses.replaceAll(with: newValue, context)
283-
context.erase(instruction: load)
282+
load.replace(with: newValue, context)
284283
}
285284

286285
private func provideValue(

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyApply.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ private func tryTransformThickToThinCallee(of apply: ApplyInst, _ context: Simpl
5656
isNonThrowing: apply.isNonThrowing,
5757
isNonAsync: apply.isNonAsync,
5858
specializationInfo: apply.specializationInfo)
59-
apply.uses.replaceAll(with: newApply, context)
60-
context.erase(instruction: apply)
59+
apply.replace(with: newApply, context)
6160
return true
6261
}
6362
return false

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginAndLoadBorrow.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ extension LoadBorrowInst : Simplifyable, SILCombineSimplifyable {
5959
}
6060
let builder = Builder(before: self, context)
6161
let loadCopy = builder.createLoad(fromAddress: address, ownership: .copy)
62-
let forwardedOwnedValue = replace(guaranteedValue: self, withOwnedValue: loadCopy, context)
63-
copy.uses.replaceAll(with: forwardedOwnedValue, context)
64-
context.erase(instruction: copy)
62+
let forwardedOwnedValue = replaceGuaranteed(value: self, withOwnedValue: loadCopy, context)
63+
copy.replace(with: forwardedOwnedValue, context)
6564
context.erase(instructionIncludingAllUsers: self)
6665
}
6766
}
@@ -118,9 +117,8 @@ private func tryReplaceCopy(
118117
}
119118
let builder = Builder(before: beginBorrow, context)
120119
let copiedOperand = builder.createCopyValue(operand: beginBorrow.borrowedValue)
121-
let forwardedOwnedValue = replace(guaranteedValue: beginBorrow, withOwnedValue: copiedOperand, context)
122-
copy.uses.replaceAll(with: forwardedOwnedValue, context)
123-
context.erase(instruction: copy)
120+
let forwardedOwnedValue = replaceGuaranteed(value: beginBorrow, withOwnedValue: copiedOperand, context)
121+
copy.replace(with: forwardedOwnedValue, context)
124122
context.erase(instructionIncludingAllUsers: beginBorrow)
125123
return true
126124
}
@@ -140,7 +138,7 @@ private func tryReplaceCopy(
140138
/// destroy_value %2
141139
/// ```
142140
private func convertAllUsesToOwned(of beginBorrow: BeginBorrowInst, _ context: SimplifyContext) {
143-
let forwardedOwnedValue = replace(guaranteedValue: beginBorrow, withOwnedValue: beginBorrow.borrowedValue, context)
141+
let forwardedOwnedValue = replaceGuaranteed(value: beginBorrow, withOwnedValue: beginBorrow.borrowedValue, context)
144142
beginBorrow.borrowedValue.replaceAllDestroys(with: forwardedOwnedValue, context)
145143
context.erase(instructionIncludingAllUsers: beginBorrow)
146144
}
@@ -216,29 +214,29 @@ private extension ForwardingInstruction {
216214
///
217215
/// Returns the last owned value in a forwarding-chain or `ownedValue` if `guaranteedValue` has
218216
/// no forwarding uses.
219-
private func replace(guaranteedValue: Value, withOwnedValue ownedValue: Value, _ context: SimplifyContext) -> Value {
217+
private func replaceGuaranteed(value: Value, withOwnedValue ownedValue: Value, _ context: SimplifyContext) -> Value {
220218
var result = ownedValue
221219
var numForwardingUses = 0
222-
for use in guaranteedValue.uses {
220+
for use in value.uses {
223221

224222
switch use.instruction {
225223
case let tei as TupleExtractInst:
226224
numForwardingUses += 1
227225
let dti = Builder(before: tei, context).createDestructureTuple(tuple: ownedValue)
228-
result = replace(guaranteedValue: tei, withOwnedValue: dti.results[tei.fieldIndex], context)
226+
result = replaceGuaranteed(value: tei, withOwnedValue: dti.results[tei.fieldIndex], context)
229227
context.erase(instruction: tei)
230228
case let sei as StructExtractInst:
231229
numForwardingUses += 1
232230
let dsi = Builder(before: sei, context).createDestructureStruct(struct: ownedValue)
233-
result = replace(guaranteedValue: sei, withOwnedValue: dsi.results[sei.fieldIndex], context)
231+
result = replaceGuaranteed(value: sei, withOwnedValue: dsi.results[sei.fieldIndex], context)
234232
context.erase(instruction: sei)
235233
case let fwdInst as (SingleValueInstruction & ForwardingInstruction) where
236234
fwdInst.isSingleForwardedOperand(use):
237235
// Other forwarding instructions beside tuple_extract and struct_extract
238236
numForwardingUses += 1
239237
use.set(to: ownedValue, context)
240238
fwdInst.setForwardingOwnership(to: .owned, context)
241-
result = replace(guaranteedValue: fwdInst, withOwnedValue: fwdInst, context)
239+
result = replaceGuaranteed(value: fwdInst, withOwnedValue: fwdInst, context)
242240
case is EndBorrowInst:
243241
break
244242
default:

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginCOWMutation.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private extension BeginCOWMutationInst {
8686

8787
for use in buffer.uses.ignoreDebugUses {
8888
let endCOW = use.instruction as! EndCOWMutationInst
89-
endCOW.uses.replaceAll(with: instance, context)
90-
context.erase(instruction: endCOW)
89+
endCOW.replace(with: instance, context)
9190
}
9291
context.erase(instructionIncludingDebugUses: self)
9392
return true

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBranch.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ private extension BranchInst {
5353
if let phi = Phi(arg),
5454
let bfi = phi.borrowedFrom
5555
{
56-
bfi.uses.replaceAll(with: op.value, context)
57-
context.erase(instruction: bfi)
56+
bfi.replace(with: op.value, context)
5857
} else {
5958
arg.uses.replaceAll(with: op.value, context)
6059
}

0 commit comments

Comments
 (0)