1 ;;;; This file contains utilities for debugging the compiler --
2 ;;;; currently only stuff for checking the consistency of the IR1.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
17 "This variable is bound to the format arguments when an error is signalled
20 (defvar *ignored-errors* (make-hash-table :test 'equal))
22 ;;; A definite inconsistency has been detected. Signal an error with
23 ;;; *args* bound to the list of the format args.
24 (declaim (ftype (function (string &rest t) (values)) barf))
25 (defun barf (string &rest *args*)
26 (unless (gethash string *ignored-errors*)
28 (apply #'error string *args*)
30 :report "Ignore this error.")
32 :report "Ignore this and all future occurrences of this error."
33 (setf (gethash string *ignored-errors*) t))))
36 (defvar *burp-action* :warn
38 "Action taken by the BURP function when a possible compiler bug is detected.
39 One of :WARN, :ERROR or :NONE.")
40 (declaim (type (member :warn :error :none) *burp-action*))
42 ;;; Called when something funny but possibly correct is noticed.
43 ;;; Otherwise similar to BARF.
44 (declaim (ftype (function (string &rest t) (values)) burp))
45 (defun burp (string &rest *args*)
47 (:warn (apply #'warn string *args*))
48 (:error (apply #'cerror "press on anyway." string *args*))
52 ;;; *SEEN-BLOCKS* is a hashtable with true values for all blocks which
53 ;;; appear in the DFO for one of the specified components.
55 ;;; *SEEN-FUNS* is similar, but records all the lambdas we
56 ;;; reached by recursing on top level functions.
57 ;;; FIXME: Is it really only LAMBDAs, not e.g. FUNCTIONALs? Then
58 ;;; shouldn't it be *SEEN-LAMBDAS*?
59 (defvar *seen-blocks* (make-hash-table :test 'eq))
60 (defvar *seen-funs* (make-hash-table :test 'eq))
62 ;;; Barf if NODE is in a block which wasn't reached during the graph
64 (declaim (ftype (function (node) (values)) check-node-reached))
65 (defun check-node-reached (node)
66 (unless (gethash (continuation-block (node-prev node)) *seen-blocks*)
67 (barf "~S was not reached." node))
70 ;;; Check everything that we can think of for consistency. When a
71 ;;; definite inconsistency is detected, we BARF. Possible problems
72 ;;; just cause us to BURP. Our argument is a list of components, but
73 ;;; we also look at the *FREE-VARS*, *FREE-FUNS* and *CONSTANTS*.
75 ;;; First we do a pre-pass which finds all the CBLOCKs and CLAMBDAs,
76 ;;; testing that they are linked together properly and entering them
77 ;;; in hashtables. Next, we iterate over the blocks again, looking at
78 ;;; the actual code and control flow. Finally, we scan the global leaf
79 ;;; hashtables, looking for lossage.
80 (declaim (ftype (function (list) (values)) check-ir1-consistency))
81 (defun check-ir1-consistency (components)
82 (clrhash *seen-blocks*)
84 (dolist (c components)
85 (let* ((head (component-head c))
86 (tail (component-tail c)))
87 (unless (and (null (block-pred head)) (null (block-succ tail)))
88 (barf "~S is malformed." c))
91 (block head (block-next block)))
93 (unless (eq prev tail)
94 (barf "wrong TAIL for DFO, ~S in ~S" prev c)))
95 (setf (gethash block *seen-blocks*) t)
96 (unless (eq (block-prev block) prev)
97 (barf "bad PREV for ~S, should be ~S" block prev))
98 (unless (or (eq block tail)
99 (eq (block-component block) c))
100 (barf "~S is not in ~S." block c)))
102 (when (or (loop-blocks c) (loop-inferiors c))
103 (do-blocks (block c :both)
104 (setf (block-flag block) nil))
105 (check-loop-consistency c nil)
106 (do-blocks (block c :both)
107 (unless (block-flag block)
108 (barf "~S was not in any loop." block))))
112 (check-fun-consistency components)
114 (dolist (c components)
115 (do ((block (block-next (component-head c)) (block-next block)))
116 ((null (block-next block)))
117 (check-block-consistency block)))
119 (maphash (lambda (k v)
121 (unless (or (constant-p v)
122 (and (global-var-p v)
123 (member (global-var-kind v)
124 '(:global :special))))
125 (barf "strange *FREE-VARS* entry: ~S" v))
126 (dolist (n (leaf-refs v))
127 (check-node-reached n))
128 (when (basic-var-p v)
129 (dolist (n (basic-var-sets v))
130 (check-node-reached n))))
133 (maphash (lambda (k v)
135 (unless (constant-p v)
136 (barf "strange *CONSTANTS* entry: ~S" v))
137 (dolist (n (leaf-refs v))
138 (check-node-reached n)))
141 (maphash (lambda (k v)
143 (unless (or (functional-p v)
144 (and (global-var-p v)
145 (eq (global-var-kind v) :global-function)))
146 (barf "strange *FREE-FUNS* entry: ~S" v))
147 (dolist (n (leaf-refs v))
148 (check-node-reached n)))
150 (clrhash *seen-funs*)
151 (clrhash *seen-blocks*)
154 ;;;; function consistency checking
156 (defun observe-functional (x)
157 (declare (type functional x))
158 (when (gethash x *seen-funs*)
159 (barf "~S was seen more than once." x))
160 (unless (eq (functional-kind x) :deleted)
161 (setf (gethash x *seen-funs*) t)))
163 ;;; Check that the specified function has been seen.
164 (defun check-fun-reached (fun where)
165 (declare (type functional fun))
166 (unless (gethash fun *seen-funs*)
167 (barf "unseen function ~S in ~S" fun where)))
169 ;;; In a CLAMBDA, check that the associated nodes are in seen blocks.
170 ;;; In an OPTIONAL-DISPATCH, check that the entry points were seen. If
171 ;;; the function is deleted, ignore it.
172 (defun check-fun-stuff (functional)
173 (ecase (functional-kind functional)
175 (let ((fun (functional-entry-fun functional)))
176 (check-fun-reached fun functional)
177 (when (functional-kind fun)
178 (barf "The function for XEP ~S has kind." functional))
179 (unless (eq (functional-entry-fun fun) functional)
180 (barf "bad back-pointer in function for XEP ~S" functional))))
181 ((:let :mv-let :assignment)
182 (check-fun-reached (lambda-home functional) functional)
183 (when (functional-entry-fun functional)
184 (barf "The LET ~S has entry function." functional))
185 (unless (member functional (lambda-lets (lambda-home functional)))
186 (barf "The LET ~S is not in LETs for HOME." functional))
187 (unless (eq (functional-kind functional) :assignment)
188 (when (rest (leaf-refs functional))
189 (barf "The LET ~S has multiple references." functional)))
190 (when (lambda-lets functional)
191 (barf "LETs in a LET: ~S" functional)))
193 (when (functional-entry-fun functional)
194 (barf ":OPTIONAL ~S has an ENTRY-FUN." functional))
195 (let ((ef (lambda-optional-dispatch functional)))
196 (check-fun-reached ef functional)
197 (unless (or (member functional (optional-dispatch-entry-points ef))
198 (eq functional (optional-dispatch-more-entry ef))
199 (eq functional (optional-dispatch-main-entry ef)))
200 (barf ":OPTIONAL ~S is not an e-p for its OPTIONAL-DISPATCH ~S."
203 (unless (eq (functional-entry-fun functional) functional)
204 (barf "The ENTRY-FUN in ~S isn't a self-pointer." functional)))
205 ((nil :escape :cleanup)
206 (let ((ef (functional-entry-fun functional)))
208 (check-fun-reached ef functional)
209 (unless (eq (functional-kind ef) :external)
210 (barf "The ENTRY-FUN in ~S isn't an XEP: ~S." functional ef)))))
212 (return-from check-fun-stuff)))
214 (case (functional-kind functional)
215 ((nil :optional :external :toplevel :escape :cleanup)
216 (when (lambda-p functional)
217 (dolist (fun (lambda-lets functional))
218 (unless (eq (lambda-home fun) functional)
219 (barf "The home in ~S is not ~S." fun functional))
220 (check-fun-reached fun functional))
221 (unless (eq (lambda-home functional) functional)
222 (barf "home not self-pointer in ~S" functional)))))
224 (etypecase functional
226 (when (lambda-bind functional)
227 (check-node-reached (lambda-bind functional)))
228 (when (lambda-return functional)
229 (check-node-reached (lambda-return functional)))
231 (dolist (var (lambda-vars functional))
232 (dolist (ref (leaf-refs var))
233 (check-node-reached ref))
234 (dolist (set (basic-var-sets var))
235 (check-node-reached set))
236 (unless (eq (lambda-var-home var) functional)
237 (barf "HOME in ~S should be ~S." var functional))))
239 (dolist (ep (optional-dispatch-entry-points functional))
240 (check-fun-reached ep functional))
241 (let ((more (optional-dispatch-more-entry functional)))
242 (when more (check-fun-reached more functional)))
243 (check-fun-reached (optional-dispatch-main-entry functional)
246 (defun check-fun-consistency (components)
247 (dolist (c components)
248 (dolist (new-fun (component-new-funs c))
249 (observe-functional new-fun))
250 (dolist (fun (component-lambdas c))
251 (when (eq (functional-kind fun) :external)
252 (let ((ef (functional-entry-fun fun)))
253 (when (optional-dispatch-p ef)
254 (observe-functional ef))))
255 (observe-functional fun)
256 (dolist (let (lambda-lets fun))
257 (observe-functional let))))
259 (dolist (c components)
260 (dolist (new-fun (component-new-funs c))
261 (check-fun-stuff new-fun))
262 (dolist (fun (component-lambdas c))
263 (when (eq (functional-kind fun) :deleted)
264 (barf "deleted lambda ~S in Lambdas for ~S" fun c))
265 (check-fun-stuff fun)
266 (dolist (let (lambda-lets fun))
267 (check-fun-stuff let)))))
269 ;;;; loop consistency checking
272 ;;; Descend through the loop nesting and check that the tree is well-formed
273 ;;; and that all blocks in the loops are known blocks. We also mark each block
274 ;;; that we see so that we can do a check later to detect blocks that weren't
276 (declaim (ftype (function (loop (or loop null)) (values)) check-loop-consistency))
277 (defun check-loop-consistency (loop superior)
278 (unless (eq (loop-superior loop) superior)
279 (barf "wrong superior in ~S, should be ~S" loop superior))
281 (/= (loop-depth loop) (1+ (loop-depth superior))))
282 (barf "wrong depth in ~S" loop))
284 (dolist (tail (loop-tail loop))
285 (check-loop-block tail loop))
286 (dolist (exit (loop-exits loop))
287 (check-loop-block exit loop))
288 (check-loop-block (loop-head loop) loop)
289 (unless (eq (block-loop (loop-head loop)) loop)
290 (barf "The head of ~S is not directly in the loop." loop))
292 (do ((block (loop-blocks loop) (block-loop-next block)))
294 (setf (block-flag block) t)
295 (unless (gethash block *seen-blocks*)
296 (barf "unseen block ~S in Blocks for ~S" block loop))
297 (unless (eq (block-loop block) loop)
298 (barf "wrong loop in ~S, should be ~S" block loop)))
300 (dolist (inferior (loop-inferiors loop))
301 (check-loop-consistency inferior loop))
304 ;;; Check that Block is either in Loop or an inferior.
305 (declaim (ftype (function (block loop) (values)) check-loop-block))
306 (defun check-loop-block (block loop)
307 (unless (gethash block *seen-blocks*)
308 (barf "unseen block ~S in loop info for ~S" block loop))
310 (if (eq (block-loop block) l)
312 (dolist (inferior (loop-inferiors l) nil)
313 (when (walk inferior) (return t))))))
315 (barf "~S is in loop info for ~S but not in the loop." block loop)))
320 ;;; Check a block for consistency at the general flow-graph level, and
321 ;;; call CHECK-NODE-CONSISTENCY on each node to locally check for
322 ;;; semantic consistency.
323 (declaim (ftype (function (cblock) (values)) check-block-consistency))
324 (defun check-block-consistency (block)
326 (dolist (pred (block-pred block))
327 (unless (gethash pred *seen-blocks*)
328 (barf "unseen predecessor ~S in ~S" pred block))
329 (unless (member block (block-succ pred))
330 (barf "bad predecessor link ~S in ~S" pred block)))
332 (let* ((fun (block-home-lambda block))
333 (fun-deleted (eq (functional-kind fun) :deleted))
334 (this-cont (block-start block))
335 (last (block-last block)))
337 (check-fun-reached fun block))
338 (when (not this-cont)
339 (barf "~S has no START." block))
341 (barf "~S has no LAST." block))
342 (unless (eq (continuation-kind this-cont) :block-start)
343 (barf "The START of ~S has the wrong kind." block))
345 (let ((use (continuation-use this-cont))
346 (uses (block-start-uses block)))
347 (when (and (null use) (= (length uses) 1))
348 (barf "~S has a unique use, but no USE." this-cont))
350 (unless (eq (node-cont node) this-cont)
351 (barf "The USE ~S for START in ~S has wrong CONT." node block))
352 (check-node-reached node)))
354 (let* ((last-cont (node-cont last))
355 (cont-block (continuation-block last-cont))
356 (dest (continuation-dest last-cont)))
357 (ecase (continuation-kind last-cont)
359 (:deleted-block-start
360 (let ((dest (continuation-dest last-cont)))
362 (check-node-reached dest)))
363 (unless (member last (block-start-uses cont-block))
364 (barf "LAST in ~S is missing from uses of its Cont." block)))
366 (check-node-reached (continuation-next last-cont))
367 (unless (member last (block-start-uses cont-block))
368 (barf "LAST in ~S is missing from uses of its Cont." block)))
370 (unless (eq cont-block block)
371 (barf "CONT of LAST in ~S is in a different BLOCK." block))
372 (unless (eq (continuation-use last-cont) last)
373 (barf "USE is not LAST in CONT of LAST in ~S." block))
374 (when (continuation-next last-cont)
375 (barf "CONT of LAST has a NEXT in ~S." block))))
378 (check-node-reached dest)))
381 (unless (eq (continuation-block this-cont) block)
382 (barf "BLOCK in ~S should be ~S." this-cont block))
384 (let ((dest (continuation-dest this-cont)))
386 (check-node-reached dest)))
388 (let ((node (continuation-next this-cont)))
389 (unless (node-p node)
390 (barf "~S has strange NEXT." this-cont))
391 (unless (eq (node-prev node) this-cont)
392 (barf "PREV in ~S should be ~S." node this-cont))
395 (check-node-consistency node))
397 (let ((cont (node-cont node)))
399 (barf "~S has no CONT." node))
400 (when (eq node last) (return))
401 (unless (eq (continuation-kind cont) :inside-block)
402 (barf "The interior continuation ~S in ~S has the wrong kind."
405 (unless (continuation-next cont)
406 (barf "~S has no NEXT." cont))
407 (unless (eq (continuation-use cont) node)
408 (barf "USE in ~S should be ~S." cont node))
409 (setq this-cont cont))))
411 (check-block-successors block))
414 ;;; Check that BLOCK is properly terminated. Each successor must be
415 ;;; accounted for by the type of the last node.
416 (declaim (ftype (function (cblock) (values)) check-block-successors))
417 (defun check-block-successors (block)
418 (let ((last (block-last block))
419 (succ (block-succ block)))
421 (let* ((comp (block-component block)))
423 (unless (gethash b *seen-blocks*)
424 (barf "unseen successor ~S in ~S" b block))
425 (unless (member block (block-pred b))
426 (barf "bad successor link ~S in ~S" b block))
427 (unless (eq (block-component b) comp)
428 (barf "The successor ~S in ~S is in a different component."
434 (unless (proper-list-of-length-p succ 1 2)
435 (barf "~S ends in an IF, but doesn't have one or two succesors."
437 (unless (member (if-consequent last) succ)
438 (barf "The CONSEQUENT for ~S isn't in SUCC for ~S." last block))
439 (unless (member (if-alternative last) succ)
440 (barf "The ALTERNATIVE for ~S isn't in SUCC for ~S." last block)))
442 (unless (if (eq (functional-kind (return-lambda last)) :deleted)
444 (and (= (length succ) 1)
446 (component-tail (block-component block)))))
447 (barf "strange successors for RETURN in ~S" block)))
449 (unless (proper-list-of-length-p succ 0 1)
450 (barf "EXIT node with strange number of successors: ~S" last)))
452 (unless (or (= (length succ) 1) (node-tail-p last)
453 (and (block-delete-p block) (null succ)))
454 (barf "~S ends in normal node, but doesn't have one successor."
458 ;;;; node consistency checking
460 ;;; Check that the DEST for CONT is the specified NODE. We also mark
461 ;;; the block CONT is in as SEEN.
462 (declaim (ftype (function (continuation node) (values)) check-dest))
463 (defun check-dest (cont node)
464 (let ((kind (continuation-kind cont)))
467 (unless (block-delete-p (node-block node))
468 (barf "DEST ~S of deleted continuation ~S is not DELETE-P."
470 (:deleted-block-start
471 (unless (eq (continuation-dest cont) node)
472 (barf "DEST for ~S should be ~S." cont node)))
473 ((:inside-block :block-start)
474 (unless (gethash (continuation-block cont) *seen-blocks*)
475 (barf "~S receives ~S, which is in an unknown block." node cont))
476 (unless (eq (continuation-dest cont) node)
477 (barf "DEST for ~S should be ~S." cont node)))))
480 ;;; This function deals with checking for consistency of the
481 ;;; type-dependent information in a node.
482 (defun check-node-consistency (node)
483 (declare (type node node))
486 (let ((leaf (ref-leaf node)))
487 (when (functional-p leaf)
488 (if (eq (functional-kind leaf) :toplevel-xep)
489 (unless (eq (component-kind (block-component (node-block node)))
491 (barf ":TOPLEVEL-XEP ref in non-top-level component: ~S"
493 (check-fun-reached leaf node)))))
495 (check-dest (basic-combination-fun node) node)
496 (dolist (arg (basic-combination-args node))
498 (arg (check-dest arg node))
499 ((not (and (eq (basic-combination-kind node) :local)
500 (combination-p node)))
501 (barf "flushed arg not in local call: ~S" node))
504 ;; KLUDGE: In sbcl-0.6.11.37, the compiler doesn't like
505 ;; (DECLARE (TYPE INDEX POS)) after the inline expansion of
506 ;; POSITION. It compiles it correctly, but it issues a type
507 ;; mismatch warning because it can't eliminate the
508 ;; possibility that control will flow through the
509 ;; NIL-returning branch. So we punt here. -- WHN 2001-04-15
510 (declare (notinline position))
511 (let ((fun (ref-leaf (continuation-use
512 (basic-combination-fun node))))
513 (pos (position arg (basic-combination-args node))))
514 (declare (type index pos))
515 (when (leaf-refs (elt (lambda-vars fun) pos))
516 (barf "flushed arg for referenced var in ~S" node)))))))
517 (let ((dest (continuation-dest (node-cont node))))
518 (when (and (return-p dest)
519 (eq (basic-combination-kind node) :local)
520 (not (eq (lambda-tail-set (combination-lambda node))
521 (lambda-tail-set (return-lambda dest)))))
522 (barf "tail local call to function with different tail set:~% ~S"
525 (check-dest (if-test node) node)
526 (unless (eq (block-last (node-block node)) node)
527 (barf "IF not at block end: ~S" node)))
529 (check-dest (set-value node) node))
531 (check-fun-reached (bind-lambda node) node))
533 (check-fun-reached (return-lambda node) node)
534 (check-dest (return-result node) node)
535 (unless (eq (block-last (node-block node)) node)
536 (barf "RETURN not at block end: ~S" node)))
538 (unless (member node (lambda-entries (node-home-lambda node)))
539 (barf "~S is not in ENTRIES for its home LAMBDA." node))
540 (dolist (exit (entry-exits node))
541 (unless (node-deleted exit)
542 (check-node-reached node))))
544 (let ((entry (exit-entry node))
545 (value (exit-value node)))
547 (check-node-reached entry)
548 (unless (member node (entry-exits entry))
549 (barf "~S is not in its ENTRY's EXITS." node))
551 (check-dest value node)))
554 (barf "~S has VALUE but no ENTRY." node)))))))
558 ;;;; IR2 consistency checking
560 ;;; Check for some kind of consistency in some REFs linked together by
561 ;;; TN-REF-ACROSS. VOP is the VOP that the references are in. WRITE-P
562 ;;; is the value of WRITE-P that should be present. COUNT is the
563 ;;; minimum number of operands expected. If MORE-P is true, then any
564 ;;; larger number will also be accepted. WHAT is a string describing
565 ;;; the kind of operand in error messages.
566 (defun check-tn-refs (refs vop write-p count more-p what)
567 (let ((vop-refs (vop-refs vop)))
568 (do ((ref refs (tn-ref-across ref))
572 (barf "There should be at least ~W ~A in ~S, but there are only ~W."
574 (when (and (not more-p) (> num count))
575 (barf "There should be ~W ~A in ~S, but are ~W."
576 count what vop num)))
577 (unless (eq (tn-ref-vop ref) vop)
578 (barf "VOP is ~S isn't ~S." ref vop))
579 (unless (eq (tn-ref-write-p ref) write-p)
580 (barf "The WRITE-P in ~S isn't ~S." vop write-p))
581 (unless (find-in #'tn-ref-next-ref ref vop-refs)
582 (barf "~S not found in REFS for ~S" ref vop))
583 (unless (find-in #'tn-ref-next ref
584 (if (tn-ref-write-p ref)
585 (tn-writes (tn-ref-tn ref))
586 (tn-reads (tn-ref-tn ref))))
587 (barf "~S not found in reads/writes for its TN" ref))
589 (let ((target (tn-ref-target ref)))
591 (unless (eq (tn-ref-write-p target) (not (tn-ref-write-p ref)))
592 (barf "The target for ~S isn't complementary WRITE-P." ref))
593 (unless (find-in #'tn-ref-next-ref target vop-refs)
594 (barf "The target for ~S isn't in REFS for ~S." ref vop)))))))
596 ;;; Verify the sanity of the VOP-Refs slot in VOP. This involves checking
597 ;;; that each referenced TN appears as an argument, result or temp, and also
598 ;;; basic checks for the plausibility of the specified ordering of the refs.
599 (defun check-vop-refs (vop)
600 (declare (type vop vop))
601 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
604 ((find-in #'tn-ref-across ref (vop-args vop)))
605 ((find-in #'tn-ref-across ref (vop-results vop)))
606 ((not (eq (tn-ref-vop ref) vop))
607 (barf "VOP in ~S isn't ~S." ref vop))
608 ((find-in #'tn-ref-across ref (vop-temps vop)))
609 ((tn-ref-write-p ref)
610 (barf "stray ref that isn't a READ: ~S" ref))
612 (let* ((tn (tn-ref-tn ref))
613 (temp (find-in #'tn-ref-across tn (vop-temps vop)
616 (barf "stray ref with no corresponding temp write: ~S" ref))
617 (unless (find-in #'tn-ref-next-ref temp (tn-ref-next-ref ref))
618 (barf "Read is after write for temp ~S in refs of ~S."
622 ;;; Check the basic sanity of the VOP linkage, then call some other
623 ;;; functions to check on the TN-REFS. We grab some info out of the
624 ;;; VOP-INFO to tell us what to expect.
626 ;;; [### Check that operand type restrictions are met?]
627 (defun check-ir2-block-consistency (2block)
628 (declare (type ir2-block 2block))
629 (do ((vop (ir2-block-start-vop 2block)
633 (unless (eq prev (ir2-block-last-vop 2block))
634 (barf "The last VOP in ~S should be ~S." 2block prev)))
635 (unless (eq (vop-prev vop) prev)
636 (barf "PREV in ~S should be ~S." vop prev))
638 (unless (eq (vop-block vop) 2block)
639 (barf "BLOCK in ~S should be ~S." vop 2block))
643 (let* ((info (vop-info vop))
644 (atypes (template-arg-types info))
645 (rtypes (template-result-types info)))
646 (check-tn-refs (vop-args vop) vop nil
647 (count-if-not (lambda (x)
649 (eq (car x) :constant)))
651 (template-more-args-type info) "args")
652 (check-tn-refs (vop-results vop) vop t
653 (if (eq rtypes :conditional) 0 (length rtypes))
654 (template-more-results-type info) "results")
655 (check-tn-refs (vop-temps vop) vop t 0 t "temps")
656 (unless (= (length (vop-codegen-info vop))
657 (template-info-arg-count info))
658 (barf "wrong number of codegen info args in ~S" vop))))
661 ;;; Check stuff about the IR2 representation of COMPONENT. This assumes the
662 ;;; sanity of the basic flow graph.
664 ;;; [### Also grovel global TN data structures? Assume pack not
665 ;;; done yet? Have separate CHECK-TN-CONSISTENCY for pre-pack and
666 ;;; CHECK-PACK-CONSISTENCY for post-pack?]
667 (defun check-ir2-consistency (component)
668 (declare (type component component))
669 (do-ir2-blocks (block component)
670 (check-ir2-block-consistency block))
673 ;;;; lifetime analysis checking
675 ;;; Dump some info about how many TNs there, and what the conflicts data
676 ;;; structures are like.
677 (defun pre-pack-tn-stats (component &optional (stream *error-output*))
678 (declare (type component component))
688 (do-packed-tns (tn component)
689 (let ((reads (tn-reads tn))
690 (writes (tn-writes tn)))
691 (when (and reads writes
692 (not (tn-ref-next reads)) (not (tn-ref-next writes))
693 (eq (tn-ref-vop reads) (tn-ref-vop writes)))
697 (unless (or (tn-reads tn) (tn-writes tn))
699 (cond ((eq (tn-kind tn) :component)
701 ((tn-global-conflicts tn)
703 ((:environment :debug-environment) (incf environment))
705 (do ((conf (tn-global-conflicts tn)
706 (global-conflicts-tn-next conf)))
712 (do ((tn (ir2-component-constant-tns (component-info component))
718 "~%TNs: ~W local, ~W temps, ~W constant, ~W env, ~W comp, ~W global.~@
719 Wired: ~W, Unused: ~W. ~W block~:P, ~W global conflict~:P.~%"
720 local temps const environment comp global wired unused
721 (ir2-block-count component)
725 ;;; If the entry in Local-TNs for TN in BLOCK is :MORE, then do some checks
726 ;;; for the validity of the usage.
727 (defun check-more-tn-entry (tn block)
728 (let* ((vop (ir2-block-start-vop block))
729 (info (vop-info vop)))
730 (macrolet ((frob (more-p ops)
732 (find-in #'tn-ref-across tn (,ops vop)
734 (unless (and (eq vop (ir2-block-last-vop block))
735 (or (frob template-more-args-type vop-args)
736 (frob template-more-results-type vop-results)))
737 (barf "strange :MORE LTN entry for ~S in ~S" tn block))))
740 (defun check-tn-conflicts (component)
741 (do-packed-tns (tn component)
742 (unless (or (not (eq (tn-kind tn) :normal))
745 (barf "no references to ~S" tn))
747 (unless (tn-sc tn) (barf "~S has no SC." tn))
749 (let ((conf (tn-global-conflicts tn))
752 ((eq kind :component)
753 (unless (member tn (ir2-component-component-tns
754 (component-info component)))
755 (barf "~S not in COMPONENT-TNs for ~S" tn component)))
757 (do ((conf conf (global-conflicts-tn-next conf))
760 (unless (eq (global-conflicts-tn conf) tn)
761 (barf "TN in ~S should be ~S." conf tn))
763 (unless (eq (global-conflicts-kind conf) :live)
764 (let* ((block (global-conflicts-block conf))
765 (ltn (svref (ir2-block-local-tns block)
766 (global-conflicts-number conf))))
768 ((eq ltn :more) (check-more-tn-entry tn block))
770 (barf "~S wrong in LTN map for ~S" conf tn)))))
773 (unless (> (ir2-block-number (global-conflicts-block conf))
774 (ir2-block-number (global-conflicts-block prev)))
775 (barf "~s and ~s out of order" prev conf)))))
776 ((member (tn-kind tn) '(:constant :specified-save)))
778 (let ((local (tn-local tn)))
780 (barf "~S has no global conflicts, but isn't local either." tn))
781 (unless (eq (svref (ir2-block-local-tns local)
782 (tn-local-number tn))
784 (barf "~S wrong in LTN map" tn))
785 (do ((ref (tn-reads tn) (tn-ref-next ref)))
787 (unless (eq (vop-block (tn-ref-vop ref)) local)
788 (barf "~S has references in blocks other than its LOCAL block."
790 (do ((ref (tn-writes tn) (tn-ref-next ref)))
792 (unless (eq (vop-block (tn-ref-vop ref)) local)
793 (barf "~S has references in blocks other than its LOCAL block."
797 (defun check-block-conflicts (component)
798 (do-ir2-blocks (block component)
799 (do ((conf (ir2-block-global-tns block)
800 (global-conflicts-next conf))
804 (unless (> (tn-number (global-conflicts-tn conf))
805 (tn-number (global-conflicts-tn prev)))
806 (barf "~S and ~S out of order in ~S" prev conf block)))
808 (unless (find-in #'global-conflicts-tn-next
811 (global-conflicts-tn conf)))
812 (barf "~S missing from global conflicts of its TN" conf)))
814 (let ((map (ir2-block-local-tns block)))
815 (dotimes (i (ir2-block-local-tn-count block))
816 (let ((tn (svref map i)))
817 (unless (or (eq tn :more)
819 (tn-global-conflicts tn)
820 (eq (tn-local tn) block))
821 (barf "strange TN ~S in LTN map for ~S" tn block)))))))
823 ;;; All TNs live at the beginning of an environment must be passing
824 ;;; locations associated with that environment. We make an exception
825 ;;; for wired TNs in XEP functions, since we randomly reference wired
826 ;;; TNs to access the full call passing locations.
827 (defun check-environment-lifetimes (component)
828 (dolist (fun (component-lambdas component))
829 (let* ((env (lambda-physenv fun))
830 (2env (physenv-info env))
831 (vars (lambda-vars fun))
832 (closure (ir2-physenv-closure 2env))
833 (pc (ir2-physenv-return-pc-pass 2env))
834 (fp (ir2-physenv-old-fp 2env))
835 (2block (block-info (lambda-block (physenv-lambda env)))))
836 (do ((conf (ir2-block-global-tns 2block)
837 (global-conflicts-next conf)))
839 (let ((tn (global-conflicts-tn conf)))
840 (unless (or (eq (global-conflicts-kind conf) :write)
843 (and (xep-p fun) (tn-offset tn))
844 (member (tn-kind tn) '(:environment :debug-environment))
845 (member tn vars :key #'leaf-info)
846 (member tn closure :key #'cdr))
847 (barf "strange TN live at head of ~S: ~S" env tn))))))
850 ;;; Check for some basic sanity in the TN conflict data structures,
851 ;;; and also check that no TNs are unexpectedly live at environment
853 (defun check-life-consistency (component)
854 (check-tn-conflicts component)
855 (check-block-conflicts component)
856 (check-environment-lifetimes component))
858 ;;;; pack consistency checking
860 (defun check-pack-consistency (component)
861 (flet ((check (scs ops)
862 (do ((scs scs (cdr scs))
863 (op ops (tn-ref-across op)))
865 (let ((load-tn (tn-ref-load-tn op)))
866 (unless (eq (svref (car scs)
869 (or load-tn (tn-ref-tn op)))))
871 (barf "operand restriction not satisfied: ~S" op))))))
872 (do-ir2-blocks (block component)
873 (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
875 (let ((info (vop-info vop)))
876 (check (vop-info-result-load-scs info) (vop-results vop))
877 (check (vop-info-arg-load-scs info) (vop-args vop))))))
880 ;;;; data structure dumping routines
882 ;;; When we print CONTINUATIONs and TNs, we assign them small numeric
883 ;;; IDs so that we can get a handle on anonymous objects given a
887 ;;; * Perhaps this machinery should be #!+SB-SHOW.
888 ;;; * Probably the hash tables should either be weak hash tables,
889 ;;; or only allocated within a single compilation unit. Otherwise
890 ;;; there will be a tendency for them to grow without bound and
891 ;;; keep garbage from being collected.
892 (macrolet ((def (counter vto vfrom fto ffrom)
894 (defvar ,vto (make-hash-table :test 'eq))
895 (defvar ,vfrom (make-hash-table :test 'eql))
896 (proclaim '(hash-table ,vto ,vfrom))
898 (proclaim '(fixnum ,counter))
902 (let ((num (incf ,counter)))
903 (setf (gethash num ,vfrom) x)
904 (setf (gethash x ,vto) num))))
907 (values (gethash num ,vfrom))))))
908 (def *continuation-number* *continuation-numbers* *number-continuations*
910 (def *tn-id* *tn-ids* *id-tns* tn-id id-tn)
911 (def *label-id* *id-labels* *label-ids* label-id id-label))
913 ;;; Print a terse one-line description of LEAF.
914 (defun print-leaf (leaf &optional (stream *standard-output*))
915 (declare (type leaf leaf) (type stream stream))
917 (lambda-var (prin1 (leaf-debug-name leaf) stream))
918 (constant (format stream "'~S" (constant-value leaf)))
920 (format stream "~S {~A}" (leaf-debug-name leaf) (global-var-kind leaf)))
922 (format stream "~S ~S" (type-of leaf) (functional-debug-name leaf)))))
924 ;;; Attempt to find a block given some thing that has to do with it.
925 (declaim (ftype (function (t) cblock) block-or-lose))
926 (defun block-or-lose (thing)
929 (ir2-block (ir2-block-block thing))
930 (vop (block-or-lose (vop-block thing)))
931 (tn-ref (block-or-lose (tn-ref-vop thing)))
932 (continuation (continuation-block thing))
933 (node (node-block thing))
934 (component (component-head thing))
935 #| (cloop (loop-head thing))|#
936 (integer (continuation-block (num-cont thing)))
937 (functional (lambda-block (main-entry thing)))
938 (null (error "Bad thing: ~S." thing))
939 (symbol (block-or-lose (gethash thing *free-funs*)))))
942 (defun print-continuation (cont)
943 (declare (type continuation cont))
944 (format t " c~D" (cont-num cont))
947 ;;; Print out the nodes in BLOCK in a format oriented toward
948 ;;; representing what the code does.
949 (defun print-nodes (block)
950 (setq block (block-or-lose block))
951 (format t "~%block start c~D" (cont-num (block-start block)))
953 (let ((last (block-last block)))
955 (do ((cont (block-start block) (node-cont (continuation-next cont))))
957 (let ((node (continuation-next cont)))
958 (format t "~3D: " (cont-num (node-cont node)))
960 (ref (print-leaf (ref-leaf node)))
962 (let ((kind (basic-combination-kind node)))
963 (format t "~(~A ~A~) c~D"
964 (if (fun-info-p kind) "known" kind)
966 (cont-num (basic-combination-fun node)))
967 (dolist (arg (basic-combination-args node))
969 (print-continuation arg)
970 (format t " <none>")))))
972 (write-string "set ")
973 (print-leaf (set-var node))
974 (print-continuation (set-value node)))
976 (format t "if c~D" (cont-num (if-test node)))
977 (print-continuation (block-start (if-consequent node)))
978 (print-continuation (block-start (if-alternative node))))
980 (write-string "bind ")
981 (print-leaf (bind-lambda node)))
983 (format t "return c~D " (cont-num (return-result node)))
984 (print-leaf (return-lambda node)))
986 (format t "entry ~S" (entry-exits node)))
988 (let ((value (exit-value node)))
990 (format t "exit c~D" (cont-num value)))
992 (format t "exit <no value>"))
994 (format t "exit <degenerate>"))))))
996 (when (eq node last) (return)))))
998 (let ((succ (block-succ block)))
999 (format t "successors~{ c~D~}~%"
1000 (mapcar (lambda (x) (cont-num (block-start x))) succ)))
1003 ;;; Print the guts of a TN. (logic shared between PRINT-OBJECT (TN T)
1004 ;;; and printers for compound objects which contain TNs)
1005 (defun print-tn-guts (tn &optional (stream *standard-output*))
1006 (declare (type tn tn))
1007 (let ((leaf (tn-leaf tn)))
1009 (print-leaf leaf stream)
1010 (format stream "!~D" (tn-id tn)))
1012 (format stream "t~D" (tn-id tn))))
1013 (when (and (tn-sc tn) (tn-offset tn))
1014 (format stream "[~A]" (location-print-name tn)))))
1016 ;;; Print the TN-REFs representing some operands to a VOP, linked by
1018 (defun print-operands (refs)
1019 (declare (type (or tn-ref null) refs))
1020 (pprint-logical-block (*standard-output* nil)
1021 (do ((ref refs (tn-ref-across ref)))
1023 (let ((tn (tn-ref-tn ref))
1024 (ltn (tn-ref-load-tn ref)))
1029 (princ (if (tn-ref-write-p ref) #\< #\>))
1030 (print-tn-guts ltn)))
1032 (pprint-newline :fill)))))
1034 ;;; Print the VOP, putting args, info and results on separate lines, if
1036 (defun print-vop (vop)
1037 (pprint-logical-block (*standard-output* nil)
1038 (princ (vop-info-name (vop-info vop)))
1040 (pprint-indent :current 0)
1041 (print-operands (vop-args vop))
1042 (pprint-newline :linear)
1043 (when (vop-codegen-info vop)
1044 (princ (with-output-to-string (stream)
1045 (let ((*print-level* 1)
1047 (format stream "{~{~S~^ ~}} " (vop-codegen-info vop)))))
1048 (pprint-newline :linear))
1049 (when (vop-results vop)
1051 (print-operands (vop-results vop))))
1054 ;;; Print the VOPs in the specified IR2 block.
1055 (defun print-ir2-block (block)
1056 (declare (type ir2-block block))
1058 ((eq (block-info (ir2-block-block block)) block)
1059 (format t "~%IR2 block start c~D~%"
1060 (cont-num (block-start (ir2-block-block block))))
1061 (let ((label (ir2-block-%label block)))
1063 (format t "L~D:~%" (label-id label)))))
1065 (format t "<overflow>~%")))
1067 (do ((vop (ir2-block-start-vop block)
1069 (number 0 (1+ number)))
1071 (format t "~W: " number)
1074 ;;; This is like PRINT-NODES, but dumps the IR2 representation of the
1076 (defun print-vops (block)
1077 (setq block (block-or-lose block))
1078 (let ((2block (block-info block)))
1079 (print-ir2-block 2block)
1080 (do ((b (ir2-block-next 2block) (ir2-block-next b)))
1081 ((not (eq (ir2-block-block b) block)))
1082 (print-ir2-block b)))
1085 ;;; Scan the IR2 blocks in emission order.
1086 (defun print-ir2-blocks (thing)
1087 (do-ir2-blocks (block (block-component (block-or-lose thing)))
1088 (print-ir2-block block))
1091 ;;; Do a PRINT-NODES on BLOCK and all blocks reachable from it by
1092 ;;; successor links.
1093 (defun print-blocks (block)
1094 (setq block (block-or-lose block))
1095 (do-blocks (block (block-component block) :both)
1096 (setf (block-flag block) nil))
1097 (labels ((walk (block)
1098 (unless (block-flag block)
1099 (setf (block-flag block) t)
1100 (when (block-start block)
1101 (print-nodes block))
1102 (dolist (block (block-succ block))
1107 ;;; Print all blocks in BLOCK's component in DFO.
1108 (defun print-all-blocks (thing)
1109 (do-blocks (block (block-component (block-or-lose thing)))
1110 (handler-case (print-nodes block)
1112 (format t "~&~A...~%" condition))))
1115 (defvar *list-conflicts-table* (make-hash-table :test 'eq))
1117 ;;; Add all ALWAYS-LIVE TNs in Block to the conflicts. TN is ignored when
1118 ;;; it appears in the global conflicts.
1119 (defun add-always-live-tns (block tn)
1120 (declare (type ir2-block block) (type tn tn))
1121 (do ((conf (ir2-block-global-tns block)
1122 (global-conflicts-next conf)))
1124 (when (eq (global-conflicts-kind conf) :live)
1125 (let ((btn (global-conflicts-tn conf)))
1127 (setf (gethash btn *list-conflicts-table*) t)))))
1130 ;;; Add all local TNs in block to the conflicts.
1131 (defun add-all-local-tns (block)
1132 (declare (type ir2-block block))
1133 (let ((ltns (ir2-block-local-tns block)))
1134 (dotimes (i (ir2-block-local-tn-count block))
1135 (setf (gethash (svref ltns i) *list-conflicts-table*) t)))
1138 ;;; Make a list out of all of the recorded conflicts.
1139 (defun listify-conflicts-table ()
1141 (maphash (lambda (k v)
1142 (declare (ignore v))
1145 *list-conflicts-table*)
1146 (clrhash *list-conflicts-table*)
1149 ;;; Return a list of a the TNs that conflict with TN. Sort of, kind
1150 ;;; of. For debugging use only. Probably doesn't work on :COMPONENT TNs.
1151 (defun list-conflicts (tn)
1152 (aver (member (tn-kind tn) '(:normal :environment :debug-environment)))
1153 (let ((confs (tn-global-conflicts tn)))
1155 (clrhash *list-conflicts-table*)
1156 (do ((conf confs (global-conflicts-tn-next conf)))
1158 (let ((block (global-conflicts-block conf)))
1159 (add-always-live-tns block tn)
1160 (if (eq (global-conflicts-kind conf) :live)
1161 (add-all-local-tns block)
1162 (let ((bconf (global-conflicts-conflicts conf))
1163 (ltns (ir2-block-local-tns block)))
1164 (dotimes (i (ir2-block-local-tn-count block))
1165 (when (/= (sbit bconf i) 0)
1166 (setf (gethash (svref ltns i) *list-conflicts-table*)
1168 (listify-conflicts-table))
1170 (let* ((block (tn-local tn))
1171 (ltns (ir2-block-local-tns block))
1172 (confs (tn-local-conflicts tn)))
1174 (dotimes (i (ir2-block-local-tn-count block))
1175 (when (/= (sbit confs i) 0)
1176 (let ((tn (svref ltns i)))
1177 (when (and tn (not (eq tn :more))
1178 (not (tn-global-conflicts tn)))
1180 (do ((gtn (ir2-block-global-tns block)
1181 (global-conflicts-next gtn)))
1183 (when (or (eq (global-conflicts-kind gtn) :live)
1184 (/= (sbit confs (global-conflicts-number gtn)) 0))
1185 (res (global-conflicts-tn gtn))))
1188 (defun nth-vop (thing n)
1190 "Return the Nth VOP in the IR2-Block pointed to by Thing."
1191 (let ((block (block-info (block-or-lose thing))))
1193 (vop (ir2-block-start-vop block) (vop-next vop)))