github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/cmd/compile/internal/ssa/check.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 // checkFunc checks invariants of f. 8 func checkFunc(f *Func) { 9 blockMark := make([]bool, f.NumBlocks()) 10 valueMark := make([]bool, f.NumValues()) 11 12 for _, b := range f.Blocks { 13 if blockMark[b.ID] { 14 f.Fatalf("block %s appears twice in %s!", b, f.Name) 15 } 16 blockMark[b.ID] = true 17 if b.Func != f { 18 f.Fatalf("%s.Func=%s, want %s", b, b.Func.Name, f.Name) 19 } 20 21 for i, e := range b.Preds { 22 if se := e.b.Succs[e.i]; se.b != b || se.i != i { 23 f.Fatalf("block pred/succ not crosslinked correctly %d:%s %d:%s", i, b, se.i, se.b) 24 } 25 } 26 for i, e := range b.Succs { 27 if pe := e.b.Preds[e.i]; pe.b != b || pe.i != i { 28 f.Fatalf("block succ/pred not crosslinked correctly %d:%s %d:%s", i, b, pe.i, pe.b) 29 } 30 } 31 32 switch b.Kind { 33 case BlockExit: 34 if len(b.Succs) != 0 { 35 f.Fatalf("exit block %s has successors", b) 36 } 37 if b.Control == nil { 38 f.Fatalf("exit block %s has no control value", b) 39 } 40 if !b.Control.Type.IsMemory() { 41 f.Fatalf("exit block %s has non-memory control value %s", b, b.Control.LongString()) 42 } 43 case BlockRet: 44 if len(b.Succs) != 0 { 45 f.Fatalf("ret block %s has successors", b) 46 } 47 if b.Control == nil { 48 f.Fatalf("ret block %s has nil control", b) 49 } 50 if !b.Control.Type.IsMemory() { 51 f.Fatalf("ret block %s has non-memory control value %s", b, b.Control.LongString()) 52 } 53 case BlockRetJmp: 54 if len(b.Succs) != 0 { 55 f.Fatalf("retjmp block %s len(Succs)==%d, want 0", b, len(b.Succs)) 56 } 57 if b.Control == nil { 58 f.Fatalf("retjmp block %s has nil control", b) 59 } 60 if !b.Control.Type.IsMemory() { 61 f.Fatalf("retjmp block %s has non-memory control value %s", b, b.Control.LongString()) 62 } 63 if b.Aux == nil { 64 f.Fatalf("retjmp block %s has nil Aux field", b) 65 } 66 case BlockPlain: 67 if len(b.Succs) != 1 { 68 f.Fatalf("plain block %s len(Succs)==%d, want 1", b, len(b.Succs)) 69 } 70 if b.Control != nil { 71 f.Fatalf("plain block %s has non-nil control %s", b, b.Control.LongString()) 72 } 73 case BlockIf: 74 if len(b.Succs) != 2 { 75 f.Fatalf("if block %s len(Succs)==%d, want 2", b, len(b.Succs)) 76 } 77 if b.Control == nil { 78 f.Fatalf("if block %s has no control value", b) 79 } 80 if !b.Control.Type.IsBoolean() { 81 f.Fatalf("if block %s has non-bool control value %s", b, b.Control.LongString()) 82 } 83 case BlockCall: 84 if len(b.Succs) != 1 { 85 f.Fatalf("call block %s len(Succs)==%d, want 1", b, len(b.Succs)) 86 } 87 if b.Control == nil { 88 f.Fatalf("call block %s has no control value", b) 89 } 90 if !b.Control.Type.IsMemory() { 91 f.Fatalf("call block %s has non-memory control value %s", b, b.Control.LongString()) 92 } 93 case BlockDefer: 94 if len(b.Succs) != 2 { 95 f.Fatalf("defer block %s len(Succs)==%d, want 2", b, len(b.Succs)) 96 } 97 if b.Control == nil { 98 f.Fatalf("defer block %s has no control value", b) 99 } 100 if !b.Control.Type.IsMemory() { 101 f.Fatalf("defer block %s has non-memory control value %s", b, b.Control.LongString()) 102 } 103 case BlockCheck: 104 if len(b.Succs) != 1 { 105 f.Fatalf("check block %s len(Succs)==%d, want 1", b, len(b.Succs)) 106 } 107 if b.Control == nil { 108 f.Fatalf("check block %s has no control value", b) 109 } 110 if !b.Control.Type.IsVoid() { 111 f.Fatalf("check block %s has non-void control value %s", b, b.Control.LongString()) 112 } 113 case BlockFirst: 114 if len(b.Succs) != 2 { 115 f.Fatalf("plain/dead block %s len(Succs)==%d, want 2", b, len(b.Succs)) 116 } 117 if b.Control != nil { 118 f.Fatalf("plain/dead block %s has a control value", b) 119 } 120 } 121 if len(b.Succs) > 2 && b.Likely != BranchUnknown { 122 f.Fatalf("likeliness prediction %d for block %s with %d successors", b.Likely, b, len(b.Succs)) 123 } 124 125 for _, v := range b.Values { 126 // Check to make sure argument count makes sense (argLen of -1 indicates 127 // variable length args) 128 nArgs := opcodeTable[v.Op].argLen 129 if nArgs != -1 && int32(len(v.Args)) != nArgs { 130 f.Fatalf("value %s has %d args, expected %d", v.LongString(), 131 len(v.Args), nArgs) 132 } 133 134 // Check to make sure aux values make sense. 135 canHaveAux := false 136 canHaveAuxInt := false 137 switch opcodeTable[v.Op].auxType { 138 case auxNone: 139 case auxBool: 140 if v.AuxInt < 0 || v.AuxInt > 1 { 141 f.Fatalf("bad bool AuxInt value for %v", v) 142 } 143 canHaveAuxInt = true 144 case auxInt8: 145 if v.AuxInt != int64(int8(v.AuxInt)) { 146 f.Fatalf("bad int8 AuxInt value for %v", v) 147 } 148 canHaveAuxInt = true 149 case auxInt16: 150 if v.AuxInt != int64(int16(v.AuxInt)) { 151 f.Fatalf("bad int16 AuxInt value for %v", v) 152 } 153 canHaveAuxInt = true 154 case auxInt32: 155 if v.AuxInt != int64(int32(v.AuxInt)) { 156 f.Fatalf("bad int32 AuxInt value for %v", v) 157 } 158 canHaveAuxInt = true 159 case auxInt64, auxFloat64: 160 canHaveAuxInt = true 161 case auxInt128: 162 // AuxInt must be zero, so leave canHaveAuxInt set to false. 163 case auxFloat32: 164 canHaveAuxInt = true 165 if !isExactFloat32(v) { 166 f.Fatalf("value %v has an AuxInt value that is not an exact float32", v) 167 } 168 case auxString, auxSym: 169 canHaveAux = true 170 case auxSymOff, auxSymValAndOff: 171 canHaveAuxInt = true 172 canHaveAux = true 173 case auxSymInt32: 174 if v.AuxInt != int64(int32(v.AuxInt)) { 175 f.Fatalf("bad int32 AuxInt value for %v", v) 176 } 177 canHaveAuxInt = true 178 canHaveAux = true 179 default: 180 f.Fatalf("unknown aux type for %s", v.Op) 181 } 182 if !canHaveAux && v.Aux != nil { 183 f.Fatalf("value %s has an Aux value %v but shouldn't", v.LongString(), v.Aux) 184 } 185 if !canHaveAuxInt && v.AuxInt != 0 { 186 f.Fatalf("value %s has an AuxInt value %d but shouldn't", v.LongString(), v.AuxInt) 187 } 188 189 for i, arg := range v.Args { 190 if arg == nil { 191 f.Fatalf("value %s has nil arg", v.LongString()) 192 } 193 if v.Op != OpPhi { 194 // For non-Phi ops, memory args must be last, if present 195 if arg.Type.IsMemory() && i != len(v.Args)-1 { 196 f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1) 197 } 198 } 199 } 200 201 if valueMark[v.ID] { 202 f.Fatalf("value %s appears twice!", v.LongString()) 203 } 204 valueMark[v.ID] = true 205 206 if v.Block != b { 207 f.Fatalf("%s.block != %s", v, b) 208 } 209 if v.Op == OpPhi && len(v.Args) != len(b.Preds) { 210 f.Fatalf("phi length %s does not match pred length %d for block %s", v.LongString(), len(b.Preds), b) 211 } 212 213 if v.Op == OpAddr { 214 if len(v.Args) == 0 { 215 f.Fatalf("no args for OpAddr %s", v.LongString()) 216 } 217 if v.Args[0].Op != OpSP && v.Args[0].Op != OpSB { 218 f.Fatalf("bad arg to OpAddr %v", v) 219 } 220 } 221 222 // TODO: check for cycles in values 223 // TODO: check type 224 } 225 } 226 227 // Check to make sure all Blocks referenced are in the function. 228 if !blockMark[f.Entry.ID] { 229 f.Fatalf("entry block %v is missing", f.Entry) 230 } 231 for _, b := range f.Blocks { 232 for _, c := range b.Preds { 233 if !blockMark[c.b.ID] { 234 f.Fatalf("predecessor block %v for %v is missing", c, b) 235 } 236 } 237 for _, c := range b.Succs { 238 if !blockMark[c.b.ID] { 239 f.Fatalf("successor block %v for %v is missing", c, b) 240 } 241 } 242 } 243 244 if len(f.Entry.Preds) > 0 { 245 f.Fatalf("entry block %s of %s has predecessor(s) %v", f.Entry, f.Name, f.Entry.Preds) 246 } 247 248 // Check to make sure all Values referenced are in the function. 249 for _, b := range f.Blocks { 250 for _, v := range b.Values { 251 for i, a := range v.Args { 252 if !valueMark[a.ID] { 253 f.Fatalf("%v, arg %d of %s, is missing", a, i, v.LongString()) 254 } 255 } 256 } 257 if b.Control != nil && !valueMark[b.Control.ID] { 258 f.Fatalf("control value for %s is missing: %v", b, b.Control) 259 } 260 } 261 for b := f.freeBlocks; b != nil; b = b.succstorage[0].b { 262 if blockMark[b.ID] { 263 f.Fatalf("used block b%d in free list", b.ID) 264 } 265 } 266 for v := f.freeValues; v != nil; v = v.argstorage[0] { 267 if valueMark[v.ID] { 268 f.Fatalf("used value v%d in free list", v.ID) 269 } 270 } 271 272 // Check to make sure all args dominate uses. 273 if f.RegAlloc == nil { 274 // Note: regalloc introduces non-dominating args. 275 // See TODO in regalloc.go. 276 idom := dominators(f) 277 sdom := newSparseTree(f, idom) 278 for _, b := range f.Blocks { 279 for _, v := range b.Values { 280 for i, arg := range v.Args { 281 x := arg.Block 282 y := b 283 if v.Op == OpPhi { 284 y = b.Preds[i].b 285 } 286 if !domCheck(f, sdom, x, y) { 287 f.Fatalf("arg %d of value %s does not dominate, arg=%s", i, v.LongString(), arg.LongString()) 288 } 289 } 290 } 291 if b.Control != nil && !domCheck(f, sdom, b.Control.Block, b) { 292 f.Fatalf("control value %s for %s doesn't dominate", b.Control, b) 293 } 294 } 295 } 296 297 // Check use counts 298 uses := make([]int32, f.NumValues()) 299 for _, b := range f.Blocks { 300 for _, v := range b.Values { 301 for _, a := range v.Args { 302 uses[a.ID]++ 303 } 304 } 305 if b.Control != nil { 306 uses[b.Control.ID]++ 307 } 308 } 309 for _, b := range f.Blocks { 310 for _, v := range b.Values { 311 if v.Uses != uses[v.ID] { 312 f.Fatalf("%s has %d uses, but has Uses=%d", v, uses[v.ID], v.Uses) 313 } 314 } 315 } 316 } 317 318 // domCheck reports whether x dominates y (including x==y). 319 func domCheck(f *Func, sdom SparseTree, x, y *Block) bool { 320 if !sdom.isAncestorEq(f.Entry, y) { 321 // unreachable - ignore 322 return true 323 } 324 return sdom.isAncestorEq(x, y) 325 } 326 327 // isExactFloat32 reoprts whether v has an AuxInt that can be exactly represented as a float32. 328 func isExactFloat32(v *Value) bool { 329 return v.AuxFloat() == float64(float32(v.AuxFloat())) 330 }