github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/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  // cse does common-subexpression elimination on the Function.
    13  // Values are just relinked, nothing is deleted. A subsequent deadcode
    14  // pass is required to actually remove duplicate expressions.
    15  func cse(f *Func) {
    16  	// Two values are equivalent if they satisfy the following definition:
    17  	// equivalent(v, w):
    18  	//   v.op == w.op
    19  	//   v.type == w.type
    20  	//   v.aux == w.aux
    21  	//   v.auxint == w.auxint
    22  	//   len(v.args) == len(w.args)
    23  	//   v.block == w.block if v.op == OpPhi
    24  	//   equivalent(v.args[i], w.args[i]) for i in 0..len(v.args)-1
    25  
    26  	// The algorithm searches for a partition of f's values into
    27  	// equivalence classes using the above definition.
    28  	// It starts with a coarse partition and iteratively refines it
    29  	// until it reaches a fixed point.
    30  
    31  	// Make initial coarse partitions by using a subset of the conditions above.
    32  	a := make([]*Value, 0, f.NumValues())
    33  	auxIDs := auxmap{}
    34  	for _, b := range f.Blocks {
    35  		for _, v := range b.Values {
    36  			if auxIDs[v.Aux] == 0 {
    37  				auxIDs[v.Aux] = int32(len(auxIDs)) + 1
    38  			}
    39  			if v.Type.IsMemory() {
    40  				continue // memory values can never cse
    41  			}
    42  			a = append(a, v)
    43  		}
    44  	}
    45  	partition := partitionValues(a, auxIDs)
    46  
    47  	// map from value id back to eqclass id
    48  	valueEqClass := make([]ID, f.NumValues())
    49  	for _, b := range f.Blocks {
    50  		for _, v := range b.Values {
    51  			// Use negative equivalence class #s for unique values.
    52  			valueEqClass[v.ID] = -v.ID
    53  		}
    54  	}
    55  	var pNum ID = 1
    56  	for _, e := range partition {
    57  		if f.pass.debug > 1 && len(e) > 500 {
    58  			fmt.Printf("CSE.large partition (%d): ", len(e))
    59  			for j := 0; j < 3; j++ {
    60  				fmt.Printf("%s ", e[j].LongString())
    61  			}
    62  			fmt.Println()
    63  		}
    64  
    65  		for _, v := range e {
    66  			valueEqClass[v.ID] = pNum
    67  		}
    68  		if f.pass.debug > 2 && len(e) > 1 {
    69  			fmt.Printf("CSE.partition #%d:", pNum)
    70  			for _, v := range e {
    71  				fmt.Printf(" %s", v.String())
    72  			}
    73  			fmt.Printf("\n")
    74  		}
    75  		pNum++
    76  	}
    77  
    78  	// Split equivalence classes at points where they have
    79  	// non-equivalent arguments.  Repeat until we can't find any
    80  	// more splits.
    81  	var splitPoints []int
    82  	byArgClass := new(partitionByArgClass) // reuseable partitionByArgClass to reduce allocations
    83  	for {
    84  		changed := false
    85  
    86  		// partition can grow in the loop. By not using a range loop here,
    87  		// we process new additions as they arrive, avoiding O(n^2) behavior.
    88  		for i := 0; i < len(partition); i++ {
    89  			e := partition[i]
    90  
    91  			if opcodeTable[e[0].Op].commutative {
    92  				// Order the first two args before comparison.
    93  				for _, v := range e {
    94  					if valueEqClass[v.Args[0].ID] > valueEqClass[v.Args[1].ID] {
    95  						v.Args[0], v.Args[1] = v.Args[1], v.Args[0]
    96  					}
    97  				}
    98  			}
    99  
   100  			// Sort by eq class of arguments.
   101  			byArgClass.a = e
   102  			byArgClass.eqClass = valueEqClass
   103  			sort.Sort(byArgClass)
   104  
   105  			// Find split points.
   106  			splitPoints = append(splitPoints[:0], 0)
   107  			for j := 1; j < len(e); j++ {
   108  				v, w := e[j-1], e[j]
   109  				// Note: commutative args already correctly ordered by byArgClass.
   110  				eqArgs := true
   111  				for k, a := range v.Args {
   112  					b := w.Args[k]
   113  					if valueEqClass[a.ID] != valueEqClass[b.ID] {
   114  						eqArgs = false
   115  						break
   116  					}
   117  				}
   118  				if !eqArgs {
   119  					splitPoints = append(splitPoints, j)
   120  				}
   121  			}
   122  			if len(splitPoints) == 1 {
   123  				continue // no splits, leave equivalence class alone.
   124  			}
   125  
   126  			// Move another equivalence class down in place of e.
   127  			partition[i] = partition[len(partition)-1]
   128  			partition = partition[:len(partition)-1]
   129  			i--
   130  
   131  			// Add new equivalence classes for the parts of e we found.
   132  			splitPoints = append(splitPoints, len(e))
   133  			for j := 0; j < len(splitPoints)-1; j++ {
   134  				f := e[splitPoints[j]:splitPoints[j+1]]
   135  				if len(f) == 1 {
   136  					// Don't add singletons.
   137  					valueEqClass[f[0].ID] = -f[0].ID
   138  					continue
   139  				}
   140  				for _, v := range f {
   141  					valueEqClass[v.ID] = pNum
   142  				}
   143  				pNum++
   144  				partition = append(partition, f)
   145  			}
   146  			changed = true
   147  		}
   148  
   149  		if !changed {
   150  			break
   151  		}
   152  	}
   153  
   154  	sdom := f.sdom()
   155  
   156  	// Compute substitutions we would like to do. We substitute v for w
   157  	// if v and w are in the same equivalence class and v dominates w.
   158  	rewrite := make([]*Value, f.NumValues())
   159  	byDom := new(partitionByDom) // reusable partitionByDom to reduce allocs
   160  	for _, e := range partition {
   161  		byDom.a = e
   162  		byDom.sdom = sdom
   163  		sort.Sort(byDom)
   164  		for i := 0; i < len(e)-1; i++ {
   165  			// e is sorted by domorder, so a maximal dominant element is first in the slice
   166  			v := e[i]
   167  			if v == nil {
   168  				continue
   169  			}
   170  
   171  			e[i] = nil
   172  			// Replace all elements of e which v dominates
   173  			for j := i + 1; j < len(e); j++ {
   174  				w := e[j]
   175  				if w == nil {
   176  					continue
   177  				}
   178  				if sdom.isAncestorEq(v.Block, w.Block) {
   179  					rewrite[w.ID] = v
   180  					e[j] = nil
   181  				} else {
   182  					// e is sorted by domorder, so v.Block doesn't dominate any subsequent blocks in e
   183  					break
   184  				}
   185  			}
   186  		}
   187  	}
   188  
   189  	// if we rewrite a tuple generator to a new one in a different block,
   190  	// copy its selectors to the new generator's block, so tuple generator
   191  	// and selectors stay together.
   192  	// be careful not to copy same selectors more than once (issue 16741).
   193  	copiedSelects := make(map[ID][]*Value)
   194  	for _, b := range f.Blocks {
   195  	out:
   196  		for _, v := range b.Values {
   197  			// New values are created when selectors are copied to
   198  			// a new block. We can safely ignore those new values,
   199  			// since they have already been copied (issue 17918).
   200  			if int(v.ID) >= len(rewrite) || rewrite[v.ID] != nil {
   201  				continue
   202  			}
   203  			if v.Op != OpSelect0 && v.Op != OpSelect1 {
   204  				continue
   205  			}
   206  			if !v.Args[0].Type.IsTuple() {
   207  				f.Fatalf("arg of tuple selector %s is not a tuple: %s", v.String(), v.Args[0].LongString())
   208  			}
   209  			t := rewrite[v.Args[0].ID]
   210  			if t != nil && t.Block != b {
   211  				// v.Args[0] is tuple generator, CSE'd into a different block as t, v is left behind
   212  				for _, c := range copiedSelects[t.ID] {
   213  					if v.Op == c.Op {
   214  						// an equivalent selector is already copied
   215  						rewrite[v.ID] = c
   216  						continue out
   217  					}
   218  				}
   219  				c := v.copyInto(t.Block)
   220  				rewrite[v.ID] = c
   221  				copiedSelects[t.ID] = append(copiedSelects[t.ID], c)
   222  			}
   223  		}
   224  	}
   225  
   226  	rewrites := int64(0)
   227  
   228  	// Apply substitutions
   229  	for _, b := range f.Blocks {
   230  		for _, v := range b.Values {
   231  			for i, w := range v.Args {
   232  				if x := rewrite[w.ID]; x != nil {
   233  					v.SetArg(i, x)
   234  					rewrites++
   235  				}
   236  			}
   237  		}
   238  		if v := b.Control; v != nil {
   239  			if x := rewrite[v.ID]; x != nil {
   240  				if v.Op == OpNilCheck {
   241  					// nilcheck pass will remove the nil checks and log
   242  					// them appropriately, so don't mess with them here.
   243  					continue
   244  				}
   245  				b.SetControl(x)
   246  			}
   247  		}
   248  	}
   249  	if f.pass.stats > 0 {
   250  		f.LogStat("CSE REWRITES", rewrites)
   251  	}
   252  }
   253  
   254  // An eqclass approximates an equivalence class. During the
   255  // algorithm it may represent the union of several of the
   256  // final equivalence classes.
   257  type eqclass []*Value
   258  
   259  // partitionValues partitions the values into equivalence classes
   260  // based on having all the following features match:
   261  //  - opcode
   262  //  - type
   263  //  - auxint
   264  //  - aux
   265  //  - nargs
   266  //  - block # if a phi op
   267  //  - first two arg's opcodes and auxint
   268  //  - NOT first two arg's aux; that can break CSE.
   269  // partitionValues returns a list of equivalence classes, each
   270  // being a sorted by ID list of *Values. The eqclass slices are
   271  // backed by the same storage as the input slice.
   272  // Equivalence classes of size 1 are ignored.
   273  func partitionValues(a []*Value, auxIDs auxmap) []eqclass {
   274  	sort.Sort(sortvalues{a, auxIDs})
   275  
   276  	var partition []eqclass
   277  	for len(a) > 0 {
   278  		v := a[0]
   279  		j := 1
   280  		for ; j < len(a); j++ {
   281  			w := a[j]
   282  			if cmpVal(v, w, auxIDs) != CMPeq {
   283  				break
   284  			}
   285  		}
   286  		if j > 1 {
   287  			partition = append(partition, a[:j])
   288  		}
   289  		a = a[j:]
   290  	}
   291  
   292  	return partition
   293  }
   294  func lt2Cmp(isLt bool) Cmp {
   295  	if isLt {
   296  		return CMPlt
   297  	}
   298  	return CMPgt
   299  }
   300  
   301  type auxmap map[interface{}]int32
   302  
   303  func cmpVal(v, w *Value, auxIDs auxmap) Cmp {
   304  	// Try to order these comparison by cost (cheaper first)
   305  	if v.Op != w.Op {
   306  		return lt2Cmp(v.Op < w.Op)
   307  	}
   308  	if v.AuxInt != w.AuxInt {
   309  		return lt2Cmp(v.AuxInt < w.AuxInt)
   310  	}
   311  	if len(v.Args) != len(w.Args) {
   312  		return lt2Cmp(len(v.Args) < len(w.Args))
   313  	}
   314  	if v.Op == OpPhi && v.Block != w.Block {
   315  		return lt2Cmp(v.Block.ID < w.Block.ID)
   316  	}
   317  	if v.Type.IsMemory() {
   318  		// We will never be able to CSE two values
   319  		// that generate memory.
   320  		return lt2Cmp(v.ID < w.ID)
   321  	}
   322  
   323  	if tc := v.Type.Compare(w.Type); tc != CMPeq {
   324  		return tc
   325  	}
   326  
   327  	if v.Aux != w.Aux {
   328  		if v.Aux == nil {
   329  			return CMPlt
   330  		}
   331  		if w.Aux == nil {
   332  			return CMPgt
   333  		}
   334  		return lt2Cmp(auxIDs[v.Aux] < auxIDs[w.Aux])
   335  	}
   336  
   337  	return CMPeq
   338  }
   339  
   340  // Sort values to make the initial partition.
   341  type sortvalues struct {
   342  	a      []*Value // array of values
   343  	auxIDs auxmap   // aux -> aux ID map
   344  }
   345  
   346  func (sv sortvalues) Len() int      { return len(sv.a) }
   347  func (sv sortvalues) Swap(i, j int) { sv.a[i], sv.a[j] = sv.a[j], sv.a[i] }
   348  func (sv sortvalues) Less(i, j int) bool {
   349  	v := sv.a[i]
   350  	w := sv.a[j]
   351  	if cmp := cmpVal(v, w, sv.auxIDs); cmp != CMPeq {
   352  		return cmp == CMPlt
   353  	}
   354  
   355  	// Sort by value ID last to keep the sort result deterministic.
   356  	return v.ID < w.ID
   357  }
   358  
   359  type partitionByDom struct {
   360  	a    []*Value // array of values
   361  	sdom SparseTree
   362  }
   363  
   364  func (sv partitionByDom) Len() int      { return len(sv.a) }
   365  func (sv partitionByDom) Swap(i, j int) { sv.a[i], sv.a[j] = sv.a[j], sv.a[i] }
   366  func (sv partitionByDom) Less(i, j int) bool {
   367  	v := sv.a[i]
   368  	w := sv.a[j]
   369  	return sv.sdom.domorder(v.Block) < sv.sdom.domorder(w.Block)
   370  }
   371  
   372  type partitionByArgClass struct {
   373  	a       []*Value // array of values
   374  	eqClass []ID     // equivalence class IDs of values
   375  }
   376  
   377  func (sv partitionByArgClass) Len() int      { return len(sv.a) }
   378  func (sv partitionByArgClass) Swap(i, j int) { sv.a[i], sv.a[j] = sv.a[j], sv.a[i] }
   379  func (sv partitionByArgClass) Less(i, j int) bool {
   380  	v := sv.a[i]
   381  	w := sv.a[j]
   382  	for i, a := range v.Args {
   383  		b := w.Args[i]
   384  		if sv.eqClass[a.ID] < sv.eqClass[b.ID] {
   385  			return true
   386  		}
   387  		if sv.eqClass[a.ID] > sv.eqClass[b.ID] {
   388  			return false
   389  		}
   390  	}
   391  	return false
   392  }