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:
149 canHaveAuxInt = true
150 case auxInt128:
151
152 case auxUInt8:
153 if v.AuxInt != int64(uint8(v.AuxInt)) {
154 f.Fatalf("bad uint8 AuxInt value for %v", v)
155 }
156 canHaveAuxInt = true
157 case auxFloat32:
158 canHaveAuxInt = true
159 if math.IsNaN(v.AuxFloat()) {
160 f.Fatalf("value %v has an AuxInt that encodes a NaN", v)
161 }
162 if !isExactFloat32(v.AuxFloat()) {
163 f.Fatalf("value %v has an AuxInt value that is not an exact float32", v)
164 }
165 case auxFloat64:
166 canHaveAuxInt = true
167 if math.IsNaN(v.AuxFloat()) {
168 f.Fatalf("value %v has an AuxInt that encodes a NaN", v)
169 }
170 case auxString:
171 if _, ok := v.Aux.(stringAux); !ok {
172 f.Fatalf("value %v has Aux type %T, want string", v, v.Aux)
173 }
174 canHaveAux = true
175 case auxCallOff:
176 canHaveAuxInt = true
177 fallthrough
178 case auxCall:
179 if ac, ok := v.Aux.(*AuxCall); ok {
180 if v.Op == OpStaticCall && ac.Fn == nil {
181 f.Fatalf("value %v has *AuxCall with nil Fn", v)
182 }
183 } else {
184 f.Fatalf("value %v has Aux type %T, want *AuxCall", v, v.Aux)
185 }
186 canHaveAux = true
187 case auxNameOffsetInt8:
188 if _, ok := v.Aux.(*AuxNameOffset); !ok {
189 f.Fatalf("value %v has Aux type %T, want *AuxNameOffset", v, v.Aux)
190 }
191 canHaveAux = true
192 canHaveAuxInt = true
193 case auxSym, auxTyp:
194 canHaveAux = true
195 case auxSymOff, auxSymValAndOff, auxTypSize:
196 canHaveAuxInt = true
197 canHaveAux = true
198 case auxCCop:
199 if opcodeTable[Op(v.AuxInt)].name == "OpInvalid" {
200 f.Fatalf("value %v has an AuxInt value that is a valid opcode", v)
201 }
202 canHaveAuxInt = true
203 case auxS390XCCMask:
204 if _, ok := v.Aux.(s390x.CCMask); !ok {
205 f.Fatalf("bad type %T for S390XCCMask in %v", v.Aux, v)
206 }
207 canHaveAux = true
208 case auxS390XRotateParams:
209 if _, ok := v.Aux.(s390x.RotateParams); !ok {
210 f.Fatalf("bad type %T for S390XRotateParams in %v", v.Aux, v)
211 }
212 canHaveAux = true
213 case auxFlagConstant:
214 if v.AuxInt < 0 || v.AuxInt > 15 {
215 f.Fatalf("bad FlagConstant AuxInt value for %v", v)
216 }
217 canHaveAuxInt = true
218 case auxPanicBoundsC, auxPanicBoundsCC:
219 canHaveAux = true
220 canHaveAuxInt = true
221 default:
222 f.Fatalf("unknown aux type for %s", v.Op)
223 }
224 if !canHaveAux && v.Aux != nil {
225 f.Fatalf("value %s has an Aux value %v but shouldn't", v.LongString(), v.Aux)
226 }
227 if !canHaveAuxInt && v.AuxInt != 0 {
228 f.Fatalf("value %s has an AuxInt value %d but shouldn't", v.LongString(), v.AuxInt)
229 }
230
231 for i, arg := range v.Args {
232 if arg == nil {
233 f.Fatalf("value %s has nil arg", v.LongString())
234 }
235 if v.Op != OpPhi {
236
237 if arg.Type.IsMemory() && i != len(v.Args)-1 {
238 f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1)
239 }
240 }
241 }
242
243 if valueMark[v.ID] {
244 f.Fatalf("value %s appears twice!", v.LongString())
245 }
246 valueMark[v.ID] = true
247
248 if v.Block != b {
249 f.Fatalf("%s.block != %s", v, b)
250 }
251 if v.Op == OpPhi && len(v.Args) != len(b.Preds) {
252 f.Fatalf("phi length %s does not match pred length %d for block %s", v.LongString(), len(b.Preds), b)
253 }
254
255 if v.Op == OpAddr {
256 if len(v.Args) == 0 {
257 f.Fatalf("no args for OpAddr %s", v.LongString())
258 }
259 if v.Args[0].Op != OpSB {
260 f.Fatalf("bad arg to OpAddr %v", v)
261 }
262 }
263
264 if v.Op == OpLocalAddr {
265 if len(v.Args) != 2 {
266 f.Fatalf("wrong # of args for OpLocalAddr %s", v.LongString())
267 }
268 if v.Args[0].Op != OpSP {
269 f.Fatalf("bad arg 0 to OpLocalAddr %v", v)
270 }
271 if !v.Args[1].Type.IsMemory() {
272 f.Fatalf("bad arg 1 to OpLocalAddr %v", v)
273 }
274 }
275
276 if f.RegAlloc != nil && f.Config.SoftFloat && v.Type.IsFloat() {
277 f.Fatalf("unexpected floating-point type %v", v.LongString())
278 }
279
280
281
282 switch c := f.Config; v.Op {
283 case OpSP, OpSB:
284 if v.Type != c.Types.Uintptr {
285 f.Fatalf("bad %s type: want uintptr, have %s",
286 v.Op, v.Type.String())
287 }
288 case OpStringLen:
289 if v.Type != c.Types.Int {
290 f.Fatalf("bad %s type: want int, have %s",
291 v.Op, v.Type.String())
292 }
293 case OpLoad:
294 if !v.Args[1].Type.IsMemory() {
295 f.Fatalf("bad arg 1 type to %s: want mem, have %s",
296 v.Op, v.Args[1].Type.String())
297 }
298 case OpStore:
299 if !v.Type.IsMemory() {
300 f.Fatalf("bad %s type: want mem, have %s",
301 v.Op, v.Type.String())
302 }
303 if !v.Args[2].Type.IsMemory() {
304 f.Fatalf("bad arg 2 type to %s: want mem, have %s",
305 v.Op, v.Args[2].Type.String())
306 }
307 case OpCondSelect:
308 if !v.Args[2].Type.IsBoolean() {
309 f.Fatalf("bad arg 2 type to %s: want boolean, have %s",
310 v.Op, v.Args[2].Type.String())
311 }
312 case OpAddPtr:
313 if !v.Args[0].Type.IsPtrShaped() && v.Args[0].Type != c.Types.Uintptr {
314 f.Fatalf("bad arg 0 type to %s: want ptr, have %s", v.Op, v.Args[0].LongString())
315 }
316 if !v.Args[1].Type.IsInteger() {
317 f.Fatalf("bad arg 1 type to %s: want integer, have %s", v.Op, v.Args[1].LongString())
318 }
319 case OpVarDef:
320 n := v.Aux.(*ir.Name)
321 if !n.Type().HasPointers() && !IsMergeCandidate(n) {
322 f.Fatalf("vardef must be merge candidate or have pointer type %s", v.Aux.(*ir.Name).Type().String())
323 }
324 case OpNilCheck:
325
326
327 if f.scheduled {
328 if v.Uses != 0 {
329 f.Fatalf("nilcheck must have 0 uses %s", v.Uses)
330 }
331 if !v.Type.IsVoid() {
332 f.Fatalf("nilcheck must have void type %s", v.Type.String())
333 }
334 } else {
335 if !v.Type.IsPtrShaped() && !v.Type.IsUintptr() {
336 f.Fatalf("nilcheck must have pointer type %s", v.Type.String())
337 }
338 }
339 if !v.Args[0].Type.IsPtrShaped() && !v.Args[0].Type.IsUintptr() {
340 f.Fatalf("nilcheck must have argument of pointer type %s", v.Args[0].Type.String())
341 }
342 if !v.Args[1].Type.IsMemory() {
343 f.Fatalf("bad arg 1 type to %s: want mem, have %s",
344 v.Op, v.Args[1].Type.String())
345 }
346 }
347
348
349 }
350 }
351
352
353 if !blockMark[f.Entry.ID] {
354 f.Fatalf("entry block %v is missing", f.Entry)
355 }
356 for _, b := range f.Blocks {
357 for _, c := range b.Preds {
358 if !blockMark[c.b.ID] {
359 f.Fatalf("predecessor block %v for %v is missing", c, b)
360 }
361 }
362 for _, c := range b.Succs {
363 if !blockMark[c.b.ID] {
364 f.Fatalf("successor block %v for %v is missing", c, b)
365 }
366 }
367 }
368
369 if len(f.Entry.Preds) > 0 {
370 f.Fatalf("entry block %s of %s has predecessor(s) %v", f.Entry, f.Name, f.Entry.Preds)
371 }
372
373
374 for _, b := range f.Blocks {
375 for _, v := range b.Values {
376 for i, a := range v.Args {
377 if !valueMark[a.ID] {
378 f.Fatalf("%v, arg %d of %s, is missing", a, i, v.LongString())
379 }
380 }
381 }
382 for _, c := range b.ControlValues() {
383 if !valueMark[c.ID] {
384 f.Fatalf("control value for %s is missing: %v", b, c)
385 }
386 }
387 }
388 for b := f.freeBlocks; b != nil; b = b.succstorage[0].b {
389 if blockMark[b.ID] {
390 f.Fatalf("used block b%d in free list", b.ID)
391 }
392 }
393 for v := f.freeValues; v != nil; v = v.argstorage[0] {
394 if valueMark[v.ID] {
395 f.Fatalf("used value v%d in free list", v.ID)
396 }
397 }
398
399
400 if f.RegAlloc == nil {
401
402
403 sdom := f.Sdom()
404 for _, b := range f.Blocks {
405 for _, v := range b.Values {
406 for i, arg := range v.Args {
407 x := arg.Block
408 y := b
409 if v.Op == OpPhi {
410 y = b.Preds[i].b
411 }
412 if !domCheck(f, sdom, x, y) {
413 f.Fatalf("arg %d of value %s does not dominate, arg=%s", i, v.LongString(), arg.LongString())
414 }
415 }
416 }
417 for _, c := range b.ControlValues() {
418 if !domCheck(f, sdom, c.Block, b) {
419 f.Fatalf("control value %s for %s doesn't dominate", c, b)
420 }
421 }
422 }
423 }
424
425
426 if f.RegAlloc == nil && f.pass != nil {
427 ln := f.loopnest()
428 if !ln.hasIrreducible {
429 po := f.postorder()
430 for _, b := range po {
431 for _, s := range b.Succs {
432 bb := s.Block()
433 if ln.b2l[b.ID] == nil && ln.b2l[bb.ID] != nil && bb != ln.b2l[bb.ID].header {
434 f.Fatalf("block %s not in loop branches to non-header block %s in loop", b.String(), bb.String())
435 }
436 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]) {
437 f.Fatalf("block %s in loop branches to non-header block %s in non-containing loop", b.String(), bb.String())
438 }
439 }
440 }
441 }
442 }
443
444
445 uses := make([]int32, f.NumValues())
446 for _, b := range f.Blocks {
447 for _, v := range b.Values {
448 for _, a := range v.Args {
449 uses[a.ID]++
450 }
451 }
452 for _, c := range b.ControlValues() {
453 uses[c.ID]++
454 }
455 }
456 for _, b := range f.Blocks {
457 for _, v := range b.Values {
458 if v.Uses != uses[v.ID] {
459 f.Fatalf("%s has %d uses, but has Uses=%d", v, uses[v.ID], v.Uses)
460 }
461 }
462 }
463
464 memCheck(f)
465 }
466
467 func memCheck(f *Func) {
468
469 for _, b := range f.Blocks {
470 for _, v := range b.Values {
471 if v.Type.IsTuple() && v.Type.FieldType(0).IsMemory() {
472 f.Fatalf("memory is first in a tuple: %s\n", v.LongString())
473 }
474 }
475 }
476
477
478
479
480
481
482 for _, b := range f.Blocks {
483 for _, v := range b.Values {
484 if (v.Op == OpCopy || v.Uses == 0) && v.Type.IsMemory() {
485 return
486 }
487 }
488 if b != f.Entry && len(b.Preds) == 0 {
489 return
490 }
491 }
492
493
494 lastmem := make([]*Value, f.NumBlocks())
495 ss := newSparseSet(f.NumValues())
496 for _, b := range f.Blocks {
497
498
499 ss.clear()
500 for _, v := range b.Values {
501 if v.Op == OpPhi || !v.Type.IsMemory() {
502 continue
503 }
504 if m := v.MemoryArg(); m != nil {
505 ss.add(m.ID)
506 }
507 }
508
509 for _, v := range b.Values {
510 if !v.Type.IsMemory() {
511 continue
512 }
513 if ss.contains(v.ID) {
514 continue
515 }
516 if lastmem[b.ID] != nil {
517 f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], v)
518 }
519 lastmem[b.ID] = v
520 }
521
522
523 if lastmem[b.ID] == nil {
524 for _, v := range b.Values {
525 if v.Op == OpPhi {
526 continue
527 }
528 m := v.MemoryArg()
529 if m == nil {
530 continue
531 }
532 if lastmem[b.ID] != nil && lastmem[b.ID] != m {
533 f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], m)
534 }
535 lastmem[b.ID] = m
536 }
537 }
538 }
539
540 for {
541 changed := false
542 for _, b := range f.Blocks {
543 if lastmem[b.ID] != nil {
544 continue
545 }
546 for _, e := range b.Preds {
547 p := e.b
548 if lastmem[p.ID] != nil {
549 lastmem[b.ID] = lastmem[p.ID]
550 changed = true
551 break
552 }
553 }
554 }
555 if !changed {
556 break
557 }
558 }
559
560 for _, b := range f.Blocks {
561 for _, v := range b.Values {
562 if v.Op == OpPhi && v.Type.IsMemory() {
563 for i, a := range v.Args {
564 if a != lastmem[b.Preds[i].b.ID] {
565 f.Fatalf("inconsistent memory phi %s %d %s %s", v.LongString(), i, a, lastmem[b.Preds[i].b.ID])
566 }
567 }
568 }
569 }
570 }
571
572
573 if f.scheduled {
574 for _, b := range f.Blocks {
575 var mem *Value
576 for _, v := range b.Values {
577 if v.Op == OpPhi {
578 if v.Type.IsMemory() {
579 mem = v
580 }
581 continue
582 }
583 if mem == nil && len(b.Preds) > 0 {
584
585 mem = lastmem[b.Preds[0].b.ID]
586 }
587 for _, a := range v.Args {
588 if a.Type.IsMemory() && a != mem {
589 f.Fatalf("two live mems @ %s: %s and %s", v, mem, a)
590 }
591 }
592 if v.Type.IsMemory() {
593 mem = v
594 }
595 }
596 }
597 }
598
599
600 if f.scheduled {
601 for _, b := range f.Blocks {
602 seenNonPhi := false
603 for _, v := range b.Values {
604 switch v.Op {
605 case OpPhi:
606 if seenNonPhi {
607 f.Fatalf("phi after non-phi @ %s: %s", b, v)
608 }
609 default:
610 seenNonPhi = true
611 }
612 }
613 }
614 }
615 }
616
617
618 func domCheck(f *Func, sdom SparseTree, x, y *Block) bool {
619 if !sdom.IsAncestorEq(f.Entry, y) {
620
621 return true
622 }
623 return sdom.IsAncestorEq(x, y)
624 }
625
626
627 func isExactFloat32(x float64) bool {
628
629 if bits.TrailingZeros64(math.Float64bits(x)) < 52-23 {
630 return false
631 }
632
633 return math.IsNaN(x) || x == float64(float32(x))
634 }
635
View as plain text