Commit 96d90d09 authored by Russ Cox's avatar Russ Cox

cmd/gc: shorten even more temporary lifetimes

1. Use n->alloc, not n->left, to hold the allocated temp being
passed from orderstmt/orderexpr to walk.

2. Treat method values the same as closures.

3. Use killed temporary for composite literal passed to
non-escaping function argument.

4. Clean temporaries promptly in if and for statements.

5. Clean temporaries promptly in select statements.
As part of this, move all the temporary-generating logic
out of select.c into order.c, so that the temporaries can
be reclaimed.

With the new temporaries, can re-enable the 1-entry
select optimization. Fixes issue 7672.

While we're here, fix a 1-line bug in select processing
turned up by the new liveness test (but unrelated; select.c:72).
Fixes #7686.

6. Clean temporaries (but not particularly promptly) in switch
and range statements.

7. Clean temporary used during convT2E/convT2I.

8. Clean temporaries promptly during && and || expressions.

---

CL 81940043 reduced the number of ambiguously live temps
in the godoc binary from 860 to 711.

CL 83090046 reduced the number from 711 to 121.

This CL reduces the number from 121 to 23.

15 the 23 that remain are in fact ambiguously live.
The final 8 could be fixed but are not trivial and
not common enough to warrant work at this point
in the release cycle.

These numbers only count ambiguously live temps,
not ambiguously live user-declared variables.
There are 18 such variables in the godoc binary after this CL,
so a total of 41 ambiguously live temps or user-declared
variables.

The net effect is that zeroing anything on entry to a function
should now be a rare event, whereas earlier it was the
common case.

This is good enough for Go 1.3, and probably good
enough for future releases too.

Fixes #7345.

LGTM=khr
R=khr
CC=golang-codereviews
https://golang.org/cl/83000048
parent 47acf167
...@@ -255,11 +255,11 @@ walkclosure(Node *func, NodeList **init) ...@@ -255,11 +255,11 @@ walkclosure(Node *func, NodeList **init)
clos->left->esc = func->esc; clos->left->esc = func->esc;
// non-escaping temp to use, if any. // non-escaping temp to use, if any.
// orderexpr did not compute the type; fill it in now. // orderexpr did not compute the type; fill it in now.
if(func->left != N) { if(func->alloc != N) {
func->left->type = clos->left->left->type; func->alloc->type = clos->left->left->type;
func->left->orig->type = func->left->type; func->alloc->orig->type = func->alloc->type;
clos->left->right = func->left; clos->left->right = func->alloc;
func->left = N; func->alloc = N;
} }
walkexpr(&clos, init); walkexpr(&clos, init);
...@@ -451,6 +451,14 @@ walkpartialcall(Node *n, NodeList **init) ...@@ -451,6 +451,14 @@ walkpartialcall(Node *n, NodeList **init)
// typecheck will insert a PTRLIT node under CONVNOP, // typecheck will insert a PTRLIT node under CONVNOP,
// tag it with escape analysis result. // tag it with escape analysis result.
clos->left->esc = n->esc; clos->left->esc = n->esc;
// non-escaping temp to use, if any.
// orderexpr did not compute the type; fill it in now.
if(n->alloc != N) {
n->alloc->type = clos->left->left->type;
n->alloc->orig->type = n->alloc->type;
clos->left->right = n->alloc;
n->alloc = N;
}
walkexpr(&clos, init); walkexpr(&clos, init);
return clos; return clos;
......
...@@ -966,9 +966,16 @@ esccall(EscState *e, Node *n) ...@@ -966,9 +966,16 @@ esccall(EscState *e, Node *n)
} }
if(haspointers(t->type)) { if(haspointers(t->type)) {
if(escassignfromtag(e, t->note, n->escretval, src) == EscNone) { if(escassignfromtag(e, t->note, n->escretval, src) == EscNone) {
switch(src->op) { a = src;
while(a->op == OCONVNOP)
a = a->left;
switch(a->op) {
case OCALLPART:
case OCLOSURE: case OCLOSURE:
case ODDDARG: case ODDDARG:
case OARRAYLIT:
case OPTRLIT:
case OSTRUCTLIT:
// The callee has already been analyzed, so its arguments have esc tags. // The callee has already been analyzed, so its arguments have esc tags.
// The argument is marked as not escaping at all. // The argument is marked as not escaping at all.
// Record that fact so that any temporary used for // Record that fact so that any temporary used for
...@@ -978,7 +985,7 @@ esccall(EscState *e, Node *n) ...@@ -978,7 +985,7 @@ esccall(EscState *e, Node *n)
// src->esc == EscNone means that src does not escape the current function. // src->esc == EscNone means that src does not escape the current function.
// src->noescape = 1 here means that src does not escape this statement // src->noescape = 1 here means that src does not escape this statement
// in the current function. // in the current function.
src->noescape = 1; a->noescape = 1;
break; break;
} }
} }
......
This diff is collapsed.
...@@ -186,7 +186,8 @@ walkrange(Node *n) ...@@ -186,7 +186,8 @@ walkrange(Node *n)
// we only use a once, so no copy needed. // we only use a once, so no copy needed.
ha = a; ha = a;
th = hiter(t); th = hiter(t);
hit = n->left; hit = n->alloc;
hit->type = th;
n->left = N; n->left = N;
keyname = newname(th->type->sym); // depends on layout of iterator struct. See reflect.c:hiter keyname = newname(th->type->sym); // depends on layout of iterator struct. See reflect.c:hiter
valname = newname(th->type->down->sym); // ditto valname = newname(th->type->down->sym); // ditto
......
...@@ -69,6 +69,7 @@ typecheckselect(Node *sel) ...@@ -69,6 +69,7 @@ typecheckselect(Node *sel)
n->op = OSELRECV2; n->op = OSELRECV2;
n->left = n->list->n; n->left = n->list->n;
n->ntest = n->list->next->n; n->ntest = n->list->next->n;
n->list = nil;
n->right = n->rlist->n; n->right = n->rlist->n;
n->rlist = nil; n->rlist = nil;
break; break;
...@@ -94,7 +95,7 @@ void ...@@ -94,7 +95,7 @@ void
walkselect(Node *sel) walkselect(Node *sel)
{ {
int lno, i; int lno, i;
Node *n, *r, *a, *tmp, *var, *cas, *dflt, *ch; Node *n, *r, *a, *var, *cas, *dflt, *ch;
NodeList *l, *init; NodeList *l, *init;
if(sel->list == nil && sel->xoffset != 0) if(sel->list == nil && sel->xoffset != 0)
...@@ -112,7 +113,7 @@ walkselect(Node *sel) ...@@ -112,7 +113,7 @@ walkselect(Node *sel)
// optimization: one-case select: single op. // optimization: one-case select: single op.
// TODO(rsc): Reenable optimization once order.c can handle it. // TODO(rsc): Reenable optimization once order.c can handle it.
// golang.org/issue/7672. // golang.org/issue/7672.
if(0 && i == 1) { if(i == 1) {
cas = sel->list->n; cas = sel->list->n;
setlineno(cas); setlineno(cas);
l = cas->ninit; l = cas->ninit;
...@@ -125,32 +126,34 @@ walkselect(Node *sel) ...@@ -125,32 +126,34 @@ walkselect(Node *sel)
fatal("select %O", n->op); fatal("select %O", n->op);
case OSEND: case OSEND:
ch = cheapexpr(n->left, &l); // ok already
n->left = ch; ch = n->left;
break; break;
case OSELRECV: case OSELRECV:
r = n->right; ch = n->right->left;
ch = cheapexpr(r->left, &l); Selrecv1:
r->left = ch;
if(n->left == N) if(n->left == N)
n = r; n = n->right;
else { else
n = nod(OAS, n->left, r); n->op = OAS;
typecheck(&n, Etop);
}
break; break;
case OSELRECV2: case OSELRECV2:
r = n->right; ch = n->right->left;
ch = cheapexpr(r->left, &l); if(n->ntest == N)
r->left = ch; goto Selrecv1;
if(n->left == N) {
a = nod(OAS2, N, N); typecheck(&nblank, Erv | Easgn);
a->list = n->list; n->left = nblank;
a->rlist = list1(n->right); }
n = a; n->op = OAS2;
n->list = list(list1(n->left), n->ntest);
n->rlist = list1(n->right);
n->right = N;
n->left = N;
n->ntest = N;
n->typecheck = 0;
typecheck(&n, Etop); typecheck(&n, Etop);
break; break;
} }
...@@ -168,7 +171,7 @@ walkselect(Node *sel) ...@@ -168,7 +171,7 @@ walkselect(Node *sel)
goto out; goto out;
} }
// introduce temporary variables for OSELRECV where needed. // convert case value arguments to addresses.
// this rewrite is used by both the general code and the next optimization. // this rewrite is used by both the general code and the next optimization.
for(l=sel->list; l; l=l->next) { for(l=sel->list; l; l=l->next) {
cas = l->n; cas = l->n;
...@@ -177,75 +180,24 @@ walkselect(Node *sel) ...@@ -177,75 +180,24 @@ walkselect(Node *sel)
if(n == N) if(n == N)
continue; continue;
switch(n->op) { switch(n->op) {
case OSEND:
n->right = nod(OADDR, n->right, N);
typecheck(&n->right, Erv);
break;
case OSELRECV: case OSELRECV:
case OSELRECV2: case OSELRECV2:
ch = n->right->left; if(n->op == OSELRECV2 && n->ntest == N)
n->op = OSELRECV;
// If we can use the address of the target without
// violating addressability or order of operations, do so.
// Otherwise introduce a temporary.
// Also introduce a temporary for := variables that escape,
// so that we can delay the heap allocation until the case
// is selected.
if(n->op == OSELRECV2) { if(n->op == OSELRECV2) {
if(n->ntest == N || isblank(n->ntest)) n->ntest = nod(OADDR, n->ntest, N);
n->ntest = nodnil(); typecheck(&n->ntest, Erv);
else if(n->ntest->op == ONAME &&
(!n->colas || (n->ntest->class&PHEAP) == 0) &&
convertop(types[TBOOL], n->ntest->type, nil) == OCONVNOP) {
n->ntest = nod(OADDR, n->ntest, N);
n->ntest->etype = 1; // pointer does not escape
typecheck(&n->ntest, Erv);
} else {
tmp = temp(types[TBOOL]);
a = nod(OADDR, tmp, N);
a->etype = 1; // pointer does not escape
typecheck(&a, Erv);
r = nod(OAS, n->ntest, tmp);
typecheck(&r, Etop);
cas->nbody = concat(list1(r), cas->nbody);
n->ntest = a;
}
} }
if(n->left == N)
if(n->left == N || isblank(n->left))
n->left = nodnil(); n->left = nodnil();
else if(n->left->op == ONAME && else {
(!n->colas || (n->left->class&PHEAP) == 0) &&
convertop(ch->type->type, n->left->type, nil) == OCONVNOP) {
if(n->colas && haspointers(ch->type->type)) {
r = nod(OAS, n->left, N);
typecheck(&r, Etop);
sel->ninit = concat(sel->ninit, list1(r));
}
n->left = nod(OADDR, n->left, N); n->left = nod(OADDR, n->left, N);
n->left->etype = 1; // pointer does not escape
typecheck(&n->left, Erv); typecheck(&n->left, Erv);
if(!eqtype(ch->type->type, n->left->type->type)) { }
n->left = nod(OCONVNOP, n->left, N);
n->left->type = ptrto(ch->type->type);
n->left->typecheck = 1;
}
} else {
tmp = temp(ch->type->type);
a = nod(OADDR, tmp, N);
if(haspointers(ch->type->type)) {
// clear tmp for garbage collector, because the recv
// must execute with tmp appearing to be live.
r = nod(OAS, tmp, N);
typecheck(&r, Etop);
sel->ninit = concat(sel->ninit, list1(r));
}
a->etype = 1; // pointer does not escape
typecheck(&a, Erv);
r = nod(OAS, n->left, tmp);
typecheck(&r, Etop);
cas->nbody = concat(list1(r), cas->nbody);
n->left = a;
}
cas->nbody = concat(n->ninit, cas->nbody);
n->ninit = nil;
break; break;
} }
} }
...@@ -269,29 +221,17 @@ walkselect(Node *sel) ...@@ -269,29 +221,17 @@ walkselect(Node *sel)
fatal("select %O", n->op); fatal("select %O", n->op);
case OSEND: case OSEND:
// if c != nil && selectnbsend(c, v) { body } else { default body } // if selectnbsend(c, v) { body } else { default body }
ch = cheapexpr(n->left, &r->ninit); ch = n->left;
a = n->right;
a = assignconv(a, ch->type->type, "select chan send");
walkexpr(&a, &r->ninit);
if(islvalue(a)) {
a = nod(OADDR, a, N);
} else {
var = temp(a->type);
tmp = nod(OAS, var, a);
typecheck(&tmp, Etop);
r->ninit = list(r->ninit, tmp);
a = nod(OADDR, var, N);
}
r->ntest = mkcall1(chanfn("selectnbsend", 2, ch->type), r->ntest = mkcall1(chanfn("selectnbsend", 2, ch->type),
types[TBOOL], &r->ninit, typename(ch->type), ch, a); types[TBOOL], &r->ninit, typename(ch->type), ch, n->right);
break; break;
case OSELRECV: case OSELRECV:
// if c != nil && selectnbrecv(&v, c) { body } else { default body } // if c != nil && selectnbrecv(&v, c) { body } else { default body }
r = nod(OIF, N, N); r = nod(OIF, N, N);
r->ninit = cas->ninit; r->ninit = cas->ninit;
ch = cheapexpr(n->right->left, &r->ninit); ch = n->right->left;
r->ntest = mkcall1(chanfn("selectnbrecv", 2, ch->type), r->ntest = mkcall1(chanfn("selectnbrecv", 2, ch->type),
types[TBOOL], &r->ninit, typename(ch->type), n->left, ch); types[TBOOL], &r->ninit, typename(ch->type), n->left, ch);
break; break;
...@@ -300,7 +240,7 @@ walkselect(Node *sel) ...@@ -300,7 +240,7 @@ walkselect(Node *sel)
// if c != nil && selectnbrecv2(&v, c) { body } else { default body } // if c != nil && selectnbrecv2(&v, c) { body } else { default body }
r = nod(OIF, N, N); r = nod(OIF, N, N);
r->ninit = cas->ninit; r->ninit = cas->ninit;
ch = cheapexpr(n->right->left, &r->ninit); ch = n->right->left;
r->ntest = mkcall1(chanfn("selectnbrecv2", 2, ch->type), r->ntest = mkcall1(chanfn("selectnbrecv2", 2, ch->type),
types[TBOOL], &r->ninit, typename(ch->type), n->left, n->ntest, ch); types[TBOOL], &r->ninit, typename(ch->type), n->left, n->ntest, ch);
break; break;
...@@ -344,18 +284,6 @@ walkselect(Node *sel) ...@@ -344,18 +284,6 @@ walkselect(Node *sel)
case OSEND: case OSEND:
// selectsend(sel *byte, hchan *chan any, elem *any) (selected bool); // selectsend(sel *byte, hchan *chan any, elem *any) (selected bool);
n->left = localexpr(safeexpr(n->left, &r->ninit), n->left->type, &r->ninit);
n->right = localexpr(n->right, n->left->type->type, &r->ninit);
n->right = nod(OADDR, n->right, N);
n->right->etype = 1; // pointer does not escape
typecheck(&n->right, Erv);
// cast to appropriate type if necessary.
if(!eqtype(n->right->type->type, n->left->type->type) &&
assignop(n->right->type->type, n->left->type->type, nil) == OCONVNOP) {
n->right = nod(OCONVNOP, n->right, N);
n->right->type = ptrto(n->left->type->type);
n->right->typecheck = 1;
}
r->ntest = mkcall1(chanfn("selectsend", 2, n->left->type), types[TBOOL], r->ntest = mkcall1(chanfn("selectsend", 2, n->left->type), types[TBOOL],
&r->ninit, var, n->left, n->right); &r->ninit, var, n->left, n->right);
break; break;
......
...@@ -767,14 +767,15 @@ slicelit(int ctxt, Node *n, Node *var, NodeList **init) ...@@ -767,14 +767,15 @@ slicelit(int ctxt, Node *n, Node *var, NodeList **init)
vauto = temp(ptrto(t)); vauto = temp(ptrto(t));
// set auto to point at new temp or heap (3 assign) // set auto to point at new temp or heap (3 assign)
if(n->left != N) { if(n->alloc != N) {
// temp allocated during order.c for dddarg // temp allocated during order.c for dddarg
n->alloc->type = t;
if(vstat == N) { if(vstat == N) {
a = nod(OAS, n->left, N); a = nod(OAS, n->alloc, N);
typecheck(&a, Etop); typecheck(&a, Etop);
*init = list(*init, a); // zero new temp *init = list(*init, a); // zero new temp
} }
a = nod(OADDR, n->left, N); a = nod(OADDR, n->alloc, N);
} else if(n->esc == EscNone) { } else if(n->esc == EscNone) {
a = temp(t); a = temp(t);
if(vstat == N) { if(vstat == N) {
......
...@@ -2101,7 +2101,7 @@ cheapexpr(Node *n, NodeList **init) ...@@ -2101,7 +2101,7 @@ cheapexpr(Node *n, NodeList **init)
Node* Node*
localexpr(Node *n, Type *t, NodeList **init) localexpr(Node *n, Type *t, NodeList **init)
{ {
if(n->op == ONAME && !n->addrtaken && if(n->op == ONAME && (!n->addrtaken || strncmp(n->sym->name, "autotmp_", 8) == 0) &&
(n->class == PAUTO || n->class == PPARAM || n->class == PPARAMOUT) && (n->class == PAUTO || n->class == PPARAM || n->class == PPARAMOUT) &&
convertop(n->type, t, nil) == OCONVNOP) convertop(n->type, t, nil) == OCONVNOP)
return n; return n;
......
...@@ -912,15 +912,15 @@ walkexpr(Node **np, NodeList **init) ...@@ -912,15 +912,15 @@ walkexpr(Node **np, NodeList **init)
ll = list(ll, n->left); ll = list(ll, n->left);
} else { } else {
// regular types are passed by reference to avoid C vararg calls // regular types are passed by reference to avoid C vararg calls
if(islvalue(n->left)) { // orderexpr arranged for n->left to be a temporary for all
// the conversions it could see. comparison of an interface
// with a non-interface, especially in a switch on interface value
// with non-interface cases, is not visible to orderstmt, so we
// have to fall back on allocating a temp here.
if(islvalue(n->left))
ll = list(ll, nod(OADDR, n->left, N)); ll = list(ll, nod(OADDR, n->left, N));
} else { else
var = temp(n->left->type); ll = list(ll, nod(OADDR, copyexpr(n->left, n->left->type, init), N));
n1 = nod(OAS, var, n->left);
typecheck(&n1, Etop);
*init = list(*init, n1);
ll = list(ll, nod(OADDR, var, N));
}
} }
argtype(fn, n->left->type); argtype(fn, n->left->type);
argtype(fn, n->type); argtype(fn, n->type);
...@@ -1556,7 +1556,7 @@ mkdotargslice(NodeList *lr0, NodeList *nn, Type *l, int fp, NodeList **init, Nod ...@@ -1556,7 +1556,7 @@ mkdotargslice(NodeList *lr0, NodeList *nn, Type *l, int fp, NodeList **init, Nod
} else { } else {
n = nod(OCOMPLIT, N, typenod(tslice)); n = nod(OCOMPLIT, N, typenod(tslice));
if(ddd != nil) if(ddd != nil)
n->left = ddd->left; // temporary to use n->alloc = ddd->alloc; // temporary to use
n->list = lr0; n->list = lr0;
n->esc = esc; n->esc = esc;
typecheck(&n, Erv); typecheck(&n, Erv);
...@@ -2533,7 +2533,7 @@ addstr(Node *n, NodeList **init) ...@@ -2533,7 +2533,7 @@ addstr(Node *n, NodeList **init)
t->type = types[TSTRING]; t->type = types[TSTRING];
t->bound = -1; t->bound = -1;
slice = nod(OCOMPLIT, N, typenod(t)); slice = nod(OCOMPLIT, N, typenod(t));
slice->left = n->left; slice->alloc = n->alloc;
slice->list = args; slice->list = args;
slice->esc = EscNone; slice->esc = EscNone;
args = list1(slice); args = list1(slice);
......
...@@ -423,3 +423,117 @@ func f30(b bool) { ...@@ -423,3 +423,117 @@ func f30(b bool) {
print(p) // ERROR "live at call to printpointer: autotmp_[0-9]+ autotmp_[0-9]+$" print(p) // ERROR "live at call to printpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
} }
} }
// conversion to interface should not leave temporary behind
func f31(b1, b2, b3 bool) {
if b1 {
g31("a") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to g31: autotmp_[0-9]+$"
}
if b2 {
h31("b") // ERROR "live at call to new: autotmp_[0-9]+$" "live at call to convT2E: autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to h31: autotmp_[0-9]+$"
}
if b3 {
panic("asdf") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to panic: autotmp_[0-9]+$"
}
print(b3)
}
func g31(interface{})
func h31(...interface{})
// non-escaping partial functions passed to function call should die on return
type T32 int
func (t *T32) Inc() { // ERROR "live at entry"
*t++
}
var t32 T32
func f32(b bool) {
if b {
call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
}
call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
}
//go:noescape
func call32(func())
// temporaries introduced during if conditions and && || expressions
// should die once the condition has been acted upon.
var m33 map[interface{}]int
func f33() {
if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println()
return
} else {
println()
}
println()
}
func f34() {
if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println()
return
}
println()
}
func f35() {
if m33[nil] == 0 && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println()
return
}
println()
}
func f36() {
if m33[nil] == 0 || m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println()
return
}
println()
}
func f37() {
if (m33[nil] == 0 || m33[nil] == 0) && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println()
return
}
println()
}
// select temps should disappear in the case bodies
var c38 chan string
func fc38() chan string
func fi38(int) *string
func fb38() *bool
func f38(b bool) {
// we don't care what temps are printed on the lines with output.
// we care that the println lines have no live variables
// and therefore no output.
if b {
select { // ERROR "live at call"
case <-fc38(): // ERROR "live at call"
println()
case fc38() <- *fi38(1): // ERROR "live at call"
println()
case *fi38(2) = <-fc38(): // ERROR "live at call"
println()
case *fi38(3), *fb38() = <-fc38(): // ERROR "live at call"
println()
}
println()
}
println()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment