Source file src/cmd/compile/internal/ssa/deadstore.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/ir"
     9  	"cmd/compile/internal/types"
    10  )
    11  
    12  // dse does dead-store elimination on the Function.
    13  // Dead stores are those which are unconditionally followed by
    14  // another store to the same location, with no intervening load.
    15  // This implementation only works within a basic block. TODO: use something more global.
    16  func dse(f *Func) {
    17  	var stores []*Value
    18  	loadUse := f.newSparseSet(f.NumValues())
    19  	defer f.retSparseSet(loadUse)
    20  	storeUse := f.newSparseSet(f.NumValues())
    21  	defer f.retSparseSet(storeUse)
    22  	shadowed := f.newSparseMap(f.NumValues())
    23  	defer f.retSparseMap(shadowed)
    24  	// localAddrs maps from a local variable (the Aux field of a LocalAddr value) to an instance of a LocalAddr value for that variable in the current block.
    25  	localAddrs := map[any]*Value{}
    26  	for _, b := range f.Blocks {
    27  		// Find all the stores in this block. Categorize their uses:
    28  		//  loadUse contains stores which are used by a subsequent load.
    29  		//  storeUse contains stores which are used by a subsequent store.
    30  		loadUse.clear()
    31  		storeUse.clear()
    32  		clear(localAddrs)
    33  		stores = stores[:0]
    34  		for _, v := range b.Values {
    35  			if v.Op == OpPhi {
    36  				// Ignore phis - they will always be first and can't be eliminated
    37  				continue
    38  			}
    39  			if v.Type.IsMemory() {
    40  				stores = append(stores, v)
    41  				for _, a := range v.Args {
    42  					if a.Block == b && a.Type.IsMemory() {
    43  						storeUse.add(a.ID)
    44  						if v.Op != OpStore && v.Op != OpZero && v.Op != OpVarDef {
    45  							// CALL, DUFFCOPY, etc. are both
    46  							// reads and writes.
    47  							loadUse.add(a.ID)
    48  						}
    49  					}
    50  				}
    51  			} else {
    52  				if v.Op == OpLocalAddr {
    53  					if _, ok := localAddrs[v.Aux]; !ok {
    54  						localAddrs[v.Aux] = v
    55  					}
    56  					continue
    57  				}
    58  				if v.Op == OpInlMark || v.Op == OpConvert {
    59  					// Not really a use of the memory. See #67957.
    60  					continue
    61  				}
    62  				for _, a := range v.Args {
    63  					if a.Block == b && a.Type.IsMemory() {
    64  						loadUse.add(a.ID)
    65  					}
    66  				}
    67  			}
    68  		}
    69  		if len(stores) == 0 {
    70  			continue
    71  		}
    72  
    73  		// find last store in the block
    74  		var last *Value
    75  		for _, v := range stores {
    76  			if storeUse.contains(v.ID) {
    77  				continue
    78  			}
    79  			if last != nil {
    80  				b.Fatalf("two final stores - simultaneous live stores %s %s", last.LongString(), v.LongString())
    81  			}
    82  			last = v
    83  		}
    84  		if last == nil {
    85  			b.Fatalf("no last store found - cycle?")
    86  		}
    87  
    88  		// Walk backwards looking for dead stores. Keep track of shadowed addresses.
    89  		// A "shadowed address" is a pointer, offset, and size describing a memory region that
    90  		// is known to be written. We keep track of shadowed addresses in the shadowed map,
    91  		// mapping the ID of the address to a shadowRange where future writes will happen.
    92  		// Since we're walking backwards, writes to a shadowed region are useless,
    93  		// as they will be immediately overwritten.
    94  		shadowed.clear()
    95  		v := last
    96  
    97  	walkloop:
    98  		if loadUse.contains(v.ID) {
    99  			// Someone might be reading this memory state.
   100  			// Clear all shadowed addresses.
   101  			shadowed.clear()
   102  		}
   103  		if v.Op == OpStore || v.Op == OpZero {
   104  			ptr := v.Args[0]
   105  			var off int64
   106  			for ptr.Op == OpOffPtr { // Walk to base pointer
   107  				off += ptr.AuxInt
   108  				ptr = ptr.Args[0]
   109  			}
   110  			var sz int64
   111  			if v.Op == OpStore {
   112  				sz = v.Aux.(*types.Type).Size()
   113  			} else { // OpZero
   114  				sz = v.AuxInt
   115  			}
   116  			if ptr.Op == OpLocalAddr {
   117  				if la, ok := localAddrs[ptr.Aux]; ok {
   118  					ptr = la
   119  				}
   120  			}
   121  			srNum, _ := shadowed.get(ptr.ID)
   122  			sr := shadowRange(srNum)
   123  			if sr.contains(off, off+sz) {
   124  				// Modify the store/zero into a copy of the memory state,
   125  				// effectively eliding the store operation.
   126  				if v.Op == OpStore {
   127  					// store addr value mem
   128  					v.SetArgs1(v.Args[2])
   129  				} else {
   130  					// zero addr mem
   131  					v.SetArgs1(v.Args[1])
   132  				}
   133  				v.Aux = nil
   134  				v.AuxInt = 0
   135  				v.Op = OpCopy
   136  			} else {
   137  				// Extend shadowed region.
   138  				shadowed.set(ptr.ID, int32(sr.merge(off, off+sz)))
   139  			}
   140  		}
   141  		// walk to previous store
   142  		if v.Op == OpPhi {
   143  			// At start of block.  Move on to next block.
   144  			// The memory phi, if it exists, is always
   145  			// the first logical store in the block.
   146  			// (Even if it isn't the first in the current b.Values order.)
   147  			continue
   148  		}
   149  		for _, a := range v.Args {
   150  			if a.Block == b && a.Type.IsMemory() {
   151  				v = a
   152  				goto walkloop
   153  			}
   154  		}
   155  	}
   156  }
   157  
   158  // A shadowRange encodes a set of byte offsets [lo():hi()] from
   159  // a given pointer that will be written to later in the block.
   160  // A zero shadowRange encodes an empty shadowed range.
   161  type shadowRange int32
   162  
   163  func (sr shadowRange) lo() int64 {
   164  	return int64(sr & 0xffff)
   165  }
   166  
   167  func (sr shadowRange) hi() int64 {
   168  	return int64((sr >> 16) & 0xffff)
   169  }
   170  
   171  // contains reports whether [lo:hi] is completely within sr.
   172  func (sr shadowRange) contains(lo, hi int64) bool {
   173  	return lo >= sr.lo() && hi <= sr.hi()
   174  }
   175  
   176  // merge returns the union of sr and [lo:hi].
   177  // merge is allowed to return something smaller than the union.
   178  func (sr shadowRange) merge(lo, hi int64) shadowRange {
   179  	if lo < 0 || hi > 0xffff {
   180  		// Ignore offsets that are too large or small.
   181  		return sr
   182  	}
   183  	if sr.lo() == sr.hi() {
   184  		// Old range is empty - use new one.
   185  		return shadowRange(lo + hi<<16)
   186  	}
   187  	if hi < sr.lo() || lo > sr.hi() {
   188  		// The two regions don't overlap or abut, so we would
   189  		// have to keep track of multiple disjoint ranges.
   190  		// Because we can only keep one, keep the larger one.
   191  		if sr.hi()-sr.lo() >= hi-lo {
   192  			return sr
   193  		}
   194  		return shadowRange(lo + hi<<16)
   195  	}
   196  	// Regions overlap or abut - compute the union.
   197  	return shadowRange(min(lo, sr.lo()) + max(hi, sr.hi())<<16)
   198  }
   199  
   200  // elimDeadAutosGeneric deletes autos that are never accessed. To achieve this
   201  // we track the operations that the address of each auto reaches and if it only
   202  // reaches stores then we delete all the stores. The other operations will then
   203  // be eliminated by the dead code elimination pass.
   204  func elimDeadAutosGeneric(f *Func) {
   205  	addr := make(map[*Value]*ir.Name) // values that the address of the auto reaches
   206  	elim := make(map[*Value]*ir.Name) // values that could be eliminated if the auto is
   207  	var used ir.NameSet               // used autos that must be kept
   208  
   209  	// visit the value and report whether any of the maps are updated
   210  	visit := func(v *Value) (changed bool) {
   211  		args := v.Args
   212  		switch v.Op {
   213  		case OpAddr, OpLocalAddr:
   214  			// Propagate the address if it points to an auto.
   215  			n, ok := v.Aux.(*ir.Name)
   216  			if !ok || n.Class != ir.PAUTO {
   217  				return
   218  			}
   219  			if addr[v] == nil {
   220  				addr[v] = n
   221  				changed = true
   222  			}
   223  			return
   224  		case OpVarDef:
   225  			// v should be eliminated if we eliminate the auto.
   226  			n, ok := v.Aux.(*ir.Name)
   227  			if !ok || n.Class != ir.PAUTO {
   228  				return
   229  			}
   230  			if elim[v] == nil {
   231  				elim[v] = n
   232  				changed = true
   233  			}
   234  			return
   235  		case OpVarLive:
   236  			// Don't delete the auto if it needs to be kept alive.
   237  
   238  			// We depend on this check to keep the autotmp stack slots
   239  			// for open-coded defers from being removed (since they
   240  			// may not be used by the inline code, but will be used by
   241  			// panic processing).
   242  			n, ok := v.Aux.(*ir.Name)
   243  			if !ok || n.Class != ir.PAUTO {
   244  				return
   245  			}
   246  			if !used.Has(n) {
   247  				used.Add(n)
   248  				changed = true
   249  			}
   250  			return
   251  		case OpStore, OpMove, OpZero:
   252  			// v should be eliminated if we eliminate the auto.
   253  			n, ok := addr[args[0]]
   254  			if ok && elim[v] == nil {
   255  				elim[v] = n
   256  				changed = true
   257  			}
   258  			// Other args might hold pointers to autos.
   259  			args = args[1:]
   260  		}
   261  
   262  		// The code below assumes that we have handled all the ops
   263  		// with sym effects already. Sanity check that here.
   264  		// Ignore Args since they can't be autos.
   265  		if v.Op.SymEffect() != SymNone && v.Op != OpArg {
   266  			panic("unhandled op with sym effect")
   267  		}
   268  
   269  		if v.Uses == 0 && v.Op != OpNilCheck && !v.Op.IsCall() && !v.Op.HasSideEffects() || len(args) == 0 {
   270  			// We need to keep nil checks even if they have no use.
   271  			// Also keep calls and values that have side effects.
   272  			return
   273  		}
   274  
   275  		// If the address of the auto reaches a memory or control
   276  		// operation not covered above then we probably need to keep it.
   277  		// We also need to keep autos if they reach Phis (issue #26153).
   278  		if v.Type.IsMemory() || v.Type.IsFlags() || v.Op == OpPhi || v.MemoryArg() != nil {
   279  			for _, a := range args {
   280  				if n, ok := addr[a]; ok {
   281  					if !used.Has(n) {
   282  						used.Add(n)
   283  						changed = true
   284  					}
   285  				}
   286  			}
   287  			return
   288  		}
   289  
   290  		// Propagate any auto addresses through v.
   291  		var node *ir.Name
   292  		for _, a := range args {
   293  			if n, ok := addr[a]; ok && !used.Has(n) {
   294  				if node == nil {
   295  					node = n
   296  				} else if node != n {
   297  					// Most of the time we only see one pointer
   298  					// reaching an op, but some ops can take
   299  					// multiple pointers (e.g. NeqPtr, Phi etc.).
   300  					// This is rare, so just propagate the first
   301  					// value to keep things simple.
   302  					used.Add(n)
   303  					changed = true
   304  				}
   305  			}
   306  		}
   307  		if node == nil {
   308  			return
   309  		}
   310  		if addr[v] == nil {
   311  			// The address of an auto reaches this op.
   312  			addr[v] = node
   313  			changed = true
   314  			return
   315  		}
   316  		if addr[v] != node {
   317  			// This doesn't happen in practice, but catch it just in case.
   318  			used.Add(node)
   319  			changed = true
   320  		}
   321  		return
   322  	}
   323  
   324  	iterations := 0
   325  	for {
   326  		if iterations == 4 {
   327  			// give up
   328  			return
   329  		}
   330  		iterations++
   331  		changed := false
   332  		for _, b := range f.Blocks {
   333  			for _, v := range b.Values {
   334  				changed = visit(v) || changed
   335  			}
   336  			// keep the auto if its address reaches a control value
   337  			for _, c := range b.ControlValues() {
   338  				if n, ok := addr[c]; ok && !used.Has(n) {
   339  					used.Add(n)
   340  					changed = true
   341  				}
   342  			}
   343  		}
   344  		if !changed {
   345  			break
   346  		}
   347  	}
   348  
   349  	// Eliminate stores to unread autos.
   350  	for v, n := range elim {
   351  		if used.Has(n) {
   352  			continue
   353  		}
   354  		// replace with OpCopy
   355  		v.SetArgs1(v.MemoryArg())
   356  		v.Aux = nil
   357  		v.AuxInt = 0
   358  		v.Op = OpCopy
   359  	}
   360  }
   361  
   362  // elimUnreadAutos deletes stores (and associated bookkeeping ops VarDef and VarKill)
   363  // to autos that are never read from.
   364  func elimUnreadAutos(f *Func) {
   365  	// Loop over all ops that affect autos taking note of which
   366  	// autos we need and also stores that we might be able to
   367  	// eliminate.
   368  	var seen ir.NameSet
   369  	var stores []*Value
   370  	for _, b := range f.Blocks {
   371  		for _, v := range b.Values {
   372  			n, ok := v.Aux.(*ir.Name)
   373  			if !ok {
   374  				continue
   375  			}
   376  			if n.Class != ir.PAUTO {
   377  				continue
   378  			}
   379  
   380  			effect := v.Op.SymEffect()
   381  			switch effect {
   382  			case SymNone, SymWrite:
   383  				// If we haven't seen the auto yet
   384  				// then this might be a store we can
   385  				// eliminate.
   386  				if !seen.Has(n) {
   387  					stores = append(stores, v)
   388  				}
   389  			default:
   390  				// Assume the auto is needed (loaded,
   391  				// has its address taken, etc.).
   392  				// Note we have to check the uses
   393  				// because dead loads haven't been
   394  				// eliminated yet.
   395  				if v.Uses > 0 {
   396  					seen.Add(n)
   397  				}
   398  			}
   399  		}
   400  	}
   401  
   402  	// Eliminate stores to unread autos.
   403  	for _, store := range stores {
   404  		n, _ := store.Aux.(*ir.Name)
   405  		if seen.Has(n) {
   406  			continue
   407  		}
   408  
   409  		// replace store with OpCopy
   410  		store.SetArgs1(store.MemoryArg())
   411  		store.Aux = nil
   412  		store.AuxInt = 0
   413  		store.Op = OpCopy
   414  	}
   415  }
   416  

View as plain text