diff --git a/src/cmd/compile/internal/ir/copy.go b/src/cmd/compile/internal/ir/copy.go index 7f5d313513..0ab355f767 100644 --- a/src/cmd/compile/internal/ir/copy.go +++ b/src/cmd/compile/internal/ir/copy.go @@ -64,12 +64,6 @@ func Copy(n Node) Node { return c } -func copyList(x Nodes) Nodes { - c := make([]Node, x.Len()) - copy(c, x.Slice()) - return AsNodes(c) -} - // DeepCopy returns a “deep” copy of n, with its entire structure copied // (except for shared nodes like ONAME, ONONAME, OLITERAL, and OTYPE). // If pos.IsKnown(), it sets the source position of newly allocated Nodes to pos. diff --git a/src/cmd/compile/internal/ir/dump.go b/src/cmd/compile/internal/ir/dump.go index bff3a40855..9d6042f78a 100644 --- a/src/cmd/compile/internal/ir/dump.go +++ b/src/cmd/compile/internal/ir/dump.go @@ -140,15 +140,8 @@ func (p *dumper) dump(x reflect.Value, depth int) { return } - // special cases - switch v := x.Interface().(type) { - case Nodes: - // unpack Nodes since reflect cannot look inside - // due to the unexported field in its struct - x = reflect.ValueOf(v.Slice()) - - case src.XPos: - p.printf("%s", base.FmtPos(v)) + if pos, ok := x.Interface().(src.XPos); ok { + p.printf("%s", base.FmtPos(pos)) return } diff --git a/src/cmd/compile/internal/ir/mini.go b/src/cmd/compile/internal/ir/mini.go index bf221f75ed..d1d2e266ed 100644 --- a/src/cmd/compile/internal/ir/mini.go +++ b/src/cmd/compile/internal/ir/mini.go @@ -114,22 +114,22 @@ func (n *miniNode) SetRight(x Node) { } } func (n *miniNode) SetInit(x Nodes) { - if x != (Nodes{}) { + if x != nil { panic(n.no("SetInit")) } } func (n *miniNode) SetBody(x Nodes) { - if x != (Nodes{}) { + if x != nil { panic(n.no("SetBody")) } } func (n *miniNode) SetList(x Nodes) { - if x != (Nodes{}) { + if x != nil { panic(n.no("SetList")) } } func (n *miniNode) SetRlist(x Nodes) { - if x != (Nodes{}) { + if x != nil { panic(n.no("SetRlist")) } } diff --git a/src/cmd/compile/internal/ir/node.go b/src/cmd/compile/internal/ir/node.go index dc86b6c683..ccf3671085 100644 --- a/src/cmd/compile/internal/ir/node.go +++ b/src/cmd/compile/internal/ir/node.go @@ -359,7 +359,7 @@ const ( // Nodes is a pointer to a slice of *Node. // For fields that are not used in most nodes, this is used instead of // a slice to save space. -type Nodes struct{ slice *[]Node } +type Nodes []Node // immutableEmptyNodes is an immutable, empty Nodes list. // The methods that would modify it panic instead. @@ -367,43 +367,37 @@ var immutableEmptyNodes = Nodes{} // asNodes returns a slice of *Node as a Nodes value. func AsNodes(s []Node) Nodes { - return Nodes{&s} + return s } // Slice returns the entries in Nodes as a slice. // Changes to the slice entries (as in s[i] = n) will be reflected in // the Nodes. func (n Nodes) Slice() []Node { - if n.slice == nil { - return nil - } - return *n.slice + return n } // Len returns the number of entries in Nodes. func (n Nodes) Len() int { - if n.slice == nil { - return 0 - } - return len(*n.slice) + return len(n) } // Index returns the i'th element of Nodes. // It panics if n does not have at least i+1 elements. func (n Nodes) Index(i int) Node { - return (*n.slice)[i] + return n[i] } // First returns the first element of Nodes (same as n.Index(0)). // It panics if n has no elements. func (n Nodes) First() Node { - return (*n.slice)[0] + return n[0] } // Second returns the second element of Nodes (same as n.Index(1)). // It panics if n has fewer than two elements. func (n Nodes) Second() Node { - return (*n.slice)[1] + return n[1] } func (n *Nodes) mutate() { @@ -422,64 +416,56 @@ func (n *Nodes) Set(s []Node) { } n.mutate() } - if len(s) == 0 { - n.slice = nil - } else { - // Copy s and take address of t rather than s to avoid - // allocation in the case where len(s) == 0 (which is - // over 3x more common, dynamically, for make.bash). - t := s - n.slice = &t - } + *n = s } // Set1 sets n to a slice containing a single node. func (n *Nodes) Set1(n1 Node) { n.mutate() - n.slice = &[]Node{n1} + *n = []Node{n1} } // Set2 sets n to a slice containing two nodes. func (n *Nodes) Set2(n1, n2 Node) { n.mutate() - n.slice = &[]Node{n1, n2} + *n = []Node{n1, n2} } // Set3 sets n to a slice containing three nodes. func (n *Nodes) Set3(n1, n2, n3 Node) { n.mutate() - n.slice = &[]Node{n1, n2, n3} + *n = []Node{n1, n2, n3} } // MoveNodes sets n to the contents of n2, then clears n2. func (n *Nodes) MoveNodes(n2 *Nodes) { n.mutate() - n.slice = n2.slice - n2.slice = nil + *n = *n2 + *n2 = nil } // SetIndex sets the i'th element of Nodes to node. // It panics if n does not have at least i+1 elements. func (n Nodes) SetIndex(i int, node Node) { - (*n.slice)[i] = node + n[i] = node } // SetFirst sets the first element of Nodes to node. // It panics if n does not have at least one elements. func (n Nodes) SetFirst(node Node) { - (*n.slice)[0] = node + n[0] = node } // SetSecond sets the second element of Nodes to node. // It panics if n does not have at least two elements. func (n Nodes) SetSecond(node Node) { - (*n.slice)[1] = node + n[1] = node } // Addr returns the address of the i'th element of Nodes. // It panics if n does not have at least i+1 elements. func (n Nodes) Addr(i int) *Node { - return &(*n.slice)[i] + return &n[i] } // Append appends entries to Nodes. @@ -488,13 +474,7 @@ func (n *Nodes) Append(a ...Node) { return } n.mutate() - if n.slice == nil { - s := make([]Node, len(a)) - copy(s, a) - n.slice = &s - return - } - *n.slice = append(*n.slice, a...) + *n = append(*n, a...) } // Prepend prepends entries to Nodes. @@ -504,38 +484,29 @@ func (n *Nodes) Prepend(a ...Node) { return } n.mutate() - if n.slice == nil { - n.slice = &a - } else { - *n.slice = append(a, *n.slice...) - } + *n = append(a, *n...) +} + +// Take clears n, returning its former contents. +func (n *Nodes) Take() []Node { + ret := *n + *n = nil + return ret } // AppendNodes appends the contents of *n2 to n, then clears n2. func (n *Nodes) AppendNodes(n2 *Nodes) { n.mutate() - switch { - case n2.slice == nil: - case n.slice == nil: - n.slice = n2.slice - default: - *n.slice = append(*n.slice, *n2.slice...) - } - n2.slice = nil + *n = append(*n, n2.Take()...) } // Copy returns a copy of the content of the slice. func (n Nodes) Copy() Nodes { - var c Nodes - if n.slice == nil { - return c + if n == nil { + return nil } - c.slice = new([]Node) - if *n.slice == nil { - return c - } - *c.slice = make([]Node, n.Len()) - copy(*c.slice, n.Slice()) + c := make(Nodes, n.Len()) + copy(c, n) return c } diff --git a/src/cmd/compile/internal/ir/sizeof_test.go b/src/cmd/compile/internal/ir/sizeof_test.go index 181f1462fe..2a618f85ed 100644 --- a/src/cmd/compile/internal/ir/sizeof_test.go +++ b/src/cmd/compile/internal/ir/sizeof_test.go @@ -20,8 +20,8 @@ func TestSizeof(t *testing.T) { _32bit uintptr // size on 32bit platforms _64bit uintptr // size on 64bit platforms }{ - {Func{}, 168, 288}, - {Name{}, 124, 216}, + {Func{}, 200, 352}, + {Name{}, 132, 232}, } for _, tt := range tests {