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