simplify, fix test

Change-Id: I2f845692852ecc65db21ac2160123b72fdcf4892
This commit is contained in:
Mateusz Poliwczak 2025-02-17 12:18:07 +01:00
parent e5847b26c3
commit d78c1b4ca5
2 changed files with 14 additions and 25 deletions

View File

@ -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"
}
}

View File

@ -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
}