2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
2016-03-10 18:23:03 -08:00
|
|
|
// unsafenmagic rewrites calls to package unsafe's functions into constants.
|
2015-02-13 14:40:36 -05:00
|
|
|
func unsafenmagic(nn *Node) *Node {
|
2015-02-23 16:07:24 -05:00
|
|
|
fn := nn.Left
|
|
|
|
|
args := nn.List
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-04-13 18:37:18 -07:00
|
|
|
if safemode || fn == nil || fn.Op != ONAME {
|
2015-03-02 12:35:15 -05:00
|
|
|
return nil
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
s := fn.Sym
|
2015-02-13 14:40:36 -05:00
|
|
|
if s == nil {
|
2015-03-02 12:35:15 -05:00
|
|
|
return nil
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if s.Pkg != unsafepkg {
|
2015-03-02 12:35:15 -05:00
|
|
|
return nil
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
if args.Len() == 0 {
|
2015-04-17 12:03:22 -04:00
|
|
|
Yyerror("missing argument for %v", s)
|
2015-03-02 12:35:15 -05:00
|
|
|
return nil
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
r := args.First()
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
var v int64
|
2016-03-10 18:23:03 -08:00
|
|
|
switch s.Name {
|
|
|
|
|
case "Alignof", "Sizeof":
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
r = typecheck(r, Erv)
|
|
|
|
|
r = defaultlit(r, nil)
|
2015-02-23 16:07:24 -05:00
|
|
|
tr := r.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
if tr == nil {
|
|
|
|
|
goto bad
|
|
|
|
|
}
|
|
|
|
|
dowidth(tr)
|
2016-03-10 18:23:03 -08:00
|
|
|
if s.Name == "Alignof" {
|
|
|
|
|
v = int64(tr.Align)
|
|
|
|
|
} else {
|
|
|
|
|
v = tr.Width
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-10 18:23:03 -08:00
|
|
|
case "Offsetof":
|
2015-02-13 14:40:36 -05:00
|
|
|
// must be a selector.
|
|
|
|
|
if r.Op != OXDOT {
|
|
|
|
|
goto bad
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remember base of selector to find it back after dot insertion.
|
|
|
|
|
// Since r->left may be mutated by typechecking, check it explicitly
|
|
|
|
|
// first to track it correctly.
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
r.Left = typecheck(r.Left, Erv)
|
2015-02-23 16:07:24 -05:00
|
|
|
base := r.Left
|
2016-03-10 18:23:03 -08:00
|
|
|
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
r = typecheck(r, Erv)
|
2015-02-13 14:40:36 -05:00
|
|
|
switch r.Op {
|
2015-04-01 09:38:44 -07:00
|
|
|
case ODOT, ODOTPTR:
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
case OCALLPART:
|
2015-04-17 12:03:22 -04:00
|
|
|
Yyerror("invalid expression %v: argument is a method value", nn)
|
2015-02-13 14:40:36 -05:00
|
|
|
goto ret
|
|
|
|
|
default:
|
|
|
|
|
goto bad
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 18:23:03 -08:00
|
|
|
// Sum offsets for dots until we reach base.
|
|
|
|
|
for r1 := r; r1 != base; r1 = r1.Left {
|
2015-02-13 14:40:36 -05:00
|
|
|
switch r1.Op {
|
2016-03-10 18:23:03 -08:00
|
|
|
case ODOTPTR:
|
|
|
|
|
// For Offsetof(s.f), s may itself be a pointer,
|
|
|
|
|
// but accessing f must not otherwise involve
|
|
|
|
|
// indirection via embedded pointer types.
|
|
|
|
|
if r1.Left != base {
|
|
|
|
|
Yyerror("invalid expression %v: selector implies indirection of embedded %v", nn, r1.Left)
|
|
|
|
|
goto ret
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
2015-02-13 14:40:36 -05:00
|
|
|
case ODOT:
|
|
|
|
|
v += r1.Xoffset
|
|
|
|
|
default:
|
|
|
|
|
Dump("unsafenmagic", r)
|
2016-04-27 14:46:09 +10:00
|
|
|
Fatalf("impossible %v node after dot insertion", oconv(r1.Op, FmtSharp))
|
2015-02-13 14:40:36 -05:00
|
|
|
goto bad
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 18:23:03 -08:00
|
|
|
default:
|
|
|
|
|
return nil
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 18:23:03 -08:00
|
|
|
if args.Len() > 1 {
|
|
|
|
|
Yyerror("extra arguments for %v", s)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-10 18:23:03 -08:00
|
|
|
goto ret
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
bad:
|
2015-04-17 12:03:22 -04:00
|
|
|
Yyerror("invalid expression %v", nn)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
ret:
|
2016-03-10 18:23:03 -08:00
|
|
|
// any side effects disappear; ignore init
|
2015-02-23 16:07:24 -05:00
|
|
|
var val Val
|
2015-05-14 17:57:42 -07:00
|
|
|
val.U = new(Mpint)
|
2016-03-20 13:55:42 -07:00
|
|
|
val.U.(*Mpint).SetInt64(v)
|
2015-02-23 16:07:24 -05:00
|
|
|
n := Nod(OLITERAL, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
n.Orig = nn
|
2015-05-27 00:47:05 -04:00
|
|
|
n.SetVal(val)
|
2015-02-13 14:40:36 -05:00
|
|
|
n.Type = Types[TUINTPTR]
|
|
|
|
|
nn.Type = Types[TUINTPTR]
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
func isunsafebuiltin(n *Node) bool {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n == nil || n.Op != ONAME || n.Sym == nil || n.Sym.Pkg != unsafepkg {
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if n.Sym.Name == "Sizeof" {
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if n.Sym.Name == "Offsetof" {
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if n.Sym.Name == "Alignof" {
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|