1 // errorcheckwithauto -0 -l -live -wb=0 -d=ssa/insert_resched_checks/off
2 // +build amd64,goexperiment.regabiargs
4 // Copyright 2014 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // liveness tests with inlining disabled.
16 func printpointer(**int)
19 func printintpointer(*int)
22 func printstringpointer(*string)
25 func printstring(string)
28 func printbytepointer(*byte)
33 var x *int // ERROR "stack object x \*int$"
34 printpointer(&x) // ERROR "live at call to printpointer: x$"
40 printint(0) // nothing live here
43 var x *int // ERROR "stack object x \*int$"
44 printpointer(&x) // ERROR "live at call to printpointer: x$"
48 func f3(b1, b2 bool) {
49 // Here x and y are ambiguously live. In previous go versions they
50 // were marked as live throughout the function to avoid being
51 // poisoned in GODEBUG=gcdead=1 mode; this is now no longer the
61 var x *int // ERROR "stack object x \*int$"
62 printpointer(&x) // ERROR "live at call to printpointer: x$"
65 var y *int // ERROR "stack object y \*int$"
66 printpointer(&y) // ERROR "live at call to printpointer: y$"
69 printint(0) // nothing is live here
72 // The old algorithm treated x as live on all code that
73 // could flow to a return statement, so it included the
74 // function entry and code above the declaration of x
75 // but would not include an indirect use of x in an infinite loop.
76 // Check that these cases are handled correctly.
78 func f4(b1, b2 bool) { // x not live here
80 printint(0) // x not live here
84 x := new(int) // ERROR "stack object x \*int$"
87 printint(**z) // ERROR "live at call to printint: x$"
89 printint(1) // x not live here
93 printint(**z) // ERROR "live at call to printint: x$"
100 x := new(int) // ERROR "stack object x \*int$"
104 y := new(int) // ERROR "stack object y \*int$"
108 printint(**z) // nothing live here
111 // confusion about the _ result used to cause spurious "live at entry to f6: _".
113 func f6() (_, y string) {
118 // confusion about addressed results used to cause "live at entry to f7: x".
120 func f7() (x string) { // ERROR "stack object x string"
126 // ignoring block returns used to cause "live at entry to f8: x, y".
128 func f8() (x, y string) {
132 func g8() (string, string)
134 // ignoring block assignments used to cause "live at entry to f9: x"
142 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$"
143 i9 = y // make y escape so the line above has to call convT2E
147 // liveness formerly confused by UNDEF followed by RET,
148 // leading to "live at entry to f10: ~r1" (unnamed result).
154 // liveness formerly confused by select, thinking runtime.selectgo
155 // can return to next instruction; it always jumps elsewhere.
156 // note that you have to use at least two cases in the select
157 // to get a true select; smaller selects compile to optimized helper functions.
162 // this used to have a spurious "live at entry to f11a: ~r0"
164 select { // ERROR "stack object .autotmp_[0-9]+ \[2\]struct"
175 // At this point p is dead: the code here cannot
176 // get to the bottom of the function.
177 // This used to have a spurious "live at call to printint: p".
178 printint(1) // nothing live here!
179 select { // ERROR "stack object .autotmp_[0-9]+ \[2\]struct"
194 sink = p // prevent stack allocation, otherwise p is rematerializeable
196 // Unlike previous, the cases in this select fall through,
197 // so we can get to the println, so p is not dead.
198 printint(1) // ERROR "live at call to printint: p$"
199 select { // ERROR "live at call to selectgo: p$" "stack object .autotmp_[0-9]+ \[2\]struct"
208 // similarly, select{} does not fall through.
209 // this used to have a spurious "live at entry to f12: ~r0".
219 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
220 // this used to be missing the fact that s is live during the call to g13 (because it is
221 // needed for the call to h13).
225 s = h13(s, g13(s)) // ERROR "live at call to g13: s.ptr$"
228 func g13(string) string
229 func h13(string, string) string
231 // more incorrectly placed VARDEF.
234 x := g14() // ERROR "stack object x string$"
235 printstringpointer(&x)
240 // Checking that various temporaries do not persist or cause
241 // ambiguously live values that must be zeroed.
242 // The exact temporary names are inconsequential but we are
243 // trying to check that there is only one at any given site,
244 // and also that none show up in "ambiguously live" messages.
247 var mi map[interface{}]int
249 // str and iface are used to ensure that a temp is required for runtime calls below.
251 func iface() interface{}
255 delete(mi, iface()) // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
261 var m2s map[string]*byte
262 var m2 map[[2]string]*byte
266 func f17a(p *byte) { // ERROR "live at entry to f17a: p$"
268 m2[x2] = p // ERROR "live at call to mapassign: p$"
270 m2[x2] = p // ERROR "live at call to mapassign: p$"
271 m2[x2] = p // ERROR "live at call to mapassign: p$"
274 func f17b(p *byte) { // ERROR "live at entry to f17b: p$"
277 m2s[str()] = p // ERROR "live at call to mapassign_faststr: p$" "live at call to str: p$"
279 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$"
284 // key and value temporaries
286 m2s[str()] = f17d() // ERROR "live at call to f17d: .autotmp_[0-9]+$" "live at call to mapassign_faststr: .autotmp_[0-9]+$"
288 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]+$"
297 // key temporary for mapaccess.
298 // temporary introduced by orderexpr.
301 z = m2[g18()] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
310 // byteptr is used to ensure that a temp is required for runtime calls below.
314 // dest temporary for channel receive.
318 z = <-ch // ERROR "stack object .autotmp_[0-9]+ \*byte$"
321 z = <-ch // ERROR "live at call to chanrecv1: .autotmp_[0-9]+$"
326 // src temporary for channel send
328 ch <- byteptr() // ERROR "stack object .autotmp_[0-9]+ \*byte$"
335 // key temporary for mapaccess using array literal key.
338 z = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
340 z = m2[[2]string{"x", "y"}]
341 z = m2[[2]string{"x", "y"}]
346 // key temporary for two-result map access using array literal key.
350 z, ok = m2[[2]string{"x", "y"}] // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
352 z, ok = m2[[2]string{"x", "y"}]
353 z, ok = m2[[2]string{"x", "y"}]
359 // key temporary for map access using array literal key.
360 // value temporary too.
362 m2[[2]string{"x", "y"}] = nil // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
364 m2[[2]string{"x", "y"}] = nil
365 m2[[2]string{"x", "y"}] = nil
368 // Non-open-coded defers should not cause autotmps. (Open-coded defers do create extra autotmps).
370 for i := 0; i < 2; i++ {
371 // Put in loop to make sure defer is not open-coded
385 // non-escaping ... slices passed to function call should die on return,
386 // so that the temporaries do not stack and do not cause ambiguously
391 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "stack object .autotmp_[0-9]+ \[3\]interface \{\}$"
393 print26((*int)(nil), (*int)(nil), (*int)(nil))
394 print26((*int)(nil), (*int)(nil), (*int)(nil))
399 func print26(...interface{})
401 // non-escaping closures passed to function call should die on return
406 call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
408 call27(func() { x++ })
409 call27(func() { x++ })
413 // but defer does escape to later execution in the function
415 func f27defer(b bool) {
418 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
420 defer call27(func() { x++ }) // ERROR "stack object .autotmp_[0-9]+ struct \{"
421 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+ .autotmp_[0-9]+"
422 return // ERROR "live at indirect call: .autotmp_[0-9]+"
425 // and newproc (go) escapes to the heap
430 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
432 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
439 // concatstring slice should die on return
441 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
445 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "stack object .autotmp_[0-9]+ \[10\]string$"
447 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
448 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
451 // map iterator should die on end of range loop
455 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$"
456 printstring(k) // ERROR "live at call to printstring: .autotmp_[0-9]+$"
459 for k := range m { // ERROR "live at call to mapiterinit: .autotmp_[0-9]+$" "live at call to mapiternext: .autotmp_[0-9]+$"
460 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]+$"
467 // copy of array of pointers should die at end of range loop
468 var pstructarr [10]pstruct
470 // Struct size chosen to make pointer to element in pstructarr
471 // not computable by strength reduction.
472 type pstruct struct {
478 // live temp during printintpointer(p):
479 // the internal iterator pointer if a pointer to pstruct in pstructarr
480 // can not be easily computed by strength reduction.
482 for _, p := range pstructarr { // ERROR "stack object .autotmp_[0-9]+ \[10\]pstruct$"
483 printintpointer(p.intp) // ERROR "live at call to printintpointer: .autotmp_[0-9]+$"
486 for _, p := range pstructarr {
487 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]+$"
494 // conversion to interface should not leave temporary behind
496 func f31(b1, b2, b3 bool) {
498 g31(g18()) // ERROR "stack object .autotmp_[0-9]+ \[2\]string$"
501 h31(g18()) // ERROR "live at call to convT2E: .autotmp_[0-9]+$" "live at call to newobject: .autotmp_[0-9]+$"
509 func g31(interface{})
510 func h31(...interface{})
512 // non-escaping partial functions passed to function call should die on return
516 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
524 call32(t32.Inc) // ERROR "stack object .autotmp_[0-9]+ struct \{"
533 // temporaries introduced during if conditions and && || expressions
534 // should die once the condition has been acted upon.
536 var m33 map[interface{}]int
539 if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
549 if m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}$"
557 if m33[byteptr()] == 0 && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
558 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
566 if m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
567 m33[byteptr()] == 0 { // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
575 if (m33[byteptr()] == 0 || // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
576 m33[byteptr()] == 0) && // ERROR "stack object .autotmp_[0-9]+ interface \{\}"
577 m33[byteptr()] == 0 {
584 // select temps should disappear in the case bodies
588 func fc38() chan string
589 func fi38(int) *string
593 // we don't care what temps are printed on the lines with output.
594 // we care that the println lines have no live variables
595 // and therefore no output.
597 select { // ERROR "live at call to selectgo:( .autotmp_[0-9]+)+$" "stack object .autotmp_[0-9]+ \[4\]struct \{"
600 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$"
602 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$"
604 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]+)+$"
612 // issue 8097: mishandling of x = x during return.
614 func f39() (x []int) {
616 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
620 func f39a() (x []int) {
622 printnl() // ERROR "live at call to printnl: .autotmp_[0-9]+$"
626 func f39b() (x [10]*int) {
628 x[0] = new(int) // ERROR "live at call to newobject: x$"
629 printnl() // ERROR "live at call to printnl: x$"
633 func f39c() (x [10]*int) {
635 x[0] = new(int) // ERROR "live at call to newobject: x$"
636 printnl() // ERROR "live at call to printnl: x$"
640 // issue 8142: lost 'addrtaken' bit on inlined variables.
641 // no inlining in this test, so just checking that non-inlined works.
652 ret.m = make(map[int]int, 42) // ERROR "live at call to makemap: &ret$"
663 ret := T40{} // ERROR "stack object ret T40$"
664 ret.m = make(map[int]int) // ERROR "live at call to fastrand: .autotmp_[0-9]+$" "stack object .autotmp_[0-9]+ map.hdr\[int\]int$"
666 printnl() // ERROR "live at call to printnl: ret$"
667 // Note: ret is live at the printnl because the compiler moves &ret
668 // from before the printnl to after.
672 func ddd1(x, y *int) { // ERROR "live at entry to ddd1: x y$"
673 ddd2(x, y) // ERROR "stack object .autotmp_[0-9]+ \[2\]\*int$"
675 // Note: no .?autotmp live at printnl. See issue 16996.
677 func ddd2(a ...*int) { // ERROR "live at entry to ddd2: a$"
681 // issue 16016: autogenerated wrapper should have arguments live
684 func (*T) Foo(ptr *int) {}
686 type R struct{ *T } // ERRORAUTO "live at entry to \(\*R\)\.Foo: \.this ptr" "live at entry to R\.Foo: \.this ptr"
688 // issue 18860: output arguments must be live all the time if there is a defer.
689 // In particular, at printint r must be live.
690 func f41(p, q *int) (r *int) { // ERROR "live at entry to f41: p q$"
695 printint(0) // ERROR "live at call to printint: q .autotmp_[0-9]+ r$"
697 return // ERROR "live at call to f41.func1: .autotmp_[0-9]+ r$"
702 f43([]*int{&p, &q, &r}) // ERROR "stack object .autotmp_[0-9]+ \[3\]\*int$"
703 f43([]*int{&p, &r, &q})
704 f43([]*int{&q, &p, &r})
710 // Assigning to a sub-element that makes up an entire local variable
711 // should clobber that variable.
712 func f44(f func() [2]*int) interface{} { // ERROR "live at entry to f44: f"
718 return ret // ERROR "stack object .autotmp_[0-9]+ T"