aboutsummaryrefslogtreecommitdiff
path: root/test/codegen/slices.go
blob: 40e857f9f69d7f83cb263fffe717d7d6be5dfff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// asmcheck

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package codegen

// This file contains code generation tests related to the handling of
// slice types.

// ------------------ //
//      Clear         //
// ------------------ //

// Issue #5373 optimize memset idiom

func SliceClear(s []int) []int {
	// amd64:`.*memclrNoHeapPointers`
	for i := range s {
		s[i] = 0
	}
	return s
}

func SliceClearPointers(s []*int) []*int {
	// amd64:`.*memclrHasPointers`
	for i := range s {
		s[i] = nil
	}
	return s
}

// ------------------ //
//      Extension     //
// ------------------ //

// Issue #21266 - avoid makeslice in append(x, make([]T, y)...)

func SliceExtensionConst(s []int) []int {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:-`.*runtime\.panicmakeslicelen`
	return append(s, make([]int, 1<<2)...)
}

func SliceExtensionConstInt64(s []int) []int {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:-`.*runtime\.panicmakeslicelen`
	return append(s, make([]int, int64(1<<2))...)
}

func SliceExtensionConstUint64(s []int) []int {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:-`.*runtime\.panicmakeslicelen`
	return append(s, make([]int, uint64(1<<2))...)
}

func SliceExtensionConstUint(s []int) []int {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:-`.*runtime\.panicmakeslicelen`
	return append(s, make([]int, uint(1<<2))...)
}

func SliceExtensionPointer(s []*int, l int) []*int {
	// amd64:`.*runtime\.memclrHasPointers`
	// amd64:-`.*runtime\.makeslice`
	return append(s, make([]*int, l)...)
}

func SliceExtensionVar(s []byte, l int) []byte {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	return append(s, make([]byte, l)...)
}

func SliceExtensionVarInt64(s []byte, l int64) []byte {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:`.*runtime\.panicmakeslicelen`
	return append(s, make([]byte, l)...)
}

func SliceExtensionVarUint64(s []byte, l uint64) []byte {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:`.*runtime\.panicmakeslicelen`
	return append(s, make([]byte, l)...)
}

func SliceExtensionVarUint(s []byte, l uint) []byte {
	// amd64:`.*runtime\.memclrNoHeapPointers`
	// amd64:-`.*runtime\.makeslice`
	// amd64:`.*runtime\.panicmakeslicelen`
	return append(s, make([]byte, l)...)
}

func SliceExtensionInt64(s []int, l64 int64) []int {
	// 386:`.*runtime\.makeslice`
	// 386:-`.*runtime\.memclr`
	return append(s, make([]int, l64)...)
}

// ------------------ //
//      Make+Copy     //
// ------------------ //

// Issue #26252 - avoid memclr for make+copy

func SliceMakeCopyLen(s []int) []int {
	// amd64:`.*runtime\.mallocgc`
	// amd64:`.*runtime\.memmove`
	// amd64:-`.*runtime\.makeslice`
	a := make([]int, len(s))
	copy(a, s)
	return a
}

func SliceMakeCopyLenPtr(s []*int) []*int {
	// amd64:`.*runtime\.makeslicecopy`
	// amd64:-`.*runtime\.makeslice\(`
	// amd64:-`.*runtime\.typedslicecopy
	a := make([]*int, len(s))
	copy(a, s)
	return a
}

func SliceMakeCopyConst(s []int) []int {
	// amd64:`.*runtime\.makeslicecopy`
	// amd64:-`.*runtime\.makeslice\(`
	// amd64:-`.*runtime\.memmove`
	a := make([]int, 4)
	copy(a, s)
	return a
}

func SliceMakeCopyConstPtr(s []*int) []*int {
	// amd64:`.*runtime\.makeslicecopy`
	// amd64:-`.*runtime\.makeslice\(`
	// amd64:-`.*runtime\.typedslicecopy
	a := make([]*int, 4)
	copy(a, s)
	return a
}

func SliceMakeCopyNoOptNoDeref(s []*int) []*int {
	a := new([]*int)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	*a = make([]*int, 4)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.typedslicecopy`
	copy(*a, s)
	return *a
}

func SliceMakeCopyNoOptNoVar(s []*int) []*int {
	a := make([][]*int, 1)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a[0] = make([]*int, 4)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.typedslicecopy`
	copy(a[0], s)
	return a[0]
}

func SliceMakeCopyNoOptBlank(s []*int) []*int {
	var a []*int
	// amd64:-`.*runtime\.makeslicecopy`
	_ = make([]*int, 4)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.typedslicecopy`
	copy(a, s)
	return a
}

func SliceMakeCopyNoOptNoMake(s []*int) []*int {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:-`.*runtime\.objectnew`
	a := *new([]*int)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.typedslicecopy`
	copy(a, s)
	return a
}

func SliceMakeCopyNoOptNoHeapAlloc(s []*int) int {
	// amd64:-`.*runtime\.makeslicecopy`
	a := make([]*int, 4)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.typedslicecopy`
	copy(a, s)
	return cap(a)
}

func SliceMakeCopyNoOptNoCap(s []*int) []*int {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]*int, 0, 4)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.typedslicecopy`
	copy(a, s)
	return a
}

func SliceMakeCopyNoOptNoCopy(s []*int) []*int {
	copy := func(x, y []*int) {}
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]*int, 4)
	// amd64:-`.*runtime\.makeslicecopy`
	copy(a, s)
	return a
}

func SliceMakeCopyNoOptWrongOrder(s []*int) []*int {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]*int, 4)
	// amd64:`.*runtime\.typedslicecopy`
	// amd64:-`.*runtime\.makeslicecopy`
	copy(s, a)
	return a
}

func SliceMakeCopyNoOptWrongAssign(s []*int) []*int {
	var a []*int
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	s = make([]*int, 4)
	// amd64:`.*runtime\.typedslicecopy`
	// amd64:-`.*runtime\.makeslicecopy`
	copy(a, s)
	return s
}

func SliceMakeCopyNoOptCopyLength(s []*int) (int, []*int) {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]*int, 4)
	// amd64:`.*runtime\.typedslicecopy`
	// amd64:-`.*runtime\.makeslicecopy`
	n := copy(a, s)
	return n, a
}

func SliceMakeCopyNoOptSelfCopy(s []*int) []*int {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]*int, 4)
	// amd64:`.*runtime\.typedslicecopy`
	// amd64:-`.*runtime\.makeslicecopy`
	copy(a, a)
	return a
}

func SliceMakeCopyNoOptTargetReference(s []*int) []*int {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]*int, 4)
	// amd64:`.*runtime\.typedslicecopy`
	// amd64:-`.*runtime\.makeslicecopy`
	copy(a, s[:len(a)])
	return a
}

func SliceMakeCopyNoOptCap(s []int) []int {
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.makeslice\(`
	a := make([]int, len(s), 9)
	// amd64:-`.*runtime\.makeslicecopy`
	// amd64:`.*runtime\.memmove`
	copy(a, s)
	return a
}

func SliceMakeCopyNoMemmoveDifferentLen(s []int) []int {
	// amd64:`.*runtime\.makeslicecopy`
	// amd64:-`.*runtime\.memmove`
	a := make([]int, len(s)-1)
	// amd64:-`.*runtime\.memmove`
	copy(a, s)
	return a
}

// ---------------------- //
//   Nil check of &s[0]   //
// ---------------------- //
// See issue 30366
func SliceNilCheck(s []int) {
	p := &s[0]
	// amd64:-`TESTB`
	_ = *p
}

// ---------------------- //
//   Init slice literal   //
// ---------------------- //
// See issue 21561
func InitSmallSliceLiteral() []int {
	// amd64:`MOVQ\t[$]42`
	return []int{42}
}

func InitNotSmallSliceLiteral() []int {
	// amd64:`MOVQ\t.*autotmp_`
	return []int{
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
		42,
	}
}