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
|
//===--- MemBehaviorDumper.swift ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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 SIL
/// Prints the memory behavior of relevant instructions in relation to address values of the function.
let memBehaviorDumper = FunctionPass(name: "dump-mem-behavior") {
(function: Function, context: FunctionPassContext) in
let aliasAnalysis = context.aliasAnalysis
print("@\(function.name)")
let values = function.allValues
var currentPair = 0
for inst in function.instructions where inst.shouldTest {
for value in values where value.definingInstruction != inst {
if value.type.isAddress || value is AddressToPointerInst {
let read = inst.mayRead(fromAddress: value, aliasAnalysis)
let write = inst.mayWrite(toAddress: value, aliasAnalysis)
print("PAIR #\(currentPair).")
print(" \(inst)")
print(" \(value)")
print(" r=\(read ? 1 : 0),w=\(write ? 1 : 0)")
currentPair += 1
}
}
}
print()
}
private extension Function {
var allValues: [Value] {
var values: [Value] = []
for block in blocks {
values.append(contentsOf: block.arguments.map { $0 })
for inst in block.instructions {
values.append(contentsOf: inst.results)
}
}
return values
}
}
private extension Instruction {
var shouldTest: Bool {
switch self {
case is ApplyInst,
is TryApplyInst,
is EndApplyInst,
is BeginApplyInst,
is AbortApplyInst,
is BeginAccessInst,
is EndAccessInst,
is EndCOWMutationInst,
is CopyValueInst,
is DestroyValueInst,
is EndBorrowInst,
is LoadInst,
is StoreInst,
is CopyAddrInst,
is BuiltinInst,
is DebugValueInst:
return true
default:
return false
}
}
}
|