Skip to content

Commit bbd15fc

Browse files
authored
Merge pull request #69152 from atrick/forwarding-utils
ForwardingUtils.swift improvements
2 parents 1751320 + 13142e3 commit bbd15fc

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/ForwardingUtils.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ private struct GatherLifetimeIntroducers : ForwardingUseDefWalker {
129129

130130
enum ForwardingUseResult: CustomStringConvertible {
131131
case operand(Operand)
132-
case deadValue(Value)
132+
case deadValue(Value, Operand?)
133133

134134
var description: String {
135135
switch self {
136136
case .operand(let operand):
137137
return operand.description
138-
case .deadValue(let deadValue):
139-
return "dead value: " + deadValue.description
138+
case .deadValue(let deadValue, let operand):
139+
var desc = "dead value: \(deadValue.description)"
140+
if let operand = operand {
141+
desc += "from: \(operand)"
142+
}
143+
return desc
140144
}
141145
}
142146
}
@@ -146,25 +150,35 @@ protocol ForwardingDefUseWalker {
146150
// Minimally, check a ValueSet. This walker may traverse chains of aggregation and destructuring by default. Implementations may handle phis.
147151
mutating func needWalk(for value: Value) -> Bool
148152

153+
mutating func leafUse(_ operand: Operand) -> WalkResult
154+
149155
// Report any initial or forwarded with no uses. Only relevant for
150156
// guaranteed values or incomplete OSSA. This could be a dead
151157
// instruction, a terminator in which the result is dead on one
152158
// path, or a dead phi.
153-
mutating func deadValue(_: Value) -> WalkResult
159+
//
160+
// \p operand is nil if \p value is the root.
161+
mutating func deadValue(_ value: Value, using operand: Operand?) -> WalkResult
154162

155-
mutating func leafUse(_: Operand) -> WalkResult
163+
mutating func walkDown(root: Value) -> WalkResult
156164

157-
mutating func walkDownUses(of: Value) -> WalkResult
165+
mutating func walkDownUses(of: Value, using: Operand?) -> WalkResult
158166

159167
mutating func walkDown(operand: Operand) -> WalkResult
160168
}
161169

162170
extension ForwardingDefUseWalker {
163-
mutating func walkDownUses(of value: Value) -> WalkResult {
164-
return walkDownUsesDefault(of: value)
171+
mutating func walkDown(root: Value) -> WalkResult {
172+
walkDownUses(of: root, using: nil)
173+
}
174+
175+
mutating func walkDownUses(of value: Value, using operand: Operand?)
176+
-> WalkResult {
177+
return walkDownUsesDefault(of: value, using: operand)
165178
}
166179

167-
mutating func walkDownUsesDefault(of value: Value) -> WalkResult {
180+
mutating func walkDownUsesDefault(of value: Value, using operand: Operand?)
181+
-> WalkResult {
168182
if !needWalk(for: value) { return .continueWalk }
169183

170184
var hasUse = false
@@ -175,7 +189,7 @@ extension ForwardingDefUseWalker {
175189
hasUse = true
176190
}
177191
if !hasUse {
178-
deadValue(value)
192+
return deadValue(value, using: operand)
179193
}
180194
return .continueWalk
181195
}
@@ -186,18 +200,18 @@ extension ForwardingDefUseWalker {
186200

187201
mutating func walkDownDefault(operand: Operand) -> WalkResult {
188202
if let inst = operand.instruction as? ForwardingInstruction {
189-
return walkDownAllResults(of: inst)
203+
return walkDownAllResults(of: inst, using: operand)
190204
}
191205
if let phi = Phi(using: operand) {
192-
return walkDownUses(of: phi.value)
206+
return walkDownUses(of: phi.value, using: operand)
193207
}
194208
return leafUse(operand)
195209
}
196210

197-
private mutating func walkDownAllResults(of inst: ForwardingInstruction)
198-
-> WalkResult {
211+
private mutating func walkDownAllResults(of inst: ForwardingInstruction,
212+
using operand: Operand?) -> WalkResult {
199213
for result in inst.forwardedResults {
200-
if walkDownUses(of: result) == .abortWalk {
214+
if walkDownUses(of: result, using: operand) == .abortWalk {
201215
return .abortWalk
202216
}
203217
}
@@ -213,7 +227,7 @@ func visitForwardedUses(introducer: Value, _ context: Context,
213227
-> WalkResult {
214228
var useVisitor = VisitForwardedUses(visitor: visitor, context)
215229
defer { useVisitor.visitedValues.deinitialize() }
216-
return useVisitor.walkDownUses(of: introducer)
230+
return useVisitor.walkDown(root: introducer)
217231
}
218232

219233
private struct VisitForwardedUses : ForwardingDefUseWalker {
@@ -234,8 +248,9 @@ private struct VisitForwardedUses : ForwardingDefUseWalker {
234248
return visitor(.operand(operand))
235249
}
236250

237-
mutating func deadValue(_ value: Value) -> WalkResult {
238-
return visitor(.deadValue(value))
251+
mutating func deadValue(_ value: Value, using operand: Operand?)
252+
-> WalkResult {
253+
return visitor(.deadValue(value, operand))
239254
}
240255
}
241256

@@ -250,8 +265,8 @@ let forwardingUseDefTest = FunctionTest("forwarding_use_def_test") {
250265
let forwardingDefUseTest = FunctionTest("forwarding_def_use_test") {
251266
function, arguments, context in
252267
let value = arguments.takeValue()
253-
_ = visitForwardedUses(introducer: value, context) { operand in
254-
print("USE: \(operand)")
268+
_ = visitForwardedUses(introducer: value, context) { useResult in
269+
print("USE: \(useResult)")
255270
return .continueWalk
256271
}
257272
}

0 commit comments

Comments
 (0)