]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: alias more variables during register allocation
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 12 May 2014 21:10:36 +0000 (17:10 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 12 May 2014 21:10:36 +0000 (17:10 -0400)
This is joint work with Daniel Morsing.

In order for the register allocator to alias two variables, they must have the same width, stack offset, and etype. Code generation was altering a variable's etype in a few places. This prevented the variable from being moved to a register, which in turn prevented peephole optimization. This failure to alias was very common, with almost 23,000 instances just running make.bash.

This phenomenon was not visible in the register allocation debug output because the variables that failed to alias had the same name. The debugging-only change to bits.c fixes this by printing the variable number with its name.

This CL fixes the source of all etype mismatches for 6g, all but one case for 8g, and depressingly few cases for 5g. (I believe that extending CL 6819083 to 5g is a prerequisite.) Fixing the remaining cases in 8g and 5g is work for the future.

The etype mismatch fixes are:

* [gc] Slicing changed the type of the base pointer into a uintptr in order to perform arithmetic on it. Instead, support addition directly on pointers.

* [*g] OSPTR was giving type uintptr to slice base pointers; undo that. This arose, for example, while compiling copy(dst, src).

* [8g] 64 bit float conversion was assigning int64 type during codegen, overwriting the existing uint64 type.

Note that some etype mismatches are appropriate, such as a struct with a single field or an array with a single element.

With these fixes, the number of registerizations that occur while running make.bash for 6g increases ~10%. Hello world binary size shrinks ~1.5%. Running all benchmarks in the standard library show performance improvements ranging from nominal to substantive (>10%); a full comparison using 6g on my laptop is available at https://gist.github.com/josharian/8f9b5beb46667c272064. The microbenchmarks must be taken with a grain of salt; see issue 7920. The few benchmarks that show real regressions are likely due to issue 7920. I manually examined the generated code for the top few regressions and none had any assembly output changes. The few benchmarks that show extraordinary improvements are likely also due to issue 7920.

Performance results from 8g appear similar to 6g.

5g shows no performance improvements. This is not surprising, given the discussion above.

Update #7316

LGTM=rsc
R=rsc, daniel.morsing, bradfitz
CC=dave, golang-codereviews
https://golang.org/cl/91850043

src/cmd/5g/cgen.c
src/cmd/5g/gsubr.c
src/cmd/6g/cgen.c
src/cmd/6g/gsubr.c
src/cmd/8g/cgen.c
src/cmd/8g/gsubr.c
src/cmd/gc/bits.c
src/cmd/gc/gen.c
src/cmd/gc/go.h
src/cmd/gc/typecheck.c
test/fixedbugs/issue7316.go [new file with mode: 0644]

index 57e4e393663029351794c22c43db099b51635d1f..9faf75461751acd6dd0244b9559acfcb8d3db038 100644 (file)
@@ -254,6 +254,7 @@ cgen(Node *n, Node *res)
        case OOR:
        case OXOR:
        case OADD:
+       case OADDPTR:
        case OMUL:
                a = optoas(n->op, nl->type);
                goto sbop;
index 72c880cf7de85bbdd15930c6d3537ac7b960f872..528e8f8cc3133a4cc92b3c3a034a4a441e738094 100644 (file)
@@ -1366,7 +1366,7 @@ naddr(Node *n, Addr *a, int canemitcode)
                naddr(n->left, a, canemitcode);
                if(a->type == D_CONST && a->offset == 0)
                        break;  // ptr(nil)
-               a->etype = simtype[TUINTPTR];
+               a->etype = simtype[tptr];
                a->offset += Array_array;
                a->width = widthptr;
                break;
@@ -1592,6 +1592,7 @@ optoas(int op, Type *t)
        case CASE(OADD, TINT32):
        case CASE(OADD, TUINT32):
        case CASE(OADD, TPTR32):
+       case CASE(OADDPTR, TPTR32):
                a = AADD;
                break;
 
index eb45b29ea18319368684e28686f18ea1deb40d05..ae1309142c9f778844f1e1f0f87fb3c8c450a3d4 100644 (file)
@@ -247,6 +247,7 @@ cgen(Node *n, Node *res)
        case OOR:
        case OXOR:
        case OADD:
+       case OADDPTR:
        case OMUL:
                a = optoas(n->op, nl->type);
                if(a == AIMULB) {
index 14cefc35a01eb42cfabeae8105f447be8f08586f..bd2f2304b455fa21c720dc7555b9a1e9ef3371f7 100644 (file)
@@ -1300,7 +1300,7 @@ naddr(Node *n, Addr *a, int canemitcode)
                naddr(n->left, a, canemitcode);
                if(a->type == D_CONST && a->offset == 0)
                        break;  // ptr(nil)
-               a->etype = simtype[TUINTPTR];
+               a->etype = simtype[tptr];
                a->offset += Array_array;
                a->width = widthptr;
                break;
@@ -1533,12 +1533,14 @@ optoas(int op, Type *t)
        case CASE(OADD, TINT32):
        case CASE(OADD, TUINT32):
        case CASE(OADD, TPTR32):
+       case CASE(OADDPTR, TPTR32):
                a = AADDL;
                break;
 
        case CASE(OADD, TINT64):
        case CASE(OADD, TUINT64):
        case CASE(OADD, TPTR64):
+       case CASE(OADDPTR, TPTR64):
                a = AADDQ;
                break;
 
index 042997a8beb5a0ea8ebfa90b7d080acc384809e8..1aae7771c7110122fc07ca3698556fbf179272a7 100644 (file)
@@ -242,6 +242,7 @@ cgen(Node *n, Node *res)
        case OOR:
        case OXOR:
        case OADD:
+       case OADDPTR:
        case OMUL:
                a = optoas(n->op, nl->type);
                if(a == AIMULB) {
index 60c74c60ecc881ecd8b1f0200eaab367bea1f3de..e83ae5d7a5202f268db7294c4e908d43348b5fc2 100644 (file)
@@ -432,6 +432,7 @@ optoas(int op, Type *t)
        case CASE(OADD, TINT32):
        case CASE(OADD, TUINT32):
        case CASE(OADD, TPTR32):
+       case CASE(OADDPTR, TPTR32):
                a = AADDL;
                break;
 
@@ -1687,7 +1688,6 @@ floatmove(Node *f, Node *t)
                gins(ACMPL, &thi, ncon(0));
                p1 = gbranch(AJLT, T, 0);
                // native
-               t1.type = types[TINT64];
                nodreg(&r1, types[tt], D_F0);
                gins(AFMOVV, &t1, &r1);
                if(tt == TFLOAT32)
@@ -2327,7 +2327,7 @@ naddr(Node *n, Addr *a, int canemitcode)
                naddr(n->left, a, canemitcode);
                if(a->type == D_CONST && a->offset == 0)
                        break;  // ptr(nil)
-               a->etype = simtype[TUINTPTR];
+               a->etype = simtype[tptr];
                a->offset += Array_array;
                a->width = widthptr;
                break;
index c0fd4d85e6629fa8c2dccb2dfcec40f12b7d2142..2e79f6f1de3f6b467cadd22961bb9125db57ce21 100644 (file)
@@ -153,7 +153,7 @@ Qconv(Fmt *fp)
                if(var[i].node == N || var[i].node->sym == S)
                        fmtprint(fp, "$%d", i);
                else {
-                       fmtprint(fp, "%s", var[i].node->sym->name);
+                       fmtprint(fp, "%s(%d)", var[i].node->sym->name, i);
                        if(var[i].offset != 0)
                                fmtprint(fp, "%+lld", (vlong)var[i].offset);
                }
index 17c0a7082dceeab6c4ac3f1e7cefc68efae39497..cf630f34843186ad834dcb390b738b9039e34afb 100644 (file)
@@ -829,7 +829,6 @@ cgen_slice(Node *n, Node *res)
                src = *n->left;
        if(n->op == OSLICE || n->op == OSLICE3 || n->op == OSLICESTR)
                src.xoffset += Array_array;
-       src.type = types[TUINTPTR];
 
        if(n->op == OSLICEARR || n->op == OSLICE3ARR) {
                if(!isptr[n->left->type->etype])
@@ -842,9 +841,11 @@ cgen_slice(Node *n, Node *res)
                        cgen(add, base);
                }
        } else if(offs == N) {
+               src.type = types[tptr];
                cgen(&src, base);
        } else {
-               add = nod(OADD, &src, offs);
+               src.type = types[tptr];
+               add = nod(OADDPTR, &src, offs);
                typecheck(&add, Erv);
                cgen(add, base);
        }
@@ -855,7 +856,7 @@ cgen_slice(Node *n, Node *res)
        // dst.array = src.array  [ + lo *width ]
        dst = *res;
        dst.xoffset += Array_array;
-       dst.type = types[TUINTPTR];
+       dst.type = types[tptr];
        
        cgen(base, &dst);
 
index 125ae9cf4401247e9a64ef7607536ef720c069f2..44e3ceda0da1e612de225f38f52b6a8d3e12abf0 100644 (file)
@@ -445,6 +445,7 @@ enum
        OSUB,   // x - y
        OOR,    // x | y
        OXOR,   // x ^ y
+       OADDPTR,        // ptr + uintptr, inserted by compiler only, used to avoid unsafe type changes during codegen
        OADDSTR,        // s + "foo"
        OADDR,  // &x
        OANDAND,        // b0 && b1
index d7a263722440e9af3be22098404477956483c579..b51fc3892a88f7501f56c4f2cc9c67bc323d5d0f 100644 (file)
@@ -535,6 +535,19 @@ reswitch:
                op = n->etype;
                goto arith;
 
+       case OADDPTR:
+               ok |= Erv;
+               l = typecheck(&n->left, Erv);
+               r = typecheck(&n->right, Erv);
+               if(l->type == T || r->type == T)
+                       goto error;
+               if(l->type->etype != tptr)
+                       fatal("bad OADDPTR left type %E for %N", l->type->etype, n->left);
+               if(r->type->etype != TUINTPTR)
+                       fatal("bad OADDPTR right type %E for %N", r->type->etype, n->right);
+               n->type = types[tptr];
+               goto ret;
+
        case OADD:
        case OAND:
        case OANDAND:
diff --git a/test/fixedbugs/issue7316.go b/test/fixedbugs/issue7316.go
new file mode 100644 (file)
index 0000000..4b32261
--- /dev/null
@@ -0,0 +1,37 @@
+// runoutput
+
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 7316
+// This test exercises all types of numeric conversions, which was one
+// of the sources of etype mismatch during register allocation in 8g.
+
+package main
+
+import "fmt"
+
+const tpl = `
+func init() {
+       var i %s
+       j := %s(i)
+       _ = %s(j)
+}
+`
+
+func main() {
+       fmt.Println("package main")
+       ntypes := []string{
+               "byte", "rune", "uintptr",
+               "float32", "float64",
+               "int", "int8", "int16", "int32", "int64",
+               "uint", "uint8", "uint16", "uint32", "uint64",
+       }
+       for i, from := range ntypes {
+               for _, to := range ntypes[i:] {
+                       fmt.Printf(tpl, from, to, from)
+               }
+       }
+       fmt.Println("func main() {}")
+}