forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcast.sil
179 lines (153 loc) · 7.52 KB
/
bitcast.sil
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
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -primary-file %s -emit-ir| %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-cpu %s
// REQUIRES: CPU=i386_or_x86_64
sil_stage canonical
import Swift
class A {}
class B: A {}
class C {}
sil_vtable A {}
sil_vtable B {}
sil_vtable C {}
protocol CP: class {}
// CHECK-i386-LABEL: define{{( protected)?}} swiftcc i32 @bitcast_trivial(%T7bitcast1CC*) {{.*}} {
// CHECK-i386: [[BUF:%.*]] = alloca %T7bitcast1CC*, align 4
// CHECK-i386: store %T7bitcast1CC* %0, %T7bitcast1CC** [[BUF]]
// CHECK-i386: [[OUT_BUF:%.*]] = bitcast %T7bitcast1CC** [[BUF]] to %TSi*
// CHECK-i386: [[VALUE_BUF:%.*]] = getelementptr inbounds %TSi, %TSi* [[OUT_BUF]], i32 0, i32 0
// CHECK-i386: [[VALUE:%.*]] = load i32, i32* [[VALUE_BUF]], align 4
// CHECK-i386: ret i32 [[VALUE]]
// CHECK-i386: }
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc i64 @bitcast_trivial(%T7bitcast1CC*) {{.*}} {
// CHECK-x86_64: [[BUF:%.*]] = alloca %T7bitcast1CC*, align 8
// CHECK-x86_64: store %T7bitcast1CC* %0, %T7bitcast1CC** [[BUF]]
// CHECK-x86_64: [[OUT_BUF:%.*]] = bitcast %T7bitcast1CC** [[BUF]] to %TSi*
// CHECK-x86_64: [[VALUE_BUF:%.*]] = getelementptr inbounds %TSi, %TSi* [[OUT_BUF]], i32 0, i32 0
// CHECK-x86_64: [[VALUE:%.*]] = load i64, i64* [[VALUE_BUF]], align 8
// CHECK-x86_64: ret i64 [[VALUE]]
// CHECK-x86_64: }
sil @bitcast_trivial: $@convention(thin) (C) -> Int {
entry(%c : $C):
%i = unchecked_trivial_bit_cast %c : $C to $Int
return %i : $Int
}
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc { i64, i8 } @bitcast_trivial_optional(i64, i8) {{.*}} {
// CHECK-x86_64-NEXT: entry:
// CHECK-x86_64-NEXT: %2 = trunc i8 %1 to i1
// CHECK-x86_64-NEXT: %3 = zext i1 %2 to i8
// CHECK-x86_64-NEXT: %4 = insertvalue { i64, i8 } undef, i64 %0, 0
// CHECK-x86_64-NEXT: %5 = insertvalue { i64, i8 } %4, i8 %3, 1
// CHECK-x86_64-NEXT: ret { i64, i8 } %5
sil @bitcast_trivial_optional: $@convention(thin) (Optional<Int>) -> Optional<Int> {
entry(%c : $Optional<Int>):
%i = unchecked_trivial_bit_cast %c : $Optional<Int> to $Optional<Int>
return %i : $Optional<Int>
}
// CHECK-i386-LABEL: define{{( protected)?}} swiftcc i32 @bitcast_ref(%T7bitcast1CC*) {{.*}} {
// CHECK-i386-NEXT: entry:
// CHECK-i386-NEXT: [[VALUE:%.*]] = ptrtoint %T7bitcast1CC* %0 to i32
// CHECK-i386-NEXT: ret i32 [[VALUE]]
// CHECK-i386-NEXT: }
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc i64 @bitcast_ref(%T7bitcast1CC*) {{.*}} {
// CHECK-x86_64-NEXT: entry:
// CHECK-x86_64-NEXT: [[VALUE:%.*]] = ptrtoint %T7bitcast1CC* %0 to i64
// CHECK-x86_64-NEXT: ret i64 [[VALUE]]
// CHECK-x86_64-NEXT: }
sil @bitcast_ref: $@convention(thin) (C) -> Optional<C> {
entry(%c : $C):
%o = unchecked_ref_cast %c : $C to $Optional<C>
return %o : $Optional<C>
}
// CHECK-i386-LABEL: define hidden swiftcc i32 @bitcast_ref_optional(i32) {{.*}} {
// CHECK-i386-NEXT: entry:
// CHECK-i386-NEXT: ret i32 %0
// CHECK-i386-NEXT: }
// CHECK-x86_64-LABEL: define hidden swiftcc i64 @bitcast_ref_optional(i64) {{.*}} {
// CHECK-x86_64-NEXT: entry:
// CHECK-x86_64-NEXT: ret i64 %0
// CHECK-x86_64-NEXT: }
sil hidden @bitcast_ref_optional : $@convention(thin) (@owned Optional<C>) -> @owned Optional<C> {
bb0(%0 : $Optional<C>):
%o = unchecked_ref_cast %0 : $Optional<C> to $Optional<C>
return %o : $Optional<C>
}
// CHECK-x86_64-LABEL: define hidden swiftcc i64 @unchecked_bitwise_cast(i64, i64) {{.*}} {
// CHECK-x86_64-NEXT: entry:
// CHECK-x86_64-NEXT: alloca <{ %TSi, %TSi }>, align 8
// A bunch of GEPs happen here to get from Int to int.
// CHECK-x86_64: store i64 %{{.*}}, i64* %bitcast.elt._value, align 8
// CHECK-x86_64: store i64 %{{.*}}, i64* %bitcast.elt1._value, align 8
// CHECK-x86_64-NEXT: %{{.*}} = bitcast <{ %TSi, %TSi }>* %bitcast to %TSi*
// CHECK-x86_64-NEXT: [[VAL:%.*]] = getelementptr inbounds %TSi, %TSi* %{{.*}}, i32 0, i32 0
// CHECK-x86_64-NEXT: [[RESULT:%.*]] = load i64, i64* [[VAL]], align 8
// CHECK-x86_64: ret i64 [[RESULT]]
// CHECK-x86_64-NEXT: }
sil hidden @unchecked_bitwise_cast : $@convention(thin) (Int, Int) -> Int {
bb0(%0 : $Int, %1 : $Int):
%2 = tuple (%0 : $Int, %1 : $Int)
%3 = unchecked_bitwise_cast %2 : $(Int, Int) to $Int
return %3 : $Int
}
// CHECK-x86_64-LABEL: define hidden swiftcc i64 @trivial_bitwise_cast(i64, i64) {{.*}} {
// CHECK-x86_64-NOT: trap
// CHECK-x86_64-NOT: unreachable
// CHECK-x86_64: bitcast <{ %TSi, %TSi }>* %bitcast to %TSi*
// CHECK-x86_64: ret
sil hidden [noinline] @trivial_bitwise_cast : $@convention(thin) (Int, Int) -> Int {
bb0(%0 : $Int, %1 : $Int):
%t1 = tuple (%0 : $Int, %1 : $Int)
%i3 = unchecked_trivial_bit_cast %t1 : $(Int, Int) to $Int
return %i3 : $Int
}
// CHECK-LABEL: define{{( protected)?}} swiftcc void @unchecked_ref_cast_addr
// CHECK-i386: call i1 @swift_rt_swift_dynamicCast(%swift.opaque* %0, %swift.opaque* %{{.*}}, %swift.type* %T, %swift.type* %U, i32 7)
// CHECK-x86_64: call i1 @swift_rt_swift_dynamicCast(%swift.opaque* %0, %swift.opaque* %{{.*}}, %swift.type* %T, %swift.type* %U, i64 7)
sil @unchecked_ref_cast_addr : $@convention(thin) <T, U> (@in T) -> @out U {
bb0(%0 : $*U, %1 : $*T):
%a = alloc_stack $T
copy_addr %1 to [initialization] %a : $*T
unchecked_ref_cast_addr T in %a : $*T to U in %0 : $*U
dealloc_stack %a : $*T
destroy_addr %1 : $*T
%r = tuple ()
return %r : $()
}
// CHECK-i386-LABEL: define{{( protected)?}} swiftcc i32 @unchecked_ref_cast_class_optional
// CHECK-i386: ptrtoint %T7bitcast1AC* %0 to i32
// CHECK-i386-NEXT: ret i32
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc i64 @unchecked_ref_cast_class_optional
// CHECK-x86_64: ptrtoint %T7bitcast1AC* %0 to i64
// CHECK-x86_64-NEXT: ret i64
sil @unchecked_ref_cast_class_optional : $@convention(thin) (@owned A) -> @owned Optional<AnyObject> {
bb0(%0 : $A):
%2 = unchecked_ref_cast %0 : $A to $Optional<AnyObject>
return %2 : $Optional<AnyObject>
}
// CHECK-i386-LABEL: define{{( protected)?}} swiftcc i32 @unchecked_ref_cast_optional_optional
// CHECK-i386: ret i32 %0
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc i64 @unchecked_ref_cast_optional_optional
// CHECK-x86_64: ret i64 %0
sil @unchecked_ref_cast_optional_optional : $@convention(thin) (@owned Optional<A>) -> @owned Optional<AnyObject> {
bb0(%0 : $Optional<A>):
%2 = unchecked_ref_cast %0 : $Optional<A> to $Optional<AnyObject>
return %2 : $Optional<AnyObject>
}
// CHECK-i386-LABEL: define{{( protected)?}} swiftcc i32 @unchecked_ref_cast_proto_optional
// CHECK-i386: ptrtoint {{%objc_object|%swift.refcounted}}* %0 to i32
// CHECK-i386-NEXT: ret i32
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc i64 @unchecked_ref_cast_proto_optional
// CHECK-x86_64: ptrtoint {{%objc_object|%swift.refcounted}}* %0 to i64
// CHECK-x86_64-NEXT: ret i64
sil @unchecked_ref_cast_proto_optional : $@convention(thin) (@owned CP) -> @owned Optional<AnyObject> {
bb0(%0 : $CP):
%2 = unchecked_ref_cast %0 : $CP to $Optional<AnyObject>
return %2 : $Optional<AnyObject>
}
// CHECK-i386-LABEL: define{{( protected)?}} swiftcc i32 @unchecked_ref_cast_optionalproto_optional
// CHECK-i386: ret i32 %0
// CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc i64 @unchecked_ref_cast_optionalproto_optional
// CHECK-x86_64: ret i64 %0
sil @unchecked_ref_cast_optionalproto_optional : $@convention(thin) (@owned Optional<CP>) -> @owned Optional<AnyObject> {
bb0(%0 : $Optional<CP>):
%2 = unchecked_ref_cast %0 : $Optional<CP> to $Optional<AnyObject>
return %2 : $Optional<AnyObject>
}