1 // errorcheckwithauto -0 -l -live -wb=0 -d=ssa/insert_resched_checks/off
2 //go:build (amd64 && goexperiment.regabiargs) || (arm64 && goexperiment.regabiargs)
3 // +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.
17 func printpointer(**int)
20 func printintpointer(*int)
23 func printstringpointer(*string)
26 func printstring(string)
29 func printbytepointer(*byte)
34 var x *int // ERROR "stack object x \*int$"
35 printpointer(&x) // ERROR "live at call to printpointer: x$"
41 printint(0) // nothing live here
44 var x *int // ERROR "stack object x \*int$"
45 printpointer(&x) // ERROR "live at call to printpointer: x$"
49 func f3(b1, b2 bool) {
50 // Here x and y are ambiguously live. In previous go versions they
51 // were marked as live throughout the function to avoid being
52 // poisoned in GODEBUG=gcdead=1 mode; this is now no longer the
62 var x *int // ERROR "stack object x \*int$"
63 printpointer(&x) // ERROR "live at call to printpointer: x$"
66 var y *int // ERROR "stack object y \*int$"
67 printpointer(&y) // ERROR "live at call to printpointer: y$"
70 printint(0) // nothing is live here
73 // The old algorithm treated x as live on all code that
74 // could flow to a return statement, so it included the
75 // function entry and code above the declaration of x
76 // but would not include an indirect use of x in an infinite loop.
77 // Check that these cases are handled correctly.
79 func f4(b1, b2 bool) { // x not live here
81 printint(0) // x not live here
85 x := new(int) // ERROR "stack object x \*int$"
88 printint(**z) // ERROR "live at call to printint: x$"
90 printint(1) // x not live here
94 printint(**z) // ERROR "live at call to printint: x$"
101 x := new(int) // ERROR "stack object x \*int$"
105 y := new(int) // ERROR "stack object y \*int$"
109 printint(**z) // nothing live here
112 // confusion about the _ result used to cause spurious "live at entry to f6: _".
114 func f6() (_, y string) {
119 // confusion about addressed results used to cause "live at entry to f7: x".
121 func f7() (x string) { // ERROR "stack object x string"
127 // ignoring block returns used to cause "live at entry to f8: x, y".
129 func f8() (x, y string) {
133 func g8() (string, string)
135 // ignoring block assignments used to cause "live at entry to f9: x"
143 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$"
144 i9 = y // make y escape so the line above has to call convT
148 // liveness formerly confused by UNDEF followed by RET,
149 // leading to "live at entry to f10: ~r1" (unnamed result).
155 // liveness formerly confused by select, thinking runtime.selectgo
156 // can return to next instruction; it always jumps elsewhere.
157 // note that you have to use at least two cases in the select
158 // to get a true select; smaller selects compile to optimized helper functions.
163 // this used to have a spurious "live at entry to f11a: ~r0"
165 select { // ERROR "stack object .autotmp_[0-9]+ \[2\]struct"
176 // At this point p is dead: the code here cannot
177 // get to the bottom of the function.
178 // This used to have a spurious "live at call to printint: p".
179 printint(1) // nothing live here!
180 select { // ERROR "stack object .autotmp_[0-9]+ \[2\]struct"
195 sink = p // prevent stack allocation, otherwise p is rematerializeable
197 // Unlike previous, the cases in this select fall through,
198 // so we can get to the println, so p is not dead.
199 printint(1) // ERROR "live at call to printint: p$"
200 select { // ERROR "live at call to selectgo: p$" "stack object .autotmp_[0-9]+ \[2\]struct"
209 // similarly, select{} does not fall through.
210 // this used to have a spurious "live at entry to f12: ~r0".
220 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
221 // this used to be missing the fact that s is live during the call to g13 (because it is
222 // needed for the call to h13).
226 s = h13(s, g13(s)) // ERROR "live at call to g13: s.ptr$"
229 func g13(string) string
230 func h13(string, string) string
232 // more incorrectly placed VARDEF.
235 x := g14() // ERROR "stack object x string$"
236 printstringpointer(&x)
241 // Checking that various temporaries do not persist or cause
242 // ambiguously live values that must be zeroed.
243 // The exact temporary names are inconsequential but we are
244 // trying to check that there is only one at any given site,
245 // and also that none show up in "ambiguously live" messages.
248 var mi map[interface{}]int
250 // str and iface are used to ensure that a temp is required for runtime calls below.
252 func iface() interface{}
256 delete(mi, iface()) // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
262 var m2s map[string]*byte
263 var m2 map[[2]string]*byte
267 func f17a(p *byte) { // ERROR "live at entry to f17a: p$"
269 m2[x2] = p // ERROR "live at call to mapassign: p$"
271 m2[x2] = p // ERROR "live at call to mapassign: p$"
272 m2[x2] = p // ERROR "live at call to mapassign: p$"
275 func f17b(p *byte) { // ERROR "live at entry to f17b: p$"
278 m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
280 m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
281 m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
285 // key and value temporaries
287 m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
289 m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
290 m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
298 // key temporary for mapaccess.
299 // temporary introduced by orderexpr.
302 z = m2[g18()] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
311 // byteptr is used to ensure that a temp is required for runtime calls below.
315 // dest temporary for channel receive.
319 z = <-ch // ERROR "stack object .autotmp_[0-9]+ \*byte$"
322 z = <-ch // ERROR "live at call to chanrecv1: .autotmp_[0-9]+$"
327 // src temporary for channel send
329 ch <- byteptr() // ERROR "stack object .autotmp_[0-9]+ \*byte$"
336 // key temporary for mapaccess using array literal key.
339 z = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
341 z = m2[[2]string{"x", "y"}]
342 z = m2[[2]string{"x", "y"}]
347 // key temporary for two-result map access using array literal key.
351 z, ok = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
353 z, ok = m2[[2]string{"x", "y"}]
354 z, ok = m2[[2]string{"x", "y"}]
360 // key temporary for map access using array literal key.
361 // value temporary too.
363 m2[[2]string{"x", "y"}] = nil // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
365 m2[[2]string{"x", "y"}] = nil
366 m2[[2]string{"x", "y"}] = nil
369 // Non-open-coded defers should not cause autotmps. (Open-coded defers do create extra autotmps).
371 for i := 0; i < 2; i++ {
372 // Put in loop to make sure defer is not open-coded
386 // non-escaping ... slices passed to function call should die on return,
387 // so that the temporaries do not stack and do not cause ambiguously
392 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "stack object .autotmp_[0-9]+ \[3\]interface \{\}$"
394 print26((*int)(nil), (*int)(nil), (*int)(nil))
395 print26((*int)(nil), (*int)(nil), (*int)(nil))
400 func print26(...interface{})
402 // non-escaping closures passed to function call should die on return
407 call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
409 call27(func() { x++ })
410 call27(func() { x++ })
414 // but defer does escape to later execution in the function
416 func f27defer(b bool) {
419 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
421 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
422 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+ .autotmp_[0-9]+"
423 return // ERROR "live at indirect call: .autotmp_[0-9]+"
426 // and newproc (go) escapes to the heap
431 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
433 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
440 // concatstring slice should die on return
442 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
446 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "stack object .autotmp_[0-9]+ \[10\]string$"
448 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
449 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
452 // map iterator should die on end of range loop
456 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$"
457 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
460 for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
461 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
463 for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
464 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
468 // copy of array of pointers should die at end of range loop
469 var pstructarr [10]pstruct
471 // Struct size chosen to make pointer to element in pstructarr
472 // not computable by strength reduction.
473 type pstruct struct {
479 // live temp during printintpointer(p):
480 // the internal iterator pointer if a pointer to pstruct in pstructarr
481 // can not be easily computed by strength reduction.
483 for _, p := range pstructarr { // ERROR "stack object .autotmp_[0-9]+ \[10\]pstruct$"
484 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
487 for _, p := range pstructarr {
488 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
490 for _, p := range pstructarr {
491 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
495 // conversion to interface should not leave temporary behind
497 func f31(b1, b2, b3 bool) {
499 g31(g18()) // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
502 h31(g18()) // ERROR "live at call to convT: .autotmp_[0-9]+$" "live at call to newobject: .autotmp_[0-9]+$"
510 func g31(interface{})
511 func h31(...interface{})
513 // non-escaping partial functions passed to function call should die on return
517 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
525 call32(t32.Inc) // ERROR "stack object .autotmp_[0-9]+ struct \{"
534 // temporaries introduced during if conditions and && || expressions
535 // should die once the condition has been acted upon.
537 var m33 map[interface{}]int
540 if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
550 if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
558 if m33[byteptr()] == 0 && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
559 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
567 if m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
568 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
576 if (m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
577 m33[byteptr()] == 0) && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
578 m33[byteptr()] == 0 {
585 // select temps should disappear in the case bodies
589 func fc38() chan string
590 func fi38(int) *string
594 // we don't care what temps are printed on the lines with output.
595 // we care that the println lines have no live variables
596 // and therefore no output.
598 select { // ERROR "live at call to selectgo:( .autotmp_[0-9]+)+$" "stack object .autotmp_[0-9]+ \[4\]struct \{"
601 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 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 case *fi38(3), *fb38() = <-fc38(): // ERROR "stack object .autotmp_[0-9]+ string$" "live at call to f[ibc]38:( .autotmp_[0-9]+)+$"
613 // issue 8097: mishandling of x = x during return.
615 func f39() (x []int) {
617 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
621 func f39a() (x []int) {
623 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
627 func f39b() (x [10]*int) {
629 x[0] = new(int) // ERROR "live at call to newobject: x$"
630 printnl() // ERROR "live at call to printnl: x$"
634 func f39c() (x [10]*int) {
636 x[0] = new(int) // ERROR "live at call to newobject: x$"
637 printnl() // ERROR "live at call to printnl: x$"
641 // issue 8142: lost 'addrtaken' bit on inlined variables.
642 // no inlining in this test, so just checking that non-inlined works.
653 ret.m = make(map[int]int, 42) // ERROR "live at call to makemap: &ret$"
664 ret := T40{} // ERROR "stack object ret T40$"
665 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 printnl() // ERROR "live at call to printnl: ret$"
668 // Note: ret is live at the printnl because the compiler moves &ret
669 // from before the printnl to after.
673 func ddd1(x, y *int) { // ERROR "live at entry to ddd1: x y$"
674 ddd2(x, y) // ERROR "stack object .autotmp_[0-9]+ \[2\]\*int$"
676 // Note: no .?autotmp live at printnl. See issue 16996.
678 func ddd2(a ...*int) { // ERROR "live at entry to ddd2: a$"
682 // issue 16016: autogenerated wrapper should have arguments live
685 func (*T) Foo(ptr *int) {}
689 // issue 18860: output arguments must be live all the time if there is a defer.
690 // In particular, at printint r must be live.
691 func f41(p, q *int) (r *int) { // ERROR "live at entry to f41: p q$"
696 printint(0) // ERROR "live at call to printint: q .autotmp_[0-9]+ r$"
698 return // ERROR "live at call to f41.func1: .autotmp_[0-9]+ r$"
703 f43([]*int{&p, &q, &r}) // ERROR "stack object .autotmp_[0-9]+ \[3\]\*int$"
704 f43([]*int{&p, &r, &q})
705 f43([]*int{&q, &p, &r})
711 // Assigning to a sub-element that makes up an entire local variable
712 // should clobber that variable.
713 func f44(f func() [2]*int) interface{} { // ERROR "live at entry to f44: f"
719 return ret // ERROR "stack object .autotmp_[0-9]+ T"