summaryrefslogtreecommitdiffstats
path: root/src/cmd/compile/internal/compare/compare_test.go
blob: db346573346174c82d2cdd2f6758e9f1add0c82e (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
// Copyright 2022 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 compare

import (
	"cmd/compile/internal/base"
	"cmd/compile/internal/typecheck"
	"cmd/compile/internal/types"
	"cmd/internal/obj"
	"cmd/internal/src"
	"cmd/internal/sys"
	"testing"
)

type typefn func() *types.Type

func init() {
	// These are the few constants that need to be initialized in order to use
	// the types package without using the typecheck package by calling
	// typecheck.InitUniverse() (the normal way to initialize the types package).
	types.PtrSize = 8
	types.RegSize = 8
	types.MaxWidth = 1 << 50
	typecheck.InitUniverse()
	base.Ctxt = &obj.Link{Arch: &obj.LinkArch{Arch: &sys.Arch{Alignment: 1, CanMergeLoads: true}}}
}

func TestEqStructCost(t *testing.T) {
	newByteField := func(parent *types.Type, offset int64) *types.Field {
		f := types.NewField(src.XPos{}, parent.Sym(), types.ByteType)
		f.Offset = offset
		return f
	}
	newArrayField := func(parent *types.Type, offset int64, len int64, kind types.Kind) *types.Field {
		f := types.NewField(src.XPos{}, parent.Sym(), types.NewArray(types.Types[kind], len))
		// Call Type.Size here to force the size calculation to be done. If not done here the size returned later is incorrect.
		f.Type.Size()
		f.Offset = offset
		return f
	}
	newField := func(parent *types.Type, offset int64, kind types.Kind) *types.Field {
		f := types.NewField(src.XPos{}, parent.Sym(), types.Types[kind])
		f.Offset = offset
		return f
	}
	tt := []struct {
		name             string
		cost             int64
		nonMergeLoadCost int64
		tfn              typefn
	}{
		{"struct without fields", 0, 0,
			func() *types.Type {
				return types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
			}},
		{"struct with 1 byte field", 1, 1,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := []*types.Field{
					newByteField(parent, 0),
				}
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 8 byte fields", 1, 8,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 8)
				for i := range fields {
					fields[i] = newByteField(parent, int64(i))
				}
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 16 byte fields", 2, 16,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 16)
				for i := range fields {
					fields[i] = newByteField(parent, int64(i))
				}
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 32 byte fields", 4, 32,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 32)
				for i := range fields {
					fields[i] = newByteField(parent, int64(i))
				}
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 2 int32 fields", 1, 2,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 2)
				for i := range fields {
					fields[i] = newField(parent, int64(i*4), types.TINT32)
				}
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 2 int32 fields and 1 int64", 2, 3,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 3)
				fields[0] = newField(parent, int64(0), types.TINT32)
				fields[1] = newField(parent, int64(4), types.TINT32)
				fields[2] = newField(parent, int64(8), types.TINT64)
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 1 int field and 1 string", 3, 3,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 2)
				fields[0] = newField(parent, int64(0), types.TINT64)
				fields[1] = newField(parent, int64(8), types.TSTRING)
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 2 strings", 4, 4,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := make([]*types.Field, 2)
				fields[0] = newField(parent, int64(0), types.TSTRING)
				fields[1] = newField(parent, int64(8), types.TSTRING)
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with 1 large byte array field", 26, 101,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := []*types.Field{
					newArrayField(parent, 0, 101, types.TUINT16),
				}
				parent.SetFields(fields)
				return parent
			},
		},
		{"struct with string array field", 4, 4,
			func() *types.Type {
				parent := types.NewStruct(types.NewPkg("main", ""), []*types.Field{})
				fields := []*types.Field{
					newArrayField(parent, 0, 2, types.TSTRING),
				}
				parent.SetFields(fields)
				return parent
			},
		},
	}

	for _, tc := range tt {
		t.Run(tc.name, func(t *testing.T) {
			want := tc.cost
			base.Ctxt.Arch.CanMergeLoads = true
			actual := EqStructCost(tc.tfn())
			if actual != want {
				t.Errorf("CanMergeLoads=true EqStructCost(%v) = %d, want %d", tc.tfn, actual, want)
			}

			base.Ctxt.Arch.CanMergeLoads = false
			want = tc.nonMergeLoadCost
			actual = EqStructCost(tc.tfn())
			if actual != want {
				t.Errorf("CanMergeLoads=false EqStructCost(%v) = %d, want %d", tc.tfn, actual, want)
			}
		})
	}
}