From 2c4b029b752a5aa8315e56b9563b2052fe8dd3fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9my=20Oudompheng?= Date: Thu, 30 May 2013 08:32:00 +0200 Subject: [PATCH] cmd/gc: use escape analysis result for make([]T, constant Escape analysis already gives that the underlying array does not escape but the result was ignored. Fixes #5484. R=golang-dev, dave, daniel.morsing CC=golang-dev https://golang.org/cl/9662046 --- src/cmd/gc/walk.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/cmd/gc/walk.c b/src/cmd/gc/walk.c index 3dd8930969..a4e20e046c 100644 --- a/src/cmd/gc/walk.c +++ b/src/cmd/gc/walk.c @@ -1246,18 +1246,35 @@ walkexpr(Node **np, NodeList **init) goto ret; case OMAKESLICE: - // makeslice(t *Type, nel int64, max int64) (ary []any) l = n->left; r = n->right; if(r == nil) l = r = safeexpr(l, init); t = n->type; - fn = syslook("makeslice", 1); - argtype(fn, t->type); // any-1 - n = mkcall1(fn, n->type, init, - typename(n->type), - conv(l, types[TINT64]), - conv(r, types[TINT64])); + if(n->esc == EscNone + && smallintconst(l) && smallintconst(r) + && mpgetfix(r->val.u.xval) < (1ULL<<16) / t->type->width) { + // var arr [r]T + // n = arr[:l] + t = aindex(r, t->type); // [r]T + var = temp(t); + a = nod(OAS, var, N); // zero temp + typecheck(&a, Etop); + *init = list(*init, a); + r = nod(OSLICE, var, nod(OKEY, N, l)); // arr[:l] + r = conv(r, n->type); // in case n->type is named. + typecheck(&r, Erv); + walkexpr(&r, init); + n = r; + } else { + // makeslice(t *Type, nel int64, max int64) (ary []any) + fn = syslook("makeslice", 1); + argtype(fn, t->type); // any-1 + n = mkcall1(fn, n->type, init, + typename(n->type), + conv(l, types[TINT64]), + conv(r, types[TINT64])); + } goto ret; case ORUNESTR: -- 2.48.1