@@ -262,67 +262,53 @@ private func canBeOperandOfIndexAddr(_ value: Value) -> Bool {
262
262
}
263
263
}
264
264
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.
267
266
/// This is useful to identify patterns like
268
267
/// ```
269
268
/// %orig_addr = global_addr @...
270
269
/// %ptr = address_to_pointer %orig_addr
271
270
/// %addr = pointer_to_address %ptr
272
271
/// ```
273
- struct PointerIdentification {
274
- private var walker = PointerIdentificationUseDefWalker ( )
272
+ extension PointerToAddressInst {
273
+ var originatingAddress : Value ? {
275
274
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 > ( )
278
279
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
+ }
285
285
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 }
290
288
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
+ }
300
294
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 :
304
302
return . abortWalk
305
303
}
306
-
307
- if addrType != atp. operand. type { return . abortWalk }
308
- if !path. isEmpty { return . abortWalk }
309
-
310
- self . result = atp. operand
311
- return . continueWalk
312
304
}
313
- return . abortWalk
314
305
}
315
306
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
325
310
}
311
+ return walker. result
326
312
}
327
313
}
328
314
@@ -388,7 +374,6 @@ struct AccessPathWalker {
388
374
private struct Walker : AddressUseDefWalker {
389
375
private( set) var result = AccessPath . unidentified ( )
390
376
private( set) var foundBeginAccess : BeginAccessInst ?
391
- private var pointerId = PointerIdentification ( )
392
377
393
378
mutating func start( ) {
394
379
result = . unidentified( )
@@ -428,7 +413,7 @@ struct AccessPathWalker {
428
413
assert ( result. base == . unidentified, " rootDef should only called once " )
429
414
// Try identifying the address a pointer originates from
430
415
if let p2ai = address as? PointerToAddressInst {
431
- if let originatingAddr = pointerId . getOriginatingAddress ( of : p2ai) {
416
+ if let originatingAddr = p2ai. originatingAddress {
432
417
return walkUp ( address: originatingAddr, path: path)
433
418
} else {
434
419
self . result = AccessPath ( base: . pointer( p2ai) , projectionPath: path. projectionPath)
0 commit comments