]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/escape/assign.go
cmd/compile/internal/escape: optimize indirect closure calls
[gostls13.git] / src / cmd / compile / internal / escape / assign.go
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package escape
6
7 import (
8         "cmd/compile/internal/base"
9         "cmd/compile/internal/ir"
10 )
11
12 // addr evaluates an addressable expression n and returns a hole
13 // that represents storing into the represented location.
14 func (e *escape) addr(n ir.Node) hole {
15         if n == nil || ir.IsBlank(n) {
16                 // Can happen in select case, range, maybe others.
17                 return e.discardHole()
18         }
19
20         k := e.heapHole()
21
22         switch n.Op() {
23         default:
24                 base.Fatalf("unexpected addr: %v", n)
25         case ir.ONAME:
26                 n := n.(*ir.Name)
27                 if n.Class == ir.PEXTERN {
28                         break
29                 }
30                 k = e.oldLoc(n).asHole()
31         case ir.OLINKSYMOFFSET:
32                 break
33         case ir.ODOT:
34                 n := n.(*ir.SelectorExpr)
35                 k = e.addr(n.X)
36         case ir.OINDEX:
37                 n := n.(*ir.IndexExpr)
38                 e.discard(n.Index)
39                 if n.X.Type().IsArray() {
40                         k = e.addr(n.X)
41                 } else {
42                         e.mutate(n.X)
43                 }
44         case ir.ODEREF, ir.ODOTPTR:
45                 e.mutate(n)
46         case ir.OINDEXMAP:
47                 n := n.(*ir.IndexExpr)
48                 e.discard(n.X)
49                 e.assignHeap(n.Index, "key of map put", n)
50         }
51
52         return k
53 }
54
55 func (e *escape) mutate(n ir.Node) {
56         e.expr(e.mutatorHole(), n)
57 }
58
59 func (e *escape) addrs(l ir.Nodes) []hole {
60         var ks []hole
61         for _, n := range l {
62                 ks = append(ks, e.addr(n))
63         }
64         return ks
65 }
66
67 func (e *escape) assignHeap(src ir.Node, why string, where ir.Node) {
68         e.expr(e.heapHole().note(where, why), src)
69 }
70
71 // assignList evaluates the assignment dsts... = srcs....
72 func (e *escape) assignList(dsts, srcs []ir.Node, why string, where ir.Node) {
73         ks := e.addrs(dsts)
74         for i, k := range ks {
75                 var src ir.Node
76                 if i < len(srcs) {
77                         src = srcs[i]
78                 }
79
80                 if dst := dsts[i]; dst != nil {
81                         // Detect implicit conversion of uintptr to unsafe.Pointer when
82                         // storing into reflect.{Slice,String}Header.
83                         if dst.Op() == ir.ODOTPTR && ir.IsReflectHeaderDataField(dst) {
84                                 e.unsafeValue(e.heapHole().note(where, why), src)
85                                 continue
86                         }
87
88                         // Filter out some no-op assignments for escape analysis.
89                         if src != nil && isSelfAssign(dst, src) {
90                                 if base.Flag.LowerM != 0 {
91                                         base.WarnfAt(where.Pos(), "%v ignoring self-assignment in %v", e.curfn, where)
92                                 }
93                                 k = e.discardHole()
94                         }
95                 }
96
97                 e.expr(k.note(where, why), src)
98         }
99
100         e.reassigned(ks, where)
101 }
102
103 // reassigned marks the locations associated with the given holes as
104 // reassigned, unless the location represents a variable declared and
105 // assigned exactly once by where.
106 func (e *escape) reassigned(ks []hole, where ir.Node) {
107         if as, ok := where.(*ir.AssignStmt); ok && as.Op() == ir.OAS && as.Y == nil {
108                 if dst, ok := as.X.(*ir.Name); ok && dst.Op() == ir.ONAME && dst.Defn == nil {
109                         // Zero-value assignment for variable declared without an
110                         // explicit initial value. Assume this is its initialization
111                         // statement.
112                         return
113                 }
114         }
115
116         for _, k := range ks {
117                 loc := k.dst
118                 // Variables declared by range statements are assigned on every iteration.
119                 if n, ok := loc.n.(*ir.Name); ok && n.Defn == where && where.Op() != ir.ORANGE {
120                         continue
121                 }
122                 loc.reassigned = true
123         }
124 }