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