]> Cypherpunks.ru repositories - gostls13.git/blob - test/live.go
cmd/compile, cmd/link, runtime: make defers low-cost through inline code and extra...
[gostls13.git] / test / live.go
1 // errorcheckwithauto -0 -l -live -wb=0 -d=ssa/insert_resched_checks/off
2 // +build !ppc64,!ppc64le
3 // ppc64 needs a better tighten pass to make f18 pass
4 // rescheduling checks need to be turned off because there are some live variables across the inserted check call
5
6 // Copyright 2014 The Go Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style
8 // license that can be found in the LICENSE file.
9
10 // liveness tests with inlining disabled.
11 // see also live2.go.
12
13 package main
14
15 func printnl()
16
17 //go:noescape
18 func printpointer(**int)
19
20 //go:noescape
21 func printintpointer(*int)
22
23 //go:noescape
24 func printstringpointer(*string)
25
26 //go:noescape
27 func printstring(string)
28
29 //go:noescape
30 func printbytepointer(*byte)
31
32 func printint(int)
33
34 func f1() {
35         var x *int       // ERROR "stack object x \*int$"
36         printpointer(&x) // ERROR "live at call to printpointer: x$"
37         printpointer(&x)
38 }
39
40 func f2(b bool) {
41         if b {
42                 printint(0) // nothing live here
43                 return
44         }
45         var x *int       // ERROR "stack object x \*int$"
46         printpointer(&x) // ERROR "live at call to printpointer: x$"
47         printpointer(&x)
48 }
49
50 func f3(b1, b2 bool) {
51         // Here x and y are ambiguously live. In previous go versions they
52         // were marked as live throughout the function to avoid being
53         // poisoned in GODEBUG=gcdead=1 mode; this is now no longer the
54         // case.
55
56         printint(0)
57         if b1 == false {
58                 printint(0)
59                 return
60         }
61
62         if b2 {
63                 var x *int       // ERROR "stack object x \*int$"
64                 printpointer(&x) // ERROR "live at call to printpointer: x$"
65                 printpointer(&x)
66         } else {
67                 var y *int       // ERROR "stack object y \*int$"
68                 printpointer(&y) // ERROR "live at call to printpointer: y$"
69                 printpointer(&y)
70         }
71         printint(0) // nothing is live here
72 }
73
74 // The old algorithm treated x as live on all code that
75 // could flow to a return statement, so it included the
76 // function entry and code above the declaration of x
77 // but would not include an indirect use of x in an infinite loop.
78 // Check that these cases are handled correctly.
79
80 func f4(b1, b2 bool) { // x not live here
81         if b2 {
82                 printint(0) // x not live here
83                 return
84         }
85         var z **int
86         x := new(int) // ERROR "stack object x \*int$"
87         *x = 42
88         z = &x
89         printint(**z) // ERROR "live at call to printint: x$"
90         if b2 {
91                 printint(1) // x not live here
92                 return
93         }
94         for {
95                 printint(**z) // ERROR "live at call to printint: x$"
96         }
97 }
98
99 func f5(b1 bool) {
100         var z **int
101         if b1 {
102                 x := new(int) // ERROR "stack object x \*int$"
103                 *x = 42
104                 z = &x
105         } else {
106                 y := new(int) // ERROR "stack object y \*int$"
107                 *y = 54
108                 z = &y
109         }
110         printint(**z) // nothing live here
111 }
112
113 // confusion about the _ result used to cause spurious "live at entry to f6: _".
114
115 func f6() (_, y string) {
116         y = "hello"
117         return
118 }
119
120 // confusion about addressed results used to cause "live at entry to f7: x".
121
122 func f7() (x string) { // ERROR "stack object x string"
123         _ = &x
124         x = "hello"
125         return
126 }
127
128 // ignoring block returns used to cause "live at entry to f8: x, y".
129
130 func f8() (x, y string) {
131         return g8()
132 }
133
134 func g8() (string, string)
135
136 // ignoring block assignments used to cause "live at entry to f9: x"
137 // issue 7205
138
139 var i9 interface{}
140
141 func f9() bool {
142         g8()
143         x := i9
144         y := interface{}(g18()) // ERROR "live at call to convT2E: x.data$" "live at call to g18: x.data$" "stack object .autotmp_[0-9]+ \[2\]string$"
145         i9 = y                  // make y escape so the line above has to call convT2E
146         return x != y
147 }
148
149 // liveness formerly confused by UNDEF followed by RET,
150 // leading to "live at entry to f10: ~r1" (unnamed result).
151
152 func f10() string {
153         panic(1)
154 }
155
156 // liveness formerly confused by select, thinking runtime.selectgo
157 // can return to next instruction; it always jumps elsewhere.
158 // note that you have to use at least two cases in the select
159 // to get a true select; smaller selects compile to optimized helper functions.
160
161 var c chan *int
162 var b bool
163
164 // this used to have a spurious "live at entry to f11a: ~r0"
165 func f11a() *int {
166         select { // ERROR "stack object .autotmp_[0-9]+ \[2\]struct"
167         case <-c:
168                 return nil
169         case <-c:
170                 return nil
171         }
172 }
173
174 func f11b() *int {
175         p := new(int)
176         if b {
177                 // At this point p is dead: the code here cannot
178                 // get to the bottom of the function.
179                 // This used to have a spurious "live at call to printint: p".
180                 printint(1) // nothing live here!
181                 select {    // ERROR "stack object .autotmp_[0-9]+ \[2\]struct"
182                 case <-c:
183                         return nil
184                 case <-c:
185                         return nil
186                 }
187         }
188         println(*p)
189         return nil
190 }
191
192 var sink *int
193
194 func f11c() *int {
195         p := new(int)
196         sink = p // prevent stack allocation, otherwise p is rematerializeable
197         if b {
198                 // Unlike previous, the cases in this select fall through,
199                 // so we can get to the println, so p is not dead.
200                 printint(1) // ERROR "live at call to printint: p$"
201                 select {    // ERROR "live at call to selectgo: p$" "stack object .autotmp_[0-9]+ \[2\]struct"
202                 case <-c:
203                 case <-c:
204                 }
205         }
206         println(*p)
207         return nil
208 }
209
210 // similarly, select{} does not fall through.
211 // this used to have a spurious "live at entry to f12: ~r0".
212
213 func f12() *int {
214         if b {
215                 select {}
216         } else {
217                 return nil
218         }
219 }
220
221 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
222 // this used to be missing the fact that s is live during the call to g13 (because it is
223 // needed for the call to h13).
224
225 func f13() {
226         s := g14()
227         s = h13(s, g13(s)) // ERROR "live at call to g13: s.ptr$"
228 }
229
230 func g13(string) string
231 func h13(string, string) string
232
233 // more incorrectly placed VARDEF.
234
235 func f14() {
236         x := g14() // ERROR "stack object x string$"
237         printstringpointer(&x)
238 }
239
240 func g14() string
241
242 // Checking that various temporaries do not persist or cause
243 // ambiguously live values that must be zeroed.
244 // The exact temporary names are inconsequential but we are
245 // trying to check that there is only one at any given site,
246 // and also that none show up in "ambiguously live" messages.
247
248 var m map[string]int
249 var mi map[interface{}]int
250
251 // str and iface are used to ensure that a temp is required for runtime calls below.
252 func str() string
253 func iface() interface{}
254
255 func f16() {
256         if b {
257                 delete(mi, iface()) // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
258         }
259         delete(mi, iface())
260         delete(mi, iface())
261 }
262
263 var m2s map[string]*byte
264 var m2 map[[2]string]*byte
265 var x2 [2]string
266 var bp *byte
267
268 func f17a(p *byte) { // ERROR "live at entry to f17a: p$"
269         if b {
270                 m2[x2] = p // ERROR "live at call to mapassign: p$"
271         }
272         m2[x2] = p // ERROR "live at call to mapassign: p$"
273         m2[x2] = p // ERROR "live at call to mapassign: p$"
274 }
275
276 func f17b(p *byte) { // ERROR "live at entry to f17b: p$"
277         // key temporary
278         if b {
279                 m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
280         }
281         m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
282         m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
283 }
284
285 func f17c() {
286         // key and value temporaries
287         if b {
288                 m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
289         }
290         m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
291         m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
292 }
293
294 func f17d() *byte
295
296 func g18() [2]string
297
298 func f18() {
299         // key temporary for mapaccess.
300         // temporary introduced by orderexpr.
301         var z *byte
302         if b {
303                 z = m2[g18()] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
304         }
305         z = m2[g18()]
306         z = m2[g18()]
307         printbytepointer(z)
308 }
309
310 var ch chan *byte
311
312 // byteptr is used to ensure that a temp is required for runtime calls below.
313 func byteptr() *byte
314
315 func f19() {
316         // dest temporary for channel receive.
317         var z *byte
318
319         if b {
320                 z = <-ch // ERROR "stack object .autotmp_[0-9]+ \*byte$"
321         }
322         z = <-ch
323         z = <-ch // ERROR "live at call to chanrecv1: .autotmp_[0-9]+$"
324         printbytepointer(z)
325 }
326
327 func f20() {
328         // src temporary for channel send
329         if b {
330                 ch <- byteptr() // ERROR "stack object .autotmp_[0-9]+ \*byte$"
331         }
332         ch <- byteptr()
333         ch <- byteptr()
334 }
335
336 func f21() {
337         // key temporary for mapaccess using array literal key.
338         var z *byte
339         if b {
340                 z = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
341         }
342         z = m2[[2]string{"x", "y"}]
343         z = m2[[2]string{"x", "y"}]
344         printbytepointer(z)
345 }
346
347 func f23() {
348         // key temporary for two-result map access using array literal key.
349         var z *byte
350         var ok bool
351         if b {
352                 z, ok = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
353         }
354         z, ok = m2[[2]string{"x", "y"}]
355         z, ok = m2[[2]string{"x", "y"}]
356         printbytepointer(z)
357         print(ok)
358 }
359
360 func f24() {
361         // key temporary for map access using array literal key.
362         // value temporary too.
363         if b {
364                 m2[[2]string{"x", "y"}] = nil // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
365         }
366         m2[[2]string{"x", "y"}] = nil
367         m2[[2]string{"x", "y"}] = nil
368 }
369
370 // Non-open-coded defers should not cause autotmps.  (Open-coded defers do create extra autotmps).
371 func f25(b bool) {
372         for i := 0; i < 2; i++ {
373                 // Put in loop to make sure defer is not open-coded
374                 defer g25()
375         }
376         if b {
377                 return
378         }
379         var x string
380         x = g14()
381         printstring(x)
382         return
383 }
384
385 func g25()
386
387 // non-escaping ... slices passed to function call should die on return,
388 // so that the temporaries do not stack and do not cause ambiguously
389 // live variables.
390
391 func f26(b bool) {
392         if b {
393                 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "stack object .autotmp_[0-9]+ \[3\]interface \{\}$"
394         }
395         print26((*int)(nil), (*int)(nil), (*int)(nil))
396         print26((*int)(nil), (*int)(nil), (*int)(nil))
397         printnl()
398 }
399
400 //go:noescape
401 func print26(...interface{})
402
403 // non-escaping closures passed to function call should die on return
404
405 func f27(b bool) {
406         x := 0
407         if b {
408                 call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
409         }
410         call27(func() { x++ })
411         call27(func() { x++ })
412         printnl()
413 }
414
415 // but defer does escape to later execution in the function
416
417 func f27defer(b bool) {
418         x := 0
419         if b {
420                 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
421         }
422         defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
423         printnl()                    // ERROR "live at call to printnl: .autotmp_[0-9]+ .autotmp_[0-9]+"
424         return                       // ERROR "live at call to call27: .autotmp_[0-9]+"
425 }
426
427 // and newproc (go) escapes to the heap
428
429 func f27go(b bool) {
430         x := 0
431         if b {
432                 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$" "live at call to newproc: &x$"
433         }
434         go call27(func() { x++ }) // ERROR "live at call to newobject: &x$"
435         printnl()
436 }
437
438 //go:noescape
439 func call27(func())
440
441 // concatstring slice should die on return
442
443 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
444
445 func f28(b bool) {
446         if b {
447                 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "stack object .autotmp_[0-9]+ \[10\]string$"
448         }
449         printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
450         printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
451 }
452
453 // map iterator should die on end of range loop
454
455 func f29(b bool) {
456         if b {
457                 for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$" "stack object .autotmp_[0-9]+ map.iter\[string\]int$"
458                         printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
459                 }
460         }
461         for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
462                 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
463         }
464         for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
465                 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
466         }
467 }
468
469 // copy of array of pointers should die at end of range loop
470 var pstructarr [10]pstruct
471
472 // Struct size chosen to make pointer to element in pstructarr
473 // not computable by strength reduction.
474 type pstruct struct {
475         intp *int
476         _    [8]byte
477 }
478
479 func f30(b bool) {
480         // live temp during printintpointer(p):
481         // the internal iterator pointer if a pointer to pstruct in pstructarr
482         // can not be easily computed by strength reduction.
483         if b {
484                 for _, p := range pstructarr { // ERROR "stack object .autotmp_[0-9]+ \[10\]pstruct$"
485                         printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
486                 }
487         }
488         for _, p := range pstructarr {
489                 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
490         }
491         for _, p := range pstructarr {
492                 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
493         }
494 }
495
496 // conversion to interface should not leave temporary behind
497
498 func f31(b1, b2, b3 bool) {
499         if b1 {
500                 g31(g18()) // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
501         }
502         if b2 {
503                 h31(g18()) // ERROR "live at call to convT2E: .autotmp_[0-9]+$" "live at call to newobject: .autotmp_[0-9]+$"
504         }
505         if b3 {
506                 panic(g18())
507         }
508         print(b3)
509 }
510
511 func g31(interface{})
512 func h31(...interface{})
513
514 // non-escaping partial functions passed to function call should die on return
515
516 type T32 int
517
518 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
519         *t++
520 }
521
522 var t32 T32
523
524 func f32(b bool) {
525         if b {
526                 call32(t32.Inc) // ERROR "stack object .autotmp_[0-9]+ struct \{"
527         }
528         call32(t32.Inc)
529         call32(t32.Inc)
530 }
531
532 //go:noescape
533 func call32(func())
534
535 // temporaries introduced during if conditions and && || expressions
536 // should die once the condition has been acted upon.
537
538 var m33 map[interface{}]int
539
540 func f33() {
541         if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
542                 printnl()
543                 return
544         } else {
545                 printnl()
546         }
547         printnl()
548 }
549
550 func f34() {
551         if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
552                 printnl()
553                 return
554         }
555         printnl()
556 }
557
558 func f35() {
559         if m33[byteptr()] == 0 && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
560                 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
561                 printnl()
562                 return
563         }
564         printnl()
565 }
566
567 func f36() {
568         if m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
569                 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
570                 printnl()
571                 return
572         }
573         printnl()
574 }
575
576 func f37() {
577         if (m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
578                 m33[byteptr()] == 0) && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
579                 m33[byteptr()] == 0 {
580                 printnl()
581                 return
582         }
583         printnl()
584 }
585
586 // select temps should disappear in the case bodies
587
588 var c38 chan string
589
590 func fc38() chan string
591 func fi38(int) *string
592 func fb38() *bool
593
594 func f38(b bool) {
595         // we don't care what temps are printed on the lines with output.
596         // we care that the println lines have no live variables
597         // and therefore no output.
598         if b {
599                 select { // ERROR "live at call to selectgo:( .autotmp_[0-9]+)+$" "stack object .autotmp_[0-9]+ \[4\]struct \{"
600                 case <-fc38():
601                         printnl()
602                 case fc38() <- *fi38(1): // ERROR "live at call to fc38:( .autotmp_[0-9]+)+$" "live at call to fi38:( .autotmp_[0-9]+)+$" "stack object .autotmp_[0-9]+ string$"
603                         printnl()
604                 case *fi38(2) = <-fc38(): // ERROR "live at call to fc38:( .autotmp_[0-9]+)+$" "live at call to fi38:( .autotmp_[0-9]+)+$" "stack object .autotmp_[0-9]+ string$"
605                         printnl()
606                 case *fi38(3), *fb38() = <-fc38(): // ERROR "stack object .autotmp_[0-9]+ string$" "live at call to fc38:( .autotmp_[0-9]+)+$" "live at call to fi38:( .autotmp_[0-9]+)+$"
607                         printnl()
608                 }
609                 printnl()
610         }
611         printnl()
612 }
613
614 // issue 8097: mishandling of x = x during return.
615
616 func f39() (x []int) {
617         x = []int{1}
618         printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
619         return x
620 }
621
622 func f39a() (x []int) {
623         x = []int{1}
624         printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
625         return
626 }
627
628 func f39b() (x [10]*int) {
629         x = [10]*int{}
630         x[0] = new(int) // ERROR "live at call to newobject: x$"
631         printnl()       // ERROR "live at call to printnl: x$"
632         return x
633 }
634
635 func f39c() (x [10]*int) {
636         x = [10]*int{}
637         x[0] = new(int) // ERROR "live at call to newobject: x$"
638         printnl()       // ERROR "live at call to printnl: x$"
639         return
640 }
641
642 // issue 8142: lost 'addrtaken' bit on inlined variables.
643 // no inlining in this test, so just checking that non-inlined works.
644
645 type T40 struct {
646         m map[int]int
647 }
648
649 //go:noescape
650 func useT40(*T40)
651
652 func newT40() *T40 {
653         ret := T40{}
654         ret.m = make(map[int]int, 42) // ERROR "live at call to makemap: &ret$"
655         return &ret
656 }
657
658 func bad40() {
659         t := newT40()
660         _ = t
661         printnl()
662 }
663
664 func good40() {
665         ret := T40{}              // ERROR "stack object ret T40$"
666         ret.m = make(map[int]int) // ERROR "live at call to fastrand: .autotmp_[0-9]+$" "stack object .autotmp_[0-9]+ map.hdr\[int\]int$"
667         t := &ret
668         printnl() // ERROR "live at call to printnl: ret$"
669         // Note: ret is live at the printnl because the compiler moves &ret
670         // from before the printnl to after.
671         useT40(t)
672 }
673
674 func ddd1(x, y *int) { // ERROR "live at entry to ddd1: x y$"
675         ddd2(x, y) // ERROR "stack object .autotmp_[0-9]+ \[2\]\*int$"
676         printnl()
677         // Note: no .?autotmp live at printnl.  See issue 16996.
678 }
679 func ddd2(a ...*int) { // ERROR "live at entry to ddd2: a$"
680         sink = a[0]
681 }
682
683 // issue 16016: autogenerated wrapper should have arguments live
684 type T struct{}
685
686 func (*T) Foo(ptr *int) {}
687
688 type R struct{ *T } // ERRORAUTO "live at entry to \(\*R\)\.Foo: \.this ptr" "live at entry to R\.Foo: \.this ptr"
689
690 // issue 18860: output arguments must be live all the time if there is a defer.
691 // In particular, at printint r must be live.
692 func f41(p, q *int) (r *int) { // ERROR "live at entry to f41: p q$"
693         r = p
694         defer func() {
695                 recover()
696         }()
697         printint(0) // ERROR "live at call to printint: q r .autotmp_[0-9]+$"
698         r = q
699         return // ERROR "live at call to f41.func1: r .autotmp_[0-9]+$"
700 }
701
702 func f42() {
703         var p, q, r int
704         f43([]*int{&p, &q, &r}) // ERROR "stack object .autotmp_[0-9]+ \[3\]\*int$"
705         f43([]*int{&p, &r, &q})
706         f43([]*int{&q, &p, &r})
707 }
708
709 //go:noescape
710 func f43(a []*int)
711
712 // Assigning to a sub-element that makes up an entire local variable
713 // should clobber that variable.
714 func f44(f func() [2]*int) interface{} { // ERROR "live at entry to f44: f"
715         type T struct {
716                 s [1][2]*int
717         }
718         ret := T{}
719         ret.s[0] = f()
720         return ret // ERROR "stack object .autotmp_5 T"
721 }