github.com/bir3/gocompiler@v0.3.205/src/cmd/compile/internal/ssa/nilcheck.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  	"github.com/bir3/gocompiler/src/cmd/compile/internal/ir"
     9  	"github.com/bir3/gocompiler/src/cmd/internal/src"
    10  	"github.com/bir3/gocompiler/src/internal/buildcfg"
    11  )
    12  
    13  // nilcheckelim eliminates unnecessary nil checks.
    14  // runs on machine-independent code.
    15  func nilcheckelim(f *Func) {
    16  	// A nil check is redundant if the same nil check was successful in a
    17  	// dominating block. The efficacy of this pass depends heavily on the
    18  	// efficacy of the cse pass.
    19  	sdom := f.Sdom()
    20  
    21  	// TODO: Eliminate more nil checks.
    22  	// We can recursively remove any chain of fixed offset calculations,
    23  	// i.e. struct fields and array elements, even with non-constant
    24  	// indices: x is non-nil iff x.a.b[i].c is.
    25  
    26  	type walkState int
    27  	const (
    28  		Work     walkState = iota // process nil checks and traverse to dominees
    29  		ClearPtr                  // forget the fact that ptr is nil
    30  	)
    31  
    32  	type bp struct {
    33  		block *Block // block, or nil in ClearPtr state
    34  		ptr   *Value // if non-nil, ptr that is to be cleared in ClearPtr state
    35  		op    walkState
    36  	}
    37  
    38  	work := make([]bp, 0, 256)
    39  	work = append(work, bp{block: f.Entry})
    40  
    41  	// map from value ID to bool indicating if value is known to be non-nil
    42  	// in the current dominator path being walked. This slice is updated by
    43  	// walkStates to maintain the known non-nil values.
    44  	nonNilValues := f.Cache.allocBoolSlice(f.NumValues())
    45  	defer f.Cache.freeBoolSlice(nonNilValues)
    46  
    47  	// make an initial pass identifying any non-nil values
    48  	for _, b := range f.Blocks {
    49  		for _, v := range b.Values {
    50  			// a value resulting from taking the address of a
    51  			// value, or a value constructed from an offset of a
    52  			// non-nil ptr (OpAddPtr) implies it is non-nil
    53  			// We also assume unsafe pointer arithmetic generates non-nil pointers. See #27180.
    54  			// We assume that SlicePtr is non-nil because we do a bounds check
    55  			// before the slice access (and all cap>0 slices have a non-nil ptr). See #30366.
    56  			if v.Op == OpAddr || v.Op == OpLocalAddr || v.Op == OpAddPtr || v.Op == OpOffPtr || v.Op == OpAdd32 || v.Op == OpAdd64 || v.Op == OpSub32 || v.Op == OpSub64 || v.Op == OpSlicePtr {
    57  				nonNilValues[v.ID] = true
    58  			}
    59  		}
    60  	}
    61  
    62  	for changed := true; changed; {
    63  		changed = false
    64  		for _, b := range f.Blocks {
    65  			for _, v := range b.Values {
    66  				// phis whose arguments are all non-nil
    67  				// are non-nil
    68  				if v.Op == OpPhi {
    69  					argsNonNil := true
    70  					for _, a := range v.Args {
    71  						if !nonNilValues[a.ID] {
    72  							argsNonNil = false
    73  							break
    74  						}
    75  					}
    76  					if argsNonNil {
    77  						if !nonNilValues[v.ID] {
    78  							changed = true
    79  						}
    80  						nonNilValues[v.ID] = true
    81  					}
    82  				}
    83  			}
    84  		}
    85  	}
    86  
    87  	// allocate auxiliary date structures for computing store order
    88  	sset := f.newSparseSet(f.NumValues())
    89  	defer f.retSparseSet(sset)
    90  	storeNumber := f.Cache.allocInt32Slice(f.NumValues())
    91  	defer f.Cache.freeInt32Slice(storeNumber)
    92  
    93  	// perform a depth first walk of the dominee tree
    94  	for len(work) > 0 {
    95  		node := work[len(work)-1]
    96  		work = work[:len(work)-1]
    97  
    98  		switch node.op {
    99  		case Work:
   100  			b := node.block
   101  
   102  			// First, see if we're dominated by an explicit nil check.
   103  			if len(b.Preds) == 1 {
   104  				p := b.Preds[0].b
   105  				if p.Kind == BlockIf && p.Controls[0].Op == OpIsNonNil && p.Succs[0].b == b {
   106  					if ptr := p.Controls[0].Args[0]; !nonNilValues[ptr.ID] {
   107  						nonNilValues[ptr.ID] = true
   108  						work = append(work, bp{op: ClearPtr, ptr: ptr})
   109  					}
   110  				}
   111  			}
   112  
   113  			// Next, order values in the current block w.r.t. stores.
   114  			b.Values = storeOrder(b.Values, sset, storeNumber)
   115  
   116  			pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block
   117  			pendingLines.clear()
   118  
   119  			// Next, process values in the block.
   120  			i := 0
   121  			for _, v := range b.Values {
   122  				b.Values[i] = v
   123  				i++
   124  				switch v.Op {
   125  				case OpIsNonNil:
   126  					ptr := v.Args[0]
   127  					if nonNilValues[ptr.ID] {
   128  						if v.Pos.IsStmt() == src.PosIsStmt { // Boolean true is a terrible statement boundary.
   129  							pendingLines.add(v.Pos)
   130  							v.Pos = v.Pos.WithNotStmt()
   131  						}
   132  						// This is a redundant explicit nil check.
   133  						v.reset(OpConstBool)
   134  						v.AuxInt = 1 // true
   135  					}
   136  				case OpNilCheck:
   137  					ptr := v.Args[0]
   138  					if nonNilValues[ptr.ID] {
   139  						// This is a redundant implicit nil check.
   140  						// Logging in the style of the former compiler -- and omit line 1,
   141  						// which is usually in generated code.
   142  						if f.fe.Debug_checknil() && v.Pos.Line() > 1 {
   143  							f.Warnl(v.Pos, "removed nil check")
   144  						}
   145  						if v.Pos.IsStmt() == src.PosIsStmt { // About to lose a statement boundary
   146  							pendingLines.add(v.Pos)
   147  						}
   148  						v.reset(OpUnknown)
   149  						f.freeValue(v)
   150  						i--
   151  						continue
   152  					}
   153  					// Record the fact that we know ptr is non nil, and remember to
   154  					// undo that information when this dominator subtree is done.
   155  					nonNilValues[ptr.ID] = true
   156  					work = append(work, bp{op: ClearPtr, ptr: ptr})
   157  					fallthrough // a non-eliminated nil check might be a good place for a statement boundary.
   158  				default:
   159  					if v.Pos.IsStmt() != src.PosNotStmt && !isPoorStatementOp(v.Op) && pendingLines.contains(v.Pos) {
   160  						v.Pos = v.Pos.WithIsStmt()
   161  						pendingLines.remove(v.Pos)
   162  					}
   163  				}
   164  			}
   165  			// This reduces the lost statement count in "go" by 5 (out of 500 total).
   166  			for j := 0; j < i; j++ { // is this an ordering problem?
   167  				v := b.Values[j]
   168  				if v.Pos.IsStmt() != src.PosNotStmt && !isPoorStatementOp(v.Op) && pendingLines.contains(v.Pos) {
   169  					v.Pos = v.Pos.WithIsStmt()
   170  					pendingLines.remove(v.Pos)
   171  				}
   172  			}
   173  			if pendingLines.contains(b.Pos) {
   174  				b.Pos = b.Pos.WithIsStmt()
   175  				pendingLines.remove(b.Pos)
   176  			}
   177  			b.truncateValues(i)
   178  
   179  			// Add all dominated blocks to the work list.
   180  			for w := sdom[node.block.ID].child; w != nil; w = sdom[w.ID].sibling {
   181  				work = append(work, bp{op: Work, block: w})
   182  			}
   183  
   184  		case ClearPtr:
   185  			nonNilValues[node.ptr.ID] = false
   186  			continue
   187  		}
   188  	}
   189  }
   190  
   191  // All platforms are guaranteed to fault if we load/store to anything smaller than this address.
   192  //
   193  // This should agree with minLegalPointer in the runtime.
   194  const minZeroPage = 4096
   195  
   196  // faultOnLoad is true if a load to an address below minZeroPage will trigger a SIGSEGV.
   197  var faultOnLoad = buildcfg.GOOS != "aix"
   198  
   199  // nilcheckelim2 eliminates unnecessary nil checks.
   200  // Runs after lowering and scheduling.
   201  func nilcheckelim2(f *Func) {
   202  	unnecessary := f.newSparseMap(f.NumValues()) // map from pointer that will be dereferenced to index of dereferencing value in b.Values[]
   203  	defer f.retSparseMap(unnecessary)
   204  
   205  	pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block
   206  
   207  	for _, b := range f.Blocks {
   208  		// Walk the block backwards. Find instructions that will fault if their
   209  		// input pointer is nil. Remove nil checks on those pointers, as the
   210  		// faulting instruction effectively does the nil check for free.
   211  		unnecessary.clear()
   212  		pendingLines.clear()
   213  		// Optimization: keep track of removed nilcheck with smallest index
   214  		firstToRemove := len(b.Values)
   215  		for i := len(b.Values) - 1; i >= 0; i-- {
   216  			v := b.Values[i]
   217  			if opcodeTable[v.Op].nilCheck && unnecessary.contains(v.Args[0].ID) {
   218  				if f.fe.Debug_checknil() && v.Pos.Line() > 1 {
   219  					f.Warnl(v.Pos, "removed nil check")
   220  				}
   221  				// For bug 33724, policy is that we might choose to bump an existing position
   222  				// off the faulting load/store in favor of the one from the nil check.
   223  
   224  				// Iteration order means that first nilcheck in the chain wins, others
   225  				// are bumped into the ordinary statement preservation algorithm.
   226  				u := b.Values[unnecessary.get(v.Args[0].ID)]
   227  				if !u.Pos.SameFileAndLine(v.Pos) {
   228  					if u.Pos.IsStmt() == src.PosIsStmt {
   229  						pendingLines.add(u.Pos)
   230  					}
   231  					u.Pos = v.Pos
   232  				} else if v.Pos.IsStmt() == src.PosIsStmt {
   233  					pendingLines.add(v.Pos)
   234  				}
   235  
   236  				v.reset(OpUnknown)
   237  				firstToRemove = i
   238  				continue
   239  			}
   240  			if v.Type.IsMemory() || v.Type.IsTuple() && v.Type.FieldType(1).IsMemory() {
   241  				if v.Op == OpVarLive || (v.Op == OpVarDef && !v.Aux.(*ir.Name).Type().HasPointers()) {
   242  					// These ops don't really change memory.
   243  					continue
   244  					// Note: OpVarDef requires that the defined variable not have pointers.
   245  					// We need to make sure that there's no possible faulting
   246  					// instruction between a VarDef and that variable being
   247  					// fully initialized. If there was, then anything scanning
   248  					// the stack during the handling of that fault will see
   249  					// a live but uninitialized pointer variable on the stack.
   250  					//
   251  					// If we have:
   252  					//
   253  					//   NilCheck p
   254  					//   VarDef x
   255  					//   x = *p
   256  					//
   257  					// We can't rewrite that to
   258  					//
   259  					//   VarDef x
   260  					//   NilCheck p
   261  					//   x = *p
   262  					//
   263  					// Particularly, even though *p faults on p==nil, we still
   264  					// have to do the explicit nil check before the VarDef.
   265  					// See issue #32288.
   266  				}
   267  				// This op changes memory.  Any faulting instruction after v that
   268  				// we've recorded in the unnecessary map is now obsolete.
   269  				unnecessary.clear()
   270  			}
   271  
   272  			// Find any pointers that this op is guaranteed to fault on if nil.
   273  			var ptrstore [2]*Value
   274  			ptrs := ptrstore[:0]
   275  			if opcodeTable[v.Op].faultOnNilArg0 && (faultOnLoad || v.Type.IsMemory()) {
   276  				// On AIX, only writing will fault.
   277  				ptrs = append(ptrs, v.Args[0])
   278  			}
   279  			if opcodeTable[v.Op].faultOnNilArg1 && (faultOnLoad || (v.Type.IsMemory() && v.Op != OpPPC64LoweredMove)) {
   280  				// On AIX, only writing will fault.
   281  				// LoweredMove is a special case because it's considered as a "mem" as it stores on arg0 but arg1 is accessed as a load and should be checked.
   282  				ptrs = append(ptrs, v.Args[1])
   283  			}
   284  
   285  			for _, ptr := range ptrs {
   286  				// Check to make sure the offset is small.
   287  				switch opcodeTable[v.Op].auxType {
   288  				case auxSym:
   289  					if v.Aux != nil {
   290  						continue
   291  					}
   292  				case auxSymOff:
   293  					if v.Aux != nil || v.AuxInt < 0 || v.AuxInt >= minZeroPage {
   294  						continue
   295  					}
   296  				case auxSymValAndOff:
   297  					off := ValAndOff(v.AuxInt).Off()
   298  					if v.Aux != nil || off < 0 || off >= minZeroPage {
   299  						continue
   300  					}
   301  				case auxInt32:
   302  					// Mips uses this auxType for atomic add constant. It does not affect the effective address.
   303  				case auxInt64:
   304  					// ARM uses this auxType for duffcopy/duffzero/alignment info.
   305  					// It does not affect the effective address.
   306  				case auxNone:
   307  					// offset is zero.
   308  				default:
   309  					v.Fatalf("can't handle aux %s (type %d) yet\n", v.auxString(), int(opcodeTable[v.Op].auxType))
   310  				}
   311  				// This instruction is guaranteed to fault if ptr is nil.
   312  				// Any previous nil check op is unnecessary.
   313  				unnecessary.set(ptr.ID, int32(i))
   314  			}
   315  		}
   316  		// Remove values we've clobbered with OpUnknown.
   317  		i := firstToRemove
   318  		for j := i; j < len(b.Values); j++ {
   319  			v := b.Values[j]
   320  			if v.Op != OpUnknown {
   321  				if !notStmtBoundary(v.Op) && pendingLines.contains(v.Pos) { // Late in compilation, so any remaining NotStmt values are probably okay now.
   322  					v.Pos = v.Pos.WithIsStmt()
   323  					pendingLines.remove(v.Pos)
   324  				}
   325  				b.Values[i] = v
   326  				i++
   327  			}
   328  		}
   329  
   330  		if pendingLines.contains(b.Pos) {
   331  			b.Pos = b.Pos.WithIsStmt()
   332  		}
   333  
   334  		b.truncateValues(i)
   335  
   336  		// TODO: if b.Kind == BlockPlain, start the analysis in the subsequent block to find
   337  		// more unnecessary nil checks.  Would fix test/nilptr3.go:159.
   338  	}
   339  }