1 // errorcheckwithauto -0 -l -live -wb=0 -d=ssa/insert_resched_checks/off
3 //go:build (amd64 && goexperiment.regabiargs) || (arm64 && goexperiment.regabiargs)
5 // Copyright 2014 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
9 // liveness tests with inlining disabled.
19 func printpointer(**int)
22 func printintpointer(*int)
25 func printstringpointer(*string)
28 func printstring(string)
31 func printbytepointer(*byte)
36 var x *int // ERROR "stack object x \*int$"
37 printpointer(&x) // ERROR "live at call to printpointer: x$"
43 printint(0) // nothing live here
46 var x *int // ERROR "stack object x \*int$"
47 printpointer(&x) // ERROR "live at call to printpointer: x$"
51 func f3(b1, b2 bool) {
52 // Here x and y are ambiguously live. In previous go versions they
53 // were marked as live throughout the function to avoid being
54 // poisoned in GODEBUG=gcdead=1 mode; this is now no longer the
64 var x *int // ERROR "stack object x \*int$"
65 printpointer(&x) // ERROR "live at call to printpointer: x$"
68 var y *int // ERROR "stack object y \*int$"
69 printpointer(&y) // ERROR "live at call to printpointer: y$"
72 printint(0) // nothing is live here
75 // The old algorithm treated x as live on all code that
76 // could flow to a return statement, so it included the
77 // function entry and code above the declaration of x
78 // but would not include an indirect use of x in an infinite loop.
79 // Check that these cases are handled correctly.
81 func f4(b1, b2 bool) { // x not live here
83 printint(0) // x not live here
87 x := new(int) // ERROR "stack object x \*int$"
90 printint(**z) // ERROR "live at call to printint: x$"
92 printint(1) // x not live here
96 printint(**z) // ERROR "live at call to printint: x$"
103 x := new(int) // ERROR "stack object x \*int$"
107 y := new(int) // ERROR "stack object y \*int$"
111 printint(**z) // nothing live here
114 // confusion about the _ result used to cause spurious "live at entry to f6: _".
116 func f6() (_, y string) {
121 // confusion about addressed results used to cause "live at entry to f7: x".
123 func f7() (x string) { // ERROR "stack object x string"
129 // ignoring block returns used to cause "live at entry to f8: x, y".
131 func f8() (x, y string) {
135 func g8() (string, string)
137 // ignoring block assignments used to cause "live at entry to f9: x"
145 y := interface{}(g18()) // ERROR "live at call to convT: x.data$" "live at call to g18: x.data$" "stack object .autotmp_[0-9]+ \[2\]string$"
146 i9 = y // make y escape so the line above has to call convT
150 // liveness formerly confused by UNDEF followed by RET,
151 // leading to "live at entry to f10: ~r1" (unnamed result).
157 // liveness formerly confused by select, thinking runtime.selectgo
158 // can return to next instruction; it always jumps elsewhere.
159 // note that you have to use at least two cases in the select
160 // to get a true select; smaller selects compile to optimized helper functions.
165 // this used to have a spurious "live at entry to f11a: ~r0"
167 select { // ERROR "stack object .autotmp_[0-9]+ \[2\]runtime.scase$"
178 // At this point p is dead: the code here cannot
179 // get to the bottom of the function.
180 // This used to have a spurious "live at call to printint: p".
181 printint(1) // nothing live here!
182 select { // ERROR "stack object .autotmp_[0-9]+ \[2\]runtime.scase$"
197 sink = p // prevent stack allocation, otherwise p is rematerializeable
199 // Unlike previous, the cases in this select fall through,
200 // so we can get to the println, so p is not dead.
201 printint(1) // ERROR "live at call to printint: p$"
202 select { // ERROR "live at call to selectgo: p$" "stack object .autotmp_[0-9]+ \[2\]runtime.scase$"
211 // similarly, select{} does not fall through.
212 // this used to have a spurious "live at entry to f12: ~r0".
222 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
223 // this used to be missing the fact that s is live during the call to g13 (because it is
224 // needed for the call to h13).
228 s = h13(s, g13(s)) // ERROR "live at call to g13: s.ptr$"
231 func g13(string) string
232 func h13(string, string) string
234 // more incorrectly placed VARDEF.
237 x := g14() // ERROR "stack object x string$"
238 printstringpointer(&x)
243 // Checking that various temporaries do not persist or cause
244 // ambiguously live values that must be zeroed.
245 // The exact temporary names are inconsequential but we are
246 // trying to check that there is only one at any given site,
247 // and also that none show up in "ambiguously live" messages.
250 var mi map[interface{}]int
252 // str and iface are used to ensure that a temp is required for runtime calls below.
254 func iface() interface{}
258 delete(mi, iface()) // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
264 var m2s map[string]*byte
265 var m2 map[[2]string]*byte
269 func f17a(p *byte) { // ERROR "live at entry to f17a: p$"
271 m2[x2] = p // ERROR "live at call to mapassign: p$"
273 m2[x2] = p // ERROR "live at call to mapassign: p$"
274 m2[x2] = p // ERROR "live at call to mapassign: p$"
277 func f17b(p *byte) { // ERROR "live at entry to f17b: p$"
280 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 m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
287 // key and value temporaries
289 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 m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
300 // key temporary for mapaccess.
301 // temporary introduced by orderexpr.
304 z = m2[g18()] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
313 // byteptr is used to ensure that a temp is required for runtime calls below.
317 // dest temporary for channel receive.
321 z = <-ch // ERROR "stack object .autotmp_[0-9]+ \*byte$"
324 z = <-ch // ERROR "live at call to chanrecv1: .autotmp_[0-9]+$"
329 // src temporary for channel send
331 ch <- byteptr() // ERROR "stack object .autotmp_[0-9]+ \*byte$"
338 // key temporary for mapaccess using array literal key.
341 z = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
343 z = m2[[2]string{"x", "y"}]
344 z = m2[[2]string{"x", "y"}]
349 // key temporary for two-result map access using array literal key.
353 z, ok = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
355 z, ok = m2[[2]string{"x", "y"}]
356 z, ok = m2[[2]string{"x", "y"}]
362 // key temporary for map access using array literal key.
363 // value temporary too.
365 m2[[2]string{"x", "y"}] = nil // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
367 m2[[2]string{"x", "y"}] = nil
368 m2[[2]string{"x", "y"}] = nil
371 // Non-open-coded defers should not cause autotmps. (Open-coded defers do create extra autotmps).
373 for i := 0; i < 2; i++ {
374 // Put in loop to make sure defer is not open-coded
388 // non-escaping ... slices passed to function call should die on return,
389 // so that the temporaries do not stack and do not cause ambiguously
394 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "stack object .autotmp_[0-9]+ \[3\]interface \{\}$"
396 print26((*int)(nil), (*int)(nil), (*int)(nil))
397 print26((*int)(nil), (*int)(nil), (*int)(nil))
402 func print26(...interface{})
404 // non-escaping closures passed to function call should die on return
409 call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
411 call27(func() { x++ })
412 call27(func() { x++ })
416 // but defer does escape to later execution in the function
418 func f27defer(b bool) {
421 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
423 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
424 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+ .autotmp_[0-9]+"
425 return // ERROR "live at indirect call: .autotmp_[0-9]+"
428 // and newproc (go) escapes to the heap
433 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$" "live at call to newobject: &x .autotmp_[0-9]+$" "live at call to newproc: &x$" // allocate two closures, the func literal, and the wrapper for go
435 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$" "live at call to newobject: .autotmp_[0-9]+$" // allocate two closures, the func literal, and the wrapper for go
442 // concatstring slice should die on return
444 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
448 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "stack object .autotmp_[0-9]+ \[10\]string$"
450 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
451 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
454 // map iterator should die on end of range loop
458 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]+ runtime.hiter$"
459 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
462 for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
463 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
465 for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
466 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
470 // copy of array of pointers should die at end of range loop
471 var pstructarr [10]pstruct
473 // Struct size chosen to make pointer to element in pstructarr
474 // not computable by strength reduction.
475 type pstruct struct {
481 // live temp during printintpointer(p):
482 // the internal iterator pointer if a pointer to pstruct in pstructarr
483 // can not be easily computed by strength reduction.
485 for _, p := range pstructarr { // ERROR "stack object .autotmp_[0-9]+ \[10\]pstruct$"
486 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
489 for _, p := range pstructarr {
490 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
492 for _, p := range pstructarr {
493 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
497 // conversion to interface should not leave temporary behind
499 func f31(b1, b2, b3 bool) {
501 g31(g18()) // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
504 h31(g18()) // ERROR "live at call to convT: .autotmp_[0-9]+$" "live at call to newobject: .autotmp_[0-9]+$"
512 func g31(interface{})
513 func h31(...interface{})
515 // non-escaping partial functions passed to function call should die on return
519 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
527 call32(t32.Inc) // ERROR "stack object .autotmp_[0-9]+ struct \{"
536 // temporaries introduced during if conditions and && || expressions
537 // should die once the condition has been acted upon.
539 var m33 map[interface{}]int
542 if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
552 if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
560 if m33[byteptr()] == 0 && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
561 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
569 if m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
570 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
578 if (m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
579 m33[byteptr()] == 0) && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
580 m33[byteptr()] == 0 {
587 // select temps should disappear in the case bodies
591 func fc38() chan string
592 func fi38(int) *string
596 // we don't care what temps are printed on the lines with output.
597 // we care that the println lines have no live variables
598 // and therefore no output.
600 select { // ERROR "live at call to selectgo:( .autotmp_[0-9]+)+$" "stack object .autotmp_[0-9]+ \[4\]runtime.scase$"
603 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$"
605 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$"
607 case *fi38(3), *fb38() = <-fc38(): // ERROR "stack object .autotmp_[0-9]+ string$" "live at call to f[ibc]38:( .autotmp_[0-9]+)+$"
615 // issue 8097: mishandling of x = x during return.
617 func f39() (x []int) {
619 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
623 func f39a() (x []int) {
625 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
629 func f39b() (x [10]*int) {
631 x[0] = new(int) // ERROR "live at call to newobject: x$"
632 printnl() // ERROR "live at call to printnl: x$"
636 func f39c() (x [10]*int) {
638 x[0] = new(int) // ERROR "live at call to newobject: x$"
639 printnl() // ERROR "live at call to printnl: x$"
643 // issue 8142: lost 'addrtaken' bit on inlined variables.
644 // no inlining in this test, so just checking that non-inlined works.
655 ret.m = make(map[int]int, 42) // ERROR "live at call to makemap: &ret$"
666 ret := T40{} // ERROR "stack object ret T40$"
667 ret.m = make(map[int]int) // ERROR "live at call to fastrand: .autotmp_[0-9]+$" "stack object .autotmp_[0-9]+ runtime.hmap$"
669 printnl() // ERROR "live at call to printnl: ret$"
670 // Note: ret is live at the printnl because the compiler moves &ret
671 // from before the printnl to after.
675 func ddd1(x, y *int) { // ERROR "live at entry to ddd1: x y$"
676 ddd2(x, y) // ERROR "stack object .autotmp_[0-9]+ \[2\]\*int$"
678 // Note: no .?autotmp live at printnl. See issue 16996.
680 func ddd2(a ...*int) { // ERROR "live at entry to ddd2: a$"
684 // issue 16016: autogenerated wrapper should have arguments live
687 func (*T) Foo(ptr *int) {}
691 // issue 18860: output arguments must be live all the time if there is a defer.
692 // In particular, at printint r must be live.
693 func f41(p, q *int) (r *int) { // ERROR "live at entry to f41: p q$"
698 printint(0) // ERROR "live at call to printint: .autotmp_[0-9]+ q r$"
700 return // ERROR "live at call to f41.func1: .autotmp_[0-9]+ r$"
705 f43([]*int{&p, &q, &r}) // ERROR "stack object .autotmp_[0-9]+ \[3\]\*int$"
706 f43([]*int{&p, &r, &q})
707 f43([]*int{&q, &p, &r})
713 // Assigning to a sub-element that makes up an entire local variable
714 // should clobber that variable.
715 func f44(f func() [2]*int) interface{} { // ERROR "live at entry to f44: f"
719 ret := T{} // ERROR "stack object ret T"
724 func f45(a, b, c, d, e, f, g, h, i, j, k, l *byte) { // ERROR "live at entry to f45: a b c d e f g h i j k l"
725 f46(a, b, c, d, e, f, g, h, i, j, k, l) // ERROR "live at call to f46: a b c d e f g h i j k l"
741 func f46(a, b, c, d, e, f, g, h, i, j, k, l *byte) {