From d78c1b4ca55fa53282e665009f689d0b013f1434 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Mon, 17 Feb 2025 12:18:07 +0100 Subject: [PATCH] simplify, fix test Change-Id: I2f845692852ecc65db21ac2160123b72fdcf4892 --- src/cmd/compile/internal/escape/utils.go | 37 +++++++++--------------- test/escape_slice.go | 2 +- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/cmd/compile/internal/escape/utils.go b/src/cmd/compile/internal/escape/utils.go index efe69ffc58..d9cb9bdf8e 100644 --- a/src/cmd/compile/internal/escape/utils.go +++ b/src/cmd/compile/internal/escape/utils.go @@ -210,38 +210,27 @@ func HeapAllocReason(n ir.Node) string { if n.Op() == ir.OMAKESLICE { n := n.(*ir.MakeExpr) + r := &n.Cap + if n.Cap == nil { + r = &n.Len + } + // Try to determine static values of make() calls, to avoid allocating them on the heap. // We are doing this in escape analysis, so that it happens after inlining and devirtualization. - if n.Cap != nil { - if s := ir.StaticValue(n.Cap); s.Op() == ir.OLITERAL { - cap, ok := s.(*ir.BasicLit) - if !ok || cap.Val().Kind() != constant.Int { - base.Fatalf("unexpected BasicLit Kind") - } - if constant.Compare(cap.Val(), token.GEQ, constant.MakeInt64(0)) { - n.Cap = s - } + if s := ir.StaticValue(*r); s.Op() == ir.OLITERAL { + lit, ok := s.(*ir.BasicLit) + if !ok || lit.Val().Kind() != constant.Int { + base.Fatalf("unexpected BasicLit Kind") } - } else if n.Len != nil { - if s := ir.StaticValue(n.Len); s.Op() == ir.OLITERAL { - len, ok := s.(*ir.BasicLit) - if !ok || len.Val().Kind() != constant.Int { - base.Fatalf("unexpected BasicLit Kind") - } - if constant.Compare(len.Val(), token.GEQ, constant.MakeInt64(0)) { - n.Len = s - } + if constant.Compare(lit.Val(), token.GEQ, constant.MakeInt64(0)) { + *r = lit } } - r := n.Cap - if r == nil { - r = n.Len - } - if !ir.IsSmallIntConst(r) { + if !ir.IsSmallIntConst(*r) { return "non-constant size" } - if t := n.Type(); t.Elem().Size() != 0 && ir.Int64Val(r) > ir.MaxImplicitStackVarSize/t.Elem().Size() { + if t := n.Type(); t.Elem().Size() != 0 && ir.Int64Val(*r) > ir.MaxImplicitStackVarSize/t.Elem().Size() { return "too large for stack" } } diff --git a/test/escape_slice.go b/test/escape_slice.go index 9687722a4b..65181e57d7 100644 --- a/test/escape_slice.go +++ b/test/escape_slice.go @@ -96,7 +96,7 @@ func slice10() []*int { func slice11() { i := 2 s := make([]int, 2, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape" - s = make([]int, i, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape" + s = make([]int, i, 3) // ERROR "make\(\[\]int, i, 3\) does not escape" s = make([]int, i, 1) // ERROR "make\(\[\]int, i, 1\) does not escape" _ = s }