1
2
3
4
5 package ssa
6
7 import (
8 "cmd/compile/internal/ir"
9 "cmd/internal/obj/s390x"
10 "math"
11 "math/bits"
12 )
13
14
15 func checkFunc(f *Func) {
16 blockMark := make([]bool, f.NumBlocks())
17 valueMark := make([]bool, f.NumValues())
18
19 for _, b := range f.Blocks {
20 if blockMark[b.ID] {
21 f.Fatalf("block %s appears twice in %s!", b, f.Name)
22 }
23 blockMark[b.ID] = true
24 if b.Func != f {
25 f.Fatalf("%s.Func=%s, want %s", b, b.Func.Name, f.Name)
26 }
27
28 for i, e := range b.Preds {
29 if se := e.b.Succs[e.i]; se.b != b || se.i != i {
30 f.Fatalf("block pred/succ not crosslinked correctly %d:%s %d:%s", i, b, se.i, se.b)
31 }
32 }
33 for i, e := range b.Succs {
34 if pe := e.b.Preds[e.i]; pe.b != b || pe.i != i {
35 f.Fatalf("block succ/pred not crosslinked correctly %d:%s %d:%s", i, b, pe.i, pe.b)
36 }
37 }
38
39 switch b.Kind {
40 case BlockExit:
41 if len(b.Succs) != 0 {
42 f.Fatalf("exit block %s has successors", b)
43 }
44 if b.NumControls() != 1 {
45 f.Fatalf("exit block %s has no control value", b)
46 }
47 if !b.Controls[0].Type.IsMemory() {
48 f.Fatalf("exit block %s has non-memory control value %s", b, b.Controls[0].LongString())
49 }
50 case BlockRet:
51 if len(b.Succs) != 0 {
52 f.Fatalf("ret block %s has successors", b)
53 }
54 if b.NumControls() != 1 {
55 f.Fatalf("ret block %s has nil control", b)
56 }
57 if !b.Controls[0].Type.IsMemory() {
58 f.Fatalf("ret block %s has non-memory control value %s", b, b.Controls[0].LongString())
59 }
60 case BlockRetJmp:
61 if len(b.Succs) != 0 {
62 f.Fatalf("retjmp block %s len(Succs)==%d, want 0", b, len(b.Succs))
63 }
64 if b.NumControls() != 1 {
65 f.Fatalf("retjmp block %s has nil control", b)
66 }
67 if !b.Controls[0].Type.IsMemory() {
68 f.Fatalf("retjmp block %s has non-memory control value %s", b, b.Controls[0].LongString())
69 }
70 case BlockPlain:
71 if len(b.Succs) != 1 {
72 f.Fatalf("plain block %s len(Succs)==%d, want 1", b, len(b.Succs))
73 }
74 if b.NumControls() != 0 {
75 f.Fatalf("plain block %s has non-nil control %s", b, b.Controls[0].LongString())
76 }
77 case BlockIf:
78 if len(b.Succs) != 2 {
79 f.Fatalf("if block %s len(Succs)==%d, want 2", b, len(b.Succs))
80 }
81 if b.NumControls() != 1 {
82 f.Fatalf("if block %s has no control value", b)
83 }
84 if !b.Controls[0].Type.IsBoolean() {
85 f.Fatalf("if block %s has non-bool control value %s", b, b.Controls[0].LongString())
86 }
87 case BlockDefer:
88 if len(b.Succs) != 2 {
89 f.Fatalf("defer block %s len(Succs)==%d, want 2", b, len(b.Succs))
90 }
91 if b.NumControls() != 1 {
92 f.Fatalf("defer block %s has no control value", b)
93 }
94 if !b.Controls[0].Type.IsMemory() {
95 f.Fatalf("defer block %s has non-memory control value %s", b, b.Controls[0].LongString())
96 }
97 case BlockFirst:
98 if len(b.Succs) != 2 {
99 f.Fatalf("plain/dead block %s len(Succs)==%d, want 2", b, len(b.Succs))
100 }
101 if b.NumControls() != 0 {
102 f.Fatalf("plain/dead block %s has a control value", b)
103 }
104 case BlockJumpTable:
105 if b.NumControls() != 1 {
106 f.Fatalf("jumpTable block %s has no control value", b)
107 }
108 }
109 if len(b.Succs) != 2 && b.Likely != BranchUnknown {
110 f.Fatalf("likeliness prediction %d for block %s with %d successors", b.Likely, b, len(b.Succs))
111 }
112
113 for _, v := range b.Values {
114
115
116 nArgs := opcodeTable[v.Op].argLen
117 if nArgs != -1 && int32(len(v.Args)) != nArgs {
118 f.Fatalf("value %s has %d args, expected %d", v.LongString(),
119 len(v.Args), nArgs)
120 }
121
122
123 canHaveAux := false
124 canHaveAuxInt := false
125
126 switch opcodeTable[v.Op].auxType {
127 case auxNone:
128 case auxBool:
129 if v.AuxInt < 0 || v.AuxInt > 1 {
130 f.Fatalf("bad bool AuxInt value for %v", v)
131 }
132 canHaveAuxInt = true
133 case auxInt8:
134 if v.AuxInt != int64(int8(v.AuxInt)) {
135 f.Fatalf("bad int8 AuxInt value for %v", v)
136 }
137 canHaveAuxInt = true
138 case auxInt16:
139 if v.AuxInt != int64(int16(v.AuxInt)) {
140 f.Fatalf("bad int16 AuxInt value for %v", v)
141 }
142 canHaveAuxInt = true
143 case auxInt32:
144 if v.AuxInt != int64(int32(v.AuxInt)) {
145 f.Fatalf("bad int32 AuxInt value for %v", v)
146 }
147 canHaveAuxInt = true
148 case auxInt64, auxARM64BitField, auxARM64ConditionalParams:
149 canHaveAuxInt = true
150 case auxInt128:
151
152 case auxUInt8:
153
154 if v.AuxInt != int64(int8(v.AuxInt)) {
155 f.Fatalf("bad uint8 AuxInt value for %v, saw %d but need %d", v, v.AuxInt, int64(int8(v.AuxInt)))
156 }
157 canHaveAuxInt = true
158 case auxFloat32:
159 canHaveAuxInt = true
160 if math.IsNaN(v.AuxFloat()) {
161 f.Fatalf("value %v has an AuxInt that encodes a NaN", v)
162 }
163 if !isExactFloat32(v.AuxFloat()) {
164 f.Fatalf("value %v has an AuxInt value that is not an exact float32", v)
165 }
166 case auxFloat64:
167 canHaveAuxInt = true
168 if math.IsNaN(v.AuxFloat()) {
169 f.Fatalf("value %v has an AuxInt that encodes a NaN", v)
170 }
171 case auxString:
172 if _, ok := v.Aux.(stringAux); !ok {
173 f.Fatalf("value %v has Aux type %T, want string", v, v.Aux)
174 }
175 canHaveAux = true
176 case auxCallOff:
177 canHaveAuxInt = true
178 fallthrough
179 case auxCall:
180 if ac, ok := v.Aux.(*AuxCall); ok {
181 if v.Op == OpStaticCall && ac.Fn == nil {
182 f.Fatalf("value %v has *AuxCall with nil Fn", v)
183 }
184 } else {
185 f.Fatalf("value %v has Aux type %T, want *AuxCall", v, v.Aux)
186 }
187 canHaveAux = true
188 case auxNameOffsetInt8:
189 if _, ok := v.Aux.(*AuxNameOffset); !ok {
190 f.Fatalf("value %v has Aux type %T, want *AuxNameOffset", v, v.Aux)
191 }
192 canHaveAux = true
193 canHaveAuxInt = true
194 case auxSym, auxTyp:
195 canHaveAux = true
196 case auxSymOff, auxSymValAndOff, auxTypSize:
197 canHaveAuxInt = true
198 canHaveAux = true
199 case auxCCop:
200 if opcodeTable[Op(v.AuxInt)].name == "OpInvalid" {
201 f.Fatalf("value %v has an AuxInt value that is a valid opcode", v)
202 }
203 canHaveAuxInt = true
204 case auxS390XCCMask:
205 if _, ok := v.Aux.(s390x.CCMask); !ok {
206 f.Fatalf("bad type %T for S390XCCMask in %v", v.Aux, v)
207 }
208 canHaveAux = true
209 case auxS390XRotateParams:
210 if _, ok := v.Aux.(s390x.RotateParams); !ok {
211 f.Fatalf("bad type %T for S390XRotateParams in %v", v.Aux, v)
212 }
213 canHaveAux = true
214 case auxFlagConstant:
215 if v.AuxInt < 0 || v.AuxInt > 15 {
216 f.Fatalf("bad FlagConstant AuxInt value for %v", v)
217 }
218 canHaveAuxInt = true
219 case auxPanicBoundsC, auxPanicBoundsCC:
220 canHaveAux = true
221 canHaveAuxInt = true
222 default:
223 f.Fatalf("unknown aux type for %s", v.Op)
224 }
225 if !canHaveAux && v.Aux != nil {
226 f.Fatalf("value %s has an Aux value %v but shouldn't", v.LongString(), v.Aux)
227 }
228 if !canHaveAuxInt && v.AuxInt != 0 {
229 f.Fatalf("value %s has an AuxInt value %d but shouldn't", v.LongString(), v.AuxInt)
230 }
231
232 for i, arg := range v.Args {
233 if arg == nil {
234 f.Fatalf("value %s has nil arg", v.LongString())
235 }
236 if v.Op != OpPhi {
237
238 if arg.Type.IsMemory() && i != len(v.Args)-1 {
239 f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1)
240 }
241 }
242 }
243
244 if valueMark[v.ID] {
245 f.Fatalf("value %s appears twice!", v.LongString())
246 }
247 valueMark[v.ID] = true
248
249 if v.Block != b {
250 f.Fatalf("%s.block != %s", v, b)
251 }
252 if v.Op == OpPhi && len(v.Args) != len(b.Preds) {
253 f.Fatalf("phi length %s does not match pred length %d for block %s", v.LongString(), len(b.Preds), b)
254 }
255
256 if v.Op == OpAddr {
257 if len(v.Args) == 0 {
258 f.Fatalf("no args for OpAddr %s", v.LongString())
259 }
260 if v.Args[0].Op != OpSB {
261 f.Fatalf("bad arg to OpAddr %v", v)
262 }
263 }
264
265 if v.Op == OpLocalAddr {
266 if len(v.Args) != 2 {
267 f.Fatalf("wrong # of args for OpLocalAddr %s", v.LongString())
268 }
269 if v.Args[0].Op != OpSP {
270 f.Fatalf("bad arg 0 to OpLocalAddr %v", v)
271 }
272 if !v.Args[1].Type.IsMemory() {
273 f.Fatalf("bad arg 1 to OpLocalAddr %v", v)
274 }
275 }
276
277 if (v.Op == OpStructMake || v.Op == OpArrayMake1) && v.Type.Size() == 0 {
278 f.Fatalf("zero-sized Make; use Empty instead %v", v)
279 }
280
281 if f.RegAlloc != nil && f.Config.SoftFloat && v.Type.IsFloat() {
282 f.Fatalf("unexpected floating-point type %v", v.LongString())
283 }
284
285
286
287 switch c := f.Config; v.Op {
288 case OpSP, OpSB:
289 if v.Type != c.Types.Uintptr {
290 f.Fatalf("bad %s type: want uintptr, have %s",
291 v.Op, v.Type.String())
292 }
293 case OpStringLen:
294 if v.Type != c.Types.Int {
295 f.Fatalf("bad %s type: want int, have %s",
296 v.Op, v.Type.String())
297 }
298 case OpLoad:
299 if !v.Args[1].Type.IsMemory() {
300 f.Fatalf("bad arg 1 type to %s: want mem, have %s",
301 v.Op, v.Args[1].Type.String())
302 }
303 case OpStore:
304 if !v.Type.IsMemory() {
305 f.Fatalf("bad %s type: want mem, have %s",
306 v.Op, v.Type.String())
307 }
308 if !v.Args[2].Type.IsMemory() {
309 f.Fatalf("bad arg 2 type to %s: want mem, have %s",
310 v.Op, v.Args[2].Type.String())
311 }
312 case OpCondSelect:
313 if !v.Args[2].Type.IsBoolean() {
314 f.Fatalf("bad arg 2 type to %s: want boolean, have %s",
315 v.Op, v.Args[2].Type.String())
316 }
317 case OpAddPtr:
318 if !v.Args[0].Type.IsPtrShaped() && v.Args[0].Type != c.Types.Uintptr {
319 f.Fatalf("bad arg 0 type to %s: want ptr, have %s", v.Op, v.Args[0].LongString())
320 }
321 if !v.Args[1].Type.IsInteger() {
322 f.Fatalf("bad arg 1 type to %s: want integer, have %s", v.Op, v.Args[1].LongString())
323 }
324 case OpVarDef:
325 n := v.Aux.(*ir.Name)
326 if !n.Type().HasPointers() && !IsMergeCandidate(n) {
327 f.Fatalf("vardef must be merge candidate or have pointer type %s", v.Aux.(*ir.Name).Type().String())
328 }
329 case OpNilCheck:
330
331
332 if f.scheduled {
333 if v.Uses != 0 {
334 f.Fatalf("nilcheck must have 0 uses %s", v.Uses)
335 }
336 if !v.Type.IsVoid() {
337 f.Fatalf("nilcheck must have void type %s", v.Type.String())
338 }
339 } else {
340 if !v.Type.IsPtrShaped() && !v.Type.IsUintptr() {
341 f.Fatalf("nilcheck must have pointer type %s", v.Type.String())
342 }
343 }
344 if !v.Args[0].Type.IsPtrShaped() && !v.Args[0].Type.IsUintptr() {
345 f.Fatalf("nilcheck must have argument of pointer type %s", v.Args[0].Type.String())
346 }
347 if !v.Args[1].Type.IsMemory() {
348 f.Fatalf("bad arg 1 type to %s: want mem, have %s",
349 v.Op, v.Args[1].Type.String())
350 }
351 }
352
353
354 }
355 }
356
357
358 if !blockMark[f.Entry.ID] {
359 f.Fatalf("entry block %v is missing", f.Entry)
360 }
361 for _, b := range f.Blocks {
362 for _, c := range b.Preds {
363 if !blockMark[c.b.ID] {
364 f.Fatalf("predecessor block %v for %v is missing", c, b)
365 }
366 }
367 for _, c := range b.Succs {
368 if !blockMark[c.b.ID] {
369 f.Fatalf("successor block %v for %v is missing", c, b)
370 }
371 }
372 }
373
374 if len(f.Entry.Preds) > 0 {
375 f.Fatalf("entry block %s of %s has predecessor(s) %v", f.Entry, f.Name, f.Entry.Preds)
376 }
377
378
379 for _, b := range f.Blocks {
380 for _, v := range b.Values {
381 for i, a := range v.Args {
382 if !valueMark[a.ID] {
383 f.Fatalf("%v, arg %d of %s, is missing", a, i, v.LongString())
384 }
385 }
386 }
387 for _, c := range b.ControlValues() {
388 if !valueMark[c.ID] {
389 f.Fatalf("control value for %s is missing: %v", b, c)
390 }
391 }
392 }
393 for b := f.freeBlocks; b != nil; b = b.succstorage[0].b {
394 if blockMark[b.ID] {
395 f.Fatalf("used block b%d in free list", b.ID)
396 }
397 }
398 for v := f.freeValues; v != nil; v = v.argstorage[0] {
399 if valueMark[v.ID] {
400 f.Fatalf("used value v%d in free list", v.ID)
401 }
402 }
403
404
405 if f.RegAlloc == nil {
406
407
408 sdom := f.Sdom()
409 for _, b := range f.Blocks {
410 for _, v := range b.Values {
411 for i, arg := range v.Args {
412 x := arg.Block
413 y := b
414 if v.Op == OpPhi {
415 y = b.Preds[i].b
416 }
417 if !domCheck(f, sdom, x, y) {
418 f.Fatalf("arg %d of value %s does not dominate, arg=%s", i, v.LongString(), arg.LongString())
419 }
420 }
421 }
422 for _, c := range b.ControlValues() {
423 if !domCheck(f, sdom, c.Block, b) {
424 f.Fatalf("control value %s for %s doesn't dominate", c, b)
425 }
426 }
427 }
428 }
429
430
431 if f.RegAlloc == nil && f.pass != nil {
432 ln := f.loopnest()
433 if !ln.hasIrreducible {
434 po := f.postorder()
435 for _, b := range po {
436 for _, s := range b.Succs {
437 bb := s.Block()
438 if ln.b2l[b.ID] == nil && ln.b2l[bb.ID] != nil && bb != ln.b2l[bb.ID].header {
439 f.Fatalf("block %s not in loop branches to non-header block %s in loop", b.String(), bb.String())
440 }
441 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]) {
442 f.Fatalf("block %s in loop branches to non-header block %s in non-containing loop", b.String(), bb.String())
443 }
444 }
445 }
446 }
447 }
448
449
450 uses := make([]int32, f.NumValues())
451 for _, b := range f.Blocks {
452 for _, v := range b.Values {
453 for _, a := range v.Args {
454 uses[a.ID]++
455 }
456 }
457 for _, c := range b.ControlValues() {
458 uses[c.ID]++
459 }
460 }
461 for _, b := range f.Blocks {
462 for _, v := range b.Values {
463 if v.Uses != uses[v.ID] {
464 f.Fatalf("%s has %d uses, but has Uses=%d", v, uses[v.ID], v.Uses)
465 }
466 }
467 }
468
469 memCheck(f)
470 }
471
472 func memCheck(f *Func) {
473
474 for _, b := range f.Blocks {
475 for _, v := range b.Values {
476 if v.Type.IsTuple() && v.Type.FieldType(0).IsMemory() {
477 f.Fatalf("memory is first in a tuple: %s\n", v.LongString())
478 }
479 }
480 }
481
482
483
484
485
486
487 for _, b := range f.Blocks {
488 for _, v := range b.Values {
489 if (v.Op == OpCopy || v.Uses == 0) && v.Type.IsMemory() {
490 return
491 }
492 }
493 if b != f.Entry && len(b.Preds) == 0 {
494 return
495 }
496 }
497
498
499 lastmem := make([]*Value, f.NumBlocks())
500 ss := newSparseSet(f.NumValues())
501 for _, b := range f.Blocks {
502
503
504 ss.clear()
505 for _, v := range b.Values {
506 if v.Op == OpPhi || !v.Type.IsMemory() {
507 continue
508 }
509 if m := v.MemoryArg(); m != nil {
510 ss.add(m.ID)
511 }
512 }
513
514 for _, v := range b.Values {
515 if !v.Type.IsMemory() {
516 continue
517 }
518 if ss.contains(v.ID) {
519 continue
520 }
521 if lastmem[b.ID] != nil {
522 f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], v)
523 }
524 lastmem[b.ID] = v
525 }
526
527
528 if lastmem[b.ID] == nil {
529 for _, v := range b.Values {
530 if v.Op == OpPhi {
531 continue
532 }
533 m := v.MemoryArg()
534 if m == nil {
535 continue
536 }
537 if lastmem[b.ID] != nil && lastmem[b.ID] != m {
538 f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], m)
539 }
540 lastmem[b.ID] = m
541 }
542 }
543 }
544
545 for {
546 changed := false
547 for _, b := range f.Blocks {
548 if lastmem[b.ID] != nil {
549 continue
550 }
551 for _, e := range b.Preds {
552 p := e.b
553 if lastmem[p.ID] != nil {
554 lastmem[b.ID] = lastmem[p.ID]
555 changed = true
556 break
557 }
558 }
559 }
560 if !changed {
561 break
562 }
563 }
564
565 for _, b := range f.Blocks {
566 for _, v := range b.Values {
567 if v.Op == OpPhi && v.Type.IsMemory() {
568 for i, a := range v.Args {
569 if a != lastmem[b.Preds[i].b.ID] {
570 f.Fatalf("inconsistent memory phi %s %d %s %s", v.LongString(), i, a, lastmem[b.Preds[i].b.ID])
571 }
572 }
573 }
574 }
575 }
576
577
578 if f.scheduled {
579 for _, b := range f.Blocks {
580 var mem *Value
581 for _, v := range b.Values {
582 if v.Op == OpPhi {
583 if v.Type.IsMemory() {
584 mem = v
585 }
586 continue
587 }
588 if mem == nil && len(b.Preds) > 0 {
589
590 mem = lastmem[b.Preds[0].b.ID]
591 }
592 for _, a := range v.Args {
593 if a.Type.IsMemory() && a != mem {
594 f.Fatalf("two live mems @ %s: %s and %s", v, mem, a)
595 }
596 }
597 if v.Type.IsMemory() {
598 mem = v
599 }
600 }
601 }
602 }
603
604
605 if f.scheduled {
606 for _, b := range f.Blocks {
607 seenNonPhi := false
608 for _, v := range b.Values {
609 switch v.Op {
610 case OpPhi:
611 if seenNonPhi {
612 f.Fatalf("phi after non-phi @ %s: %s", b, v)
613 }
614 default:
615 seenNonPhi = true
616 }
617 }
618 }
619 }
620 }
621
622
623 func domCheck(f *Func, sdom SparseTree, x, y *Block) bool {
624 if !sdom.IsAncestorEq(f.Entry, y) {
625
626 return true
627 }
628 return sdom.IsAncestorEq(x, y)
629 }
630
631
632 func isExactFloat32(x float64) bool {
633
634 if bits.TrailingZeros64(math.Float64bits(x)) < 52-23 {
635 return false
636 }
637
638 return math.IsNaN(x) || x == float64(float32(x))
639 }
640
View as plain text