github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/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  import (
     8  	"math"
     9  	"math/bits"
    10  )
    11  
    12  // checkFunc checks invariants of f.
    13  func checkFunc(f *Func) {
    14  	blockMark := make([]bool, f.NumBlocks())
    15  	valueMark := make([]bool, f.NumValues())
    16  
    17  	for _, b := range f.Blocks {
    18  		if blockMark[b.ID] {
    19  			f.Fatalf("block %s appears twice in %s!", b, f.Name)
    20  		}
    21  		blockMark[b.ID] = true
    22  		if b.Func != f {
    23  			f.Fatalf("%s.Func=%s, want %s", b, b.Func.Name, f.Name)
    24  		}
    25  
    26  		for i, e := range b.Preds {
    27  			if se := e.b.Succs[e.i]; se.b != b || se.i != i {
    28  				f.Fatalf("block pred/succ not crosslinked correctly %d:%s %d:%s", i, b, se.i, se.b)
    29  			}
    30  		}
    31  		for i, e := range b.Succs {
    32  			if pe := e.b.Preds[e.i]; pe.b != b || pe.i != i {
    33  				f.Fatalf("block succ/pred not crosslinked correctly %d:%s %d:%s", i, b, pe.i, pe.b)
    34  			}
    35  		}
    36  
    37  		switch b.Kind {
    38  		case BlockExit:
    39  			if len(b.Succs) != 0 {
    40  				f.Fatalf("exit block %s has successors", b)
    41  			}
    42  			if b.NumControls() != 1 {
    43  				f.Fatalf("exit block %s has no control value", b)
    44  			}
    45  			if !b.Controls[0].Type.IsMemory() {
    46  				f.Fatalf("exit block %s has non-memory control value %s", b, b.Controls[0].LongString())
    47  			}
    48  		case BlockRet:
    49  			if len(b.Succs) != 0 {
    50  				f.Fatalf("ret block %s has successors", b)
    51  			}
    52  			if b.NumControls() != 1 {
    53  				f.Fatalf("ret block %s has nil control", b)
    54  			}
    55  			if !b.Controls[0].Type.IsMemory() {
    56  				f.Fatalf("ret block %s has non-memory control value %s", b, b.Controls[0].LongString())
    57  			}
    58  		case BlockRetJmp:
    59  			if len(b.Succs) != 0 {
    60  				f.Fatalf("retjmp block %s len(Succs)==%d, want 0", b, len(b.Succs))
    61  			}
    62  			if b.NumControls() != 1 {
    63  				f.Fatalf("retjmp block %s has nil control", b)
    64  			}
    65  			if !b.Controls[0].Type.IsMemory() {
    66  				f.Fatalf("retjmp block %s has non-memory control value %s", b, b.Controls[0].LongString())
    67  			}
    68  			if b.Aux == nil {
    69  				f.Fatalf("retjmp block %s has nil Aux field", b)
    70  			}
    71  		case BlockPlain:
    72  			if len(b.Succs) != 1 {
    73  				f.Fatalf("plain block %s len(Succs)==%d, want 1", b, len(b.Succs))
    74  			}
    75  			if b.NumControls() != 0 {
    76  				f.Fatalf("plain block %s has non-nil control %s", b, b.Controls[0].LongString())
    77  			}
    78  		case BlockIf:
    79  			if len(b.Succs) != 2 {
    80  				f.Fatalf("if block %s len(Succs)==%d, want 2", b, len(b.Succs))
    81  			}
    82  			if b.NumControls() != 1 {
    83  				f.Fatalf("if block %s has no control value", b)
    84  			}
    85  			if !b.Controls[0].Type.IsBoolean() {
    86  				f.Fatalf("if block %s has non-bool control value %s", b, b.Controls[0].LongString())
    87  			}
    88  		case BlockDefer:
    89  			if len(b.Succs) != 2 {
    90  				f.Fatalf("defer block %s len(Succs)==%d, want 2", b, len(b.Succs))
    91  			}
    92  			if b.NumControls() != 1 {
    93  				f.Fatalf("defer block %s has no control value", b)
    94  			}
    95  			if !b.Controls[0].Type.IsMemory() {
    96  				f.Fatalf("defer block %s has non-memory control value %s", b, b.Controls[0].LongString())
    97  			}
    98  		case BlockFirst:
    99  			if len(b.Succs) != 2 {
   100  				f.Fatalf("plain/dead block %s len(Succs)==%d, want 2", b, len(b.Succs))
   101  			}
   102  			if b.NumControls() != 0 {
   103  				f.Fatalf("plain/dead block %s has a control value", b)
   104  			}
   105  		}
   106  		if len(b.Succs) != 2 && b.Likely != BranchUnknown {
   107  			f.Fatalf("likeliness prediction %d for block %s with %d successors", b.Likely, b, len(b.Succs))
   108  		}
   109  
   110  		for _, v := range b.Values {
   111  			// Check to make sure argument count makes sense (argLen of -1 indicates
   112  			// variable length args)
   113  			nArgs := opcodeTable[v.Op].argLen
   114  			if nArgs != -1 && int32(len(v.Args)) != nArgs {
   115  				f.Fatalf("value %s has %d args, expected %d", v.LongString(),
   116  					len(v.Args), nArgs)
   117  			}
   118  
   119  			// Check to make sure aux values make sense.
   120  			canHaveAux := false
   121  			canHaveAuxInt := false
   122  			switch opcodeTable[v.Op].auxType {
   123  			case auxNone:
   124  			case auxBool:
   125  				if v.AuxInt < 0 || v.AuxInt > 1 {
   126  					f.Fatalf("bad bool AuxInt value for %v", v)
   127  				}
   128  				canHaveAuxInt = true
   129  			case auxInt8:
   130  				if v.AuxInt != int64(int8(v.AuxInt)) {
   131  					f.Fatalf("bad int8 AuxInt value for %v", v)
   132  				}
   133  				canHaveAuxInt = true
   134  			case auxInt16:
   135  				if v.AuxInt != int64(int16(v.AuxInt)) {
   136  					f.Fatalf("bad int16 AuxInt value for %v", v)
   137  				}
   138  				canHaveAuxInt = true
   139  			case auxInt32:
   140  				if v.AuxInt != int64(int32(v.AuxInt)) {
   141  					f.Fatalf("bad int32 AuxInt value for %v", v)
   142  				}
   143  				canHaveAuxInt = true
   144  			case auxInt64, auxFloat64:
   145  				canHaveAuxInt = true
   146  			case auxInt128:
   147  				// AuxInt must be zero, so leave canHaveAuxInt set to false.
   148  			case auxFloat32:
   149  				canHaveAuxInt = true
   150  				if !isExactFloat32(v.AuxFloat()) {
   151  					f.Fatalf("value %v has an AuxInt value that is not an exact float32", v)
   152  				}
   153  			case auxString, auxSym, auxTyp, auxArchSpecific:
   154  				canHaveAux = true
   155  			case auxSymOff, auxSymValAndOff, auxTypSize:
   156  				canHaveAuxInt = true
   157  				canHaveAux = true
   158  			case auxCCop:
   159  				if _, ok := v.Aux.(Op); !ok {
   160  					f.Fatalf("bad type %T for CCop in %v", v.Aux, v)
   161  				}
   162  				canHaveAux = true
   163  			default:
   164  				f.Fatalf("unknown aux type for %s", v.Op)
   165  			}
   166  			if !canHaveAux && v.Aux != nil {
   167  				f.Fatalf("value %s has an Aux value %v but shouldn't", v.LongString(), v.Aux)
   168  			}
   169  			if !canHaveAuxInt && v.AuxInt != 0 {
   170  				f.Fatalf("value %s has an AuxInt value %d but shouldn't", v.LongString(), v.AuxInt)
   171  			}
   172  
   173  			for i, arg := range v.Args {
   174  				if arg == nil {
   175  					f.Fatalf("value %s has nil arg", v.LongString())
   176  				}
   177  				if v.Op != OpPhi {
   178  					// For non-Phi ops, memory args must be last, if present
   179  					if arg.Type.IsMemory() && i != len(v.Args)-1 {
   180  						f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1)
   181  					}
   182  				}
   183  			}
   184  
   185  			if valueMark[v.ID] {
   186  				f.Fatalf("value %s appears twice!", v.LongString())
   187  			}
   188  			valueMark[v.ID] = true
   189  
   190  			if v.Block != b {
   191  				f.Fatalf("%s.block != %s", v, b)
   192  			}
   193  			if v.Op == OpPhi && len(v.Args) != len(b.Preds) {
   194  				f.Fatalf("phi length %s does not match pred length %d for block %s", v.LongString(), len(b.Preds), b)
   195  			}
   196  
   197  			if v.Op == OpAddr {
   198  				if len(v.Args) == 0 {
   199  					f.Fatalf("no args for OpAddr %s", v.LongString())
   200  				}
   201  				if v.Args[0].Op != OpSB {
   202  					f.Fatalf("bad arg to OpAddr %v", v)
   203  				}
   204  			}
   205  
   206  			if v.Op == OpLocalAddr {
   207  				if len(v.Args) != 2 {
   208  					f.Fatalf("wrong # of args for OpLocalAddr %s", v.LongString())
   209  				}
   210  				if v.Args[0].Op != OpSP {
   211  					f.Fatalf("bad arg 0 to OpLocalAddr %v", v)
   212  				}
   213  				if !v.Args[1].Type.IsMemory() {
   214  					f.Fatalf("bad arg 1 to OpLocalAddr %v", v)
   215  				}
   216  			}
   217  
   218  			if f.RegAlloc != nil && f.Config.SoftFloat && v.Type.IsFloat() {
   219  				f.Fatalf("unexpected floating-point type %v", v.LongString())
   220  			}
   221  
   222  			// Check types.
   223  			// TODO: more type checks?
   224  			switch c := f.Config; v.Op {
   225  			case OpSP, OpSB:
   226  				if v.Type != c.Types.Uintptr {
   227  					f.Fatalf("bad %s type: want uintptr, have %s",
   228  						v.Op, v.Type.String())
   229  				}
   230  			}
   231  
   232  			// TODO: check for cycles in values
   233  		}
   234  	}
   235  
   236  	// Check to make sure all Blocks referenced are in the function.
   237  	if !blockMark[f.Entry.ID] {
   238  		f.Fatalf("entry block %v is missing", f.Entry)
   239  	}
   240  	for _, b := range f.Blocks {
   241  		for _, c := range b.Preds {
   242  			if !blockMark[c.b.ID] {
   243  				f.Fatalf("predecessor block %v for %v is missing", c, b)
   244  			}
   245  		}
   246  		for _, c := range b.Succs {
   247  			if !blockMark[c.b.ID] {
   248  				f.Fatalf("successor block %v for %v is missing", c, b)
   249  			}
   250  		}
   251  	}
   252  
   253  	if len(f.Entry.Preds) > 0 {
   254  		f.Fatalf("entry block %s of %s has predecessor(s) %v", f.Entry, f.Name, f.Entry.Preds)
   255  	}
   256  
   257  	// Check to make sure all Values referenced are in the function.
   258  	for _, b := range f.Blocks {
   259  		for _, v := range b.Values {
   260  			for i, a := range v.Args {
   261  				if !valueMark[a.ID] {
   262  					f.Fatalf("%v, arg %d of %s, is missing", a, i, v.LongString())
   263  				}
   264  			}
   265  		}
   266  		for _, c := range b.ControlValues() {
   267  			if !valueMark[c.ID] {
   268  				f.Fatalf("control value for %s is missing: %v", b, c)
   269  			}
   270  		}
   271  	}
   272  	for b := f.freeBlocks; b != nil; b = b.succstorage[0].b {
   273  		if blockMark[b.ID] {
   274  			f.Fatalf("used block b%d in free list", b.ID)
   275  		}
   276  	}
   277  	for v := f.freeValues; v != nil; v = v.argstorage[0] {
   278  		if valueMark[v.ID] {
   279  			f.Fatalf("used value v%d in free list", v.ID)
   280  		}
   281  	}
   282  
   283  	// Check to make sure all args dominate uses.
   284  	if f.RegAlloc == nil {
   285  		// Note: regalloc introduces non-dominating args.
   286  		// See TODO in regalloc.go.
   287  		sdom := f.Sdom()
   288  		for _, b := range f.Blocks {
   289  			for _, v := range b.Values {
   290  				for i, arg := range v.Args {
   291  					x := arg.Block
   292  					y := b
   293  					if v.Op == OpPhi {
   294  						y = b.Preds[i].b
   295  					}
   296  					if !domCheck(f, sdom, x, y) {
   297  						f.Fatalf("arg %d of value %s does not dominate, arg=%s", i, v.LongString(), arg.LongString())
   298  					}
   299  				}
   300  			}
   301  			for _, c := range b.ControlValues() {
   302  				if !domCheck(f, sdom, c.Block, b) {
   303  					f.Fatalf("control value %s for %s doesn't dominate", c, b)
   304  				}
   305  			}
   306  		}
   307  	}
   308  
   309  	// Check loop construction
   310  	if f.RegAlloc == nil && f.pass != nil { // non-nil pass allows better-targeted debug printing
   311  		ln := f.loopnest()
   312  		if !ln.hasIrreducible {
   313  			po := f.postorder() // use po to avoid unreachable blocks.
   314  			for _, b := range po {
   315  				for _, s := range b.Succs {
   316  					bb := s.Block()
   317  					if ln.b2l[b.ID] == nil && ln.b2l[bb.ID] != nil && bb != ln.b2l[bb.ID].header {
   318  						f.Fatalf("block %s not in loop branches to non-header block %s in loop", b.String(), bb.String())
   319  					}
   320  					if ln.b2l[b.ID] != nil && ln.b2l[bb.ID] != nil && bb != ln.b2l[bb.ID].header && !ln.b2l[b.ID].isWithinOrEq(ln.b2l[bb.ID]) {
   321  						f.Fatalf("block %s in loop branches to non-header block %s in non-containing loop", b.String(), bb.String())
   322  					}
   323  				}
   324  			}
   325  		}
   326  	}
   327  
   328  	// Check use counts
   329  	uses := make([]int32, f.NumValues())
   330  	for _, b := range f.Blocks {
   331  		for _, v := range b.Values {
   332  			for _, a := range v.Args {
   333  				uses[a.ID]++
   334  			}
   335  		}
   336  		for _, c := range b.ControlValues() {
   337  			uses[c.ID]++
   338  		}
   339  	}
   340  	for _, b := range f.Blocks {
   341  		for _, v := range b.Values {
   342  			if v.Uses != uses[v.ID] {
   343  				f.Fatalf("%s has %d uses, but has Uses=%d", v, uses[v.ID], v.Uses)
   344  			}
   345  		}
   346  	}
   347  
   348  	memCheck(f)
   349  }
   350  
   351  func memCheck(f *Func) {
   352  	// Check that if a tuple has a memory type, it is second.
   353  	for _, b := range f.Blocks {
   354  		for _, v := range b.Values {
   355  			if v.Type.IsTuple() && v.Type.FieldType(0).IsMemory() {
   356  				f.Fatalf("memory is first in a tuple: %s\n", v.LongString())
   357  			}
   358  		}
   359  	}
   360  
   361  	// Single live memory checks.
   362  	// These checks only work if there are no memory copies.
   363  	// (Memory copies introduce ambiguity about which mem value is really live.
   364  	// probably fixable, but it's easier to avoid the problem.)
   365  	// For the same reason, disable this check if some memory ops are unused.
   366  	for _, b := range f.Blocks {
   367  		for _, v := range b.Values {
   368  			if (v.Op == OpCopy || v.Uses == 0) && v.Type.IsMemory() {
   369  				return
   370  			}
   371  		}
   372  		if b != f.Entry && len(b.Preds) == 0 {
   373  			return
   374  		}
   375  	}
   376  
   377  	// Compute live memory at the end of each block.
   378  	lastmem := make([]*Value, f.NumBlocks())
   379  	ss := newSparseSet(f.NumValues())
   380  	for _, b := range f.Blocks {
   381  		// Mark overwritten memory values. Those are args of other
   382  		// ops that generate memory values.
   383  		ss.clear()
   384  		for _, v := range b.Values {
   385  			if v.Op == OpPhi || !v.Type.IsMemory() {
   386  				continue
   387  			}
   388  			if m := v.MemoryArg(); m != nil {
   389  				ss.add(m.ID)
   390  			}
   391  		}
   392  		// There should be at most one remaining unoverwritten memory value.
   393  		for _, v := range b.Values {
   394  			if !v.Type.IsMemory() {
   395  				continue
   396  			}
   397  			if ss.contains(v.ID) {
   398  				continue
   399  			}
   400  			if lastmem[b.ID] != nil {
   401  				f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], v)
   402  			}
   403  			lastmem[b.ID] = v
   404  		}
   405  		// If there is no remaining memory value, that means there was no memory update.
   406  		// Take any memory arg.
   407  		if lastmem[b.ID] == nil {
   408  			for _, v := range b.Values {
   409  				if v.Op == OpPhi {
   410  					continue
   411  				}
   412  				m := v.MemoryArg()
   413  				if m == nil {
   414  					continue
   415  				}
   416  				if lastmem[b.ID] != nil && lastmem[b.ID] != m {
   417  					f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], m)
   418  				}
   419  				lastmem[b.ID] = m
   420  			}
   421  		}
   422  	}
   423  	// Propagate last live memory through storeless blocks.
   424  	for {
   425  		changed := false
   426  		for _, b := range f.Blocks {
   427  			if lastmem[b.ID] != nil {
   428  				continue
   429  			}
   430  			for _, e := range b.Preds {
   431  				p := e.b
   432  				if lastmem[p.ID] != nil {
   433  					lastmem[b.ID] = lastmem[p.ID]
   434  					changed = true
   435  					break
   436  				}
   437  			}
   438  		}
   439  		if !changed {
   440  			break
   441  		}
   442  	}
   443  	// Check merge points.
   444  	for _, b := range f.Blocks {
   445  		for _, v := range b.Values {
   446  			if v.Op == OpPhi && v.Type.IsMemory() {
   447  				for i, a := range v.Args {
   448  					if a != lastmem[b.Preds[i].b.ID] {
   449  						f.Fatalf("inconsistent memory phi %s %d %s %s", v.LongString(), i, a, lastmem[b.Preds[i].b.ID])
   450  					}
   451  				}
   452  			}
   453  		}
   454  	}
   455  
   456  	// Check that only one memory is live at any point.
   457  	if f.scheduled {
   458  		for _, b := range f.Blocks {
   459  			var mem *Value // the current live memory in the block
   460  			for _, v := range b.Values {
   461  				if v.Op == OpPhi {
   462  					if v.Type.IsMemory() {
   463  						mem = v
   464  					}
   465  					continue
   466  				}
   467  				if mem == nil && len(b.Preds) > 0 {
   468  					// If no mem phi, take mem of any predecessor.
   469  					mem = lastmem[b.Preds[0].b.ID]
   470  				}
   471  				for _, a := range v.Args {
   472  					if a.Type.IsMemory() && a != mem {
   473  						f.Fatalf("two live mems @ %s: %s and %s", v, mem, a)
   474  					}
   475  				}
   476  				if v.Type.IsMemory() {
   477  					mem = v
   478  				}
   479  			}
   480  		}
   481  	}
   482  
   483  	// Check that after scheduling, phis are always first in the block.
   484  	if f.scheduled {
   485  		for _, b := range f.Blocks {
   486  			seenNonPhi := false
   487  			for _, v := range b.Values {
   488  				switch v.Op {
   489  				case OpPhi:
   490  					if seenNonPhi {
   491  						f.Fatalf("phi after non-phi @ %s: %s", b, v)
   492  					}
   493  				default:
   494  					seenNonPhi = true
   495  				}
   496  			}
   497  		}
   498  	}
   499  }
   500  
   501  // domCheck reports whether x dominates y (including x==y).
   502  func domCheck(f *Func, sdom SparseTree, x, y *Block) bool {
   503  	if !sdom.IsAncestorEq(f.Entry, y) {
   504  		// unreachable - ignore
   505  		return true
   506  	}
   507  	return sdom.IsAncestorEq(x, y)
   508  }
   509  
   510  // isExactFloat32 reports whether x can be exactly represented as a float32.
   511  func isExactFloat32(x float64) bool {
   512  	// Check the mantissa is in range.
   513  	if bits.TrailingZeros64(math.Float64bits(x)) < 52-23 {
   514  		return false
   515  	}
   516  	// Check the exponent is in range. The mantissa check above is sufficient for NaN values.
   517  	return math.IsNaN(x) || x == float64(float32(x))
   518  }