github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/ssa/cse.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  import (
     8  	"fmt"
     9  	"sort"
    10  )
    11  
    12  const (
    13  	cmpDepth = 4
    14  )
    15  
    16  // cse does common-subexpression elimination on the Function.
    17  // Values are just relinked, nothing is deleted. A subsequent deadcode
    18  // pass is required to actually remove duplicate expressions.
    19  func cse(f *Func) {
    20  	// Two values are equivalent if they satisfy the following definition:
    21  	// equivalent(v, w):
    22  	//   v.op == w.op
    23  	//   v.type == w.type
    24  	//   v.aux == w.aux
    25  	//   v.auxint == w.auxint
    26  	//   len(v.args) == len(w.args)
    27  	//   v.block == w.block if v.op == OpPhi
    28  	//   equivalent(v.args[i], w.args[i]) for i in 0..len(v.args)-1
    29  
    30  	// The algorithm searches for a partition of f's values into
    31  	// equivalence classes using the above definition.
    32  	// It starts with a coarse partition and iteratively refines it
    33  	// until it reaches a fixed point.
    34  
    35  	// Make initial coarse partitions by using a subset of the conditions above.
    36  	a := make([]*Value, 0, f.NumValues())
    37  	auxIDs := auxmap{}
    38  	for _, b := range f.Blocks {
    39  		for _, v := range b.Values {
    40  			if auxIDs[v.Aux] == 0 {
    41  				auxIDs[v.Aux] = int32(len(auxIDs)) + 1
    42  			}
    43  			if v.Type.IsMemory() {
    44  				continue // memory values can never cse
    45  			}
    46  			if opcodeTable[v.Op].commutative && len(v.Args) == 2 && v.Args[1].ID < v.Args[0].ID {
    47  				// Order the arguments of binary commutative operations.
    48  				v.Args[0], v.Args[1] = v.Args[1], v.Args[0]
    49  			}
    50  			a = append(a, v)
    51  		}
    52  	}
    53  	partition := partitionValues(a, auxIDs)
    54  
    55  	// map from value id back to eqclass id
    56  	valueEqClass := make([]ID, f.NumValues())
    57  	for _, b := range f.Blocks {
    58  		for _, v := range b.Values {
    59  			// Use negative equivalence class #s for unique values.
    60  			valueEqClass[v.ID] = -v.ID
    61  		}
    62  	}
    63  	for i, e := range partition {
    64  		if f.pass.debug > 1 && len(e) > 500 {
    65  			fmt.Printf("CSE.large partition (%d): ", len(e))
    66  			for j := 0; j < 3; j++ {
    67  				fmt.Printf("%s ", e[j].LongString())
    68  			}
    69  			fmt.Println()
    70  		}
    71  
    72  		for _, v := range e {
    73  			valueEqClass[v.ID] = ID(i)
    74  		}
    75  		if f.pass.debug > 2 && len(e) > 1 {
    76  			fmt.Printf("CSE.partition #%d:", i)
    77  			for _, v := range e {
    78  				fmt.Printf(" %s", v.String())
    79  			}
    80  			fmt.Printf("\n")
    81  		}
    82  	}
    83  
    84  	// Find an equivalence class where some members of the class have
    85  	// non-equivalent arguments. Split the equivalence class appropriately.
    86  	// Repeat until we can't find any more splits.
    87  	for {
    88  		changed := false
    89  
    90  		// partition can grow in the loop. By not using a range loop here,
    91  		// we process new additions as they arrive, avoiding O(n^2) behavior.
    92  		for i := 0; i < len(partition); i++ {
    93  			e := partition[i]
    94  			v := e[0]
    95  			// all values in this equiv class that are not equivalent to v get moved
    96  			// into another equiv class.
    97  			// To avoid allocating while building that equivalence class,
    98  			// move the values equivalent to v to the beginning of e
    99  			// and other values to the end of e.
   100  			allvals := e
   101  		eqloop:
   102  			for j := 1; j < len(e); {
   103  				w := e[j]
   104  				equivalent := true
   105  				for i := 0; i < len(v.Args); i++ {
   106  					if valueEqClass[v.Args[i].ID] != valueEqClass[w.Args[i].ID] {
   107  						equivalent = false
   108  						break
   109  					}
   110  				}
   111  				if !equivalent || !v.Type.Equal(w.Type) {
   112  					// w is not equivalent to v.
   113  					// move it to the end and shrink e.
   114  					e[j], e[len(e)-1] = e[len(e)-1], e[j]
   115  					e = e[:len(e)-1]
   116  					valueEqClass[w.ID] = ID(len(partition))
   117  					changed = true
   118  					continue eqloop
   119  				}
   120  				// v and w are equivalent. Keep w in e.
   121  				j++
   122  			}
   123  			partition[i] = e
   124  			if len(e) < len(allvals) {
   125  				partition = append(partition, allvals[len(e):])
   126  			}
   127  		}
   128  
   129  		if !changed {
   130  			break
   131  		}
   132  	}
   133  
   134  	// Compute dominator tree
   135  	idom := dominators(f)
   136  	sdom := newSparseTree(f, idom)
   137  
   138  	// Compute substitutions we would like to do. We substitute v for w
   139  	// if v and w are in the same equivalence class and v dominates w.
   140  	rewrite := make([]*Value, f.NumValues())
   141  	for _, e := range partition {
   142  		for len(e) > 1 {
   143  			// Find a maximal dominant element in e
   144  			v := e[0]
   145  			for _, w := range e[1:] {
   146  				if sdom.isAncestorEq(w.Block, v.Block) {
   147  					v = w
   148  				}
   149  			}
   150  
   151  			// Replace all elements of e which v dominates
   152  			for i := 0; i < len(e); {
   153  				w := e[i]
   154  				if w == v {
   155  					e, e[i] = e[:len(e)-1], e[len(e)-1]
   156  				} else if sdom.isAncestorEq(v.Block, w.Block) {
   157  					rewrite[w.ID] = v
   158  					e, e[i] = e[:len(e)-1], e[len(e)-1]
   159  				} else {
   160  					i++
   161  				}
   162  			}
   163  		}
   164  	}
   165  
   166  	rewrites := int64(0)
   167  
   168  	// Apply substitutions
   169  	for _, b := range f.Blocks {
   170  		for _, v := range b.Values {
   171  			for i, w := range v.Args {
   172  				if x := rewrite[w.ID]; x != nil {
   173  					v.SetArg(i, x)
   174  					rewrites++
   175  				}
   176  			}
   177  		}
   178  		if v := b.Control; v != nil {
   179  			if x := rewrite[v.ID]; x != nil {
   180  				if v.Op == OpNilCheck {
   181  					// nilcheck pass will remove the nil checks and log
   182  					// them appropriately, so don't mess with them here.
   183  					continue
   184  				}
   185  				b.SetControl(x)
   186  			}
   187  		}
   188  	}
   189  	if f.pass.stats > 0 {
   190  		f.logStat("CSE REWRITES", rewrites)
   191  	}
   192  }
   193  
   194  // An eqclass approximates an equivalence class. During the
   195  // algorithm it may represent the union of several of the
   196  // final equivalence classes.
   197  type eqclass []*Value
   198  
   199  // partitionValues partitions the values into equivalence classes
   200  // based on having all the following features match:
   201  //  - opcode
   202  //  - type
   203  //  - auxint
   204  //  - aux
   205  //  - nargs
   206  //  - block # if a phi op
   207  //  - first two arg's opcodes and auxint
   208  //  - NOT first two arg's aux; that can break CSE.
   209  // partitionValues returns a list of equivalence classes, each
   210  // being a sorted by ID list of *Values. The eqclass slices are
   211  // backed by the same storage as the input slice.
   212  // Equivalence classes of size 1 are ignored.
   213  func partitionValues(a []*Value, auxIDs auxmap) []eqclass {
   214  	sort.Sort(sortvalues{a, auxIDs})
   215  
   216  	var partition []eqclass
   217  	for len(a) > 0 {
   218  		v := a[0]
   219  		j := 1
   220  		for ; j < len(a); j++ {
   221  			w := a[j]
   222  			if cmpVal(v, w, auxIDs, cmpDepth) != CMPeq {
   223  				break
   224  			}
   225  		}
   226  		if j > 1 {
   227  			partition = append(partition, a[:j])
   228  		}
   229  		a = a[j:]
   230  	}
   231  
   232  	return partition
   233  }
   234  func lt2Cmp(isLt bool) Cmp {
   235  	if isLt {
   236  		return CMPlt
   237  	}
   238  	return CMPgt
   239  }
   240  
   241  type auxmap map[interface{}]int32
   242  
   243  func cmpVal(v, w *Value, auxIDs auxmap, depth int) Cmp {
   244  	// Try to order these comparison by cost (cheaper first)
   245  	if v.Op != w.Op {
   246  		return lt2Cmp(v.Op < w.Op)
   247  	}
   248  	if v.AuxInt != w.AuxInt {
   249  		return lt2Cmp(v.AuxInt < w.AuxInt)
   250  	}
   251  	if len(v.Args) != len(w.Args) {
   252  		return lt2Cmp(len(v.Args) < len(w.Args))
   253  	}
   254  	if v.Op == OpPhi && v.Block != w.Block {
   255  		return lt2Cmp(v.Block.ID < w.Block.ID)
   256  	}
   257  
   258  	if tc := v.Type.Compare(w.Type); tc != CMPeq {
   259  		return tc
   260  	}
   261  
   262  	if v.Aux != w.Aux {
   263  		if v.Aux == nil {
   264  			return CMPlt
   265  		}
   266  		if w.Aux == nil {
   267  			return CMPgt
   268  		}
   269  		return lt2Cmp(auxIDs[v.Aux] < auxIDs[w.Aux])
   270  	}
   271  
   272  	if depth > 0 {
   273  		for i := range v.Args {
   274  			if v.Args[i] == w.Args[i] {
   275  				// skip comparing equal args
   276  				continue
   277  			}
   278  			if ac := cmpVal(v.Args[i], w.Args[i], auxIDs, depth-1); ac != CMPeq {
   279  				return ac
   280  			}
   281  		}
   282  	}
   283  
   284  	return CMPeq
   285  }
   286  
   287  // Sort values to make the initial partition.
   288  type sortvalues struct {
   289  	a      []*Value // array of values
   290  	auxIDs auxmap   // aux -> aux ID map
   291  }
   292  
   293  func (sv sortvalues) Len() int      { return len(sv.a) }
   294  func (sv sortvalues) Swap(i, j int) { sv.a[i], sv.a[j] = sv.a[j], sv.a[i] }
   295  func (sv sortvalues) Less(i, j int) bool {
   296  	v := sv.a[i]
   297  	w := sv.a[j]
   298  	if cmp := cmpVal(v, w, sv.auxIDs, cmpDepth); cmp != CMPeq {
   299  		return cmp == CMPlt
   300  	}
   301  
   302  	// Sort by value ID last to keep the sort result deterministic.
   303  	return v.ID < w.ID
   304  }