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. Otherwise
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 appear
53 ;;; in the DFO for one of the specified components.
54 (defvar *seen-blocks* (make-hash-table :test 'eq))
56 ;;; *Seen-Functions* is similar, but records all the lambdas we reached by
57 ;;; recursing on top-level functions.
58 (defvar *seen-functions* (make-hash-table :test 'eq))
60 ;;; Barf if Node is in a block which wasn't reached during the graph walk.
61 (declaim (ftype (function (node) (values)) check-node-reached))
62 (defun check-node-reached (node)
63 (unless (gethash (continuation-block (node-prev node)) *seen-blocks*)
64 (barf "~S was not reached." node))
67 ;;; Check everything that we can think of for consistency. When a definite
68 ;;; inconsistency is detected, we BARF. Possible problems just cause us to
69 ;;; BURP. Our argument is a list of components, but we also look at the
70 ;;; *FREE-VARIABLES*, *FREE-FUNCTIONS* and *CONSTANTS*.
72 ;;; First we do a pre-pass which finds all the blocks and lambdas, testing
73 ;;; that they are linked together properly and entering them in hashtables.
74 ;;; Next, we iterate over the blocks again, looking at the actual code and
75 ;;; control flow. Finally, we scan the global leaf hashtables, looking for
77 (declaim (ftype (function (list) (values)) check-ir1-consistency))
78 (defun check-ir1-consistency (components)
79 (clrhash *seen-blocks*)
80 (clrhash *seen-functions*)
81 (dolist (c components)
82 (let* ((head (component-head c))
83 (tail (component-tail c)))
84 (unless (and (null (block-pred head)) (null (block-succ tail)))
85 (barf "~S is malformed." c))
88 (block head (block-next block)))
90 (unless (eq prev tail)
91 (barf "wrong Tail for DFO, ~S in ~S" prev c)))
92 (setf (gethash block *seen-blocks*) t)
93 (unless (eq (block-prev block) prev)
94 (barf "bad PREV for ~S, should be ~S" block prev))
95 (unless (or (eq block tail)
96 (eq (block-component block) c))
97 (barf "~S is not in ~S." block c)))
99 (when (or (loop-blocks c) (loop-inferiors c))
100 (do-blocks (block c :both)
101 (setf (block-flag block) nil))
102 (check-loop-consistency c nil)
103 (do-blocks (block c :both)
104 (unless (block-flag block)
105 (barf "~S was not in any loop." block))))
109 (check-function-consistency components)
111 (dolist (c components)
112 (do ((block (block-next (component-head c)) (block-next block)))
113 ((null (block-next block)))
114 (check-block-consistency block)))
116 (maphash #'(lambda (k v)
118 (unless (or (constant-p v)
119 (and (global-var-p v)
120 (member (global-var-kind v)
121 '(:global :special :constant))))
122 (barf "strange *FREE-VARIABLES* entry: ~S" v))
123 (dolist (n (leaf-refs v))
124 (check-node-reached n))
125 (when (basic-var-p v)
126 (dolist (n (basic-var-sets v))
127 (check-node-reached n))))
130 (maphash #'(lambda (k v)
132 (unless (constant-p v)
133 (barf "strange *CONSTANTS* entry: ~S" v))
134 (dolist (n (leaf-refs v))
135 (check-node-reached n)))
138 (maphash #'(lambda (k v)
140 (unless (or (functional-p v)
141 (and (global-var-p v)
142 (eq (global-var-kind v) :global-function)))
143 (barf "strange *FREE-FUNCTIONS* entry: ~S" v))
144 (dolist (n (leaf-refs v))
145 (check-node-reached n)))
147 (clrhash *seen-functions*)
148 (clrhash *seen-blocks*)
151 ;;;; function consistency checking
153 (defun observe-functional (x)
154 (declare (type functional x))
155 (when (gethash x *seen-functions*)
156 (barf "~S was seen more than once." x))
157 (unless (eq (functional-kind x) :deleted)
158 (setf (gethash x *seen-functions*) t)))
160 ;;; Check that the specified function has been seen.
161 (defun check-function-reached (fun where)
162 (declare (type functional fun))
163 (unless (gethash fun *seen-functions*)
164 (barf "unseen function ~S in ~S" fun where)))
166 ;;; In a lambda, check that the associated nodes are in seen blocks. In an
167 ;;; optional dispatch, check that the entry points were seen. If the function
168 ;;; is deleted, ignore it.
169 (defun check-function-stuff (functional)
170 (ecase (functional-kind functional)
172 (let ((fun (functional-entry-function functional)))
173 (check-function-reached fun functional)
174 (when (functional-kind fun)
175 (barf "The function for XEP ~S has kind." functional))
176 (unless (eq (functional-entry-function fun) functional)
177 (barf "bad back-pointer in function for XEP ~S" functional))))
178 ((:let :mv-let :assignment)
179 (check-function-reached (lambda-home functional) functional)
180 (when (functional-entry-function functional)
181 (barf "The LET ~S has entry function." functional))
182 (unless (member functional (lambda-lets (lambda-home functional)))
183 (barf "The LET ~S is not in LETs for HOME." functional))
184 (unless (eq (functional-kind functional) :assignment)
185 (when (rest (leaf-refs functional))
186 (barf "The LET ~S has multiple references." functional)))
187 (when (lambda-lets functional)
188 (barf "LETs in a LET: ~S" functional)))
190 (when (functional-entry-function functional)
191 (barf ":OPTIONAL ~S has an ENTRY-FUNCTION." functional))
192 (let ((ef (lambda-optional-dispatch functional)))
193 (check-function-reached ef functional)
194 (unless (or (member functional (optional-dispatch-entry-points ef))
195 (eq functional (optional-dispatch-more-entry ef))
196 (eq functional (optional-dispatch-main-entry ef)))
197 (barf ":OPTIONAL ~S is not an e-p for its OPTIONAL-DISPATCH ~S."
200 (unless (eq (functional-entry-function functional) functional)
201 (barf "The ENTRY-FUNCTION in ~S isn't a self-pointer." functional)))
202 ((nil :escape :cleanup)
203 (let ((ef (functional-entry-function functional)))
205 (check-function-reached ef functional)
206 (unless (eq (functional-kind ef) :external)
207 (barf "The ENTRY-FUNCTION in ~S isn't an XEP: ~S."
211 (return-from check-function-stuff)))
213 (case (functional-kind functional)
214 ((nil :optional :external :top-level :escape :cleanup)
215 (when (lambda-p functional)
216 (dolist (fun (lambda-lets functional))
217 (unless (eq (lambda-home fun) functional)
218 (barf "The home in ~S is not ~S." fun functional))
219 (check-function-reached fun functional))
220 (unless (eq (lambda-home functional) functional)
221 (barf "home not self-pointer in ~S" functional)))))
223 (etypecase functional
225 (when (lambda-bind functional)
226 (check-node-reached (lambda-bind functional)))
227 (when (lambda-return functional)
228 (check-node-reached (lambda-return functional)))
230 (dolist (var (lambda-vars functional))
231 (dolist (ref (leaf-refs var))
232 (check-node-reached ref))
233 (dolist (set (basic-var-sets var))
234 (check-node-reached set))
235 (unless (eq (lambda-var-home var) functional)
236 (barf "HOME in ~S should be ~S." var functional))))
238 (dolist (ep (optional-dispatch-entry-points functional))
239 (check-function-reached ep functional))
240 (let ((more (optional-dispatch-more-entry functional)))
241 (when more (check-function-reached more functional)))
242 (check-function-reached (optional-dispatch-main-entry functional)
245 (defun check-function-consistency (components)
246 (dolist (c components)
247 (dolist (fun (component-new-functions c))
248 (observe-functional fun))
249 (dolist (fun (component-lambdas c))
250 (when (eq (functional-kind fun) :external)
251 (let ((ef (functional-entry-function fun)))
252 (when (optional-dispatch-p ef)
253 (observe-functional ef))))
254 (observe-functional fun)
255 (dolist (let (lambda-lets fun))
256 (observe-functional let))))
258 (dolist (c components)
259 (dolist (fun (component-new-functions c))
260 (check-function-stuff fun))
261 (dolist (fun (component-lambdas c))
262 (when (eq (functional-kind fun) :deleted)
263 (barf "deleted lambda ~S in Lambdas for ~S" fun c))
264 (check-function-stuff fun)
265 (dolist (let (lambda-lets fun))
266 (check-function-stuff let)))))
268 ;;;; loop consistency checking
271 ;;; Descend through the loop nesting and check that the tree is well-formed
272 ;;; and that all blocks in the loops are known blocks. We also mark each block
273 ;;; that we see so that we can do a check later to detect blocks that weren't
275 (declaim (ftype (function (loop (or loop null)) (values)) check-loop-consistency))
276 (defun check-loop-consistency (loop superior)
277 (unless (eq (loop-superior loop) superior)
278 (barf "wrong superior in ~S, should be ~S" loop superior))
280 (/= (loop-depth loop) (1+ (loop-depth superior))))
281 (barf "wrong depth in ~S" loop))
283 (dolist (tail (loop-tail loop))
284 (check-loop-block tail loop))
285 (dolist (exit (loop-exits loop))
286 (check-loop-block exit loop))
287 (check-loop-block (loop-head loop) loop)
288 (unless (eq (block-loop (loop-head loop)) loop)
289 (barf "The head of ~S is not directly in the loop." loop))
291 (do ((block (loop-blocks loop) (block-loop-next block)))
293 (setf (block-flag block) t)
294 (unless (gethash block *seen-blocks*)
295 (barf "unseen block ~S in Blocks for ~S" block loop))
296 (unless (eq (block-loop block) loop)
297 (barf "wrong loop in ~S, should be ~S" block loop)))
299 (dolist (inferior (loop-inferiors loop))
300 (check-loop-consistency inferior loop))
303 ;;; Check that Block is either in Loop or an inferior.
304 (declaim (ftype (function (block loop) (values)) check-loop-block))
305 (defun check-loop-block (block loop)
306 (unless (gethash block *seen-blocks*)
307 (barf "unseen block ~S in loop info for ~S" block loop))
309 (if (eq (block-loop block) l)
311 (dolist (inferior (loop-inferiors l) nil)
312 (when (walk inferior) (return t))))))
314 (barf "~S is in loop info for ~S but not in the loop." block loop)))
319 ;;; Check a block for consistency at the general flow-graph level, and call
320 ;;; Check-Node-Consistency on each node to locally check for semantic
322 (declaim (ftype (function (cblock) (values)) check-block-consistency))
323 (defun check-block-consistency (block)
325 (dolist (pred (block-pred block))
326 (unless (gethash pred *seen-blocks*)
327 (barf "unseen predecessor ~S in ~S" pred block))
328 (unless (member block (block-succ pred))
329 (barf "bad predecessor link ~S in ~S" pred block)))
331 (let* ((fun (block-home-lambda block))
332 (fun-deleted (eq (functional-kind fun) :deleted))
333 (this-cont (block-start block))
334 (last (block-last block)))
336 (check-function-reached fun block))
337 (when (not this-cont)
338 (barf "~S has no START." block))
340 (barf "~S has no LAST." block))
341 (unless (eq (continuation-kind this-cont) :block-start)
342 (barf "The START of ~S has the wrong kind." block))
344 (let ((use (continuation-use this-cont))
345 (uses (block-start-uses block)))
346 (when (and (null use) (= (length uses) 1))
347 (barf "~S has a unique use, but no USE." this-cont))
349 (unless (eq (node-cont node) this-cont)
350 (barf "The USE ~S for START in ~S has wrong CONT." node block))
351 (check-node-reached node)))
353 (let* ((last-cont (node-cont last))
354 (cont-block (continuation-block last-cont))
355 (dest (continuation-dest last-cont)))
356 (ecase (continuation-kind last-cont)
358 (:deleted-block-start
359 (let ((dest (continuation-dest last-cont)))
361 (check-node-reached dest)))
362 (unless (member last (block-start-uses cont-block))
363 (barf "LAST in ~S is missing from uses of its Cont." block)))
365 (check-node-reached (continuation-next last-cont))
366 (unless (member last (block-start-uses cont-block))
367 (barf "LAST in ~S is missing from uses of its Cont." block)))
369 (unless (eq cont-block block)
370 (barf "CONT of LAST in ~S is in a different BLOCK." block))
371 (unless (eq (continuation-use last-cont) last)
372 (barf "USE is not LAST in CONT of LAST in ~S." block))
373 (when (continuation-next last-cont)
374 (barf "CONT of LAST has a NEXT in ~S." block))))
377 (check-node-reached dest)))
380 (unless (eq (continuation-block this-cont) block)
381 (barf "BLOCK in ~S should be ~S." this-cont block))
383 (let ((dest (continuation-dest this-cont)))
385 (check-node-reached dest)))
387 (let ((node (continuation-next this-cont)))
388 (unless (node-p node)
389 (barf "~S has strange NEXT." this-cont))
390 (unless (eq (node-prev node) this-cont)
391 (barf "PREV in ~S should be ~S." node this-cont))
394 (check-node-consistency node))
396 (let ((cont (node-cont node)))
398 (barf "~S has no CONT." node))
399 (when (eq node last) (return))
400 (unless (eq (continuation-kind cont) :inside-block)
401 (barf "The interior continuation ~S in ~S has the wrong kind."
404 (unless (continuation-next cont)
405 (barf "~S has no NEXT." cont))
406 (unless (eq (continuation-use cont) node)
407 (barf "USE in ~S should be ~S." cont node))
408 (setq this-cont cont))))
410 (check-block-successors block))
413 ;;; Check that Block is properly terminated. Each successor must be
414 ;;; accounted for by the type of the last node.
415 (declaim (ftype (function (cblock) (values)) check-block-successors))
416 (defun check-block-successors (block)
417 (let ((last (block-last block))
418 (succ (block-succ block)))
420 (let* ((comp (block-component block)))
422 (unless (gethash b *seen-blocks*)
423 (barf "unseen successor ~S in ~S" b block))
424 (unless (member block (block-pred b))
425 (barf "bad successor link ~S in ~S" b block))
426 (unless (eq (block-component b) comp)
427 (barf "The successor ~S in ~S is in a different component."
433 (unless (proper-list-of-length-p succ 1 2)
434 (barf "~S ends in an IF, but doesn't have one or two succesors."
436 (unless (member (if-consequent last) succ)
437 (barf "The CONSEQUENT for ~S isn't in SUCC for ~S." last block))
438 (unless (member (if-alternative last) succ)
439 (barf "The ALTERNATIVE for ~S isn't in SUCC for ~S." last block)))
441 (unless (if (eq (functional-kind (return-lambda last)) :deleted)
443 (and (= (length succ) 1)
445 (component-tail (block-component block)))))
446 (barf "strange successors for RETURN in ~S" block)))
448 (unless (proper-list-of-length-p succ 0 1)
449 (barf "EXIT node with strange number of successors: ~S" last)))
451 (unless (or (= (length succ) 1) (node-tail-p last)
452 (and (block-delete-p block) (null succ)))
453 (barf "~S ends in normal node, but doesn't have one successor."
457 ;;;; node consistency checking
459 ;;; Check that the Dest for Cont is the specified Node. We also mark the
460 ;;; block Cont is in as Seen.
461 (declaim (ftype (function (continuation node) (values)) check-dest))
462 (defun check-dest (cont node)
463 (let ((kind (continuation-kind cont)))
466 (unless (block-delete-p (node-block node))
467 (barf "DEST ~S of deleted continuation ~S is not DELETE-P."
469 (:deleted-block-start
470 (unless (eq (continuation-dest cont) node)
471 (barf "DEST for ~S should be ~S." cont node)))
472 ((:inside-block :block-start)
473 (unless (gethash (continuation-block cont) *seen-blocks*)
474 (barf "~S receives ~S, which is in an unknown block." node cont))
475 (unless (eq (continuation-dest cont) node)
476 (barf "DEST for ~S should be ~S." cont node)))))
479 ;;; This function deals with checking for consistency the type-dependent
480 ;;; information in a node.
481 (defun check-node-consistency (node)
482 (declare (type node node))
485 (let ((leaf (ref-leaf node)))
486 (when (functional-p leaf)
487 (if (eq (functional-kind leaf) :top-level-xep)
488 (unless (eq (component-kind (block-component (node-block node)))
490 (barf ":TOP-LEVEL-XEP ref in non-top-level component: ~S"
492 (check-function-reached leaf node)))))
494 (check-dest (basic-combination-fun node) node)
495 (dolist (arg (basic-combination-args node))
497 (arg (check-dest arg node))
498 ((not (and (eq (basic-combination-kind node) :local)
499 (combination-p node)))
500 (barf "flushed arg not in local call: ~S" node))
502 (let ((fun (ref-leaf (continuation-use
503 (basic-combination-fun node))))
504 (pos (position arg (basic-combination-args node))))
505 (check-type pos fixnum) ; to suppress warning -- WHN 19990311
506 (when (leaf-refs (elt (lambda-vars fun) pos))
507 (barf "flushed arg for referenced var in ~S" node))))))
509 (let ((dest (continuation-dest (node-cont node))))
510 (when (and (return-p dest)
511 (eq (basic-combination-kind node) :local)
512 (not (eq (lambda-tail-set (combination-lambda node))
513 (lambda-tail-set (return-lambda dest)))))
514 (barf "tail local call to function with different tail set:~% ~S"
517 (check-dest (if-test node) node)
518 (unless (eq (block-last (node-block node)) node)
519 (barf "IF not at block end: ~S" node)))
521 (check-dest (set-value node) node))
523 (check-function-reached (bind-lambda node) node))
525 (check-function-reached (return-lambda node) node)
526 (check-dest (return-result node) node)
527 (unless (eq (block-last (node-block node)) node)
528 (barf "RETURN not at block end: ~S" node)))
530 (unless (member node (lambda-entries (node-home-lambda node)))
531 (barf "~S is not in ENTRIES for its home LAMBDA." node))
532 (dolist (exit (entry-exits node))
533 (unless (node-deleted exit)
534 (check-node-reached node))))
536 (let ((entry (exit-entry node))
537 (value (exit-value node)))
539 (check-node-reached entry)
540 (unless (member node (entry-exits entry))
541 (barf "~S is not in its ENTRY's EXITS." node))
543 (check-dest value node)))
546 (barf "~S has VALUE but no ENTRY." node)))))))
550 ;;;; IR2 consistency checking
552 ;;; Check for some kind of consistency in some Refs linked together by
553 ;;; TN-Ref-Across. VOP is the VOP that the references are in. Write-P is the
554 ;;; value of Write-P that should be present. Count is the minimum number of
555 ;;; operands expected. If More-P is true, then any larger number will also be
556 ;;; accepted. What is a string describing the kind of operand in error
558 (defun check-tn-refs (refs vop write-p count more-p what)
559 (let ((vop-refs (vop-refs vop)))
560 (do ((ref refs (tn-ref-across ref))
564 (barf "There should be at least ~D ~A in ~S, but are only ~D."
566 (when (and (not more-p) (> num count))
567 (barf "There should be ~D ~A in ~S, but are ~D."
568 count what vop num)))
569 (unless (eq (tn-ref-vop ref) vop)
570 (barf "VOP is ~S isn't ~S." ref vop))
571 (unless (eq (tn-ref-write-p ref) write-p)
572 (barf "The WRITE-P in ~S isn't ~S." vop write-p))
573 (unless (find-in #'tn-ref-next-ref ref vop-refs)
574 (barf "~S not found in REFS for ~S" ref vop))
575 (unless (find-in #'tn-ref-next ref
576 (if (tn-ref-write-p ref)
577 (tn-writes (tn-ref-tn ref))
578 (tn-reads (tn-ref-tn ref))))
579 (barf "~S not found in reads/writes for its TN" ref))
581 (let ((target (tn-ref-target ref)))
583 (unless (eq (tn-ref-write-p target) (not (tn-ref-write-p ref)))
584 (barf "The target for ~S isn't complementary WRITE-P." ref))
585 (unless (find-in #'tn-ref-next-ref target vop-refs)
586 (barf "The target for ~S isn't in REFS for ~S." ref vop)))))))
588 ;;; Verify the sanity of the VOP-Refs slot in VOP. This involves checking
589 ;;; that each referenced TN appears as an argument, result or temp, and also
590 ;;; basic checks for the plausibility of the specified ordering of the refs.
591 (defun check-vop-refs (vop)
592 (declare (type vop vop))
593 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
596 ((find-in #'tn-ref-across ref (vop-args vop)))
597 ((find-in #'tn-ref-across ref (vop-results vop)))
598 ((not (eq (tn-ref-vop ref) vop))
599 (barf "VOP in ~S isn't ~S." ref vop))
600 ((find-in #'tn-ref-across ref (vop-temps vop)))
601 ((tn-ref-write-p ref)
602 (barf "stray ref that isn't a READ: ~S" ref))
604 (let* ((tn (tn-ref-tn ref))
605 (temp (find-in #'tn-ref-across tn (vop-temps vop)
608 (barf "stray ref with no corresponding temp write: ~S" ref))
609 (unless (find-in #'tn-ref-next-ref temp (tn-ref-next-ref ref))
610 (barf "Read is after write for temp ~S in refs of ~S."
614 ;;; Check the basic sanity of the VOP linkage, then call some other
615 ;;; functions to check on the TN-Refs. We grab some info out of the VOP-Info
616 ;;; to tell us what to expect.
618 ;;; [### Check that operand type restrictions are met?]
619 (defun check-ir2-block-consistency (2block)
620 (declare (type ir2-block 2block))
621 (do ((vop (ir2-block-start-vop 2block)
625 (unless (eq prev (ir2-block-last-vop 2block))
626 (barf "The last VOP in ~S should be ~S." 2block prev)))
627 (unless (eq (vop-prev vop) prev)
628 (barf "PREV in ~S should be ~S." vop prev))
630 (unless (eq (vop-block vop) 2block)
631 (barf "BLOCK in ~S should be ~S." vop 2block))
635 (let* ((info (vop-info vop))
636 (atypes (template-arg-types info))
637 (rtypes (template-result-types info)))
638 (check-tn-refs (vop-args vop) vop nil
639 (count-if-not #'(lambda (x)
641 (eq (car x) :constant)))
643 (template-more-args-type info) "args")
644 (check-tn-refs (vop-results vop) vop t
645 (if (eq rtypes :conditional) 0 (length rtypes))
646 (template-more-results-type info) "results")
647 (check-tn-refs (vop-temps vop) vop t 0 t "temps")
648 (unless (= (length (vop-codegen-info vop))
649 (template-info-arg-count info))
650 (barf "wrong number of codegen info args in ~S" vop))))
653 ;;; Check stuff about the IR2 representation of Component. This assumes the
654 ;;; sanity of the basic flow graph.
656 ;;; [### Also grovel global TN data structures? Assume pack not
657 ;;; done yet? Have separate check-tn-consistency for pre-pack and
658 ;;; check-pack-consistency for post-pack?]
659 (defun check-ir2-consistency (component)
660 (declare (type component component))
661 (do-ir2-blocks (block component)
662 (check-ir2-block-consistency block))
665 ;;;; lifetime analysis checking
667 ;;; Dump some info about how many TNs there, and what the conflicts data
668 ;;; structures are like.
669 (defun pre-pack-tn-stats (component &optional (stream *error-output*))
670 (declare (type component component))
680 (do-packed-tns (tn component)
681 (let ((reads (tn-reads tn))
682 (writes (tn-writes tn)))
683 (when (and reads writes
684 (not (tn-ref-next reads)) (not (tn-ref-next writes))
685 (eq (tn-ref-vop reads) (tn-ref-vop writes)))
689 (unless (or (tn-reads tn) (tn-writes tn))
691 (cond ((eq (tn-kind tn) :component)
693 ((tn-global-conflicts tn)
695 ((:environment :debug-environment) (incf environment))
697 (do ((conf (tn-global-conflicts tn)
698 (global-conflicts-tn-next conf)))
704 (do ((tn (ir2-component-constant-tns (component-info component))
710 "~%TNs: ~D local, ~D temps, ~D constant, ~D env, ~D comp, ~D global.~@
711 Wired: ~D, Unused: ~D. ~D block~:P, ~D global conflict~:P.~%"
712 local temps const environment comp global wired unused
713 (ir2-block-count component)
717 ;;; If the entry in Local-TNs for TN in Block is :More, then do some checks
718 ;;; for the validity of the usage.
719 (defun check-more-tn-entry (tn block)
720 (let* ((vop (ir2-block-start-vop block))
721 (info (vop-info vop)))
722 (macrolet ((frob (more-p ops)
724 (find-in #'tn-ref-across tn (,ops vop)
726 (unless (and (eq vop (ir2-block-last-vop block))
727 (or (frob template-more-args-type vop-args)
728 (frob template-more-results-type vop-results)))
729 (barf "strange :MORE LTN entry for ~S in ~S" tn block))))
732 (defun check-tn-conflicts (component)
733 (do-packed-tns (tn component)
734 (unless (or (not (eq (tn-kind tn) :normal))
737 (barf "no references to ~S" tn))
739 (unless (tn-sc tn) (barf "~S has no SC." tn))
741 (let ((conf (tn-global-conflicts tn))
744 ((eq kind :component)
745 (unless (member tn (ir2-component-component-tns
746 (component-info component)))
747 (barf "~S not in Component-TNs for ~S" tn component)))
749 (do ((conf conf (global-conflicts-tn-next conf))
752 (unless (eq (global-conflicts-tn conf) tn)
753 (barf "TN in ~S should be ~S." conf tn))
755 (unless (eq (global-conflicts-kind conf) :live)
756 (let* ((block (global-conflicts-block conf))
757 (ltn (svref (ir2-block-local-tns block)
758 (global-conflicts-number conf))))
760 ((eq ltn :more) (check-more-tn-entry tn block))
762 (barf "~S wrong in LTN map for ~S" conf tn)))))
765 (unless (> (ir2-block-number (global-conflicts-block conf))
766 (ir2-block-number (global-conflicts-block prev)))
767 (barf "~s and ~s out of order" prev conf)))))
768 ((member (tn-kind tn) '(:constant :specified-save)))
770 (let ((local (tn-local tn)))
772 (barf "~S has no global conflicts, but isn't local either." tn))
773 (unless (eq (svref (ir2-block-local-tns local)
774 (tn-local-number tn))
776 (barf "~S wrong in LTN map" tn))
777 (do ((ref (tn-reads tn) (tn-ref-next ref)))
779 (unless (eq (vop-block (tn-ref-vop ref)) local)
780 (barf "~S has references in blocks other than its LOCAL block."
782 (do ((ref (tn-writes tn) (tn-ref-next ref)))
784 (unless (eq (vop-block (tn-ref-vop ref)) local)
785 (barf "~S has references in blocks other than its LOCAL block."
789 (defun check-block-conflicts (component)
790 (do-ir2-blocks (block component)
791 (do ((conf (ir2-block-global-tns block)
792 (global-conflicts-next conf))
796 (unless (> (tn-number (global-conflicts-tn conf))
797 (tn-number (global-conflicts-tn prev)))
798 (barf "~S and ~S out of order in ~S" prev conf block)))
800 (unless (find-in #'global-conflicts-tn-next
803 (global-conflicts-tn conf)))
804 (barf "~S missing from global conflicts of its TN" conf)))
806 (let ((map (ir2-block-local-tns block)))
807 (dotimes (i (ir2-block-local-tn-count block))
808 (let ((tn (svref map i)))
809 (unless (or (eq tn :more)
811 (tn-global-conflicts tn)
812 (eq (tn-local tn) block))
813 (barf "strange TN ~S in LTN map for ~S" tn block)))))))
815 ;;; All TNs live at the beginning of an environment must be passing
816 ;;; locations associated with that environment. We make an exception for wired
817 ;;; TNs in XEP functions, since we randomly reference wired TNs to access the
818 ;;; full call passing locations.
819 (defun check-environment-lifetimes (component)
820 (dolist (fun (component-lambdas component))
821 (let* ((env (lambda-environment fun))
822 (2env (environment-info env))
823 (vars (lambda-vars fun))
824 (closure (ir2-environment-environment 2env))
825 (pc (ir2-environment-return-pc-pass 2env))
826 (fp (ir2-environment-old-fp 2env))
830 (environment-function env))))))
831 (do ((conf (ir2-block-global-tns 2block)
832 (global-conflicts-next conf)))
834 (let ((tn (global-conflicts-tn conf)))
835 (unless (or (eq (global-conflicts-kind conf) :write)
838 (and (external-entry-point-p fun)
840 (member (tn-kind tn) '(:environment :debug-environment))
841 (member tn vars :key #'leaf-info)
842 (member tn closure :key #'cdr))
843 (barf "strange TN live at head of ~S: ~S" env tn))))))
846 ;;; Check for some basic sanity in the TN conflict data structures, and also
847 ;;; check that no TNs are unexpectedly live at environment entry.
848 (defun check-life-consistency (component)
849 (check-tn-conflicts component)
850 (check-block-conflicts component)
851 (check-environment-lifetimes component))
853 ;;;; pack consistency checking
855 (defun check-pack-consistency (component)
856 (flet ((check (scs ops)
857 (do ((scs scs (cdr scs))
858 (op ops (tn-ref-across op)))
860 (let ((load-tn (tn-ref-load-tn op)))
861 (unless (eq (svref (car scs)
864 (or load-tn (tn-ref-tn op)))))
866 (barf "operand restriction not satisfied: ~S" op))))))
867 (do-ir2-blocks (block component)
868 (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
870 (let ((info (vop-info vop)))
871 (check (vop-info-result-load-scs info) (vop-results vop))
872 (check (vop-info-arg-load-scs info) (vop-args vop))))))
875 ;;;; data structure dumping routines
877 ;;; When we print Continuations and TNs, we assign them small numeric IDs so
878 ;;; that we can get a handle on anonymous objects given a printout.
879 (macrolet ((def-frob (counter vto vfrom fto ffrom)
881 (defvar ,vto (make-hash-table :test 'eq))
882 (defvar ,vfrom (make-hash-table :test 'eql))
883 (proclaim '(hash-table ,vto ,vfrom))
885 (proclaim '(fixnum ,counter))
889 (let ((num (incf ,counter)))
890 (setf (gethash num ,vfrom) x)
891 (setf (gethash x ,vto) num))))
894 (values (gethash num ,vfrom))))))
895 (def-frob *continuation-number* *continuation-numbers* *number-continuations* cont-num num-cont)
896 (def-frob *tn-id* *tn-ids* *id-tns* tn-id id-tn)
897 (def-frob *label-id* *id-labels* *label-ids* label-id id-label))
899 ;;; Print out a terse one-line description of a leaf.
900 (defun print-leaf (leaf &optional (stream *standard-output*))
901 (declare (type leaf leaf) (type stream stream))
903 (lambda-var (prin1 (leaf-name leaf) stream))
904 (constant (format stream "'~S" (constant-value leaf)))
906 (format stream "~S {~A}" (leaf-name leaf) (global-var-kind leaf)))
908 (format stream "lambda ~S ~S" (leaf-name leaf)
909 (mapcar #'leaf-name (lambda-vars leaf))))
911 (format stream "optional-dispatch ~S" (leaf-name leaf)))
913 (assert (eq (functional-kind leaf) :top-level-xep))
914 (format stream "TL-XEP ~S"
915 (let ((info (leaf-info leaf)))
917 (entry-info (entry-info-name info))
918 (byte-lambda-info :byte-compiled-entry)))))))
920 ;;; Attempt to find a block given some thing that has to do with it.
921 (declaim (ftype (function (t) cblock) block-or-lose))
922 (defun block-or-lose (thing)
925 (ir2-block (ir2-block-block thing))
926 (vop (block-or-lose (vop-block thing)))
927 (tn-ref (block-or-lose (tn-ref-vop thing)))
928 (continuation (continuation-block thing))
929 (node (node-block thing))
930 (component (component-head thing))
931 #| (cloop (loop-head thing))|#
932 (integer (continuation-block (num-cont thing)))
933 (functional (node-block (lambda-bind (main-entry thing))))
934 (null (error "Bad thing: ~S." thing))
935 (symbol (block-or-lose (gethash thing *free-functions*)))))
938 (defun print-continuation (cont)
939 (declare (type continuation cont))
940 (format t " c~D" (cont-num cont))
943 ;;; Print out the nodes in Block in a format oriented toward representing
944 ;;; what the code does.
945 (defun print-nodes (block)
946 (setq block (block-or-lose block))
947 (format t "~%block start c~D" (cont-num (block-start block)))
949 (let ((last (block-last block)))
951 (do ((cont (block-start block) (node-cont (continuation-next cont))))
953 (let ((node (continuation-next cont)))
954 (format t "~3D: " (cont-num (node-cont node)))
956 (ref (print-leaf (ref-leaf node)))
958 (let ((kind (basic-combination-kind node)))
959 (format t "~(~A ~A~) c~D"
960 (if (function-info-p kind) "known" kind)
962 (cont-num (basic-combination-fun node)))
963 (dolist (arg (basic-combination-args node))
965 (print-continuation arg)
966 (format t " <none>")))))
968 (write-string "set ")
969 (print-leaf (set-var node))
970 (print-continuation (set-value node)))
972 (format t "if c~D" (cont-num (if-test node)))
973 (print-continuation (block-start (if-consequent node)))
974 (print-continuation (block-start (if-alternative node))))
976 (write-string "bind ")
977 (print-leaf (bind-lambda node)))
979 (format t "return c~D " (cont-num (return-result node)))
980 (print-leaf (return-lambda node)))
982 (format t "entry ~S" (entry-exits node)))
984 (let ((value (exit-value node)))
986 (format t "exit c~D" (cont-num value)))
988 (format t "exit <no value>"))
990 (format t "exit <degenerate>"))))))
992 (when (eq node last) (return)))))
994 (let ((succ (block-succ block)))
995 (format t "successors~{ c~D~}~%"
996 (mapcar #'(lambda (x) (cont-num (block-start x))) succ)))
999 ;;; Print a useful representation of a TN. If the TN has a leaf, then do a
1000 ;;; Print-Leaf on that, otherwise print a generated ID.
1001 (defun print-tn (tn &optional (stream *standard-output*))
1002 (declare (type tn tn))
1003 (let ((leaf (tn-leaf tn)))
1005 (print-leaf leaf stream)
1006 (format stream "!~D" (tn-id tn)))
1008 (format stream "t~D" (tn-id tn))))
1009 (when (and (tn-sc tn) (tn-offset tn))
1010 (format stream "[~A]" (location-print-name tn)))))
1012 ;;; Print the TN-Refs representing some operands to a VOP, linked by
1014 (defun print-operands (refs)
1015 (declare (type (or tn-ref null) refs))
1016 (pprint-logical-block (*standard-output* nil)
1017 (do ((ref refs (tn-ref-across ref)))
1019 (let ((tn (tn-ref-tn ref))
1020 (ltn (tn-ref-load-tn ref)))
1025 (princ (if (tn-ref-write-p ref) #\< #\>))
1028 (pprint-newline :fill)))))
1030 ;;; Print the vop, putting args, info and results on separate lines, if
1032 (defun print-vop (vop)
1033 (pprint-logical-block (*standard-output* nil)
1034 (princ (vop-info-name (vop-info vop)))
1036 (pprint-indent :current 0)
1037 (print-operands (vop-args vop))
1038 (pprint-newline :linear)
1039 (when (vop-codegen-info vop)
1040 (princ (with-output-to-string (stream)
1041 (let ((*print-level* 1)
1043 (format stream "{~{~S~^ ~}} " (vop-codegen-info vop)))))
1044 (pprint-newline :linear))
1045 (when (vop-results vop)
1047 (print-operands (vop-results vop))))
1050 ;;; Print the VOPs in the specified IR2 block.
1051 (defun print-ir2-block (block)
1052 (declare (type ir2-block block))
1054 ((eq (block-info (ir2-block-block block)) block)
1055 (format t "~%IR2 block start c~D~%"
1056 (cont-num (block-start (ir2-block-block block))))
1057 (let ((label (ir2-block-%label block)))
1059 (format t "L~D:~%" (label-id label)))))
1061 (format t "<overflow>~%")))
1063 (do ((vop (ir2-block-start-vop block)
1065 (number 0 (1+ number)))
1067 (format t "~D: " number)
1070 ;;; Like Print-Nodes, but dumps the IR2 representation of the code in Block.
1071 (defun print-vops (block)
1072 (setq block (block-or-lose block))
1073 (let ((2block (block-info block)))
1074 (print-ir2-block 2block)
1075 (do ((b (ir2-block-next 2block) (ir2-block-next b)))
1076 ((not (eq (ir2-block-block b) block)))
1077 (print-ir2-block b)))
1080 ;;; Scan the IR2 blocks in emission order.
1081 (defun print-ir2-blocks (thing)
1082 (do-ir2-blocks (block (block-component (block-or-lose thing)))
1083 (print-ir2-block block))
1086 ;;; Do a Print-Nodes on Block and all blocks reachable from it by successor
1088 (defun print-blocks (block)
1089 (setq block (block-or-lose block))
1090 (do-blocks (block (block-component block) :both)
1091 (setf (block-flag block) nil))
1092 (labels ((walk (block)
1093 (unless (block-flag block)
1094 (setf (block-flag block) t)
1095 (when (block-start block)
1096 (print-nodes block))
1097 (dolist (block (block-succ block))
1102 ;;; Print all blocks in Block's component in DFO.
1103 (defun print-all-blocks (thing)
1104 (do-blocks (block (block-component (block-or-lose thing)))
1105 (handler-case (print-nodes block)
1107 (format t "~&~A...~%" condition))))
1110 (defvar *list-conflicts-table* (make-hash-table :test 'eq))
1112 ;;; Add all Always-Live TNs in Block to the conflicts. TN is ignored when
1113 ;;; it appears in the global conflicts.
1114 (defun add-always-live-tns (block tn)
1115 (declare (type ir2-block block) (type tn tn))
1116 (do ((conf (ir2-block-global-tns block)
1117 (global-conflicts-next conf)))
1119 (when (eq (global-conflicts-kind conf) :live)
1120 (let ((btn (global-conflicts-tn conf)))
1122 (setf (gethash btn *list-conflicts-table*) t)))))
1125 ;;; Add all local TNs in block to the conflicts.
1126 (defun add-all-local-tns (block)
1127 (declare (type ir2-block block))
1128 (let ((ltns (ir2-block-local-tns block)))
1129 (dotimes (i (ir2-block-local-tn-count block))
1130 (setf (gethash (svref ltns i) *list-conflicts-table*) t)))
1133 ;;; Make a list out of all of the recorded conflicts.
1134 (defun listify-conflicts-table ()
1136 (maphash #'(lambda (k v)
1137 (declare (ignore v))
1140 *list-conflicts-table*)
1141 (clrhash *list-conflicts-table*)
1144 (defun list-conflicts (tn)
1146 "Return a list of a the TNs that conflict with TN. Sort of, kind of. For
1147 debugging use only. Probably doesn't work on :COMPONENT TNs."
1148 (assert (member (tn-kind tn) '(:normal :environment :debug-environment)))
1149 (let ((confs (tn-global-conflicts tn)))
1151 (clrhash *list-conflicts-table*)
1152 (do ((conf confs (global-conflicts-tn-next conf)))
1154 (let ((block (global-conflicts-block conf)))
1155 (add-always-live-tns block tn)
1156 (if (eq (global-conflicts-kind conf) :live)
1157 (add-all-local-tns block)
1158 (let ((bconf (global-conflicts-conflicts conf))
1159 (ltns (ir2-block-local-tns block)))
1160 (dotimes (i (ir2-block-local-tn-count block))
1161 (when (/= (sbit bconf i) 0)
1162 (setf (gethash (svref ltns i) *list-conflicts-table*)
1164 (listify-conflicts-table))
1166 (let* ((block (tn-local tn))
1167 (ltns (ir2-block-local-tns block))
1168 (confs (tn-local-conflicts tn)))
1170 (dotimes (i (ir2-block-local-tn-count block))
1171 (when (/= (sbit confs i) 0)
1172 (let ((tn (svref ltns i)))
1173 (when (and tn (not (eq tn :more))
1174 (not (tn-global-conflicts tn)))
1176 (do ((gtn (ir2-block-global-tns block)
1177 (global-conflicts-next gtn)))
1179 (when (or (eq (global-conflicts-kind gtn) :live)
1180 (/= (sbit confs (global-conflicts-number gtn)) 0))
1181 (res (global-conflicts-tn gtn))))
1184 (defun nth-vop (thing n)
1186 "Return the Nth VOP in the IR2-Block pointed to by Thing."
1187 (let ((block (block-info (block-or-lose thing))))
1189 (vop (ir2-block-start-vop block) (vop-next vop)))