Commit aa0b573a authored by Alan Donovan's avatar Alan Donovan

exp/ssa: make Parameters values, not addresses.

We explicitly spill all parameters to the frame during initial
SSA construction.  (Later passes will remove spills.)
We now properly handle local Allocs escaping via Captures.

Also: allocate BasicBlock.Succs inline.

R=iant, iant
CC=golang-dev
https://golang.org/cl/7231050
parent 81221f51
...@@ -5,10 +5,6 @@ package ssa ...@@ -5,10 +5,6 @@ package ssa
// TODO(adonovan): instead of creating several "unreachable" blocks // TODO(adonovan): instead of creating several "unreachable" blocks
// per function in the Builder, reuse a single one (e.g. at Blocks[1]) // per function in the Builder, reuse a single one (e.g. at Blocks[1])
// to reduce garbage. // to reduce garbage.
//
// TODO(adonovan): in the absence of multiway branch instructions,
// each BasicBlock has 0, 1, or 2 successors. We should preallocate
// the backing array for the Succs slice inline in BasicBlock.
import ( import (
"fmt" "fmt"
...@@ -117,7 +113,7 @@ func fuseBlocks(f *Function, a *BasicBlock) bool { ...@@ -117,7 +113,7 @@ func fuseBlocks(f *Function, a *BasicBlock) bool {
} }
// A inherits B's successors // A inherits B's successors
a.Succs = b.Succs a.Succs = append(a.succs2[:0], b.Succs...)
// Fix up Preds links of all successors of B. // Fix up Preds links of all successors of B.
for _, c := range b.Succs { for _, c := range b.Succs {
......
...@@ -151,16 +151,27 @@ func (f *Function) labelledBlock(label *ast.Ident) *lblock { ...@@ -151,16 +151,27 @@ func (f *Function) labelledBlock(label *ast.Ident) *lblock {
func (f *Function) addParam(name string, typ types.Type) *Parameter { func (f *Function) addParam(name string, typ types.Type) *Parameter {
v := &Parameter{ v := &Parameter{
Name_: name, Name_: name,
Type_: pointer(typ), // address of param Type_: typ,
} }
f.Params = append(f.Params, v) f.Params = append(f.Params, v)
return v return v
} }
func (f *Function) addObjParam(obj types.Object) *Parameter { // addSpilledParam declares a parameter that is pre-spilled to the
p := f.addParam(obj.GetName(), obj.GetType()) // stack; the function body will load/store the spilled location.
f.objects[obj] = p // Subsequent registerization will eliminate spills where possible.
return p //
func (f *Function) addSpilledParam(obj types.Object) {
name := obj.GetName()
param := f.addParam(name, obj.GetType())
spill := &Alloc{
Name_: name + "~", // "~" means "spilled"
Type_: pointer(obj.GetType()),
}
f.objects[obj] = spill
f.Locals = append(f.Locals, spill)
f.emit(spill)
f.emit(&Store{Addr: spill, Val: param})
} }
// start initializes the function prior to generating SSA code for its body. // start initializes the function prior to generating SSA code for its body.
...@@ -186,7 +197,7 @@ func (f *Function) start(mode BuilderMode, idents map[*ast.Ident]types.Object) { ...@@ -186,7 +197,7 @@ func (f *Function) start(mode BuilderMode, idents map[*ast.Ident]types.Object) {
if f.syntax.recvField != nil { if f.syntax.recvField != nil {
for _, field := range f.syntax.recvField.List { for _, field := range f.syntax.recvField.List {
for _, n := range field.Names { for _, n := range field.Names {
f.addObjParam(idents[n]) f.addSpilledParam(idents[n])
} }
if field.Names == nil { if field.Names == nil {
f.addParam(f.Signature.Recv.Name, f.Signature.Recv.Type) f.addParam(f.Signature.Recv.Name, f.Signature.Recv.Type)
...@@ -198,7 +209,7 @@ func (f *Function) start(mode BuilderMode, idents map[*ast.Ident]types.Object) { ...@@ -198,7 +209,7 @@ func (f *Function) start(mode BuilderMode, idents map[*ast.Ident]types.Object) {
if f.syntax.paramFields != nil { if f.syntax.paramFields != nil {
for _, field := range f.syntax.paramFields.List { for _, field := range f.syntax.paramFields.List {
for _, n := range field.Names { for _, n := range field.Names {
f.addObjParam(idents[n]) f.addSpilledParam(idents[n])
} }
} }
} }
...@@ -300,18 +311,18 @@ func (f *Function) addLocal(typ types.Type) *Alloc { ...@@ -300,18 +311,18 @@ func (f *Function) addLocal(typ types.Type) *Alloc {
func (f *Function) lookup(obj types.Object, escaping bool) Value { func (f *Function) lookup(obj types.Object, escaping bool) Value {
if v, ok := f.objects[obj]; ok { if v, ok := f.objects[obj]; ok {
if escaping { if escaping {
switch v := v.(type) { // Walk up the chain of Captures.
case *Capture: x := v
// TODO(adonovan): fix: we must support this case. for {
// Requires copying to a 'new' Alloc. if c, ok := x.(*Capture); ok {
fmt.Fprintln(os.Stderr, "Error: escaping reference to Capture") x = c.Outer
case *Parameter: } else {
v.Heap = true break
case *Alloc: }
v.Heap = true
default:
panic(fmt.Sprintf("Unexpected Function.objects kind: %T", v))
} }
// By construction, all captures are ultimately Allocs in the
// naive SSA form. Parameters are pre-spilled to the stack.
x.(*Alloc).Heap = true
} }
return v // function-local var (address) return v // function-local var (address)
} }
...@@ -340,7 +351,7 @@ func (f *Function) emit(instr Instruction) Value { ...@@ -340,7 +351,7 @@ func (f *Function) emit(instr Instruction) Value {
func (f *Function) DumpTo(w io.Writer) { func (f *Function) DumpTo(w io.Writer) {
fmt.Fprintf(w, "# Name: %s\n", f.FullName()) fmt.Fprintf(w, "# Name: %s\n", f.FullName())
fmt.Fprintf(w, "# Declared at %s\n", f.Prog.Files.Position(f.Pos)) fmt.Fprintf(w, "# Declared at %s\n", f.Prog.Files.Position(f.Pos))
fmt.Fprintf(w, "# Type: %s\n", f.Type()) fmt.Fprintf(w, "# Type: %s\n", f.Signature)
if f.Enclosing != nil { if f.Enclosing != nil {
fmt.Fprintf(w, "# Parent: %s\n", f.Enclosing.Name()) fmt.Fprintf(w, "# Parent: %s\n", f.Enclosing.Name())
...@@ -411,6 +422,7 @@ func (f *Function) newBasicBlock(name string) *BasicBlock { ...@@ -411,6 +422,7 @@ func (f *Function) newBasicBlock(name string) *BasicBlock {
Name: fmt.Sprintf("%d.%s", len(f.Blocks), name), Name: fmt.Sprintf("%d.%s", len(f.Blocks), name),
Func: f, Func: f,
} }
b.Succs = b.succs2[:0]
f.Blocks = append(f.Blocks, b) f.Blocks = append(f.Blocks, b)
return b return b
} }
...@@ -246,19 +246,20 @@ type Function struct { ...@@ -246,19 +246,20 @@ type Function struct {
// instructions, respectively). // instructions, respectively).
// //
type BasicBlock struct { type BasicBlock struct {
Name string // label; no semantic significance Name string // label; no semantic significance
Func *Function // containing function Func *Function // containing function
Instrs []Instruction // instructions in order Instrs []Instruction // instructions in order
Preds, Succs []*BasicBlock // predecessors and successors Preds, Succs []*BasicBlock // predecessors and successors
succs2 [2]*BasicBlock // initial space for Succs.
} }
// Pure values ---------------------------------------- // Pure values ----------------------------------------
// A Capture is a pointer to a lexically enclosing local variable. // A Capture is a pointer to a lexically enclosing local variable.
// //
// The referent of a capture is a Parameter, Alloc or another Capture // The referent of a capture is an Alloc or another Capture and is
// and is always considered potentially escaping, so Captures are // always considered potentially escaping, so Captures are always
// always addresses in the heap, and have pointer types. // addresses in the heap, and have pointer types.
// //
type Capture struct { type Capture struct {
Outer Value // the Value captured from the enclosing context. Outer Value // the Value captured from the enclosing context.
...@@ -266,22 +267,9 @@ type Capture struct { ...@@ -266,22 +267,9 @@ type Capture struct {
// A Parameter represents an input parameter of a function. // A Parameter represents an input parameter of a function.
// //
// Parameters are addresses and thus have pointer types.
// TODO(adonovan): this will change. We should just spill parameters
// to ordinary Alloc-style locals if they are ever used in an
// addressable context. Then we can lose the Heap flag.
//
// In the common case where Heap=false, Parameters are pointers into
// the function's stack frame. If the case where Heap=true because a
// parameter's address may escape from its function, Parameters are
// pointers into a space in the heap implicitly allocated during the
// function call. (See also Alloc, which uses the Heap flag in a
// similar manner.)
//
type Parameter struct { type Parameter struct {
Name_ string Name_ string
Type_ *types.Pointer Type_ types.Type
Heap bool
} }
// A Literal represents a literal nil, boolean, string or numeric // A Literal represents a literal nil, boolean, string or numeric
......
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