File: string_optimization.sil

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (289 lines) | stat: -rw-r--r-- 16,040 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// RUN: %target-sil-opt %s -parse-serialized-sil -string-optimization -sil-combine | %FileCheck %s

sil_stage canonical

import Builtin
import Swift
import SwiftShims

sil [readonly] [_semantics "string.makeUTF8"] @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
sil [readonly] [_semantics "string.init_empty"] @empty_string : $@convention(method) (@thin String.Type) -> @owned String
sil [_semantics "string.init_empty"] @empty_string_with_capacity : $@convention(method) (Int, @thin String.Type) -> @owned String
sil [_semantics "string.append"] @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
sil [_semantics "string.concat"] @string_concat : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
sil [readonly] [_semantics "string.getUTF8CString"] @string_getUTF8CString : $@convention(method) (@guaranteed String) -> @owned ContiguousArray<Int8>

// CHECK-LABEL: sil @append_to_empty_string
// CHECK:       [[S:%[0-9]+]] = alloc_stack $String
// CHECK:       [[F:%[0-9]+]] = function_ref @empty_string
// CHECK:       [[A:%[0-9]+]] = apply [[F]]
// CHECK-NEXT:  store [[A]] to [[S]]
// CHECK-NEXT:  retain_value %0
// CHECK-NEXT:  destroy_addr [[S]]
// CHECK-NEXT:  store %0 to [[S]]
// CHECK-NEXT:  [[L:%[0-9]+]] = load [[S]]
// CHECK:       return [[L]]
// CHECK:     } // end sil function 'append_to_empty_string'
sil @append_to_empty_string : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : $String):
  %2 = alloc_stack $String
  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  store %7 to %2 : $*String
  %14 = function_ref @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
  %15 = apply %14(%0, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()
  %28 = load %2 : $*String
  dealloc_stack %2 : $*String
  return %28 : $String
}

// CHECK-LABEL: sil [ossa] @ossa_append_to_empty_string
// CHECK:       [[S:%[0-9]+]] = alloc_stack $String
// CHECK:       [[F:%[0-9]+]] = function_ref @empty_string
// CHECK:       [[A:%[0-9]+]] = apply [[F]]
// CHECK-NEXT:  store [[A]] to [init] [[S]]
// CHECK-NEXT:  [[COPY:%[0-9]+]] = copy_value %0
// CHECK-NEXT:  store [[COPY]] to [assign] [[S]]
// CHECK-NEXT:  [[L:%[0-9]+]] = load [take] [[S]]
// CHECK:       return [[L]]
// CHECK:     } // end sil function 'ossa_append_to_empty_string'
sil [ossa] @ossa_append_to_empty_string : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : @guaranteed $String):
  %2 = alloc_stack $String
  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  store %7 to [init] %2 : $*String
  %14 = function_ref @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
  %15 = apply %14(%0, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()
  %28 = load [take] %2 : $*String
  dealloc_stack %2 : $*String
  return %28 : $String
}

// CHECK-LABEL: sil @append_empty_string
// CHECK:       [[S:%[0-9]+]] = alloc_stack $String
// CHECK-NEXT:  store %0 to [[S]]
// CHECK-NEXT:  [[L:%[0-9]+]] = load [[S]]
// CHECK:       return [[L]]
// CHECK:     } // end sil function 'append_empty_string'
sil @append_empty_string : $@convention(thin) (@owned String) -> @owned String {
bb0(%0 : $String):
  %2 = alloc_stack $String
  store %0 to %2 : $*String
  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  %14 = function_ref @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
  %15 = apply %14(%7, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()
  release_value %7 : $String
  %28 = load %2 : $*String
  dealloc_stack %2 : $*String
  return %28 : $String
}

// CHECK-LABEL: sil @append_constant_strings
// CHECK:       [[S:%[0-9]+]] = alloc_stack $String
// CHECK:       [[STR:%[0-9]+]] = string_literal utf8 "a🙂"
// CHECK:       [[LENGTH:%[0-9]+]] = integer_literal $Builtin.Word, 5
// CHECK:       [[ASCII:%[0-9]+]] = integer_literal $Builtin.Int1, 0
// CHECK:       [[MT:%[0-9]+]] = metatype $@thin String.Type
// CHECK:       [[F:%[0-9]+]] = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC
// CHECK:       [[A:%[0-9]+]] = apply [[F]]([[STR]], [[LENGTH]], [[ASCII]], [[MT]])
// CHECK-NEXT:  destroy_addr [[S]]
// CHECK-NEXT:  store [[A]] to [[S]]
// CHECK-NEXT:  [[L:%[0-9]+]] = load [[S]]
// CHECK:       return [[L]]
// CHECK:     } // end sil function 'append_constant_strings'
sil @append_constant_strings : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : $String):
  %2 = alloc_stack $String
  %3 = string_literal utf8 "a"
  %4 = integer_literal $Builtin.Word, 1
  %5 = integer_literal $Builtin.Int1, -1
  %6 = metatype $@thin String.Type
  %7 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %8 = apply %7(%3, %4, %5, %6) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %9 = string_literal utf8 "🙂"
  %10 = integer_literal $Builtin.Word, 4
  %11 = apply %7(%9, %10, %5, %6) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  store %8 to %2 : $*String
  %14 = function_ref @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
  %15 = apply %14(%11, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()
  release_value %11 : $String
  %28 = load %2 : $*String
  dealloc_stack %2 : $*String
  return %28 : $String
}

// CHECK-LABEL: sil @dont_concat_non_const_strings
// CHECK:       [[S:%[0-9]+]] = alloc_stack $String
// CHECK-DAG:   [[APPEND:%[0-9]+]] = function_ref @string_append
// CHECK-DAG:   store %0 to [[S]]
// CHECK:       [[A_STR:%[0-9]+]] = apply {{.*}} : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
// CHECK:       apply [[APPEND]]([[A_STR]], [[S]])
// CHECK:       [[L:%[0-9]+]] = load [[S]]
// CHECK:       return [[L]]
// CHECK:     } // end sil function 'dont_concat_non_const_strings'
sil @dont_concat_non_const_strings : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : $String):
  %2 = alloc_stack $String

  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  store %7 to %2 : $*String

  %10 = function_ref @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
  %11 = apply %10(%0, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()

  %12 = string_literal utf8 "a"
  %13 = integer_literal $Builtin.Word, 1
  %14 = integer_literal $Builtin.Int1, -1
  %15 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %16 = apply %15(%12, %13, %14, %5) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String

  %17 = apply %10(%16, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()
  release_value %16 : $String

  %28 = load %2 : $*String
  dealloc_stack %2 : $*String
  return %28 : $String
}

sil @unknown_modify : $@convention(thin) (@inout String) -> ()

// CHECK-LABEL: sil @dont_optimize_modified_string
// CHECK:       [[INIT:%[0-9]+]] = function_ref @empty_string
// CHECK:       apply [[INIT]]
// CHECK:       [[UNKNOWN:%[0-9]+]] = function_ref @unknown_modify
// CHECK:       apply [[UNKNOWN]]
// CHECK:       [[APPEND:%[0-9]+]] = function_ref @string_append
// CHECK:       apply [[APPEND]]
// CHECK:     } // end sil function 'dont_optimize_modified_string'
sil @dont_optimize_modified_string : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : $String):
  %2 = alloc_stack $String
  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  store %7 to %2 : $*String
  %8 = function_ref @unknown_modify : $@convention(thin) (@inout String) -> ()
  %9 = apply %8(%2) : $@convention(thin) (@inout String) -> ()
  %14 = function_ref @string_append : $@convention(method) (@guaranteed String, @inout String) -> ()
  %15 = apply %14(%0, %2) : $@convention(method) (@guaranteed String, @inout String) -> ()
  %28 = load %2 : $*String
  dealloc_stack %2 : $*String
  return %28 : $String
}

// CHECK-LABEL: sil @concat_to_empty_string
// CHECK:     bb0(%0 : $String):
// CHECK-NEXT:  retain_value %0
// CHECK-NEXT:  return %0
// CHECK:     } // end sil function 'concat_to_empty_string'
sil @concat_to_empty_string : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : $String):
  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  %8 = function_ref @string_concat : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
  %9 = apply %8(%7, %0, %5) : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
  return %9 : $String
}

// CHECK-LABEL: sil @concat_empty_string
// CHECK:     bb0(%0 : $String):
// CHECK-NEXT:  retain_value %0
// CHECK-NEXT:  return %0
// CHECK:     } // end sil function 'concat_empty_string'
sil @concat_empty_string : $@convention(thin) (@owned String) -> @owned String {
bb0(%0 : $String):
  %5 = metatype $@thin String.Type
  %6 = function_ref @empty_string : $@convention(method) (@thin String.Type) -> @owned String
  %7 = apply %6(%5) : $@convention(method) (@thin String.Type) -> @owned String
  %8 = function_ref @string_concat : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
  %9 = apply %8(%0, %7, %5) : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
  return %9 : $String
}

// CHECK-LABEL: sil @concat_constant_strings
// CHECK:       [[STR:%[0-9]+]] = string_literal utf8 "a🙂"
// CHECK:       [[LENGTH:%[0-9]+]] = integer_literal $Builtin.Word, 5
// CHECK:       [[ASCII:%[0-9]+]] = integer_literal $Builtin.Int1, 0
// CHECK:       [[MT:%[0-9]+]] = metatype $@thin String.Type
// CHECK:       [[F:%[0-9]+]] = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC
// CHECK:       [[A:%[0-9]+]] = apply [[F]]([[STR]], [[LENGTH]], [[ASCII]], [[MT]])
// CHECK:       return [[A]]
// CHECK:     } // end sil function 'concat_constant_strings'
sil @concat_constant_strings : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : $String):
  %3 = string_literal utf8 "a"
  %4 = integer_literal $Builtin.Word, 1
  %5 = integer_literal $Builtin.Int1, -1
  %6 = metatype $@thin String.Type
  %7 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %8 = apply %7(%3, %4, %5, %6) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %9 = string_literal utf8 "🙂"
  %10 = integer_literal $Builtin.Word, 4
  %11 = apply %7(%9, %10, %5, %6) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %12 = function_ref @string_concat : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
  %13 = apply %12(%8, %11, %6) : $@convention(method) (@guaranteed String, @guaranteed String, @thin String.Type) -> @owned String
  return %13 : $String
}

// CHECK-LABEL: sil @test_cstring
// CHECK:       %0 = string_literal utf8 "a"
// CHECK:       return %0
// CHECK:     } // end sil function 'test_cstring'
sil @test_cstring : $@convention(thin) () -> Builtin.RawPointer {
bb0:
  %3 = string_literal utf8 "a"
  %4 = integer_literal $Builtin.Word, 1
  %5 = integer_literal $Builtin.Int1, -1
  %6 = metatype $@thin String.Type
  %7 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %8 = apply %7(%3, %4, %5, %6) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %d = struct $DefaultStringInterpolation (%8 : $String)
  %e = struct_extract %d : $DefaultStringInterpolation, #DefaultStringInterpolation._storage
  %9 = function_ref @string_getUTF8CString : $@convention(method) (@guaranteed String) -> @owned ContiguousArray<Int8>
  %10 = apply %9(%e) : $@convention(method) (@guaranteed String) -> @owned ContiguousArray<Int8>
  %11 = struct_extract %10 : $ContiguousArray<Int8>, #ContiguousArray._buffer
  %12 = struct_extract %11 : $_ContiguousArrayBuffer<Int8>, #_ContiguousArrayBuffer._storage
  %13 = ref_tail_addr %12 : $__ContiguousArrayStorageBase, $Int8
  %14 = address_to_pointer %13 : $*Int8 to $Builtin.RawPointer
  return %14 : $Builtin.RawPointer
}

// CHECK-LABEL: sil @test_interpolated_cstring
// CHECK:       apply
// CHECK:       [[S:%[0-9]+]] = string_literal utf8 "a"
// CHECK:       return [[S]]
// CHECK:     } // end sil function 'test_interpolated_cstring'
sil @test_interpolated_cstring : $@convention(thin) () -> Builtin.RawPointer {
bb0:
  %3 = string_literal utf8 "a"
  %4 = integer_literal $Builtin.Word, 1
  %5 = integer_literal $Builtin.Int1, -1
  %6 = metatype $@thin String.Type
  %7 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %8 = apply %7(%3, %4, %5, %6) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
  %d = struct $DefaultStringInterpolation (%8 : $String)
  %f = function_ref @$sSS19stringInterpolationSSs013DefaultStringB0V_tcfC : $@convention(method) (@owned DefaultStringInterpolation, @thin String.Type) -> @owned String
  %a = apply %f(%d, %6) : $@convention(method) (@owned DefaultStringInterpolation, @thin String.Type) -> @owned String
  %9 = function_ref @string_getUTF8CString : $@convention(method) (@guaranteed String) -> @owned ContiguousArray<Int8>
  %10 = apply %9(%a) : $@convention(method) (@guaranteed String) -> @owned ContiguousArray<Int8>
  %11 = struct_extract %10 : $ContiguousArray<Int8>, #ContiguousArray._buffer
  %12 = struct_extract %11 : $_ContiguousArrayBuffer<Int8>, #_ContiguousArrayBuffer._storage
  %13 = ref_tail_addr %12 : $__ContiguousArrayStorageBase, $Int8
  %14 = address_to_pointer %13 : $*Int8 to $Builtin.RawPointer
  return %14 : $Builtin.RawPointer
}

sil public_external [readonly] @$sSS19stringInterpolationSSs013DefaultStringB0V_tcfC : $@convention(method) (@owned DefaultStringInterpolation, @thin String.Type) -> @owned String {
bb0(%0 : $DefaultStringInterpolation, %1 : $@thin String.Type):
  %2 = struct_extract %0 : $DefaultStringInterpolation, #DefaultStringInterpolation._storage
  return %2 : $String
}