Skip to content

Commit 0f58c45

Browse files
randall77pull[bot]
authored andcommitted
cmd/compile: simplify algorithm kinds
Add a ANOALG kind which is "ANOEQ, plus has a part that is marked Noalg". That way, AlgType can return just a kind. The field we used to return was used only to get this bit of information. Change-Id: Iaa409742825cc1f19ab414b1f5b74c1f112ed5f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/572075 Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent a915842 commit 0f58c45

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

src/cmd/compile/internal/compare/compare.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import (
1818

1919
// IsRegularMemory reports whether t can be compared/hashed as regular memory.
2020
func IsRegularMemory(t *types.Type) bool {
21-
a, _ := types.AlgType(t)
22-
return a == types.AMEM
21+
return types.AlgType(t) == types.AMEM
2322
}
2423

2524
// Memrun finds runs of struct fields for which memory-only algs are appropriate.

src/cmd/compile/internal/reflectdata/alg.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
// AlgType returns the fixed-width AMEMxx variants instead of the general
2121
// AMEM kind when possible.
2222
func AlgType(t *types.Type) types.AlgKind {
23-
a, _ := types.AlgType(t)
23+
a := types.AlgType(t)
2424
if a == types.AMEM {
2525
if t.Alignment() < int64(base.Ctxt.Arch.Alignment) && t.Alignment() < t.Size() {
2626
// For example, we can't treat [2]int16 as an int32 if int32s require
@@ -254,7 +254,7 @@ func runtimeHashFor(name string, t *types.Type) *ir.Name {
254254

255255
// hashfor returns the function to compute the hash of a value of type t.
256256
func hashfor(t *types.Type) *ir.Name {
257-
switch a, _ := types.AlgType(t); a {
257+
switch types.AlgType(t) {
258258
case types.AMEM:
259259
base.Fatalf("hashfor with AMEM type")
260260
case types.AINTER:
@@ -293,7 +293,7 @@ func sysClosure(name string) *obj.LSym {
293293
// equality for two objects of type t.
294294
func geneq(t *types.Type) *obj.LSym {
295295
switch AlgType(t) {
296-
case types.ANOEQ:
296+
case types.ANOEQ, types.ANOALG:
297297
// The runtime will panic if it tries to compare
298298
// a type with a nil equality function.
299299
return nil
@@ -643,7 +643,7 @@ func eqFunc(t *types.Type) *ir.Func {
643643
// EqFor returns ONAME node represents type t's equal function, and a boolean
644644
// to indicates whether a length needs to be passed when calling the function.
645645
func EqFor(t *types.Type) (ir.Node, bool) {
646-
switch a, _ := types.AlgType(t); a {
646+
switch types.AlgType(t) {
647647
case types.AMEM:
648648
return typecheck.LookupRuntime("memequal", t, t), true
649649
case types.ASPECIAL:

src/cmd/compile/internal/types/alg.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
AFLOAT64
2828
ACPLX64
2929
ACPLX128
30+
ANOALG // implies ANOEQ, and in addition has a part that is marked Noalg
3031

3132
// Type can be compared/hashed as regular memory.
3233
AMEM AlgKind = 100
@@ -36,71 +37,66 @@ const (
3637
)
3738

3839
// AlgType returns the AlgKind used for comparing and hashing Type t.
39-
// If it returns ANOEQ, it also returns the component type of t that
40-
// makes it incomparable.
41-
func AlgType(t *Type) (AlgKind, *Type) {
40+
func AlgType(t *Type) AlgKind {
4241
if t.Noalg() {
43-
return ANOEQ, t
42+
return ANOALG
4443
}
4544

4645
switch t.Kind() {
4746
case TANY, TFORW:
4847
// will be defined later.
49-
return ANOEQ, t
48+
return ANOEQ
5049

5150
case TINT8, TUINT8, TINT16, TUINT16,
5251
TINT32, TUINT32, TINT64, TUINT64,
5352
TINT, TUINT, TUINTPTR,
5453
TBOOL, TPTR,
5554
TCHAN, TUNSAFEPTR:
56-
return AMEM, nil
55+
return AMEM
5756

5857
case TFUNC, TMAP:
59-
return ANOEQ, t
58+
return ANOEQ
6059

6160
case TFLOAT32:
62-
return AFLOAT32, nil
61+
return AFLOAT32
6362

6463
case TFLOAT64:
65-
return AFLOAT64, nil
64+
return AFLOAT64
6665

6766
case TCOMPLEX64:
68-
return ACPLX64, nil
67+
return ACPLX64
6968

7069
case TCOMPLEX128:
71-
return ACPLX128, nil
70+
return ACPLX128
7271

7372
case TSTRING:
74-
return ASTRING, nil
73+
return ASTRING
7574

7675
case TINTER:
7776
if t.IsEmptyInterface() {
78-
return ANILINTER, nil
77+
return ANILINTER
7978
}
80-
return AINTER, nil
79+
return AINTER
8180

8281
case TSLICE:
83-
return ANOEQ, t
82+
return ANOEQ
8483

8584
case TARRAY:
86-
a, bad := AlgType(t.Elem())
87-
switch a {
88-
case AMEM:
89-
return AMEM, nil
90-
case ANOEQ:
91-
return ANOEQ, bad
85+
a := AlgType(t.Elem())
86+
if a == AMEM || a == ANOEQ || a == ANOALG {
87+
return a
9288
}
9389

9490
switch t.NumElem() {
9591
case 0:
9692
// We checked above that the element type is comparable.
97-
return AMEM, nil
93+
return AMEM
9894
case 1:
9995
// Single-element array is same as its lone element.
100-
return a, nil
96+
return a
10197
}
10298

103-
return ASPECIAL, nil
99+
return ASPECIAL
104100

105101
case TSTRUCT:
106102
fields := t.Fields()
@@ -113,9 +109,9 @@ func AlgType(t *Type) (AlgKind, *Type) {
113109
ret := AMEM
114110
for i, f := range fields {
115111
// All fields must be comparable.
116-
a, bad := AlgType(f.Type)
117-
if a == ANOEQ {
118-
return ANOEQ, bad
112+
a := AlgType(f.Type)
113+
if a == ANOEQ || a == ANOALG {
114+
return a
119115
}
120116

121117
// Blank fields, padded fields, fields with non-memory
@@ -125,24 +121,23 @@ func AlgType(t *Type) (AlgKind, *Type) {
125121
}
126122
}
127123

128-
return ret, nil
124+
return ret
129125
}
130126

131127
base.Fatalf("AlgType: unexpected type %v", t)
132-
return 0, nil
128+
return 0
133129
}
134130

135131
// TypeHasNoAlg reports whether t does not have any associated hash/eq
136132
// algorithms because t, or some component of t, is marked Noalg.
137133
func TypeHasNoAlg(t *Type) bool {
138-
a, bad := AlgType(t)
139-
return a == ANOEQ && bad.Noalg()
134+
return AlgType(t) == ANOALG
140135
}
141136

142137
// IsComparable reports whether t is a comparable type.
143138
func IsComparable(t *Type) bool {
144-
a, _ := AlgType(t)
145-
return a != ANOEQ
139+
a := AlgType(t)
140+
return a != ANOEQ && a != ANOALG
146141
}
147142

148143
// IncomparableField returns an incomparable Field of struct Type t, if any.

src/cmd/compile/internal/types/algkind_string.go

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)