1 ;;;; transforms and other stuff used to compile ALIEN operations
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 (defknown %sap-alien (system-area-pointer alien-type) alien-value
18 (defknown alien-sap (alien-value) system-area-pointer
21 (defknown slot (alien-value symbol) t
22 (flushable recursive))
23 (defknown %set-slot (alien-value symbol t) t
25 (defknown %slot-addr (alien-value symbol) (alien (* t))
26 (flushable movable recursive))
28 (defknown deref (alien-value &rest index) t
30 (defknown %set-deref (alien-value t &rest index) t
32 (defknown %deref-addr (alien-value &rest index) (alien (* t))
35 (defknown %heap-alien (heap-alien-info) t
37 (defknown %set-heap-alien (heap-alien-info t) t
39 (defknown %heap-alien-addr (heap-alien-info) (alien (* t))
42 (defknown make-local-alien (local-alien-info) t
44 (defknown note-local-alien-type (local-alien-info t) null
46 (defknown local-alien (local-alien-info t) t
48 (defknown %local-alien-forced-to-memory-p (local-alien-info) (member t nil)
50 (defknown %set-local-alien (local-alien-info t t) t
52 (defknown %local-alien-addr (local-alien-info t) (alien (* t))
54 (defknown dispose-local-alien (local-alien-info t) t
57 (defknown %cast (alien-value alien-type) alien
60 (defknown naturalize (t alien-type) alien
62 (defknown deport (alien alien-type) t
64 (defknown extract-alien-value (system-area-pointer index alien-type) t
66 (defknown deposit-alien-value (system-area-pointer index alien-type t) t
69 (defknown alien-funcall (alien-value &rest *) *
72 ;;;; cosmetic transforms
74 (deftransform slot ((object slot)
75 ((alien (* t)) symbol))
76 '(slot (deref object) slot))
78 (deftransform %set-slot ((object slot value)
79 ((alien (* t)) symbol t))
80 '(%set-slot (deref object) slot value))
82 (deftransform %slot-addr ((object slot)
83 ((alien (* t)) symbol))
84 '(%slot-addr (deref object) slot))
88 (defun find-slot-offset-and-type (alien slot)
89 (unless (constant-lvar-p slot)
90 (give-up-ir1-transform
91 "The slot is not constant, so access cannot be open coded."))
92 (let ((type (lvar-type alien)))
93 (unless (alien-type-type-p type)
94 (give-up-ir1-transform))
95 (let ((alien-type (alien-type-type-alien-type type)))
96 (unless (alien-record-type-p alien-type)
97 (give-up-ir1-transform))
98 (let* ((slot-name (lvar-value slot))
99 (field (find slot-name (alien-record-type-fields alien-type)
100 :key #'alien-record-field-name)))
102 (abort-ir1-transform "~S doesn't have a slot named ~S"
105 (values (alien-record-field-offset field)
106 (alien-record-field-type field))))))
108 #+nil ;; Shouldn't be necessary.
109 (defoptimizer (slot derive-type) ((alien slot))
111 (catch 'give-up-ir1-transform
112 (multiple-value-bind (slot-offset slot-type)
113 (find-slot-offset-and-type alien slot)
114 (declare (ignore slot-offset))
115 (return (make-alien-type-type slot-type))))
118 (deftransform slot ((alien slot) * * :important t)
119 (multiple-value-bind (slot-offset slot-type)
120 (find-slot-offset-and-type alien slot)
121 `(extract-alien-value (alien-sap alien)
125 #+nil ;; ### But what about coercions?
126 (defoptimizer (%set-slot derive-type) ((alien slot value))
128 (catch 'give-up-ir1-transform
129 (multiple-value-bind (slot-offset slot-type)
130 (find-slot-offset-and-type alien slot)
131 (declare (ignore slot-offset))
132 (let ((type (make-alien-type-type slot-type)))
133 (assert-lvar-type value type)
137 (deftransform %set-slot ((alien slot value) * * :important t)
138 (multiple-value-bind (slot-offset slot-type)
139 (find-slot-offset-and-type alien slot)
140 `(deposit-alien-value (alien-sap alien)
145 (defoptimizer (%slot-addr derive-type) ((alien slot))
147 (catch 'give-up-ir1-transform
148 (multiple-value-bind (slot-offset slot-type)
149 (find-slot-offset-and-type alien slot)
150 (declare (ignore slot-offset))
151 (return (make-alien-type-type
152 (make-alien-pointer-type :to slot-type)))))
155 (deftransform %slot-addr ((alien slot) * * :important t)
156 (multiple-value-bind (slot-offset slot-type)
157 (find-slot-offset-and-type alien slot)
158 (/noshow "in DEFTRANSFORM %SLOT-ADDR, creating %SAP-ALIEN")
159 `(%sap-alien (sap+ (alien-sap alien) (/ ,slot-offset sb!vm:n-byte-bits))
160 ',(make-alien-pointer-type :to slot-type))))
164 (defun find-deref-alien-type (alien)
165 (let ((alien-type (lvar-type alien)))
166 (unless (alien-type-type-p alien-type)
167 (give-up-ir1-transform))
168 (let ((alien-type (alien-type-type-alien-type alien-type)))
169 (if (alien-type-p alien-type)
171 (give-up-ir1-transform)))))
173 (defun find-deref-element-type (alien)
174 (let ((alien-type (find-deref-alien-type alien)))
177 (alien-pointer-type-to alien-type))
179 (alien-array-type-element-type alien-type))
181 (give-up-ir1-transform)))))
183 (defun compute-deref-guts (alien indices)
184 (let ((alien-type (find-deref-alien-type alien)))
188 (abort-ir1-transform "too many indices for pointer deref: ~W"
190 (let ((element-type (alien-pointer-type-to alien-type)))
192 (let ((bits (alien-type-bits element-type))
193 (alignment (alien-type-alignment element-type)))
195 (abort-ir1-transform "unknown element size"))
197 (abort-ir1-transform "unknown element alignment"))
200 ,(align-offset bits alignment))
202 (values nil 0 element-type))))
204 (let* ((element-type (alien-array-type-element-type alien-type))
205 (bits (alien-type-bits element-type))
206 (alignment (alien-type-alignment element-type))
207 (dims (alien-array-type-dimensions alien-type)))
208 (unless (= (length indices) (length dims))
209 (give-up-ir1-transform "incorrect number of indices"))
211 (give-up-ir1-transform "Element size is unknown."))
213 (give-up-ir1-transform "Element alignment is unknown."))
215 (values nil 0 element-type)
216 (let* ((arg (gensym))
219 (dolist (dim (cdr dims))
220 (let ((arg (gensym)))
222 (setf offsetexpr `(+ (* ,offsetexpr ,dim) ,arg))))
223 (values (reverse args)
225 ,(align-offset bits alignment))
228 (abort-ir1-transform "~S not either a pointer or array type."
231 #+nil ;; Shouldn't be necessary.
232 (defoptimizer (deref derive-type) ((alien &rest noise))
233 (declare (ignore noise))
235 (catch 'give-up-ir1-transform
236 (return (make-alien-type-type (find-deref-element-type alien))))
239 (deftransform deref ((alien &rest indices) * * :important t)
240 (multiple-value-bind (indices-args offset-expr element-type)
241 (compute-deref-guts alien indices)
242 `(lambda (alien ,@indices-args)
243 (extract-alien-value (alien-sap alien)
247 #+nil ;; ### Again, the value might be coerced.
248 (defoptimizer (%set-deref derive-type) ((alien value &rest noise))
249 (declare (ignore noise))
251 (catch 'give-up-ir1-transform
252 (let ((type (make-alien-type-type
253 (make-alien-pointer-type
254 :to (find-deref-element-type alien)))))
255 (assert-lvar-type value type)
259 (deftransform %set-deref ((alien value &rest indices) * * :important t)
260 (multiple-value-bind (indices-args offset-expr element-type)
261 (compute-deref-guts alien indices)
262 `(lambda (alien value ,@indices-args)
263 (deposit-alien-value (alien-sap alien)
268 (defoptimizer (%deref-addr derive-type) ((alien &rest noise))
269 (declare (ignore noise))
271 (catch 'give-up-ir1-transform
272 (return (make-alien-type-type
273 (make-alien-pointer-type
274 :to (find-deref-element-type alien)))))
277 (deftransform %deref-addr ((alien &rest indices) * * :important t)
278 (multiple-value-bind (indices-args offset-expr element-type)
279 (compute-deref-guts alien indices)
280 (/noshow "in DEFTRANSFORM %DEREF-ADDR, creating (LAMBDA .. %SAP-ALIEN)")
281 `(lambda (alien ,@indices-args)
282 (%sap-alien (sap+ (alien-sap alien) (/ ,offset-expr sb!vm:n-byte-bits))
283 ',(make-alien-pointer-type :to element-type)))))
285 ;;;; support for aliens on the heap
287 (defun heap-alien-sap-and-type (info)
288 (unless (constant-lvar-p info)
289 (give-up-ir1-transform "info not constant; can't open code"))
290 (let ((info (lvar-value info)))
291 (values (heap-alien-info-sap-form info)
292 (heap-alien-info-type info))))
294 #+nil ; shouldn't be necessary
295 (defoptimizer (%heap-alien derive-type) ((info))
298 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
299 (declare (ignore sap))
300 (return (make-alien-type-type type))))
303 (deftransform %heap-alien ((info) * * :important t)
304 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
305 `(extract-alien-value ,sap 0 ',type)))
307 #+nil ;; ### Again, deposit value might change the type.
308 (defoptimizer (%set-heap-alien derive-type) ((info value))
310 (catch 'give-up-ir1-transform
311 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
312 (declare (ignore sap))
313 (let ((type (make-alien-type-type type)))
314 (assert-lvar-type value type)
318 (deftransform %set-heap-alien ((info value) (heap-alien-info *) * :important t)
319 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
320 `(deposit-alien-value ,sap 0 ',type value)))
322 (defoptimizer (%heap-alien-addr derive-type) ((info))
324 (catch 'give-up-ir1-transform
325 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
326 (declare (ignore sap))
327 (return (make-alien-type-type (make-alien-pointer-type :to type)))))
330 (deftransform %heap-alien-addr ((info) * * :important t)
331 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
332 (/noshow "in DEFTRANSFORM %HEAP-ALIEN-ADDR, creating %SAP-ALIEN")
333 `(%sap-alien ,sap ',type)))
335 ;;;; support for local (stack or register) aliens
337 (deftransform make-local-alien ((info) * * :important t)
338 (unless (constant-lvar-p info)
339 (abort-ir1-transform "Local alien info isn't constant?"))
340 (let* ((info (lvar-value info))
341 (alien-type (local-alien-info-type info))
342 (bits (alien-type-bits alien-type)))
344 (abort-ir1-transform "unknown size: ~S" (unparse-alien-type alien-type)))
345 (/noshow "in DEFTRANSFORM MAKE-LOCAL-ALIEN" info)
346 (/noshow (local-alien-info-force-to-memory-p info))
347 (/noshow alien-type (unparse-alien-type alien-type) (alien-type-bits alien-type))
348 (if (local-alien-info-force-to-memory-p info)
349 #!+x86 `(truly-the system-area-pointer
350 (%primitive alloc-alien-stack-space
351 ,(ceiling (alien-type-bits alien-type)
353 #!-x86 `(truly-the system-area-pointer
354 (%primitive alloc-number-stack-space
355 ,(ceiling (alien-type-bits alien-type)
357 (let* ((alien-rep-type-spec (compute-alien-rep-type alien-type))
358 (alien-rep-type (specifier-type alien-rep-type-spec)))
359 (cond ((csubtypep (specifier-type 'system-area-pointer)
362 ((ctypep 0 alien-rep-type) 0)
363 ((ctypep 0.0f0 alien-rep-type) 0.0f0)
364 ((ctypep 0.0d0 alien-rep-type) 0.0d0)
367 "Aliens of type ~S cannot be represented immediately."
368 (unparse-alien-type alien-type))))))))
370 (deftransform note-local-alien-type ((info var) * * :important t)
371 ;; FIXME: This test and error occur about a zillion times. They
372 ;; could be factored into a function.
373 (unless (constant-lvar-p info)
374 (abort-ir1-transform "Local alien info isn't constant?"))
375 (let ((info (lvar-value info)))
376 (/noshow "in DEFTRANSFORM NOTE-LOCAL-ALIEN-TYPE" info)
377 (/noshow (local-alien-info-force-to-memory-p info))
378 (unless (local-alien-info-force-to-memory-p info)
379 (let ((var-node (lvar-uses var)))
380 (/noshow var-node (ref-p var-node))
381 (when (ref-p var-node)
382 (propagate-to-refs (ref-leaf var-node)
384 (compute-alien-rep-type
385 (local-alien-info-type info))))))))
388 (deftransform local-alien ((info var) * * :important t)
389 (unless (constant-lvar-p info)
390 (abort-ir1-transform "Local alien info isn't constant?"))
391 (let* ((info (lvar-value info))
392 (alien-type (local-alien-info-type info)))
393 (/noshow "in DEFTRANSFORM LOCAL-ALIEN" info alien-type)
394 (/noshow (local-alien-info-force-to-memory-p info))
395 (if (local-alien-info-force-to-memory-p info)
396 `(extract-alien-value var 0 ',alien-type)
397 `(naturalize var ',alien-type))))
399 (deftransform %local-alien-forced-to-memory-p ((info) * * :important t)
400 (unless (constant-lvar-p info)
401 (abort-ir1-transform "Local alien info isn't constant?"))
402 (let ((info (lvar-value info)))
403 (local-alien-info-force-to-memory-p info)))
405 (deftransform %set-local-alien ((info var value) * * :important t)
406 (unless (constant-lvar-p info)
407 (abort-ir1-transform "Local alien info isn't constant?"))
408 (let* ((info (lvar-value info))
409 (alien-type (local-alien-info-type info)))
410 (if (local-alien-info-force-to-memory-p info)
411 `(deposit-alien-value var 0 ',alien-type value)
412 '(error "This should be eliminated as dead code."))))
414 (defoptimizer (%local-alien-addr derive-type) ((info var))
415 (if (constant-lvar-p info)
416 (let* ((info (lvar-value info))
417 (alien-type (local-alien-info-type info)))
418 (make-alien-type-type (make-alien-pointer-type :to alien-type)))
421 (deftransform %local-alien-addr ((info var) * * :important t)
422 (unless (constant-lvar-p info)
423 (abort-ir1-transform "Local alien info isn't constant?"))
424 (let* ((info (lvar-value info))
425 (alien-type (local-alien-info-type info)))
426 (/noshow "in DEFTRANSFORM %LOCAL-ALIEN-ADDR, creating %SAP-ALIEN")
427 (if (local-alien-info-force-to-memory-p info)
428 `(%sap-alien var ',(make-alien-pointer-type :to alien-type))
429 (error "This shouldn't happen."))))
431 (deftransform dispose-local-alien ((info var) * * :important t)
432 (unless (constant-lvar-p info)
433 (abort-ir1-transform "Local alien info isn't constant?"))
434 (let* ((info (lvar-value info))
435 (alien-type (local-alien-info-type info)))
436 (if (local-alien-info-force-to-memory-p info)
437 #!+x86 `(%primitive dealloc-alien-stack-space
438 ,(ceiling (alien-type-bits alien-type)
440 #!-x86 `(%primitive dealloc-number-stack-space
441 ,(ceiling (alien-type-bits alien-type)
447 (defoptimizer (%cast derive-type) ((alien type))
448 (or (when (constant-lvar-p type)
449 (let ((alien-type (lvar-value type)))
450 (when (alien-type-p alien-type)
451 (make-alien-type-type alien-type))))
454 (deftransform %cast ((alien target-type) * * :important t)
455 (unless (constant-lvar-p target-type)
456 (give-up-ir1-transform
457 "The alien type is not constant, so access cannot be open coded."))
458 (let ((target-type (lvar-value target-type)))
459 (cond ((or (alien-pointer-type-p target-type)
460 (alien-array-type-p target-type)
461 (alien-fun-type-p target-type))
462 `(naturalize (alien-sap alien) ',target-type))
464 (abort-ir1-transform "cannot cast to alien type ~S" target-type)))))
466 ;;;; ALIEN-SAP, %SAP-ALIEN, %ADDR, etc.
468 (deftransform alien-sap ((alien) * * :important t)
469 (let ((alien-node (lvar-uses alien)))
472 (extract-fun-args alien '%sap-alien 2)
474 (declare (ignore type))
477 (give-up-ir1-transform)))))
479 (defoptimizer (%sap-alien derive-type) ((sap type))
480 (declare (ignore sap))
481 (if (constant-lvar-p type)
482 (make-alien-type-type (lvar-value type))
485 (deftransform %sap-alien ((sap type) * * :important t)
486 (give-up-ir1-transform
487 ;; FIXME: The hardcoded newline here causes more-than-usually
488 ;; screwed-up formatting of the optimization note output.
489 "could not optimize away %SAP-ALIEN: forced to do runtime ~@
490 allocation of alien-value structure"))
492 ;;;; NATURALIZE/DEPORT/EXTRACT/DEPOSIT magic
494 (flet ((%computed-lambda (compute-lambda type)
495 (declare (type function compute-lambda))
496 (unless (constant-lvar-p type)
497 (give-up-ir1-transform
498 "The type is not constant at compile time; can't open code."))
500 (let ((result (funcall compute-lambda (lvar-value type))))
501 (/noshow "in %COMPUTED-LAMBDA" (lvar-value type) result)
504 (compiler-error "~A" condition)))))
505 (deftransform naturalize ((object type) * * :important t)
506 (%computed-lambda #'compute-naturalize-lambda type))
507 (deftransform deport ((alien type) * * :important t)
508 (%computed-lambda #'compute-deport-lambda type))
509 (deftransform extract-alien-value ((sap offset type) * * :important t)
510 (%computed-lambda #'compute-extract-lambda type))
511 (deftransform deposit-alien-value ((sap offset type value) * * :important t)
512 (%computed-lambda #'compute-deposit-lambda type)))
514 ;;;; a hack to clean up divisions
516 (defun count-low-order-zeros (thing)
519 (if (constant-lvar-p thing)
520 (count-low-order-zeros (lvar-value thing))
521 (count-low-order-zeros (lvar-uses thing))))
523 (case (lvar-fun-name (combination-fun thing))
525 (let ((min most-positive-fixnum)
526 (itype (specifier-type 'integer)))
527 (dolist (arg (combination-args thing) min)
528 (if (csubtypep (lvar-type arg) itype)
529 (setf min (min min (count-low-order-zeros arg)))
533 (itype (specifier-type 'integer)))
534 (dolist (arg (combination-args thing) result)
535 (if (csubtypep (lvar-type arg) itype)
536 (setf result (+ result (count-low-order-zeros arg)))
539 (let ((args (combination-args thing)))
540 (if (= (length args) 2)
541 (let ((amount (second args)))
542 (if (constant-lvar-p amount)
543 (max (+ (count-low-order-zeros (first args))
553 (do ((result 0 (1+ result))
554 (num thing (ash num -1)))
555 ((logbitp 0 num) result))))
559 (deftransform / ((numerator denominator) (integer integer))
560 (unless (constant-lvar-p denominator)
561 (give-up-ir1-transform))
562 (let* ((denominator (lvar-value denominator))
563 (bits (1- (integer-length denominator))))
564 (unless (= (ash 1 bits) denominator)
565 (give-up-ir1-transform))
566 (let ((alignment (count-low-order-zeros numerator)))
567 (unless (>= alignment bits)
568 (give-up-ir1-transform))
569 `(ash numerator ,(- bits)))))
571 (deftransform ash ((value amount))
572 (let ((value-node (lvar-uses value)))
573 (unless (and (combination-p value-node)
574 (eq (lvar-fun-name (combination-fun value-node))
576 (give-up-ir1-transform))
577 (let ((inside-args (combination-args value-node)))
578 (unless (= (length inside-args) 2)
579 (give-up-ir1-transform))
580 (let ((inside-amount (second inside-args)))
581 (unless (and (constant-lvar-p inside-amount)
582 (not (minusp (lvar-value inside-amount))))
583 (give-up-ir1-transform)))))
584 (extract-fun-args value 'ash 2)
585 '(lambda (value amount1 amount2)
586 (ash value (+ amount1 amount2))))
588 ;;;; ALIEN-FUNCALL support
590 (deftransform alien-funcall ((function &rest args)
591 ((alien (* t)) &rest *) *
593 (let ((names (make-gensym-list (length args))))
594 (/noshow "entering first DEFTRANSFORM ALIEN-FUNCALL" function args)
595 `(lambda (function ,@names)
596 (alien-funcall (deref function) ,@names))))
598 (deftransform alien-funcall ((function &rest args) * * :important t)
599 (let ((type (lvar-type function)))
600 (unless (alien-type-type-p type)
601 (give-up-ir1-transform "can't tell function type at compile time"))
602 (/noshow "entering second DEFTRANSFORM ALIEN-FUNCALL" function)
603 (let ((alien-type (alien-type-type-alien-type type)))
604 (unless (alien-fun-type-p alien-type)
605 (give-up-ir1-transform))
606 (let ((arg-types (alien-fun-type-arg-types alien-type)))
607 (unless (= (length args) (length arg-types))
609 "wrong number of arguments; expected ~W, got ~W"
612 (collect ((params) (deports))
613 (dolist (arg-type arg-types)
614 (let ((param (gensym)))
616 (deports `(deport ,param ',arg-type))))
617 (let ((return-type (alien-fun-type-result-type alien-type))
618 (body `(%alien-funcall (deport function ',alien-type)
621 (if (alien-values-type-p return-type)
622 (collect ((temps) (results))
623 (dolist (type (alien-values-type-values return-type))
624 (let ((temp (gensym)))
626 (results `(naturalize ,temp ',type))))
628 `(multiple-value-bind ,(temps) ,body
629 (values ,@(results)))))
630 (setf body `(naturalize ,body ',return-type)))
631 (/noshow "returning from DEFTRANSFORM ALIEN-FUNCALL" (params) body)
632 `(lambda (function ,@(params))
635 (defoptimizer (%alien-funcall derive-type) ((function type &rest args))
636 (declare (ignore function args))
637 (unless (constant-lvar-p type)
638 (error "Something is broken."))
639 (let ((type (lvar-value type)))
640 (unless (alien-fun-type-p type)
641 (error "Something is broken."))
642 (values-specifier-type
643 (compute-alien-rep-type
644 (alien-fun-type-result-type type)))))
646 (defoptimizer (%alien-funcall ltn-annotate)
647 ((function type &rest args) node ltn-policy)
648 (setf (basic-combination-info node) :funny)
649 (setf (node-tail-p node) nil)
650 (annotate-ordinary-lvar function)
652 (annotate-ordinary-lvar arg)))
654 (defoptimizer (%alien-funcall ir2-convert)
655 ((function type &rest args) call block)
656 (let ((type (if (constant-lvar-p type)
658 (error "Something is broken.")))
659 (lvar (node-lvar call))
661 (multiple-value-bind (nsp stack-frame-size arg-tns result-tns)
662 (make-call-out-tns type)
663 (vop alloc-number-stack-space call block stack-frame-size nsp)
665 (let* ((arg (pop args))
668 #!-x86 (temp-tn (make-representation-tn (tn-primitive-type tn)
670 (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
672 (unless (= (length move-arg-vops) 1)
673 (error "no unique move-arg-vop for moves in SC ~S" (sc-name sc)))
674 #!+x86 (emit-move-arg-template call
676 (first move-arg-vops)
677 (lvar-tn call block arg)
683 (lvar-tn call block arg)
685 (emit-move-arg-template call
687 (first move-arg-vops)
692 (unless (listp result-tns)
693 (setf result-tns (list result-tns)))
694 (vop* call-out call block
695 ((lvar-tn call block function)
696 (reference-tn-list arg-tns nil))
697 ((reference-tn-list result-tns t)))
698 (vop dealloc-number-stack-space call block stack-frame-size)
699 (move-lvar-result call block result-tns lvar))))