Skip to content

Commit 84058a7

Browse files
authored
Merge pull request #81286 from eeckstein/temprvalue-opt
TempRValueElimination: re-implement the pass in swift
2 parents 2269a35 + c6b1e3e commit 84058a7

28 files changed

+1194
-1459
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ struct AliasAnalysis {
262262
case let copy as SourceDestAddrInstruction:
263263
let mayRead = memLoc.mayAlias(with: copy.source, self)
264264
let mayWrite = memLoc.mayAlias(with: copy.destination, self)
265-
var effects = SideEffects.Memory(read: mayRead, write: mayWrite || (mayRead && copy.isTakeOfSrc))
266-
if !copy.isInitializationOfDest {
265+
var effects = SideEffects.Memory(read: mayRead, write: mayWrite || (mayRead && copy.isTakeOfSource))
266+
if !copy.isInitializationOfDestination {
267267
effects.merge(with: defaultEffects(of: copy, on: memLoc))
268268
}
269269
return effects

SwiftCompilerSources/Sources/Optimizer/DataStructures/Set.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,44 @@ struct SpecificInstructionSet<InstType: Instruction> : IntrusiveSet {
177177
}
178178
}
179179

180+
/// An `InstructionSet` which also provides a `count` property.
181+
struct SpecificInstructionSetWithCount<InstType: Instruction> : IntrusiveSet {
182+
private(set) var count = 0
183+
private var underlyingSet: SpecificInstructionSet<InstType>
184+
185+
init(_ context: some Context) {
186+
self.underlyingSet = SpecificInstructionSet(context)
187+
}
188+
189+
func contains(_ inst: InstType) -> Bool { underlyingSet.contains(inst) }
190+
191+
var isEmpty: Bool { count == 0 }
192+
193+
/// Returns true if `inst` was not contained in the set before inserting.
194+
@discardableResult
195+
mutating func insert(_ inst: InstType) -> Bool {
196+
if underlyingSet.insert(inst) {
197+
count += 1
198+
return true
199+
}
200+
return false
201+
}
202+
203+
mutating func erase(_ inst: InstType) {
204+
if underlyingSet.contains(inst) {
205+
count -= 1
206+
assert(count >= 0)
207+
}
208+
underlyingSet.erase(inst)
209+
}
210+
211+
var description: String { underlyingSet.description }
212+
213+
mutating func deinitialize() { underlyingSet.deinitialize() }
214+
}
215+
180216
typealias InstructionSet = SpecificInstructionSet<Instruction>
217+
typealias InstructionSetWithCount = SpecificInstructionSetWithCount<Instruction>
181218

182219
/// A set of operands.
183220
///

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ swift_compiler_sources(Optimizer
3333
SimplificationPasses.swift
3434
StackPromotion.swift
3535
StripObjectHeaders.swift
36+
TempRValueElimination.swift
3637
)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ private struct CollectedEffects {
108108
addEffects(.read, to: copy.source)
109109
addEffects(.write, to: copy.destination)
110110

111-
if !copy.isTakeOfSrc {
111+
if !copy.isTakeOfSource {
112112
addEffects(.copy, to: copy.source)
113113
}
114-
if !copy.isInitializationOfDest {
114+
if !copy.isInitializationOfDestination {
115115
addDestroyEffects(ofAddress: copy.destination)
116116
}
117117

@@ -494,7 +494,7 @@ private struct ArgumentEscapingWalker : ValueDefUseWalker, AddressDefUseWalker {
494494
case let copy as CopyAddrInst:
495495
if address == copy.sourceOperand &&
496496
!address.value.hasTrivialType &&
497-
(!function.hasOwnership || copy.isTakeOfSrc) {
497+
(!function.hasOwnership || copy.isTakeOfSource) {
498498
foundTakingLoad = true
499499
}
500500
return .continueWalk

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LetPropertyLowering.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private func constructLetInitRegion(
153153

154154
case let copy as CopyAddrInst
155155
where copy.destination.isLetFieldAddress(of: markUninitialized):
156-
assert(copy.isInitializationOfDest)
156+
assert(copy.isInitializationOfDestination)
157157
initRegion.insert(inst)
158158

159159
case let beginAccess as BeginAccessInst

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/NamedReturnValueOptimization.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private func findCopyForNRVO(for outArg: FunctionArgument) -> CopyAddrInst? {
7878
// %local = alloc_stack $T
7979
// store %in to %local : $*T
8080
// copy_addr %local to [init] %out : $*T
81-
if !copyToArg.isTakeOfSrc {
81+
if !copyToArg.isTakeOfSource {
8282
return nil
8383
}
8484

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extension CopyAddrInst : LoadingInstruction {
158158
return false
159159
}
160160
if !parentFunction.hasOwnership {
161-
if !isTakeOfSrc || !isInitializationOfDest {
161+
if !isTakeOfSource || !isInitializationOfDestination {
162162
// For simplicity, bail if we would have to insert compensating retains and releases.
163163
return false
164164
}

0 commit comments

Comments
 (0)