github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/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 // nilcheckelim eliminates unnecessary nil checks. 8 // runs on machine-independent code. 9 func nilcheckelim(f *Func) { 10 // A nil check is redundant if the same nil check was successful in a 11 // dominating block. The efficacy of this pass depends heavily on the 12 // efficacy of the cse pass. 13 sdom := f.sdom() 14 15 // TODO: Eliminate more nil checks. 16 // We can recursively remove any chain of fixed offset calculations, 17 // i.e. struct fields and array elements, even with non-constant 18 // indices: x is non-nil iff x.a.b[i].c is. 19 20 type walkState int 21 const ( 22 Work walkState = iota // process nil checks and traverse to dominees 23 ClearPtr // forget the fact that ptr is nil 24 ) 25 26 type bp struct { 27 block *Block // block, or nil in ClearPtr state 28 ptr *Value // if non-nil, ptr that is to be cleared in ClearPtr state 29 op walkState 30 } 31 32 work := make([]bp, 0, 256) 33 work = append(work, bp{block: f.Entry}) 34 35 // map from value ID to bool indicating if value is known to be non-nil 36 // in the current dominator path being walked. This slice is updated by 37 // walkStates to maintain the known non-nil values. 38 nonNilValues := make([]bool, f.NumValues()) 39 40 // make an initial pass identifying any non-nil values 41 for _, b := range f.Blocks { 42 for _, v := range b.Values { 43 // a value resulting from taking the address of a 44 // value, or a value constructed from an offset of a 45 // non-nil ptr (OpAddPtr) implies it is non-nil 46 if v.Op == OpAddr || v.Op == OpAddPtr { 47 nonNilValues[v.ID] = true 48 } 49 } 50 } 51 52 for changed := true; changed; { 53 changed = false 54 for _, b := range f.Blocks { 55 for _, v := range b.Values { 56 // phis whose arguments are all non-nil 57 // are non-nil 58 if v.Op == OpPhi { 59 argsNonNil := true 60 for _, a := range v.Args { 61 if !nonNilValues[a.ID] { 62 argsNonNil = false 63 break 64 } 65 } 66 if argsNonNil { 67 if !nonNilValues[v.ID] { 68 changed = true 69 } 70 nonNilValues[v.ID] = true 71 } 72 } 73 } 74 } 75 } 76 77 // allocate auxiliary date structures for computing store order 78 sset := f.newSparseSet(f.NumValues()) 79 defer f.retSparseSet(sset) 80 storeNumber := make([]int32, f.NumValues()) 81 82 // perform a depth first walk of the dominee tree 83 for len(work) > 0 { 84 node := work[len(work)-1] 85 work = work[:len(work)-1] 86 87 switch node.op { 88 case Work: 89 b := node.block 90 91 // First, see if we're dominated by an explicit nil check. 92 if len(b.Preds) == 1 { 93 p := b.Preds[0].b 94 if p.Kind == BlockIf && p.Control.Op == OpIsNonNil && p.Succs[0].b == b { 95 ptr := p.Control.Args[0] 96 if !nonNilValues[ptr.ID] { 97 nonNilValues[ptr.ID] = true 98 work = append(work, bp{op: ClearPtr, ptr: ptr}) 99 } 100 } 101 } 102 103 // Next, order values in the current block w.r.t. stores. 104 b.Values = storeOrder(b.Values, sset, storeNumber) 105 106 // Next, process values in the block. 107 i := 0 108 for _, v := range b.Values { 109 b.Values[i] = v 110 i++ 111 switch v.Op { 112 case OpIsNonNil: 113 ptr := v.Args[0] 114 if nonNilValues[ptr.ID] { 115 // This is a redundant explicit nil check. 116 v.reset(OpConstBool) 117 v.AuxInt = 1 // true 118 } 119 case OpNilCheck: 120 ptr := v.Args[0] 121 if nonNilValues[ptr.ID] { 122 // This is a redundant implicit nil check. 123 // Logging in the style of the former compiler -- and omit line 1, 124 // which is usually in generated code. 125 if f.fe.Debug_checknil() && v.Pos.Line() > 1 { 126 f.Warnl(v.Pos, "removed nil check") 127 } 128 v.reset(OpUnknown) 129 // TODO: f.freeValue(v) 130 i-- 131 continue 132 } 133 // Record the fact that we know ptr is non nil, and remember to 134 // undo that information when this dominator subtree is done. 135 nonNilValues[ptr.ID] = true 136 work = append(work, bp{op: ClearPtr, ptr: ptr}) 137 } 138 } 139 for j := i; j < len(b.Values); j++ { 140 b.Values[j] = nil 141 } 142 b.Values = b.Values[:i] 143 144 // Add all dominated blocks to the work list. 145 for w := sdom[node.block.ID].child; w != nil; w = sdom[w.ID].sibling { 146 work = append(work, bp{op: Work, block: w}) 147 } 148 149 case ClearPtr: 150 nonNilValues[node.ptr.ID] = false 151 continue 152 } 153 } 154 } 155 156 // All platforms are guaranteed to fault if we load/store to anything smaller than this address. 157 // 158 // This should agree with minLegalPointer in the runtime. 159 const minZeroPage = 4096 160 161 // nilcheckelim2 eliminates unnecessary nil checks. 162 // Runs after lowering and scheduling. 163 func nilcheckelim2(f *Func) { 164 unnecessary := f.newSparseSet(f.NumValues()) 165 defer f.retSparseSet(unnecessary) 166 for _, b := range f.Blocks { 167 // Walk the block backwards. Find instructions that will fault if their 168 // input pointer is nil. Remove nil checks on those pointers, as the 169 // faulting instruction effectively does the nil check for free. 170 unnecessary.clear() 171 for i := len(b.Values) - 1; i >= 0; i-- { 172 v := b.Values[i] 173 if opcodeTable[v.Op].nilCheck && unnecessary.contains(v.Args[0].ID) { 174 if f.fe.Debug_checknil() && v.Pos.Line() > 1 { 175 f.Warnl(v.Pos, "removed nil check") 176 } 177 v.reset(OpUnknown) 178 continue 179 } 180 if v.Type.IsMemory() || v.Type.IsTuple() && v.Type.FieldType(1).IsMemory() { 181 if v.Op == OpVarDef || v.Op == OpVarKill || v.Op == OpVarLive { 182 // These ops don't really change memory. 183 continue 184 } 185 // This op changes memory. Any faulting instruction after v that 186 // we've recorded in the unnecessary map is now obsolete. 187 unnecessary.clear() 188 } 189 190 // Find any pointers that this op is guaranteed to fault on if nil. 191 var ptrstore [2]*Value 192 ptrs := ptrstore[:0] 193 if opcodeTable[v.Op].faultOnNilArg0 { 194 ptrs = append(ptrs, v.Args[0]) 195 } 196 if opcodeTable[v.Op].faultOnNilArg1 { 197 ptrs = append(ptrs, v.Args[1]) 198 } 199 for _, ptr := range ptrs { 200 // Check to make sure the offset is small. 201 switch opcodeTable[v.Op].auxType { 202 case auxSymOff: 203 if v.Aux != nil || v.AuxInt < 0 || v.AuxInt >= minZeroPage { 204 continue 205 } 206 case auxSymValAndOff: 207 off := ValAndOff(v.AuxInt).Off() 208 if v.Aux != nil || off < 0 || off >= minZeroPage { 209 continue 210 } 211 case auxInt32: 212 // Mips uses this auxType for atomic add constant. It does not affect the effective address. 213 case auxInt64: 214 // ARM uses this auxType for duffcopy/duffzero/alignment info. 215 // It does not affect the effective address. 216 case auxNone: 217 // offset is zero. 218 default: 219 v.Fatalf("can't handle aux %s (type %d) yet\n", v.auxString(), int(opcodeTable[v.Op].auxType)) 220 } 221 // This instruction is guaranteed to fault if ptr is nil. 222 // Any previous nil check op is unnecessary. 223 unnecessary.add(ptr.ID) 224 } 225 } 226 // Remove values we've clobbered with OpUnknown. 227 i := 0 228 for _, v := range b.Values { 229 if v.Op != OpUnknown { 230 b.Values[i] = v 231 i++ 232 } 233 } 234 for j := i; j < len(b.Values); j++ { 235 b.Values[j] = nil 236 } 237 b.Values = b.Values[:i] 238 239 // TODO: if b.Kind == BlockPlain, start the analysis in the subsequent block to find 240 // more unnecessary nil checks. Would fix test/nilptr3_ssa.go:157. 241 } 242 }