File: mem2reg_ossa_nontrivial_casts.sil

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (81 lines) | stat: -rw-r--r-- 3,212 bytes parent folder | download | duplicates (2)
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
// RUN: %target-sil-opt -enable-sil-verify-all %s -mem2reg | %FileCheck %s

import Builtin
import Swift

// We cannot use unchecked_value_cast for conversions to trivial type.
// Since it is forwarding, the ownership of the src forwards, but we cannot destroy the dst because it is trivial

// CHECK-LABEL: sil [ossa] @casttotrivial :
// CHECK: [[CAST:%.*]]  = unchecked_bitwise_cast %0 : $AnyObject to $UInt8
// CHECK-NEXT: destroy_value %0 : $AnyObject
// CHECK-NEXT: return [[CAST]] : $UInt8
// CHECK-LABEL: } // end sil function 'casttotrivial'
sil [ossa] @casttotrivial : $@convention(thin) (@owned AnyObject) -> @owned UInt8 {
bb0(%0 : @owned $AnyObject):
  %1 = alloc_stack $AnyObject
  store %0 to [init] %1 : $*AnyObject
  %2 = unchecked_addr_cast %1 : $*AnyObject to $*UInt8
  %3 = load [trivial] %2 : $*UInt8
  %4 = load [take] %1 : $*AnyObject
  destroy_value %4 : $AnyObject
  dealloc_stack %1 : $*AnyObject
  return %3 : $UInt8
}

// We cannot use unchecked_value_cast, because it is forwarding, it forwards the src, and the src can no longer be used as a RunningVal
// To get rid of this issue we need to use a copy_value of the src, and make sure we don't generate copy_value in case of a load [copy].
// To avoid all this spl handling, just use bitwise cast

// CHECK-LABEL: sil [ossa] @casttonontrivial :
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast %0 : $AnyObject to $String
// CHECK: [[COPY:%.*]] = copy_value [[CAST]] : $String
// CHECK-NEXT: destroy_value %0 : $AnyObject
// CHECK-NEXT: return [[COPY]] : $String
// CHECK-LABEL: } // end sil function 'casttonontrivial'
sil [ossa] @casttonontrivial : $@convention(thin) (@owned AnyObject) -> @owned String {
bb0(%0 : @owned $AnyObject):
  %1 = alloc_stack $AnyObject
  store %0 to [init] %1 : $*AnyObject
  %2 = unchecked_addr_cast %1 : $*AnyObject to $*String
  %3 = load [copy] %2 : $*String
  %4 = load [take] %1 : $*AnyObject
  destroy_value %4 : $AnyObject
  dealloc_stack %1 : $*AnyObject
  return %3 : $String
}

struct Pair { var lhs: AnyObject; var rhs: AnyObject }

// CHECK-LABEL: sil [ossa] @shorteningcast :
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast %0 : $Pair to $AnyObject
// CHECK: [[COPY:%.*]] = copy_value [[CAST]] : $AnyObject
// CHECK-NEXT: destroy_value %0 : $Pair
// CHECK-NEXT: return [[COPY]] : $AnyObject
// CHECK-LABEL: } // end sil function 'shorteningcast'
sil [ossa] @shorteningcast : $@convention(thin) (@owned Pair) -> @owned AnyObject {
bb0(%0 : @owned $Pair):
  %1 = alloc_stack $Pair
  store %0 to [init] %1 : $*Pair
  %2 = unchecked_addr_cast %1 : $*Pair to $*AnyObject
  %3 = load [copy] %2 : $*AnyObject
  %4 = load [take] %1 : $*Pair
  destroy_value %4 : $Pair
  dealloc_stack %1 : $*Pair
  return %3 : $AnyObject
}

// CHECK-LABEL: sil [ossa] @deadcast :
// CHECK-LABEL: bb0
// CHECK-NEXT: destroy_value %0 : $AnyObject
// CHECK-LABEL: } // end sil function 'deadcast'
sil [ossa] @deadcast : $@convention(thin) (@owned AnyObject) -> () {
bb0(%0 : @owned $AnyObject):
  %1 = alloc_stack $AnyObject
  store %0 to [init] %1 : $*AnyObject
  %2 = unchecked_addr_cast %1 : $*AnyObject to $*String
  destroy_addr %1 : $*AnyObject
  dealloc_stack %1 : $*AnyObject
  %4 = tuple()
  return %4 : $()
}