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