Skip to content

Commit 3e75e77

Browse files
committed
Add AccessBaseAndScopes API: outerAddress, enclosingAddress.
1 parent 255ccfa commit 3e75e77

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

SwiftCompilerSources/Sources/SIL/Utilities/AccessUtils.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public enum AccessBase : CustomStringConvertible, Hashable {
5555
case stack(AllocStackInst)
5656

5757
/// The address of a global variable.
58+
///
59+
/// TODO: make this payload the global address. Make AccessBase.address non-optional. Make AccessBase comparison see
60+
/// though things like project_box and global_addr. Then cleanup APIs like LifetimeDependence.Scope that carry extra
61+
/// address values around.
5862
case global(GlobalVariable)
5963

6064
/// The address of a stored property of a class instance.
@@ -501,13 +505,38 @@ public enum EnclosingAccessScope {
501505
case access(BeginAccessInst)
502506
case base(AccessBase)
503507
case dependence(MarkDependenceInst)
508+
509+
// TODO: make this non-optional after fixing AccessBase.global.
510+
public var address: Value? {
511+
switch self {
512+
case let .access(beginAccess):
513+
return beginAccess
514+
case let .base(accessBase):
515+
return accessBase.address
516+
case let .dependence(markDep):
517+
return markDep
518+
}
519+
}
504520
}
505521

506522
// An AccessBase with the nested enclosing scopes that contain the original address in bottom-up order.
507523
public struct AccessBaseAndScopes {
508524
public let base: AccessBase
509525
public let scopes: SingleInlineArray<EnclosingAccessScope>
510526

527+
public init(base: AccessBase, scopes: SingleInlineArray<EnclosingAccessScope>) {
528+
self.base = base
529+
self.scopes = scopes
530+
}
531+
532+
public var outerAddress: Value? {
533+
base.address ?? scopes.last?.address
534+
}
535+
536+
public var enclosingAccess: EnclosingAccessScope {
537+
return scopes.first ?? .base(base)
538+
}
539+
511540
public var innermostAccess: BeginAccessInst? {
512541
for scope in scopes {
513542
if case let .access(beginAccess) = scope {
@@ -518,6 +547,20 @@ public struct AccessBaseAndScopes {
518547
}
519548
}
520549

550+
extension AccessBaseAndScopes {
551+
// This must return false if a mark_dependence scope is present.
552+
public var isOnlyReadAccess: Bool {
553+
scopes.allSatisfy(
554+
{
555+
if case let .access(beginAccess) = $0 {
556+
return beginAccess.accessKind == .read
557+
}
558+
// preserve any dependence scopes.
559+
return false
560+
})
561+
}
562+
}
563+
521564
extension BeginAccessInst {
522565
// Recognize an access scope for a unsafe addressor:
523566
// %adr = pointer_to_address

0 commit comments

Comments
 (0)