1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
//===--- AliasAnalysis.swift - the alias analysis -------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import OptimizerBridging
import SIL
struct AliasAnalysis {
let bridged: BridgedAliasAnalysis
/// Returns the correct path for address-alias functions.
static func getPtrOrAddressPath(for value: Value) -> SmallProjectionPath {
let ty = value.type
if ty.isAddress {
// This is the regular case: the path selects any sub-fields of an address.
return SmallProjectionPath(.anyValueFields)
}
// Some optimizations use the address-alias APIs with non-address SIL values.
// TODO: this is non-intuitive and we should eliminate those API uses.
if ty.isClass {
// If the value is a (non-address) reference it means: all addresses within the class instance.
return SmallProjectionPath(.anyValueFields).push(.anyClassField)
}
// Any other non-address value means: all addresses of any referenced class instances within the value.
return SmallProjectionPath(.anyValueFields).push(.anyClassField).push(.anyValueFields)
}
static func register() {
BridgedAliasAnalysis.registerAnalysis(
// getMemEffectsFn
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction, complexityBudget: Int) -> BridgedMemoryBehavior in
let context = FunctionPassContext(_bridged: bridgedCtxt)
let inst = bridgedInst.instruction
let val = bridgedVal.value
let path = AliasAnalysis.getPtrOrAddressPath(for: val)
switch inst {
case let apply as ApplySite:
return getMemoryEffect(ofApply: apply, for: val, path: path, context).bridged
case let builtin as BuiltinInst:
return getMemoryEffect(ofBuiltin: builtin, for: val, path: path, context).bridged
default:
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true),
complexityBudget: complexityBudget, context) {
return .MayReadWrite
}
return .None
}
},
// isObjReleasedFn
{ (bridgedCtxt: BridgedPassContext, bridgedObj: BridgedValue, bridgedInst: BridgedInstruction, complexityBudget: Int) -> Bool in
let context = FunctionPassContext(_bridged: bridgedCtxt)
let inst = bridgedInst.instruction
let obj = bridgedObj.value
let path = SmallProjectionPath(.anyValueFields)
if let apply = inst as? ApplySite {
// Workaround for quadratic complexity in ARCSequenceOpts.
// We need to use an ever lower budget to not get into noticeable compile time troubles.
let budget = complexityBudget / 10
let effect = getOwnershipEffect(of: apply, for: obj, path: path, complexityBudget: budget, context)
return effect.destroy
}
return obj.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: false),
complexityBudget: complexityBudget, context)
},
// isAddrVisibleFromObj
{ (bridgedCtxt: BridgedPassContext, bridgedAddr: BridgedValue, bridgedObj: BridgedValue, complexityBudget: Int) -> Bool in
let context = FunctionPassContext(_bridged: bridgedCtxt)
let addr = bridgedAddr.value.at(AliasAnalysis.getPtrOrAddressPath(for: bridgedAddr.value))
// This is similar to `canReferenceSameFieldFn`, except that all addresses of all objects are
// considered which are transitively visible from `bridgedObj`.
let anythingReachableFromObj = bridgedObj.value.at(SmallProjectionPath(.anything))
return addr.canAddressAlias(with: anythingReachableFromObj, complexityBudget: complexityBudget, context)
},
// canReferenceSameFieldFn
{ (bridgedCtxt: BridgedPassContext, bridgedLhs: BridgedValue, bridgedRhs: BridgedValue) -> Bool in
let context = FunctionPassContext(_bridged: bridgedCtxt)
// If `lhs` or `rhs` is not an address, but an object, it means: check for alias of any class
// field address of the object.
let lhs = bridgedLhs.value.at(AliasAnalysis.getPtrOrAddressPath(for: bridgedLhs.value))
let rhs = bridgedRhs.value.at(AliasAnalysis.getPtrOrAddressPath(for: bridgedRhs.value))
return lhs.canAddressAlias(with: rhs, context)
}
)
}
}
extension Instruction {
func mayRead(fromAddress: Value, _ aliasAnalysis: AliasAnalysis) -> Bool {
switch aliasAnalysis.bridged.getMemBehavior(bridged, fromAddress.bridged) {
case .MayRead, .MayReadWrite, .MayHaveSideEffects:
return true
default:
return false
}
}
func mayWrite(toAddress: Value, _ aliasAnalysis: AliasAnalysis) -> Bool {
switch aliasAnalysis.bridged.getMemBehavior(bridged, toAddress.bridged) {
case .MayWrite, .MayReadWrite, .MayHaveSideEffects:
return true
default:
return false
}
}
func mayReadOrWrite(address: Value, _ aliasAnalysis: AliasAnalysis) -> Bool {
switch aliasAnalysis.bridged.getMemBehavior(bridged, address.bridged) {
case .MayRead, .MayWrite, .MayReadWrite, .MayHaveSideEffects:
return true
default:
return false
}
}
}
private func getMemoryEffect(ofApply apply: ApplySite, for address: Value, path: SmallProjectionPath, _ context: FunctionPassContext) -> SideEffects.Memory {
let calleeAnalysis = context.calleeAnalysis
let visitor = SideEffectsVisitor(apply: apply, calleeAnalysis: calleeAnalysis, isAddress: true)
let memoryEffects: SideEffects.Memory
// First try to figure out to which argument(s) the address "escapes" to.
if let result = address.at(path).visit(using: visitor, context) {
// The resulting effects are the argument effects to which `address` escapes to.
memoryEffects = result.memory
} else {
// `address` has unknown escapes. So we have to take the global effects of the called function(s).
memoryEffects = calleeAnalysis.getSideEffects(ofApply: apply).memory
}
// Do some magic for `let` variables. Function calls cannot modify let variables.
// The only exception is that the let variable is directly passed to an indirect out of the
// apply.
// TODO: make this a more formal and verified approach.
if memoryEffects.write && address.accessBase.isLet && !address.isIndirectResult(of: apply) {
return SideEffects.Memory(read: memoryEffects.read, write: false)
}
return memoryEffects
}
private func getMemoryEffect(ofBuiltin builtin: BuiltinInst, for address: Value, path: SmallProjectionPath, _ context: FunctionPassContext) -> SideEffects.Memory {
switch builtin.id {
case .Once, .OnceWithContext:
if !address.at(path).isEscaping(using: AddressVisibleByBuiltinOnceVisitor(), context) {
return SideEffects.Memory()
}
let callee = builtin.operands[1].value
return context.calleeAnalysis.getSideEffects(ofCallee: callee).memory
default:
if address.at(path).isEscaping(using: EscapesToInstructionVisitor(target: builtin, isAddress: true), context) {
return builtin.memoryEffects
}
return SideEffects.Memory()
}
}
private func getOwnershipEffect(of apply: ApplySite, for value: Value, path: SmallProjectionPath,
complexityBudget: Int, _ context: FunctionPassContext) -> SideEffects.Ownership {
let visitor = SideEffectsVisitor(apply: apply, calleeAnalysis: context.calleeAnalysis, isAddress: false)
if let result = value.at(path).visit(using: visitor, complexityBudget: complexityBudget, context) {
// The resulting effects are the argument effects to which `value` escapes to.
return result.ownership
} else {
// `value` has unknown escapes. So we have to take the global effects of the called function(s).
return visitor.calleeAnalysis.getSideEffects(ofApply: apply).ownership
}
}
private struct SideEffectsVisitor : EscapeVisitorWithResult {
let apply: ApplySite
let calleeAnalysis: CalleeAnalysis
let isAddress: Bool
var result = SideEffects.GlobalEffects()
mutating func visitUse(operand: Operand, path: EscapePath) -> UseResult {
let user = operand.instruction
if user is ReturnInst {
// Anything which is returned cannot escape to an instruction inside the function.
return .ignore
}
if user == apply {
let e = calleeAnalysis.getSideEffects(of: apply, operand: operand, path: path.projectionPath)
result.merge(with: e)
}
return .continueWalk
}
var followTrivialTypes: Bool { isAddress }
var followLoads: Bool { !isAddress }
}
private struct AddressVisibleByBuiltinOnceVisitor : EscapeVisitor {
var followTrivialTypes: Bool { true }
var followLoads: Bool { false }
}
/// Lets `ProjectedValue.isEscaping` return true if the value is "escaping" to the `target` instruction.
private struct EscapesToInstructionVisitor : EscapeVisitor {
let target: Instruction
let isAddress: Bool
mutating func visitUse(operand: Operand, path: EscapePath) -> UseResult {
let user = operand.instruction
if user == target {
return .abort
}
if user is ReturnInst {
// Anything which is returned cannot escape to an instruction inside the function.
return .ignore
}
return .continueWalk
}
var followTrivialTypes: Bool { isAddress }
var followLoads: Bool { !isAddress }
}
private extension Value {
/// Returns true if this address is passed as indirect out of `apply`.
func isIndirectResult(of apply: ApplySite) -> Bool {
guard let fullApply = apply as? FullApplySite else {
return false
}
if fullApply.numIndirectResultArguments == 0 {
return false
}
var walker = IsIndirectResultWalker(apply: fullApply)
return walker.walkDownUses(ofAddress: self, path: UnusedWalkingPath()) == .abortWalk
}
}
private struct IsIndirectResultWalker: AddressDefUseWalker {
let apply: FullApplySite
mutating func leafUse(address: Operand, path: UnusedWalkingPath) -> WalkResult {
if address.instruction == apply,
let argIdx = apply.calleeArgumentIndex(of: address),
argIdx < apply.numIndirectResultArguments {
return .abortWalk
}
return .continueWalk
}
}
private extension SideEffects.Memory {
var bridged: BridgedMemoryBehavior {
switch (read, write) {
case (false, false): return .None
case (true, false): return .MayRead
case (false, true): return .MayWrite
case (true, true): return .MayReadWrite
}
}
}
|