Skip to content

Commit eec201c

Browse files
committed
AccessUtils: Replace the struct PointerIdentification with a computed property var PointerToAddressInst.originatingAddress
It's a simpler API.
1 parent 2137e41 commit eec201c

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/AccessUtils.swift

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -262,67 +262,53 @@ private func canBeOperandOfIndexAddr(_ value: Value) -> Bool {
262262
}
263263
}
264264

265-
/// Given a `%addr = pointer_to_address %ptr_operand` instruction tries to identify
266-
/// where the pointer operand `ptr_operand` originates from.
265+
/// Tries to identify from which address the pointer operand originates from.
267266
/// This is useful to identify patterns like
268267
/// ```
269268
/// %orig_addr = global_addr @...
270269
/// %ptr = address_to_pointer %orig_addr
271270
/// %addr = pointer_to_address %ptr
272271
/// ```
273-
struct PointerIdentification {
274-
private var walker = PointerIdentificationUseDefWalker()
272+
extension PointerToAddressInst {
273+
var originatingAddress: Value? {
275274

276-
mutating func getOriginatingAddress(of pointerToAddr: PointerToAddressInst) -> Value? {
277-
defer { walker.clear() }
275+
struct Walker : ValueUseDefWalker {
276+
let addrType: Type
277+
var result: Value?
278+
var walkUpCache = WalkerCache<Path>()
278279

279-
walker.start(pointerToAddr.type)
280-
if walker.walkUp(value: pointerToAddr.operand, path: SmallProjectionPath()) == .abortWalk {
281-
return nil
282-
}
283-
return walker.result
284-
}
280+
mutating func rootDef(value: Value, path: SmallProjectionPath) -> WalkResult {
281+
if let atp = value as? AddressToPointerInst {
282+
if let res = result, atp.operand != res {
283+
return .abortWalk
284+
}
285285

286-
private struct PointerIdentificationUseDefWalker : ValueUseDefWalker {
287-
private var addrType: Type!
288-
private(set) var result: Value?
289-
var walkUpCache = WalkerCache<Path>()
286+
if addrType != atp.operand.type { return .abortWalk }
287+
if !path.isEmpty { return .abortWalk }
290288

291-
mutating func start(_ addrType: Type) {
292-
self.addrType = addrType
293-
assert(result == nil)
294-
}
295-
296-
mutating func clear() {
297-
result = nil
298-
walkUpCache.clear()
299-
}
289+
self.result = atp.operand
290+
return .continueWalk
291+
}
292+
return .abortWalk
293+
}
300294

301-
mutating func rootDef(value: Value, path: SmallProjectionPath) -> WalkResult {
302-
if let atp = value as? AddressToPointerInst {
303-
if let res = result, atp.operand != res {
295+
mutating func walkUp(value: Value, path: SmallProjectionPath) -> WalkResult {
296+
switch value {
297+
case is BlockArgument, is MarkDependenceInst, is CopyValueInst,
298+
is StructExtractInst, is TupleExtractInst, is StructInst, is TupleInst,
299+
is FunctionArgument, is AddressToPointerInst:
300+
return walkUpDefault(value: value, path: path)
301+
default:
304302
return .abortWalk
305303
}
306-
307-
if addrType != atp.operand.type { return .abortWalk }
308-
if !path.isEmpty { return .abortWalk }
309-
310-
self.result = atp.operand
311-
return .continueWalk
312304
}
313-
return .abortWalk
314305
}
315306

316-
mutating func walkUp(value: Value, path: SmallProjectionPath) -> WalkResult {
317-
switch value {
318-
case is BlockArgument, is MarkDependenceInst, is CopyValueInst,
319-
is StructExtractInst, is TupleExtractInst, is StructInst, is TupleInst,
320-
is FunctionArgument, is AddressToPointerInst:
321-
return walkUpDefault(value: value, path: path)
322-
default:
323-
return .abortWalk
324-
}
307+
var walker = Walker(addrType: type)
308+
if walker.walkUp(value: operand, path: SmallProjectionPath()) == .abortWalk {
309+
return nil
325310
}
311+
return walker.result
326312
}
327313
}
328314

@@ -388,7 +374,6 @@ struct AccessPathWalker {
388374
private struct Walker : AddressUseDefWalker {
389375
private(set) var result = AccessPath.unidentified()
390376
private(set) var foundBeginAccess: BeginAccessInst?
391-
private var pointerId = PointerIdentification()
392377

393378
mutating func start() {
394379
result = .unidentified()
@@ -428,7 +413,7 @@ struct AccessPathWalker {
428413
assert(result.base == .unidentified, "rootDef should only called once")
429414
// Try identifying the address a pointer originates from
430415
if let p2ai = address as? PointerToAddressInst {
431-
if let originatingAddr = pointerId.getOriginatingAddress(of: p2ai) {
416+
if let originatingAddr = p2ai.originatingAddress {
432417
return walkUp(address: originatingAddr, path: path)
433418
} else {
434419
self.result = AccessPath(base: .pointer(p2ai), projectionPath: path.projectionPath)

0 commit comments

Comments
 (0)