Skip to content

Commit d78c1b4

Browse files
committed
simplify, fix test
Change-Id: I2f845692852ecc65db21ac2160123b72fdcf4892
1 parent e5847b2 commit d78c1b4

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,38 +210,27 @@ func HeapAllocReason(n ir.Node) string {
210210
if n.Op() == ir.OMAKESLICE {
211211
n := n.(*ir.MakeExpr)
212212

213+
r := &n.Cap
214+
if n.Cap == nil {
215+
r = &n.Len
216+
}
217+
213218
// Try to determine static values of make() calls, to avoid allocating them on the heap.
214219
// 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-
}
220+
if s := ir.StaticValue(*r); s.Op() == ir.OLITERAL {
221+
lit, ok := s.(*ir.BasicLit)
222+
if !ok || lit.Val().Kind() != constant.Int {
223+
base.Fatalf("unexpected BasicLit Kind")
224224
}
225-
} else if n.Len != nil {
226-
if s := ir.StaticValue(n.Len); s.Op() == ir.OLITERAL {
227-
len, ok := s.(*ir.BasicLit)
228-
if !ok || len.Val().Kind() != constant.Int {
229-
base.Fatalf("unexpected BasicLit Kind")
230-
}
231-
if constant.Compare(len.Val(), token.GEQ, constant.MakeInt64(0)) {
232-
n.Len = s
233-
}
225+
if constant.Compare(lit.Val(), token.GEQ, constant.MakeInt64(0)) {
226+
*r = lit
234227
}
235228
}
236229

237-
r := n.Cap
238-
if r == nil {
239-
r = n.Len
240-
}
241-
if !ir.IsSmallIntConst(r) {
230+
if !ir.IsSmallIntConst(*r) {
242231
return "non-constant size"
243232
}
244-
if t := n.Type(); t.Elem().Size() != 0 && ir.Int64Val(r) > ir.MaxImplicitStackVarSize/t.Elem().Size() {
233+
if t := n.Type(); t.Elem().Size() != 0 && ir.Int64Val(*r) > ir.MaxImplicitStackVarSize/t.Elem().Size() {
245234
return "too large for stack"
246235
}
247236
}

test/escape_slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func slice10() []*int {
9696
func slice11() {
9797
i := 2
9898
s := make([]int, 2, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape"
99-
s = make([]int, i, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape"
99+
s = make([]int, i, 3) // ERROR "make\(\[\]int, i, 3\) does not escape"
100100
s = make([]int, i, 1) // ERROR "make\(\[\]int, i, 1\) does not escape"
101101
_ = s
102102
}

0 commit comments

Comments
 (0)