2013-08-27 16:49:13 -06:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
package ssa
|
|
|
|
|
|
|
|
// This file defines algorithms related to dominance.
|
|
|
|
|
|
|
|
// Dominator tree construction ----------------------------------------
|
|
|
|
//
|
|
|
|
// We use the algorithm described in Lengauer & Tarjan. 1979. A fast
|
|
|
|
// algorithm for finding dominators in a flowgraph.
|
|
|
|
// http://doi.acm.org/10.1145/357062.357071
|
|
|
|
//
|
|
|
|
// We also apply the optimizations to SLT described in Georgiadis et
|
|
|
|
// al, Finding Dominators in Practice, JGAA 2006,
|
|
|
|
// http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
|
|
|
|
// to avoid the need for buckets of size > 1.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2013-12-05 07:50:18 -07:00
|
|
|
"sort"
|
2013-05-17 14:25:48 -06:00
|
|
|
)
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
// Idom returns the block that immediately dominates b:
|
|
|
|
// its parent in the dominator tree, if any.
|
|
|
|
// Neither the entry node (b.Index==0) nor recover node
|
|
|
|
// (b==b.Parent().Recover()) have a parent.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-12-05 07:50:18 -07:00
|
|
|
func (b *BasicBlock) Idom() *BasicBlock { return b.dom.idom }
|
|
|
|
|
|
|
|
// Dominees returns the list of blocks that b immediately dominates:
|
|
|
|
// its children in the dominator tree.
|
|
|
|
//
|
|
|
|
func (b *BasicBlock) Dominees() []*BasicBlock { return b.dom.children }
|
|
|
|
|
|
|
|
// Dominates reports whether b dominates c.
|
|
|
|
func (b *BasicBlock) Dominates(c *BasicBlock) bool {
|
|
|
|
return b.dom.pre <= c.dom.pre && c.dom.post <= b.dom.post
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
type byDomPreorder []*BasicBlock
|
|
|
|
|
|
|
|
func (a byDomPreorder) Len() int { return len(a) }
|
|
|
|
func (a byDomPreorder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byDomPreorder) Less(i, j int) bool { return a[i].dom.pre < a[j].dom.pre }
|
|
|
|
|
|
|
|
// DomPreorder returns a new slice containing the blocks of f in
|
|
|
|
// dominator tree preorder.
|
|
|
|
//
|
|
|
|
func (f *Function) DomPreorder() []*BasicBlock {
|
|
|
|
n := len(f.Blocks)
|
|
|
|
order := make(byDomPreorder, n, n)
|
|
|
|
copy(order, f.Blocks)
|
|
|
|
sort.Sort(order)
|
|
|
|
return order
|
|
|
|
}
|
|
|
|
|
|
|
|
// domInfo contains a BasicBlock's dominance information.
|
|
|
|
type domInfo struct {
|
|
|
|
idom *BasicBlock // immediate dominator (parent in domtree)
|
|
|
|
children []*BasicBlock // nodes immediately dominated by this one
|
|
|
|
pre, post int32 // pre- and post-order numbering within domtree
|
|
|
|
}
|
|
|
|
|
|
|
|
// ltState holds the working state for Lengauer-Tarjan algorithm
|
|
|
|
// (during which domInfo.pre is repurposed for CFG DFS preorder number).
|
|
|
|
type ltState struct {
|
|
|
|
// Each slice is indexed by b.Index.
|
|
|
|
sdom []*BasicBlock // b's semidominator
|
|
|
|
parent []*BasicBlock // b's parent in DFS traversal of CFG
|
|
|
|
ancestor []*BasicBlock // b's ancestor with least sdom
|
|
|
|
}
|
|
|
|
|
|
|
|
// dfs implements the depth-first search part of the LT algorithm.
|
|
|
|
func (lt *ltState) dfs(v *BasicBlock, i int32, preorder []*BasicBlock) int32 {
|
2013-05-17 14:25:48 -06:00
|
|
|
preorder[i] = v
|
2013-12-05 07:50:18 -07:00
|
|
|
v.dom.pre = i // For now: DFS preorder of spanning tree of CFG
|
2013-05-17 14:25:48 -06:00
|
|
|
i++
|
2013-12-05 07:50:18 -07:00
|
|
|
lt.sdom[v.Index] = v
|
|
|
|
lt.link(nil, v)
|
|
|
|
for _, w := range v.Succs {
|
|
|
|
if lt.sdom[w.Index] == nil {
|
|
|
|
lt.parent[w.Index] = v
|
|
|
|
i = lt.dfs(w, i, preorder)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
// eval implements the EVAL part of the LT algorithm.
|
|
|
|
func (lt *ltState) eval(v *BasicBlock) *BasicBlock {
|
2013-05-17 14:25:48 -06:00
|
|
|
// TODO(adonovan): opt: do path compression per simple LT.
|
|
|
|
u := v
|
2013-12-05 07:50:18 -07:00
|
|
|
for ; lt.ancestor[v.Index] != nil; v = lt.ancestor[v.Index] {
|
|
|
|
if lt.sdom[v.Index].dom.pre < lt.sdom[u.Index].dom.pre {
|
2013-05-17 14:25:48 -06:00
|
|
|
u = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
// link implements the LINK part of the LT algorithm.
|
|
|
|
func (lt *ltState) link(v, w *BasicBlock) {
|
|
|
|
lt.ancestor[w.Index] = v
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildDomTree computes the dominator tree of f using the LT algorithm.
|
|
|
|
// Precondition: all blocks are reachable (e.g. optimizeBlocks has been run).
|
|
|
|
//
|
|
|
|
func buildDomTree(f *Function) {
|
|
|
|
// The step numbers refer to the original LT paper; the
|
|
|
|
// reodering is due to Georgiadis.
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
// Clear any previous domInfo.
|
2013-05-17 14:25:48 -06:00
|
|
|
for _, b := range f.Blocks {
|
2013-12-05 07:50:18 -07:00
|
|
|
b.dom = domInfo{}
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
n := len(f.Blocks)
|
2013-12-05 07:50:18 -07:00
|
|
|
// Allocate space for 5 contiguous [n]*BasicBlock arrays:
|
|
|
|
// sdom, parent, ancestor, preorder, buckets.
|
|
|
|
space := make([]*BasicBlock, 5*n, 5*n)
|
|
|
|
lt := ltState{
|
|
|
|
sdom: space[0:n],
|
|
|
|
parent: space[n : 2*n],
|
|
|
|
ancestor: space[2*n : 3*n],
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 1. Number vertices by depth-first preorder.
|
|
|
|
preorder := space[3*n : 4*n]
|
|
|
|
root := f.Blocks[0]
|
|
|
|
prenum := lt.dfs(root, 0, preorder)
|
|
|
|
recover := f.Recover
|
|
|
|
if recover != nil {
|
|
|
|
lt.dfs(recover, prenum, preorder)
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
buckets := space[4*n : 5*n]
|
2013-05-17 14:25:48 -06:00
|
|
|
copy(buckets, preorder)
|
|
|
|
|
|
|
|
// In reverse preorder...
|
2013-12-05 07:50:18 -07:00
|
|
|
for i := int32(n) - 1; i > 0; i-- {
|
2013-05-17 14:25:48 -06:00
|
|
|
w := preorder[i]
|
|
|
|
|
|
|
|
// Step 3. Implicitly define the immediate dominator of each node.
|
2013-12-05 07:50:18 -07:00
|
|
|
for v := buckets[i]; v != w; v = buckets[v.dom.pre] {
|
|
|
|
u := lt.eval(v)
|
|
|
|
if lt.sdom[u.Index].dom.pre < i {
|
|
|
|
v.dom.idom = u
|
2013-05-17 14:25:48 -06:00
|
|
|
} else {
|
2013-12-05 07:50:18 -07:00
|
|
|
v.dom.idom = w
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2. Compute the semidominators of all nodes.
|
2013-12-05 07:50:18 -07:00
|
|
|
lt.sdom[w.Index] = lt.parent[w.Index]
|
|
|
|
for _, v := range w.Preds {
|
|
|
|
u := lt.eval(v)
|
|
|
|
if lt.sdom[u.Index].dom.pre < lt.sdom[w.Index].dom.pre {
|
|
|
|
lt.sdom[w.Index] = lt.sdom[u.Index]
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
lt.link(lt.parent[w.Index], w)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
if lt.parent[w.Index] == lt.sdom[w.Index] {
|
|
|
|
w.dom.idom = lt.parent[w.Index]
|
2013-05-17 14:25:48 -06:00
|
|
|
} else {
|
2013-12-05 07:50:18 -07:00
|
|
|
buckets[i] = buckets[lt.sdom[w.Index].dom.pre]
|
|
|
|
buckets[lt.sdom[w.Index].dom.pre] = w
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The final 'Step 3' is now outside the loop.
|
2013-12-05 07:50:18 -07:00
|
|
|
for v := buckets[0]; v != root; v = buckets[v.dom.pre] {
|
|
|
|
v.dom.idom = root
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4. Explicitly define the immediate dominator of each
|
|
|
|
// node, in preorder.
|
|
|
|
for _, w := range preorder[1:] {
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
if w == root || w == recover {
|
2013-12-05 07:50:18 -07:00
|
|
|
w.dom.idom = nil
|
2013-05-17 14:25:48 -06:00
|
|
|
} else {
|
2013-12-05 07:50:18 -07:00
|
|
|
if w.dom.idom != lt.sdom[w.Index] {
|
|
|
|
w.dom.idom = w.dom.idom.dom.idom
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
// Calculate Children relation as inverse of Idom.
|
2013-12-05 07:50:18 -07:00
|
|
|
w.dom.idom.dom.children = append(w.dom.idom.dom.children, w)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 07:50:18 -07:00
|
|
|
pre, post := numberDomTree(root, 0, 0)
|
|
|
|
if recover != nil {
|
|
|
|
numberDomTree(recover, pre, post)
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
// printDomTreeDot(os.Stderr, f) // debugging
|
|
|
|
// printDomTreeText(os.Stderr, root, 0) // debugging
|
|
|
|
|
|
|
|
if f.Prog.mode&SanityCheckFunctions != 0 {
|
|
|
|
sanityCheckDomTree(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// numberDomTree sets the pre- and post-order numbers of a depth-first
|
|
|
|
// traversal of the dominator tree rooted at v. These are used to
|
2013-12-05 07:50:18 -07:00
|
|
|
// answer dominance queries in constant time.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-12-05 07:50:18 -07:00
|
|
|
func numberDomTree(v *BasicBlock, pre, post int32) (int32, int32) {
|
|
|
|
v.dom.pre = pre
|
2013-05-17 14:25:48 -06:00
|
|
|
pre++
|
2013-12-05 07:50:18 -07:00
|
|
|
for _, child := range v.dom.children {
|
|
|
|
pre, post = numberDomTree(child, pre, post)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-12-05 07:50:18 -07:00
|
|
|
v.dom.post = post
|
2013-05-17 14:25:48 -06:00
|
|
|
post++
|
|
|
|
return pre, post
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing utilities ----------------------------------------
|
|
|
|
|
|
|
|
// sanityCheckDomTree checks the correctness of the dominator tree
|
|
|
|
// computed by the LT algorithm by comparing against the dominance
|
|
|
|
// relation computed by a naive Kildall-style forward dataflow
|
|
|
|
// analysis (Algorithm 10.16 from the "Dragon" book).
|
|
|
|
//
|
|
|
|
func sanityCheckDomTree(f *Function) {
|
|
|
|
n := len(f.Blocks)
|
|
|
|
|
|
|
|
// D[i] is the set of blocks that dominate f.Blocks[i],
|
|
|
|
// represented as a bit-set of block indices.
|
|
|
|
D := make([]big.Int, n)
|
|
|
|
|
|
|
|
one := big.NewInt(1)
|
|
|
|
|
|
|
|
// all is the set of all blocks; constant.
|
|
|
|
var all big.Int
|
|
|
|
all.Set(one).Lsh(&all, uint(n)).Sub(&all, one)
|
|
|
|
|
|
|
|
// Initialization.
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
for i, b := range f.Blocks {
|
|
|
|
if i == 0 || b == f.Recover {
|
2013-12-05 07:50:18 -07:00
|
|
|
// A root is dominated only by itself.
|
2013-05-17 14:25:48 -06:00
|
|
|
D[i].SetBit(&D[0], 0, 1)
|
|
|
|
} else {
|
|
|
|
// All other blocks are (initially) dominated
|
|
|
|
// by every block.
|
|
|
|
D[i].Set(&all)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteration until fixed point.
|
|
|
|
for changed := true; changed; {
|
|
|
|
changed = false
|
|
|
|
for i, b := range f.Blocks {
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
if i == 0 || b == f.Recover {
|
2013-05-17 14:25:48 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Compute intersection across predecessors.
|
|
|
|
var x big.Int
|
|
|
|
x.Set(&all)
|
|
|
|
for _, pred := range b.Preds {
|
|
|
|
x.And(&x, &D[pred.Index])
|
|
|
|
}
|
|
|
|
x.SetBit(&x, i, 1) // a block always dominates itself.
|
|
|
|
if D[i].Cmp(&x) != 0 {
|
|
|
|
D[i].Set(&x)
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the entire relation. O(n^2).
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
// The Recover block (if any) must be treated specially so we skip it.
|
2013-05-17 14:25:48 -06:00
|
|
|
ok := true
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
for j := 0; j < n; j++ {
|
|
|
|
b, c := f.Blocks[i], f.Blocks[j]
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
if c == f.Recover {
|
|
|
|
continue
|
|
|
|
}
|
2013-12-05 07:50:18 -07:00
|
|
|
actual := b.Dominates(c)
|
2013-05-17 14:25:48 -06:00
|
|
|
expected := D[j].Bit(i) == 1
|
|
|
|
if actual != expected {
|
|
|
|
fmt.Fprintf(os.Stderr, "dominates(%s, %s)==%t, want %t\n", b, c, actual, expected)
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 07:50:18 -07:00
|
|
|
|
|
|
|
preorder := f.DomPreorder()
|
|
|
|
for _, b := range f.Blocks {
|
|
|
|
if got := preorder[b.dom.pre]; got != b {
|
|
|
|
fmt.Fprintf(os.Stderr, "preorder[%d]==%s, want %s\n", b.dom.pre, got, b)
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
if !ok {
|
2013-06-26 10:38:08 -06:00
|
|
|
panic("sanityCheckDomTree failed for " + f.String())
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-12-05 07:50:18 -07:00
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Printing functions ----------------------------------------
|
|
|
|
|
|
|
|
// printDomTree prints the dominator tree as text, using indentation.
|
2013-12-05 07:50:18 -07:00
|
|
|
func printDomTreeText(w io.Writer, v *BasicBlock, indent int) {
|
|
|
|
fmt.Fprintf(w, "%*s%s\n", 4*indent, "", v)
|
|
|
|
for _, child := range v.dom.children {
|
2013-05-17 14:25:48 -06:00
|
|
|
printDomTreeText(w, child, indent+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// printDomTreeDot prints the dominator tree of f in AT&T GraphViz
|
|
|
|
// (.dot) format.
|
|
|
|
func printDomTreeDot(w io.Writer, f *Function) {
|
2013-06-26 10:38:08 -06:00
|
|
|
fmt.Fprintln(w, "//", f)
|
2013-05-17 14:25:48 -06:00
|
|
|
fmt.Fprintln(w, "digraph domtree {")
|
|
|
|
for i, b := range f.Blocks {
|
|
|
|
v := b.dom
|
2013-12-05 07:50:18 -07:00
|
|
|
fmt.Fprintf(w, "\tn%d [label=\"%s (%d, %d)\",shape=\"rectangle\"];\n", v.pre, b, v.pre, v.post)
|
2013-05-17 14:25:48 -06:00
|
|
|
// TODO(adonovan): improve appearance of edges
|
|
|
|
// belonging to both dominator tree and CFG.
|
|
|
|
|
|
|
|
// Dominator tree edge.
|
|
|
|
if i != 0 {
|
2013-12-05 07:50:18 -07:00
|
|
|
fmt.Fprintf(w, "\tn%d -> n%d [style=\"solid\",weight=100];\n", v.idom.dom.pre, v.pre)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
// CFG edges.
|
|
|
|
for _, pred := range b.Preds {
|
2013-12-05 07:50:18 -07:00
|
|
|
fmt.Fprintf(w, "\tn%d -> n%d [style=\"dotted\",weight=0];\n", pred.dom.pre, v.pre)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
}
|