1 ;;;; This file contains utilities used for creating and manipulating
2 ;;;; TNs, and some other more assorted IR2 utilities.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;; The component that is currently being compiled. TNs are allocated
16 ;;; in this component.
17 (defvar *component-being-compiled*)
19 (defmacro do-packed-tns ((tn component &optional result) &body body)
21 "Do-Packed-TNs (TN-Var Component [Result]) Declaration* Form*
22 Iterate over all packed TNs allocated in Component."
23 (let ((n-component (gensym)))
24 `(let ((,n-component (component-info ,component)))
25 (do ((,tn (ir2-component-normal-tns ,n-component) (tn-next ,tn)))
28 (do ((,tn (ir2-component-restricted-tns ,n-component) (tn-next ,tn)))
31 (do ((,tn (ir2-component-wired-tns ,n-component) (tn-next ,tn)))
36 ;;; Remove all TNs with no references from the lists of unpacked TNs. We
37 ;;; null out the Offset so that nobody will mistake deleted wired TNs for
38 ;;; properly packed TNs. We mark non-deleted alias TNs so that aliased TNs
39 ;;; aren't considered to be unreferenced.
40 (defun delete-unreferenced-tns (component)
41 (let* ((2comp (component-info component))
42 (aliases (make-array (1+ (ir2-component-global-tn-counter 2comp))
43 :element-type 'bit :initial-element 0)))
44 (labels ((delete-some (getter setter)
46 (do ((tn (funcall getter 2comp) (tn-next tn)))
50 (and (eq (tn-kind tn) :specified-save)
51 (used-p (tn-save-tn tn))))
54 (delete-1 tn prev setter))))))
56 (or (tn-reads tn) (tn-writes tn)
57 (member (tn-kind tn) '(:component :environment))
58 (not (zerop (sbit aliases (tn-number tn))))))
59 (delete-1 (tn prev setter)
61 (setf (tn-next prev) (tn-next tn))
62 (funcall setter (tn-next tn) 2comp))
63 (setf (tn-offset tn) nil)
67 #'ir2-environment-live-tns
68 #'(setf ir2-environment-live-tns)))
71 #'ir2-environment-debug-live-tns
72 #'(setf ir2-environment-debug-live-tns)))))
73 (clear-live (tn getter setter)
74 (let ((env (environment-info (tn-environment tn))))
75 (funcall setter (delete tn (funcall getter env)) env))))
76 (declare (inline used-p delete-some delete-1 clear-live))
77 (delete-some #'ir2-component-alias-tns
78 #'(setf ir2-component-alias-tns))
79 (do ((tn (ir2-component-alias-tns 2comp) (tn-next tn)))
81 (setf (sbit aliases (tn-number (tn-save-tn tn))) 1))
82 (delete-some #'ir2-component-normal-tns
83 #'(setf ir2-component-normal-tns))
84 (delete-some #'ir2-component-restricted-tns
85 #'(setf ir2-component-restricted-tns))
86 (delete-some #'ir2-component-wired-tns
87 #'(setf ir2-component-wired-tns))))
92 ;;; Create a packed TN of the specified primitive-type in the
93 ;;; *COMPONENT-BEING-COMPILED*. We use the SCs from the primitive type
94 ;;; to determine which SCs it can be packed in.
95 (defun make-normal-tn (type)
96 (declare (type primitive-type type))
97 (let* ((component (component-info *component-being-compiled*))
98 (res (make-tn (incf (ir2-component-global-tn-counter component))
100 (push-in tn-next res (ir2-component-normal-tns component))
103 ;;; Create a normal packed TN with representation indicated by SCN.
104 (defun make-representation-tn (ptype scn)
105 (declare (type primitive-type ptype) (type sc-number scn))
106 (let* ((component (component-info *component-being-compiled*))
107 (res (make-tn (incf (ir2-component-global-tn-counter component))
109 (svref *backend-sc-numbers* scn))))
110 (push-in tn-next res (ir2-component-normal-tns component))
113 ;;; Create a TN wired to a particular location in an SC. We set the Offset
114 ;;; and FSC to record where it goes, and then put it on the current component's
115 ;;; Wired-TNs list. Ptype is the TN's primitive-type, which may be NIL in VOP
117 (defun make-wired-tn (ptype scn offset)
118 (declare (type (or primitive-type null) ptype)
119 (type sc-number scn) (type unsigned-byte offset))
120 (let* ((component (component-info *component-being-compiled*))
121 (res (make-tn (incf (ir2-component-global-tn-counter component))
123 (svref *backend-sc-numbers* scn))))
124 (setf (tn-offset res) offset)
125 (push-in tn-next res (ir2-component-wired-tns component))
128 ;;; Create a packed TN restricted to the SC with number SCN. Ptype is as
129 ;;; for MAKE-WIRED-TN.
130 (defun make-restricted-tn (ptype scn)
131 (declare (type (or primitive-type null) ptype) (type sc-number scn))
132 (let* ((component (component-info *component-being-compiled*))
133 (res (make-tn (incf (ir2-component-global-tn-counter component))
135 (svref *backend-sc-numbers* scn))))
136 (push-in tn-next res (ir2-component-restricted-tns component))
139 ;;; Make TN be live throughout environment. Return TN. In the DEBUG case,
140 ;;; the TN is treated normally in blocks in the environment which reference the
141 ;;; TN, allowing targeting to/from the TN. This results in move efficient
142 ;;; code, but may result in the TN sometimes not being live when you want it.
143 (defun environment-live-tn (tn env)
144 (declare (type tn tn) (type environment env))
145 (aver (eq (tn-kind tn) :normal))
146 (setf (tn-kind tn) :environment)
147 (setf (tn-environment tn) env)
148 (push tn (ir2-environment-live-tns (environment-info env)))
150 (defun environment-debug-live-tn (tn env)
151 (declare (type tn tn) (type environment env))
152 (aver (eq (tn-kind tn) :normal))
153 (setf (tn-kind tn) :debug-environment)
154 (setf (tn-environment tn) env)
155 (push tn (ir2-environment-debug-live-tns (environment-info env)))
158 ;;; Make TN be live throughout the current component. Return TN.
159 (defun component-live-tn (tn)
160 (declare (type tn tn))
161 (aver (eq (tn-kind tn) :normal))
162 (setf (tn-kind tn) :component)
163 (push tn (ir2-component-component-tns (component-info
164 *component-being-compiled*)))
167 ;;; Specify that Save be used as the save location for TN. TN is returned.
168 (defun specify-save-tn (tn save)
169 (declare (type tn tn save))
170 (aver (eq (tn-kind save) :normal))
171 (aver (and (not (tn-save-tn tn)) (not (tn-save-tn save))))
172 (setf (tn-kind save) :specified-save)
173 (setf (tn-save-tn tn) save)
174 (setf (tn-save-tn save) tn)
176 (ir2-component-specified-save-tns
177 (component-info *component-being-compiled*)))
180 ;;; Create a constant TN. The implementation dependent
181 ;;; Immediate-Constant-SC function is used to determine whether the constant
182 ;;; has an immediate representation.
183 (defun make-constant-tn (constant)
184 (declare (type constant constant))
185 (let* ((component (component-info *component-being-compiled*))
186 (immed (immediate-constant-sc (constant-value constant)))
187 (sc (svref *backend-sc-numbers*
188 (or immed (sc-number-or-lose 'constant))))
189 (res (make-tn 0 :constant (primitive-type (leaf-type constant)) sc)))
191 (let ((constants (ir2-component-constants component)))
192 (setf (tn-offset res) (fill-pointer constants))
193 (vector-push-extend constant constants)))
194 (push-in tn-next res (ir2-component-constant-tns component))
195 (setf (tn-leaf res) constant)
198 (defun make-load-time-value-tn (handle type)
199 (let* ((component (component-info *component-being-compiled*))
200 (sc (svref *backend-sc-numbers*
201 (sc-number-or-lose 'constant)))
202 (res (make-tn 0 :constant (primitive-type type) sc))
203 (constants (ir2-component-constants component)))
204 (setf (tn-offset res) (fill-pointer constants))
205 (vector-push-extend (cons :load-time-value handle) constants)
206 (push-in tn-next res (ir2-component-constant-tns component))
209 ;;; Make a TN that aliases TN for use in local call argument passing.
210 (defun make-alias-tn (tn)
211 (declare (type tn tn))
212 (let* ((component (component-info *component-being-compiled*))
213 (res (make-tn (incf (ir2-component-global-tn-counter component))
214 :alias (tn-primitive-type tn) nil)))
215 (setf (tn-save-tn res) tn)
217 (ir2-component-alias-tns component))
220 ;;; Return a load-time constant TN with the specified Kind and Info. If the
221 ;;; desired Constants entry already exists, then reuse it, otherwise allocate a
222 ;;; new load-time constant slot.
223 (defun make-load-time-constant-tn (kind info)
224 (declare (type keyword kind))
225 (let* ((component (component-info *component-being-compiled*))
228 *backend-t-primitive-type*
229 (svref *backend-sc-numbers*
230 (sc-number-or-lose 'constant))))
231 (constants (ir2-component-constants component)))
234 ((= i (length constants))
235 (setf (tn-offset res) i)
236 (vector-push-extend (cons kind info) constants))
237 (let ((entry (aref constants i)))
238 (when (and (consp entry)
239 (eq (car entry) kind)
240 (or (eq (cdr entry) info)
242 (equal (cdr entry) info))))
243 (setf (tn-offset res) i)
246 (push-in tn-next res (ir2-component-constant-tns component))
251 ;;; Make a TN-Ref that references TN and return it. Write-P should be true
252 ;;; if this is a write reference, otherwise false. All we do other than
253 ;;; calling the constructor is add the reference to the TN's references.
254 (defun reference-tn (tn write-p)
255 (declare (type tn tn) (type boolean write-p))
256 (let ((res (make-tn-ref tn write-p)))
258 (push-in tn-ref-next res (tn-writes tn))
259 (push-in tn-ref-next res (tn-reads tn)))
262 ;;; Make TN-Refs to reference each TN in TNs, linked together by
263 ;;; TN-Ref-Across. Write-P is the Write-P value for the refs. More is
264 ;;; stuck in the TN-Ref-Across of the ref for the last TN, or returned as the
265 ;;; result if there are no TNs.
266 (defun reference-tn-list (tns write-p &optional more)
267 (declare (list tns) (type boolean write-p) (type (or tn-ref null) more))
269 (let* ((first (reference-tn (first tns) write-p))
271 (dolist (tn (rest tns))
272 (let ((res (reference-tn tn write-p)))
273 (setf (tn-ref-across prev) res)
275 (setf (tn-ref-across prev) more)
279 ;;; Remove Ref from the references for its associated TN.
280 (defun delete-tn-ref (ref)
281 (declare (type tn-ref ref))
282 (if (tn-ref-write-p ref)
283 (deletef-in tn-ref-next (tn-writes (tn-ref-tn ref)) ref)
284 (deletef-in tn-ref-next (tn-reads (tn-ref-tn ref)) ref))
287 ;;; Do stuff to change the TN referenced by Ref. We remove Ref from its
288 ;;; old TN's refs, add ref to TN's refs, and set the TN-Ref-TN.
289 (defun change-tn-ref-tn (ref tn)
290 (declare (type tn-ref ref) (type tn tn))
292 (setf (tn-ref-tn ref) tn)
293 (if (tn-ref-write-p ref)
294 (push-in tn-ref-next ref (tn-writes tn))
295 (push-in tn-ref-next ref (tn-reads tn)))
298 ;;;; miscellaneous utilities
300 ;;; Emit a move-like template determined at run-time, with X as the argument
301 ;;; and Y as the result. Useful for move, coerce and type-check templates. If
302 ;;; supplied, then insert before VOP, otherwise insert at then end of the
303 ;;; block. Returns the last VOP inserted.
304 (defun emit-move-template (node block template x y &optional before)
305 (declare (type node node) (type ir2-block block)
306 (type template template) (type tn x y))
307 (let ((arg (reference-tn x nil))
308 (result (reference-tn y t)))
309 (multiple-value-bind (first last)
310 (funcall (template-emit-function template) node block template arg
312 (insert-vop-sequence first last block before)
315 ;;; Like EMIT-MOVE-TEMPLATE, except that we pass in Info args too.
316 (defun emit-load-template (node block template x y info &optional before)
317 (declare (type node node) (type ir2-block block)
318 (type template template) (type tn x y))
319 (let ((arg (reference-tn x nil))
320 (result (reference-tn y t)))
321 (multiple-value-bind (first last)
322 (funcall (template-emit-function template) node block template arg
324 (insert-vop-sequence first last block before)
327 ;;; Like EMIT-MOVE-TEMPLATE, except that the VOP takes two args.
328 (defun emit-move-arg-template (node block template x f y &optional before)
329 (declare (type node node) (type ir2-block block)
330 (type template template) (type tn x f y))
331 (let ((x-ref (reference-tn x nil))
332 (f-ref (reference-tn f nil))
333 (y-ref (reference-tn y t)))
334 (setf (tn-ref-across x-ref) f-ref)
335 (multiple-value-bind (first last)
336 (funcall (template-emit-function template) node block template x-ref
338 (insert-vop-sequence first last block before)
341 ;;; Like EMIT-MOVE-TEMPLATE, except that the VOP takes no args.
342 (defun emit-context-template (node block template y &optional before)
343 (declare (type node node) (type ir2-block block)
344 (type template template) (type tn y))
345 (let ((y-ref (reference-tn y t)))
346 (multiple-value-bind (first last)
347 (funcall (template-emit-function template) node block template nil
349 (insert-vop-sequence first last block before)
352 ;;; Return the label marking the start of Block, assigning one if necessary.
353 (defun block-label (block)
354 (declare (type cblock block))
355 (let ((2block (block-info block)))
356 (or (ir2-block-%label 2block)
357 (setf (ir2-block-%label 2block) (gen-label)))))
359 ;;; Return true if Block is emitted immediately after the block ended by Node.
360 (defun drop-thru-p (node block)
361 (declare (type node node) (type cblock block))
362 (let ((next-block (ir2-block-next (block-info (node-block node)))))
363 (aver (eq node (block-last (node-block node))))
364 (eq next-block (block-info block))))
366 ;;; Link a list of VOPs from First to Last into Block, Before the specified
367 ;;; VOP. If Before is NIL, insert at the end.
368 (defun insert-vop-sequence (first last block before)
369 (declare (type vop first last) (type ir2-block block)
370 (type (or vop null) before))
372 (let ((prev (vop-prev before)))
373 (setf (vop-prev first) prev)
375 (setf (vop-next prev) first)
376 (setf (ir2-block-start-vop block) first))
377 (setf (vop-next last) before)
378 (setf (vop-prev before) last))
379 (let ((current (ir2-block-last-vop block)))
380 (setf (vop-prev first) current)
381 (setf (ir2-block-last-vop block) last)
383 (setf (vop-next current) first)
384 (setf (ir2-block-start-vop block) first))))
387 ;;; Delete all of the TN-Refs associated with VOP and remove VOP from the IR2.
388 (defun delete-vop (vop)
389 (declare (type vop vop))
390 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
394 (let ((prev (vop-prev vop))
395 (next (vop-next vop))
396 (block (vop-block vop)))
398 (setf (vop-next prev) next)
399 (setf (ir2-block-start-vop block) next))
401 (setf (vop-prev next) prev)
402 (setf (ir2-block-last-vop block) prev)))
406 ;;; Return a list of N normal TNs of the specified primitive type.
407 (defun make-n-tns (n ptype)
408 (declare (type unsigned-byte n) (type primitive-type ptype))
411 (res (make-normal-tn ptype)))
414 ;;; Return true if X and Y are packed in the same location, false otherwise.
415 ;;; This is false if either operand is constant.
416 (defun location= (x y)
417 (declare (type tn x y))
418 (and (eq (sc-sb (tn-sc x)) (sc-sb (tn-sc y)))
419 (eql (tn-offset x) (tn-offset y))
420 (not (or (eq (tn-kind x) :constant)
421 (eq (tn-kind y) :constant)))))
423 ;;; Return the value of an immediate constant TN.
425 (declare (type tn tn))
426 (aver (member (tn-kind tn) '(:constant :cached-constant)))
427 (constant-value (tn-leaf tn)))
429 ;;; Force TN to be allocated in a SC that doesn't need to be saved: an
430 ;;; unbounded non-save-p SC. We don't actually make it a real "restricted" TN,
431 ;;; but since we change the SC to an unbounded one, we should always succeed in
432 ;;; packing it in that SC.
433 (defun force-tn-to-stack (tn)
434 (declare (type tn tn))
435 (let ((sc (tn-sc tn)))
436 (unless (and (not (sc-save-p sc))
437 (eq (sb-kind (sc-sb sc)) :unbounded))
438 (dolist (alt (sc-alternate-scs sc)
439 (error "SC ~S has no :unbounded :save-p NIL alternate SC."
441 (when (and (not (sc-save-p alt))
442 (eq (sb-kind (sc-sb alt)) :unbounded))
443 (setf (tn-sc tn) alt)