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