File: SimplifyFixLifetime.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (55 lines) | stat: -rw-r--r-- 1,854 bytes parent folder | download
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
//===--- SimplifyFixLifetime.swift ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 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

/// Canonicalize a `fix_lifetime` from an address to a `load` + `fix_lifetime`:
/// ```
///    %1 = alloc_stack $T
///    ...
///    fix_lifetime %1
/// ```
/// ->
/// ```
///    %1 = alloc_stack $T
///    ...
///    %2 = load %1
///    fix_lifetime %2
/// ```
///
/// This transformation is done for `alloc_stack` and `store_borrow` (which always has an `alloc_stack`
/// operand).
/// The benefit of this transformation is that it enables other optimizations, like mem2reg.
///
extension FixLifetimeInst : Simplifiable, SILCombineSimplifiable {
  func simplify(_ context: SimplifyContext) {
    let opValue = operand.value
    guard opValue is AllocStackInst || opValue is StoreBorrowInst,
          opValue.type.isLoadable(in: parentFunction)
    else {
      return
    }

    let builder = Builder(before: self, context)
    let loadedValue: Value
    if !parentFunction.hasOwnership {
      loadedValue = builder.createLoad(fromAddress: opValue, ownership: .unqualified)
    } else if opValue.type.isTrivial(in: parentFunction) {
      loadedValue = builder.createLoad(fromAddress: opValue, ownership: .trivial)
    } else {
      loadedValue = builder.createLoadBorrow(fromAddress: opValue)
      Builder(after: self, context).createEndBorrow(of: loadedValue)
    }
    operand.set(to: loadedValue, context)
  }
}