File: dead_code_elimination.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 (272 lines) | stat: -rw-r--r-- 8,800 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
// RUN: %target-sil-opt -enable-sil-verify-all -dce %s | %FileCheck %s

sil_stage canonical

import Builtin
import Swift

// CHECK-LABEL: sil @dead1
sil @dead1 : $@convention(thin) (Int32, Int32) -> Int32 {
// CHECK: bb0
bb0(%0 : $Int32, %1 : $Int32):
  %3 = struct_extract %0 : $Int32, #Int32._value
  %4 = struct_extract %1 : $Int32, #Int32._value
  %5 = integer_literal $Builtin.Int1, -1
  %6 = builtin "sadd_with_overflow_Int32"(%3 : $Builtin.Int32, %4 : $Builtin.Int32, %5 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
  %7 = tuple_extract %6 : $(Builtin.Int32, Builtin.Int1), 0
  %8 = struct $Int32 (%7 : $Builtin.Int32)
// CHECK-NEXT: return %0
  return %0 : $Int32
}

// CHECK-LABEL: sil @dead2
sil @dead2 : $@convention(thin) () -> () {
// CHECK: bb0
bb0:
// CHECK-NOT: integer_literal $Builtin.Int32, 2
  %0 = integer_literal $Builtin.Int32, 2
// CHECK-NOT: integer_literal $Builtin.Int32, 0
  %1 = integer_literal $Builtin.Int32, 0
// CHECK-NEXT: br bb1
  br bb1(%0 : $Builtin.Int32, %1 : $Builtin.Int32)

// CHECK: bb1
bb1(%3 : $Builtin.Int32, %4 : $Builtin.Int32):
// CHECK-NEXT: br bb2
  %5 = integer_literal $Builtin.Int32, 100
  %7 = builtin "cmp_slt_Int32"(%4 : $Builtin.Int32, %5 : $Builtin.Int32) : $Builtin.Int1
  cond_br %7, bb2, bb3

bb2:
  %9 = integer_literal $Builtin.Int32, 3
  %11 = integer_literal $Builtin.Int1, -1
  %12 = builtin "sadd_with_overflow_Int32"(%3 : $Builtin.Int32, %9 : $Builtin.Int32, %11 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
  %13 = tuple_extract %12 : $(Builtin.Int32, Builtin.Int1), 0
  %14 = integer_literal $Builtin.Int32, 1
  %15 = builtin "sadd_with_overflow_Int32"(%4 : $Builtin.Int32, %14 : $Builtin.Int32, %11 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
  %16 = tuple_extract %15 : $(Builtin.Int32, Builtin.Int1), 0
  br bb1(%13 : $Builtin.Int32, %16 : $Builtin.Int32)

// CHECK: bb2
bb3:
// CHECK-NEXT: [[RESULT:%[a-zA-Z0-9]+]] = tuple ()
  %18 = tuple ()
// CHECK-NEXT: return [[RESULT]]
  return %18 : $()
}

// CHECK-LABEL: sil @dead3
sil @dead3 : $@convention(thin) () -> () {
bb0:
// CHECK: bb0
  br bb1
// CHECK: bb1
bb1:
// CHECK:      bb1:
// CHECK-NEXT:   br bb1
  %0 = integer_literal $Builtin.Int32, 0
  br bb1
}

// CHECK-LABEL: sil hidden @test_dce_bbargs
sil hidden @test_dce_bbargs : $@convention(thin) () -> () {
bb0:
  %0 = integer_literal $Builtin.Int32, 0
  %1 = struct $Int32 (%0 : $Builtin.Int32)
  %2 = integer_literal $Builtin.Int32, 0
  %3 = struct $Int32 (%2 : $Builtin.Int32)
  %4 = integer_literal $Builtin.Int32, 0
  %5 = struct $Int32 (%4 : $Builtin.Int32)
  %6 = integer_literal $Builtin.Int32, 0
  %7 = struct $Int32 (%6 : $Builtin.Int32)
  %8 = integer_literal $Builtin.Int32, 0
  %9 = struct $Int32 (%8 : $Builtin.Int32)
  %10 = integer_literal $Builtin.Int32, 0
  %11 = struct $Int32 (%10 : $Builtin.Int32)
  br bb1(%1 : $Int32, %3 : $Int32, %11 : $Int32, %7 : $Int32, %9 : $Int32)

bb1(%13 : $Int32, %14 : $Int32, %15 : $Int32, %16 : $Int32, %17 : $Int32):
  %18 = integer_literal $Builtin.Int32, 11
  %20 = struct_extract %15 : $Int32, #Int32._value
  %21 = builtin "cmp_slt_Int32"(%20 : $Builtin.Int32, %18 : $Builtin.Int32) : $Builtin.Int1
  %22 = struct $Bool (%21 : $Builtin.Int1)
  %23 = struct_extract %22 : $Bool, #Bool._value
  cond_br %23, bb2, bb10

// CHECK: bb2:
bb2:
  %25 = integer_literal $Builtin.Int32, 0
  %26 = struct $Int32 (%25 : $Builtin.Int32)
// br bb3(undef : $Int32, undef : $Int32,
  br bb3(%13 : $Int32, %14 : $Int32, %26 : $Int32, %17 : $Int32)

bb3(%28 : $Int32, %29 : $Int32, %30 : $Int32, %31 : $Int32):
  %32 = integer_literal $Builtin.Int32, 22
  %34 = struct_extract %30 : $Int32, #Int32._value
  %35 = builtin "cmp_slt_Int32"(%34 : $Builtin.Int32, %32 : $Builtin.Int32) : $Builtin.Int1
  %36 = struct $Bool (%35 : $Builtin.Int1)
  %37 = struct_extract %36 : $Bool, #Bool._value
  cond_br %37, bb4, bb9

// CHECK: bb4:
bb4:
  %39 = integer_literal $Builtin.Int32, 0
  %41 = struct_extract %28 : $Int32, #Int32._value
  %42 = builtin "cmp_eq_Int32"(%39 : $Builtin.Int32, %41 : $Builtin.Int32) : $Builtin.Int1
  %43 = struct $Bool (%42 : $Builtin.Int1)
  %44 = struct_extract %43 : $Bool, #Bool._value
// CHECK-NOT: cond_br
// CHECK: br bb5
  cond_br %44, bb5, bb6(%28 : $Int32)

bb5:
  %46 = integer_literal $Builtin.Int32, 0
  %47 = struct $Int32 (%46 : $Builtin.Int32)
  br bb6(%47 : $Int32)

bb6(%49 : $Int32):
  %50 = integer_literal $Builtin.Int32, 10
  %52 = struct_extract %29 : $Int32, #Int32._value
  %53 = builtin "cmp_eq_Int32"(%50 : $Builtin.Int32, %52 : $Builtin.Int32) : $Builtin.Int1
  %54 = struct $Bool (%53 : $Builtin.Int1)
  %55 = struct_extract %54 : $Bool, #Bool._value
  cond_br %55, bb7, bb8(%29 : $Int32)

bb7:
  %57 = integer_literal $Builtin.Int32, 0
  %58 = struct $Int32 (%57 : $Builtin.Int32)
  br bb8(%58 : $Int32)

// CHECK: bb5(%{{[0-9]+}} : $Int32):
bb8(%60 : $Int32):
  %61 = struct_extract %31 : $Int32, #Int32._value
  %62 = integer_literal $Builtin.Int32, 1
  %64 = integer_literal $Builtin.Int1, -1
// CHECK-NOT: builtin "sadd_with_overflow_Int32"
// CHECK-NOT: cond_fail
  %65 = builtin "sadd_with_overflow_Int32"(%61 : $Builtin.Int32, %62 : $Builtin.Int32, %64 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
  %66 = tuple_extract %65 : $(Builtin.Int32, Builtin.Int1), 0
  %67 = tuple_extract %65 : $(Builtin.Int32, Builtin.Int1), 1
  cond_fail %67 : $Builtin.Int1
  %69 = struct $Int32 (%66 : $Builtin.Int32)
  %70 = struct_extract %30 : $Int32, #Int32._value
  %71 = integer_literal $Builtin.Int32, 1
  %73 = integer_literal $Builtin.Int1, -1
// CHECK: builtin "ssub_with_overflow_Int32"
  %74 = builtin "ssub_with_overflow_Int32"(%70 : $Builtin.Int32, %71 : $Builtin.Int32, %73 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
  %75 = tuple_extract %74 : $(Builtin.Int32, Builtin.Int1), 0
  %76 = tuple_extract %74 : $(Builtin.Int32, Builtin.Int1), 1
// CHECK: cond_fail
  cond_fail %76 : $Builtin.Int1
  %78 = struct $Int32 (%75 : $Builtin.Int32)
  %tif = function_ref @take_int32 : $@convention(thin) (Int32) -> ()
  %atif = apply %tif(%78) : $@convention(thin) (Int32) -> ()
// CHECK: br bb3(undef : $Int32, undef : $Int32,
  br bb3(%49 : $Int32, %60 : $Int32, %78 : $Int32, %69 : $Int32)

// CHECK: bb6:
bb9:
  %80 = struct_extract %15 : $Int32, #Int32._value
  %81 = integer_literal $Builtin.Int32, 1
  %83 = integer_literal $Builtin.Int1, -1
  %84 = builtin "sadd_with_overflow_Int32"(%80 : $Builtin.Int32, %81 : $Builtin.Int32, %83 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
  %85 = tuple_extract %84 : $(Builtin.Int32, Builtin.Int1), 0
  %86 = tuple_extract %84 : $(Builtin.Int32, Builtin.Int1), 1
  cond_fail %86 : $Builtin.Int1
  %88 = struct $Int32 (%85 : $Builtin.Int32)
// CHECK: br bb1(undef : $Int32, undef : $Int32,
  br bb1(%28 : $Int32, %29 : $Int32, %88 : $Int32, %30 : $Int32, %31 : $Int32)

// CHECK: bb7:
bb10:
  %90 = tuple ()
// CHECK: return
  return %90 : $()
}

sil @take_int32 : $@convention(thin) (Int32) -> ()

// CHECK-LABEL: sil @remove_fix_lifetime
// CHECK: bb0:
// CHECK-NEXT: tuple
// CHECK-NEXT: return
sil @remove_fix_lifetime : $@convention(thin) () -> () {
bb0:
  %0 = integer_literal $Builtin.Int32, 0
  %1 = struct $Int32 (%0 : $Builtin.Int32)

  fix_lifetime %1 : $Int32
  fix_lifetime %1 : $Int32

  %90 = tuple ()
  return %90 : $()
}

// CHECK-LABEL: sil @dont_remove_fix_lifetime
// CHECK: fix_lifetime
// CHECK: fix_lifetime
// CHECK: return
sil @dont_remove_fix_lifetime : $@convention(thin) () -> Int32 {
bb0:
  %0 = integer_literal $Builtin.Int32, 0
  %1 = struct $Int32 (%0 : $Builtin.Int32)

  fix_lifetime %1 : $Int32
  fix_lifetime %1 : $Int32

  return %1 : $Int32
}

struct Container {
	var i: Int32
}

// CHECK-LABEL: sil @dont_remove_addr_fix_lifetime
// CHECK: fix_lifetime
// CHECK: fix_lifetime
// CHECK: return
sil @dont_remove_addr_fix_lifetime : $@convention(thin) (@in Container) -> () {
bb0(%0 : $*Container):
  %1 = struct_element_addr %0 : $*Container, #Container.i

  fix_lifetime %1 : $*Int32
  fix_lifetime %1 : $*Int32

  %90 = tuple ()
  return %90 : $()
}

// Check that DCE does not crash with an infinite loop through a cond_br.
// CHECK-LABEL: sil @deal_with_infinite_loop
// CHECK: cond_br
sil @deal_with_infinite_loop : $@convention(thin) () -> () {
bb0:
  br bb1

bb1:
  cond_br undef, bb2, bb3

bb2:
  br bb1

bb3:
  br bb1
}

// Check that DCE eliminates dead access instructions.
// CHECK-LABEL: sil @dead_access
// CHECK: bb0
// CHECK-NEXT: tuple
// CHECK-NEXT: return
// CHECK-LABEL: end sil function 'dead_access'
sil @dead_access : $@convention(thin) (@in Container) -> () {
bb0(%0 : $*Container):
  %1 = begin_access [modify] [dynamic] %0 : $*Container
  end_access %1 : $*Container
  
  %3 = begin_access [read] [static] %0 : $*Container
  end_access %3 : $*Container

  %999 = tuple ()
  return %999 : $()
}