1 ;;;; This file contains the lifetime analysis phase in the compiler.
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
16 ;;; Link in a global-conflicts structure for TN in Block with Number as the
17 ;;; LTN number. The conflict is inserted in the per-TN Global-Conflicts thread
18 ;;; after the TN's Current-Conflict. We change the Current-Conflict to point
19 ;;; to the new conflict. Since we scan the blocks in reverse DFO, this list is
20 ;;; automatically built in order. We have to actually scan the current
21 ;;; Global-TNs for the block in order to keep that thread sorted.
22 (defun add-global-conflict (kind tn block number)
23 (declare (type (member :read :write :read-only :live) kind)
24 (type tn tn) (type ir2-block block)
25 (type (or local-tn-number null) number))
26 (let ((new (make-global-conflicts kind tn block number)))
27 (let ((last (tn-current-conflict tn)))
29 (shiftf (global-conflicts-tn-next new)
30 (global-conflicts-tn-next last)
32 (shiftf (global-conflicts-tn-next new)
33 (tn-global-conflicts tn)
35 (setf (tn-current-conflict tn) new)
37 (insert-block-global-conflict new block))
40 ;;; Do the actual insertion of the conflict New into Block's global conflicts.
41 (defun insert-block-global-conflict (new block)
42 (let ((global-num (tn-number (global-conflicts-tn new))))
44 (conf (ir2-block-global-tns block)
45 (global-conflicts-next conf)))
47 (> (tn-number (global-conflicts-tn conf)) global-num))
49 (setf (global-conflicts-next prev) new)
50 (setf (ir2-block-global-tns block) new))
51 (setf (global-conflicts-next new) conf))))
54 ;;; Reset the Current-Conflict slot in all packed TNs to point to the head
55 ;;; of the Global-Conflicts thread.
56 (defun reset-current-conflict (component)
57 (do-packed-tns (tn component)
58 (setf (tn-current-conflict tn) (tn-global-conflicts tn))))
62 ;;; Convert TN (currently local) to be a global TN, since we discovered that
63 ;;; it is referenced in more than one block. We just add a global-conflicts
64 ;;; structure with a kind derived from the Kill and Live sets.
65 (defun convert-to-global (tn)
66 (declare (type tn tn))
67 (let ((block (tn-local tn))
68 (num (tn-local-number tn)))
70 (if (zerop (sbit (ir2-block-written block) num))
72 (if (zerop (sbit (ir2-block-live-out block) num))
78 ;;; Scan all references to packed TNs in block. We assign LTN numbers to
79 ;;; each referenced TN, and also build the Kill and Live sets that summarize
80 ;;; the references to each TN for purposes of lifetime analysis.
82 ;;; It is possible that we will run out of LTN numbers. If this happens,
83 ;;; then we return the VOP that we were processing at the time we ran out,
84 ;;; otherwise we return NIL.
86 ;;; If a TN is referenced in more than one block, then we must represent
87 ;;; references using Global-Conflicts structures. When we first see a TN, we
88 ;;; assume it will be local. If we see a reference later on in a different
89 ;;; block, then we go back and fix the TN to global.
91 ;;; We must globalize TNs that have a block other than the current one in
92 ;;; their Local slot and have no Global-Conflicts. The latter condition is
93 ;;; necessary because we always set Local and Local-Number when we process a
94 ;;; reference to a TN, even when the TN is already known to be global.
96 ;;; When we see reference to global TNs during the scan, we add the
97 ;;; global-conflict as :Read-Only, since we don't know the correct kind until
98 ;;; we are done scanning the block.
99 (defun find-local-references (block)
100 (declare (type ir2-block block))
101 (let ((kill (ir2-block-written block))
102 (live (ir2-block-live-out block))
103 (tns (ir2-block-local-tns block)))
104 (let ((ltn-num (ir2-block-local-tn-count block)))
105 (do ((vop (ir2-block-last-vop block)
108 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
110 (let* ((tn (tn-ref-tn ref))
111 (local (tn-local tn))
113 (unless (member kind '(:component :environment :constant))
114 (unless (eq local block)
115 (when (= ltn-num local-tn-limit)
116 (return-from find-local-references vop))
118 (unless (tn-global-conflicts tn)
119 (convert-to-global tn))
120 (add-global-conflict :read-only tn block ltn-num))
122 (setf (tn-local tn) block)
123 (setf (tn-local-number tn) ltn-num)
124 (setf (svref tns ltn-num) tn)
127 (let ((num (tn-local-number tn)))
128 (if (tn-ref-write-p ref)
129 (setf (sbit kill num) 1 (sbit live num) 0)
130 (setf (sbit live num) 1)))))))
132 (setf (ir2-block-local-tn-count block) ltn-num)))
135 ;;; Finish up the global conflicts for TNs referenced in Block according to
136 ;;; the local Kill and Live sets.
138 ;;; We set the kind for TNs already in the global-TNs. If not written at
139 ;;; all, then is :Read-Only, the default. Must have been referenced somehow,
140 ;;; or we wouldn't have conflicts for it.
142 ;;; We also iterate over all the local TNs, looking for TNs local to this
143 ;;; block that are still live at the block beginning, and thus must be global.
144 ;;; This case is only important when a TN is read in a block but not written in
145 ;;; any other, since otherwise the write would promote the TN to global. But
146 ;;; this does happen with various passing-location TNs that are magically
147 ;;; written. This also serves to propagate the lives of erroneously
148 ;;; uninitialized TNs so that consistency checks can detect them.
149 (defun init-global-conflict-kind (block)
150 (declare (type ir2-block block))
151 (let ((live (ir2-block-live-out block)))
152 (let ((kill (ir2-block-written block)))
153 (do ((conf (ir2-block-global-tns block)
154 (global-conflicts-next conf)))
156 (let ((num (global-conflicts-number conf)))
157 (unless (zerop (sbit kill num))
158 (setf (global-conflicts-kind conf)
159 (if (zerop (sbit live num))
163 (let ((ltns (ir2-block-local-tns block)))
164 (dotimes (i (ir2-block-local-tn-count block))
165 (let ((tn (svref ltns i)))
166 (unless (or (eq tn :more)
167 (tn-global-conflicts tn)
168 (zerop (sbit live i)))
169 (convert-to-global tn))))))
173 (defevent split-ir2-block "Split an IR2 block to meet Local-TN-Limit.")
175 ;;; Move the code after the VOP Lose in 2block into its own block. The
176 ;;; block is linked into the emit order following 2block. Number is the block
177 ;;; number assigned to the new block. We return the new block.
178 (defun split-ir2-blocks (2block lose number)
179 (declare (type ir2-block 2block) (type vop lose)
180 (type unsigned-byte number))
181 (event split-ir2-block (vop-node lose))
182 (let ((new (make-ir2-block (ir2-block-block 2block)))
183 (new-start (vop-next lose)))
184 (setf (ir2-block-number new) number)
185 (add-to-emit-order new 2block)
187 (do ((vop new-start (vop-next vop)))
189 (setf (vop-block vop) new))
191 (setf (ir2-block-start-vop new) new-start)
192 (shiftf (ir2-block-last-vop new) (ir2-block-last-vop 2block) lose)
194 (setf (vop-next lose) nil)
195 (setf (vop-prev new-start) nil)
199 ;;; Clear the global and local conflict info in Block so that we can
200 ;;; recompute it without any old cruft being retained. It is assumed that all
201 ;;; LTN numbers are in use.
203 ;;; First we delete all the global conflicts. The conflict we are deleting
204 ;;; must be the last in the TN's global-conflicts, but we must scan for it in
205 ;;; order to find the previous conflict.
207 ;;; Next, we scan the local TNs, nulling out the Local slot in all TNs with
208 ;;; no global conflicts. This allows these TNs to be treated as local when we
209 ;;; scan the block again.
211 ;;; If there are conflicts, then we set Local to one of the conflicting
212 ;;; blocks. This ensures that Local doesn't hold over Block as its value,
213 ;;; causing the subsequent reanalysis to think that the TN has already been
214 ;;; seen in that block.
216 ;;; This function must not be called on blocks that have :More TNs.
217 (defun clear-lifetime-info (block)
218 (declare (type ir2-block block))
219 (setf (ir2-block-local-tn-count block) 0)
221 (do ((conf (ir2-block-global-tns block)
222 (global-conflicts-next conf)))
224 (setf (ir2-block-global-tns block) nil))
225 (let ((tn (global-conflicts-tn conf)))
226 (aver (eq (tn-current-conflict tn) conf))
227 (aver (null (global-conflicts-tn-next conf)))
228 (do ((current (tn-global-conflicts tn)
229 (global-conflicts-tn-next current))
233 (setf (global-conflicts-tn-next prev) nil)
234 (setf (tn-global-conflicts tn) nil))
235 (setf (tn-current-conflict tn) prev)))))
237 (fill (ir2-block-written block) 0)
238 (let ((ltns (ir2-block-local-tns block)))
239 (dotimes (i local-tn-limit)
240 (let ((tn (svref ltns i)))
241 (aver (not (eq tn :more)))
242 (let ((conf (tn-global-conflicts tn)))
245 (global-conflicts-block conf)
250 ;;; This provides a panic mode for assigning LTN numbers when there is a VOP
251 ;;; with so many more operands that they can't all be assigned distinct
252 ;;; numbers. When this happens, we recover by assigning all the more operands
253 ;;; the same LTN number. We can get away with this, since all more args (and
254 ;;; results) are referenced simultaneously as far as conflict analysis is
257 ;;; Block is the IR2-Block that the more VOP is at the end of. Ops is the
258 ;;; full argument or result TN-Ref list. Fixed is the types of the fixed
259 ;;; operands (used only to skip those operands.)
261 ;;; What we do is grab a LTN number, then make a :Read-Only global conflict
262 ;;; for each more operand TN. We require that there be no existing global
263 ;;; conflict in Block for any of the operands. Since conflicts must be cleared
264 ;;; before the first call, this only prohibits the same TN being used both as a
265 ;;; more operand and as any other operand to the same VOP.
267 ;;; We don't have to worry about getting the correct conflict kind, since
268 ;;; Init-Global-Conflict-Kind will fix things up. Similarly,
269 ;;; FIND-LOCAL-REFERENCES will set the local conflict bit corresponding to this
272 ;;; We also set the Local and Local-Number slots in each TN. It is
273 ;;; possible that there are no operands in any given call to this function, but
274 ;;; there had better be either some more args or more results.
275 (defun coalesce-more-ltn-numbers (block ops fixed)
276 (declare (type ir2-block block) (type (or tn-ref null) ops) (list fixed))
277 (let ((num (ir2-block-local-tn-count block)))
278 (aver (< num local-tn-limit))
279 (incf (ir2-block-local-tn-count block))
280 (setf (svref (ir2-block-local-tns block) num) :more)
282 (do ((op (do ((op ops (tn-ref-across op))
284 ((= i (length fixed)) op)
285 (declare (type index i)))
288 (let ((tn (tn-ref-tn op)))
291 (do ((ref refs (tn-ref-next ref)))
293 (when (and (eq (vop-block (tn-ref-vop ref)) block)
296 (and (frob (tn-reads tn)) (frob (tn-writes tn))))
297 () "More operand ~S used more than once in its VOP." op)
298 (aver (not (find-in #'global-conflicts-next tn
299 (ir2-block-global-tns block)
300 :key #'global-conflicts-tn)))
302 (add-global-conflict :read-only tn block num)
303 (setf (tn-local tn) block)
304 (setf (tn-local-number tn) num))))
307 (defevent coalesce-more-ltn-numbers
308 "Coalesced LTN numbers for a more operand to meet Local-TN-Limit.")
310 ;;; Loop over the blocks in Component, assigning LTN numbers and recording
311 ;;; TN birth and death. The only interesting action is when we run out of
312 ;;; local TN numbers while finding local references.
314 ;;; If we run out of LTN numbers while processing a VOP within the block,
315 ;;; then we just split off the VOPs we have successfully processed into their
318 ;;; If we run out of LTN numbers while processing the our first VOP (the
319 ;;; last in the block), then it must be the case that this VOP has large more
320 ;;; operands. We split the VOP into its own block, and then call
321 ;;; Coalesce-More-Ltn-Numbers to assign all the more args/results the same LTN
324 ;;; In either case, we clear the lifetime information that we computed so
325 ;;; far, recomputing it after taking corrective action.
327 ;;; Whenever we split a block, we finish the pre-pass on the split-off block
328 ;;; by doing Find-Local-References and Init-Global-Conflict-Kind. This can't
329 ;;; run out of LTN numbers.
330 (defun lifetime-pre-pass (component)
331 (declare (type component component))
333 (declare (type fixnum counter))
334 (do-blocks-backwards (block component)
335 (let ((2block (block-info block)))
336 (do ((lose (find-local-references 2block)
337 (find-local-references 2block))
341 (init-global-conflict-kind 2block)
342 (setf (ir2-block-number 2block) (incf counter)))
344 (clear-lifetime-info 2block)
348 (aver (not (eq last-lose lose)))
349 (let ((new (split-ir2-blocks 2block lose (incf counter))))
350 (aver (not (find-local-references new)))
351 (init-global-conflict-kind new)))
353 (aver (not (eq lose coalesced)))
354 (setq coalesced lose)
355 (event coalesce-more-ltn-numbers (vop-node lose))
356 (let ((info (vop-info lose))
357 (new (if (vop-prev lose)
358 (split-ir2-blocks 2block (vop-prev lose)
361 (coalesce-more-ltn-numbers new (vop-args lose)
362 (vop-info-arg-types info))
363 (coalesce-more-ltn-numbers new (vop-results lose)
364 (vop-info-result-types info))
365 (let ((lose (find-local-references new)))
367 (init-global-conflict-kind new))))))))
371 ;;;; environment TN stuff
373 ;;; Add a :LIVE global conflict for TN in 2block if there is none present.
374 ;;; If Debug-P is false (a :ENVIRONMENT TN), then modify any existing conflict
376 (defun setup-environment-tn-conflict (tn 2block debug-p)
377 (declare (type tn tn) (type ir2-block 2block))
378 (let ((block-num (ir2-block-number 2block)))
379 (do ((conf (tn-current-conflict tn) (global-conflicts-tn-next conf))
382 (> (ir2-block-number (global-conflicts-block conf)) block-num))
383 (setf (tn-current-conflict tn) prev)
384 (add-global-conflict :live tn 2block nil))
385 (when (eq (global-conflicts-block conf) 2block)
387 (eq (global-conflicts-kind conf) :live))
388 (setf (global-conflicts-kind conf) :live)
389 (setf (svref (ir2-block-local-tns 2block)
390 (global-conflicts-number conf))
392 (setf (global-conflicts-number conf) nil))
393 (setf (tn-current-conflict tn) conf)
397 ;;; Iterate over all the blocks in Env, setting up :LIVE conflicts for TN.
398 ;;; We make the TN global if it isn't already. The TN must have at least one
400 (defun setup-environment-tn-conflicts (component tn env debug-p)
401 (declare (type component component) (type tn tn) (type environment env))
403 (not (tn-global-conflicts tn))
405 (convert-to-global tn))
406 (setf (tn-current-conflict tn) (tn-global-conflicts tn))
407 (do-blocks-backwards (block component)
408 (when (eq (block-environment block) env)
409 (let* ((2block (block-info block))
410 (last (do ((b (ir2-block-next 2block) (ir2-block-next b))
412 ((not (eq (ir2-block-block b) block))
414 (do ((b last (ir2-block-prev b)))
415 ((not (eq (ir2-block-block b) block)))
416 (setup-environment-tn-conflict tn b debug-p)))))
419 ;;; Iterate over all the environment TNs, adding always-live conflicts as
421 (defun setup-environment-live-conflicts (component)
422 (declare (type component component))
423 (dolist (fun (component-lambdas component))
424 (let* ((env (lambda-environment fun))
425 (2env (environment-info env)))
426 (dolist (tn (ir2-environment-live-tns 2env))
427 (setup-environment-tn-conflicts component tn env nil))
428 (dolist (tn (ir2-environment-debug-live-tns 2env))
429 (setup-environment-tn-conflicts component tn env t))))
432 ;;; Convert a :NORMAL or :DEBUG-ENVIRONMENT TN to an :ENVIRONMENT TN. This
433 ;;; requires adding :LIVE conflicts to all blocks in TN-ENV.
434 (defun convert-to-environment-tn (tn tn-env)
435 (declare (type tn tn) (type environment tn-env))
436 (aver (member (tn-kind tn) '(:normal :debug-environment)))
437 (when (eq (tn-kind tn) :debug-environment)
438 (aver (eq (tn-environment tn) tn-env))
439 (let ((2env (environment-info tn-env)))
440 (setf (ir2-environment-debug-live-tns 2env)
441 (delete tn (ir2-environment-debug-live-tns 2env)))))
442 (setup-environment-tn-conflicts *component-being-compiled* tn tn-env nil)
443 (setf (tn-local tn) nil)
444 (setf (tn-local-number tn) nil)
445 (setf (tn-kind tn) :environment)
446 (setf (tn-environment tn) tn-env)
447 (push tn (ir2-environment-live-tns (environment-info tn-env)))
452 ;;; For each Global-TN in Block2 that is :Live, :Read or :Read-Only, ensure
453 ;;; that there is a corresponding Global-Conflict in Block1. If there is none,
454 ;;; make a :Live Global-Conflict. If there is a :Read-Only conflict, promote
457 ;;; If we did added a new conflict, return true, otherwise false. We don't
458 ;;; need to return true when we promote a :Read-Only conflict, since it doesn't
459 ;;; reveal any new information to predecessors of Block1.
461 ;;; We use the Tn-Current-Conflict to walk through the global
462 ;;; conflicts. Since the global conflicts for a TN are ordered by block, we
463 ;;; can be sure that the Current-Conflict always points at or before the block
464 ;;; that we are looking at. This allows us to quickly determine if there is a
465 ;;; global conflict for a given TN in Block1.
467 ;;; When we scan down the conflicts, we know that there must be at least one
468 ;;; conflict for TN, since we got our hands on TN by picking it out of a
469 ;;; conflict in Block2.
471 ;;; We leave the Current-Conflict pointing to the conflict for Block1. The
472 ;;; Current-Conflict must be initialized to the head of the Global-Conflicts
473 ;;; for the TN between each flow analysis iteration.
474 (defun propagate-live-tns (block1 block2)
475 (declare (type ir2-block block1 block2))
476 (let ((live-in (ir2-block-live-in block1))
478 (do ((conf2 (ir2-block-global-tns block2)
479 (global-conflicts-next conf2)))
481 (ecase (global-conflicts-kind conf2)
482 ((:live :read :read-only)
483 (let* ((tn (global-conflicts-tn conf2))
484 (tn-conflicts (tn-current-conflict tn))
485 (number1 (ir2-block-number block1)))
487 (do ((current tn-conflicts (global-conflicts-tn-next current))
490 (> (ir2-block-number (global-conflicts-block current))
492 (setf (tn-current-conflict tn) prev)
493 (add-global-conflict :live tn block1 nil)
494 (setq did-something t))
495 (when (eq (global-conflicts-block current) block1)
496 (case (global-conflicts-kind current)
499 (setf (global-conflicts-kind current) :live)
500 (setf (svref (ir2-block-local-tns block1)
501 (global-conflicts-number current))
503 (setf (global-conflicts-number current) nil)
504 (setf (tn-current-conflict tn) current))
506 (setf (sbit live-in (global-conflicts-number current)) 1)))
511 ;;; Do backward global flow analysis to find all TNs live at each block
513 (defun lifetime-flow-analysis (component)
515 (reset-current-conflict component)
516 (let ((did-something nil))
517 (do-blocks-backwards (block component)
518 (let* ((2block (block-info block))
519 (last (do ((b (ir2-block-next 2block) (ir2-block-next b))
521 ((not (eq (ir2-block-block b) block))
524 (dolist (b (block-succ block))
525 (when (and (block-start b)
526 (propagate-live-tns last (block-info b)))
527 (setq did-something t)))
529 (do ((b (ir2-block-prev last) (ir2-block-prev b))
531 ((not (eq (ir2-block-block b) block)))
532 (when (propagate-live-tns b prev)
533 (setq did-something t)))))
535 (unless did-something (return))))
541 ;;; Note that TN conflicts with all current live TNs. Num is TN's LTN
542 ;;; number. We bit-ior Live-Bits with TN's Local-Conflicts, and set TN's
543 ;;; number in the conflicts of all TNs in Live-List.
544 (defun note-conflicts (live-bits live-list tn num)
545 (declare (type tn tn) (type (or tn null) live-list)
546 (type local-tn-bit-vector live-bits)
547 (type local-tn-number num))
548 (let ((lconf (tn-local-conflicts tn)))
549 (bit-ior live-bits lconf lconf))
550 (do ((live live-list (tn-next* live)))
552 (setf (sbit (tn-local-conflicts live) num) 1))
555 ;;; Compute a bit vector of the TNs live after VOP that aren't results.
556 (defun compute-save-set (vop live-bits)
557 (declare (type vop vop) (type local-tn-bit-vector live-bits))
558 (let ((live (bit-vector-copy live-bits)))
559 (do ((r (vop-results vop) (tn-ref-across r)))
561 (let ((tn (tn-ref-tn r)))
563 ((:normal :debug-environment)
564 (setf (sbit live (tn-local-number tn)) 0))
565 (:environment :component))))
568 ;;; Used to determine whether a :DEBUG-ENVIRONMENT TN should be considered
569 ;;; live at block end. We return true if a VOP with non-null SAVE-P appears
570 ;;; before the first read of TN (hence is seen first in our backward scan.)
571 (defun saved-after-read (tn block)
572 (do ((vop (ir2-block-last-vop block) (vop-prev vop)))
574 (when (vop-info-save-p (vop-info vop)) (return t))
575 (when (find-in #'tn-ref-across tn (vop-args vop) :key #'tn-ref-tn)
578 ;;; If the block has no successors, or its successor is the component tail,
579 ;;; then all :DEBUG-ENVIRONMENT TNs are always added, regardless of whether
580 ;;; they appeared to be live. This ensures that these TNs are considered to be
581 ;;; live throughout blocks that read them, but don't have any interesting
582 ;;; successors (such as a return or tail call.) In this case, we set the
583 ;;; corresponding bit in LIVE-IN as well.
584 (defun make-debug-environment-tns-live (block live-bits live-list)
585 (let* ((1block (ir2-block-block block))
586 (live-in (ir2-block-live-in block))
587 (succ (block-succ 1block))
588 (next (ir2-block-next block)))
590 (not (eq (ir2-block-block next) 1block))
593 (component-tail (block-component 1block)))))
594 (do ((conf (ir2-block-global-tns block)
595 (global-conflicts-next conf)))
597 (let* ((tn (global-conflicts-tn conf))
598 (num (global-conflicts-number conf)))
599 (when (and num (zerop (sbit live-bits num))
600 (eq (tn-kind tn) :debug-environment)
601 (eq (tn-environment tn) (block-environment 1block))
602 (saved-after-read tn block))
603 (note-conflicts live-bits live-list tn num)
604 (setf (sbit live-bits num) 1)
605 (push-in tn-next* tn live-list)
606 (setf (sbit live-in num) 1))))))
608 (values live-bits live-list))
610 ;;; Return as values, a LTN bit-vector and a list (threaded by TN-Next*)
611 ;;; representing the TNs live at the end of Block (exclusive of :Live TNs).
613 ;;; We iterate over the TNs in the global conflicts that are live at the block
614 ;;; end, setting up the TN-Local-Conflicts and TN-Local-Number, and adding the
615 ;;; TN to the live list.
617 ;;; If a :MORE result is not live, we effectively fake a read to it. This is
618 ;;; part of the action described in ENSURE-RESULTS-LIVE.
620 ;;; At the end, we call MAKE-DEBUG-ENVIRONEMNT-TNS-LIVE to make debug
621 ;;; environment TNs appear live when appropriate, even when they aren't.
623 ;;; ### Note: we alias the global-conflicts-conflicts here as the
624 ;;; tn-local-conflicts.
625 (defun compute-initial-conflicts (block)
626 (declare (type ir2-block block))
627 (let* ((live-in (ir2-block-live-in block))
628 (ltns (ir2-block-local-tns block))
629 (live-bits (bit-vector-copy live-in))
632 (do ((conf (ir2-block-global-tns block)
633 (global-conflicts-next conf)))
635 (let ((bits (global-conflicts-conflicts conf))
636 (tn (global-conflicts-tn conf))
637 (num (global-conflicts-number conf))
638 (kind (global-conflicts-kind conf)))
639 (setf (tn-local-number tn) num)
640 (unless (eq kind :live)
641 (cond ((not (zerop (sbit live-bits num)))
642 (bit-vector-replace bits live-bits)
643 (setf (sbit bits num) 0)
644 (push-in tn-next* tn live-list))
645 ((and (eq (svref ltns num) :more)
647 (note-conflicts live-bits live-list tn num)
648 (setf (sbit live-bits num) 1)
649 (push-in tn-next* tn live-list)
650 (setf (sbit live-in num) 1)))
652 (setf (tn-local-conflicts tn) bits))))
654 (make-debug-environment-tns-live block live-bits live-list)))
656 ;;; A function called in Conflict-Analyze-1-Block when we have a VOP with
657 ;;; SAVE-P true. We compute the save-set, and if :FORCE-TO-STACK, force all
658 ;;; the live TNs to be stack environment TNs.
659 (defun do-save-p-stuff (vop block live-bits)
660 (declare (type vop vop) (type ir2-block block)
661 (type local-tn-bit-vector live-bits))
662 (let ((ss (compute-save-set vop live-bits)))
663 (setf (vop-save-set vop) ss)
664 (when (eq (vop-info-save-p (vop-info vop)) :force-to-stack)
665 (do-live-tns (tn ss block)
666 (unless (eq (tn-kind tn) :component)
667 (force-tn-to-stack tn)
668 (unless (eq (tn-kind tn) :environment)
669 (convert-to-environment-tn
671 (block-environment (ir2-block-block block))))))))
674 ;;; FIXME: The next 3 macros aren't needed in the target runtime.
675 ;;; Figure out some way to make them only at build time. (Just
676 ;;; (EVAL-WHEN (:COMPILE-TOPLEVEL :EXECUTE) (DEFMACRO ..)) isn't good enough,
677 ;;; since we need CL:DEFMACRO at build-the-cross-compiler time and
678 ;;; SB!XC:DEFMACRO at run-the-cross-compiler time.)
680 ;;; Used in SCAN-VOP-REFS to simultaneously do something to all of the TNs
681 ;;; referenced by a big more arg. We have to treat these TNs specially, since
682 ;;; when we set or clear the bit in the live TNs, the represents a change in
683 ;;; the liveness of all the more TNs. If we iterated as normal, the next more
684 ;;; ref would be thought to be not live when it was, etc. We update Ref to be
685 ;;; the last :more ref we scanned, so that the main loop will step to the next
687 (defmacro frob-more-tns (action)
688 `(when (eq (svref ltns num) :more)
690 (do ((mref (tn-ref-next-ref ref) (tn-ref-next-ref mref)))
692 (let ((mtn (tn-ref-tn mref)))
693 (unless (eql (tn-local-number mtn) num)
699 ;;; Handle the part of CONFLICT-ANALYZE-1-BLOCK that scans the REFs for the
700 ;;; current VOP. This macro shamelessly references free variables in C-A-1-B.
701 (defmacro scan-vop-refs ()
702 '(do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
704 (let* ((tn (tn-ref-tn ref))
705 (num (tn-local-number tn)))
708 ((not (zerop (sbit live-bits num)))
709 (when (tn-ref-write-p ref)
710 (setf (sbit live-bits num) 0)
711 (deletef-in tn-next* live-list tn)
712 (frob-more-tns (deletef-in tn-next* live-list mtn))))
714 (aver (not (tn-ref-write-p ref)))
715 (note-conflicts live-bits live-list tn num)
716 (frob-more-tns (note-conflicts live-bits live-list mtn num))
717 (setf (sbit live-bits num) 1)
718 (push-in tn-next* tn live-list)
719 (frob-more-tns (push-in tn-next* mtn live-list)))))))
721 ;;; This macro is called by CONFLICT-ANALYZE-1-BLOCK to scan the current
722 ;;; VOP's results, and make any dead ones live. This is necessary, since even
723 ;;; though a result is dead after the VOP, it may be in use for an extended
724 ;;; period within the VOP (especially if it has :FROM specified.) During this
725 ;;; interval, temporaries must be noted to conflict with the result. More
726 ;;; results are finessed in COMPUTE-INITIAL-CONFLICTS, so we ignore them here.
727 (defmacro ensure-results-live ()
728 '(do ((res (vop-results vop) (tn-ref-across res)))
730 (let* ((tn (tn-ref-tn res))
731 (num (tn-local-number tn)))
732 (when (and num (zerop (sbit live-bits num)))
733 (unless (eq (svref ltns num) :more)
734 (note-conflicts live-bits live-list tn num)
735 (setf (sbit live-bits num) 1)
736 (push-in tn-next* tn live-list))))))
738 ;;; Compute the block-local conflict information for Block. We iterate over
739 ;;; all the TN-Refs in a block in reference order, maintaining the set of live
740 ;;; TNs in both a list and a bit-vector representation.
741 (defun conflict-analyze-1-block (block)
742 (declare (type ir2-block block))
743 (multiple-value-bind (live-bits live-list)
744 (compute-initial-conflicts block)
745 (let ((ltns (ir2-block-local-tns block)))
746 (do ((vop (ir2-block-last-vop block)
749 (when (vop-info-save-p (vop-info vop))
750 (do-save-p-stuff vop block live-bits))
751 (ensure-results-live)
754 ;;; Conflict analyze each block, and also add it.
755 (defun lifetime-post-pass (component)
756 (declare (type component component))
757 (do-ir2-blocks (block component)
758 (conflict-analyze-1-block block)))
762 ;;; Destructively modify Oconf to include the conflict information in Conf.
763 (defun merge-alias-block-conflicts (conf oconf)
764 (declare (type global-conflicts conf oconf))
765 (let* ((kind (global-conflicts-kind conf))
766 (num (global-conflicts-number conf))
767 (okind (global-conflicts-kind oconf))
768 (onum (global-conflicts-number oconf))
769 (block (global-conflicts-block oconf))
770 (ltns (ir2-block-local-tns block)))
774 (setf (global-conflicts-kind oconf) :live)
775 (setf (svref ltns onum) nil)
776 (setf (global-conflicts-number oconf) nil))
778 (unless (eq kind okind)
779 (setf (global-conflicts-kind oconf) :read))
780 ;; Make original conflict with all the local TNs the alias conflicted
782 (bit-ior (global-conflicts-conflicts oconf)
783 (global-conflicts-conflicts conf)
786 (unless (zerop (sbit x num))
787 (setf (sbit x onum) 1))))
788 ;; Make all the local TNs that conflicted with the alias conflict
789 ;; with the original.
790 (dotimes (i (ir2-block-local-tn-count block))
791 (let ((tn (svref ltns i)))
792 (when (and tn (not (eq tn :more))
793 (null (tn-global-conflicts tn)))
794 (frob (tn-local-conflicts tn)))))
795 ;; Same for global TNs...
796 (do ((current (ir2-block-global-tns block)
797 (global-conflicts-next current)))
799 (unless (eq (global-conflicts-kind current) :live)
800 (frob (global-conflicts-conflicts current))))
801 ;; Make the original TN live everywhere that the alias was live.
802 (frob (ir2-block-written block))
803 (frob (ir2-block-live-in block))
804 (frob (ir2-block-live-out block))
805 (do ((vop (ir2-block-start-vop block)
808 (let ((sset (vop-save-set vop)))
809 (when sset (frob sset)))))))
810 ;; Delete the alias's conflict info.
812 (setf (svref ltns num) nil))
813 (deletef-in global-conflicts-next (ir2-block-global-tns block) conf))
817 ;;; Co-opt Conf to be a conflict for TN.
818 (defun change-global-conflicts-tn (conf new)
819 (declare (type global-conflicts conf) (type tn new))
820 (setf (global-conflicts-tn conf) new)
821 (let ((ltn-num (global-conflicts-number conf))
822 (block (global-conflicts-block conf)))
823 (deletef-in global-conflicts-next (ir2-block-global-tns block) conf)
824 (setf (global-conflicts-next conf) nil)
825 (insert-block-global-conflict conf block)
827 (setf (svref (ir2-block-local-tns block) ltn-num) new)))
830 ;;; Do CONVERT-TO-GLOBAL on TN if it has no global conflicts. Copy the
831 ;;; local conflicts into the global bit vector.
832 (defun ensure-global-tn (tn)
833 (declare (type tn tn))
834 (cond ((tn-global-conflicts tn))
836 (convert-to-global tn)
837 (bit-ior (global-conflicts-conflicts (tn-global-conflicts tn))
838 (tn-local-conflicts tn)
841 (aver (and (null (tn-reads tn)) (null (tn-writes tn))))))
844 ;;; For each :ALIAS TN, destructively merge the conflict info into the
845 ;;; original TN and replace the uses of the alias.
847 ;;; For any block that uses only the alias TN, just insert that
848 ;;; conflict into the conflicts for the original TN, changing the LTN
849 ;;; map to refer to the original TN. This gives a result
850 ;;; indistinguishable from the what there would have been if the
851 ;;; original TN had always been referenced. This leaves no sign that
852 ;;; an alias TN was ever involved.
854 ;;; If a block has references to both the alias and the original TN,
855 ;;; then we call MERGE-ALIAS-BLOCK-CONFLICTS to combine the conflicts
856 ;;; into the original conflict.
857 (defun merge-alias-conflicts (component)
858 (declare (type component component))
859 (do ((tn (ir2-component-alias-tns (component-info component))
862 (let ((original (tn-save-tn tn)))
863 (ensure-global-tn tn)
864 (ensure-global-tn original)
865 (let ((conf (tn-global-conflicts tn))
866 (oconf (tn-global-conflicts original))
871 (setf (global-conflicts-tn-next oprev) conf)
872 (setf (tn-global-conflicts original) conf))
873 (do ((current conf (global-conflicts-tn-next current)))
875 (change-global-conflicts-tn current original))
877 (let* ((block (global-conflicts-block conf))
878 (num (ir2-block-number block))
879 (onum (ir2-block-number (global-conflicts-block oconf))))
882 (shiftf oprev oconf (global-conflicts-tn-next oconf)))
885 (setf (global-conflicts-tn-next oprev) conf)
886 (setf (tn-global-conflicts original) conf))
887 (change-global-conflicts-tn conf original)
888 (shiftf oprev conf (global-conflicts-tn-next conf) oconf))
890 (merge-alias-block-conflicts conf oconf)
891 (shiftf oprev oconf (global-conflicts-tn-next oconf))
892 (setf conf (global-conflicts-tn-next conf)))))
893 (unless conf (return))))
899 (unless ref (return))
900 (setq next (tn-ref-next ref))
901 (change-tn-ref-tn ref original)
904 (frob (tn-writes tn)))
905 (setf (tn-global-conflicts tn) nil)))
909 (defun lifetime-analyze (component)
910 (lifetime-pre-pass component)
911 (setup-environment-live-conflicts component)
912 (lifetime-flow-analysis component)
913 (lifetime-post-pass component)
914 (merge-alias-conflicts component))
916 ;;;; conflict testing
918 ;;; Test for a conflict between the local TN X and the global TN Y. We just
919 ;;; look for a global conflict of Y in X's block, and then test for conflict in
921 ;;; [### Might be more efficient to scan Y's global conflicts. This depends on
922 ;;; whether there are more global TNs than blocks.]
923 (defun tns-conflict-local-global (x y)
924 (let ((block (tn-local x)))
925 (do ((conf (ir2-block-global-tns block)
926 (global-conflicts-next conf)))
928 (when (eq (global-conflicts-tn conf) y)
929 (let ((num (global-conflicts-number conf)))
930 (return (or (not num)
931 (not (zerop (sbit (tn-local-conflicts x)
934 ;;; Test for conflict between two global TNs X and Y.
935 (defun tns-conflict-global-global (x y)
936 (declare (type tn x y))
937 (let* ((x-conf (tn-global-conflicts x))
938 (x-num (ir2-block-number (global-conflicts-block x-conf)))
939 (y-conf (tn-global-conflicts y))
940 (y-num (ir2-block-number (global-conflicts-block y-conf))))
942 (macrolet ((advance (n c)
944 (setq ,c (global-conflicts-tn-next ,c))
945 (unless ,c (return-from tns-conflict-global-global nil))
946 (setq ,n (ir2-block-number (global-conflicts-block ,c)))))
953 ;; x-conf, y-conf true, x-num, y-num corresponding block numbers.
954 (scan x-num y-num y-conf)
955 (scan y-num x-num x-conf)
956 (when (= x-num y-num)
957 (let ((ltn-num-x (global-conflicts-number x-conf)))
958 (unless (and ltn-num-x
959 (global-conflicts-number y-conf)
960 (zerop (sbit (global-conflicts-conflicts y-conf)
963 (advance x-num x-conf)
964 (advance y-num y-conf)))))))
966 ;;; Return true if X and Y are distinct and the lifetimes of X and Y overlap
968 (defun tns-conflict (x y)
969 (declare (type tn x y))
970 (let ((x-kind (tn-kind x))
971 (y-kind (tn-kind y)))
973 ((or (eq x-kind :component) (eq y-kind :component)) t)
974 ((tn-global-conflicts x)
975 (if (tn-global-conflicts y)
976 (tns-conflict-global-global x y)
977 (tns-conflict-local-global y x)))
978 ((tn-global-conflicts y)
979 (tns-conflict-local-global x y))
981 (and (eq (tn-local x) (tn-local y))
982 (not (zerop (sbit (tn-local-conflicts x)
983 (tn-local-number y)))))))))