b30b86d3a38fedfda23bd7b4869689b71b51ad8c
[sbcl.git] / src / compiler / pack.lisp
1 ;;;; This file contains the implementation-independent code for Pack
2 ;;;; phase in the compiler. Pack is responsible for assigning TNs to
3 ;;;; storage allocations or "register allocation".
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15
16 ;;; for debugging: some parameters controlling which optimizations we
17 ;;; attempt
18 (defvar *pack-assign-costs* t)
19 (defvar *pack-optimize-saves* t)
20 ;;; FIXME: Perhaps SB-FLUID should be renamed to SB-TWEAK and these
21 ;;; should be made conditional on SB-TWEAK.
22
23 (declaim (ftype (function (component) index) ir2-block-count))
24 \f
25 ;;;; conflict determination
26
27 ;;; Return true if the element at the specified offset in SB has a
28 ;;; conflict with TN:
29 ;;; -- If a component-live TN (:COMPONENT kind), then iterate over
30 ;;;    all the blocks. If the element at OFFSET is used anywhere in
31 ;;;    any of the component's blocks (always-live /= 0), then there
32 ;;;    is a conflict.
33 ;;; -- If TN is global (Confs true), then iterate over the blocks TN
34 ;;;    is live in (using TN-GLOBAL-CONFLICTS). If the TN is live
35 ;;;    everywhere in the block (:LIVE), then there is a conflict
36 ;;;    if the element at offset is used anywhere in the block
37 ;;;    (Always-Live /= 0). Otherwise, we use the local TN number for
38 ;;;    TN in block to find whether TN has a conflict at Offset in
39 ;;;    that block.
40 ;;; -- If TN is local, then we just check for a conflict in the block
41 ;;;    it is local to.
42 (defun offset-conflicts-in-sb (tn sb offset)
43   (declare (type tn tn) (type finite-sb sb) (type index offset))
44   (let ((confs (tn-global-conflicts tn))
45         (kind (tn-kind tn)))
46     (cond
47      ((eq kind :component)
48       (let ((loc-live (svref (finite-sb-always-live sb) offset)))
49         (dotimes (i (ir2-block-count *component-being-compiled*) nil)
50           (when (/= (sbit loc-live i) 0)
51             (return t)))))
52      (confs
53       (let ((loc-confs (svref (finite-sb-conflicts sb) offset))
54             (loc-live (svref (finite-sb-always-live sb) offset)))
55         (do ((conf confs (global-conflicts-next-tnwise conf)))
56             ((null conf)
57              nil)
58           (let* ((block (global-conflicts-block conf))
59                  (num (ir2-block-number block)))
60             (if (eq (global-conflicts-kind conf) :live)
61                 (when (/= (sbit loc-live num) 0)
62                   (return t))
63                 (when (/= (sbit (svref loc-confs num)
64                                 (global-conflicts-number conf))
65                           0)
66                   (return t)))))))
67      (t
68       (/= (sbit (svref (svref (finite-sb-conflicts sb) offset)
69                        (ir2-block-number (tn-local tn)))
70                 (tn-local-number tn))
71           0)))))
72
73 ;;; Return true if TN has a conflict in SC at the specified offset.
74 (defun conflicts-in-sc (tn sc offset)
75   (declare (type tn tn) (type sc sc) (type index offset))
76   (let ((sb (sc-sb sc)))
77     (dotimes (i (sc-element-size sc) nil)
78       (when (offset-conflicts-in-sb tn sb (+ offset i))
79         (return t)))))
80
81 ;;; Add TN's conflicts into the conflicts for the location at OFFSET
82 ;;; in SC. We iterate over each location in TN, adding to the
83 ;;; conflicts for that location:
84 ;;; -- If TN is a :COMPONENT TN, then iterate over all the blocks,
85 ;;;    setting all of the local conflict bits and the always-live bit.
86 ;;;    This records a conflict with any TN that has a LTN number in
87 ;;;    the block, as well as with :ALWAYS-LIVE and :ENVIRONMENT TNs.
88 ;;; -- If TN is global, then iterate over the blocks TN is live in. In
89 ;;;    addition to setting the always-live bit to represent the conflict
90 ;;;    with TNs live throughout the block, we also set bits in the
91 ;;;    local conflicts. If TN is :ALWAYS-LIVE in the block, we set all
92 ;;;    the bits, otherwise we OR in the local conflict bits.
93 ;;; -- If the TN is local, then we just do the block it is local to,
94 ;;;    setting always-live and OR'ing in the local conflicts.
95 (defun add-location-conflicts (tn sc offset optimize)
96   (declare (type tn tn) (type sc sc) (type index offset))
97   (let ((confs (tn-global-conflicts tn))
98         (sb (sc-sb sc))
99         (kind (tn-kind tn)))
100     (dotimes (i (sc-element-size sc))
101       (declare (type index i))
102       (let* ((this-offset (+ offset i))
103              (loc-confs (svref (finite-sb-conflicts sb) this-offset))
104              (loc-live (svref (finite-sb-always-live sb) this-offset)))
105         (cond
106          ((eq kind :component)
107           (dotimes (num (ir2-block-count *component-being-compiled*))
108             (declare (type index num))
109             (setf (sbit loc-live num) 1)
110             (set-bit-vector (svref loc-confs num))))
111          (confs
112           (do ((conf confs (global-conflicts-next-tnwise conf)))
113               ((null conf))
114             (let* ((block (global-conflicts-block conf))
115                    (num (ir2-block-number block))
116                    (local-confs (svref loc-confs num)))
117               (declare (type local-tn-bit-vector local-confs))
118               (setf (sbit loc-live num) 1)
119               (if (eq (global-conflicts-kind conf) :live)
120                   (set-bit-vector local-confs)
121                   (bit-ior local-confs (global-conflicts-conflicts conf) t)))))
122          (t
123           (let ((num (ir2-block-number (tn-local tn))))
124             (setf (sbit loc-live num) 1)
125             (bit-ior (the local-tn-bit-vector (svref loc-confs num))
126                      (tn-local-conflicts tn) t))))
127         ;; Calculating ALWAYS-LIVE-COUNT is moderately expensive, and
128         ;; currently the information isn't used unless (> SPEED
129         ;; COMPILE-SPEED).
130         (when optimize
131           (setf (svref (finite-sb-always-live-count sb) this-offset)
132                 (find-location-usage sb this-offset))))))
133   (values))
134
135 ;; A rought measure of how much a given OFFSET in SB is currently
136 ;; used. Current implementation counts the amount of blocks where the
137 ;; offset has been marked as ALWAYS-LIVE.
138 (defun find-location-usage (sb offset)
139   (declare (optimize speed))
140   (declare (type sb sb) (type index offset))
141   (let* ((always-live (svref (finite-sb-always-live sb) offset)))
142     (declare (simple-bit-vector always-live))
143     (count 1 always-live)))
144
145 ;;; Return the total number of IR2-BLOCKs in COMPONENT.
146 (defun ir2-block-count (component)
147   (declare (type component component))
148   (do ((2block (block-info (block-next (component-head component)))
149                (ir2-block-next 2block)))
150       ((null 2block)
151        (error "What?  No ir2 blocks have a non-nil number?"))
152     (when (ir2-block-number 2block)
153       (return (1+ (ir2-block-number 2block))))))
154
155 ;;; Ensure that the conflicts vectors for each :FINITE SB are large
156 ;;; enough for the number of blocks allocated. Also clear any old
157 ;;; conflicts and reset the current size to the initial size.
158 (defun init-sb-vectors (component)
159   (let ((nblocks (ir2-block-count component)))
160     (dolist (sb *backend-sb-list*)
161       (unless (eq (sb-kind sb) :non-packed)
162         (let* ((conflicts (finite-sb-conflicts sb))
163                (always-live (finite-sb-always-live sb))
164                (always-live-count (finite-sb-always-live-count sb))
165                (max-locs (length conflicts))
166                (last-count (finite-sb-last-block-count sb)))
167           (unless (zerop max-locs)
168             (let ((current-size (length (the simple-vector
169                                              (svref conflicts 0)))))
170               (cond
171                ((> nblocks current-size)
172                 (let ((new-size (max nblocks (* current-size 2))))
173                   (declare (type index new-size))
174                   (dotimes (i max-locs)
175                     (declare (type index i))
176                     (let ((new-vec (make-array new-size)))
177                       (let ((old (svref conflicts i)))
178                         (declare (simple-vector old))
179                         (dotimes (j current-size)
180                           (declare (type index j))
181                           (setf (svref new-vec j)
182                                 (clear-bit-vector (svref old j)))))
183
184                       (do ((j current-size (1+ j)))
185                           ((= j new-size))
186                         (declare (type index j))
187                         (setf (svref new-vec j)
188                               (make-array local-tn-limit :element-type 'bit
189                                           :initial-element 0)))
190                       (setf (svref conflicts i) new-vec))
191                     (setf (svref always-live i)
192                           (make-array new-size :element-type 'bit
193                                       :initial-element 0))
194                     (setf (svref always-live-count i) 0))))
195                (t
196                 (dotimes (i (finite-sb-current-size sb))
197                   (declare (type index i))
198                   (let ((conf (svref conflicts i)))
199                     (declare (simple-vector conf))
200                     (dotimes (j last-count)
201                       (declare (type index j))
202                       (clear-bit-vector (svref conf j))))
203                   (clear-bit-vector (svref always-live i))
204                   (setf (svref always-live-count i) 0))))))
205
206           (setf (finite-sb-last-block-count sb) nblocks)
207           (setf (finite-sb-current-size sb) (sb-size sb))
208           (setf (finite-sb-last-offset sb) 0))))))
209
210 ;;; Expand the :UNBOUNDED SB backing SC by either the initial size or
211 ;;; the SC element size, whichever is larger. If NEEDED-SIZE is
212 ;;; larger, then use that size.
213 (defun grow-sc (sc &optional (needed-size 0))
214   (declare (type sc sc) (type index needed-size))
215   (let* ((sb (sc-sb sc))
216          (size (finite-sb-current-size sb))
217          (align-mask (1- (sc-alignment sc)))
218          (inc (max (finite-sb-size-increment sb)
219                    (+ (sc-element-size sc)
220                       (- (logandc2 (+ size align-mask) align-mask)
221                          size))
222                    (- needed-size size)))
223          (new-size (let ((align-mask (1- (finite-sb-size-alignment sb))))
224                      (logandc2 (+  size inc align-mask) align-mask)))
225          (conflicts (finite-sb-conflicts sb))
226          (block-size (if (zerop (length conflicts))
227                          (ir2-block-count *component-being-compiled*)
228                          (length (the simple-vector (svref conflicts 0)))))
229          (padded-size (ash 1 (integer-length (1- new-size)))))
230     (declare (type index inc new-size padded-size))
231     (aver (eq (sb-kind sb) :unbounded))
232
233     (when (> padded-size (length conflicts))
234       (let ((new-conf (make-array padded-size)))
235         (replace new-conf conflicts)
236         (do ((i size (1+ i)))
237             ((= i padded-size))
238           (declare (type index i))
239           (let ((loc-confs (make-array block-size)))
240             (dotimes (j block-size)
241               (setf (svref loc-confs j)
242                     (make-array local-tn-limit
243                                 :initial-element 0
244                                 :element-type 'bit)))
245             (setf (svref new-conf i) loc-confs)))
246         (setf (finite-sb-conflicts sb) new-conf))
247
248       (let ((new-live (make-array padded-size)))
249         (replace new-live (finite-sb-always-live sb))
250         (do ((i size (1+ i)))
251             ((= i padded-size))
252           (setf (svref new-live i)
253                 (make-array block-size
254                             :initial-element 0
255                             :element-type 'bit)))
256         (setf (finite-sb-always-live sb) new-live))
257
258       (let ((new-live-count (make-array padded-size)))
259         (declare (optimize speed)) ;; FILL deftransform
260         (replace new-live-count (finite-sb-always-live-count sb))
261         (fill new-live-count 0 :start size)
262         (setf (finite-sb-always-live-count sb) new-live-count))
263
264       (let ((new-tns (make-array padded-size :initial-element nil)))
265         (replace new-tns (finite-sb-live-tns sb))
266         (fill (finite-sb-live-tns sb) nil)
267         (setf (finite-sb-live-tns sb) new-tns)))
268
269     (setf (finite-sb-current-size sb) new-size))
270   (values))
271
272 \f
273 ;;;; internal errors
274
275 ;;; Give someone a hard time because there isn't any load function
276 ;;; defined to move from SRC to DEST.
277 (defun no-load-fun-error (src dest)
278   (let* ((src-sc (tn-sc src))
279          (src-name (sc-name src-sc))
280          (dest-sc (tn-sc dest))
281          (dest-name (sc-name dest-sc)))
282     (cond ((eq (sb-kind (sc-sb src-sc)) :non-packed)
283            (unless (member src-sc (sc-constant-scs dest-sc))
284              (error "loading from an invalid constant SC?~@
285                      VM definition inconsistent, try recompiling."))
286            (error "no load function defined to load SC ~S ~
287                    from its constant SC ~S"
288                   dest-name src-name))
289           ((member src-sc (sc-alternate-scs dest-sc))
290            (error "no load function defined to load SC ~S from its ~
291                    alternate SC ~S"
292                   dest-name src-name))
293           ((member dest-sc (sc-alternate-scs src-sc))
294            (error "no load function defined to save SC ~S in its ~
295                    alternate SC ~S"
296                   src-name dest-name))
297           (t
298            ;; FIXME: "VM definition is inconsistent" shouldn't be a
299            ;; possibility in SBCL.
300            (error "loading to/from SCs that aren't alternates?~@
301                    VM definition is inconsistent, try recompiling.")))))
302
303 ;;; Called when we failed to pack TN. If RESTRICTED is true, then we
304 ;;; are restricted to pack TN in its SC.
305 (defun failed-to-pack-error (tn restricted)
306   (declare (type tn tn))
307   (let* ((sc (tn-sc tn))
308          (scs (cons sc (sc-alternate-scs sc))))
309     (cond
310      (restricted
311       (error "failed to pack restricted TN ~S in its SC ~S"
312              tn (sc-name sc)))
313      (t
314       (aver (not (find :unbounded scs
315                        :key (lambda (x) (sb-kind (sc-sb x))))))
316       (let ((ptype (tn-primitive-type tn)))
317         (cond
318          (ptype
319           (aver (member (sc-number sc) (primitive-type-scs ptype)))
320           (error "SC ~S doesn't have any :UNBOUNDED alternate SCs, but is~@
321                   a SC for primitive-type ~S."
322                  (sc-name sc) (primitive-type-name ptype)))
323          (t
324           (error "SC ~S doesn't have any :UNBOUNDED alternate SCs."
325                  (sc-name sc)))))))))
326
327 ;;; Return a list of format arguments describing how TN is used in
328 ;;; OP's VOP.
329 (defun describe-tn-use (loc tn op)
330   (let* ((vop (tn-ref-vop op))
331          (args (vop-args vop))
332          (results (vop-results vop))
333          (name (with-output-to-string (stream)
334                  (print-tn-guts tn stream)))
335          (2comp (component-info *component-being-compiled*))
336          temp)
337     (cond
338      ((setq temp (position-in #'tn-ref-across tn args :key #'tn-ref-tn))
339       `("~2D: ~A (~:R argument)" ,loc ,name ,(1+ temp)))
340      ((setq temp (position-in #'tn-ref-across tn results :key #'tn-ref-tn))
341       `("~2D: ~A (~:R result)" ,loc ,name ,(1+ temp)))
342      ((setq temp (position-in #'tn-ref-across tn args :key #'tn-ref-load-tn))
343       `("~2D: ~A (~:R argument load TN)" ,loc ,name ,(1+ temp)))
344      ((setq temp (position-in #'tn-ref-across tn results :key
345                               #'tn-ref-load-tn))
346       `("~2D: ~A (~:R result load TN)" ,loc ,name ,(1+ temp)))
347      ((setq temp (position-in #'tn-ref-across tn (vop-temps vop)
348                               :key #'tn-ref-tn))
349       `("~2D: ~A (temporary ~A)" ,loc ,name
350         ,(operand-parse-name (elt (vop-parse-temps
351                                    (vop-parse-or-lose
352                                     (vop-info-name  (vop-info vop))))
353                                   temp))))
354      ((eq (tn-kind tn) :component)
355       `("~2D: ~A (component live)" ,loc ,name))
356      ((position-in #'tn-next tn (ir2-component-wired-tns 2comp))
357       `("~2D: ~A (wired)" ,loc ,name))
358      ((position-in #'tn-next tn (ir2-component-restricted-tns 2comp))
359       `("~2D: ~A (restricted)" ,loc ,name))
360      (t
361       `("~2D: not referenced?" ,loc)))))
362
363 ;;; If load TN packing fails, try to give a helpful error message. We
364 ;;; find a TN in each location that conflicts, and print it.
365 (defun failed-to-pack-load-tn-error (scs op)
366   (declare (list scs) (type tn-ref op))
367   (collect ((used)
368             (unused))
369     (dolist (sc scs)
370       (let* ((sb (sc-sb sc))
371              (confs (finite-sb-live-tns sb)))
372         (aver (eq (sb-kind sb) :finite))
373         (dolist (el (sc-locations sc))
374           (declare (type index el))
375           (let ((conf (load-tn-conflicts-in-sc op sc el t)))
376             (if conf
377                 (used (describe-tn-use el conf op))
378                 (do ((i el (1+ i))
379                      (end (+ el (sc-element-size sc))))
380                     ((= i end)
381                      (unused el))
382                   (declare (type index i end))
383                   (let ((victim (svref confs i)))
384                     (when victim
385                       (used (describe-tn-use el victim op))
386                       (return t)))))))))
387
388     (multiple-value-bind (arg-p n more-p costs load-scs incon)
389         (get-operand-info op)
390       (declare (ignore costs load-scs))
391         (aver (not more-p))
392         (error "unable to pack a Load-TN in SC ~{~A~#[~^~;, or ~:;,~]~} ~
393                 for the ~:R ~:[result~;argument~] to~@
394                 the ~S VOP,~@
395                 ~:[since all SC elements are in use:~:{~%~@?~}~%~;~
396                 ~:*but these SC elements are not in use:~%  ~S~%Bug?~*~]~
397                 ~:[~;~@
398                 Current cost info inconsistent with that in effect at compile ~
399                 time. Recompile.~%Compilation order may be incorrect.~]"
400                (mapcar #'sc-name scs)
401                n arg-p
402                (vop-info-name (vop-info (tn-ref-vop op)))
403                (unused) (used)
404                incon))))
405
406 ;;; This is called when none of the SCs that we can load OP into are
407 ;;; allowed by OP's primitive-type.
408 (defun no-load-scs-allowed-by-primitive-type-error (ref)
409   (declare (type tn-ref ref))
410   (let* ((tn (tn-ref-tn ref))
411          (ptype (tn-primitive-type tn)))
412     (multiple-value-bind (arg-p pos more-p costs load-scs incon)
413         (get-operand-info ref)
414       (declare (ignore costs))
415       (aver (not more-p))
416       (error "~S is not valid as the ~:R ~:[result~;argument~] to VOP:~
417               ~%  ~S,~@
418               since the TN's primitive type ~S doesn't allow any of the SCs~@
419               allowed by the operand restriction:~%  ~S~
420               ~:[~;~@
421               Current cost info inconsistent with that in effect at compile ~
422               time. Recompile.~%Compilation order may be incorrect.~]"
423              tn pos arg-p
424              (template-name (vop-info (tn-ref-vop ref)))
425              (primitive-type-name ptype)
426              (mapcar #'sc-name (listify-restrictions load-scs))
427              incon))))
428 \f
429 ;;;; register saving
430
431 ;;; Do stuff to note that TN is spilled at VOP for the debugger's benefit.
432 (defun note-spilled-tn (tn vop)
433   (when (and (tn-leaf tn) (vop-save-set vop))
434     (let ((2comp (component-info *component-being-compiled*)))
435       (setf (gethash tn (ir2-component-spilled-tns 2comp)) t)
436       (pushnew tn (gethash vop (ir2-component-spilled-vops 2comp)))))
437   (values))
438
439 ;;; Make a save TN for TN, pack it, and return it. We copy various
440 ;;; conflict information from the TN so that pack does the right
441 ;;; thing.
442 (defun pack-save-tn (tn)
443   (declare (type tn tn))
444   (let ((res (make-tn 0 :save nil nil)))
445     (dolist (alt (sc-alternate-scs (tn-sc tn))
446                  (error "no unbounded alternate for SC ~S"
447                         (sc-name (tn-sc tn))))
448       (when (eq (sb-kind (sc-sb alt)) :unbounded)
449         (setf (tn-save-tn tn) res)
450         (setf (tn-save-tn res) tn)
451         (setf (tn-sc res) alt)
452         (pack-tn res t nil)
453         (return res)))))
454
455 ;;; Find the load function for moving from SRC to DEST and emit a
456 ;;; MOVE-OPERAND VOP with that function as its info arg.
457 (defun emit-operand-load (node block src dest before)
458   (declare (type node node) (type ir2-block block)
459            (type tn src dest) (type (or vop null) before))
460   (emit-load-template node block
461                       (template-or-lose 'move-operand)
462                       src dest
463                       (list (or (svref (sc-move-funs (tn-sc dest))
464                                        (sc-number (tn-sc src)))
465                                 (no-load-fun-error src dest)))
466                       before)
467   (values))
468
469 ;;; Find the preceding use of the VOP NAME in the emit order, starting
470 ;;; with VOP. We must find the VOP in the same IR1 block.
471 (defun reverse-find-vop (name vop)
472   (do* ((block (vop-block vop) (ir2-block-prev block))
473         (last vop (ir2-block-last-vop block)))
474        (nil)
475     (aver (eq (ir2-block-block block) (ir2-block-block (vop-block vop))))
476     (do ((current last (vop-prev current)))
477         ((null current))
478       (when (eq (vop-info-name (vop-info current)) name)
479         (return-from reverse-find-vop current)))))
480
481 ;;; For TNs that have other than one writer, we save the TN before
482 ;;; each call. If a local call (MOVE-ARGS is :LOCAL-CALL), then we
483 ;;; scan back for the ALLOCATE-FRAME VOP, and emit the save there.
484 ;;; This is necessary because in a self-recursive local call, the
485 ;;; registers holding the current arguments may get trashed by setting
486 ;;; up the call arguments. The ALLOCATE-FRAME VOP marks a place at
487 ;;; which the values are known to be good.
488 (defun save-complex-writer-tn (tn vop)
489   (let ((save (or (tn-save-tn tn)
490                   (pack-save-tn tn)))
491         (node (vop-node vop))
492         (block (vop-block vop))
493         (next (vop-next vop)))
494     (when (eq (tn-kind save) :specified-save)
495       (setf (tn-kind save) :save))
496     (aver (eq (tn-kind save) :save))
497     (emit-operand-load node block tn save
498                        (if (eq (vop-info-move-args (vop-info vop))
499                                :local-call)
500                            (reverse-find-vop 'allocate-frame vop)
501                            vop))
502     (emit-operand-load node block save tn next)))
503
504 ;;; Return a VOP after which is an OK place to save the value of TN.
505 ;;; For correctness, it is only required that this location be after
506 ;;; any possible write and before any possible restore location.
507 ;;;
508 ;;; In practice, we return the unique writer VOP, but give up if the
509 ;;; TN is ever read by a VOP with MOVE-ARGS :LOCAL-CALL. This prevents
510 ;;; us from being confused by non-tail local calls.
511 ;;;
512 ;;; When looking for writes, we have to ignore uses of MOVE-OPERAND,
513 ;;; since they will correspond to restores that we have already done.
514 (defun find-single-writer (tn)
515   (declare (type tn tn))
516   (do ((write (tn-writes tn) (tn-ref-next write))
517        (res nil))
518       ((null write)
519        (when (and res
520                   (do ((read (tn-reads tn) (tn-ref-next read)))
521                       ((not read) t)
522                     (when (eq (vop-info-move-args
523                                (vop-info
524                                 (tn-ref-vop read)))
525                               :local-call)
526                       (return nil))))
527          (tn-ref-vop res)))
528
529     (unless (eq (vop-info-name (vop-info (tn-ref-vop write)))
530                 'move-operand)
531       (when res (return nil))
532       (setq res write))))
533
534 ;;; Try to save TN at a single location. If we succeed, return T,
535 ;;; otherwise NIL.
536 (defun save-single-writer-tn (tn)
537   (declare (type tn tn))
538   (let* ((old-save (tn-save-tn tn))
539          (save (or old-save (pack-save-tn tn)))
540          (writer (find-single-writer tn)))
541     (when (and writer
542                (or (not old-save)
543                    (eq (tn-kind old-save) :specified-save)))
544       (emit-operand-load (vop-node writer) (vop-block writer)
545                          tn save (vop-next writer))
546       (setf (tn-kind save) :save-once)
547       t)))
548
549 ;;; Restore a TN with a :SAVE-ONCE save TN.
550 (defun restore-single-writer-tn (tn vop)
551   (declare (type tn) (type vop vop))
552   (let ((save (tn-save-tn tn)))
553     (aver (eq (tn-kind save) :save-once))
554     (emit-operand-load (vop-node vop) (vop-block vop) save tn (vop-next vop)))
555   (values))
556
557 ;;; Save a single TN that needs to be saved, choosing save-once if
558 ;;; appropriate. This is also called by SPILL-AND-PACK-LOAD-TN.
559 (defun basic-save-tn (tn vop)
560   (declare (type tn tn) (type vop vop))
561   (let ((save (tn-save-tn tn)))
562     (cond ((and save (eq (tn-kind save) :save-once))
563            (restore-single-writer-tn tn vop))
564           ((save-single-writer-tn tn)
565            (restore-single-writer-tn tn vop))
566           (t
567            (save-complex-writer-tn tn vop))))
568   (values))
569
570 ;;; Scan over the VOPs in BLOCK, emiting saving code for TNs noted in
571 ;;; the codegen info that are packed into saved SCs.
572 (defun emit-saves (block)
573   (declare (type ir2-block block))
574   (do ((vop (ir2-block-start-vop block) (vop-next vop)))
575       ((null vop))
576     (when (eq (vop-info-save-p (vop-info vop)) t)
577       (do-live-tns (tn (vop-save-set vop) block)
578         (when (and (sc-save-p (tn-sc tn))
579                    (not (eq (tn-kind tn) :component)))
580           (basic-save-tn tn vop)))))
581
582   (values))
583 \f
584 ;;;; optimized saving
585
586 ;;; Save TN if it isn't a single-writer TN that has already been
587 ;;; saved. If multi-write, we insert the save BEFORE the specified
588 ;;; VOP. CONTEXT is a VOP used to tell which node/block to use for the
589 ;;; new VOP.
590 (defun save-if-necessary (tn before context)
591   (declare (type tn tn) (type (or vop null) before) (type vop context))
592   (let ((save (tn-save-tn tn)))
593     (when (eq (tn-kind save) :specified-save)
594       (setf (tn-kind save) :save))
595     (aver (member (tn-kind save) '(:save :save-once)))
596     (unless (eq (tn-kind save) :save-once)
597       (or (save-single-writer-tn tn)
598           (emit-operand-load (vop-node context) (vop-block context)
599                              tn save before))))
600   (values))
601
602 ;;; Load the TN from its save location, allocating one if necessary.
603 ;;; The load is inserted BEFORE the specified VOP. CONTEXT is a VOP
604 ;;; used to tell which node/block to use for the new VOP.
605 (defun restore-tn (tn before context)
606   (declare (type tn tn) (type (or vop null) before) (type vop context))
607   (let ((save (or (tn-save-tn tn) (pack-save-tn tn))))
608     (emit-operand-load (vop-node context) (vop-block context)
609                        save tn before))
610   (values))
611
612 ;;; Start scanning backward at the end of BLOCK, looking which TNs are
613 ;;; live and looking for places where we have to save. We manipulate
614 ;;; two sets: SAVES and RESTORES.
615 ;;;
616 ;;; SAVES is a set of all the TNs that have to be saved because they
617 ;;; are restored after some call. We normally delay saving until the
618 ;;; beginning of the block, but we must save immediately if we see a
619 ;;; write of the saved TN. We also immediately save all TNs and exit
620 ;;; when we see a NOTE-ENVIRONMENT-START VOP, since saves can't be
621 ;;; done before the environment is properly initialized.
622 ;;;
623 ;;; RESTORES is a set of all the TNs read (and not written) between
624 ;;; here and the next call, i.e. the set of TNs that must be restored
625 ;;; when we reach the next (earlier) call VOP. Unlike SAVES, this set
626 ;;; is cleared when we do the restoring after a call. Any TNs that
627 ;;; were in RESTORES are moved into SAVES to ensure that they are
628 ;;; saved at some point.
629 ;;;
630 ;;; SAVES and RESTORES are represented using both a list and a
631 ;;; bit-vector so that we can quickly iterate and test for membership.
632 ;;; The incoming SAVES and RESTORES args are used for computing these
633 ;;; sets (the initial contents are ignored.)
634 ;;;
635 ;;; When we hit a VOP with :COMPUTE-ONLY SAVE-P (an internal error
636 ;;; location), we pretend that all live TNs were read, unless (= speed
637 ;;; 3), in which case we mark all the TNs that are live but not
638 ;;; restored as spilled.
639 (defun optimized-emit-saves-block (block saves restores)
640   (declare (type ir2-block block) (type simple-bit-vector saves restores))
641   (let ((1block (ir2-block-block block))
642         (saves-list ())
643         (restores-list ())
644         (skipping nil))
645     (declare (list saves-list restores-list))
646     (clear-bit-vector saves)
647     (clear-bit-vector restores)
648     (do-live-tns (tn (ir2-block-live-in block) block)
649       (when (and (sc-save-p (tn-sc tn))
650                  (not (eq (tn-kind tn) :component)))
651         (let ((num (tn-number tn)))
652           (setf (sbit restores num) 1)
653           (push tn restores-list))))
654
655     (do ((block block (ir2-block-prev block))
656          (prev nil block))
657         ((not (eq (ir2-block-block block) 1block))
658          (aver (not skipping))
659          (dolist (save saves-list)
660            (let ((start (ir2-block-start-vop prev)))
661              (save-if-necessary save start start)))
662          prev)
663       (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
664           ((null vop))
665         (let ((info (vop-info vop)))
666           (case (vop-info-name info)
667             (allocate-frame
668              (aver skipping)
669              (setq skipping nil))
670             (note-environment-start
671              (aver (not skipping))
672              (dolist (save saves-list)
673                (save-if-necessary save (vop-next vop) vop))
674              (return-from optimized-emit-saves-block block)))
675
676           (unless skipping
677             (do ((write (vop-results vop) (tn-ref-across write)))
678                 ((null write))
679               (let* ((tn (tn-ref-tn write))
680                      (num (tn-number tn)))
681                 (unless (zerop (sbit restores num))
682                   (setf (sbit restores num) 0)
683                   (setq restores-list
684                         (delete tn restores-list :test #'eq)))
685                 (unless (zerop (sbit saves num))
686                   (setf (sbit saves num) 0)
687                   (save-if-necessary tn (vop-next vop) vop)
688                   (setq saves-list
689                         (delete tn saves-list :test #'eq))))))
690
691           (macrolet ((save-note-read (tn)
692                        `(let* ((tn ,tn)
693                                (num (tn-number tn)))
694                           (when (and (sc-save-p (tn-sc tn))
695                                      (zerop (sbit restores num))
696                                      (not (eq (tn-kind tn) :component)))
697                           (setf (sbit restores num) 1)
698                           (push tn restores-list)))))
699
700             (case (vop-info-save-p info)
701               ((t)
702                (dolist (tn restores-list)
703                  (restore-tn tn (vop-next vop) vop)
704                  (let ((num (tn-number tn)))
705                    (when (zerop (sbit saves num))
706                      (push tn saves-list)
707                      (setf (sbit saves num) 1))))
708                (setq restores-list nil)
709                (clear-bit-vector restores))
710               (:compute-only
711                (cond ((policy (vop-node vop) (= speed 3))
712                       (do-live-tns (tn (vop-save-set vop) block)
713                         (when (zerop (sbit restores (tn-number tn)))
714                           (note-spilled-tn tn vop))))
715                      (t
716                       (do-live-tns (tn (vop-save-set vop) block)
717                         (save-note-read tn))))))
718
719             (if (eq (vop-info-move-args info) :local-call)
720                 (setq skipping t)
721                 (do ((read (vop-args vop) (tn-ref-across read)))
722                     ((null read))
723                   (save-note-read (tn-ref-tn read))))))))))
724
725 ;;; This is like EMIT-SAVES, only different. We avoid redundant saving
726 ;;; within the block, and don't restore values that aren't used before
727 ;;; the next call. This function is just the top level loop over the
728 ;;; blocks in the component, which locates blocks that need saving
729 ;;; done.
730 (defun optimized-emit-saves (component)
731   (declare (type component component))
732   (let* ((gtn-count (1+ (ir2-component-global-tn-counter
733                          (component-info component))))
734          (saves (make-array gtn-count :element-type 'bit))
735          (restores (make-array gtn-count :element-type 'bit))
736          (block (ir2-block-prev (block-info (component-tail component))))
737          (head (block-info (component-head component))))
738     (loop
739       (when (eq block head) (return))
740       (when (do ((vop (ir2-block-start-vop block) (vop-next vop)))
741                 ((null vop) nil)
742               (when (eq (vop-info-save-p (vop-info vop)) t)
743                 (return t)))
744         (setq block (optimized-emit-saves-block block saves restores)))
745       (setq block (ir2-block-prev block)))))
746
747 ;;; Iterate over the normal TNs, finding the cost of packing on the
748 ;;; stack in units of the number of references. We count all
749 ;;; references as +1, and subtract out REGISTER-SAVE-PENALTY for each
750 ;;; place where we would have to save a register.
751 (defun assign-tn-costs (component)
752   (do-ir2-blocks (block component)
753     (do ((vop (ir2-block-start-vop block) (vop-next vop)))
754         ((null vop))
755       (when (eq (vop-info-save-p (vop-info vop)) t)
756         (do-live-tns (tn (vop-save-set vop) block)
757           (decf (tn-cost tn) *backend-register-save-penalty*)))))
758
759   (do ((tn (ir2-component-normal-tns (component-info component))
760            (tn-next tn)))
761       ((null tn))
762     (let ((cost (tn-cost tn)))
763       (declare (fixnum cost))
764       (do ((ref (tn-reads tn) (tn-ref-next ref)))
765           ((null ref))
766         (incf cost))
767       (do ((ref (tn-writes tn) (tn-ref-next ref)))
768           ((null ref))
769         (incf cost))
770       (setf (tn-cost tn) cost))))
771
772 ;;; Iterate over the normal TNs, storing the depth of the deepest loop
773 ;;; that the TN is used in TN-LOOP-DEPTH.
774 (defun assign-tn-depths (component)
775   (when *loop-analyze*
776     (do-ir2-blocks (block component)
777       (do ((vop (ir2-block-start-vop block)
778                 (vop-next vop)))
779           ((null vop))
780         (flet ((find-all-tns (head-fun)
781                  (collect ((tns))
782                    (do ((ref (funcall head-fun vop) (tn-ref-across ref)))
783                        ((null ref))
784                      (tns (tn-ref-tn ref)))
785                    (tns))))
786           (dolist (tn (nconc (find-all-tns #'vop-args)
787                              (find-all-tns #'vop-results)
788                              (find-all-tns #'vop-temps)
789                              ;; What does "references in this VOP
790                              ;; mean"? Probably something that isn't
791                              ;; useful in this context, since these
792                              ;; TN-REFs are linked with TN-REF-NEXT
793                              ;; instead of TN-REF-ACROSS. --JES
794                              ;; 2004-09-11
795                              ;; (find-all-tns #'vop-refs)
796                              ))
797             (setf (tn-loop-depth tn)
798                   (max (tn-loop-depth tn)
799                        (let* ((ir1-block (ir2-block-block (vop-block vop)))
800                               (loop (block-loop ir1-block)))
801                          (if loop
802                              (loop-depth loop)
803                              0))))))))))
804
805 \f
806 ;;;; load TN packing
807
808 ;;; These variables indicate the last location at which we computed
809 ;;; the Live-TNs. They hold the BLOCK and VOP values that were passed
810 ;;; to COMPUTE-LIVE-TNS.
811 (defvar *live-block*)
812 (defvar *live-vop*)
813
814 ;;; If we unpack some TNs, then we mark all affected blocks by
815 ;;; sticking them in this hash-table. This is initially null. We
816 ;;; create the hashtable if we do any unpacking.
817 (defvar *repack-blocks*)
818 (declaim (type list *repack-blocks*))
819
820 ;;; Set the LIVE-TNS vectors in all :FINITE SBs to represent the TNs
821 ;;; live at the end of BLOCK.
822 (defun init-live-tns (block)
823   (dolist (sb *backend-sb-list*)
824     (when (eq (sb-kind sb) :finite)
825       (fill (finite-sb-live-tns sb) nil)))
826
827   (do-live-tns (tn (ir2-block-live-in block) block)
828     (let* ((sc (tn-sc tn))
829            (sb (sc-sb sc)))
830       (when (eq (sb-kind sb) :finite)
831         ;; KLUDGE: we can have "live" TNs that are neither read
832         ;; to nor written from, due to more aggressive (type-
833         ;; directed) constant propagation.  Such TNs will never
834         ;; be assigned an offset nor be in conflict with anything.
835         ;;
836         ;; Ideally, it seems to me we could make sure these TNs
837         ;; are never allocated in the first place in
838         ;; ASSIGN-LAMBDA-VAR-TNS.
839         (if (tn-offset tn)
840             (do ((offset (tn-offset tn) (1+ offset))
841                  (end (+ (tn-offset tn) (sc-element-size sc))))
842                 ((= offset end))
843               (declare (type index offset end))
844               (setf (svref (finite-sb-live-tns sb) offset) tn))
845             (assert (and (null (tn-reads tn))
846                          (null (tn-writes tn))))))))
847
848   (setq *live-block* block)
849   (setq *live-vop* (ir2-block-last-vop block))
850
851   (values))
852
853 ;;; Set the LIVE-TNs in :FINITE SBs to represent the TNs live
854 ;;; immediately after the evaluation of VOP in BLOCK, excluding
855 ;;; results of the VOP. If VOP is null, then compute the live TNs at
856 ;;; the beginning of the block. Sequential calls on the same block
857 ;;; must be in reverse VOP order.
858 (defun compute-live-tns (block vop)
859   (declare (type ir2-block block) (type vop vop))
860   (unless (eq block *live-block*)
861     (init-live-tns block))
862
863   (do ((current *live-vop* (vop-prev current)))
864       ((eq current vop)
865        (do ((res (vop-results vop) (tn-ref-across res)))
866            ((null res))
867          (let* ((tn (tn-ref-tn res))
868                 (sc (tn-sc tn))
869                 (sb (sc-sb sc)))
870            (when (eq (sb-kind sb) :finite)
871              (do ((offset (tn-offset tn) (1+ offset))
872                   (end (+ (tn-offset tn) (sc-element-size sc))))
873                  ((= offset end))
874                (declare (type index offset end))
875                (setf (svref (finite-sb-live-tns sb) offset) nil))))))
876     (do ((ref (vop-refs current) (tn-ref-next-ref ref)))
877         ((null ref))
878       (let ((ltn (tn-ref-load-tn ref)))
879         (when ltn
880           (let* ((sc (tn-sc ltn))
881                  (sb (sc-sb sc)))
882             (when (eq (sb-kind sb) :finite)
883               (let ((tns (finite-sb-live-tns sb)))
884                 (do ((offset (tn-offset ltn) (1+ offset))
885                      (end (+ (tn-offset ltn) (sc-element-size sc))))
886                     ((= offset end))
887                   (declare (type index offset end))
888                   (aver (null (svref tns offset)))))))))
889
890       (let* ((tn (tn-ref-tn ref))
891              (sc (tn-sc tn))
892              (sb (sc-sb sc)))
893         (when (eq (sb-kind sb) :finite)
894           (let ((tns (finite-sb-live-tns sb)))
895             (do ((offset (tn-offset tn) (1+ offset))
896                  (end (+ (tn-offset tn) (sc-element-size sc))))
897                 ((= offset end))
898               (declare (type index offset end))
899               (if (tn-ref-write-p ref)
900                   (setf (svref tns offset) nil)
901                   (let ((old (svref tns offset)))
902                     (aver (or (null old) (eq old tn)))
903                     (setf (svref tns offset) tn)))))))))
904
905   (setq *live-vop* vop)
906   (values))
907
908 ;;; This is kind of like OFFSET-CONFLICTS-IN-SB, except that it uses
909 ;;; the VOP refs to determine whether a Load-TN for OP could be packed
910 ;;; in the specified location, disregarding conflicts with TNs not
911 ;;; referenced by this VOP. There is a conflict if either:
912 ;;;  1. The reference is a result, and the same location is either:
913 ;;;     -- Used by some other result.
914 ;;;     -- Used in any way after the reference (exclusive).
915 ;;;  2. The reference is an argument, and the same location is either:
916 ;;;     -- Used by some other argument.
917 ;;;     -- Used in any way before the reference (exclusive).
918 ;;;
919 ;;; In 1 (and 2) above, the first bullet corresponds to result-result
920 ;;; (and argument-argument) conflicts. We need this case because there
921 ;;; aren't any TN-REFs to represent the implicit reading of results or
922 ;;; writing of arguments.
923 ;;;
924 ;;; The second bullet corresponds to conflicts with temporaries or
925 ;;; between arguments and results.
926 ;;;
927 ;;; We consider both the TN-REF-TN and the TN-REF-LOAD-TN (if any) to
928 ;;; be referenced simultaneously and in the same way. This causes
929 ;;; load-TNs to appear live to the beginning (or end) of the VOP, as
930 ;;; appropriate.
931 ;;;
932 ;;; We return a conflicting TN if there is a conflict.
933 (defun load-tn-offset-conflicts-in-sb (op sb offset)
934   (declare (type tn-ref op) (type finite-sb sb) (type index offset))
935   (aver (eq (sb-kind sb) :finite))
936   (let ((vop (tn-ref-vop op)))
937     (labels ((tn-overlaps (tn)
938                (let ((sc (tn-sc tn))
939                      (tn-offset (tn-offset tn)))
940                  (when (and (eq (sc-sb sc) sb)
941                             (<= tn-offset offset)
942                             (< offset
943                                (the index
944                                     (+ tn-offset (sc-element-size sc)))))
945                    tn)))
946              (same (ref)
947                (let ((tn (tn-ref-tn ref))
948                      (ltn (tn-ref-load-tn ref)))
949                  (or (tn-overlaps tn)
950                      (and ltn (tn-overlaps ltn)))))
951              (is-op (ops)
952                (do ((ops ops (tn-ref-across ops)))
953                    ((null ops) nil)
954                  (let ((found (same ops)))
955                    (when (and found (not (eq ops op)))
956                      (return found)))))
957              (is-ref (refs end)
958                (do ((refs refs (tn-ref-next-ref refs)))
959                    ((eq refs end) nil)
960                  (let ((found (same refs)))
961                  (when found (return found))))))
962       (declare (inline is-op is-ref tn-overlaps))
963       (if (tn-ref-write-p op)
964           (or (is-op (vop-results vop))
965               (is-ref (vop-refs vop) op))
966           (or (is-op (vop-args vop))
967               (is-ref (tn-ref-next-ref op) nil))))))
968
969 ;;; Iterate over all the elements in the SB that would be allocated by
970 ;;; allocating a TN in SC at Offset, checking for conflict with
971 ;;; load-TNs or other TNs (live in the LIVE-TNS, which must be set
972 ;;; up.) We also return true if there aren't enough locations after
973 ;;; Offset to hold a TN in SC. If Ignore-Live is true, then we ignore
974 ;;; the live-TNs, considering only references within Op's VOP.
975 ;;;
976 ;;; We return a conflicting TN, or :OVERFLOW if the TN won't fit.
977 (defun load-tn-conflicts-in-sc (op sc offset ignore-live)
978   (let* ((sb (sc-sb sc))
979          (size (finite-sb-current-size sb)))
980     (do ((i offset (1+ i))
981          (end (+ offset (sc-element-size sc))))
982         ((= i end) nil)
983       (declare (type index i end))
984       (let ((res (or (when (>= i size) :overflow)
985                      (and (not ignore-live)
986                           (svref (finite-sb-live-tns sb) i))
987                      (load-tn-offset-conflicts-in-sb op sb i))))
988         (when res (return res))))))
989
990 ;;; If a load-TN for OP is targeted to a legal location in SC, then
991 ;;; return the offset, otherwise return NIL. We see whether the target
992 ;;; of the operand is packed, and try that location. There isn't any
993 ;;; need to chain down the target path, since everything is packed
994 ;;; now.
995 ;;;
996 ;;; We require the target to be in SC (and not merely to overlap with
997 ;;; SC). This prevents SC information from being lost in load TNs (we
998 ;;; won't pack a load TN in ANY-REG when it is targeted to a
999 ;;; DESCRIPTOR-REG.) This shouldn't hurt the code as long as all
1000 ;;; relevant overlapping SCs are allowed in the operand SC
1001 ;;; restriction.
1002 (defun find-load-tn-target (op sc)
1003   (declare (inline member))
1004   (let ((target (tn-ref-target op)))
1005     (when target
1006       (let* ((tn (tn-ref-tn target))
1007              (loc (tn-offset tn)))
1008         (if (and (eq (tn-sc tn) sc)
1009                  (member (the index loc) (sc-locations sc))
1010                  (not (load-tn-conflicts-in-sc op sc loc nil)))
1011             loc
1012             nil)))))
1013
1014 ;;; Select a legal location for a load TN for Op in SC. We just
1015 ;;; iterate over the SC's locations. If we can't find a legal
1016 ;;; location, return NIL.
1017 (defun select-load-tn-location (op sc)
1018   (declare (type tn-ref op) (type sc sc))
1019
1020   ;; Check any target location first.
1021   (let ((target (tn-ref-target op)))
1022     (when target
1023       (let* ((tn (tn-ref-tn target))
1024              (loc (tn-offset tn)))
1025         (when (and (eq (sc-sb sc) (sc-sb (tn-sc tn)))
1026                    (member (the index loc) (sc-locations sc))
1027                    (not (load-tn-conflicts-in-sc op sc loc nil)))
1028               (return-from select-load-tn-location loc)))))
1029
1030   (dolist (loc (sc-locations sc) nil)
1031     (unless (load-tn-conflicts-in-sc op sc loc nil)
1032       (return loc))))
1033
1034 (defevent unpack-tn "Unpacked a TN to satisfy operand SC restriction.")
1035
1036 ;;; Make TN's location the same as for its save TN (allocating a save
1037 ;;; TN if necessary.) Delete any save/restore code that has been
1038 ;;; emitted thus far. Mark all blocks containing references as needing
1039 ;;; to be repacked.
1040 (defun unpack-tn (tn)
1041   (event unpack-tn)
1042   (let ((stn (or (tn-save-tn tn)
1043                  (pack-save-tn tn))))
1044     (setf (tn-sc tn) (tn-sc stn))
1045     (setf (tn-offset tn) (tn-offset stn))
1046     (flet ((zot (refs)
1047              (do ((ref refs (tn-ref-next ref)))
1048                  ((null ref))
1049                (let ((vop (tn-ref-vop ref)))
1050                  (if (eq (vop-info-name (vop-info vop)) 'move-operand)
1051                      (delete-vop vop)
1052                      (pushnew (vop-block vop) *repack-blocks*))))))
1053       (zot (tn-reads tn))
1054       (zot (tn-writes tn))))
1055
1056   (values))
1057
1058 (defevent unpack-fallback "Unpacked some operand TN.")
1059
1060 ;;; This is called by PACK-LOAD-TN where there isn't any location free
1061 ;;; that we can pack into. What we do is move some live TN in one of
1062 ;;; the specified SCs to memory, then mark all blocks that reference
1063 ;;; the TN as needing repacking. If we succeed, we throw to UNPACKED-TN.
1064 ;;; If we fail, we return NIL.
1065 ;;;
1066 ;;; We can unpack any live TN that appears in the NORMAL-TNs list
1067 ;;; (isn't wired or restricted.) We prefer to unpack TNs that are not
1068 ;;; used by the VOP. If we can't find any such TN, then we unpack some
1069 ;;; argument or result TN. The only way we can fail is if all
1070 ;;; locations in SC are used by load-TNs or temporaries in VOP.
1071 (defun unpack-for-load-tn (sc op)
1072   (declare (type sc sc) (type tn-ref op))
1073   (let ((sb (sc-sb sc))
1074         (normal-tns (ir2-component-normal-tns
1075                      (component-info *component-being-compiled*)))
1076         (node (vop-node (tn-ref-vop op)))
1077         (fallback nil))
1078     (flet ((unpack-em (victims)
1079              (pushnew (vop-block (tn-ref-vop op)) *repack-blocks*)
1080              (dolist (victim victims)
1081                (event unpack-tn node)
1082                (unpack-tn victim))
1083              (throw 'unpacked-tn nil)))
1084       (dolist (loc (sc-locations sc))
1085         (declare (type index loc))
1086         (block SKIP
1087           (collect ((victims nil adjoin))
1088             (do ((i loc (1+ i))
1089                  (end (+ loc (sc-element-size sc))))
1090                 ((= i end))
1091               (declare (type index i end))
1092               (let ((victim (svref (finite-sb-live-tns sb) i)))
1093                 (when victim
1094                   (unless (find-in #'tn-next victim normal-tns)
1095                     (return-from SKIP))
1096                   (victims victim))))
1097
1098             (let ((conf (load-tn-conflicts-in-sc op sc loc t)))
1099               (cond ((not conf)
1100                      (unpack-em (victims)))
1101                     ((eq conf :overflow))
1102                     ((not fallback)
1103                      (cond ((find conf (victims))
1104                             (setq fallback (victims)))
1105                            ((find-in #'tn-next conf normal-tns)
1106                             (setq fallback (list conf))))))))))
1107
1108       (when fallback
1109         (event unpack-fallback node)
1110         (unpack-em fallback))))
1111
1112   nil)
1113
1114 ;;; Try to pack a load TN in the SCs indicated by Load-SCs. If we run
1115 ;;; out of SCs, then we unpack some TN and try again. We return the
1116 ;;; packed load TN.
1117 ;;;
1118 ;;; Note: we allow a Load-TN to be packed in the target location even
1119 ;;; if that location is in a SC not allowed by the primitive type.
1120 ;;; (The SC must still be allowed by the operand restriction.) This
1121 ;;; makes move VOPs more efficient, since we won't do a move from the
1122 ;;; stack into a non-descriptor any-reg through a descriptor argument
1123 ;;; load-TN. This does give targeting some real semantics, making it
1124 ;;; not a pure advisory to pack. It allows pack to do some packing it
1125 ;;; wouldn't have done before.
1126 (defun pack-load-tn (load-scs op)
1127   (declare (type sc-vector load-scs) (type tn-ref op))
1128   (let ((vop (tn-ref-vop op)))
1129     (compute-live-tns (vop-block vop) vop))
1130
1131   (let* ((tn (tn-ref-tn op))
1132          (ptype (tn-primitive-type tn))
1133          (scs (svref load-scs (sc-number (tn-sc tn)))))
1134     (let ((current-scs scs)
1135           (allowed ()))
1136       (loop
1137         (cond
1138          ((null current-scs)
1139           (unless allowed
1140             (no-load-scs-allowed-by-primitive-type-error op))
1141           (dolist (sc allowed)
1142             (unpack-for-load-tn sc op))
1143           (failed-to-pack-load-tn-error allowed op))
1144         (t
1145          (let* ((sc (svref *backend-sc-numbers* (pop current-scs)))
1146                 (target (find-load-tn-target op sc)))
1147            (when (or target (sc-allowed-by-primitive-type sc ptype))
1148              (let ((loc (or target
1149                             (select-load-tn-location op sc))))
1150                (when loc
1151                  (let ((res (make-tn 0 :load nil sc)))
1152                    (setf (tn-offset res) loc)
1153                    (return res))))
1154              (push sc allowed)))))))))
1155
1156 ;;; Scan a list of load-SCs vectors and a list of TN-REFS threaded by
1157 ;;; TN-REF-ACROSS. When we find a reference whose TN doesn't satisfy
1158 ;;; the restriction, we pack a Load-TN and load the operand into it.
1159 ;;; If a load-tn has already been allocated, we can assume that the
1160 ;;; restriction is satisfied.
1161 #!-sb-fluid (declaim (inline check-operand-restrictions))
1162 (defun check-operand-restrictions (scs ops)
1163   (declare (list scs) (type (or tn-ref null) ops))
1164
1165   ;; Check the targeted operands first.
1166   (do ((scs scs (cdr scs))
1167        (op ops (tn-ref-across op)))
1168       ((null scs))
1169       (let ((target (tn-ref-target op)))
1170         (when target
1171            (let* ((load-tn (tn-ref-load-tn op))
1172                   (load-scs (svref (car scs)
1173                                    (sc-number
1174                                     (tn-sc (or load-tn (tn-ref-tn op)))))))
1175              (if load-tn
1176                  (aver (eq load-scs t))
1177                (unless (eq load-scs t)
1178                        (setf (tn-ref-load-tn op)
1179                              (pack-load-tn (car scs) op))))))))
1180
1181   (do ((scs scs (cdr scs))
1182        (op ops (tn-ref-across op)))
1183       ((null scs))
1184       (let ((target (tn-ref-target op)))
1185         (unless target
1186            (let* ((load-tn (tn-ref-load-tn op))
1187                   (load-scs (svref (car scs)
1188                                    (sc-number
1189                                     (tn-sc (or load-tn (tn-ref-tn op)))))))
1190              (if load-tn
1191                  (aver (eq load-scs t))
1192                (unless (eq load-scs t)
1193                        (setf (tn-ref-load-tn op)
1194                              (pack-load-tn (car scs) op))))))))
1195
1196   (values))
1197
1198 ;;; Scan the VOPs in BLOCK, looking for operands whose SC restrictions
1199 ;;; aren't satisfied. We do the results first, since they are
1200 ;;; evaluated later, and our conflict analysis is a backward scan.
1201 (defun pack-load-tns (block)
1202   (catch 'unpacked-tn
1203     (let ((*live-block* nil)
1204           (*live-vop* nil))
1205       (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
1206           ((null vop))
1207         (let ((info (vop-info vop)))
1208           (check-operand-restrictions (vop-info-result-load-scs info)
1209                                       (vop-results vop))
1210           (check-operand-restrictions (vop-info-arg-load-scs info)
1211                                       (vop-args vop))))))
1212   (values))
1213 \f
1214 ;;;; targeting
1215
1216 ;;; Link the TN-REFS READ and WRITE together using the TN-REF-TARGET
1217 ;;; when this seems like a good idea. Currently we always do, as this
1218 ;;; increases the success of load-TN targeting.
1219 (defun target-if-desirable (read write)
1220   (declare (type tn-ref read write))
1221   ;; As per the comments at the definition of TN-REF-TARGET, read and
1222   ;; write refs are always paired, with TARGET in the read pointing to
1223   ;; the write and vice versa.
1224   (aver (eq (tn-ref-write-p read)
1225             (not (tn-ref-write-p write))))
1226   (setf (tn-ref-target read) write)
1227   (setf (tn-ref-target write) read))
1228
1229 ;;; If TN can be packed into SC so as to honor a preference to TARGET,
1230 ;;; then return the offset to pack at, otherwise return NIL. TARGET
1231 ;;; must be already packed.
1232 (defun check-ok-target (target tn sc)
1233   (declare (type tn target tn) (type sc sc) (inline member))
1234   (let* ((loc (tn-offset target))
1235          (target-sc (tn-sc target))
1236          (target-sb (sc-sb target-sc)))
1237     (declare (type index loc))
1238     ;; We can honor a preference if:
1239     ;; -- TARGET's location is in SC's locations.
1240     ;; -- The element sizes of the two SCs are the same.
1241     ;; -- TN doesn't conflict with target's location.
1242     (if (and (eq target-sb (sc-sb sc))
1243              (or (eq (sb-kind target-sb) :unbounded)
1244                  (member loc (sc-locations sc)))
1245              (= (sc-element-size target-sc) (sc-element-size sc))
1246              (not (conflicts-in-sc tn sc loc))
1247              (zerop (mod loc (sc-alignment sc))))
1248         loc
1249         nil)))
1250
1251 ;;; Scan along the target path from TN, looking at readers or writers.
1252 ;;; When we find a packed TN, return CHECK-OK-TARGET of that TN. If
1253 ;;; there is no target, or if the TN has multiple readers (writers),
1254 ;;; then we return NIL. We also always return NIL after 10 iterations
1255 ;;; to get around potential circularity problems.
1256 ;;;
1257 ;;; FIXME: (30 minutes of reverse engineering?) It'd be nice to
1258 ;;; rewrite the header comment here to explain the interface and its
1259 ;;; motivation, and move remarks about implementation details (like
1260 ;;; 10!) inside.
1261 (defun find-ok-target-offset (tn sc)
1262   (declare (type tn tn) (type sc sc))
1263   (flet ((frob-slot (slot-fun)
1264            (declare (type function slot-fun))
1265            (let ((count 10)
1266                  (current tn))
1267              (declare (type index count))
1268              (loop
1269               (let ((refs (funcall slot-fun current)))
1270                 (unless (and (plusp count)
1271                              refs
1272                              (not (tn-ref-next refs)))
1273                   (return nil))
1274                 (let ((target (tn-ref-target refs)))
1275                   (unless target (return nil))
1276                   (setq current (tn-ref-tn target))
1277                   (when (tn-offset current)
1278                     (return (check-ok-target current tn sc)))
1279                   (decf count)))))))
1280     (declare (inline frob-slot)) ; until DYNAMIC-EXTENT works
1281     (or (frob-slot #'tn-reads)
1282         (frob-slot #'tn-writes))))
1283 \f
1284 ;;;; location selection
1285
1286 ;;; Select some location for TN in SC, returning the offset if we
1287 ;;; succeed, and NIL if we fail.
1288 ;;;
1289 ;;; For :UNBOUNDED SCs just find the smallest correctly aligned offset
1290 ;;; where the TN doesn't conflict with the TNs that have already been
1291 ;;; packed. For :FINITE SCs try to pack the TN into the most heavily
1292 ;;; used locations first (as estimated in FIND-LOCATION-USAGE).
1293 ;;;
1294 ;;; Historically SELECT-LOCATION tried did the opposite and tried to
1295 ;;; distribute the TNs evenly across the available locations. At least
1296 ;;; on register-starved architectures (x86) this seems to be a bad
1297 ;;; strategy. -- JES 2004-09-11
1298 (defun select-location (tn sc &key use-reserved-locs optimize)
1299   (declare (type tn tn) (type sc sc) (inline member))
1300   (let* ((sb (sc-sb sc))
1301          (element-size (sc-element-size sc))
1302          (alignment (sc-alignment sc))
1303          (align-mask (1- alignment))
1304          (size (finite-sb-current-size sb)))
1305     (flet ((attempt-location (start-offset)
1306              (dotimes (i element-size
1307                        (return-from select-location start-offset))
1308                (declare (type index i))
1309                (let ((offset (+ start-offset i)))
1310                  (when (offset-conflicts-in-sb tn sb offset)
1311                    (return (logandc2 (the index (+ (the index (1+ offset))
1312                                                    align-mask))
1313                                      align-mask)))))))
1314       (if (eq (sb-kind sb) :unbounded)
1315           (loop with offset = 0
1316                 until (> (+ offset element-size) size) do
1317                 (setf offset (attempt-location offset)))
1318           (let ((locations (sc-locations sc)))
1319             (when optimize
1320               (setf locations
1321                     (stable-sort (copy-list locations) #'>
1322                                  :key (lambda (location-offset)
1323                                         (loop for offset from location-offset
1324                                               repeat element-size
1325                                               maximize (svref
1326                                                         (finite-sb-always-live-count sb)
1327                                                         offset))))))
1328             (dolist (offset locations)
1329               (when (or use-reserved-locs
1330                         (not (member offset
1331                                      (sc-reserve-locations sc))))
1332                 (attempt-location offset))))))))
1333
1334 ;;; If a save TN, return the saved TN, otherwise return TN. This is
1335 ;;; useful for getting the conflicts of a TN that might be a save TN.
1336 (defun original-tn (tn)
1337   (declare (type tn tn))
1338   (if (member (tn-kind tn) '(:save :save-once :specified-save))
1339       (tn-save-tn tn)
1340       tn))
1341 \f
1342 ;;;; pack interface
1343
1344 ;;; Attempt to pack TN in all possible SCs, first in the SC chosen by
1345 ;;; representation selection, then in the alternate SCs in the order
1346 ;;; they were specified in the SC definition. If the TN-COST is
1347 ;;; negative, then we don't attempt to pack in SCs that must be saved.
1348 ;;; If Restricted, then we can only pack in TN-SC, not in any
1349 ;;; Alternate-SCs.
1350 ;;;
1351 ;;; If we are attempting to pack in the SC of the save TN for a TN
1352 ;;; with a :SPECIFIED-SAVE TN, then we pack in that location, instead
1353 ;;; of allocating a new stack location.
1354 (defun pack-tn (tn restricted optimize &key (allow-unbounded-sc t))
1355   (declare (type tn tn))
1356   (let* ((original (original-tn tn))
1357          (fsc (tn-sc tn))
1358          (alternates (unless restricted (sc-alternate-scs fsc)))
1359          (save (tn-save-tn tn))
1360          (specified-save-sc
1361           (when (and save
1362                      (eq (tn-kind save) :specified-save))
1363             (tn-sc save))))
1364     (do ((sc fsc (pop alternates)))
1365         ((null sc)
1366          (failed-to-pack-error tn restricted))
1367       (unless (or allow-unbounded-sc
1368                   (neq (sb-kind (sc-sb sc)) :unbounded))
1369         (return nil))
1370       (when (eq sc specified-save-sc)
1371         (unless (tn-offset save)
1372           (pack-tn save nil optimize))
1373         (setf (tn-offset tn) (tn-offset save))
1374         (setf (tn-sc tn) (tn-sc save))
1375         (return t))
1376       (when (or restricted
1377                 (not (and (minusp (tn-cost tn)) (sc-save-p sc))))
1378         (let ((loc (or (find-ok-target-offset original sc)
1379                        (select-location original sc)
1380                        (and restricted
1381                             (select-location original sc :use-reserved-locs t))
1382                        (when (eq (sb-kind (sc-sb sc)) :unbounded)
1383                          (grow-sc sc)
1384                          (or (select-location original sc)
1385                              (error "failed to pack after growing SC?"))))))
1386           (when loc
1387             (add-location-conflicts original sc loc optimize)
1388             (setf (tn-sc tn) sc)
1389             (setf (tn-offset tn) loc)
1390             (return t))))))
1391   (values))
1392
1393 ;;; Pack a wired TN, checking that the offset is in bounds for the SB,
1394 ;;; and that the TN doesn't conflict with some other TN already packed
1395 ;;; in that location. If the TN is wired to a location beyond the end
1396 ;;; of a :UNBOUNDED SB, then grow the SB enough to hold the TN.
1397 ;;;
1398 ;;; ### Checking for conflicts is disabled for :SPECIFIED-SAVE TNs.
1399 ;;; This is kind of a hack to make specifying wired stack save
1400 ;;; locations for local call arguments (such as OLD-FP) work, since
1401 ;;; the caller and callee OLD-FP save locations may conflict when the
1402 ;;; save locations don't really (due to being in different frames.)
1403 (defun pack-wired-tn (tn optimize)
1404   (declare (type tn tn))
1405   (let* ((sc (tn-sc tn))
1406          (sb (sc-sb sc))
1407          (offset (tn-offset tn))
1408          (end (+ offset (sc-element-size sc)))
1409          (original (original-tn tn)))
1410     (when (> end (finite-sb-current-size sb))
1411       (unless (eq (sb-kind sb) :unbounded)
1412         (error "~S is wired to a location that is out of bounds." tn))
1413       (grow-sc sc end))
1414
1415     ;; For non-x86 ports the presence of a save-tn associated with a
1416     ;; tn is used to identify the old-fp and return-pc tns. It depends
1417     ;; on the old-fp and return-pc being passed in registers.
1418     #!-(or x86 x86-64)
1419     (when (and (not (eq (tn-kind tn) :specified-save))
1420                (conflicts-in-sc original sc offset))
1421       (error "~S is wired to a location that it conflicts with." tn))
1422
1423     ;; Use the above check, but only print a verbose warning. This can
1424     ;; be helpful for debugging the x86 port.
1425     #+nil
1426     (when (and (not (eq (tn-kind tn) :specified-save))
1427                (conflicts-in-sc original sc offset))
1428           (format t "~&* Pack-wired-tn possible conflict:~%  ~
1429                      tn: ~S; tn-kind: ~S~%  ~
1430                      sc: ~S~%  ~
1431                      sb: ~S; sb-name: ~S; sb-kind: ~S~%  ~
1432                      offset: ~S; end: ~S~%  ~
1433                      original ~S~%  ~
1434                      tn-save-tn: ~S; tn-kind of tn-save-tn: ~S~%"
1435                   tn (tn-kind tn) sc
1436                   sb (sb-name sb) (sb-kind sb)
1437                   offset end
1438                   original
1439                   (tn-save-tn tn) (tn-kind (tn-save-tn tn))))
1440
1441     ;; On the x86 ports the old-fp and return-pc are often passed on
1442     ;; the stack so the above hack for the other ports does not always
1443     ;; work. Here the old-fp and return-pc tns are identified by being
1444     ;; on the stack in their standard save locations.
1445     #!+(or x86 x86-64)
1446     (when (and (not (eq (tn-kind tn) :specified-save))
1447                (not (and (string= (sb-name sb) "STACK")
1448                          (or (= offset 0)
1449                              (= offset 1))))
1450                (conflicts-in-sc original sc offset))
1451       (error "~S is wired to a location that it conflicts with." tn))
1452
1453     (add-location-conflicts original sc offset optimize)))
1454
1455 (defevent repack-block "Repacked a block due to TN unpacking.")
1456
1457 ;;; KLUDGE: Prior to SBCL version 0.8.9.xx, this function was known as
1458 ;;; PACK-BEFORE-GC-HOOK, but was non-functional since approximately
1459 ;;; version 0.8.3.xx since the removal of GC hooks from the system.
1460 ;;; This currently (as of 2004-04-12) runs now after every call to
1461 ;;; PACK, rather than -- as was originally intended -- once per GC
1462 ;;; cycle; this is probably non-optimal, and might require tuning,
1463 ;;; maybe to be called when the data structures exceed a certain size,
1464 ;;; or maybe once every N times.  The KLUDGE is that this rewrite has
1465 ;;; done nothing to improve the reentrance or threadsafety of the
1466 ;;; compiler; it still fails to be callable from several threads at
1467 ;;; the same time.
1468 ;;;
1469 ;;; Brief experiments indicate that during a compilation cycle this
1470 ;;; causes about 10% more consing, and takes about 1%-2% more time.
1471 ;;;
1472 ;;; -- CSR, 2004-04-12
1473 (defun clean-up-pack-structures ()
1474   (dolist (sb *backend-sb-list*)
1475     (unless (eq (sb-kind sb) :non-packed)
1476       (let ((size (sb-size sb)))
1477         (fill (finite-sb-always-live sb) nil)
1478         (setf (finite-sb-always-live sb)
1479               (make-array size
1480                           :initial-element
1481                           #-sb-xc #*
1482                           ;; The cross-compiler isn't very good at
1483                           ;; dumping specialized arrays, so we delay
1484                           ;; construction of this SIMPLE-BIT-VECTOR
1485                           ;; until runtime.
1486                           #+sb-xc (make-array 0 :element-type 'bit)))
1487         (setf (finite-sb-always-live-count sb)
1488               (make-array size
1489                           :initial-element
1490                           #-sb-xc #*
1491                           ;; Ibid
1492                           #+sb-xc (make-array 0 :element-type 'fixnum)))
1493
1494         (fill (finite-sb-conflicts sb) nil)
1495         (setf (finite-sb-conflicts sb)
1496               (make-array size :initial-element '#()))
1497
1498         (fill (finite-sb-live-tns sb) nil)
1499         (setf (finite-sb-live-tns sb)
1500               (make-array size :initial-element nil))))))
1501
1502 (defun tn-lexical-depth (tn)
1503   (let ((path t)) ; dummy initial value
1504     (labels ((path (lambda)
1505                (nreverse (loop while lambda
1506                                collect lambda
1507                                do (setf lambda (lambda-parent lambda)))))
1508              (register-scope (lambda)
1509                (let ((new-path (path lambda)))
1510                  (setf path (if (eql path t)
1511                                 new-path
1512                                 (subseq path
1513                                         0 (mismatch path new-path))))))
1514              (walk-tn-refs (ref)
1515                (do ((ref ref (tn-ref-next ref)))
1516                    ((null ref))
1517                  (binding* ((node (vop-node (tn-ref-vop ref))
1518                                   :exit-if-null))
1519                    (register-scope (lexenv-lambda
1520                                     (node-lexenv node)))))))
1521       (walk-tn-refs (tn-reads tn))
1522       (walk-tn-refs (tn-writes tn))
1523       (if (eql path t)
1524           most-positive-fixnum
1525           (length path)))))
1526
1527 (defun pack (component)
1528   (unwind-protect
1529        (let ((optimize nil)
1530              (2comp (component-info component)))
1531          (init-sb-vectors component)
1532
1533          ;; Determine whether we want to do more expensive packing by
1534          ;; checking whether any blocks in the component have (> SPEED
1535          ;; COMPILE-SPEED).
1536          ;;
1537          ;; FIXME: This means that a declaration can have a minor
1538          ;; effect even outside its scope, and as the packing is done
1539          ;; component-globally it'd be tricky to use strict scoping. I
1540          ;; think this is still acceptable since it's just a tradeoff
1541          ;; between compilation speed and allocation quality and
1542          ;; doesn't affect the semantics of the generated code in any
1543          ;; way. -- JES 2004-10-06
1544          (do-ir2-blocks (block component)
1545            (when (policy (block-last (ir2-block-block block))
1546                          (> speed compilation-speed))
1547              (setf optimize t)
1548              (return)))
1549
1550          ;; Call the target functions.
1551          (do-ir2-blocks (block component)
1552            (do ((vop (ir2-block-start-vop block) (vop-next vop)))
1553                ((null vop))
1554              (let ((target-fun (vop-info-target-fun (vop-info vop))))
1555                (when target-fun
1556                  (funcall target-fun vop)))))
1557
1558          ;; Pack wired TNs first.
1559          (do ((tn (ir2-component-wired-tns 2comp) (tn-next tn)))
1560              ((null tn))
1561            (pack-wired-tn tn optimize))
1562
1563          ;; Pack restricted component TNs.
1564          (do ((tn (ir2-component-restricted-tns 2comp) (tn-next tn)))
1565              ((null tn))
1566            (when (eq (tn-kind tn) :component)
1567              (pack-tn tn t optimize)))
1568
1569          ;; Pack other restricted TNs.
1570          (do ((tn (ir2-component-restricted-tns 2comp) (tn-next tn)))
1571              ((null tn))
1572            (unless (tn-offset tn)
1573              (pack-tn tn t optimize)))
1574
1575          ;; Assign costs to normal TNs so we know which ones should
1576          ;; always be packed on the stack.
1577          (when *pack-assign-costs*
1578            (assign-tn-costs component)
1579            (assign-tn-depths component))
1580
1581          ;; Allocate normal TNs, starting with the TNs that are used
1582          ;; in deep loops.  Only allocate in finite SCs (i.e. not on
1583          ;; the stack).
1584          (collect ((tns))
1585            (do-ir2-blocks (block component)
1586              (let ((ltns (ir2-block-local-tns block)))
1587                (do ((i (1- (ir2-block-local-tn-count block)) (1- i)))
1588                    ((minusp i))
1589                  (declare (fixnum i))
1590                  (let ((tn (svref ltns i)))
1591                    (unless (or (null tn)
1592                                (eq tn :more)
1593                                (tn-offset tn))
1594                      ;; If loop analysis has been disabled we might as
1595                      ;; well revert to the old behaviour of just
1596                      ;; packing TNs linearly as they appear.
1597                      (unless *loop-analyze*
1598                        (pack-tn tn nil optimize :allow-unbounded-sc nil))
1599                      (tns tn))))))
1600            (dolist (tn (stable-sort (tns)
1601                                     (lambda (a b)
1602                                       (cond
1603                                         ((> (tn-loop-depth a)
1604                                             (tn-loop-depth b))
1605                                          t)
1606                                         ((= (tn-loop-depth a)
1607                                             (tn-loop-depth b))
1608                                          (> (tn-cost a) (tn-cost b)))
1609                                         (t nil)))))
1610              (unless (tn-offset tn)
1611                (pack-tn tn nil optimize :allow-unbounded-sc nil))))
1612
1613          ;; Pack any leftover normal TNs that could not be allocated
1614          ;; to finite SCs, or TNs that do not appear in any local TN
1615          ;; map (e.g. :MORE TNs).  Since we'll likely be allocating
1616          ;; on the stack, first allocate TNs that are associated with
1617          ;; code at shallow lexical depths: this will allocate long
1618          ;; live ranges (i.e. TNs with more conflicts) first, and
1619          ;; hopefully minimise stack fragmentation.
1620          ;;
1621          ;; Collect in reverse order to give priority to older TNs.
1622          (let ((contiguous-tns '())
1623                (tns '()))
1624            (do ((tn (ir2-component-normal-tns 2comp) (tn-next tn)))
1625                ((null tn))
1626              (unless (tn-offset tn)
1627                (let ((key (cons tn (tn-lexical-depth tn))))
1628                  (if (memq (tn-kind tn) '(:environment :debug-environment
1629                                           :component))
1630                      (push key contiguous-tns)
1631                      (push key tns)))))
1632            (flet ((pack-tns (tns)
1633                     (dolist (tn (stable-sort tns #'< :key #'cdr))
1634                       (let ((tn (car tn)))
1635                         (unless (tn-offset tn)
1636                           (pack-tn tn nil optimize))))))
1637              ;; first pack TNs that are known to have simple
1638              ;; live ranges (contiguous lexical scopes)
1639              (pack-tns contiguous-tns)
1640              (pack-tns tns)))
1641
1642          ;; Do load TN packing and emit saves.
1643          (let ((*repack-blocks* nil))
1644            (cond ((and optimize *pack-optimize-saves*)
1645                   (optimized-emit-saves component)
1646                   (do-ir2-blocks (block component)
1647                     (pack-load-tns block)))
1648                  (t
1649                   (do-ir2-blocks (block component)
1650                     (emit-saves block)
1651                     (pack-load-tns block))))
1652            (loop
1653               (unless *repack-blocks* (return))
1654               (let ((orpb *repack-blocks*))
1655                 (setq *repack-blocks* nil)
1656                 (dolist (block orpb)
1657                   (event repack-block)
1658                   (pack-load-tns block)))))
1659
1660          (values))
1661     (clean-up-pack-structures)))