Source file test/codegen/append_freegc.go

     1  // asmcheck
     2  
     3  //go:build goexperiment.runtimefreegc
     4  
     5  // Copyright 2025 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.
     8  
     9  package codegen
    10  
    11  func Append1(n int) []int {
    12  	var r []int
    13  	for i := range n {
    14  		// amd64:`.*growsliceNoAlias\b`
    15  		r = append(r, i)
    16  	}
    17  	// amd64:`.*moveSliceNoCapNoScan\b`
    18  	return r
    19  }
    20  
    21  func Append2(n int) (r []int) {
    22  	for i := range n {
    23  		// amd64:`.*growslice\b`
    24  		r = append(r, i)
    25  	}
    26  	// amd64:`.*moveSliceNoCapNoScan\b`
    27  	return
    28  }
    29  
    30  func Append3(n int) (r []int) {
    31  	for i := range n {
    32  		// amd64:`.*growslice\b`
    33  		r = append(r, i)
    34  	}
    35  	// amd64:`.*moveSliceNoCapNoScan\b`
    36  	return r
    37  }
    38  
    39  func Append4(n int) []int {
    40  	var r []int
    41  	for i := range n {
    42  		// amd64:`.*growsliceBufNoAlias\b`
    43  		r = append(r, i)
    44  	}
    45  	println(cap(r))
    46  	// amd64:`.*moveSliceNoScan\b`
    47  	return r
    48  }
    49  
    50  func Append5(n int) []int {
    51  	var r []int
    52  	for i := range n {
    53  		// amd64:`.*growsliceBufNoAlias\b`
    54  		r = append(r, i)
    55  	}
    56  	useSlice(r)
    57  	// amd64:`.*moveSliceNoScan\b`
    58  	return r
    59  }
    60  
    61  func Append5b(n int) []int {
    62  	var r []int
    63  	useSlice(r)
    64  	for i := range n {
    65  		// amd64:`.*growsliceBuf\b` -`.*growsliceBufNoAlias\b`
    66  		r = append(r, i)
    67  	}
    68  	// amd64:`.*moveSliceNoScan\b`
    69  	return r
    70  }
    71  
    72  func Append6(n int) []*int {
    73  	var r []*int
    74  	for i := range n {
    75  		// TODO(thepudds): for now, the compiler only uses the NoAlias version
    76  		// for element types without pointers.
    77  		// amd64:`.*growslice\b`
    78  		r = append(r, new(i))
    79  	}
    80  	// amd64:`.*moveSliceNoCap\b`
    81  	return r
    82  }
    83  
    84  func Append7(n int) []*int {
    85  	var r []*int
    86  	for i := range n {
    87  		// TODO(thepudds): for now, the compiler only uses the NoAlias version
    88  		// for element types without pointers.
    89  		// amd64:`.*growsliceBuf\b`
    90  		r = append(r, new(i))
    91  	}
    92  	println(cap(r))
    93  	// amd64:`.*moveSlice\b`
    94  	return r
    95  }
    96  
    97  func Append8(n int, p *[]int) {
    98  	var r []int
    99  	for i := range n {
   100  		// amd64:`.*growsliceNoAlias\b`
   101  		r = append(r, i)
   102  	}
   103  	// amd64:`.*moveSliceNoCapNoScan\b`
   104  	*p = r
   105  }
   106  
   107  func Append8b(n int, p *[]int) {
   108  	var r []int
   109  	// amd64:`.*moveSliceNoCapNoScan\b`
   110  	*p = r
   111  	for i := range n {
   112  		// amd64:`.*growslice\b` -`.*growsliceNoAlias\b`
   113  		r = append(r, i)
   114  	}
   115  }
   116  
   117  func Append9(n int) []int {
   118  	var r []int
   119  	for i := range n {
   120  		// amd64:`.*growsliceNoAlias\b`
   121  		r = append(r, i)
   122  	}
   123  	println(len(r))
   124  	// amd64:`.*moveSliceNoCapNoScan\b`
   125  	return r
   126  }
   127  
   128  func Append10(n int) []int {
   129  	var r []int
   130  	for i := range n {
   131  		// amd64:`.*growsliceNoAlias\b`
   132  		r = append(r, i)
   133  	}
   134  	println(r[3])
   135  	// amd64:`.*moveSliceNoCapNoScan\b`
   136  	return r
   137  }
   138  
   139  func Append11(n int) []int {
   140  	var r []int
   141  	for i := range n {
   142  		// amd64:`.*growsliceBufNoAlias\b`
   143  		r = append(r, i)
   144  	}
   145  	r = r[3:5]
   146  	// amd64:`.*moveSliceNoScan\b`
   147  	return r
   148  }
   149  
   150  func Append12(n int) []int {
   151  	var r []int
   152  	r = nil
   153  	for i := range n {
   154  		// amd64:`.*growslice\b`
   155  		r = append(r, i)
   156  	}
   157  	// amd64:`.*moveSliceNoCapNoScan\b`
   158  	return r
   159  }
   160  
   161  func Append13(n int) []int {
   162  	var r []int
   163  	r, r = nil, nil
   164  	for i := range n {
   165  		// amd64:`.*growslice\b`
   166  		r = append(r, i)
   167  	}
   168  	// amd64:`.*moveSliceNoCapNoScan\b`
   169  	return r
   170  }
   171  
   172  func Append14(n int) []int {
   173  	var r []int
   174  	r = []int{3, 4, 5}
   175  	for i := range n {
   176  		// amd64:`.*growsliceBufNoAlias\b`
   177  		r = append(r, i)
   178  	}
   179  	// amd64:`.*moveSliceNoScan\b`
   180  	return r
   181  }
   182  
   183  func Append15(n int) []int {
   184  	r := []int{3, 4, 5}
   185  	for i := range n {
   186  		// amd64:`.*growsliceBufNoAlias\b`
   187  		r = append(r, i)
   188  	}
   189  	// amd64:`.*moveSliceNoScan\b`
   190  	return r
   191  }
   192  
   193  func Append16(r []int, n int) []int {
   194  	for i := range n {
   195  		// amd64:`.*growslice\b`
   196  		r = append(r, i)
   197  	}
   198  	// amd64:`.*moveSliceNoCapNoScan\b`
   199  	return r
   200  }
   201  
   202  func Append17(n int) []int {
   203  	var r []int
   204  	for i := range n {
   205  		// amd64:`.*growsliceNoAlias\b`
   206  		r = append(r, i)
   207  	}
   208  	for i, x := range r {
   209  		println(i, x)
   210  	}
   211  	// amd64:`.*moveSliceNoCapNoScan\b`
   212  	return r
   213  }
   214  
   215  //go:noinline
   216  func useSlice(s []int) {
   217  }
   218  

View as plain text