github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/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 BlockDefer:
    84  			if len(b.Succs) != 2 {
    85  				f.Fatalf("defer block %s len(Succs)==%d, want 2", b, len(b.Succs))
    86  			}
    87  			if b.Control == nil {
    88  				f.Fatalf("defer block %s has no control value", b)
    89  			}
    90  			if !b.Control.Type.IsMemory() {
    91  				f.Fatalf("defer block %s has non-memory control value %s", b, b.Control.LongString())
    92  			}
    93  		case BlockFirst:
    94  			if len(b.Succs) != 2 {
    95  				f.Fatalf("plain/dead block %s len(Succs)==%d, want 2", b, len(b.Succs))
    96  			}
    97  			if b.Control != nil {
    98  				f.Fatalf("plain/dead block %s has a control value", b)
    99  			}
   100  		}
   101  		if len(b.Succs) > 2 && b.Likely != BranchUnknown {
   102  			f.Fatalf("likeliness prediction %d for block %s with %d successors", b.Likely, b, len(b.Succs))
   103  		}
   104  
   105  		for _, v := range b.Values {
   106  			// Check to make sure argument count makes sense (argLen of -1 indicates
   107  			// variable length args)
   108  			nArgs := opcodeTable[v.Op].argLen
   109  			if nArgs != -1 && int32(len(v.Args)) != nArgs {
   110  				f.Fatalf("value %s has %d args, expected %d", v.LongString(),
   111  					len(v.Args), nArgs)
   112  			}
   113  
   114  			// Check to make sure aux values make sense.
   115  			canHaveAux := false
   116  			canHaveAuxInt := false
   117  			switch opcodeTable[v.Op].auxType {
   118  			case auxNone:
   119  			case auxBool:
   120  				if v.AuxInt < 0 || v.AuxInt > 1 {
   121  					f.Fatalf("bad bool AuxInt value for %v", v)
   122  				}
   123  				canHaveAuxInt = true
   124  			case auxInt8:
   125  				if v.AuxInt != int64(int8(v.AuxInt)) {
   126  					f.Fatalf("bad int8 AuxInt value for %v", v)
   127  				}
   128  				canHaveAuxInt = true
   129  			case auxInt16:
   130  				if v.AuxInt != int64(int16(v.AuxInt)) {
   131  					f.Fatalf("bad int16 AuxInt value for %v", v)
   132  				}
   133  				canHaveAuxInt = true
   134  			case auxInt32:
   135  				if v.AuxInt != int64(int32(v.AuxInt)) {
   136  					f.Fatalf("bad int32 AuxInt value for %v", v)
   137  				}
   138  				canHaveAuxInt = true
   139  			case auxInt64, auxFloat64:
   140  				canHaveAuxInt = true
   141  			case auxInt128:
   142  				// AuxInt must be zero, so leave canHaveAuxInt set to false.
   143  			case auxFloat32:
   144  				canHaveAuxInt = true
   145  				if !isExactFloat32(v) {
   146  					f.Fatalf("value %v has an AuxInt value that is not an exact float32", v)
   147  				}
   148  			case auxSizeAndAlign:
   149  				canHaveAuxInt = true
   150  			case auxString, auxSym:
   151  				canHaveAux = true
   152  			case auxSymOff, auxSymValAndOff, auxSymSizeAndAlign:
   153  				canHaveAuxInt = true
   154  				canHaveAux = true
   155  			case auxSymInt32:
   156  				if v.AuxInt != int64(int32(v.AuxInt)) {
   157  					f.Fatalf("bad int32 AuxInt value for %v", v)
   158  				}
   159  				canHaveAuxInt = true
   160  				canHaveAux = true
   161  			default:
   162  				f.Fatalf("unknown aux type for %s", v.Op)
   163  			}
   164  			if !canHaveAux && v.Aux != nil {
   165  				f.Fatalf("value %s has an Aux value %v but shouldn't", v.LongString(), v.Aux)
   166  			}
   167  			if !canHaveAuxInt && v.AuxInt != 0 {
   168  				f.Fatalf("value %s has an AuxInt value %d but shouldn't", v.LongString(), v.AuxInt)
   169  			}
   170  
   171  			for i, arg := range v.Args {
   172  				if arg == nil {
   173  					f.Fatalf("value %s has nil arg", v.LongString())
   174  				}
   175  				if v.Op != OpPhi {
   176  					// For non-Phi ops, memory args must be last, if present
   177  					if arg.Type.IsMemory() && i != len(v.Args)-1 {
   178  						f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1)
   179  					}
   180  				}
   181  			}
   182  
   183  			if valueMark[v.ID] {
   184  				f.Fatalf("value %s appears twice!", v.LongString())
   185  			}
   186  			valueMark[v.ID] = true
   187  
   188  			if v.Block != b {
   189  				f.Fatalf("%s.block != %s", v, b)
   190  			}
   191  			if v.Op == OpPhi && len(v.Args) != len(b.Preds) {
   192  				f.Fatalf("phi length %s does not match pred length %d for block %s", v.LongString(), len(b.Preds), b)
   193  			}
   194  
   195  			if v.Op == OpAddr {
   196  				if len(v.Args) == 0 {
   197  					f.Fatalf("no args for OpAddr %s", v.LongString())
   198  				}
   199  				if v.Args[0].Op != OpSP && v.Args[0].Op != OpSB {
   200  					f.Fatalf("bad arg to OpAddr %v", v)
   201  				}
   202  			}
   203  
   204  			// TODO: check for cycles in values
   205  			// TODO: check type
   206  		}
   207  	}
   208  
   209  	// Check to make sure all Blocks referenced are in the function.
   210  	if !blockMark[f.Entry.ID] {
   211  		f.Fatalf("entry block %v is missing", f.Entry)
   212  	}
   213  	for _, b := range f.Blocks {
   214  		for _, c := range b.Preds {
   215  			if !blockMark[c.b.ID] {
   216  				f.Fatalf("predecessor block %v for %v is missing", c, b)
   217  			}
   218  		}
   219  		for _, c := range b.Succs {
   220  			if !blockMark[c.b.ID] {
   221  				f.Fatalf("successor block %v for %v is missing", c, b)
   222  			}
   223  		}
   224  	}
   225  
   226  	if len(f.Entry.Preds) > 0 {
   227  		f.Fatalf("entry block %s of %s has predecessor(s) %v", f.Entry, f.Name, f.Entry.Preds)
   228  	}
   229  
   230  	// Check to make sure all Values referenced are in the function.
   231  	for _, b := range f.Blocks {
   232  		for _, v := range b.Values {
   233  			for i, a := range v.Args {
   234  				if !valueMark[a.ID] {
   235  					f.Fatalf("%v, arg %d of %s, is missing", a, i, v.LongString())
   236  				}
   237  			}
   238  		}
   239  		if b.Control != nil && !valueMark[b.Control.ID] {
   240  			f.Fatalf("control value for %s is missing: %v", b, b.Control)
   241  		}
   242  	}
   243  	for b := f.freeBlocks; b != nil; b = b.succstorage[0].b {
   244  		if blockMark[b.ID] {
   245  			f.Fatalf("used block b%d in free list", b.ID)
   246  		}
   247  	}
   248  	for v := f.freeValues; v != nil; v = v.argstorage[0] {
   249  		if valueMark[v.ID] {
   250  			f.Fatalf("used value v%d in free list", v.ID)
   251  		}
   252  	}
   253  
   254  	// Check to make sure all args dominate uses.
   255  	if f.RegAlloc == nil {
   256  		// Note: regalloc introduces non-dominating args.
   257  		// See TODO in regalloc.go.
   258  		sdom := f.sdom()
   259  		for _, b := range f.Blocks {
   260  			for _, v := range b.Values {
   261  				for i, arg := range v.Args {
   262  					x := arg.Block
   263  					y := b
   264  					if v.Op == OpPhi {
   265  						y = b.Preds[i].b
   266  					}
   267  					if !domCheck(f, sdom, x, y) {
   268  						f.Fatalf("arg %d of value %s does not dominate, arg=%s", i, v.LongString(), arg.LongString())
   269  					}
   270  				}
   271  			}
   272  			if b.Control != nil && !domCheck(f, sdom, b.Control.Block, b) {
   273  				f.Fatalf("control value %s for %s doesn't dominate", b.Control, b)
   274  			}
   275  		}
   276  	}
   277  
   278  	// Check use counts
   279  	uses := make([]int32, f.NumValues())
   280  	for _, b := range f.Blocks {
   281  		for _, v := range b.Values {
   282  			for _, a := range v.Args {
   283  				uses[a.ID]++
   284  			}
   285  		}
   286  		if b.Control != nil {
   287  			uses[b.Control.ID]++
   288  		}
   289  	}
   290  	for _, b := range f.Blocks {
   291  		for _, v := range b.Values {
   292  			if v.Uses != uses[v.ID] {
   293  				f.Fatalf("%s has %d uses, but has Uses=%d", v, uses[v.ID], v.Uses)
   294  			}
   295  		}
   296  	}
   297  }
   298  
   299  // domCheck reports whether x dominates y (including x==y).
   300  func domCheck(f *Func, sdom SparseTree, x, y *Block) bool {
   301  	if !sdom.isAncestorEq(f.Entry, y) {
   302  		// unreachable - ignore
   303  		return true
   304  	}
   305  	return sdom.isAncestorEq(x, y)
   306  }
   307  
   308  // isExactFloat32 reoprts whether v has an AuxInt that can be exactly represented as a float32.
   309  func isExactFloat32(v *Value) bool {
   310  	return v.AuxFloat() == float64(float32(v.AuxFloat()))
   311  }