0.pre7.125:
[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-FUNCTIONS* is similar, but records all the lambdas we
56 ;;; reached by recursing on top level functions.
57 (defvar *seen-blocks* (make-hash-table :test 'eq))
58 (defvar *seen-functions* (make-hash-table :test 'eq))
59
60 ;;; Barf if NODE is in a block which wasn't reached during the graph
61 ;;; walk.
62 (declaim (ftype (function (node) (values)) check-node-reached))
63 (defun check-node-reached (node)
64   (unless (gethash (continuation-block (node-prev node)) *seen-blocks*)
65     (barf "~S was not reached." node))
66   (values))
67
68 ;;; Check everything that we can think of for consistency. When a
69 ;;; definite inconsistency is detected, we BARF. Possible problems
70 ;;; just cause us to BURP. Our argument is a list of components, but
71 ;;; we also look at the *FREE-VARIABLES*, *FREE-FUNCTIONS* and
72 ;;; *CONSTANTS*.
73 ;;;
74 ;;; First we do a pre-pass which finds all the CBLOCKs and CLAMBDAs,
75 ;;; testing that they are linked together properly and entering them
76 ;;; in hashtables. Next, we iterate over the blocks again, looking at
77 ;;; the actual code and control flow. Finally, we scan the global leaf
78 ;;; hashtables, looking for lossage.
79 (declaim (ftype (function (list) (values)) check-ir1-consistency))
80 (defun check-ir1-consistency (components)
81   (clrhash *seen-blocks*)
82   (clrhash *seen-functions*)
83   (dolist (c components)
84     (let* ((head (component-head c))
85            (tail (component-tail c)))
86       (unless (and (null (block-pred head)) (null (block-succ tail)))
87         (barf "~S is malformed." c))
88
89       (do ((prev nil block)
90            (block head (block-next block)))
91           ((null block)
92            (unless (eq prev tail)
93              (barf "wrong Tail for DFO, ~S in ~S" prev c)))
94         (setf (gethash block *seen-blocks*) t)
95         (unless (eq (block-prev block) prev)
96           (barf "bad PREV for ~S, should be ~S" block prev))
97         (unless (or (eq block tail)
98                     (eq (block-component block) c))
99           (barf "~S is not in ~S." block c)))
100 #|
101       (when (or (loop-blocks c) (loop-inferiors c))
102         (do-blocks (block c :both)
103           (setf (block-flag block) nil))
104         (check-loop-consistency c nil)
105         (do-blocks (block c :both)
106           (unless (block-flag block)
107             (barf "~S was not in any loop." block))))
108 |#
109     ))
110
111   (check-function-consistency components)
112
113   (dolist (c components)
114     (do ((block (block-next (component-head c)) (block-next block)))
115         ((null (block-next block)))
116       (check-block-consistency block)))
117
118   (maphash (lambda (k v)
119              (declare (ignore k))
120              (unless (or (constant-p v)
121                          (and (global-var-p v)
122                               (member (global-var-kind v)
123                                       '(:global :special))))
124                (barf "strange *FREE-VARIABLES* entry: ~S" v))
125              (dolist (n (leaf-refs v))
126                (check-node-reached n))
127              (when (basic-var-p v)
128                (dolist (n (basic-var-sets v))
129                  (check-node-reached n))))
130            *free-variables*)
131
132   (maphash (lambda (k v)
133              (declare (ignore k))
134              (unless (constant-p v)
135                (barf "strange *CONSTANTS* entry: ~S" v))
136              (dolist (n (leaf-refs v))
137                (check-node-reached n)))
138            *constants*)
139
140   (maphash (lambda (k v)
141              (declare (ignore k))
142              (unless (or (functional-p v)
143                          (and (global-var-p v)
144                               (eq (global-var-kind v) :global-function)))
145                (barf "strange *FREE-FUNCTIONS* entry: ~S" v))
146              (dolist (n (leaf-refs v))
147                (check-node-reached n)))
148            *free-functions*)
149   (clrhash *seen-functions*)
150   (clrhash *seen-blocks*)
151   (values))
152 \f
153 ;;;; function consistency checking
154
155 (defun observe-functional (x)
156   (declare (type functional x))
157   (when (gethash x *seen-functions*)
158     (barf "~S was seen more than once." x))
159   (unless (eq (functional-kind x) :deleted)
160     (setf (gethash x *seen-functions*) t)))
161
162 ;;; Check that the specified function has been seen.
163 (defun check-function-reached (fun where)
164   (declare (type functional fun))
165   (unless (gethash fun *seen-functions*)
166     (barf "unseen function ~S in ~S" fun where)))
167
168 ;;; In a CLAMBDA, check that the associated nodes are in seen blocks.
169 ;;; In an OPTIONAL-DISPATCH, check that the entry points were seen. If
170 ;;; the function is deleted, ignore it.
171 (defun check-function-stuff (functional)
172   (ecase (functional-kind functional)
173     (:external
174      (let ((fun (functional-entry-fun functional)))
175        (check-function-reached fun functional)
176        (when (functional-kind fun)
177          (barf "The function for XEP ~S has kind." functional))
178        (unless (eq (functional-entry-fun fun) functional)
179          (barf "bad back-pointer in function for XEP ~S" functional))))
180     ((:let :mv-let :assignment)
181      (check-function-reached (lambda-home functional) functional)
182      (when (functional-entry-fun functional)
183        (barf "The LET ~S has entry function." functional))
184      (unless (member functional (lambda-lets (lambda-home functional)))
185        (barf "The LET ~S is not in LETs for HOME." functional))
186      (unless (eq (functional-kind functional) :assignment)
187        (when (rest (leaf-refs functional))
188          (barf "The LET ~S has multiple references." functional)))
189      (when (lambda-lets functional)
190        (barf "LETs in a LET: ~S" functional)))
191     (:optional
192      (when (functional-entry-fun functional)
193        (barf ":OPTIONAL ~S has an ENTRY-FUN." functional))
194      (let ((ef (lambda-optional-dispatch functional)))
195        (check-function-reached ef functional)
196        (unless (or (member functional (optional-dispatch-entry-points ef))
197                    (eq functional (optional-dispatch-more-entry ef))
198                    (eq functional (optional-dispatch-main-entry ef)))
199          (barf ":OPTIONAL ~S is not an e-p for its OPTIONAL-DISPATCH ~S."
200                functional ef))))
201     (:toplevel
202      (unless (eq (functional-entry-fun functional) functional)
203        (barf "The ENTRY-FUN in ~S isn't a self-pointer." functional)))
204     ((nil :escape :cleanup)
205      (let ((ef (functional-entry-fun functional)))
206        (when ef
207          (check-function-reached ef functional)
208          (unless (eq (functional-kind ef) :external)
209            (barf "The ENTRY-FUN in ~S isn't an XEP: ~S." functional ef)))))
210     (:deleted
211      (return-from check-function-stuff)))
212
213   (case (functional-kind functional)
214     ((nil :optional :external :toplevel :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)))))
222
223   (etypecase functional
224     (clambda
225      (when (lambda-bind functional)
226        (check-node-reached (lambda-bind functional)))
227      (when (lambda-return functional)
228        (check-node-reached (lambda-return functional)))
229
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))))
237     (optional-dispatch
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)
243                              functional))))
244
245 (defun check-function-consistency (components)
246   (dolist (c components)
247     (dolist (new-fun (component-new-funs c))
248       (observe-functional new-fun))
249     (dolist (fun (component-lambdas c))
250       (when (eq (functional-kind fun) :external)
251         (let ((ef (functional-entry-fun 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))))
257
258   (dolist (c components)
259     (dolist (new-fun (component-new-funs c))
260       (check-function-stuff new-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)))))
267 \f
268 ;;;; loop consistency checking
269
270 #|
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
274 ;;; in any loop.
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))
279   (when (and superior
280              (/= (loop-depth loop) (1+ (loop-depth superior))))
281     (barf "wrong depth in ~S" loop))
282
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))
290
291   (do ((block (loop-blocks loop) (block-loop-next block)))
292       ((null 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)))
298
299   (dolist (inferior (loop-inferiors loop))
300     (check-loop-consistency inferior loop))
301   (values))
302
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))
308   (labels ((walk (l)
309              (if (eq (block-loop block) l)
310                  t
311                  (dolist (inferior (loop-inferiors l) nil)
312                    (when (walk inferior) (return t))))))
313     (unless (walk loop)
314       (barf "~S is in loop info for ~S but not in the loop." block loop)))
315   (values))
316
317 |#
318
319 ;;; Check a block for consistency at the general flow-graph level, and
320 ;;; call CHECK-NODE-CONSISTENCY on each node to locally check for
321 ;;; semantic consistency.
322 (declaim (ftype (function (cblock) (values)) check-block-consistency))
323 (defun check-block-consistency (block)
324
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)))
330
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)))
335     (unless fun-deleted
336       (check-function-reached fun block))
337     (when (not this-cont)
338       (barf "~S has no START." block))
339     (when (not last)
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))
343
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))
348       (dolist (node uses)
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)))
352
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)
357         (:deleted)
358         (:deleted-block-start
359          (let ((dest (continuation-dest last-cont)))
360            (when dest
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)))
364         (:block-start
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)))
368         (:inside-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))))
375
376       (when dest
377         (check-node-reached dest)))
378
379     (loop       
380       (unless (eq (continuation-block this-cont) block)
381         (barf "BLOCK in ~S should be ~S." this-cont block))
382
383       (let ((dest (continuation-dest this-cont)))
384         (when dest
385           (check-node-reached dest)))
386
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))
392
393         (unless fun-deleted
394           (check-node-consistency node))
395         
396         (let ((cont (node-cont node)))
397           (when (not cont)
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."
402                   cont
403                   block))
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))))
409         
410     (check-block-successors block))
411   (values))
412
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)))
419
420     (let* ((comp (block-component block)))
421       (dolist (b succ)
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."
428                 b
429                 block))))
430
431     (typecase last
432       (cif
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."
435                block))
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)))
440       (creturn
441        (unless (if (eq (functional-kind (return-lambda last)) :deleted)
442                    (null succ)
443                    (and (= (length succ) 1)
444                         (eq (first succ)
445                             (component-tail (block-component block)))))
446          (barf "strange successors for RETURN in ~S" block)))
447       (exit
448        (unless (proper-list-of-length-p succ 0 1)
449          (barf "EXIT node with strange number of successors: ~S" last)))
450       (t
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."
454                block)))))
455   (values))
456 \f
457 ;;;; node consistency checking
458
459 ;;; Check that the DEST for CONT is the specified NODE. We also mark
460 ;;; the 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)))
464     (ecase kind
465       (:deleted
466        (unless (block-delete-p (node-block node))
467          (barf "DEST ~S of deleted continuation ~S is not DELETE-P."
468                cont node)))
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)))))
477   (values))
478
479 ;;; This function deals with checking for consistency of the
480 ;;; type-dependent information in a node.
481 (defun check-node-consistency (node)
482   (declare (type node node))
483   (etypecase node
484     (ref
485      (let ((leaf (ref-leaf node)))
486        (when (functional-p leaf)
487          (if (eq (functional-kind leaf) :toplevel-xep)
488              (unless (eq (component-kind (block-component (node-block node)))
489                          :toplevel)
490                (barf ":TOPLEVEL-XEP ref in non-top-level component: ~S"
491                      node))
492              (check-function-reached leaf node)))))
493     (basic-combination
494      (check-dest (basic-combination-fun node) node)
495      (dolist (arg (basic-combination-args node))
496        (cond
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))
501         (t
502          (locally
503            ;; KLUDGE: In sbcl-0.6.11.37, the compiler doesn't like
504            ;; (DECLARE (TYPE INDEX POS)) after the inline expansion of
505            ;; POSITION. It compiles it correctly, but it issues a type
506            ;; mismatch warning because it can't eliminate the
507            ;; possibility that control will flow through the
508            ;; NIL-returning branch. So we punt here. -- WHN 2001-04-15
509            (declare (notinline position))
510            (let ((fun (ref-leaf (continuation-use
511                                  (basic-combination-fun node))))
512                  (pos (position arg (basic-combination-args node))))
513              (declare (type index pos))
514              (when (leaf-refs (elt (lambda-vars fun) pos))
515                (barf "flushed arg for referenced var in ~S" node)))))))
516      (let ((dest (continuation-dest (node-cont node))))
517        (when (and (return-p dest)
518                   (eq (basic-combination-kind node) :local)
519                   (not (eq (lambda-tail-set (combination-lambda node))
520                            (lambda-tail-set (return-lambda dest)))))
521          (barf "tail local call to function with different tail set:~%  ~S"
522                node))))
523     (cif
524      (check-dest (if-test node) node)
525      (unless (eq (block-last (node-block node)) node)
526        (barf "IF not at block end: ~S" node)))
527     (cset
528      (check-dest (set-value node) node))
529     (bind
530      (check-function-reached (bind-lambda node) node))
531     (creturn
532      (check-function-reached (return-lambda node) node)
533      (check-dest (return-result node) node)
534      (unless (eq (block-last (node-block node)) node)
535        (barf "RETURN not at block end: ~S" node)))
536     (entry
537      (unless (member node (lambda-entries (node-home-lambda node)))
538        (barf "~S is not in ENTRIES for its home LAMBDA." node))
539      (dolist (exit (entry-exits node))
540        (unless (node-deleted exit)
541          (check-node-reached node))))
542     (exit
543      (let ((entry (exit-entry node))
544            (value (exit-value node)))
545        (cond (entry
546               (check-node-reached entry)
547               (unless (member node (entry-exits entry))
548                 (barf "~S is not in its ENTRY's EXITS." node))
549               (when value
550                 (check-dest value node)))
551              (t
552               (when value
553                 (barf "~S has VALUE but no ENTRY." node)))))))
554
555   (values))
556 \f
557 ;;;; IR2 consistency checking
558
559 ;;; Check for some kind of consistency in some Refs linked together by
560 ;;; TN-Ref-Across. VOP is the VOP that the references are in. Write-P is the
561 ;;; value of Write-P that should be present. Count is the minimum number of
562 ;;; operands expected. If More-P is true, then any larger number will also be
563 ;;; accepted. What is a string describing the kind of operand in error
564 ;;; messages.
565 (defun check-tn-refs (refs vop write-p count more-p what)
566   (let ((vop-refs (vop-refs vop)))
567     (do ((ref refs (tn-ref-across ref))
568          (num 0 (1+ num)))
569         ((null ref)
570          (when (< num count)
571            (barf "There should be at least ~W ~A in ~S, but there are only ~W."
572                  count what vop num))
573          (when (and (not more-p) (> num count))
574            (barf "There should be ~W ~A in ~S, but are ~W."
575                  count what vop num)))
576       (unless (eq (tn-ref-vop ref) vop)
577         (barf "VOP is ~S isn't ~S." ref vop))
578       (unless (eq (tn-ref-write-p ref) write-p)
579         (barf "The WRITE-P in ~S isn't ~S." vop write-p))
580       (unless (find-in #'tn-ref-next-ref ref vop-refs)
581         (barf "~S not found in REFS for ~S" ref vop))
582       (unless (find-in #'tn-ref-next ref
583                        (if (tn-ref-write-p ref)
584                            (tn-writes (tn-ref-tn ref))
585                            (tn-reads (tn-ref-tn ref))))
586         (barf "~S not found in reads/writes for its TN" ref))
587
588       (let ((target (tn-ref-target ref)))
589         (when target
590           (unless (eq (tn-ref-write-p target) (not (tn-ref-write-p ref)))
591             (barf "The target for ~S isn't complementary WRITE-P." ref))
592           (unless (find-in #'tn-ref-next-ref target vop-refs)
593             (barf "The target for ~S isn't in REFS for ~S." ref vop)))))))
594
595 ;;; Verify the sanity of the VOP-Refs slot in VOP. This involves checking
596 ;;; that each referenced TN appears as an argument, result or temp, and also
597 ;;; basic checks for the plausibility of the specified ordering of the refs.
598 (defun check-vop-refs (vop)
599   (declare (type vop vop))
600   (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
601       ((null ref))
602     (cond
603      ((find-in #'tn-ref-across ref (vop-args vop)))
604      ((find-in #'tn-ref-across ref (vop-results vop)))
605      ((not (eq (tn-ref-vop ref) vop))
606       (barf "VOP in ~S isn't ~S." ref vop))
607      ((find-in #'tn-ref-across ref (vop-temps vop)))
608      ((tn-ref-write-p ref)
609       (barf "stray ref that isn't a READ: ~S" ref))
610      (t
611       (let* ((tn (tn-ref-tn ref))
612              (temp (find-in #'tn-ref-across tn (vop-temps vop)
613                             :key #'tn-ref-tn)))
614         (unless temp
615           (barf "stray ref with no corresponding temp write: ~S" ref))
616         (unless (find-in #'tn-ref-next-ref temp (tn-ref-next-ref ref))
617           (barf "Read is after write for temp ~S in refs of ~S."
618                 tn vop))))))
619   (values))
620
621 ;;; Check the basic sanity of the VOP linkage, then call some other
622 ;;; functions to check on the TN-Refs. We grab some info out of the VOP-Info
623 ;;; to tell us what to expect.
624 ;;;
625 ;;; [### Check that operand type restrictions are met?]
626 (defun check-ir2-block-consistency (2block)
627   (declare (type ir2-block 2block))
628   (do ((vop (ir2-block-start-vop 2block)
629             (vop-next vop))
630        (prev nil vop))
631       ((null vop)
632        (unless (eq prev (ir2-block-last-vop 2block))
633          (barf "The last VOP in ~S should be ~S." 2block prev)))
634     (unless (eq (vop-prev vop) prev)
635       (barf "PREV in ~S should be ~S." vop prev))
636
637     (unless (eq (vop-block vop) 2block)
638       (barf "BLOCK in ~S should be ~S." vop 2block))
639
640     (check-vop-refs vop)
641
642     (let* ((info (vop-info vop))
643            (atypes (template-arg-types info))
644            (rtypes (template-result-types info)))
645       (check-tn-refs (vop-args vop) vop nil
646                      (count-if-not (lambda (x)
647                                      (and (consp x)
648                                           (eq (car x) :constant)))
649                                    atypes)
650                      (template-more-args-type info) "args")
651       (check-tn-refs (vop-results vop) vop t
652                      (if (eq rtypes :conditional) 0 (length rtypes))
653                      (template-more-results-type info) "results")
654       (check-tn-refs (vop-temps vop) vop t 0 t "temps")
655       (unless (= (length (vop-codegen-info vop))
656                  (template-info-arg-count info))
657         (barf "wrong number of codegen info args in ~S" vop))))
658   (values))
659
660 ;;; Check stuff about the IR2 representation of Component. This assumes the
661 ;;; sanity of the basic flow graph.
662 ;;;
663 ;;; [### Also grovel global TN data structures?  Assume pack not
664 ;;; done yet?  Have separate check-tn-consistency for pre-pack and
665 ;;; check-pack-consistency for post-pack?]
666 (defun check-ir2-consistency (component)
667   (declare (type component component))
668   (do-ir2-blocks (block component)
669     (check-ir2-block-consistency block))
670   (values))
671 \f
672 ;;;; lifetime analysis checking
673
674 ;;; Dump some info about how many TNs there, and what the conflicts data
675 ;;; structures are like.
676 (defun pre-pack-tn-stats (component &optional (stream *error-output*))
677   (declare (type component component))
678   (let ((wired 0)
679         (global 0)
680         (local 0)
681         (confs 0)
682         (unused 0)
683         (const 0)
684         (temps 0)
685         (environment 0)
686         (comp 0))
687     (do-packed-tns (tn component)
688       (let ((reads (tn-reads tn))
689             (writes (tn-writes tn)))
690         (when (and reads writes
691                    (not (tn-ref-next reads)) (not (tn-ref-next writes))
692                    (eq (tn-ref-vop reads) (tn-ref-vop writes)))
693           (incf temps)))
694       (when (tn-offset tn)
695         (incf wired))
696       (unless (or (tn-reads tn) (tn-writes tn))
697         (incf unused))
698       (cond ((eq (tn-kind tn) :component)
699              (incf comp))
700             ((tn-global-conflicts tn)
701              (case (tn-kind tn)
702                ((:environment :debug-environment) (incf environment))
703                (t (incf global)))
704              (do ((conf (tn-global-conflicts tn)
705                         (global-conflicts-tn-next conf)))
706                  ((null conf))
707                (incf confs)))
708             (t
709              (incf local))))
710
711     (do ((tn (ir2-component-constant-tns (component-info component))
712              (tn-next tn)))
713         ((null tn))
714       (incf const))
715
716     (format stream
717      "~%TNs: ~W local, ~W temps, ~W constant, ~W env, ~W comp, ~W global.~@
718        Wired: ~W, Unused: ~W. ~W block~:P, ~W global conflict~:P.~%"
719        local temps const environment comp global wired unused
720        (ir2-block-count component)
721        confs))
722   (values))
723
724 ;;; If the entry in Local-TNs for TN in Block is :More, then do some checks
725 ;;; for the validity of the usage.
726 (defun check-more-tn-entry (tn block)
727   (let* ((vop (ir2-block-start-vop block))
728          (info (vop-info vop)))
729     (macrolet ((frob (more-p ops)
730                  `(and (,more-p info)
731                        (find-in #'tn-ref-across tn (,ops vop)
732                                 :key #'tn-ref-tn))))
733       (unless (and (eq vop (ir2-block-last-vop block))
734                    (or (frob template-more-args-type vop-args)
735                        (frob template-more-results-type vop-results)))
736         (barf "strange :MORE LTN entry for ~S in ~S" tn block))))
737   (values))
738
739 (defun check-tn-conflicts (component)
740   (do-packed-tns (tn component)
741     (unless (or (not (eq (tn-kind tn) :normal))
742                 (tn-reads tn)
743                 (tn-writes tn))
744       (barf "no references to ~S" tn))
745
746     (unless (tn-sc tn) (barf "~S has no SC." tn))
747
748     (let ((conf (tn-global-conflicts tn))
749           (kind (tn-kind tn)))
750       (cond
751        ((eq kind :component)
752         (unless (member tn (ir2-component-component-tns
753                             (component-info component)))
754           (barf "~S not in Component-TNs for ~S" tn component)))
755        (conf
756         (do ((conf conf (global-conflicts-tn-next conf))
757              (prev nil conf))
758             ((null conf))
759           (unless (eq (global-conflicts-tn conf) tn)
760             (barf "TN in ~S should be ~S." conf tn))
761
762           (unless (eq (global-conflicts-kind conf) :live)
763             (let* ((block (global-conflicts-block conf))
764                    (ltn (svref (ir2-block-local-tns block)
765                                (global-conflicts-number conf))))
766               (cond ((eq ltn tn))
767                     ((eq ltn :more) (check-more-tn-entry tn block))
768                     (t
769                      (barf "~S wrong in LTN map for ~S" conf tn)))))
770
771           (when prev
772             (unless (> (ir2-block-number (global-conflicts-block conf))
773                        (ir2-block-number (global-conflicts-block prev)))
774               (barf "~s and ~s out of order" prev conf)))))
775        ((member (tn-kind tn) '(:constant :specified-save)))
776        (t
777         (let ((local (tn-local tn)))
778           (unless local
779             (barf "~S has no global conflicts, but isn't local either." tn))
780           (unless (eq (svref (ir2-block-local-tns local)
781                              (tn-local-number tn))
782                       tn)
783             (barf "~S wrong in LTN map" tn))
784           (do ((ref (tn-reads tn) (tn-ref-next ref)))
785               ((null ref))
786             (unless (eq (vop-block (tn-ref-vop ref)) local)
787               (barf "~S has references in blocks other than its LOCAL block."
788                     tn)))
789           (do ((ref (tn-writes tn) (tn-ref-next ref)))
790               ((null ref))
791             (unless (eq (vop-block (tn-ref-vop ref)) local)
792               (barf "~S has references in blocks other than its LOCAL block."
793                     tn))))))))
794   (values))
795
796 (defun check-block-conflicts (component)
797   (do-ir2-blocks (block component)
798     (do ((conf (ir2-block-global-tns block)
799                (global-conflicts-next conf))
800          (prev nil conf))
801         ((null conf))
802       (when prev
803         (unless (> (tn-number (global-conflicts-tn conf))
804                    (tn-number (global-conflicts-tn prev)))
805           (barf "~S and ~S out of order in ~S" prev conf block)))
806
807       (unless (find-in #'global-conflicts-tn-next
808                        conf
809                        (tn-global-conflicts
810                         (global-conflicts-tn conf)))
811         (barf "~S missing from global conflicts of its TN" conf)))
812
813     (let ((map (ir2-block-local-tns block)))
814       (dotimes (i (ir2-block-local-tn-count block))
815         (let ((tn (svref map i)))
816           (unless (or (eq tn :more)
817                       (null tn)
818                       (tn-global-conflicts tn)
819                       (eq (tn-local tn) block))
820             (barf "strange TN ~S in LTN map for ~S" tn block)))))))
821
822 ;;; All TNs live at the beginning of an environment must be passing
823 ;;; locations associated with that environment. We make an exception
824 ;;; for wired TNs in XEP functions, since we randomly reference wired
825 ;;; TNs to access the full call passing locations.
826 (defun check-environment-lifetimes (component)
827   (dolist (fun (component-lambdas component))
828     (let* ((env (lambda-physenv fun))
829            (2env (physenv-info env))
830            (vars (lambda-vars fun))
831            (closure (ir2-physenv-closure 2env))
832            (pc (ir2-physenv-return-pc-pass 2env))
833            (fp (ir2-physenv-old-fp 2env))
834            (2block (block-info (lambda-block (physenv-lambda env)))))
835       (do ((conf (ir2-block-global-tns 2block)
836                  (global-conflicts-next conf)))
837           ((null conf))
838         (let ((tn (global-conflicts-tn conf)))
839           (unless (or (eq (global-conflicts-kind conf) :write)
840                       (eq tn pc)
841                       (eq tn fp)
842                       (and (xep-p fun) (tn-offset tn))
843                       (member (tn-kind tn) '(:environment :debug-environment))
844                       (member tn vars :key #'leaf-info)
845                       (member tn closure :key #'cdr))
846             (barf "strange TN live at head of ~S: ~S" env tn))))))
847   (values))
848
849 ;;; Check for some basic sanity in the TN conflict data structures,
850 ;;; and also check that no TNs are unexpectedly live at environment
851 ;;; entry.
852 (defun check-life-consistency (component)
853   (check-tn-conflicts component)
854   (check-block-conflicts component)
855   (check-environment-lifetimes component))
856 \f
857 ;;;; pack consistency checking
858
859 (defun check-pack-consistency (component)
860   (flet ((check (scs ops)
861            (do ((scs scs (cdr scs))
862                 (op ops (tn-ref-across op)))
863                ((null scs))
864              (let ((load-tn (tn-ref-load-tn op)))
865                (unless (eq (svref (car scs)
866                                   (sc-number
867                                    (tn-sc
868                                     (or load-tn (tn-ref-tn op)))))
869                            t)
870                  (barf "operand restriction not satisfied: ~S" op))))))
871     (do-ir2-blocks (block component)
872       (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
873           ((null vop))
874         (let ((info (vop-info vop)))
875           (check (vop-info-result-load-scs info) (vop-results vop))
876           (check (vop-info-arg-load-scs info) (vop-args vop))))))
877   (values))
878 \f
879 ;;;; data structure dumping routines
880
881 ;;; When we print Continuations and TNs, we assign them small numeric IDs so
882 ;;; that we can get a handle on anonymous objects given a printout.
883 (macrolet ((def-frob (counter vto vfrom fto ffrom)
884              `(progn
885                 (defvar ,vto (make-hash-table :test 'eq))
886                 (defvar ,vfrom (make-hash-table :test 'eql))
887                 (proclaim '(hash-table ,vto ,vfrom))
888                 (defvar ,counter 0)
889                 (proclaim '(fixnum ,counter))
890                 
891                 (defun ,fto (x)
892                   (or (gethash x ,vto)
893                       (let ((num (incf ,counter)))
894                         (setf (gethash num ,vfrom) x)
895                         (setf (gethash x ,vto) num))))
896                 
897                 (defun ,ffrom (num)
898                   (values (gethash num ,vfrom))))))
899   (def-frob *continuation-number* *continuation-numbers* *number-continuations* cont-num num-cont)
900   (def-frob *tn-id* *tn-ids* *id-tns* tn-id id-tn)
901   (def-frob *label-id* *id-labels* *label-ids* label-id id-label))
902
903 ;;; Print a terse one-line description of LEAF.
904 (defun print-leaf (leaf &optional (stream *standard-output*))
905   (declare (type leaf leaf) (type stream stream))
906   (etypecase leaf
907     (lambda-var (prin1 (leaf-debug-name leaf) stream))
908     (constant (format stream "'~S" (constant-value leaf)))
909     (global-var
910      (format stream "~S {~A}" (leaf-debug-name leaf) (global-var-kind leaf)))
911     (functional
912      (format stream "~S ~S" (type-of leaf) (functional-debug-name leaf)))))
913
914 ;;; Attempt to find a block given some thing that has to do with it.
915 (declaim (ftype (function (t) cblock) block-or-lose))
916 (defun block-or-lose (thing)
917   (ctypecase thing
918     (cblock thing)
919     (ir2-block (ir2-block-block thing))
920     (vop (block-or-lose (vop-block thing)))
921     (tn-ref (block-or-lose (tn-ref-vop thing)))
922     (continuation (continuation-block thing))
923     (node (node-block thing))
924     (component (component-head thing))
925 #|    (cloop (loop-head thing))|#
926     (integer (continuation-block (num-cont thing)))
927     (functional (lambda-block (main-entry thing)))
928     (null (error "Bad thing: ~S." thing))
929     (symbol (block-or-lose (gethash thing *free-functions*)))))
930
931 ;;; Print cN.
932 (defun print-continuation (cont)
933   (declare (type continuation cont))
934   (format t " c~D" (cont-num cont))
935   (values))
936
937 ;;; Print out the nodes in BLOCK in a format oriented toward
938 ;;; representing what the code does.
939 (defun print-nodes (block)
940   (setq block (block-or-lose block))
941   (format t "~%block start c~D" (cont-num (block-start block)))
942
943   (let ((last (block-last block)))
944     (terpri)
945     (do ((cont (block-start block) (node-cont (continuation-next cont))))
946         (())
947       (let ((node (continuation-next cont)))
948         (format t "~3D: " (cont-num (node-cont node)))
949         (etypecase node
950           (ref (print-leaf (ref-leaf node)))
951           (basic-combination
952            (let ((kind (basic-combination-kind node)))
953              (format t "~(~A ~A~) c~D"
954                      (if (function-info-p kind) "known" kind)
955                      (type-of node)
956                      (cont-num (basic-combination-fun node)))
957              (dolist (arg (basic-combination-args node))
958                (if arg
959                    (print-continuation arg)
960                    (format t " <none>")))))
961           (cset
962            (write-string "set ")
963            (print-leaf (set-var node))
964            (print-continuation (set-value node)))
965           (cif
966            (format t "if c~D" (cont-num (if-test node)))
967            (print-continuation (block-start (if-consequent node)))
968            (print-continuation (block-start (if-alternative node))))
969           (bind
970            (write-string "bind ")
971            (print-leaf (bind-lambda node)))
972           (creturn
973            (format t "return c~D " (cont-num (return-result node)))
974            (print-leaf (return-lambda node)))
975           (entry
976            (format t "entry ~S" (entry-exits node)))
977           (exit
978            (let ((value (exit-value node)))
979              (cond (value
980                     (format t "exit c~D" (cont-num value)))
981                    ((exit-entry node)
982                     (format t "exit <no value>"))
983                    (t
984                     (format t "exit <degenerate>"))))))
985         (terpri)
986         (when (eq node last) (return)))))
987
988   (let ((succ (block-succ block)))
989     (format t "successors~{ c~D~}~%"
990             (mapcar (lambda (x) (cont-num (block-start x))) succ)))
991   (values))
992
993 ;;; Print a useful representation of a TN. If the TN has a leaf, then do a
994 ;;; Print-Leaf on that, otherwise print a generated ID.
995 (defun print-tn (tn &optional (stream *standard-output*))
996   (declare (type tn tn))
997   (let ((leaf (tn-leaf tn)))
998     (cond (leaf
999            (print-leaf leaf stream)
1000            (format stream "!~D" (tn-id tn)))
1001           (t
1002            (format stream "t~D" (tn-id tn))))
1003     (when (and (tn-sc tn) (tn-offset tn))
1004       (format stream "[~A]" (location-print-name tn)))))
1005
1006 ;;; Print the TN-Refs representing some operands to a VOP, linked by
1007 ;;; TN-Ref-Across.
1008 (defun print-operands (refs)
1009   (declare (type (or tn-ref null) refs))
1010   (pprint-logical-block (*standard-output* nil)
1011     (do ((ref refs (tn-ref-across ref)))
1012         ((null ref))
1013       (let ((tn (tn-ref-tn ref))
1014             (ltn (tn-ref-load-tn ref)))
1015         (cond ((not ltn)
1016                (print-tn tn))
1017               (t
1018                (print-tn tn)
1019                (princ (if (tn-ref-write-p ref) #\< #\>))
1020                (print-tn ltn)))
1021         (princ #\space)
1022         (pprint-newline :fill)))))
1023
1024 ;;; Print the vop, putting args, info and results on separate lines, if
1025 ;;; necessary.
1026 (defun print-vop (vop)
1027   (pprint-logical-block (*standard-output* nil)
1028     (princ (vop-info-name (vop-info vop)))
1029     (princ #\space)
1030     (pprint-indent :current 0)
1031     (print-operands (vop-args vop))
1032     (pprint-newline :linear)
1033     (when (vop-codegen-info vop)
1034       (princ (with-output-to-string (stream)
1035                (let ((*print-level* 1)
1036                      (*print-length* 3))
1037                  (format stream "{~{~S~^ ~}} " (vop-codegen-info vop)))))
1038       (pprint-newline :linear))
1039     (when (vop-results vop)
1040       (princ "=> ")
1041       (print-operands (vop-results vop))))
1042   (terpri))
1043
1044 ;;; Print the VOPs in the specified IR2 block.
1045 (defun print-ir2-block (block)
1046   (declare (type ir2-block block))
1047   (cond
1048    ((eq (block-info (ir2-block-block block)) block)
1049     (format t "~%IR2 block start c~D~%"
1050             (cont-num (block-start (ir2-block-block block))))
1051     (let ((label (ir2-block-%label block)))
1052       (when label
1053         (format t "L~D:~%" (label-id label)))))
1054    (t
1055     (format t "<overflow>~%")))
1056
1057   (do ((vop (ir2-block-start-vop block)
1058             (vop-next vop))
1059        (number 0 (1+ number)))
1060       ((null vop))
1061     (format t "~W: " number)
1062     (print-vop vop)))
1063
1064 ;;; This is like PRINT-NODES, but dumps the IR2 representation of the
1065 ;;; code in BLOCK.
1066 (defun print-vops (block)
1067   (setq block (block-or-lose block))
1068   (let ((2block (block-info block)))
1069     (print-ir2-block 2block)
1070     (do ((b (ir2-block-next 2block) (ir2-block-next b)))
1071         ((not (eq (ir2-block-block b) block)))
1072       (print-ir2-block b)))
1073   (values))
1074
1075 ;;; Scan the IR2 blocks in emission order.
1076 (defun print-ir2-blocks (thing)
1077   (do-ir2-blocks (block (block-component (block-or-lose thing)))
1078     (print-ir2-block block))
1079   (values))
1080
1081 ;;; Do a PRINT-NODES on BLOCK and all blocks reachable from it by
1082 ;;; successor links.
1083 (defun print-blocks (block)
1084   (setq block (block-or-lose block))
1085   (do-blocks (block (block-component block) :both)
1086     (setf (block-flag block) nil))
1087   (labels ((walk (block)
1088              (unless (block-flag block)
1089                (setf (block-flag block) t)
1090                (when (block-start block)
1091                  (print-nodes block))
1092                (dolist (block (block-succ block))
1093                  (walk block)))))
1094     (walk block))
1095   (values))
1096
1097 ;;; Print all blocks in BLOCK's component in DFO.
1098 (defun print-all-blocks (thing)
1099   (do-blocks (block (block-component (block-or-lose thing)))
1100     (handler-case (print-nodes block)
1101       (error (condition)
1102         (format t "~&~A...~%" condition))))
1103   (values))
1104
1105 (defvar *list-conflicts-table* (make-hash-table :test 'eq))
1106
1107 ;;; Add all ALWAYS-LIVE TNs in Block to the conflicts. TN is ignored when
1108 ;;; it appears in the global conflicts.
1109 (defun add-always-live-tns (block tn)
1110   (declare (type ir2-block block) (type tn tn))
1111   (do ((conf (ir2-block-global-tns block)
1112              (global-conflicts-next conf)))
1113       ((null conf))
1114     (when (eq (global-conflicts-kind conf) :live)
1115       (let ((btn (global-conflicts-tn conf)))
1116         (unless (eq btn tn)
1117           (setf (gethash btn *list-conflicts-table*) t)))))
1118   (values))
1119
1120 ;;; Add all local TNs in block to the conflicts.
1121 (defun add-all-local-tns (block)
1122   (declare (type ir2-block block))
1123   (let ((ltns (ir2-block-local-tns block)))
1124     (dotimes (i (ir2-block-local-tn-count block))
1125       (setf (gethash (svref ltns i) *list-conflicts-table*) t)))
1126   (values))
1127
1128 ;;; Make a list out of all of the recorded conflicts.
1129 (defun listify-conflicts-table ()
1130   (collect ((res))
1131     (maphash (lambda (k v)
1132                (declare (ignore v))
1133                (when k
1134                  (res k)))
1135              *list-conflicts-table*)
1136     (clrhash *list-conflicts-table*)
1137     (res)))
1138
1139 ;;; Return a list of a the TNs that conflict with TN. Sort of, kind
1140 ;;; of. For debugging use only. Probably doesn't work on :COMPONENT TNs.
1141 (defun list-conflicts (tn)
1142   (aver (member (tn-kind tn) '(:normal :environment :debug-environment)))
1143   (let ((confs (tn-global-conflicts tn)))
1144     (cond (confs
1145            (clrhash *list-conflicts-table*)
1146            (do ((conf confs (global-conflicts-tn-next conf)))
1147                ((null conf))
1148              (let ((block (global-conflicts-block conf)))
1149                (add-always-live-tns block tn)
1150                (if (eq (global-conflicts-kind conf) :live)
1151                    (add-all-local-tns block)
1152                    (let ((bconf (global-conflicts-conflicts conf))
1153                          (ltns (ir2-block-local-tns block)))
1154                      (dotimes (i (ir2-block-local-tn-count block))
1155                        (when (/= (sbit bconf i) 0)
1156                          (setf (gethash (svref ltns i) *list-conflicts-table*)
1157                                t)))))))
1158            (listify-conflicts-table))
1159           (t
1160            (let* ((block (tn-local tn))
1161                   (ltns (ir2-block-local-tns block))
1162                   (confs (tn-local-conflicts tn)))
1163              (collect ((res))
1164                (dotimes (i (ir2-block-local-tn-count block))
1165                  (when (/= (sbit confs i) 0)
1166                    (let ((tn (svref ltns i)))
1167                      (when (and tn (not (eq tn :more))
1168                                 (not (tn-global-conflicts tn)))
1169                        (res tn)))))
1170                (do ((gtn (ir2-block-global-tns block)
1171                          (global-conflicts-next gtn)))
1172                    ((null gtn))
1173                  (when (or (eq (global-conflicts-kind gtn) :live)
1174                            (/= (sbit confs (global-conflicts-number gtn)) 0))
1175                    (res (global-conflicts-tn gtn))))
1176                (res)))))))
1177
1178 (defun nth-vop (thing n)
1179   #!+sb-doc
1180   "Return the Nth VOP in the IR2-Block pointed to by Thing."
1181   (let ((block (block-info (block-or-lose thing))))
1182     (do ((i 0 (1+ i))
1183          (vop (ir2-block-start-vop block) (vop-next vop)))
1184         ((= i n) vop))))