2015-03-27 13:41:30 -07:00
|
|
|
// Copyright 2015 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 ssa
|
|
|
|
|
|
2015-09-04 06:33:56 -05:00
|
|
|
import "sort"
|
2015-03-27 13:41:30 -07:00
|
|
|
|
|
|
|
|
// cse does common-subexpression elimination on the Function.
|
|
|
|
|
// Values are just relinked, nothing is deleted. A subsequent deadcode
|
|
|
|
|
// pass is required to actually remove duplicate expressions.
|
|
|
|
|
func cse(f *Func) {
|
|
|
|
|
// Two values are equivalent if they satisfy the following definition:
|
|
|
|
|
// equivalent(v, w):
|
|
|
|
|
// v.op == w.op
|
|
|
|
|
// v.type == w.type
|
|
|
|
|
// v.aux == w.aux
|
2015-06-23 16:44:06 -07:00
|
|
|
// v.auxint == w.auxint
|
2015-03-27 13:41:30 -07:00
|
|
|
// len(v.args) == len(w.args)
|
2015-07-20 18:50:17 -07:00
|
|
|
// v.block == w.block if v.op == OpPhi
|
2015-03-27 13:41:30 -07:00
|
|
|
// equivalent(v.args[i], w.args[i]) for i in 0..len(v.args)-1
|
|
|
|
|
|
|
|
|
|
// The algorithm searches for a partition of f's values into
|
|
|
|
|
// equivalence classes using the above definition.
|
|
|
|
|
// It starts with a coarse partition and iteratively refines it
|
|
|
|
|
// until it reaches a fixed point.
|
|
|
|
|
|
2015-07-22 21:04:25 -07:00
|
|
|
// Make initial partition based on opcode, type-name, aux, auxint, nargs, phi-block, and the ops of v's first args
|
2015-03-27 13:41:30 -07:00
|
|
|
type key struct {
|
2015-06-14 23:06:39 +01:00
|
|
|
op Op
|
2015-06-24 14:34:28 -07:00
|
|
|
typ string
|
2015-06-14 23:06:39 +01:00
|
|
|
aux interface{}
|
|
|
|
|
auxint int64
|
|
|
|
|
nargs int
|
2015-07-20 18:50:17 -07:00
|
|
|
block ID // block id for phi vars, -1 otherwise
|
2015-07-22 21:04:25 -07:00
|
|
|
arg0op Op // v.Args[0].Op if len(v.Args) > 0, OpInvalid otherwise
|
|
|
|
|
arg1op Op // v.Args[1].Op if len(v.Args) > 1, OpInvalid otherwise
|
2015-03-27 13:41:30 -07:00
|
|
|
}
|
|
|
|
|
m := map[key]eqclass{}
|
|
|
|
|
for _, b := range f.Blocks {
|
|
|
|
|
for _, v := range b.Values {
|
2015-07-20 18:50:17 -07:00
|
|
|
bid := ID(-1)
|
|
|
|
|
if v.Op == OpPhi {
|
|
|
|
|
bid = b.ID
|
|
|
|
|
}
|
2015-07-22 21:04:25 -07:00
|
|
|
arg0op := OpInvalid
|
|
|
|
|
if len(v.Args) > 0 {
|
|
|
|
|
arg0op = v.Args[0].Op
|
|
|
|
|
}
|
|
|
|
|
arg1op := OpInvalid
|
|
|
|
|
if len(v.Args) > 1 {
|
|
|
|
|
arg1op = v.Args[1].Op
|
|
|
|
|
}
|
2015-09-02 20:17:47 -05:00
|
|
|
|
2015-09-04 06:33:56 -05:00
|
|
|
// This assumes that floats are stored in AuxInt
|
|
|
|
|
// instead of Aux. If not, then we need to use the
|
|
|
|
|
// float bits as part of the key, otherwise since 0.0 == -0.0
|
|
|
|
|
// this would incorrectly treat 0.0 and -0.0 as identical values
|
|
|
|
|
k := key{v.Op, v.Type.String(), v.Aux, v.AuxInt, len(v.Args), bid, arg0op, arg1op}
|
2015-03-27 13:41:30 -07:00
|
|
|
m[k] = append(m[k], v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A partition is a set of disjoint eqclasses.
|
|
|
|
|
var partition []eqclass
|
|
|
|
|
for _, v := range m {
|
|
|
|
|
partition = append(partition, v)
|
|
|
|
|
}
|
2015-07-20 18:50:17 -07:00
|
|
|
// TODO: Sort partition here for perfect reproducibility?
|
|
|
|
|
// Sort by what? Partition size?
|
|
|
|
|
// (Could that improve efficiency by discovering splits earlier?)
|
2015-03-27 13:41:30 -07:00
|
|
|
|
|
|
|
|
// map from value id back to eqclass id
|
|
|
|
|
valueEqClass := make([]int, f.NumValues())
|
|
|
|
|
for i, e := range partition {
|
|
|
|
|
for _, v := range e {
|
|
|
|
|
valueEqClass[v.ID] = i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find an equivalence class where some members of the class have
|
2015-03-31 15:37:25 -07:00
|
|
|
// non-equivalent arguments. Split the equivalence class appropriately.
|
2015-03-27 13:41:30 -07:00
|
|
|
// Repeat until we can't find any more splits.
|
|
|
|
|
for {
|
|
|
|
|
changed := false
|
|
|
|
|
|
2015-07-15 14:38:19 -06:00
|
|
|
// partition can grow in the loop. By not using a range loop here,
|
|
|
|
|
// we process new additions as they arrive, avoiding O(n^2) behavior.
|
|
|
|
|
for i := 0; i < len(partition); i++ {
|
|
|
|
|
e := partition[i]
|
2015-03-27 13:41:30 -07:00
|
|
|
v := e[0]
|
|
|
|
|
// all values in this equiv class that are not equivalent to v get moved
|
2015-07-22 21:21:50 -07:00
|
|
|
// into another equiv class.
|
|
|
|
|
// To avoid allocating while building that equivalence class,
|
2015-09-11 10:28:33 -07:00
|
|
|
// move the values equivalent to v to the beginning of e
|
|
|
|
|
// and other values to the end of e.
|
2015-07-22 21:21:50 -07:00
|
|
|
allvals := e
|
2015-03-27 13:41:30 -07:00
|
|
|
eqloop:
|
|
|
|
|
for j := 1; j < len(e); {
|
|
|
|
|
w := e[j]
|
|
|
|
|
for i := 0; i < len(v.Args); i++ {
|
2015-06-24 14:34:28 -07:00
|
|
|
if valueEqClass[v.Args[i].ID] != valueEqClass[w.Args[i].ID] || !v.Type.Equal(w.Type) {
|
2015-03-27 13:41:30 -07:00
|
|
|
// w is not equivalent to v.
|
2015-09-11 10:28:33 -07:00
|
|
|
// move it to the end and shrink e.
|
2015-07-22 21:21:50 -07:00
|
|
|
e[j], e[len(e)-1] = e[len(e)-1], e[j]
|
|
|
|
|
e = e[:len(e)-1]
|
2015-03-27 13:41:30 -07:00
|
|
|
valueEqClass[w.ID] = len(partition)
|
|
|
|
|
changed = true
|
|
|
|
|
continue eqloop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// v and w are equivalent. Keep w in e.
|
|
|
|
|
j++
|
|
|
|
|
}
|
|
|
|
|
partition[i] = e
|
2015-09-11 10:28:33 -07:00
|
|
|
if len(e) < len(allvals) {
|
|
|
|
|
partition = append(partition, allvals[len(e):])
|
2015-03-27 13:41:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !changed {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compute dominator tree
|
|
|
|
|
idom := dominators(f)
|
2015-09-06 21:32:24 -04:00
|
|
|
sdom := newSparseTree(f, idom)
|
2015-03-27 13:41:30 -07:00
|
|
|
|
|
|
|
|
// Compute substitutions we would like to do. We substitute v for w
|
|
|
|
|
// if v and w are in the same equivalence class and v dominates w.
|
|
|
|
|
rewrite := make([]*Value, f.NumValues())
|
|
|
|
|
for _, e := range partition {
|
|
|
|
|
sort.Sort(e) // ensure deterministic ordering
|
|
|
|
|
for len(e) > 1 {
|
|
|
|
|
// Find a maximal dominant element in e
|
|
|
|
|
v := e[0]
|
|
|
|
|
for _, w := range e[1:] {
|
2015-09-06 21:32:24 -04:00
|
|
|
if sdom.isAncestorEq(w.Block, v.Block) {
|
2015-03-27 13:41:30 -07:00
|
|
|
v = w
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace all elements of e which v dominates
|
|
|
|
|
for i := 0; i < len(e); {
|
|
|
|
|
w := e[i]
|
2015-04-15 15:51:25 -07:00
|
|
|
if w == v {
|
|
|
|
|
e, e[i] = e[:len(e)-1], e[len(e)-1]
|
2015-09-06 21:32:24 -04:00
|
|
|
} else if sdom.isAncestorEq(v.Block, w.Block) {
|
2015-03-27 13:41:30 -07:00
|
|
|
rewrite[w.ID] = v
|
|
|
|
|
e, e[i] = e[:len(e)-1], e[len(e)-1]
|
|
|
|
|
} else {
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO(khr): if value is a control value, do we need to keep it block-local?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply substitutions
|
|
|
|
|
for _, b := range f.Blocks {
|
|
|
|
|
for _, v := range b.Values {
|
|
|
|
|
for i, w := range v.Args {
|
|
|
|
|
if x := rewrite[w.ID]; x != nil {
|
|
|
|
|
v.SetArg(i, x)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns true if b dominates c.
|
2015-09-06 21:32:24 -04:00
|
|
|
// simple and iterative, has O(depth) complexity in tall trees.
|
2015-03-27 13:41:30 -07:00
|
|
|
func dom(b, c *Block, idom []*Block) bool {
|
|
|
|
|
// Walk up from c in the dominator tree looking for b.
|
|
|
|
|
for c != nil {
|
|
|
|
|
if c == b {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
c = idom[c.ID]
|
|
|
|
|
}
|
|
|
|
|
// Reached the entry block, never saw b.
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An eqclass approximates an equivalence class. During the
|
|
|
|
|
// algorithm it may represent the union of several of the
|
|
|
|
|
// final equivalence classes.
|
|
|
|
|
type eqclass []*Value
|
|
|
|
|
|
|
|
|
|
// Sort an equivalence class by value ID.
|
|
|
|
|
func (e eqclass) Len() int { return len(e) }
|
|
|
|
|
func (e eqclass) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
|
|
|
|
|
func (e eqclass) Less(i, j int) bool { return e[i].ID < e[j].ID }
|