sbcl-0.8.14.11:
[sbcl.git] / src / compiler / debug.lisp
1 ;;;; This file contains utilities for debugging the compiler --
2 ;;;; currently only stuff for checking the consistency of the IR1.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!C")
14
15 (defvar *args* ()
16   #!+sb-doc
17   "This variable is bound to the format arguments when an error is signalled
18   by BARF or BURP.")
19
20 (defvar *ignored-errors* (make-hash-table :test 'equal))
21
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*)
27     (restart-case
28         (apply #'error string *args*)
29       (continue ()
30         :report "Ignore this error.")
31       (ignore-all ()
32         :report "Ignore this and all future occurrences of this error."
33         (setf (gethash string *ignored-errors*) t))))
34   (values))
35
36 (defvar *burp-action* :warn
37   #!+sb-doc
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*))
41
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*)
46   (ecase *burp-action*
47     (:warn (apply #'warn string *args*))
48     (:error (apply #'cerror "press on anyway." string *args*))
49     (:none))
50   (values))
51
52 ;;; *SEEN-BLOCKS* is a hashtable with true values for all blocks which
53 ;;; appear in the DFO for one of the specified components.
54 ;;;
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))
61
62 ;;; Barf if NODE is in a block which wasn't reached during the graph
63 ;;; walk.
64 (declaim (ftype (function (node) (values)) check-node-reached))
65 (defun check-node-reached (node)
66   (unless (gethash (ctran-block (node-prev node)) *seen-blocks*)
67     (barf "~S was not reached." node))
68   (values))
69
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*.
74 ;;;
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*)
83   (clrhash *seen-funs*)
84   (dolist (c components)
85     (let* ((head (component-head c))
86            (tail (component-tail c)))
87       (unless (and (null (block-pred head))
88                    (null (block-succ tail)))
89         (barf "~S is malformed." c))
90
91       (do ((prev nil block)
92            (block head (block-next block)))
93           ((null block)
94            (unless (eq prev tail)
95              (barf "wrong TAIL for DFO, ~S in ~S" prev c)))
96         (setf (gethash block *seen-blocks*) t)
97         (unless (eq (block-prev block) prev)
98           (barf "bad PREV for ~S, should be ~S" block prev))
99         (unless (or (eq block tail)
100                     (eq (block-component block) c))
101           (barf "~S is not in ~S." block c)))
102 #|
103       (when (or (loop-blocks c) (loop-inferiors c))
104         (do-blocks (block c :both)
105           (setf (block-flag block) nil))
106         (check-loop-consistency c nil)
107         (do-blocks (block c :both)
108           (unless (block-flag block)
109             (barf "~S was not in any loop." block))))
110 |#
111     ))
112
113   (check-fun-consistency components)
114
115   (dolist (c components)
116     (do ((block (block-next (component-head c)) (block-next block)))
117         ((null (block-next block)))
118       (check-block-consistency block)))
119
120   (maphash (lambda (k v)
121              (declare (ignore k))
122              (unless (or (constant-p v)
123                          (and (global-var-p v)
124                               (member (global-var-kind v)
125                                       '(:global :special))))
126                (barf "strange *FREE-VARS* entry: ~S" v))
127              (dolist (n (leaf-refs v))
128                (check-node-reached n))
129              (when (basic-var-p v)
130                (dolist (n (basic-var-sets v))
131                  (check-node-reached n))))
132            *free-vars*)
133
134   (maphash (lambda (k v)
135              (declare (ignore k))
136              (unless (constant-p v)
137                (barf "strange *CONSTANTS* entry: ~S" v))
138              (dolist (n (leaf-refs v))
139                (check-node-reached n)))
140            *constants*)
141
142   (maphash (lambda (k v)
143              (declare (ignore k))
144              (unless (or (functional-p v)
145                          (and (global-var-p v)
146                               (eq (global-var-kind v) :global-function)))
147                (barf "strange *FREE-FUNS* entry: ~S" v))
148              (dolist (n (leaf-refs v))
149                (check-node-reached n)))
150            *free-funs*)
151   (clrhash *seen-funs*)
152   (clrhash *seen-blocks*)
153   (values))
154 \f
155 ;;;; function consistency checking
156
157 (defun observe-functional (x)
158   (declare (type functional x))
159   (when (gethash x *seen-funs*)
160     (barf "~S was seen more than once." x))
161   (unless (eq (functional-kind x) :deleted)
162     (setf (gethash x *seen-funs*) t)))
163
164 ;;; Check that the specified function has been seen.
165 (defun check-fun-reached (fun where)
166   (declare (type functional fun))
167   (unless (gethash fun *seen-funs*)
168     (barf "unseen function ~S in ~S" fun where)))
169
170 ;;; In a CLAMBDA, check that the associated nodes are in seen blocks.
171 ;;; In an OPTIONAL-DISPATCH, check that the entry points were seen. If
172 ;;; the function is deleted, ignore it.
173 (defun check-fun-stuff (functional)
174   (ecase (functional-kind functional)
175     (:external
176      (let ((fun (functional-entry-fun functional)))
177        (check-fun-reached fun functional)
178        (when (functional-kind fun)
179          (barf "The function for XEP ~S has kind." functional))
180        (unless (eq (functional-entry-fun fun) functional)
181          (barf "bad back-pointer in function for XEP ~S" functional))))
182     ((:let :mv-let :assignment) ; i.e. SOMEWHAT-LETLIKE-P
183      (check-fun-reached (lambda-home functional) functional)
184      (when (functional-entry-fun functional)
185        (barf "The LET ~S has entry function." functional))
186      (unless (member functional (lambda-lets (lambda-home functional)))
187        (barf "The LET ~S is not in LETs for HOME." functional))
188      (unless (eq (functional-kind functional) :assignment)
189        (when (rest (leaf-refs functional))
190          (barf "The LET ~S has multiple references." functional)))
191      (when (lambda-lets functional)
192        (barf "LETs in a LET: ~S" functional)))
193     (:optional
194      (when (functional-entry-fun functional)
195        (barf ":OPTIONAL ~S has an ENTRY-FUN." functional))
196      (let ((ef (lambda-optional-dispatch functional)))
197        (check-fun-reached ef functional)
198        (unless (or (member functional (optional-dispatch-entry-points ef))
199                    (eq functional (optional-dispatch-more-entry ef))
200                    (eq functional (optional-dispatch-main-entry ef)))
201          (barf ":OPTIONAL ~S is not an e-p for its OPTIONAL-DISPATCH ~S."
202                functional ef))))
203     (:toplevel
204      (unless (eq (functional-entry-fun functional) functional)
205        (barf "The ENTRY-FUN in ~S isn't a self-pointer." functional)))
206     ((nil :escape :cleanup)
207      (let ((ef (functional-entry-fun functional)))
208        (when ef
209          (check-fun-reached ef functional)
210          (unless (eq (functional-kind ef) :external)
211            (barf "The ENTRY-FUN in ~S isn't an XEP: ~S." functional ef)))))
212     (:deleted
213      (return-from check-fun-stuff)))
214
215   (case (functional-kind functional)
216     ((nil :optional :external :toplevel :escape :cleanup)
217      (when (lambda-p functional)
218        (dolist (fun (lambda-lets functional))
219          (unless (eq (lambda-home fun) functional)
220            (barf "The home in ~S is not ~S." fun functional))
221          (check-fun-reached fun functional))
222        (unless (eq (lambda-home functional) functional)
223          (barf "home not self-pointer in ~S" functional)))))
224
225   (etypecase functional
226     (clambda
227      (when (lambda-bind functional)
228        (check-node-reached (lambda-bind functional)))
229      (when (lambda-return functional)
230        (check-node-reached (lambda-return functional)))
231
232      (dolist (var (lambda-vars functional))
233        (dolist (ref (leaf-refs var))
234          (check-node-reached ref))
235        (dolist (set (basic-var-sets var))
236          (check-node-reached set))
237        (unless (eq (lambda-var-home var) functional)
238          (barf "HOME in ~S should be ~S." var functional))))
239     (optional-dispatch
240      (dolist (ep (optional-dispatch-entry-points functional))
241        (check-fun-reached ep functional))
242      (let ((more (optional-dispatch-more-entry functional)))
243        (when more (check-fun-reached more functional)))
244      (check-fun-reached (optional-dispatch-main-entry functional)
245                         functional))))
246
247 (defun check-fun-consistency (components)
248   (dolist (c components)
249     (dolist (new-fun (component-new-functionals c))
250       (observe-functional new-fun))
251     (dolist (fun (component-lambdas c))
252       (when (eq (functional-kind fun) :external)
253         (let ((ef (functional-entry-fun fun)))
254           (when (optional-dispatch-p ef)
255             (observe-functional ef))))
256       (observe-functional fun)
257       (dolist (let (lambda-lets fun))
258         (observe-functional let))))
259
260   (dolist (c components)
261     (dolist (new-fun (component-new-functionals c))
262       (check-fun-stuff new-fun))
263     (dolist (fun (component-lambdas c))
264       (when (eq (functional-kind fun) :deleted)
265         (barf "deleted lambda ~S in Lambdas for ~S" fun c))
266       (check-fun-stuff fun)
267       (dolist (let (lambda-lets fun))
268         (check-fun-stuff let)))))
269 \f
270 ;;;; loop consistency checking
271
272 #|
273 ;;; Descend through the loop nesting and check that the tree is well-formed
274 ;;; and that all blocks in the loops are known blocks. We also mark each block
275 ;;; that we see so that we can do a check later to detect blocks that weren't
276 ;;; in any loop.
277 (declaim (ftype (function (loop (or loop null)) (values)) check-loop-consistency))
278 (defun check-loop-consistency (loop superior)
279   (unless (eq (loop-superior loop) superior)
280     (barf "wrong superior in ~S, should be ~S" loop superior))
281   (when (and superior
282              (/= (loop-depth loop) (1+ (loop-depth superior))))
283     (barf "wrong depth in ~S" loop))
284
285   (dolist (tail (loop-tail loop))
286     (check-loop-block tail loop))
287   (dolist (exit (loop-exits loop))
288     (check-loop-block exit loop))
289   (check-loop-block (loop-head loop) loop)
290   (unless (eq (block-loop (loop-head loop)) loop)
291     (barf "The head of ~S is not directly in the loop." loop))
292
293   (do ((block (loop-blocks loop) (block-loop-next block)))
294       ((null block))
295     (setf (block-flag block) t)
296     (unless (gethash block *seen-blocks*)
297       (barf "unseen block ~S in Blocks for ~S" block loop))
298     (unless (eq (block-loop block) loop)
299       (barf "wrong loop in ~S, should be ~S" block loop)))
300
301   (dolist (inferior (loop-inferiors loop))
302     (check-loop-consistency inferior loop))
303   (values))
304
305 ;;; Check that Block is either in Loop or an inferior.
306 (declaim (ftype (function (block loop) (values)) check-loop-block))
307 (defun check-loop-block (block loop)
308   (unless (gethash block *seen-blocks*)
309     (barf "unseen block ~S in loop info for ~S" block loop))
310   (labels ((walk (l)
311              (if (eq (block-loop block) l)
312                  t
313                  (dolist (inferior (loop-inferiors l) nil)
314                    (when (walk inferior) (return t))))))
315     (unless (walk loop)
316       (barf "~S is in loop info for ~S but not in the loop." block loop)))
317   (values))
318
319 |#
320
321 ;;; Check a block for consistency at the general flow-graph level, and
322 ;;; call CHECK-NODE-CONSISTENCY on each node to locally check for
323 ;;; semantic consistency.
324 (declaim (ftype (function (cblock) (values)) check-block-consistency))
325 (defun check-block-consistency (block)
326
327   (dolist (pred (block-pred block))
328     (unless (gethash pred *seen-blocks*)
329       (barf "unseen predecessor ~S in ~S" pred block))
330     (unless (member block (block-succ pred))
331       (barf "bad predecessor link ~S in ~S" pred block)))
332
333   (let* ((fun (block-home-lambda block))
334          (fun-deleted (eq (functional-kind fun) :deleted))
335          (this-ctran (block-start block))
336          (last (block-last block)))
337     (unless fun-deleted
338       (check-fun-reached fun block))
339     (when (not this-ctran)
340       (barf "~S has no START." block))
341     (when (not last)
342       (barf "~S has no LAST." block))
343     (unless (eq (ctran-kind this-ctran) :block-start)
344       (barf "The START of ~S has the wrong kind." block))
345
346     (when (ctran-use this-ctran)
347       (barf "The ctran ~S is used." this-ctran))
348
349     (when (node-next last)
350       (barf "Last node ~S of ~S has next ctran." last block))
351
352     (loop
353       (unless (eq (ctran-block this-ctran) block)
354         (barf "BLOCK of ~S should be ~S." this-ctran block))
355
356       (let ((node (ctran-next this-ctran)))
357         (unless (node-p node)
358           (barf "~S has strange NEXT." this-ctran))
359         (unless (eq (node-prev node) this-ctran)
360           (barf "PREV in ~S should be ~S." node this-ctran))
361
362         (when (valued-node-p node)
363           (binding* ((lvar (node-lvar node) :exit-if-null))
364             (unless (memq node (find-uses lvar))
365               (barf "~S is not used by its LVAR ~S." node lvar))
366             (when (singleton-p (lvar-uses lvar))
367               (barf "~S has exactly 1 use, but LVAR-USES is a list."
368                     lvar))
369             (unless (lvar-dest lvar)
370               (barf "~S does not have dest." lvar))))
371
372         (check-node-reached node)
373         (unless fun-deleted
374           (check-node-consistency node))
375
376         (let ((next (node-next node)))
377           (when (and (not next) (not (eq node last)))
378             (barf "~S has no NEXT." node))
379           (when (eq node last) (return))
380           (unless (eq (ctran-kind next) :inside-block)
381             (barf "The interior ctran ~S in ~S has the wrong kind."
382                   next
383                   block))
384           (unless (ctran-next next)
385             (barf "~S has no NEXT." next))
386           (unless (eq (ctran-use next) node)
387             (barf "USE in ~S should be ~S." next node))
388           (setq this-ctran next))))
389
390     (check-block-successors block))
391   (values))
392
393 ;;; Check that BLOCK is properly terminated. Each successor must be
394 ;;; accounted for by the type of the last node.
395 (declaim (ftype (function (cblock) (values)) check-block-successors))
396 (defun check-block-successors (block)
397   (let ((last (block-last block))
398         (succ (block-succ block)))
399
400     (let* ((comp (block-component block)))
401       (dolist (b succ)
402         (unless (gethash b *seen-blocks*)
403           (barf "unseen successor ~S in ~S" b block))
404         (unless (member block (block-pred b))
405           (barf "bad successor link ~S in ~S" b block))
406         (unless (eq (block-component b) comp)
407           (barf "The successor ~S in ~S is in a different component."
408                 b
409                 block))))
410
411     (typecase last
412       (cif
413        (unless (proper-list-of-length-p succ 1 2)
414          (barf "~S ends in an IF, but doesn't have one or two succesors."
415                block))
416        (unless (member (if-consequent last) succ)
417          (barf "The CONSEQUENT for ~S isn't in SUCC for ~S." last block))
418        (unless (member (if-alternative last) succ)
419          (barf "The ALTERNATIVE for ~S isn't in SUCC for ~S." last block)))
420       (creturn
421        (unless (if (eq (functional-kind (return-lambda last)) :deleted)
422                    (null succ)
423                    (and (= (length succ) 1)
424                         (eq (first succ)
425                             (component-tail (block-component block)))))
426          (barf "strange successors for RETURN in ~S" block)))
427       (exit
428        (unless (proper-list-of-length-p succ 0 1)
429          (barf "EXIT node with strange number of successors: ~S" last)))
430       (t
431        (unless (or (= (length succ) 1) (node-tail-p last)
432                    (and (block-delete-p block) (null succ)))
433          (barf "~S ends in normal node, but doesn't have one successor."
434                block)))))
435   (values))
436 \f
437 ;;;; node consistency checking
438
439 ;;; Check that the DEST for LVAR is the specified NODE. We also mark
440 ;;; the block LVAR is in as SEEN.
441 #+nil(declaim (ftype (function (lvar node) (values)) check-dest))
442 (defun check-dest (lvar node)
443   (do-uses (use lvar)
444     (unless (gethash (node-block use) *seen-blocks*)
445       (barf "Node ~S using ~S is in an unknown block." use lvar)))
446   (unless (eq (lvar-dest lvar) node)
447     (barf "DEST for ~S should be ~S." lvar node))
448   (unless (find-uses lvar)
449     (barf "Lvar ~S has a destinatin, but no uses."
450           lvar))
451   (values))
452
453 ;;; This function deals with checking for consistency of the
454 ;;; type-dependent information in a node.
455 (defun check-node-consistency (node)
456   (declare (type node node))
457   (etypecase node
458     (ref
459      (let ((leaf (ref-leaf node)))
460        (when (functional-p leaf)
461          (if (eq (functional-kind leaf) :toplevel-xep)
462              (unless (eq (component-kind (block-component (node-block node)))
463                          :toplevel)
464                (barf ":TOPLEVEL-XEP ref in non-top-level component: ~S"
465                      node))
466              (check-fun-reached leaf node)))))
467     (basic-combination
468      (check-dest (basic-combination-fun node) node)
469      (when (and (mv-combination-p node)
470                 (eq (basic-combination-kind node) :local))
471        (let ((fun-lvar (basic-combination-fun node)))
472          (unless (ref-p (lvar-uses fun-lvar))
473            (barf "function in a local mv-combination is not a LEAF: ~S" node))
474          (let ((fun (ref-leaf (lvar-use fun-lvar))))
475            (unless (lambda-p fun)
476              (barf "function ~S in a local mv-combination ~S is not local"
477                    fun node))
478            (unless (eq (functional-kind fun) :mv-let)
479              (barf "function ~S in a local mv-combination ~S is not of kind :MV-LET"
480                    fun node)))))
481      (dolist (arg (basic-combination-args node))
482        (cond
483          (arg (check-dest arg node))
484          ((not (and (eq (basic-combination-kind node) :local)
485                     (combination-p node)))
486           (barf "flushed arg not in local call: ~S" node))
487          (t
488           (locally
489               ;; KLUDGE: In sbcl-0.6.11.37, the compiler doesn't like
490               ;; (DECLARE (TYPE INDEX POS)) after the inline expansion of
491               ;; POSITION. It compiles it correctly, but it issues a type
492               ;; mismatch warning because it can't eliminate the
493               ;; possibility that control will flow through the
494               ;; NIL-returning branch. So we punt here. -- WHN 2001-04-15
495               (declare (notinline position))
496             (let ((fun (ref-leaf (lvar-use
497                                   (basic-combination-fun node))))
498                   (pos (position arg (basic-combination-args node))))
499               (declare (type index pos))
500               (when (leaf-refs (elt (lambda-vars fun) pos))
501                 (barf "flushed arg for referenced var in ~S" node)))))))
502      (let* ((lvar (node-lvar node))
503             (dest (and lvar (lvar-dest lvar))))
504        (when (and (return-p dest)
505                   (eq (basic-combination-kind node) :local)
506                   (not (eq (lambda-tail-set (combination-lambda node))
507                            (lambda-tail-set (return-lambda dest)))))
508          (barf "tail local call to function with different tail set:~%  ~S"
509                node))))
510     (cif
511      (check-dest (if-test node) node)
512      (unless (eq (block-last (node-block node)) node)
513        (barf "IF not at block end: ~S" node)))
514     (cset
515      (check-dest (set-value node) node))
516     (cast
517      (check-dest (cast-value node) node))
518     (bind
519      (check-fun-reached (bind-lambda node) node))
520     (creturn
521      (check-fun-reached (return-lambda node) node)
522      (check-dest (return-result node) node)
523      (unless (eq (block-last (node-block node)) node)
524        (barf "RETURN not at block end: ~S" node)))
525     (entry
526      (unless (member node (lambda-entries (node-home-lambda node)))
527        (barf "~S is not in ENTRIES for its home LAMBDA." node))
528      (dolist (exit (entry-exits node))
529        (unless (node-deleted exit)
530          (check-node-reached node))))
531     (exit
532      (let ((entry (exit-entry node))
533            (value (exit-value node)))
534        (cond (entry
535               (check-node-reached entry)
536               (unless (member node (entry-exits entry))
537                 (barf "~S is not in its ENTRY's EXITS." node))
538               (when value
539                 (check-dest value node)))
540              (t
541               (when value
542                 (barf "~S has VALUE but no ENTRY." node)))))))
543
544   (values))
545 \f
546 ;;;; IR2 consistency checking
547
548 ;;; Check for some kind of consistency in some REFs linked together by
549 ;;; TN-REF-ACROSS. VOP is the VOP that the references are in. WRITE-P
550 ;;; is the value of WRITE-P that should be present. COUNT is the
551 ;;; minimum number of operands expected. If MORE-P is true, then any
552 ;;; larger number will also be accepted. WHAT is a string describing
553 ;;; the kind of operand in error messages.
554 (defun check-tn-refs (refs vop write-p count more-p what)
555   (let ((vop-refs (vop-refs vop)))
556     (do ((ref refs (tn-ref-across ref))
557          (num 0 (1+ num)))
558         ((null ref)
559          (when (< num count)
560            (barf "There should be at least ~W ~A in ~S, but there are only ~W."
561                  count what vop num))
562          (when (and (not more-p) (> num count))
563            (barf "There should be ~W ~A in ~S, but are ~W."
564                  count what vop num)))
565       (unless (eq (tn-ref-vop ref) vop)
566         (barf "VOP is ~S isn't ~S." ref vop))
567       (unless (eq (tn-ref-write-p ref) write-p)
568         (barf "The WRITE-P in ~S isn't ~S." vop write-p))
569       (unless (find-in #'tn-ref-next-ref ref vop-refs)
570         (barf "~S not found in REFS for ~S" ref vop))
571       (unless (find-in #'tn-ref-next ref
572                        (if (tn-ref-write-p ref)
573                            (tn-writes (tn-ref-tn ref))
574                            (tn-reads (tn-ref-tn ref))))
575         (barf "~S not found in reads/writes for its TN" ref))
576
577       (let ((target (tn-ref-target ref)))
578         (when target
579           (unless (eq (tn-ref-write-p target) (not (tn-ref-write-p ref)))
580             (barf "The target for ~S isn't complementary WRITE-P." ref))
581           (unless (find-in #'tn-ref-next-ref target vop-refs)
582             (barf "The target for ~S isn't in REFS for ~S." ref vop)))))))
583
584 ;;; Verify the sanity of the VOP-REFS slot in VOP. This involves checking
585 ;;; that each referenced TN appears as an argument, result or temp, and also
586 ;;; basic checks for the plausibility of the specified ordering of the refs.
587 (defun check-vop-refs (vop)
588   (declare (type vop vop))
589   (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
590       ((null ref))
591     (cond
592      ((find-in #'tn-ref-across ref (vop-args vop)))
593      ((find-in #'tn-ref-across ref (vop-results vop)))
594      ((not (eq (tn-ref-vop ref) vop))
595       (barf "VOP in ~S isn't ~S." ref vop))
596      ((find-in #'tn-ref-across ref (vop-temps vop)))
597      ((tn-ref-write-p ref)
598       (barf "stray ref that isn't a READ: ~S" ref))
599      (t
600       (let* ((tn (tn-ref-tn ref))
601              (temp (find-in #'tn-ref-across tn (vop-temps vop)
602                             :key #'tn-ref-tn)))
603         (unless temp
604           (barf "stray ref with no corresponding temp write: ~S" ref))
605         (unless (find-in #'tn-ref-next-ref temp (tn-ref-next-ref ref))
606           (barf "Read is after write for temp ~S in refs of ~S."
607                 tn vop))))))
608   (values))
609
610 ;;; Check the basic sanity of the VOP linkage, then call some other
611 ;;; functions to check on the TN-REFS. We grab some info out of the
612 ;;; VOP-INFO to tell us what to expect.
613 ;;;
614 ;;; [### Check that operand type restrictions are met?]
615 (defun check-ir2-block-consistency (2block)
616   (declare (type ir2-block 2block))
617   (do ((vop (ir2-block-start-vop 2block)
618             (vop-next vop))
619        (prev nil vop))
620       ((null vop)
621        (unless (eq prev (ir2-block-last-vop 2block))
622          (barf "The last VOP in ~S should be ~S." 2block prev)))
623     (unless (eq (vop-prev vop) prev)
624       (barf "PREV in ~S should be ~S." vop prev))
625
626     (unless (eq (vop-block vop) 2block)
627       (barf "BLOCK in ~S should be ~S." vop 2block))
628
629     (check-vop-refs vop)
630
631     (let* ((info (vop-info vop))
632            (atypes (template-arg-types info))
633            (rtypes (template-result-types info)))
634       (check-tn-refs (vop-args vop) vop nil
635                      (count-if-not (lambda (x)
636                                      (and (consp x)
637                                           (eq (car x) :constant)))
638                                    atypes)
639                      (template-more-args-type info) "args")
640       (check-tn-refs (vop-results vop) vop t
641                      (if (eq rtypes :conditional) 0 (length rtypes))
642                      (template-more-results-type info) "results")
643       (check-tn-refs (vop-temps vop) vop t 0 t "temps")
644       (unless (= (length (vop-codegen-info vop))
645                  (template-info-arg-count info))
646         (barf "wrong number of codegen info args in ~S" vop))))
647   (values))
648
649 ;;; Check stuff about the IR2 representation of COMPONENT. This assumes the
650 ;;; sanity of the basic flow graph.
651 ;;;
652 ;;; [### Also grovel global TN data structures?  Assume pack not
653 ;;; done yet?  Have separate CHECK-TN-CONSISTENCY for pre-pack and
654 ;;; CHECK-PACK-CONSISTENCY for post-pack?]
655 (defun check-ir2-consistency (component)
656   (declare (type component component))
657   (do-ir2-blocks (block component)
658     (check-ir2-block-consistency block))
659   (values))
660 \f
661 ;;;; lifetime analysis checking
662
663 ;;; Dump some info about how many TNs there, and what the conflicts data
664 ;;; structures are like.
665 (defun pre-pack-tn-stats (component &optional (stream *error-output*))
666   (declare (type component component))
667   (let ((wired 0)
668         (global 0)
669         (local 0)
670         (confs 0)
671         (unused 0)
672         (const 0)
673         (temps 0)
674         (environment 0)
675         (comp 0))
676     (do-packed-tns (tn component)
677       (let ((reads (tn-reads tn))
678             (writes (tn-writes tn)))
679         (when (and reads writes
680                    (not (tn-ref-next reads)) (not (tn-ref-next writes))
681                    (eq (tn-ref-vop reads) (tn-ref-vop writes)))
682           (incf temps)))
683       (when (tn-offset tn)
684         (incf wired))
685       (unless (or (tn-reads tn) (tn-writes tn))
686         (incf unused))
687       (cond ((eq (tn-kind tn) :component)
688              (incf comp))
689             ((tn-global-conflicts tn)
690              (case (tn-kind tn)
691                ((:environment :debug-environment) (incf environment))
692                (t (incf global)))
693              (do ((conf (tn-global-conflicts tn)
694                         (global-conflicts-next-tnwise conf)))
695                  ((null conf))
696                (incf confs)))
697             (t
698              (incf local))))
699
700     (do ((tn (ir2-component-constant-tns (component-info component))
701              (tn-next tn)))
702         ((null tn))
703       (incf const))
704
705     (format stream
706      "~%TNs: ~W local, ~W temps, ~W constant, ~W env, ~W comp, ~W global.~@
707        Wired: ~W, Unused: ~W. ~W block~:P, ~W global conflict~:P.~%"
708        local temps const environment comp global wired unused
709        (ir2-block-count component)
710        confs))
711   (values))
712
713 ;;; If the entry in Local-TNs for TN in BLOCK is :MORE, then do some checks
714 ;;; for the validity of the usage.
715 (defun check-more-tn-entry (tn block)
716   (let* ((vop (ir2-block-start-vop block))
717          (info (vop-info vop)))
718     (macrolet ((frob (more-p ops)
719                  `(and (,more-p info)
720                        (find-in #'tn-ref-across tn (,ops vop)
721                                 :key #'tn-ref-tn))))
722       (unless (and (eq vop (ir2-block-last-vop block))
723                    (or (frob template-more-args-type vop-args)
724                        (frob template-more-results-type vop-results)))
725         (barf "strange :MORE LTN entry for ~S in ~S" tn block))))
726   (values))
727
728 (defun check-tn-conflicts (component)
729   (do-packed-tns (tn component)
730     (unless (or (not (eq (tn-kind tn) :normal))
731                 (tn-reads tn)
732                 (tn-writes tn))
733       (barf "no references to ~S" tn))
734
735     (unless (tn-sc tn) (barf "~S has no SC." tn))
736
737     (let ((conf (tn-global-conflicts tn))
738           (kind (tn-kind tn)))
739       (cond
740        ((eq kind :component)
741         (unless (member tn (ir2-component-component-tns
742                             (component-info component)))
743           (barf "~S not in COMPONENT-TNs for ~S" tn component)))
744        (conf
745         (do ((conf conf (global-conflicts-next-tnwise conf))
746              (prev nil conf))
747             ((null conf))
748           (unless (eq (global-conflicts-tn conf) tn)
749             (barf "TN in ~S should be ~S." conf tn))
750
751           (unless (eq (global-conflicts-kind conf) :live)
752             (let* ((block (global-conflicts-block conf))
753                    (ltn (svref (ir2-block-local-tns block)
754                                (global-conflicts-number conf))))
755               (cond ((eq ltn tn))
756                     ((eq ltn :more) (check-more-tn-entry tn block))
757                     (t
758                      (barf "~S wrong in LTN map for ~S" conf tn)))))
759
760           (when prev
761             (unless (> (ir2-block-number (global-conflicts-block conf))
762                        (ir2-block-number (global-conflicts-block prev)))
763               (barf "~s and ~s out of order" prev conf)))))
764        ((member (tn-kind tn) '(:constant :specified-save)))
765        (t
766         (let ((local (tn-local tn)))
767           (unless local
768             (barf "~S has no global conflicts, but isn't local either." tn))
769           (unless (eq (svref (ir2-block-local-tns local)
770                              (tn-local-number tn))
771                       tn)
772             (barf "~S wrong in LTN map" tn))
773           (do ((ref (tn-reads tn) (tn-ref-next ref)))
774               ((null ref))
775             (unless (eq (vop-block (tn-ref-vop ref)) local)
776               (barf "~S has references in blocks other than its LOCAL block."
777                     tn)))
778           (do ((ref (tn-writes tn) (tn-ref-next ref)))
779               ((null ref))
780             (unless (eq (vop-block (tn-ref-vop ref)) local)
781               (barf "~S has references in blocks other than its LOCAL block."
782                     tn))))))))
783   (values))
784
785 (defun check-block-conflicts (component)
786   (do-ir2-blocks (block component)
787     (do ((conf (ir2-block-global-tns block)
788                (global-conflicts-next-blockwise conf))
789          (prev nil conf))
790         ((null conf))
791       (when prev
792         (unless (> (tn-number (global-conflicts-tn conf))
793                    (tn-number (global-conflicts-tn prev)))
794           (barf "~S and ~S out of order in ~S" prev conf block)))
795
796       (unless (find-in #'global-conflicts-next-tnwise
797                        conf
798                        (tn-global-conflicts
799                         (global-conflicts-tn conf)))
800         (barf "~S missing from global conflicts of its TN" conf)))
801
802     (let ((map (ir2-block-local-tns block)))
803       (dotimes (i (ir2-block-local-tn-count block))
804         (let ((tn (svref map i)))
805           (unless (or (eq tn :more)
806                       (null tn)
807                       (tn-global-conflicts tn)
808                       (eq (tn-local tn) block))
809             (barf "strange TN ~S in LTN map for ~S" tn block)))))))
810
811 ;;; All TNs live at the beginning of an environment must be passing
812 ;;; locations associated with that environment. We make an exception
813 ;;; for wired TNs in XEP functions, since we randomly reference wired
814 ;;; TNs to access the full call passing locations.
815 (defun check-environment-lifetimes (component)
816   (dolist (fun (component-lambdas component))
817     (let* ((env (lambda-physenv fun))
818            (2env (physenv-info env))
819            (vars (lambda-vars fun))
820            (closure (ir2-physenv-closure 2env))
821            (pc (ir2-physenv-return-pc-pass 2env))
822            (fp (ir2-physenv-old-fp 2env))
823            (2block (block-info (lambda-block (physenv-lambda env)))))
824       (do ((conf (ir2-block-global-tns 2block)
825                  (global-conflicts-next-blockwise conf)))
826           ((null conf))
827         (let ((tn (global-conflicts-tn conf)))
828           (unless (or (eq (global-conflicts-kind conf) :write)
829                       (eq tn pc)
830                       (eq tn fp)
831                       (and (xep-p fun) (tn-offset tn))
832                       (member (tn-kind tn) '(:environment :debug-environment))
833                       (member tn vars :key #'leaf-info)
834                       (member tn closure :key #'cdr))
835             (barf "strange TN live at head of ~S: ~S" env tn))))))
836   (values))
837
838 ;;; Check for some basic sanity in the TN conflict data structures,
839 ;;; and also check that no TNs are unexpectedly live at environment
840 ;;; entry.
841 (defun check-life-consistency (component)
842   (check-tn-conflicts component)
843   (check-block-conflicts component)
844   (check-environment-lifetimes component))
845 \f
846 ;;;; pack consistency checking
847
848 (defun check-pack-consistency (component)
849   (flet ((check (scs ops)
850            (do ((scs scs (cdr scs))
851                 (op ops (tn-ref-across op)))
852                ((null scs))
853              (let ((load-tn (tn-ref-load-tn op)))
854                (unless (eq (svref (car scs)
855                                   (sc-number
856                                    (tn-sc
857                                     (or load-tn (tn-ref-tn op)))))
858                            t)
859                  (barf "operand restriction not satisfied: ~S" op))))))
860     (do-ir2-blocks (block component)
861       (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
862           ((null vop))
863         (let ((info (vop-info vop)))
864           (check (vop-info-result-load-scs info) (vop-results vop))
865           (check (vop-info-arg-load-scs info) (vop-args vop))))))
866   (values))
867 \f
868 ;;;; data structure dumping routines
869
870 ;;; When we print CONTINUATIONs and TNs, we assign them small numeric
871 ;;; IDs so that we can get a handle on anonymous objects given a
872 ;;; printout.
873 ;;;
874 ;;; FIXME:
875 ;;;   * Perhaps this machinery should be #!+SB-SHOW.
876 ;;;   * Probably the hash tables should either be weak hash tables,
877 ;;;     or only allocated within a single compilation unit. Otherwise
878 ;;;     there will be a tendency for them to grow without bound and
879 ;;;     keep garbage from being collected.
880 (macrolet ((def (counter vto vfrom fto ffrom)
881              `(progn
882                 (declaim (type hash-table ,vto ,vfrom))
883                 (defvar ,vto (make-hash-table :test 'eq))
884                 (defvar ,vfrom (make-hash-table :test 'eql))
885                 (declaim (type fixnum ,counter))
886                 (defvar ,counter 0)
887
888                 (defun ,fto (x)
889                   (or (gethash x ,vto)
890                       (let ((num (incf ,counter)))
891                         (setf (gethash num ,vfrom) x)
892                         (setf (gethash x ,vto) num))))
893
894                 (defun ,ffrom (num)
895                   (values (gethash num ,vfrom))))))
896   (def *continuation-number* *continuation-numbers* *number-continuations*
897        cont-num num-cont)
898   (def *tn-id* *tn-ids* *id-tns* tn-id id-tn)
899   (def *label-id* *id-labels* *label-ids* label-id id-label))
900
901 ;;; Print a terse one-line description of LEAF.
902 (defun print-leaf (leaf &optional (stream *standard-output*))
903   (declare (type leaf leaf) (type stream stream))
904   (etypecase leaf
905     (lambda-var (prin1 (leaf-debug-name leaf) stream))
906     (constant (format stream "'~S" (constant-value leaf)))
907     (global-var
908      (format stream "~S {~A}" (leaf-debug-name leaf) (global-var-kind leaf)))
909     (functional
910      (format stream "~S ~S" (type-of leaf) (functional-debug-name leaf)))))
911
912 ;;; Attempt to find a block given some thing that has to do with it.
913 (declaim (ftype (sfunction (t) cblock) block-or-lose))
914 (defun block-or-lose (thing)
915   (ctypecase thing
916     (cblock thing)
917     (ir2-block (ir2-block-block thing))
918     (vop (block-or-lose (vop-block thing)))
919     (tn-ref (block-or-lose (tn-ref-vop thing)))
920     (ctran (ctran-block thing))
921     (node (node-block thing))
922     (component (component-head thing))
923 #|    (cloop (loop-head thing))|#
924     (integer (ctran-block (num-cont thing)))
925     (functional (lambda-block (main-entry thing)))
926     (null (error "Bad thing: ~S." thing))
927     (symbol (block-or-lose (gethash thing *free-funs*)))))
928
929 ;;; Print cN.
930 (defun print-continuation (cont)
931   (declare (type continuation cont))
932   (format t " c~D" (cont-num cont))
933   (values))
934
935 (defun print-ctran (cont)
936   (declare (type ctran cont))
937   (format t "c~D " (cont-num cont))
938   (values))
939 (defun print-lvar (cont)
940   (declare (type lvar cont))
941   (format t "v~D " (cont-num cont))
942   (values))
943
944 (defun print-lvar-stack (stack &optional (stream *standard-output*))
945   (loop for (lvar . rest) on stack
946         do (format stream "~:[u~;d~]v~D~@[ ~]"
947                    (lvar-dynamic-extent lvar) (cont-num lvar) rest)))
948
949 ;;; Print out the nodes in BLOCK in a format oriented toward
950 ;;; representing what the code does.
951 (defun print-nodes (block)
952   (setq block (block-or-lose block))
953   (pprint-logical-block (nil nil)
954     (format t "~:@_IR1 block ~D start c~D"
955             (block-number block) (cont-num (block-start block)))
956     (when (block-delete-p block)
957       (format t " <deleted>"))
958
959     (pprint-newline :mandatory)
960     (awhen (block-info block)
961       (format t "start stack: ")
962       (print-lvar-stack (ir2-block-start-stack it))
963       (pprint-newline :mandatory))
964     (do ((ctran (block-start block) (node-next (ctran-next ctran))))
965         ((not ctran))
966       (let ((node (ctran-next ctran)))
967         (format t "~3D>~:[    ~;~:*~3D:~] "
968                 (cont-num ctran)
969                 (when (and (valued-node-p node) (node-lvar node))
970                   (cont-num (node-lvar node))))
971         (etypecase node
972           (ref (print-leaf (ref-leaf node)))
973           (basic-combination
974            (let ((kind (basic-combination-kind node)))
975              (format t "~(~A~A ~A~) "
976                      (if (node-tail-p node) "tail " "")
977                      kind
978                      (type-of node))
979              (print-lvar (basic-combination-fun node))
980              (dolist (arg (basic-combination-args node))
981                (if arg
982                    (print-lvar arg)
983                    (format t "<none> ")))))
984           (cset
985            (write-string "set ")
986            (print-leaf (set-var node))
987            (write-char #\space)
988            (print-lvar (set-value node)))
989           (cif
990            (write-string "if ")
991            (print-lvar (if-test node))
992            (print-ctran (block-start (if-consequent node)))
993            (print-ctran (block-start (if-alternative node))))
994           (bind
995            (write-string "bind ")
996            (print-leaf (bind-lambda node))
997            (when (functional-kind (bind-lambda node))
998              (format t " ~S ~S" :kind (functional-kind (bind-lambda node)))))
999           (creturn
1000            (write-string "return ")
1001            (print-lvar (return-result node))
1002            (print-leaf (return-lambda node)))
1003           (entry
1004            (let ((cleanup (entry-cleanup node)))
1005              (case (cleanup-kind cleanup)
1006                ((:dynamic-extent)
1007                 (format t "entry DX~{ v~D~}"
1008                         (mapcar #'cont-num (cleanup-info cleanup))))
1009                (t
1010                 (format t "entry ~S" (entry-exits node))))))
1011           (exit
1012            (let ((value (exit-value node)))
1013              (cond (value
1014                     (format t "exit ")
1015                     (print-lvar value))
1016                    ((exit-entry node)
1017                     (format t "exit <no value>"))
1018                    (t
1019                     (format t "exit <degenerate>")))))
1020           (cast
1021            (let ((value (cast-value node)))
1022              (format t "cast v~D ~A[~S -> ~S]" (cont-num value)
1023                      (if (cast-%type-check node) #\+ #\-)
1024                      (cast-type-to-check node)
1025                      (cast-asserted-type node)))))
1026         (pprint-newline :mandatory)))
1027
1028     (awhen (block-info block)
1029       (format t "end stack: ")
1030       (print-lvar-stack (ir2-block-end-stack it))
1031       (pprint-newline :mandatory))
1032     (let ((succ (block-succ block)))
1033       (format t "successors~{ c~D~}~%"
1034               (mapcar (lambda (x) (cont-num (block-start x))) succ))))
1035   (values))
1036
1037 ;;; Print the guts of a TN. (logic shared between PRINT-OBJECT (TN T)
1038 ;;; and printers for compound objects which contain TNs)
1039 (defun print-tn-guts (tn &optional (stream *standard-output*))
1040   (declare (type tn tn))
1041   (let ((leaf (tn-leaf tn)))
1042     (cond (leaf
1043            (print-leaf leaf stream)
1044            (format stream "!~D" (tn-id tn)))
1045           (t
1046            (format stream "t~D" (tn-id tn))))
1047     (when (and (tn-sc tn) (tn-offset tn))
1048       (format stream "[~A]" (location-print-name tn)))))
1049
1050 ;;; Print the TN-REFs representing some operands to a VOP, linked by
1051 ;;; TN-REF-ACROSS.
1052 (defun print-operands (refs)
1053   (declare (type (or tn-ref null) refs))
1054   (pprint-logical-block (*standard-output* nil)
1055     (do ((ref refs (tn-ref-across ref)))
1056         ((null ref))
1057       (let ((tn (tn-ref-tn ref))
1058             (ltn (tn-ref-load-tn ref)))
1059         (cond ((not ltn)
1060                (print-tn-guts tn))
1061               (t
1062                (print-tn-guts tn)
1063                (princ (if (tn-ref-write-p ref) #\< #\>))
1064                (print-tn-guts ltn)))
1065         (princ #\space)
1066         (pprint-newline :fill)))))
1067
1068 ;;; Print the VOP, putting args, info and results on separate lines, if
1069 ;;; necessary.
1070 (defun print-vop (vop)
1071   (pprint-logical-block (*standard-output* nil)
1072     (princ (vop-info-name (vop-info vop)))
1073     (princ #\space)
1074     (pprint-indent :current 0)
1075     (print-operands (vop-args vop))
1076     (pprint-newline :linear)
1077     (when (vop-codegen-info vop)
1078       (princ (with-output-to-string (stream)
1079                (let ((*print-level* 1)
1080                      (*print-length* 3))
1081                  (format stream "{~{~S~^ ~}} " (vop-codegen-info vop)))))
1082       (pprint-newline :linear))
1083     (when (vop-results vop)
1084       (princ "=> ")
1085       (print-operands (vop-results vop))))
1086   (pprint-newline :mandatory))
1087
1088 ;;; Print the VOPs in the specified IR2 block.
1089 (defun print-ir2-block (block)
1090   (declare (type ir2-block block))
1091   (pprint-logical-block (*standard-output* nil)
1092     (cond
1093       ((eq (block-info (ir2-block-block block)) block)
1094        (format t "~:@_IR2 block ~D start c~D~:@_"
1095                (ir2-block-number block)
1096                (cont-num (block-start (ir2-block-block block))))
1097        (let ((label (ir2-block-%label block)))
1098          (when label
1099            (format t "L~D:~:@_" (label-id label)))))
1100       (t
1101        (format t "<overflow>~:@_")))
1102
1103     (do ((vop (ir2-block-start-vop block)
1104               (vop-next vop))
1105          (number 0 (1+ number)))
1106         ((null vop))
1107       (format t "~W: " number)
1108       (print-vop vop))))
1109
1110 ;;; This is like PRINT-NODES, but dumps the IR2 representation of the
1111 ;;; code in BLOCK.
1112 (defun print-vops (block)
1113   (setq block (block-or-lose block))
1114   (let ((2block (block-info block)))
1115     (print-ir2-block 2block)
1116     (do ((b (ir2-block-next 2block) (ir2-block-next b)))
1117         ((not (eq (ir2-block-block b) block)))
1118       (print-ir2-block b)))
1119   (values))
1120
1121 ;;; Scan the IR2 blocks in emission order.
1122 (defun print-ir2-blocks (thing &optional full)
1123   (let* ((block (component-head (block-component (block-or-lose thing))))
1124          (2block (block-info block)))
1125     (pprint-logical-block (nil nil)
1126       (loop while 2block
1127          do (setq block (ir2-block-block 2block))
1128          do (pprint-logical-block (*standard-output* nil)
1129               (if full
1130                   (print-nodes block)
1131                   (format t "IR1 block ~D start c~D"
1132                           (block-number block)
1133                           (cont-num (block-start block))))
1134               (pprint-indent :block 4)
1135               (pprint-newline :mandatory)
1136               (loop while (and 2block (eq (ir2-block-block 2block) block))
1137                  do (print-ir2-block 2block)
1138                  do (setq 2block (ir2-block-next 2block))))
1139          do (pprint-newline :mandatory))))
1140   (values))
1141
1142 ;;; Do a PRINT-NODES on BLOCK and all blocks reachable from it by
1143 ;;; successor links.
1144 (defun print-blocks (block)
1145   (setq block (block-or-lose block))
1146   (do-blocks (block (block-component block) :both)
1147     (setf (block-flag block) nil))
1148   (labels ((walk (block)
1149              (unless (block-flag block)
1150                (setf (block-flag block) t)
1151                (when (block-start block)
1152                  (print-nodes block))
1153                (dolist (block (block-succ block))
1154                  (walk block)))))
1155     (walk block))
1156   (values))
1157
1158 ;;; Print all blocks in BLOCK's component in DFO.
1159 (defun print-all-blocks (thing)
1160   (do-blocks (block (block-component (block-or-lose thing)))
1161     (handler-case (print-nodes block)
1162       (error (condition)
1163         (format t "~&~A...~%" condition))))
1164   (values))
1165
1166 (defvar *list-conflicts-table* (make-hash-table :test 'eq))
1167
1168 ;;; Add all ALWAYS-LIVE TNs in BLOCK to the conflicts. TN is ignored
1169 ;;; when it appears in the global conflicts.
1170 (defun add-always-live-tns (block tn)
1171   (declare (type ir2-block block) (type tn tn))
1172   (do ((conf (ir2-block-global-tns block)
1173              (global-conflicts-next-blockwise conf)))
1174       ((null conf))
1175     (when (eq (global-conflicts-kind conf) :live)
1176       (let ((btn (global-conflicts-tn conf)))
1177         (unless (eq btn tn)
1178           (setf (gethash btn *list-conflicts-table*) t)))))
1179   (values))
1180
1181 ;;; Add all local TNs in BLOCK to the conflicts.
1182 (defun add-all-local-tns (block)
1183   (declare (type ir2-block block))
1184   (let ((ltns (ir2-block-local-tns block)))
1185     (dotimes (i (ir2-block-local-tn-count block))
1186       (setf (gethash (svref ltns i) *list-conflicts-table*) t)))
1187   (values))
1188
1189 ;;; Make a list out of all of the recorded conflicts.
1190 (defun listify-conflicts-table ()
1191   (collect ((res))
1192     (maphash (lambda (k v)
1193                (declare (ignore v))
1194                (when k
1195                  (res k)))
1196              *list-conflicts-table*)
1197     (clrhash *list-conflicts-table*)
1198     (res)))
1199
1200 ;;; Return a list of a the TNs that conflict with TN. Sort of, kind
1201 ;;; of. For debugging use only. Probably doesn't work on :COMPONENT TNs.
1202 (defun list-conflicts (tn)
1203   (aver (member (tn-kind tn) '(:normal :environment :debug-environment)))
1204   (let ((confs (tn-global-conflicts tn)))
1205     (cond (confs
1206            (clrhash *list-conflicts-table*)
1207            (do ((conf confs (global-conflicts-next-tnwise conf)))
1208                ((null conf))
1209              (format t "~&#<block ~D kind ~S>~%"
1210                      (block-number (ir2-block-block (global-conflicts-block
1211                                                      conf)))
1212                      (global-conflicts-kind conf))
1213              (let ((block (global-conflicts-block conf)))
1214                (add-always-live-tns block tn)
1215                (if (eq (global-conflicts-kind conf) :live)
1216                    (add-all-local-tns block)
1217                    (let ((bconf (global-conflicts-conflicts conf))
1218                          (ltns (ir2-block-local-tns block)))
1219                      (dotimes (i (ir2-block-local-tn-count block))
1220                        (when (/= (sbit bconf i) 0)
1221                          (setf (gethash (svref ltns i) *list-conflicts-table*)
1222                                t)))))))
1223            (listify-conflicts-table))
1224           (t
1225            (let* ((block (tn-local tn))
1226                   (ltns (ir2-block-local-tns block))
1227                   (confs (tn-local-conflicts tn)))
1228              (collect ((res))
1229                (dotimes (i (ir2-block-local-tn-count block))
1230                  (when (/= (sbit confs i) 0)
1231                    (let ((tn (svref ltns i)))
1232                      (when (and tn (not (eq tn :more))
1233                                 (not (tn-global-conflicts tn)))
1234                        (res tn)))))
1235                (do ((gtn (ir2-block-global-tns block)
1236                          (global-conflicts-next-blockwise gtn)))
1237                    ((null gtn))
1238                  (when (or (eq (global-conflicts-kind gtn) :live)
1239                            (/= (sbit confs (global-conflicts-number gtn)) 0))
1240                    (res (global-conflicts-tn gtn))))
1241                (res)))))))
1242
1243 (defun nth-vop (thing n)
1244   #!+sb-doc
1245   "Return the Nth VOP in the IR2-BLOCK pointed to by THING."
1246   (let ((block (block-info (block-or-lose thing))))
1247     (do ((i 0 (1+ i))
1248          (vop (ir2-block-start-vop block) (vop-next vop)))
1249         ((= i n) vop))))