File: predictable_memopt_dependence.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 (564 lines) | stat: -rw-r--r-- 25,471 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// RUN: %target-sil-opt -enable-sil-verify-all %s -update-borrowed-from -predictable-memaccess-opts -predictable-deadalloc-elim  | %FileCheck %s

sil_stage raw

import Swift
import Builtin

class SomeClass {}

struct SomeClassPair {
  var x: SomeClass
  var y: SomeClass
}

///////////////////////////////
// mark_dependence semantics //
///////////////////////////////

// Test mark_dependence base: box promotion
//
// Eliminate the load [copy]
// Update the mark_dependence base to the available value.
//
// FIXME: This creates an extra copy in order to promote a load to a copy_value.
// Fix predictable memopts so it never introduces new copies.
// A copy_value should only be introduced if the corresponding store can be
// eliminated.
//
// TODO: box promotion is unsupported:
// - Eliminate the allocation and store
// - Update the mark_dependence base to the available value
//
// CHECK-LABEL: sil [ossa] @markdep_base_promote_box : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> @owned SomeClass {
// CHECK: [[ST:%.*]] = copy_value %0 : $SomeClass
// CHECK: store %0 to [init]
// CHECK: [[LD:%.*]] = copy_value [[ST]] : $SomeClass
// CHECK: return [[LD]]
// CHECK-LABEL: } // end sil function 'markdep_base_promote_box'
sil [ossa] @markdep_base_promote_box : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> @owned SomeClass {
bb0(%0 : @owned $SomeClass, %1 : $UnsafeMutablePointer<Int>):
  %2 = alloc_box $<τ_0_0> { var τ_0_0 } <SomeClass>
  %2a = project_box %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>, 0
  store %0 to [init] %2a : $*SomeClass
  %md = mark_dependence %1 : $UnsafeMutablePointer<Int> on %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>
  %ld = load [copy] %2a : $*SomeClass
  %rawp = struct_extract %md : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %addr = pointer_to_address %rawp : $Builtin.RawPointer to [strict] $*Int
  %acc = begin_access [read] [unsafe] %addr : $*Int
  %ldp = load [trivial] %acc : $*Int
  end_access %acc : $*Int
  destroy_value %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>
  return %ld : $SomeClass
}

// Test mark_dependence base: stack promotion
//
// Eliminate the allocation, store, and load.
// Update the mark_dependence base to the available value.
//
// CHECK-LABEL: sil [ossa] @markdep_base_promote_stack : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> @owned SomeClass {
// CHECK: [[ST:%.*]] = copy_value %0 : $SomeClass
// CHECK: [[MD:%.*]] = mark_dependence %1 : $UnsafeMutablePointer<Int> on %0 : $SomeClass
// CHECK: [[LD:%.*]] = copy_value [[ST]] : $SomeClass
// CHECK: destroy_value [[ST]] : $SomeClass
// CHECK: struct_extract [[MD]] : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
// CHECK: return [[LD]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_base_promote_stack'
sil [ossa] @markdep_base_promote_stack : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> @owned SomeClass {
bb0(%0 : @owned $SomeClass, %1 : $UnsafeMutablePointer<Int>):
  %2 = alloc_stack $SomeClass
  store %0 to [init] %2 : $*SomeClass
  %md = mark_dependence %1 : $UnsafeMutablePointer<Int> on %2 : $*SomeClass
  %ld = load [copy] %2 : $*SomeClass
  %rawp = struct_extract %md : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %addr = pointer_to_address %rawp : $Builtin.RawPointer to [strict] $*Int
  %acc = begin_access [read] [unsafe] %addr : $*Int
  %ldp = load [trivial] %acc : $*Int
  end_access %acc : $*Int
  destroy_addr %2 : $*SomeClass
  dealloc_stack %2 : $*SomeClass
  return %ld : $SomeClass
}

// Test mark_dependence base: stack promotion on trivial allocation
//
// CHECK-LABEL: sil [ossa] @markdep_trivial_base_promote_stack : $@convention(thin) (Int, UnsafeMutablePointer<Int>) -> Int {
// CHECK: bb0(%0 : $Int, %1 : $UnsafeMutablePointer<Int>):
// CHECK-NOT: alloc_stack
// CHECK:   mark_dependence %1 : $UnsafeMutablePointer<Int> on %0 : $Int
// CHECK:   return %0 : $Int
// CHECK: } // end sil function 'markdep_trivial_base_promote_stack'
sil [ossa] @markdep_trivial_base_promote_stack : $@convention(thin) (Int, UnsafeMutablePointer<Int>) -> Int {
bb0(%0 : $Int, %1 : $UnsafeMutablePointer<Int>):
  %2 = alloc_stack $Int
  store %0 to [trivial] %2 : $*Int
  %md = mark_dependence %1 : $UnsafeMutablePointer<Int> on %2 : $*Int
  %ld = load [trivial] %2 : $*Int
  %rawp = struct_extract %md : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %addr = pointer_to_address %rawp : $Builtin.RawPointer to [strict] $*Int
  %acc = begin_access [read] [unsafe] %addr : $*Int
  %ldp = load [trivial] %acc : $*Int
  end_access %acc : $*Int
  destroy_addr %2 : $*Int
  dealloc_stack %2 : $*Int
  return %ld : $Int
}

// Test mark_dependence source: box promotion
//
// Eliminate the load and promote the mark_dependence.
//
// Update the mark_dependence source to the available value.
//
// TODO: box promotion is unsupported:
// - Eliminate the allocation and store
//
// CHECK-LABEL: sil [ossa] @markdep_source_promote_box : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> @owned SomeClass {
// CHECK: [[CP1:%.*]] = copy_value %0 : $SomeClass
// CHECK: store %0 to [init]
// CHECK: [[MD:%.*]] = mark_dependence [[CP1]] : $SomeClass on %1 : $SomeClass
// CHECK: [[CP2:%.*]] = copy_value [[MD]] : $SomeClass
// CHECK: destroy_value [[MD]] : $SomeClass
// CHECK: destroy_value %{{.*}} : $<τ_0_0> { var τ_0_0 } <SomeClass>
// CHECK: return [[CP2]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_promote_box'
sil [ossa] @markdep_source_promote_box : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> @owned SomeClass {
bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
  %2 = alloc_box $<τ_0_0> { var τ_0_0 } <SomeClass>
  %2a = project_box %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>, 0
  store %0 to [init] %2a : $*SomeClass
  %md = mark_dependence %2a : $*SomeClass on %1 : $SomeClass
  %ld = load [copy] %md : $*SomeClass
  destroy_value %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>
  return %ld : $SomeClass
}

// Test mark_dependence source: stack promotion
//
// Eliminate the allocation, store, and load.
// Update the mark_dependence source to the available value.
//
// CHECK-LABEL: sil [ossa] @markdep_source_promote_stack : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> @owned SomeClass {
// CHECK:   [[CP1:%.*]] = copy_value %0 : $SomeClass
// CHECK:   [[MD:%.*]] = mark_dependence [[CP1]] : $SomeClass on %1 : $SomeClass
// CHECK:   [[CP2:%.*]] = copy_value [[MD]] : $SomeClass
// CHECK:   destroy_value [[MD]] : $SomeClass
// CHECK:   destroy_value %0 : $SomeClass
// CHECK:   return [[CP2]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_promote_stack'
sil [ossa] @markdep_source_promote_stack: $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> @owned SomeClass {
bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
  %2 = alloc_stack $SomeClass
  store %0 to [init] %2 : $*SomeClass
  %md = mark_dependence %2 : $*SomeClass on %1 : $SomeClass
  %ld = load [copy] %md : $*SomeClass
  destroy_addr %2 : $*SomeClass
  dealloc_stack %2 : $*SomeClass
  return %ld : $SomeClass
}

// Test mark_dependence source: stack promotion on trivial allocation.
//
// CHECK-LABEL: sil [ossa] @markdep_trivial_source_promote_stack : $@convention(thin) (Int, @guaranteed SomeClass) -> Int {
// CHECK: bb0(%0 : $Int, %1 : @guaranteed $SomeClass):
// CHECK-NEXT:   [[MD:%.*]] = mark_dependence %0 : $Int on %1 : $SomeClass
// CHECK-NEXT:   return [[MD]] : $Int
// CHECK-LABEL: } // end sil function 'markdep_trivial_source_promote_stack'
sil [ossa] @markdep_trivial_source_promote_stack: $@convention(thin) (Int, @guaranteed SomeClass) -> Int {
bb0(%0 : $Int, %1 : @guaranteed $SomeClass):
  %2 = alloc_stack $Int
  store %0 to [trivial] %2 : $*Int
  %md = mark_dependence %2 : $*Int on %1 : $SomeClass
  %ld = load [trivial] %md : $*Int
  destroy_addr %2 : $*Int
  dealloc_stack %2 : $*Int
  return %ld : $Int
}

// Test mark_dependence base: dead box
//
// Currently, no optimization here.
//
// TODO: box promotion is unsupported:
// - Eliminate the allocation and store.
// - Promote the mark_dependence base.
// - The stored value must not be destroyed until after the end_access
//
// CHECK-LABEL: sil [ossa] @markdep_base_dead_box : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> Int {
// CHECK:   store %0 to [init] %{{.*}} : $*SomeClass
// CHECK:   %{{.*}} = mark_dependence %1 : $UnsafeMutablePointer<Int> on %{{.*}} : $<τ_0_0> { var τ_0_0 } <SomeClass>
// CHECK:   end_access %{{.*}} : $*Int
// CHECK:   destroy_value %{{.*}} : $<τ_0_0> { var τ_0_0 } <SomeClass>
// CHECK-LABEL: } // end sil function 'markdep_base_dead_box'
sil [ossa] @markdep_base_dead_box : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> Int {
bb0(%0 : @owned $SomeClass, %1 : $UnsafeMutablePointer<Int>):
  %2 = alloc_box $<τ_0_0> { var τ_0_0 } <SomeClass>
  %2a = project_box %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>, 0
  store %0 to [init] %2a : $*SomeClass
  %md = mark_dependence %1 : $UnsafeMutablePointer<Int> on %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>
  %rawp = struct_extract %md : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %addr = pointer_to_address %rawp : $Builtin.RawPointer to [strict] $*Int
  %acc = begin_access [read] [unsafe] %addr : $*Int
  %ldp = load [trivial] %acc : $*Int
  end_access %acc : $*Int
  destroy_value %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>
  return %ldp : $Int
}

// Test mark_dependence base: dead stack
//
// Eliminate the allocation and store.
// Update the mark_dependence to the stored value.
// The stored value must not be destroyed until after the end_access
//
// CHECK-LABEL: sil [ossa] @markdep_base_dead_stack : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> Int {
// CHECK-NOT: alloc_stack
// CHECK-NOT: store
// CHECK: mark_dependence %1 : $UnsafeMutablePointer<Int> on %0 : $SomeClass
// CHECK: end_access %{{.*}} : $*Int
// CHECK: destroy_value %0 : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_base_dead_stack'
sil [ossa] @markdep_base_dead_stack : $@convention(thin) (@owned SomeClass, UnsafeMutablePointer<Int>) -> Int {
bb0(%0 : @owned $SomeClass, %1 : $UnsafeMutablePointer<Int>):
  %2 = alloc_stack $SomeClass
  store %0 to [init] %2 : $*SomeClass
  %md = mark_dependence %1 : $UnsafeMutablePointer<Int> on %2 : $*SomeClass
  %rawp = struct_extract %md : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %addr = pointer_to_address %rawp : $Builtin.RawPointer to [strict] $*Int
  %acc = begin_access [read] [unsafe] %addr : $*Int
  %ldp = load [trivial] %acc : $*Int
  end_access %acc : $*Int
  destroy_addr %2 : $*SomeClass
  dealloc_stack %2: $*SomeClass
  return %ldp : $Int
}

// Test mark_dependence source: dead box
//
// Promote the mark_dependence and eliminate the load.
//
// TODO: box promotion is unsupported:
// - Eliminate the allocation and store.
// - The stored value must not be destroyed until after the end_access
//
// CHECK-LABEL: sil [ossa] @markdep_source_dead_box : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> () {
// CHECK: [[CP1:%.*]] = copy_value %0 : $SomeClass
// CHECK: store %0 to [init]
// CHECK: [[MD:%.*]] = mark_dependence [[CP1]] : $SomeClass on %1 : $SomeClass
// CHECK: [[CP2:%.*]] = copy_value [[MD]] : $SomeClass
// CHECK: destroy_value [[MD]] : $SomeClass
// CHECK: destroy_value %{{.*}} : $<τ_0_0> { var τ_0_0 } <SomeClass>
// CHECK: destroy_value [[CP2]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_dead_box'
sil [ossa] @markdep_source_dead_box : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> () {
bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
  %2 = alloc_box $<τ_0_0> { var τ_0_0 } <SomeClass>
  %2a = project_box %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>, 0
  store %0 to [init] %2a : $*SomeClass
  %md = mark_dependence %2a : $*SomeClass on %1 : $SomeClass
  %ld = load [copy] %md : $*SomeClass
  destroy_value %2 : $<τ_0_0> { var τ_0_0 } <SomeClass>
  destroy_value %ld : $SomeClass
  %99 = tuple ()
  return %99 : $()
}

// Test mark_dependence source: dead stack
//
// Eliminate the allocation, store, mark_dependence, and load.
//
// CHECK-LABEL: sil [ossa] @markdep_source_dead_stack : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
// CHECK-NEXT: [[CP1:%.*]] = copy_value %0 : $SomeClass
// CHECK-NEXT: [[MD:%.*]] = mark_dependence [[CP1]] : $SomeClass on %1 : $SomeClass
// CHECK-NEXT: [[CP2:%.*]] = copy_value [[MD]] : $SomeClass
// CHECK-NEXT: destroy_value [[MD]] : $SomeClass
// CHECK-NEXT: destroy_value %0 : $SomeClass
// CHECK-NEXT: destroy_value [[CP2]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_dead_stack'
sil [ossa] @markdep_source_dead_stack: $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> () {
bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
  %2 = alloc_stack $SomeClass
  store %0 to [init] %2 : $*SomeClass
  %md = mark_dependence %2 : $*SomeClass on %1 : $SomeClass
  %ld = load [copy] %md : $*SomeClass
  destroy_addr %2 : $*SomeClass
  dealloc_stack %2 : $*SomeClass
  destroy_value %ld : $SomeClass
  %99 = tuple ()
  return %99 : $()
}

// Test mark_dependence source: fully available before a partial init
//
// Elimimate the alocation and stores.
//
// CHECK-LABEL: sil [ossa] @markdep_source_before_partial : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
// CHECK-NEXT:  struct $SomeClassPair (%0 : $SomeClass, %1 : $SomeClass)
// CHECK-NEXT:  destroy_value
// CHECK-LABEL: } // end sil function 'markdep_source_before_partial'
sil [ossa] @markdep_source_before_partial : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
  %stack = alloc_stack $SomeClassPair
  %md = mark_dependence %stack : $*SomeClassPair on %2 : $SomeClass
  %stack0 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.x
  %stack1 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.y
  store %0 to [init] %stack0 : $*SomeClass
  store %1 to [init] %stack1 : $*SomeClass
  destroy_addr %stack : $*SomeClassPair
  dealloc_stack %stack : $*SomeClassPair
  %9999 = tuple()
  return %9999 : $()
}

// Test mark_dependence source: partially available before init
//
// CHECK-LABEL: sil [ossa] @markdep_source_partial_preinit : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
// CHECK-NEXT: [[PAIR:%.*]] = struct $SomeClassPair (%0 : $SomeClass, %1 : $SomeClass)
// CHECK-NEXT: destroy_value [[PAIR]] : $SomeClassPair
// CHECK-NOT: destroy_addr
// CHECK-LABEL: } // end sil function 'markdep_source_partial_preinit'
sil [ossa] @markdep_source_partial_preinit : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
  %stack = alloc_stack $SomeClassPair
  %stack0 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.x
  %stack1 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.y
  %md0 = mark_dependence %stack0 : $*SomeClass on %2 : $SomeClass
  store %0 to [init] %md0 : $*SomeClass
  store %1 to [init] %stack1 : $*SomeClass
  destroy_addr %stack : $*SomeClassPair
  dealloc_stack %stack : $*SomeClassPair
  %9999 = tuple()
  return %9999 : $()
}

// Test mark_dependence source: partially available after init
//
// TODO: Partially available values are not handled by dependent dataflow.
//
// CHECK-LABEL: sil [ossa] @markdep_source_partial_postinit : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
// CHECK: [[STK:%.*]] = alloc_stack $SomeClassPair
// CHECK: [[STK1:%.*]] = struct_element_addr [[STK]] : $*SomeClassPair, #SomeClassPair.y
// CHECK: store %1 to [init] [[STK1]] : $*SomeClass
// CHECK: [[MD:%.*]] = mark_dependence [[STK1]] : $*SomeClass on %2 : $SomeClass
// CHECK: destroy_addr [[STK]] : $*SomeClassPair
// CHECK-LABEL: } // end sil function 'markdep_source_partial_postinit'
sil [ossa] @markdep_source_partial_postinit : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
  %stack = alloc_stack $SomeClassPair
  %stack0 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.x
  %stack1 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.y
  store %0 to [init] %stack0 : $*SomeClass
  store %1 to [init] %stack1 : $*SomeClass
  %md1 = mark_dependence %stack1 : $*SomeClass on %2 : $SomeClass
  destroy_addr %stack : $*SomeClassPair
  dealloc_stack %stack : $*SomeClassPair
  %9999 = tuple()
  return %9999 : $()
}

// Test mark_dependence source: load from dependent phi value.
//
// TODO: Phis are not handled by load promotion.
//
// CHECK-LABEL: sil [ossa] @markdep_source_load_phi : $@convention(thin) (@guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @guaranteed $SomeClass):
// CHECK: [[ALLOC:%.*]] = alloc_stack $SomeClass
// CHECK: [[MD:%.*]] = mark_dependence [[ALLOC]] : $*SomeClass on %0 : $SomeClass
// CHECK: [[LD:%.*]] = load [copy] [[MD]] : $*SomeClass
// CHECK: destroy_addr %1 : $*SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_load_phi'
sil [ossa] @markdep_source_load_phi : $@convention(thin) (@guaranteed SomeClass) -> () {
bb0(%0 : @guaranteed $SomeClass):
  %1 = alloc_stack $SomeClass
  %copy = copy_value %0 : $SomeClass
  cond_br undef, bb1, bb2

bb1:
  store %copy to [init] %1 : $*SomeClass
  br bb3

bb2:
  store %copy to [init] %1 : $*SomeClass
  br bb3

bb3:
  %md = mark_dependence %1 : $*SomeClass on %0 : $SomeClass
  %ld = load [copy] %md : $*SomeClass
  destroy_value %ld : $SomeClass
  destroy_addr %1 : $*SomeClass
  dealloc_stack %1 : $*SomeClass
  %9999 = tuple()
  return %9999 : $()
}

// Test mark_dependence source: dead dependent phi.
//
// Eliminate the loads, allocation, and stores.
//
// CHECK-LABEL: sil [ossa] @markdep_source_dead_phi : $@convention(thin) (@guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @guaranteed $SomeClass):
// CHECK-NOT: alloc_stack
// CHECK: cond_br undef, bb1, bb2
// CHECK: bb1:
// CHECK-NOT: store
// CHECK-NOT: load
// CHECK: bb2:
// CHECK-NOT: store
// CHECK-NOT: load
// CHECK: bb3:
// CHECK-NEXT: [[MD:%.*]] = mark_dependence %1 : $SomeClass on %0 : $SomeClass
// CHECK-NEXT: destroy_value [[MD]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_dead_phi'
sil [ossa] @markdep_source_dead_phi : $@convention(thin) (@guaranteed SomeClass) -> () {
bb0(%0 : @guaranteed $SomeClass):
  %1 = alloc_stack $SomeClass
  %copy = copy_value %0 : $SomeClass
  cond_br undef, bb1, bb2

bb1:
  store %copy to [init] %1 : $*SomeClass
  %ld1 = load [copy] %1 : $*SomeClass
  destroy_value %ld1 : $SomeClass
  br bb3

bb2:
  store %copy to [init] %1 : $*SomeClass
  %ld2 = load [copy] %1 : $*SomeClass
  destroy_value %ld2 : $SomeClass
  br bb3

bb3:
  %md = mark_dependence %1 : $*SomeClass on %0 : $SomeClass
  destroy_addr %1 : $*SomeClass
  dealloc_stack %1 : $*SomeClass
  %9999 = tuple()
  return %9999 : $()
}

// Test mark_dependence source: conflicting available values.
//
// No optimization.
//
// TODO: the current algorithm can't handle this. With the current
// approach, we would need to run a completely separate dataflow for
// each mark_dependence.
// CHECK-LABEL: sil [ossa] @markdep_source_conflicting_load : $@convention(thin) (@guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @guaranteed $SomeClass):
// CHECK:   %1 = alloc_stack $SomeClass
// CHECK:   store %{{.*}} to [init] %1 : $*SomeClass
// CHECK: bb1:
// CHECK:   %{{.*}} = mark_dependence %1 : $*SomeClass on %0 : $SomeClass
// CHECK: bb2:
// CHECK:   load [take] %1 : $*SomeClass
// CHECK:   store %{{.*}} to [init] %1 : $*SomeClass
// CHECK: bb3:
// CHECK:   load [copy] %1 : $*SomeClass
// CHECK:   destroy_addr %1 : $*SomeClass
// CHECK:   dealloc_stack %1 : $*SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_conflicting_load'
sil [ossa] @markdep_source_conflicting_load : $@convention(thin) (@guaranteed SomeClass) -> () {
bb0(%0 : @guaranteed $SomeClass):
  %1 = alloc_stack $SomeClass
  %copy0 = copy_value %0 : $SomeClass
  store %copy0 to [init] %1 : $*SomeClass
  cond_br undef, bb1, bb2

bb1:
  %md = mark_dependence %1 : $*SomeClass on %0 : $SomeClass
  br bb3

bb2:
  %ld2 = load [take] %1 : $*SomeClass
  destroy_value %ld2 : $SomeClass
  %copy2 = copy_value %0 : $SomeClass
  store %copy2 to [init] %1 : $*SomeClass
  br bb3

bb3:
  %ld3 = load [copy] %1 : $*SomeClass
  destroy_value %ld3 : $SomeClass
  destroy_addr %1 : $*SomeClass
  dealloc_stack %1 : $*SomeClass
  %9999 = tuple()
  return %9999 : $()
}

// Test mark_dependence source with a mark_dependence base use.
//
// Promote both mark_dependence instructions during dead allocatio eliminiation,
// reusing the promoted instruction as the available value.
//
// CHECK-LABEL: sil [ossa] @markdep_source_base_dead_stack : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass, UnsafeMutablePointer<Int>) -> Int {
// CHECK: bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass, %2 : $UnsafeMutablePointer<Int>):
// CHECK-NEXT: [[MD1:%.*]] = mark_dependence %0 : $SomeClass on %1 : $SomeClass
// CHECK-NEXT: [[MD2:%.*]] = mark_dependence %2 : $UnsafeMutablePointer<Int> on [[MD1]] : $SomeClass
// CHECK-NOT:  mark_dependence
// CHECK:      struct_extract [[MD2]] : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
// CHECK:      destroy_value [[MD1]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_base_dead_stack'
sil [ossa] @markdep_source_base_dead_stack : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass, UnsafeMutablePointer<Int>) -> Int {
bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass, %2 : $UnsafeMutablePointer<Int>):
  %stk = alloc_stack $SomeClass
  store %0 to [init] %stk : $*SomeClass
  %md1 = mark_dependence %stk : $*SomeClass on %1 : $SomeClass
  %md2 = mark_dependence %2 : $UnsafeMutablePointer<Int> on %md1 : $*SomeClass
  %rawp = struct_extract %md2 : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
  %addr = pointer_to_address %rawp : $Builtin.RawPointer to [strict] $*Int
  %acc = begin_access [read] [unsafe] %addr : $*Int
  %ldp = load [trivial] %acc : $*Int
  end_access %acc : $*Int
  destroy_addr %md1 : $*SomeClass
  dealloc_stack %stk: $*SomeClass
  return %ldp : $Int
}

// Test mark_dependence source with a load [take] use.
//
// Promote the mark_dependence and eliminate the load.
//
// CHECK-LABEL: sil [ossa] @markdep_source_load_take : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> @owned SomeClass {
// CHECK: bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
// CHECK-NEXT: [[MD:%.*]] = mark_dependence %0 : $SomeClass on %1 : $SomeClass
// CHECK-NEXT: return [[MD]] : $SomeClass
// CHECK-LABEL: } // end sil function 'markdep_source_load_take'
sil [ossa] @markdep_source_load_take : $@convention(thin) (@owned SomeClass, @guaranteed SomeClass) -> @owned SomeClass {
bb0(%0 : @owned $SomeClass, %1 : @guaranteed $SomeClass):
  %stk = alloc_stack $SomeClass
  store %0 to [init] %stk : $*SomeClass
  %md1 = mark_dependence %stk : $*SomeClass on %1 : $SomeClass
  %ld = load [take] %md1 : $*SomeClass
  dealloc_stack %stk: $*SomeClass
  return %ld : $SomeClass
}

// Test a chain of mark_dependence that are promoted during dataflow,
// but need to be deleted after a conflict is detected.
//
// CHECK-LABEL: sil [ossa] @markdep_source_chain_cleanup : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
// CHECK: bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
// CHECK:   [[STK:%.*]] = alloc_stack $SomeClassPair
// CHECK:   [[Y:%.*]] = struct_element_addr [[STK]] : $*SomeClassPair, #SomeClassPair.y
// CHECK:   store %1 to [init] [[Y]] : $*SomeClass
// CHECK-NOT: mark_dependence %{{.*}} : $SomeClass
// CHECK:   [[MD1:%.*]] = mark_dependence [[Y]] : $*SomeClass on %2 : $SomeClass
// CHECK:   mark_dependence [[MD1]] : $*SomeClass on %2 : $SomeClass
// CHECK:   destroy_addr [[STK]] : $*SomeClassPair
// CHECK-LABEL: } // end sil function 'markdep_source_chain_cleanup'
sil [ossa] @markdep_source_chain_cleanup : $@convention(thin) (@owned SomeClass, @owned SomeClass, @guaranteed SomeClass) -> () {
bb0(%0 : @owned $SomeClass, %1 : @owned $SomeClass, %2 : @guaranteed $SomeClass):
  %stack = alloc_stack $SomeClassPair
  %stack0 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.x
  %stack1 = struct_element_addr %stack : $*SomeClassPair, #SomeClassPair.y
  store %0 to [init] %stack0 : $*SomeClass
  store %1 to [init] %stack1 : $*SomeClass
  %md1 = mark_dependence %stack1 : $*SomeClass on %2 : $SomeClass
  %md2 = mark_dependence %md1 : $*SomeClass on %2 : $SomeClass
  destroy_addr %stack : $*SomeClassPair
  dealloc_stack %stack : $*SomeClassPair
  %9999 = tuple()
  return %9999 : $()
}