|
| 1 | +//===--- AliasInfoDumper.swift --------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SIL |
| 14 | + |
| 15 | +/// Prints the memory behavior of relevant instructions in relation to address values of the function. |
| 16 | +let aliasInfoDumper = FunctionPass(name: "dump-alias-info") { |
| 17 | + (function: Function, context: FunctionPassContext) in |
| 18 | + |
| 19 | + let aliasAnalysis = context.aliasAnalysis |
| 20 | + |
| 21 | + print("@\(function.name)") |
| 22 | + |
| 23 | + let values = function.allValues |
| 24 | + |
| 25 | + var pair = 0 |
| 26 | + for (index1, value1) in values.enumerated() { |
| 27 | + for (index2, value2) in values.enumerated() { |
| 28 | + if index2 >= index1 { |
| 29 | + let result = aliasAnalysis.mayAlias(value1, value2) |
| 30 | + precondition(result == aliasAnalysis.mayAlias(value2, value1), "alias analysis not symmetric") |
| 31 | + |
| 32 | + print("PAIR #\(pair).") |
| 33 | + print(" \(value1)") |
| 34 | + print(" \(value2)") |
| 35 | + if result { |
| 36 | + print(" MayAlias") |
| 37 | + } else if !value1.uses.isEmpty && !value2.uses.isEmpty { |
| 38 | + print(" NoAlias") |
| 39 | + } else { |
| 40 | + print(" noalias?") |
| 41 | + } |
| 42 | + |
| 43 | + pair += 1 |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +private extension Function { |
| 50 | + var allValues: [Value] { |
| 51 | + var values: [Value] = [] |
| 52 | + for block in blocks { |
| 53 | + values.append(contentsOf: block.arguments.map { $0 }) |
| 54 | + for inst in block.instructions { |
| 55 | + values.append(contentsOf: inst.results) |
| 56 | + } |
| 57 | + } |
| 58 | + return values |
| 59 | + } |
| 60 | +} |
0 commit comments