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 not 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 case auxSizeAndAlign:
223 if _, ok := v.Aux.(int64Aux); !ok {
224 f.Fatalf("value %v has Aux type %T, want int64Aux", v, v.Aux)
225 }
226 canHaveAux = true
227 canHaveAuxInt = true
228 default:
229 f.Fatalf("unknown aux type %T for %s", opcodeTable[v.Op].auxType, v.Op)
230 }
231 if !canHaveAux && v.Aux != nil {
232 f.Fatalf("value %s has an Aux value %v but shouldn't", v.LongString(), v.Aux)
233 }
234 if !canHaveAuxInt && v.AuxInt != 0 {
235 f.Fatalf("value %s has an AuxInt value %d but shouldn't", v.LongString(), v.AuxInt)
236 }
237
238 for i, arg := range v.Args {
239 if arg == nil {
240 f.Fatalf("value %s has nil arg", v.LongString())
241 }
242 if v.Op != OpPhi {
243
244 if arg.Type.IsMemory() && i != len(v.Args)-1 {
245 f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1)
246 }
247 }
248 }
249
250 if valueMark[v.ID] {
251 f.Fatalf("value %s appears twice!", v.LongString())
252 }
253 valueMark[v.ID] = true
254
255 if v.Block != b {
256 f.Fatalf("%s.block != %s", v, b)
257 }
258 if v.Op == OpPhi && len(v.Args) != len(b.Preds) {
259 f.Fatalf("phi length %s does not match pred length %d for block %s", v.LongString(), len(b.Preds), b)
260 }
261
262 if v.Op == OpAddr {
263 if len(v.Args) == 0 {
264 f.Fatalf("no args for OpAddr %s", v.LongString())
265 }
266 if v.Args[0].Op != OpSB {
267 f.Fatalf("bad arg to OpAddr %v", v)
268 }
269 }
270
271 if v.Op == OpLocalAddr {
272 if len(v.Args) != 2 {
273 f.Fatalf("wrong # of args for OpLocalAddr %s", v.LongString())
274 }
275 if v.Args[0].Op != OpSP {
276 f.Fatalf("bad arg 0 to OpLocalAddr %v", v)
277 }
278 if !v.Args[1].Type.IsMemory() {
279 f.Fatalf("bad arg 1 to OpLocalAddr %v", v)
280 }
281 }
282
283 if (v.Op == OpStructMake || v.Op == OpArrayMake1) && v.Type.Size() == 0 {
284 f.Fatalf("zero-sized Make; use Empty instead %v", v)
285 }
286
287 if f.RegAlloc != nil && f.Config.SoftFloat && v.Type.IsFloat() {
288 f.Fatalf("unexpected floating-point type %v", v.LongString())
289 }
290
291
292
293 switch c := f.Config; v.Op {
294 case OpSP, OpSB:
295 if v.Type != c.Types.Uintptr {
296 f.Fatalf("bad %s type: want uintptr, have %s",
297 v.Op, v.Type.String())
298 }
299 case OpStringLen:
300 if v.Type != c.Types.Int {
301 f.Fatalf("bad %s type: want int, have %s",
302 v.Op, v.Type.String())
303 }
304 case OpLoad:
305 if !v.Args[1].Type.IsMemory() {
306 f.Fatalf("bad arg 1 type to %s: want mem, have %s",
307 v.Op, v.Args[1].Type.String())
308 }
309 case OpStore:
310 if !v.Type.IsMemory() {
311 f.Fatalf("bad %s type: want mem, have %s",
312 v.Op, v.Type.String())
313 }
314 if !v.Args[2].Type.IsMemory() {
315 f.Fatalf("bad arg 2 type to %s: want mem, have %s",
316 v.Op, v.Args[2].Type.String())
317 }
318 case OpCondSelect:
319 if !v.Args[2].Type.IsBoolean() {
320 f.Fatalf("bad arg 2 type to %s: want boolean, have %s",
321 v.Op, v.Args[2].Type.String())
322 }
323 case OpAddPtr:
324 if !v.Args[0].Type.IsPtrShaped() && v.Args[0].Type != c.Types.Uintptr {
325 f.Fatalf("bad arg 0 type to %s: want ptr, have %s", v.Op, v.Args[0].LongString())
326 }
327 if !v.Args[1].Type.IsInteger() {
328 f.Fatalf("bad arg 1 type to %s: want integer, have %s", v.Op, v.Args[1].LongString())
329 }
330 case OpVarDef:
331 n := v.Aux.(*ir.Name)
332 if !n.Type().HasPointers() && !IsMergeCandidate(n) {
333 f.Fatalf("vardef must be merge candidate or have pointer type %s", v.Aux.(*ir.Name).Type().String())
334 }
335 case OpNilCheck:
336
337
338 if f.scheduled {
339 if v.Uses != 0 {
340 f.Fatalf("nilcheck must have 0 uses %s", v.Uses)
341 }
342 if !v.Type.IsVoid() {
343 f.Fatalf("nilcheck must have void type %s", v.Type.String())
344 }
345 } else {
346 if !v.Type.IsPtrShaped() && !v.Type.IsUintptr() {
347 f.Fatalf("nilcheck must have pointer type %s", v.Type.String())
348 }
349 }
350 if !v.Args[0].Type.IsPtrShaped() && !v.Args[0].Type.IsUintptr() {
351 f.Fatalf("nilcheck must have argument of pointer type %s", v.Args[0].Type.String())
352 }
353 if !v.Args[1].Type.IsMemory() {
354 f.Fatalf("bad arg 1 type to %s: want mem, have %s",
355 v.Op, v.Args[1].Type.String())
356 }
357 }
358
359
360
361 var argSize int64
362 switch v.Op {
363 case OpAdd8, OpSub8, OpMul8, OpDiv8, OpDiv8u, OpMod8, OpMod8u,
364 OpAnd8, OpOr8, OpXor8,
365 OpEq8, OpNeq8, OpLess8, OpLeq8,
366 OpNeg8, OpCom8,
367 OpSignExt8to16, OpSignExt8to32, OpSignExt8to64,
368 OpZeroExt8to16, OpZeroExt8to32, OpZeroExt8to64:
369 argSize = 1
370 case OpAdd16, OpSub16, OpMul16, OpDiv16, OpDiv16u, OpMod16, OpMod16u,
371 OpAnd16, OpOr16, OpXor16,
372 OpEq16, OpNeq16, OpLess16, OpLeq16,
373 OpNeg16, OpCom16,
374 OpSignExt16to32, OpSignExt16to64,
375 OpZeroExt16to32, OpZeroExt16to64,
376 OpTrunc16to8:
377 argSize = 2
378 case OpAdd32, OpSub32, OpMul32, OpDiv32, OpDiv32u, OpMod32, OpMod32u,
379 OpAnd32, OpOr32, OpXor32,
380 OpEq32, OpNeq32, OpLess32, OpLeq32,
381 OpNeg32, OpCom32,
382 OpSignExt32to64, OpZeroExt32to64,
383 OpTrunc32to8, OpTrunc32to16:
384 argSize = 4
385 case OpAdd64, OpSub64, OpMul64, OpDiv64, OpDiv64u, OpMod64, OpMod64u,
386 OpAnd64, OpOr64, OpXor64,
387 OpEq64, OpNeq64, OpLess64, OpLeq64,
388 OpNeg64, OpCom64,
389 OpTrunc64to8, OpTrunc64to16, OpTrunc64to32:
390 argSize = 8
391 }
392 if argSize != 0 {
393 for i, arg := range v.Args {
394 if arg.Type.Size() != argSize {
395 f.Fatalf("arg %d to %s (%v) should be %d bytes in size, it is %s", i, v.Op, v, argSize, arg.Type.String())
396 }
397 }
398 }
399
400
401 }
402 }
403
404
405 if !blockMark[f.Entry.ID] {
406 f.Fatalf("entry block %v is missing", f.Entry)
407 }
408 for _, b := range f.Blocks {
409 for _, c := range b.Preds {
410 if !blockMark[c.b.ID] {
411 f.Fatalf("predecessor block %v for %v is missing", c, b)
412 }
413 }
414 for _, c := range b.Succs {
415 if !blockMark[c.b.ID] {
416 f.Fatalf("successor block %v for %v is missing", c, b)
417 }
418 }
419 }
420
421 if len(f.Entry.Preds) > 0 {
422 f.Fatalf("entry block %s of %s has predecessor(s) %v", f.Entry, f.Name, f.Entry.Preds)
423 }
424
425
426 for _, b := range f.Blocks {
427 for _, v := range b.Values {
428 for i, a := range v.Args {
429 if !valueMark[a.ID] {
430 f.Fatalf("%v, arg %d of %s, is missing", a, i, v.LongString())
431 }
432 }
433 }
434 for _, c := range b.ControlValues() {
435 if !valueMark[c.ID] {
436 f.Fatalf("control value for %s is missing: %v", b, c)
437 }
438 }
439 }
440 for b := f.freeBlocks; b != nil; b = b.succstorage[0].b {
441 if blockMark[b.ID] {
442 f.Fatalf("used block b%d in free list", b.ID)
443 }
444 }
445 for v := f.freeValues; v != nil; v = v.argstorage[0] {
446 if valueMark[v.ID] {
447 f.Fatalf("used value v%d in free list", v.ID)
448 }
449 }
450
451
452 if f.RegAlloc == nil {
453
454
455 sdom := f.Sdom()
456 for _, b := range f.Blocks {
457 for _, v := range b.Values {
458 for i, arg := range v.Args {
459 x := arg.Block
460 y := b
461 if v.Op == OpPhi {
462 y = b.Preds[i].b
463 }
464 if !domCheck(f, sdom, x, y) {
465 f.Fatalf("arg %d of value %s does not dominate, arg=%s", i, v.LongString(), arg.LongString())
466 }
467 }
468 }
469 for _, c := range b.ControlValues() {
470 if !domCheck(f, sdom, c.Block, b) {
471 f.Fatalf("control value %s for %s doesn't dominate", c, b)
472 }
473 }
474 }
475 }
476
477
478 if f.RegAlloc == nil && f.pass != nil {
479 ln := f.loopnest()
480 if !ln.hasIrreducible {
481 po := f.postorder()
482 for _, b := range po {
483 for _, s := range b.Succs {
484 bb := s.Block()
485 if ln.b2l[b.ID] == nil && ln.b2l[bb.ID] != nil && bb != ln.b2l[bb.ID].header {
486 f.Fatalf("block %s not in loop branches to non-header block %s in loop", b.String(), bb.String())
487 }
488 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]) {
489 f.Fatalf("block %s in loop branches to non-header block %s in non-containing loop", b.String(), bb.String())
490 }
491 }
492 }
493 }
494 }
495
496
497 uses := make([]int32, f.NumValues())
498 for _, b := range f.Blocks {
499 for _, v := range b.Values {
500 for _, a := range v.Args {
501 uses[a.ID]++
502 }
503 }
504 for _, c := range b.ControlValues() {
505 uses[c.ID]++
506 }
507 }
508 for _, b := range f.Blocks {
509 for _, v := range b.Values {
510 if v.Uses != uses[v.ID] {
511 f.Fatalf("%s has %d uses, but has Uses=%d", v, uses[v.ID], v.Uses)
512 }
513 }
514 }
515
516 memCheck(f)
517 }
518
519 func memCheck(f *Func) {
520
521 for _, b := range f.Blocks {
522 for _, v := range b.Values {
523 if v.Type.IsTuple() && v.Type.FieldType(0).IsMemory() {
524 f.Fatalf("memory is first in a tuple: %s\n", v.LongString())
525 }
526 }
527 }
528
529
530
531
532
533
534 for _, b := range f.Blocks {
535 for _, v := range b.Values {
536 if (v.Op == OpCopy || v.Uses == 0) && v.Type.IsMemory() {
537 return
538 }
539 }
540 if b != f.Entry && len(b.Preds) == 0 {
541 return
542 }
543 }
544
545
546 lastmem := make([]*Value, f.NumBlocks())
547 ss := newSparseSet(f.NumValues())
548 for _, b := range f.Blocks {
549
550
551 ss.clear()
552 for _, v := range b.Values {
553 if v.Op == OpPhi || !v.Type.IsMemory() {
554 continue
555 }
556 if m := v.MemoryArg(); m != nil {
557 ss.add(m.ID)
558 }
559 }
560
561 for _, v := range b.Values {
562 if !v.Type.IsMemory() {
563 continue
564 }
565 if ss.contains(v.ID) {
566 continue
567 }
568 if lastmem[b.ID] != nil {
569 f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], v)
570 }
571 lastmem[b.ID] = v
572 }
573
574
575 if lastmem[b.ID] == nil {
576 for _, v := range b.Values {
577 if v.Op == OpPhi {
578 continue
579 }
580 m := v.MemoryArg()
581 if m == nil {
582 continue
583 }
584 if lastmem[b.ID] != nil && lastmem[b.ID] != m {
585 f.Fatalf("two live memory values in %s: %s and %s", b, lastmem[b.ID], m)
586 }
587 lastmem[b.ID] = m
588 }
589 }
590 }
591
592 for {
593 changed := false
594 for _, b := range f.Blocks {
595 if lastmem[b.ID] != nil {
596 continue
597 }
598 for _, e := range b.Preds {
599 p := e.b
600 if lastmem[p.ID] != nil {
601 lastmem[b.ID] = lastmem[p.ID]
602 changed = true
603 break
604 }
605 }
606 }
607 if !changed {
608 break
609 }
610 }
611
612 for _, b := range f.Blocks {
613 for _, v := range b.Values {
614 if v.Op == OpPhi && v.Type.IsMemory() {
615 for i, a := range v.Args {
616 if a != lastmem[b.Preds[i].b.ID] {
617 f.Fatalf("inconsistent memory phi %s %d %s %s", v.LongString(), i, a, lastmem[b.Preds[i].b.ID])
618 }
619 }
620 }
621 }
622 }
623
624
625 if f.scheduled {
626 for _, b := range f.Blocks {
627 var mem *Value
628 for _, v := range b.Values {
629 if v.Op == OpPhi {
630 if v.Type.IsMemory() {
631 mem = v
632 }
633 continue
634 }
635 if mem == nil && len(b.Preds) > 0 {
636
637 mem = lastmem[b.Preds[0].b.ID]
638 }
639 for _, a := range v.Args {
640 if a.Type.IsMemory() && a != mem {
641 f.Fatalf("two live mems @ %s: %s and %s", v, mem, a)
642 }
643 }
644 if v.Type.IsMemory() {
645 mem = v
646 }
647 }
648 }
649 }
650
651
652 if f.scheduled {
653 for _, b := range f.Blocks {
654 seenNonPhi := false
655 for _, v := range b.Values {
656 switch v.Op {
657 case OpPhi:
658 if seenNonPhi {
659 f.Fatalf("phi after non-phi @ %s: %s", b, v)
660 }
661 default:
662 seenNonPhi = true
663 }
664 }
665 }
666 }
667 }
668
669
670 func domCheck(f *Func, sdom SparseTree, x, y *Block) bool {
671 if !sdom.IsAncestorEq(f.Entry, y) {
672
673 return true
674 }
675 return sdom.IsAncestorEq(x, y)
676 }
677
678
679 func isExactFloat32(x float64) bool {
680
681 if bits.TrailingZeros64(math.Float64bits(x)) < 52-23 {
682 return false
683 }
684
685 return math.IsNaN(x) || x == float64(float32(x))
686 }
687
View as plain text