Source file src/cmd/compile/internal/ssa/cse.go

     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  	"cmd/compile/internal/types"
     9  	"cmd/internal/src"
    10  	"cmp"
    11  	"fmt"
    12  	"slices"
    13  )
    14  
    15  // cse does common-subexpression elimination on the Function.
    16  // Values are just relinked, nothing is deleted. A subsequent deadcode
    17  // pass is required to actually remove duplicate expressions.
    18  func cse(f *Func) {
    19  	// Two values are equivalent if they satisfy the following definition:
    20  	// equivalent(v, w):
    21  	//   v.op == w.op
    22  	//   v.type == w.type
    23  	//   v.aux == w.aux
    24  	//   v.auxint == w.auxint
    25  	//   len(v.args) == len(w.args)
    26  	//   v.block == w.block if v.op == OpPhi
    27  	//   equivalent(v.args[i], w.args[i]) for i in 0..len(v.args)-1
    28  
    29  	// The algorithm searches for a partition of f's values into
    30  	// equivalence classes using the above definition.
    31  	// It starts with a coarse partition and iteratively refines it
    32  	// until it reaches a fixed point.
    33  
    34  	// Make initial coarse partitions by using a subset of the conditions above.
    35  	a := f.Cache.allocValueSlice(f.NumValues())
    36  	defer func() { f.Cache.freeValueSlice(a) }() // inside closure to use final value of a
    37  	a = a[:0]
    38  	if f.auxmap == nil {
    39  		f.auxmap = auxmap{}
    40  	}
    41  	for _, b := range f.Blocks {
    42  		for _, v := range b.Values {
    43  			if v.Type.IsMemory() {
    44  				continue // memory values can never cse
    45  			}
    46  			if f.auxmap[v.Aux] == 0 {
    47  				f.auxmap[v.Aux] = int32(len(f.auxmap)) + 1
    48  			}
    49  			a = append(a, v)
    50  		}
    51  	}
    52  	partition := partitionValues(a, f.auxmap)
    53  
    54  	// map from value id back to eqclass id
    55  	valueEqClass := f.Cache.allocIDSlice(f.NumValues())
    56  	defer f.Cache.freeIDSlice(valueEqClass)
    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  	var pNum ID = 1
    64  	for _, e := range partition {
    65  		if f.pass.debug > 1 && len(e) > 500 {
    66  			fmt.Printf("CSE.large partition (%d): ", len(e))
    67  			for j := 0; j < 3; j++ {
    68  				fmt.Printf("%s ", e[j].LongString())
    69  			}
    70  			fmt.Println()
    71  		}
    72  
    73  		for _, v := range e {
    74  			valueEqClass[v.ID] = pNum
    75  		}
    76  		if f.pass.debug > 2 && len(e) > 1 {
    77  			fmt.Printf("CSE.partition #%d:", pNum)
    78  			for _, v := range e {
    79  				fmt.Printf(" %s", v.String())
    80  			}
    81  			fmt.Printf("\n")
    82  		}
    83  		pNum++
    84  	}
    85  
    86  	// Split equivalence classes at points where they have
    87  	// non-equivalent arguments.  Repeat until we can't find any
    88  	// more splits.
    89  	var splitPoints []int
    90  	for {
    91  		changed := false
    92  
    93  		// partition can grow in the loop. By not using a range loop here,
    94  		// we process new additions as they arrive, avoiding O(n^2) behavior.
    95  		for i := 0; i < len(partition); i++ {
    96  			e := partition[i]
    97  
    98  			if opcodeTable[e[0].Op].commutative {
    99  				// Order the first two args before comparison.
   100  				for _, v := range e {
   101  					if valueEqClass[v.Args[0].ID] > valueEqClass[v.Args[1].ID] {
   102  						v.Args[0], v.Args[1] = v.Args[1], v.Args[0]
   103  					}
   104  				}
   105  			}
   106  
   107  			// Sort by eq class of arguments.
   108  			slices.SortFunc(e, func(v, w *Value) int {
   109  				for i, a := range v.Args {
   110  					b := w.Args[i]
   111  					if valueEqClass[a.ID] < valueEqClass[b.ID] {
   112  						return -1
   113  					}
   114  					if valueEqClass[a.ID] > valueEqClass[b.ID] {
   115  						return +1
   116  					}
   117  				}
   118  				return 0
   119  			})
   120  
   121  			// Find split points.
   122  			splitPoints = append(splitPoints[:0], 0)
   123  			for j := 1; j < len(e); j++ {
   124  				v, w := e[j-1], e[j]
   125  				// Note: commutative args already correctly ordered by byArgClass.
   126  				eqArgs := true
   127  				for k, a := range v.Args {
   128  					b := w.Args[k]
   129  					if valueEqClass[a.ID] != valueEqClass[b.ID] {
   130  						eqArgs = false
   131  						break
   132  					}
   133  				}
   134  				if !eqArgs {
   135  					splitPoints = append(splitPoints, j)
   136  				}
   137  			}
   138  			if len(splitPoints) == 1 {
   139  				continue // no splits, leave equivalence class alone.
   140  			}
   141  
   142  			// Move another equivalence class down in place of e.
   143  			partition[i] = partition[len(partition)-1]
   144  			partition = partition[:len(partition)-1]
   145  			i--
   146  
   147  			// Add new equivalence classes for the parts of e we found.
   148  			splitPoints = append(splitPoints, len(e))
   149  			for j := 0; j < len(splitPoints)-1; j++ {
   150  				f := e[splitPoints[j]:splitPoints[j+1]]
   151  				if len(f) == 1 {
   152  					// Don't add singletons.
   153  					valueEqClass[f[0].ID] = -f[0].ID
   154  					continue
   155  				}
   156  				for _, v := range f {
   157  					valueEqClass[v.ID] = pNum
   158  				}
   159  				pNum++
   160  				partition = append(partition, f)
   161  			}
   162  			changed = true
   163  		}
   164  
   165  		if !changed {
   166  			break
   167  		}
   168  	}
   169  
   170  	sdom := f.Sdom()
   171  
   172  	// Compute substitutions we would like to do. We substitute v for w
   173  	// if v and w are in the same equivalence class and v dominates w.
   174  	rewrite := f.Cache.allocValueSlice(f.NumValues())
   175  	defer f.Cache.freeValueSlice(rewrite)
   176  	for _, e := range partition {
   177  		slices.SortFunc(e, func(v, w *Value) int {
   178  			return cmp.Compare(sdom.domorder(v.Block), sdom.domorder(w.Block))
   179  		})
   180  
   181  		for i := 0; i < len(e)-1; i++ {
   182  			// e is sorted by domorder, so a maximal dominant element is first in the slice
   183  			v := e[i]
   184  			if v == nil {
   185  				continue
   186  			}
   187  
   188  			e[i] = nil
   189  			// Replace all elements of e which v dominates
   190  			for j := i + 1; j < len(e); j++ {
   191  				w := e[j]
   192  				if w == nil {
   193  					continue
   194  				}
   195  				if sdom.IsAncestorEq(v.Block, w.Block) {
   196  					rewrite[w.ID] = v
   197  					e[j] = nil
   198  				} else {
   199  					// e is sorted by domorder, so v.Block doesn't dominate any subsequent blocks in e
   200  					break
   201  				}
   202  			}
   203  		}
   204  	}
   205  
   206  	rewrites := int64(0)
   207  
   208  	// Apply substitutions
   209  	for _, b := range f.Blocks {
   210  		for _, v := range b.Values {
   211  			for i, w := range v.Args {
   212  				if x := rewrite[w.ID]; x != nil {
   213  					if w.Pos.IsStmt() == src.PosIsStmt {
   214  						// about to lose a statement marker, w
   215  						// w is an input to v; if they're in the same block
   216  						// and the same line, v is a good-enough new statement boundary.
   217  						if w.Block == v.Block && w.Pos.Line() == v.Pos.Line() {
   218  							v.Pos = v.Pos.WithIsStmt()
   219  							w.Pos = w.Pos.WithNotStmt()
   220  						} // TODO and if this fails?
   221  					}
   222  					v.SetArg(i, x)
   223  					rewrites++
   224  				}
   225  			}
   226  		}
   227  		for i, v := range b.ControlValues() {
   228  			if x := rewrite[v.ID]; x != nil {
   229  				if v.Op == OpNilCheck {
   230  					// nilcheck pass will remove the nil checks and log
   231  					// them appropriately, so don't mess with them here.
   232  					continue
   233  				}
   234  				b.ReplaceControl(i, x)
   235  			}
   236  		}
   237  	}
   238  
   239  	if f.pass.stats > 0 {
   240  		f.LogStat("CSE REWRITES", rewrites)
   241  	}
   242  }
   243  
   244  // An eqclass approximates an equivalence class. During the
   245  // algorithm it may represent the union of several of the
   246  // final equivalence classes.
   247  type eqclass []*Value
   248  
   249  // partitionValues partitions the values into equivalence classes
   250  // based on having all the following features match:
   251  //   - opcode
   252  //   - type
   253  //   - auxint
   254  //   - aux
   255  //   - nargs
   256  //   - block # if a phi op
   257  //   - first two arg's opcodes and auxint
   258  //   - NOT first two arg's aux; that can break CSE.
   259  //
   260  // partitionValues returns a list of equivalence classes, each
   261  // being a sorted by ID list of *Values. The eqclass slices are
   262  // backed by the same storage as the input slice.
   263  // Equivalence classes of size 1 are ignored.
   264  func partitionValues(a []*Value, auxIDs auxmap) []eqclass {
   265  	slices.SortFunc(a, func(v, w *Value) int {
   266  		switch cmpVal(v, w, auxIDs) {
   267  		case types.CMPlt:
   268  			return -1
   269  		case types.CMPgt:
   270  			return +1
   271  		default:
   272  			// Sort by value ID last to keep the sort result deterministic.
   273  			return cmp.Compare(v.ID, w.ID)
   274  		}
   275  	})
   276  
   277  	var partition []eqclass
   278  	for len(a) > 0 {
   279  		v := a[0]
   280  		j := 1
   281  		for ; j < len(a); j++ {
   282  			w := a[j]
   283  			if cmpVal(v, w, auxIDs) != types.CMPeq {
   284  				break
   285  			}
   286  		}
   287  		if j > 1 {
   288  			partition = append(partition, a[:j])
   289  		}
   290  		a = a[j:]
   291  	}
   292  
   293  	return partition
   294  }
   295  func lt2Cmp(isLt bool) types.Cmp {
   296  	if isLt {
   297  		return types.CMPlt
   298  	}
   299  	return types.CMPgt
   300  }
   301  
   302  type auxmap map[Aux]int32
   303  
   304  func cmpVal(v, w *Value, auxIDs auxmap) types.Cmp {
   305  	// Try to order these comparison by cost (cheaper first)
   306  	if v.Op != w.Op {
   307  		return lt2Cmp(v.Op < w.Op)
   308  	}
   309  	if v.AuxInt != w.AuxInt {
   310  		return lt2Cmp(v.AuxInt < w.AuxInt)
   311  	}
   312  	if len(v.Args) != len(w.Args) {
   313  		return lt2Cmp(len(v.Args) < len(w.Args))
   314  	}
   315  	if v.Op == OpPhi && v.Block != w.Block {
   316  		return lt2Cmp(v.Block.ID < w.Block.ID)
   317  	}
   318  	if v.Type.IsMemory() {
   319  		// We will never be able to CSE two values
   320  		// that generate memory.
   321  		return lt2Cmp(v.ID < w.ID)
   322  	}
   323  	// OpSelect is a pseudo-op. We need to be more aggressive
   324  	// regarding CSE to keep multiple OpSelect's of the same
   325  	// argument from existing.
   326  	if v.Op != OpSelect0 && v.Op != OpSelect1 && v.Op != OpSelectN {
   327  		if tc := v.Type.Compare(w.Type); tc != types.CMPeq {
   328  			return tc
   329  		}
   330  	}
   331  
   332  	if v.Aux != w.Aux {
   333  		if v.Aux == nil {
   334  			return types.CMPlt
   335  		}
   336  		if w.Aux == nil {
   337  			return types.CMPgt
   338  		}
   339  		return lt2Cmp(auxIDs[v.Aux] < auxIDs[w.Aux])
   340  	}
   341  
   342  	return types.CMPeq
   343  }
   344  

View as plain text