/[cmucl]/src/compiler/life.lisp
ViewVC logotype

Contents of /src/compiler/life.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Tue Feb 6 11:29:14 1990 UTC (24 years, 2 months ago) by wlott
Branch: MAIN
Initial revision
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the Spice Lisp project at
5 ;;; Carnegie-Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of Spice Lisp, please contact
7 ;;; Scott Fahlman (FAHLMAN@CMUC).
8 ;;; **********************************************************************
9 ;;;
10 ;;; This file contains the lifetime analysis phase in the compiler.
11 ;;;
12 ;;; Written by Rob MacLachlan
13 ;;;
14 (in-package 'c)
15
16
17 ;;;; Utilities:
18
19 ;;; Add-Global-Conflict -- Internal
20 ;;;
21 ;;; Link in a global-conflicts structure for TN in Block with Number as the
22 ;;; LTN number. The conflict is inserted in the per-TN Global-Conflicts thread
23 ;;; after the TN's Current-Conflict. We change the Current-Conflict to point
24 ;;; to the new conflict. Since we scan the blocks in reverse DFO, this list is
25 ;;; automatically built in order. We have to actually scan the current
26 ;;; Global-TNs for the block in order to keep that thread sorted.
27 ;;;
28 (defun add-global-conflict (kind tn block number)
29 (declare (type (member :read :write :read-only :live) kind)
30 (type tn tn) (type ir2-block block)
31 (type (or local-tn-number null) number))
32 (let ((new (make-global-conflicts kind tn block number)))
33 (let ((last (tn-current-conflict tn)))
34 (if last
35 (shiftf (global-conflicts-tn-next new)
36 (global-conflicts-tn-next last)
37 new)
38 (shiftf (global-conflicts-tn-next new)
39 (tn-global-conflicts tn)
40 new)))
41 (setf (tn-current-conflict tn) new)
42
43 (let ((global-num (tn-number tn)))
44 (do ((prev nil conf)
45 (conf (ir2-block-global-tns block)
46 (global-conflicts-next conf)))
47 ((or (null conf)
48 (> (tn-number (global-conflicts-tn conf)) global-num))
49 (if prev
50 (setf (global-conflicts-next prev) new)
51 (setf (ir2-block-global-tns block) new))
52 (setf (global-conflicts-next new) conf)))))
53 (undefined-value))
54
55
56 ;;; Reset-Current-Conflict -- Internal
57 ;;;
58 ;;; Reset the Current-Conflict slot in all packed TNs to point to the head
59 ;;; of the Global-Conflicts thread.
60 ;;;
61 (defun reset-current-conflict (component)
62 (do-packed-tns (tn component)
63 (setf (tn-current-conflict tn) (tn-global-conflicts tn))))
64
65
66 ;;;; Pre-pass:
67
68 ;;; Convert-To-Global -- Internal
69 ;;;
70 ;;; Convert TN (currently local) to be a global TN, since we discovered that
71 ;;; it is referenced in more than one block. We just add a global-conflicts
72 ;;; structure with a kind derived from the Kill and Live sets.
73 ;;;
74 (defun convert-to-global (tn)
75 (declare (type tn tn))
76 (let ((block (tn-local tn))
77 (num (tn-local-number tn)))
78 (add-global-conflict
79 (if (zerop (sbit (ir2-block-written block) num))
80 :read-only
81 (if (zerop (sbit (ir2-block-live-out block) num))
82 :write
83 :read))
84 tn block num))
85 (undefined-value))
86
87
88 ;;; Find-Local-References -- Internal
89 ;;;
90 ;;; Scan all references to packed TNs in block. We assign LTN numbers to
91 ;;; each referenced TN, and also build the Kill and Live sets that summarize
92 ;;; the references to each TN for purposes of lifetime analysis.
93 ;;;
94 ;;; It is possible that we will run out of LTN numbers. If this happens,
95 ;;; then we return the VOP that we were processing at the time we ran out,
96 ;;; otherwise we return NIL.
97 ;;;
98 ;;; If a TN is referenced in more than one block, then we must represent
99 ;;; references using Global-Conflicts structures. When we first see a TN, we
100 ;;; assume it will be local. If we see a reference later on in a different
101 ;;; block, then we go back and fix the TN to global.
102 ;;;
103 ;;; We must globalize TNs that have a block other than the current one in
104 ;;; their Local slot and have no Global-Conflicts. The latter condition is
105 ;;; necessary because we always set Local and Local-Number when we process a
106 ;;; reference to a TN, even when the TN is already known to be global.
107 ;;;
108 ;;; When we see reference to global TNs during the scan, we add the
109 ;;; global-conflict as :Read-Only, since we don't know the corrent kind until
110 ;;; we are done scanning the block.
111 ;;;
112 (defun find-local-references (block)
113 (declare (type ir2-block block))
114 (let ((kill (ir2-block-written block))
115 (live (ir2-block-live-out block))
116 (tns (ir2-block-local-tns block)))
117 (let ((ltn-num (ir2-block-local-tn-count block)))
118 (do ((vop (ir2-block-last-vop block)
119 (vop-prev vop)))
120 ((null vop))
121 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
122 ((null ref))
123 (let* ((tn (tn-ref-tn ref))
124 (local (tn-local tn))
125 (kind (tn-kind tn)))
126 (unless (or (eq kind :constant)
127 (eq kind :environment))
128 (unless (eq local block)
129 (when (= ltn-num local-tn-limit)
130 (return-from find-local-references vop))
131 (when local
132 (unless (tn-global-conflicts tn)
133 (convert-to-global tn))
134 (add-global-conflict :read-only tn block ltn-num))
135
136 (setf (tn-local tn) block)
137 (setf (tn-local-number tn) ltn-num)
138 (setf (svref tns ltn-num) tn)
139 (incf ltn-num))
140
141 (let ((num (tn-local-number tn)))
142 (if (tn-ref-write-p ref)
143 (setf (sbit kill num) 1 (sbit live num) 0)
144 (setf (sbit live num) 1)))))))
145
146 (setf (ir2-block-local-tn-count block) ltn-num)))
147 nil)
148
149
150 ;;; Init-Global-Conflict-Kind -- Internal
151 ;;;
152 ;;; Finish up the global conflicts for TNs referenced in Block according to
153 ;;; the local Kill and Live sets.
154 ;;;
155 ;;; We set the kind for TNs already in the global-TNs. If not written at
156 ;;; all, then is :Read-Only, the default. Must have been referenced somehow,
157 ;;; or we wouldn't have conflicts for it.
158 ;;;
159 ;;; We also iterate over all the local TNs, looking for TNs local to this
160 ;;; block that are still live at the block beginning, and thus must be global.
161 ;;; This case is only important when a TN is read in a block but not written in
162 ;;; any other, since otherwise the write would promote the TN to global. But
163 ;;; this does happen with various passing-location TNs that are magically
164 ;;; written. This also serves to propagate the lives of erroneously
165 ;;; uninitialized TNs so that consistency checks can detect them.
166 ;;;
167 (defun init-global-conflict-kind (block)
168 (declare (type ir2-block block))
169 (let ((live (ir2-block-live-out block)))
170 (let ((kill (ir2-block-written block)))
171 (do ((conf (ir2-block-global-tns block)
172 (global-conflicts-next conf)))
173 ((null conf))
174 (let ((num (global-conflicts-number conf)))
175 (unless (zerop (sbit kill num))
176 (setf (global-conflicts-kind conf)
177 (if (zerop (sbit live num))
178 :write
179 :read))))))
180
181 (let ((ltns (ir2-block-local-tns block)))
182 (dotimes (i (ir2-block-local-tn-count block))
183 (let ((tn (svref ltns i)))
184 (unless (or (eq tn :more)
185 (tn-global-conflicts tn)
186 (zerop (sbit live i)))
187 (convert-to-global tn)))))))
188
189 (undefined-value))
190
191
192 (defevent split-ir2-block "Split an IR2 block to meet Local-TN-Limit.")
193
194 ;;; Split-IR2-Blocks -- Internal
195 ;;;
196 ;;; Move the code after the VOP Lose in 2block into its own block. The
197 ;;; block is linked into the emit order following 2block. Number is the block
198 ;;; number assigned to the new block. We return the new block.
199 ;;;
200 (defun split-ir2-blocks (2block lose number)
201 (declare (type ir2-block 2block) (type vop lose)
202 (type unsigned-byte number))
203 (event split-ir2-block (vop-node lose))
204 (let ((new (make-ir2-block (ir2-block-block 2block)))
205 (new-start (vop-next lose)))
206 (setf (ir2-block-number new) number)
207 (add-to-emit-order new 2block)
208
209 (do ((vop new-start (vop-next vop)))
210 ((null vop))
211 (setf (vop-block vop) new))
212
213 (setf (ir2-block-start-vop new) new-start)
214 (shiftf (ir2-block-last-vop new) (ir2-block-last-vop 2block) lose)
215
216 (setf (vop-next lose) nil)
217 (setf (vop-prev new-start) nil)
218
219 new))
220
221
222 ;;; Clear-Lifetime-Info -- Internal
223 ;;;
224 ;;; Clear the global and local conflict info in Block so that we can
225 ;;; recompute it without any old cruft being retained. It is assumed that all
226 ;;; LTN numbers are in use.
227 ;;;
228 ;;; First we delete all the global conflicts. The conflict we are deleting
229 ;;; must be the last in the TN's global-conflicts, but we must scan for it in
230 ;;; order to find the previous conflict.
231 ;;;
232 ;;; Next, we scan the local TNs, nulling out the Local slot in all TNs with
233 ;;; no global conflicts. This allows these TNs to be treated as local when we
234 ;;; scan the block again.
235 ;;;
236 ;;; If there are conflicts, then we set Local to one of the conflicting
237 ;;; blocks. This ensures that Local doesn't hold over Block as its value,
238 ;;; causing the subsequent reanalysis to think that the TN has already been
239 ;;; seen in that block.
240 ;;;
241 ;;; This function must not be called on blocks that have :More TNs.
242 ;;;
243 (defun clear-lifetime-info (block)
244 (declare (type ir2-block block))
245 (setf (ir2-block-local-tn-count block) 0)
246
247 (do ((conf (ir2-block-global-tns block)
248 (global-conflicts-next conf)))
249 ((null conf)
250 (setf (ir2-block-global-tns block) nil))
251 (let ((tn (global-conflicts-tn conf)))
252 (assert (eq (tn-current-conflict tn) conf))
253 (assert (null (global-conflicts-tn-next conf)))
254 (do ((current (tn-global-conflicts tn)
255 (global-conflicts-tn-next current))
256 (prev nil current))
257 ((eq current conf)
258 (if prev
259 (setf (global-conflicts-tn-next prev) nil)
260 (setf (tn-global-conflicts tn) nil))
261 (setf (tn-current-conflict tn) prev)))))
262
263 (fill (ir2-block-written block) 0)
264 (let ((ltns (ir2-block-local-tns block)))
265 (dotimes (i local-tn-limit)
266 (let ((tn (svref ltns i)))
267 (assert (not (eq tn :more)))
268 (let ((conf (tn-global-conflicts tn)))
269 (setf (tn-local tn)
270 (if conf
271 (global-conflicts-block conf)
272 nil)))))))
273
274 (undefined-value))
275
276
277 ;;; Coalesce-More-LTN-Numbers -- Internal
278 ;;;
279 ;;; This provides a panic mode for assigning LTN numbers when there is a VOP
280 ;;; with so many more operands that they can't all be assigned distinct
281 ;;; numbers. When this happens, we recover by assigning all the more operands
282 ;;; the same LTN number. We can get away with this, since all more args (and
283 ;;; results) are referenced simultaneously as far as conflict analysis is
284 ;;; concerned.
285 ;;;
286 ;;; Block is the IR2-Block that the more VOP is at the end of. Ops is the
287 ;;; full argument or result TN-Ref list. Fixed is the types of the fixed
288 ;;; operands (used only to skip those operands.)
289 ;;;
290 ;;; What we do is grab a LTN number, then make a :Read-Only global conflict
291 ;;; for each more operand TN. We require that there be no existing global
292 ;;; conflict in Block for any of the operands. Since conflicts must be cleared
293 ;;; before the first call, this only prohibits the same TN being used both as a
294 ;;; more operand and as any other operand to the same VOP.
295 ;;;
296 ;;; We don't have to worry about getting the correct conflict kind, since
297 ;;; Init-Global-Conflict-Kind will fix things up.
298 ;;;
299 ;;; We also set the Local and Local-Number slots in each TN.
300 ;;;
301 (defun coalesce-more-ltn-numbers (block ops fixed)
302 (declare (type ir2-block block) (type tn-ref ops) (list fixed))
303 (let ((num (ir2-block-local-tn-count block)))
304 (assert (< num local-tn-limit))
305 (incf (ir2-block-local-tn-count block))
306 (setf (svref (ir2-block-local-tns block) num) :more)
307
308 (do ((op (do ((op ops (tn-ref-across op))
309 (i 0 (1+ i)))
310 ((= i (length fixed)) op))
311 (tn-ref-across op)))
312 ((null op))
313 (let ((tn (tn-ref-tn op)))
314 (assert
315 (flet ((frob (refs)
316 (do ((ref refs (tn-ref-next ref)))
317 ((null ref) t)
318 (when (and (eq (vop-block (tn-ref-vop ref)) block)
319 (not (eq ref op)))
320 (return nil)))))
321 (and (frob (tn-reads tn)) (frob (tn-writes tn))))
322 () "More operand ~S used more than once in its VOP." op)
323 (assert (not (find-in #'global-conflicts-next tn
324 (ir2-block-global-tns block)
325 :key #'global-conflicts-tn)))
326
327 (add-global-conflict :read-only tn block num)
328 (setf (tn-local tn) block)
329 (setf (tn-local-number tn) num))))
330 (undefined-value))
331
332
333 (defevent coalesce-more-ltn-numbers
334 "Coalesced LTN numbers for a more operand to meet Local-TN-Limit.")
335
336 ;;; Lifetime-Pre-Pass -- Internal
337 ;;;
338 ;;; Loop over the blocks in Component, assigning LTN numbers and recording
339 ;;; TN birth and death. The only interesting action is when we run out of
340 ;;; local TN numbers while finding local references.
341 ;;;
342 ;;; If we run out of LTN numbers while processing a VOP within the block,
343 ;;; then we just split off the VOPs we have successfully processed into their
344 ;;; own block.
345 ;;;
346 ;;; If we run out of LTN numbers while processing the our first VOP (the
347 ;;; last in the block), then it must be the case that this VOP has large more
348 ;;; operands. We split the VOP into its own block, and then call
349 ;;; Coalesce-More-Ltn-Numbers to assign all the more args/results the same LTN
350 ;;; number(s).
351 ;;;
352 ;;; In either case, we clear the lifetime information that we computed so
353 ;;; far, recomputing it after taking corrective action.
354 ;;;
355 ;;; Whenever we split a block, we finish the pre-pass on the split-off block
356 ;;; by doing Find-Local-References and Init-Global-Conflict-Kind. This can't
357 ;;; run out of LTN numbers.
358 ;;;
359 (defun lifetime-pre-pass (component)
360 (declare (type component component))
361 (let ((counter -1))
362 (do-blocks-backwards (block component)
363 (let ((2block (block-info block)))
364 (do ((lose (find-local-references 2block)
365 (find-local-references 2block))
366 (last-lose nil lose)
367 (coalesced nil))
368 ((not lose)
369 (init-global-conflict-kind 2block)
370 (setf (ir2-block-number 2block) (incf counter)))
371
372 (clear-lifetime-info 2block)
373
374 (cond
375 ((vop-next lose)
376 (assert (not (eq last-lose lose)))
377 (let ((new (split-ir2-blocks 2block lose (incf counter))))
378 (assert (not (find-local-references new)))
379 (init-global-conflict-kind new)))
380 (t
381 (assert (not (eq lose coalesced)))
382 (setq coalesced lose)
383 (event coalesce-more-ltn-numbers (vop-node lose))
384 (let ((info (vop-info lose))
385 (new (if (vop-prev lose)
386 (split-ir2-blocks 2block (vop-prev lose)
387 (incf counter))
388 2block)))
389 (coalesce-more-ltn-numbers new (vop-args lose)
390 (vop-info-arg-types info))
391 (coalesce-more-ltn-numbers new (vop-results lose)
392 (vop-info-result-types info))
393 (assert (not (find-local-references new)))
394 (init-global-conflict-kind new))))))))
395
396 (undefined-value))
397
398
399 ;;;; Flow analysis:
400
401 ;;; Propagate-Live-TNs -- Internal
402 ;;;
403 ;;; For each Global-TN in Block2 that is :Live, :Read or :Read-Only, ensure
404 ;;; that there is a corresponding Global-Conflict in Block1. If there is none,
405 ;;; make a :Live Global-Conflict. If there is a :Read-Only conflict, promote
406 ;;; it to :Live.
407 ;;;
408 ;;; If we did added a new conflict, return true, otherwise false. We don't
409 ;;; need to return true when we promote a :Read-Only conflict, since it doesn't
410 ;;; reveal any new information to predecessors of Block1.
411 ;;;
412 ;;; We use the Tn-Current-Conflict to walk through the global
413 ;;; conflicts. Since the global conflicts for a TN are ordered by block, we
414 ;;; can be sure that the Current-Conflict always points at or before the block
415 ;;; that we are looking at. This allows us to quickly determine if there is a
416 ;;; global conflict for a given TN in Block1.
417 ;;;
418 ;;; When we scan down the conflicts, we know that there must be at least one
419 ;;; conflict for TN, since we got our hands on TN by picking it out of a
420 ;;; conflict in Block2.
421 ;;;
422 ;;; We leave the Current-Conflict pointing to the conflict for Block1. The
423 ;;; Current-Conflict must be initialized to the head of the Global-Conflicts
424 ;;; for the TN between each flow analysis iteration.
425 ;;;
426 (defun propagate-live-tns (block1 block2)
427 (declare (type ir2-block block1 block2))
428 (let ((live-in (ir2-block-live-in block1))
429 (did-something nil))
430 (do ((conf2 (ir2-block-global-tns block2)
431 (global-conflicts-next conf2)))
432 ((null conf2))
433 (ecase (global-conflicts-kind conf2)
434 ((:live :read :read-only)
435 (let* ((tn (global-conflicts-tn conf2))
436 (tn-conflicts (tn-current-conflict tn))
437 (number1 (ir2-block-number block1)))
438 (assert tn-conflicts)
439 (do ((current tn-conflicts (global-conflicts-tn-next current))
440 (prev nil current))
441 ((or (null current)
442 (> (ir2-block-number (global-conflicts-block current))
443 number1))
444 (setf (tn-current-conflict tn) prev)
445 (add-global-conflict :live tn block1 nil)
446 (setq did-something t))
447 (when (eq (global-conflicts-block current) block1)
448 (case (global-conflicts-kind current)
449 (:live)
450 (:read-only
451 (setf (global-conflicts-kind current) :live)
452 (setf (svref (ir2-block-local-tns block1)
453 (global-conflicts-number current))
454 nil)
455 (setf (global-conflicts-number current) nil)
456 (setf (tn-current-conflict tn) current))
457 (t
458 (setf (sbit live-in (global-conflicts-number current)) 1)))
459 (return)))))
460 (:write)))
461 did-something))
462
463
464 ;;; Lifetime-Flow-Analysis -- Internal
465 ;;;
466 ;;; Do backward global flow analysis to find all TNs live at each block
467 ;;; boundary.
468 ;;;
469 (defun lifetime-flow-analysis (component)
470 (loop
471 (reset-current-conflict component)
472 (let ((did-something nil))
473 (do-blocks-backwards (block component)
474 (let* ((2block (block-info block))
475 (last (do ((b (ir2-block-next 2block) (ir2-block-next b))
476 (prev 2block b))
477 ((not (eq (ir2-block-block b) block))
478 prev))))
479
480 (dolist (b (block-succ block))
481 (when (and (block-lambda b)
482 (propagate-live-tns last (block-info b)))
483 (setq did-something t)))
484
485 (do ((b (ir2-block-prev last) (ir2-block-prev b))
486 (prev last b))
487 ((not (eq (ir2-block-block b) block)))
488 (when (propagate-live-tns b prev)
489 (setq did-something t)))))
490
491 (unless did-something (return))))
492
493 (undefined-value))
494
495
496 ;;;; Post-pass:
497
498 ;;; Convert-To-Environment-TN -- Internal
499 ;;;
500 ;;; If TN isn't already a :Environment TN, then make it into one. This
501 ;;; requires deleting the existing conflict info.
502 ;;;
503 (defun convert-to-environment-tn (tn)
504 (declare (type tn tn))
505 (ecase (tn-kind tn)
506 (:environment)
507 (:normal
508 (let ((confs (tn-global-conflicts tn)))
509 (if confs
510 (do ((conf confs (global-conflicts-tn-next conf)))
511 ((null conf))
512 (let ((block (global-conflicts-block conf)))
513 (unless (eq (global-conflicts-kind conf) :live)
514 (let ((ltns (ir2-block-local-tns block))
515 (num (global-conflicts-number conf)))
516 (assert (not (eq (svref ltns num) :more)))
517 (setf (svref ltns num) nil)))
518 (deletef-in global-conflicts-next (ir2-block-global-tns block)
519 conf)))
520 (setf (svref (ir2-block-local-tns (tn-local tn))
521 (tn-local-number tn))
522 nil))
523 (setf (tn-local tn) nil)
524 (setf (tn-local-number tn) nil)
525 (setf (tn-global-conflicts tn) nil)
526 (setf (tn-kind tn) :environment)
527 (push tn (ir2-environment-live-tns (tn-environment tn))))))
528 (undefined-value))
529
530
531 ;;; Note-Conflicts -- Internal
532 ;;;
533 ;;; Note that TN conflicts with all current live TNs. Num is TN's LTN
534 ;;; number. We bit-ior Live-Bits with TN's Local-Conflicts, and set TN's
535 ;;; number in the conflicts of all TNs in Live-List.
536 ;;;
537 (defun note-conflicts (live-bits live-list tn num)
538 (declare (type tn tn) (type (or tn null) live-list)
539 (type local-tn-bit-vector live-bits)
540 (type local-tn-number num))
541 (let ((lconf (tn-local-conflicts tn)))
542 (bit-ior live-bits lconf lconf))
543 (do ((live live-list (tn-next* live)))
544 ((null live))
545 (setf (sbit (tn-local-conflicts live) num) 1))
546 (undefined-value))
547
548
549 ;;; Compute-Save-Set -- Internal
550 ;;;
551 ;;; Compute a list of the TNs live after VOP that aren't results.
552 ;;;
553 (defun compute-save-set (vop block live-list)
554 (declare (type vop vop) (type ir2-block block) (type (or tn null) live-list))
555 (collect ((save))
556 (let ((results (vop-results vop)))
557 (do ((live live-list (tn-next* live)))
558 ((null live))
559 (unless (find-in #'tn-ref-across live results :key #'tn-ref-tn)
560 (save live))))
561 (do ((conf (ir2-block-global-tns block) (global-conflicts-next conf)))
562 ((null conf))
563 (when (eq (global-conflicts-kind conf) :live)
564 (save (global-conflicts-tn conf))))
565 (save)))
566
567
568 ;;; Compute-Initial-Conflicts -- Internal
569 ;;;
570 ;;; Return as values, a LTN bit-vector and a list (threaded by TN-Next*)
571 ;;; representing the TNs live at the end of Block (exclusive of :Live TNs).
572 ;;;
573 ;;; We iterate over the TNs in the global conflicts that are live at the block
574 ;;; end, setting up the TN-Local-Conflicts and TN-Local-Number, and adding the
575 ;;; TN to the live list.
576 ;;;
577 ;;; ### Note: we alias the global-conflicts-conflicts here as the
578 ;;; tn-local-conflicts.
579 ;;;
580 (defun compute-initial-conflicts (block)
581 (declare (type ir2-block block))
582 (let ((live-bits (copy-seq (ir2-block-live-in block)))
583 (live-list nil))
584
585 (do ((conf (ir2-block-global-tns block)
586 (global-conflicts-next conf)))
587 ((null conf))
588 (let ((bits (global-conflicts-conflicts conf))
589 (tn (global-conflicts-tn conf))
590 (num (global-conflicts-number conf)))
591 (setf (tn-local-number tn) num)
592 (unless (eq (global-conflicts-kind conf) :live)
593 (unless (zerop (sbit live-bits num))
594 (bit-vector-replace bits live-bits)
595 (setf (sbit bits num) 0)
596 (push-in tn-next* tn live-list))
597 (setf (tn-local-conflicts tn) bits))))
598
599 (values live-bits live-list)))
600
601
602 (eval-when (compile eval)
603
604 ;;; Frob-More-TNs -- Internal
605 ;;;
606 ;;; Used in the guts of Conflict-Analyze-1-Block to simultaneously do
607 ;;; something to all of the TNs referenced by a big more arg. We have to treat
608 ;;; these TNs specially, since when we set or clear the bit in the live TNs,
609 ;;; the represents a change in the liveness of all the more TNs. If we
610 ;;; iterated as normal, the next more ref would be thought to be not live when
611 ;;; it was, etc. We return true if there where more TNs.
612 ;;;
613 (defmacro frob-more-tns (action)
614 `(when (eq (svref ltns num) :more)
615 (do ((mref (tn-ref-next-ref ref) (tn-ref-next-ref mref)))
616 ((null mref))
617 (let ((mtn (tn-ref-tn mref)))
618 (unless (eql (tn-local-number mtn) num)
619 (return))
620 ,action))
621 t))
622
623 ); Eval-When (Compile Eval)
624
625
626 ;;; Conflict-Analyze-1-Block -- Internal
627 ;;;
628 ;;; Compute the block-local conflict information for Block. We iterate over
629 ;;; all the TN-Refs in a block in reference order, maintaining the set of live
630 ;;; TNs in both a list and a bit-vector representation.
631 ;;;
632 (defun conflict-analyze-1-block (block)
633 (declare (type ir2-block block))
634 (multiple-value-bind
635 (live-bits live-list)
636 (compute-initial-conflicts block)
637 (let ((ltns (ir2-block-local-tns block)))
638
639 (do ((vop (ir2-block-last-vop block)
640 (vop-prev vop)))
641 ((null vop))
642
643 (let ((save-p (vop-info-save-p (vop-info vop))))
644 (when save-p
645 (let ((save-set (compute-save-set vop block live-list)))
646 (ecase save-p
647 (:force-to-stack
648 (dolist (tn save-set)
649 (force-tn-to-stack tn)
650 (convert-to-environment-tn tn)))
651 ((t)
652 (setf (first (vop-codegen-info vop)) save-set))))))
653
654 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
655 ((null ref))
656 (let* ((tn (tn-ref-tn ref))
657 (num (tn-local-number tn)))
658
659 (cond
660 ((not num))
661 ((not (zerop (sbit live-bits num)))
662 (when (tn-ref-write-p ref)
663 (setf (sbit live-bits num) 0)
664 (deletef-in tn-next* live-list tn)
665 (when (frob-more-tns (deletef-in tn-next* live-list mtn))
666 (return))))
667 ((tn-ref-write-p ref)
668 (note-conflicts live-bits live-list tn num))
669 (t
670 (note-conflicts live-bits live-list tn num)
671 (frob-more-tns (note-conflicts live-bits live-list mtn num))
672 (setf (sbit live-bits num) 1)
673 (push-in tn-next* tn live-list)
674 (when (frob-more-tns (push-in tn-next* mtn live-list))
675 (return))))))))))
676
677
678 ;;; Lifetime-Post-Pass -- Internal
679 ;;;
680 ;;; Conflict analyze each block, and also add it
681 (defun lifetime-post-pass (component)
682 (declare (type component component))
683 (do-ir2-blocks (block component)
684 (conflict-analyze-1-block block)))
685
686
687 ;;; Lifetime-Analyze -- Interface
688 ;;;
689 ;;;
690 (defun lifetime-analyze (component)
691 (lifetime-pre-pass component)
692 (lifetime-flow-analysis component)
693 (lifetime-post-pass component))
694
695
696 ;;;; Conflict testing:
697
698 ;;; TNs-Conflict-Local-Global -- Internal
699 ;;;
700 ;;; Test for a conflict between the local TN X and the global TN Y. We just
701 ;;; look for a global conflict of Y in X's block, and then test for conflict in
702 ;;; that block.
703 ;;; [### Might be more efficient to scan Y's global conflicts. This depends on
704 ;;; whether there are more global TNs than blocks.]
705 ;;;
706 (defun tns-conflict-local-global (x y)
707 (let ((block (tn-local x)))
708 (do ((conf (ir2-block-global-tns block)
709 (global-conflicts-next conf)))
710 ((null conf) nil)
711 (when (eq (global-conflicts-tn conf) y)
712 (let ((num (global-conflicts-number conf)))
713 (return (or (not num)
714 (not (zerop (sbit (tn-local-conflicts x)
715 num))))))))))
716
717
718 ;;; TNs-Conflict-Global-Global -- Internal
719 ;;;
720 ;;; Test for conflict between two global TNs X and Y.
721 ;;;
722 (defun tns-conflict-global-global (x y)
723 (declare (type tn x y))
724 (let* ((x-conf (tn-global-conflicts x))
725 (x-num (ir2-block-number (global-conflicts-block x-conf)))
726 (y-conf (tn-global-conflicts y))
727 (y-num (ir2-block-number (global-conflicts-block y-conf))))
728
729 (macrolet ((advance (n c)
730 `(progn
731 (setq ,c (global-conflicts-tn-next ,c))
732 (unless ,c (return-from tns-conflict-global-global nil))
733 (setq ,n (ir2-block-number (global-conflicts-block ,c)))))
734 (scan (g l lc)
735 `(do ()
736 ((>= ,g ,l))
737 (advance ,l ,lc))))
738
739 (loop
740 ;; x-conf, y-conf true, x-num, y-num corresponding block numbers.
741 (scan x-num y-num y-conf)
742 (scan y-num x-num x-conf)
743 (when (= x-num y-num)
744 (let ((ltn-num-x (global-conflicts-number x-conf)))
745 (unless (and ltn-num-x
746 (global-conflicts-number y-conf)
747 (zerop (sbit (global-conflicts-conflicts y-conf)
748 ltn-num-x)))
749 (return t))
750 (advance x-num x-conf)
751 (advance y-num y-conf)))))))
752
753
754 ;;; TNs-Conflict-Environment-Global -- Interface
755 ;;;
756 ;;; Return true if any of Y's blocks are in X's environment.
757 ;;;
758 (defun tns-conflict-environment-global (x y)
759 (declare (type tn x y))
760 (let ((env (tn-environment x)))
761 (do ((conf (tn-global-conflicts y) (global-conflicts-tn-next conf)))
762 ((null conf)
763 nil)
764 (when (eq (environment-info
765 (lambda-environment
766 (block-lambda
767 (ir2-block-block (global-conflicts-block conf)))))
768 env)
769 (return t)))))
770
771
772 ;;; TNs-Conflict-Environment-Local -- Interface
773 ;;;
774 ;;; Return true if Y's block is in X's environment.
775 ;;;
776 (defun tns-conflict-environment-local (x y)
777 (declare (type tn x y))
778 (eq (environment-info
779 (lambda-environment
780 (block-lambda
781 (ir2-block-block (tn-local y)))))
782 (tn-environment x)))
783
784
785 ;;; TNs-Conflict -- Interface
786 ;;;
787 ;;; Return true if the lifetimes of X and Y overlap at any point.
788 ;;;
789 (defun tns-conflict (x y)
790 (declare (type tn x y))
791 (cond ((eq (tn-kind x) :environment)
792 (cond ((tn-global-conflicts y)
793 (tns-conflict-environment-global x y))
794 ((eq (tn-kind y) :environment)
795 (eq (tn-environment x) (tn-environment y)))
796 (t
797 (tns-conflict-environment-local x y))))
798 ((eq (tn-kind y) :environment)
799 (if (tn-global-conflicts x)
800 (tns-conflict-environment-global y x)
801 (tns-conflict-environment-local y x)))
802 ((tn-global-conflicts x)
803 (if (tn-global-conflicts y)
804 (tns-conflict-global-global x y)
805 (tns-conflict-local-global y x)))
806 ((tn-global-conflicts y)
807 (tns-conflict-local-global x y))
808 (t
809 (and (eq (tn-local x) (tn-local y))
810 (not (zerop (sbit (tn-local-conflicts x)
811 (tn-local-number y))))))))

  ViewVC Help
Powered by ViewVC 1.1.5