Skip to content

Commit 1bacc01

Browse files
committed
move to utils
1 parent e42e793 commit 1bacc01

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

src/cmd/compile/internal/escape/escape.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package escape
66

77
import (
88
"fmt"
9-
"go/constant"
10-
"go/token"
119

1210
"cmd/compile/internal/base"
1311
"cmd/compile/internal/ir"
@@ -121,41 +119,6 @@ type escape struct {
121119
}
122120

123121
func Funcs(all []*ir.Func) {
124-
// Try to determine static values of make() calls, to avoid allocating them on the heap.
125-
// We are doing this in escape analysis, so that it happens after inlining and devirtualization.
126-
for _, v := range all {
127-
ir.VisitList(v.Body, func(n ir.Node) {
128-
if n, ok := n.(*ir.MakeExpr); ok {
129-
if n.Cap != nil {
130-
if s := ir.StaticValue(n.Cap); s.Op() == ir.OLITERAL {
131-
cap, ok := s.(*ir.BasicLit)
132-
if !ok || cap.Val().Kind() != constant.Int {
133-
base.Fatalf("unexpected BasicLit Kind")
134-
}
135-
if constant.Compare(cap.Val(), token.GEQ, constant.MakeInt64(0)) {
136-
n.Cap = s
137-
}
138-
}
139-
}
140-
if n.Len != nil {
141-
if s := ir.StaticValue(n.Len); s.Op() == ir.OLITERAL {
142-
len, ok := s.(*ir.BasicLit)
143-
if !ok || len.Val().Kind() != constant.Int {
144-
base.Fatalf("unexpected BasicLit Kind")
145-
}
146-
147-
if constant.Compare(len.Val(), token.GEQ, constant.MakeInt64(0)) {
148-
cap, ok := n.Cap.(*ir.BasicLit)
149-
if n.Cap == nil || (ok && constant.Compare(cap.Val(), token.GEQ, len.Val())) {
150-
n.Len = s
151-
}
152-
}
153-
}
154-
}
155-
}
156-
})
157-
}
158-
159122
ir.VisitFuncsBottomUp(all, Batch)
160123
}
161124

src/cmd/compile/internal/escape/utils.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
package escape
66

77
import (
8+
"cmd/compile/internal/base"
89
"cmd/compile/internal/ir"
910
"cmd/compile/internal/typecheck"
1011
"cmd/compile/internal/types"
12+
"go/constant"
13+
"go/token"
1114
)
1215

1316
func isSliceSelfAssign(dst, src ir.Node) bool {
@@ -206,6 +209,36 @@ func HeapAllocReason(n ir.Node) string {
206209

207210
if n.Op() == ir.OMAKESLICE {
208211
n := n.(*ir.MakeExpr)
212+
213+
// Try to determine static values of make() calls, to avoid allocating them on the heap.
214+
// We are doing this in escape analysis, so that it happens after inlining and devirtualization.
215+
if n.Cap != nil {
216+
if s := ir.StaticValue(n.Cap); s.Op() == ir.OLITERAL {
217+
cap, ok := s.(*ir.BasicLit)
218+
if !ok || cap.Val().Kind() != constant.Int {
219+
base.Fatalf("unexpected BasicLit Kind")
220+
}
221+
if constant.Compare(cap.Val(), token.GEQ, constant.MakeInt64(0)) {
222+
n.Cap = s
223+
}
224+
}
225+
}
226+
if n.Len != nil {
227+
if s := ir.StaticValue(n.Len); s.Op() == ir.OLITERAL {
228+
len, ok := s.(*ir.BasicLit)
229+
if !ok || len.Val().Kind() != constant.Int {
230+
base.Fatalf("unexpected BasicLit Kind")
231+
}
232+
233+
if constant.Compare(len.Val(), token.GEQ, constant.MakeInt64(0)) {
234+
cap, ok := n.Cap.(*ir.BasicLit)
235+
if n.Cap == nil || (ok && constant.Compare(cap.Val(), token.GEQ, len.Val())) {
236+
n.Len = s
237+
}
238+
}
239+
}
240+
}
241+
209242
r := n.Cap
210243
if r == nil {
211244
r = n.Len

0 commit comments

Comments
 (0)