1 ;;;; This file contains macro-like source transformations which
2 ;;;; convert uses of certain functions into the canonical form desired
3 ;;;; within the compiler. ### and other IR1 transforms and stuff.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
16 ;;; Convert into an IF so that IF optimizations will eliminate redundant
18 (def-source-transform not (x) `(if ,x nil t))
19 (def-source-transform null (x) `(if ,x nil t))
21 ;;; ENDP is just NULL with a LIST assertion.
22 (def-source-transform endp (x) `(null (the list ,x)))
23 ;;; FIXME: Is THE LIST a strong enough assertion for ANSI's "should
24 ;;; return an error"? (THE LIST is optimized away when safety is low;
25 ;;; does that satisfy the spec?)
27 ;;; We turn IDENTITY into PROG1 so that it is obvious that it just
28 ;;; returns the first value of its argument. Ditto for VALUES with one
30 (def-source-transform identity (x) `(prog1 ,x))
31 (def-source-transform values (x) `(prog1 ,x))
33 ;;; Bind the values and make a closure that returns them.
34 (def-source-transform constantly (value &rest values)
35 (let ((temps (make-gensym-list (1+ (length values))))
37 `(let ,(loop for temp in temps and
38 value in (list* value values)
39 collect `(,temp ,value))
40 #'(lambda (&rest ,dum)
41 (declare (ignore ,dum))
44 ;;; If the function has a known number of arguments, then return a
45 ;;; lambda with the appropriate fixed number of args. If the
46 ;;; destination is a FUNCALL, then do the &REST APPLY thing, and let
47 ;;; MV optimization figure things out.
48 (deftransform complement ((fun) * * :node node :when :both)
50 (multiple-value-bind (min max)
51 (function-type-nargs (continuation-type fun))
53 ((and min (eql min max))
54 (let ((dums (make-gensym-list min)))
55 `#'(lambda ,dums (not (funcall fun ,@dums)))))
56 ((let* ((cont (node-cont node))
57 (dest (continuation-dest cont)))
58 (and (combination-p dest)
59 (eq (combination-fun dest) cont)))
60 '#'(lambda (&rest args)
61 (not (apply fun args))))
63 (give-up-ir1-transform
64 "The function doesn't have a fixed argument count.")))))
68 ;;; Translate CxxR into CAR/CDR combos.
70 (defun source-transform-cxr (form)
71 (if (or (byte-compiling) (/= (length form) 2))
73 (let ((name (symbol-name (car form))))
74 (do ((i (- (length name) 2) (1- i))
76 `(,(ecase (char name i)
83 (b '(1 0) (cons i b)))
85 (dotimes (j (ash 1 i))
86 (setf (info :function :source-transform
87 (intern (format nil "C~{~:[A~;D~]~}R"
88 (mapcar #'(lambda (x) (logbitp x j)) b))))
89 #'source-transform-cxr)))
91 ;;; Turn FIRST..FOURTH and REST into the obvious synonym, assuming
92 ;;; whatever is right for them is right for us. FIFTH..TENTH turn into
93 ;;; Nth, which can be expanded into a CAR/CDR later on if policy
95 (def-source-transform first (x) `(car ,x))
96 (def-source-transform rest (x) `(cdr ,x))
97 (def-source-transform second (x) `(cadr ,x))
98 (def-source-transform third (x) `(caddr ,x))
99 (def-source-transform fourth (x) `(cadddr ,x))
100 (def-source-transform fifth (x) `(nth 4 ,x))
101 (def-source-transform sixth (x) `(nth 5 ,x))
102 (def-source-transform seventh (x) `(nth 6 ,x))
103 (def-source-transform eighth (x) `(nth 7 ,x))
104 (def-source-transform ninth (x) `(nth 8 ,x))
105 (def-source-transform tenth (x) `(nth 9 ,x))
107 ;;; Translate RPLACx to LET and SETF.
108 (def-source-transform rplaca (x y)
113 (def-source-transform rplacd (x y)
119 (def-source-transform nth (n l) `(car (nthcdr ,n ,l)))
121 (defvar *default-nthcdr-open-code-limit* 6)
122 (defvar *extreme-nthcdr-open-code-limit* 20)
124 (deftransform nthcdr ((n l) (unsigned-byte t) * :node node)
125 "convert NTHCDR to CAxxR"
126 (unless (constant-continuation-p n)
127 (give-up-ir1-transform))
128 (let ((n (continuation-value n)))
130 (if (policy node (= speed 3) (= space 0))
131 *extreme-nthcdr-open-code-limit*
132 *default-nthcdr-open-code-limit*))
133 (give-up-ir1-transform))
138 `(cdr ,(frob (1- n))))))
141 ;;;; arithmetic and numerology
143 (def-source-transform plusp (x) `(> ,x 0))
144 (def-source-transform minusp (x) `(< ,x 0))
145 (def-source-transform zerop (x) `(= ,x 0))
147 (def-source-transform 1+ (x) `(+ ,x 1))
148 (def-source-transform 1- (x) `(- ,x 1))
150 (def-source-transform oddp (x) `(not (zerop (logand ,x 1))))
151 (def-source-transform evenp (x) `(zerop (logand ,x 1)))
153 ;;; Note that all the integer division functions are available for
154 ;;; inline expansion.
156 ;;; FIXME: DEF-FROB instead of FROB
157 (macrolet ((frob (fun)
158 `(def-source-transform ,fun (x &optional (y nil y-p))
165 #!+propagate-float-type
167 #!+propagate-float-type
170 (def-source-transform lognand (x y) `(lognot (logand ,x ,y)))
171 (def-source-transform lognor (x y) `(lognot (logior ,x ,y)))
172 (def-source-transform logandc1 (x y) `(logand (lognot ,x) ,y))
173 (def-source-transform logandc2 (x y) `(logand ,x (lognot ,y)))
174 (def-source-transform logorc1 (x y) `(logior (lognot ,x) ,y))
175 (def-source-transform logorc2 (x y) `(logior ,x (lognot ,y)))
176 (def-source-transform logtest (x y) `(not (zerop (logand ,x ,y))))
177 (def-source-transform logbitp (index integer)
178 `(not (zerop (logand (ash 1 ,index) ,integer))))
179 (def-source-transform byte (size position) `(cons ,size ,position))
180 (def-source-transform byte-size (spec) `(car ,spec))
181 (def-source-transform byte-position (spec) `(cdr ,spec))
182 (def-source-transform ldb-test (bytespec integer)
183 `(not (zerop (mask-field ,bytespec ,integer))))
185 ;;; With the ratio and complex accessors, we pick off the "identity"
186 ;;; case, and use a primitive to handle the cell access case.
187 (def-source-transform numerator (num)
188 (once-only ((n-num `(the rational ,num)))
192 (def-source-transform denominator (num)
193 (once-only ((n-num `(the rational ,num)))
195 (%denominator ,n-num)
198 ;;;; Interval arithmetic for computing bounds
199 ;;;; (toy@rtp.ericsson.se)
201 ;;;; This is a set of routines for operating on intervals. It
202 ;;;; implements a simple interval arithmetic package. Although SBCL
203 ;;;; has an interval type in numeric-type, we choose to use our own
204 ;;;; for two reasons:
206 ;;;; 1. This package is simpler than numeric-type
208 ;;;; 2. It makes debugging much easier because you can just strip
209 ;;;; out these routines and test them independently of SBCL. (a
212 ;;;; One disadvantage is a probable increase in consing because we
213 ;;;; have to create these new interval structures even though
214 ;;;; numeric-type has everything we want to know. Reason 2 wins for
217 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
219 #!+propagate-float-type
222 ;;; The basic interval type. It can handle open and closed intervals.
223 ;;; A bound is open if it is a list containing a number, just like
224 ;;; Lisp says. NIL means unbounded.
226 (:constructor %make-interval))
229 (defun make-interval (&key low high)
230 (labels ((normalize-bound (val)
231 (cond ((and (floatp val)
232 (float-infinity-p val))
237 ;; Handle any closed bounds
240 ;; We have an open bound. Normalize the numeric
241 ;; bound. If the normalized bound is still a number
242 ;; (not nil), keep the bound open. Otherwise, the
243 ;; bound is really unbounded, so drop the openness.
244 (let ((new-val (normalize-bound (first val))))
246 ;; Bound exists, so keep it open still
249 (error "Unknown bound type in make-interval!")))))
250 (%make-interval :low (normalize-bound low)
251 :high (normalize-bound high))))
253 #!-sb-fluid (declaim (inline bound-value set-bound))
255 ;;; Extract the numeric value of a bound. Return NIL, if X is NIL.
256 (defun bound-value (x)
257 (if (consp x) (car x) x))
259 ;;; Given a number X, create a form suitable as a bound for an
260 ;;; interval. Make the bound open if OPEN-P is T. NIL remains NIL.
261 (defun set-bound (x open-p)
262 (if (and x open-p) (list x) x))
264 ;;; Apply the function F to a bound X. If X is an open bound, then
265 ;;; the result will be open. IF X is NIL, the result is NIL.
266 (defun bound-func (f x)
268 (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)
269 ;; With these traps masked, we might get things like infinity
270 ;; or negative infinity returned. Check for this and return
271 ;; NIL to indicate unbounded.
272 (let ((y (funcall f (bound-value x))))
274 (float-infinity-p y))
276 (set-bound (funcall f (bound-value x)) (consp x)))))))
278 ;;; Apply a binary operator OP to two bounds X and Y. The result is
279 ;;; NIL if either is NIL. Otherwise bound is computed and the result
280 ;;; is open if either X or Y is open.
282 ;;; FIXME: only used in this file, not needed in target runtime
283 (defmacro bound-binop (op x y)
285 (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)
286 (set-bound (,op (bound-value ,x)
288 (or (consp ,x) (consp ,y))))))
290 ;;; NUMERIC-TYPE->INTERVAL
292 ;;; Convert a numeric-type object to an interval object.
294 (defun numeric-type->interval (x)
295 (declare (type numeric-type x))
296 (make-interval :low (numeric-type-low x)
297 :high (numeric-type-high x)))
299 (defun copy-interval-limit (limit)
304 (defun copy-interval (x)
305 (declare (type interval x))
306 (make-interval :low (copy-interval-limit (interval-low x))
307 :high (copy-interval-limit (interval-high x))))
311 ;;; Given a point P contained in the interval X, split X into two
312 ;;; interval at the point P. If CLOSE-LOWER is T, then the left
313 ;;; interval contains P. If CLOSE-UPPER is T, the right interval
314 ;;; contains P. You can specify both to be T or NIL.
315 (defun interval-split (p x &optional close-lower close-upper)
316 (declare (type number p)
318 (list (make-interval :low (copy-interval-limit (interval-low x))
319 :high (if close-lower p (list p)))
320 (make-interval :low (if close-upper (list p) p)
321 :high (copy-interval-limit (interval-high x)))))
325 ;;; Return the closure of the interval. That is, convert open bounds
326 ;;; to closed bounds.
327 (defun interval-closure (x)
328 (declare (type interval x))
329 (make-interval :low (bound-value (interval-low x))
330 :high (bound-value (interval-high x))))
332 (defun signed-zero->= (x y)
336 (>= (float-sign (float x))
337 (float-sign (float y))))))
339 ;;; INTERVAL-RANGE-INFO
341 ;;; For an interval X, if X >= POINT, return '+. If X <= POINT, return
342 ;;; '-. Otherwise return NIL.
344 (defun interval-range-info (x &optional (point 0))
345 (declare (type interval x))
346 (let ((lo (interval-low x))
347 (hi (interval-high x)))
348 (cond ((and lo (signed-zero->= (bound-value lo) point))
350 ((and hi (signed-zero->= point (bound-value hi)))
354 (defun interval-range-info (x &optional (point 0))
355 (declare (type interval x))
356 (labels ((signed->= (x y)
357 (if (and (zerop x) (zerop y) (floatp x) (floatp y))
358 (>= (float-sign x) (float-sign y))
360 (let ((lo (interval-low x))
361 (hi (interval-high x)))
362 (cond ((and lo (signed->= (bound-value lo) point))
364 ((and hi (signed->= point (bound-value hi)))
369 ;;; INTERVAL-BOUNDED-P
371 ;;; Test to see whether the interval X is bounded. HOW determines the
372 ;;; test, and should be either ABOVE, BELOW, or BOTH.
373 (defun interval-bounded-p (x how)
374 (declare (type interval x))
381 (and (interval-low x) (interval-high x)))))
383 ;;; Signed zero comparison functions. Use these functions if we need
384 ;;; to distinguish between signed zeroes.
386 (defun signed-zero-< (x y)
390 (< (float-sign (float x))
391 (float-sign (float y))))))
392 (defun signed-zero-> (x y)
396 (> (float-sign (float x))
397 (float-sign (float y))))))
399 (defun signed-zero-= (x y)
402 (= (float-sign (float x))
403 (float-sign (float y)))))
405 (defun signed-zero-<= (x y)
409 (<= (float-sign (float x))
410 (float-sign (float y))))))
412 ;;; INTERVAL-CONTAINS-P
414 ;;; See whether the interval X contains the number P, taking into account
415 ;;; that the interval might not be closed.
416 (defun interval-contains-p (p x)
417 (declare (type number p)
419 ;; Does the interval X contain the number P? This would be a lot
420 ;; easier if all intervals were closed!
421 (let ((lo (interval-low x))
422 (hi (interval-high x)))
424 ;; The interval is bounded
425 (if (and (signed-zero-<= (bound-value lo) p)
426 (signed-zero-<= p (bound-value hi)))
427 ;; P is definitely in the closure of the interval.
428 ;; We just need to check the end points now.
429 (cond ((signed-zero-= p (bound-value lo))
431 ((signed-zero-= p (bound-value hi))
436 ;; Interval with upper bound
437 (if (signed-zero-< p (bound-value hi))
439 (and (numberp hi) (signed-zero-= p hi))))
441 ;; Interval with lower bound
442 (if (signed-zero-> p (bound-value lo))
444 (and (numberp lo) (signed-zero-= p lo))))
446 ;; Interval with no bounds
449 ;;; INTERVAL-INTERSECT-P
451 ;;; Determine if two intervals X and Y intersect. Return T if so. If
452 ;;; CLOSED-INTERVALS-P is T, the treat the intervals as if they were
453 ;;; closed. Otherwise the intervals are treated as they are.
455 ;;; Thus if X = [0, 1) and Y = (1, 2), then they do not intersect
456 ;;; because no element in X is in Y. However, if CLOSED-INTERVALS-P
457 ;;; is T, then they do intersect because we use the closure of X = [0,
458 ;;; 1] and Y = [1, 2] to determine intersection.
459 (defun interval-intersect-p (x y &optional closed-intervals-p)
460 (declare (type interval x y))
461 (multiple-value-bind (intersect diff)
462 (interval-intersection/difference (if closed-intervals-p
465 (if closed-intervals-p
468 (declare (ignore diff))
471 ;;; Are the two intervals adjacent? That is, is there a number
472 ;;; between the two intervals that is not an element of either
473 ;;; interval? If so, they are not adjacent. For example [0, 1) and
474 ;;; [1, 2] are adjacent but [0, 1) and (1, 2] are not because 1 lies
475 ;;; between both intervals.
476 (defun interval-adjacent-p (x y)
477 (declare (type interval x y))
478 (flet ((adjacent (lo hi)
479 ;; Check to see whether lo and hi are adjacent. If either is
480 ;; nil, they can't be adjacent.
481 (when (and lo hi (= (bound-value lo) (bound-value hi)))
482 ;; The bounds are equal. They are adjacent if one of
483 ;; them is closed (a number). If both are open (consp),
484 ;; then there is a number that lies between them.
485 (or (numberp lo) (numberp hi)))))
486 (or (adjacent (interval-low y) (interval-high x))
487 (adjacent (interval-low x) (interval-high y)))))
489 ;;; INTERVAL-INTERSECTION/DIFFERENCE
491 ;;; Compute the intersection and difference between two intervals.
492 ;;; Two values are returned: the intersection and the difference.
494 ;;; Let the two intervals be X and Y, and let I and D be the two
495 ;;; values returned by this function. Then I = X intersect Y. If I
496 ;;; is NIL (the empty set), then D is X union Y, represented as the
497 ;;; list of X and Y. If I is not the empty set, then D is (X union Y)
498 ;;; - I, which is a list of two intervals.
500 ;;; For example, let X = [1,5] and Y = [-1,3). Then I = [1,3) and D =
501 ;;; [-1,1) union [3,5], which is returned as a list of two intervals.
502 (defun interval-intersection/difference (x y)
503 (declare (type interval x y))
504 (let ((x-lo (interval-low x))
505 (x-hi (interval-high x))
506 (y-lo (interval-low y))
507 (y-hi (interval-high y)))
510 ;; If p is an open bound, make it closed. If p is a closed
511 ;; bound, make it open.
516 ;; Test whether P is in the interval.
517 (when (interval-contains-p (bound-value p)
518 (interval-closure int))
519 (let ((lo (interval-low int))
520 (hi (interval-high int)))
521 ;; Check for endpoints
522 (cond ((and lo (= (bound-value p) (bound-value lo)))
523 (not (and (consp p) (numberp lo))))
524 ((and hi (= (bound-value p) (bound-value hi)))
525 (not (and (numberp p) (consp hi))))
527 (test-lower-bound (p int)
528 ;; P is a lower bound of an interval.
531 (not (interval-bounded-p int 'below))))
532 (test-upper-bound (p int)
533 ;; P is an upper bound of an interval
536 (not (interval-bounded-p int 'above)))))
537 (let ((x-lo-in-y (test-lower-bound x-lo y))
538 (x-hi-in-y (test-upper-bound x-hi y))
539 (y-lo-in-x (test-lower-bound y-lo x))
540 (y-hi-in-x (test-upper-bound y-hi x)))
541 (cond ((or x-lo-in-y x-hi-in-y y-lo-in-x y-hi-in-x)
542 ;; Intervals intersect. Let's compute the intersection
543 ;; and the difference.
544 (multiple-value-bind (lo left-lo left-hi)
545 (cond (x-lo-in-y (values x-lo y-lo (opposite-bound x-lo)))
546 (y-lo-in-x (values y-lo x-lo (opposite-bound y-lo))))
547 (multiple-value-bind (hi right-lo right-hi)
549 (values x-hi (opposite-bound x-hi) y-hi))
551 (values y-hi (opposite-bound y-hi) x-hi)))
552 (values (make-interval :low lo :high hi)
553 (list (make-interval :low left-lo :high left-hi)
554 (make-interval :low right-lo :high right-hi))))))
556 (values nil (list x y))))))))
558 ;;; INTERVAL-MERGE-PAIR
560 ;;; If intervals X and Y intersect, return a new interval that is the
561 ;;; union of the two. If they do not intersect, return NIL.
562 (defun interval-merge-pair (x y)
563 (declare (type interval x y))
564 ;; If x and y intersect or are adjacent, create the union.
565 ;; Otherwise return nil
566 (when (or (interval-intersect-p x y)
567 (interval-adjacent-p x y))
568 (flet ((select-bound (x1 x2 min-op max-op)
569 (let ((x1-val (bound-value x1))
570 (x2-val (bound-value x2)))
572 ;; Both bounds are finite. Select the right one.
573 (cond ((funcall min-op x1-val x2-val)
574 ;; x1 definitely better
576 ((funcall max-op x1-val x2-val)
577 ;; x2 definitely better
580 ;; Bounds are equal. Select either
581 ;; value and make it open only if
583 (set-bound x1-val (and (consp x1) (consp x2))))))
585 ;; At least one bound is not finite. The
586 ;; non-finite bound always wins.
588 (let* ((x-lo (copy-interval-limit (interval-low x)))
589 (x-hi (copy-interval-limit (interval-high x)))
590 (y-lo (copy-interval-limit (interval-low y)))
591 (y-hi (copy-interval-limit (interval-high y))))
592 (make-interval :low (select-bound x-lo y-lo #'< #'>)
593 :high (select-bound x-hi y-hi #'> #'<))))))
595 ;;; Basic arithmetic operations on intervals. We probably should do
596 ;;; true interval arithmetic here, but it's complicated because we
597 ;;; have float and integer types and bounds can be open or closed.
601 ;;; The negative of an interval
602 (defun interval-neg (x)
603 (declare (type interval x))
604 (make-interval :low (bound-func #'- (interval-high x))
605 :high (bound-func #'- (interval-low x))))
609 ;;; Add two intervals
610 (defun interval-add (x y)
611 (declare (type interval x y))
612 (make-interval :low (bound-binop + (interval-low x) (interval-low y))
613 :high (bound-binop + (interval-high x) (interval-high y))))
617 ;;; Subtract two intervals
618 (defun interval-sub (x y)
619 (declare (type interval x y))
620 (make-interval :low (bound-binop - (interval-low x) (interval-high y))
621 :high (bound-binop - (interval-high x) (interval-low y))))
625 ;;; Multiply two intervals
626 (defun interval-mul (x y)
627 (declare (type interval x y))
628 (flet ((bound-mul (x y)
629 (cond ((or (null x) (null y))
630 ;; Multiply by infinity is infinity
632 ((or (and (numberp x) (zerop x))
633 (and (numberp y) (zerop y)))
634 ;; Multiply by closed zero is special. The result
635 ;; is always a closed bound. But don't replace this
636 ;; with zero; we want the multiplication to produce
637 ;; the correct signed zero, if needed.
638 (* (bound-value x) (bound-value y)))
639 ((or (and (floatp x) (float-infinity-p x))
640 (and (floatp y) (float-infinity-p y)))
641 ;; Infinity times anything is infinity
644 ;; General multiply. The result is open if either is open.
645 (bound-binop * x y)))))
646 (let ((x-range (interval-range-info x))
647 (y-range (interval-range-info y)))
648 (cond ((null x-range)
649 ;; Split x into two and multiply each separately
650 (destructuring-bind (x- x+) (interval-split 0 x t t)
651 (interval-merge-pair (interval-mul x- y)
652 (interval-mul x+ y))))
654 ;; Split y into two and multiply each separately
655 (destructuring-bind (y- y+) (interval-split 0 y t t)
656 (interval-merge-pair (interval-mul x y-)
657 (interval-mul x y+))))
659 (interval-neg (interval-mul (interval-neg x) y)))
661 (interval-neg (interval-mul x (interval-neg y))))
662 ((and (eq x-range '+) (eq y-range '+))
663 ;; If we are here, X and Y are both positive
664 (make-interval :low (bound-mul (interval-low x) (interval-low y))
665 :high (bound-mul (interval-high x) (interval-high y))))
667 (error "This shouldn't happen!"))))))
671 ;;; Divide two intervals.
672 (defun interval-div (top bot)
673 (declare (type interval top bot))
674 (flet ((bound-div (x y y-low-p)
677 ;; Divide by infinity means result is 0. However,
678 ;; we need to watch out for the sign of the result,
679 ;; to correctly handle signed zeros. We also need
680 ;; to watch out for positive or negative infinity.
681 (if (floatp (bound-value x))
683 (- (float-sign (bound-value x) 0.0))
684 (float-sign (bound-value x) 0.0))
686 ((zerop (bound-value y))
687 ;; Divide by zero means result is infinity
689 ((and (numberp x) (zerop x))
690 ;; Zero divided by anything is zero.
693 (bound-binop / x y)))))
694 (let ((top-range (interval-range-info top))
695 (bot-range (interval-range-info bot)))
696 (cond ((null bot-range)
697 ;; The denominator contains zero, so anything goes!
698 (make-interval :low nil :high nil))
700 ;; Denominator is negative so flip the sign, compute the
701 ;; result, and flip it back.
702 (interval-neg (interval-div top (interval-neg bot))))
704 ;; Split top into two positive and negative parts, and
705 ;; divide each separately
706 (destructuring-bind (top- top+) (interval-split 0 top t t)
707 (interval-merge-pair (interval-div top- bot)
708 (interval-div top+ bot))))
710 ;; Top is negative so flip the sign, divide, and flip the
711 ;; sign of the result.
712 (interval-neg (interval-div (interval-neg top) bot)))
713 ((and (eq top-range '+) (eq bot-range '+))
715 (make-interval :low (bound-div (interval-low top) (interval-high bot) t)
716 :high (bound-div (interval-high top) (interval-low bot) nil)))
718 (error "This shouldn't happen!"))))))
722 ;;; Apply the function F to the interval X. If X = [a, b], then the
723 ;;; result is [f(a), f(b)]. It is up to the user to make sure the
724 ;;; result makes sense. It will if F is monotonic increasing (or
726 (defun interval-func (f x)
727 (declare (type interval x))
728 (let ((lo (bound-func f (interval-low x)))
729 (hi (bound-func f (interval-high x))))
730 (make-interval :low lo :high hi)))
734 ;;; Return T if X < Y. That is every number in the interval X is
735 ;;; always less than any number in the interval Y.
736 (defun interval-< (x y)
737 (declare (type interval x y))
738 ;; X < Y only if X is bounded above, Y is bounded below, and they
740 (when (and (interval-bounded-p x 'above)
741 (interval-bounded-p y 'below))
742 ;; Intervals are bounded in the appropriate way. Make sure they
744 (let ((left (interval-high x))
745 (right (interval-low y)))
746 (cond ((> (bound-value left)
748 ;; Definitely overlap so result is NIL
750 ((< (bound-value left)
752 ;; Definitely don't touch, so result is T
755 ;; Limits are equal. Check for open or closed bounds.
756 ;; Don't overlap if one or the other are open.
757 (or (consp left) (consp right)))))))
761 ;;; Return T if X >= Y. That is, every number in the interval X is
762 ;;; always greater than any number in the interval Y.
763 (defun interval->= (x y)
764 (declare (type interval x y))
765 ;; X >= Y if lower bound of X >= upper bound of Y
766 (when (and (interval-bounded-p x 'below)
767 (interval-bounded-p y 'above))
768 (>= (bound-value (interval-low x)) (bound-value (interval-high y)))))
772 ;;; Return an interval that is the absolute value of X. Thus, if X =
773 ;;; [-1 10], the result is [0, 10].
774 (defun interval-abs (x)
775 (declare (type interval x))
776 (case (interval-range-info x)
782 (destructuring-bind (x- x+) (interval-split 0 x t t)
783 (interval-merge-pair (interval-neg x-) x+)))))
787 ;;; Compute the square of an interval.
788 (defun interval-sqr (x)
789 (declare (type interval x))
790 (interval-func #'(lambda (x) (* x x))
794 ;;;; numeric derive-type methods
796 ;;; Utility for defining derive-type methods of integer operations. If the
797 ;;; types of both X and Y are integer types, then we compute a new integer type
798 ;;; with bounds determined Fun when applied to X and Y. Otherwise, we use
799 ;;; Numeric-Contagion.
800 (defun derive-integer-type (x y fun)
801 (declare (type continuation x y) (type function fun))
802 (let ((x (continuation-type x))
803 (y (continuation-type y)))
804 (if (and (numeric-type-p x) (numeric-type-p y)
805 (eq (numeric-type-class x) 'integer)
806 (eq (numeric-type-class y) 'integer)
807 (eq (numeric-type-complexp x) :real)
808 (eq (numeric-type-complexp y) :real))
809 (multiple-value-bind (low high) (funcall fun x y)
810 (make-numeric-type :class 'integer
814 (numeric-contagion x y))))
816 #!+(or propagate-float-type propagate-fun-type)
819 ;; Simple utility to flatten a list
820 (defun flatten-list (x)
821 (labels ((flatten-helper (x r);; 'r' is the stuff to the 'right'.
825 (t (flatten-helper (car x)
826 (flatten-helper (cdr x) r))))))
827 (flatten-helper x nil)))
829 ;;; Take some type of continuation and massage it so that we get a
830 ;;; list of the constituent types. If ARG is *EMPTY-TYPE*, return NIL
831 ;;; to indicate failure.
832 (defun prepare-arg-for-derive-type (arg)
833 (flet ((listify (arg)
838 (union-type-types arg))
841 (unless (eq arg *empty-type*)
842 ;; Make sure all args are some type of numeric-type. For member
843 ;; types, convert the list of members into a union of equivalent
844 ;; single-element member-type's.
845 (let ((new-args nil))
846 (dolist (arg (listify arg))
847 (if (member-type-p arg)
848 ;; Run down the list of members and convert to a list of
850 (dolist (member (member-type-members arg))
851 (push (if (numberp member)
852 (make-member-type :members (list member))
855 (push arg new-args)))
856 (unless (member *empty-type* new-args)
859 ;;; Convert from the standard type convention for which -0.0 and 0.0
860 ;;; and equal to an intermediate convention for which they are
861 ;;; considered different which is more natural for some of the
863 #!-negative-zero-is-not-zero
864 (defun convert-numeric-type (type)
865 (declare (type numeric-type type))
866 ;;; Only convert real float interval delimiters types.
867 (if (eq (numeric-type-complexp type) :real)
868 (let* ((lo (numeric-type-low type))
869 (lo-val (bound-value lo))
870 (lo-float-zero-p (and lo (floatp lo-val) (= lo-val 0.0)))
871 (hi (numeric-type-high type))
872 (hi-val (bound-value hi))
873 (hi-float-zero-p (and hi (floatp hi-val) (= hi-val 0.0))))
874 (if (or lo-float-zero-p hi-float-zero-p)
876 :class (numeric-type-class type)
877 :format (numeric-type-format type)
879 :low (if lo-float-zero-p
881 (list (float 0.0 lo-val))
884 :high (if hi-float-zero-p
886 (list (float -0.0 hi-val))
893 ;;; Convert back from the intermediate convention for which -0.0 and
894 ;;; 0.0 are considered different to the standard type convention for
896 #!-negative-zero-is-not-zero
897 (defun convert-back-numeric-type (type)
898 (declare (type numeric-type type))
899 ;;; Only convert real float interval delimiters types.
900 (if (eq (numeric-type-complexp type) :real)
901 (let* ((lo (numeric-type-low type))
902 (lo-val (bound-value lo))
904 (and lo (floatp lo-val) (= lo-val 0.0)
905 (float-sign lo-val)))
906 (hi (numeric-type-high type))
907 (hi-val (bound-value hi))
909 (and hi (floatp hi-val) (= hi-val 0.0)
910 (float-sign hi-val))))
912 ;; (float +0.0 +0.0) => (member 0.0)
913 ;; (float -0.0 -0.0) => (member -0.0)
914 ((and lo-float-zero-p hi-float-zero-p)
915 ;; Shouldn't have exclusive bounds here.
916 (assert (and (not (consp lo)) (not (consp hi))))
917 (if (= lo-float-zero-p hi-float-zero-p)
918 ;; (float +0.0 +0.0) => (member 0.0)
919 ;; (float -0.0 -0.0) => (member -0.0)
920 (specifier-type `(member ,lo-val))
921 ;; (float -0.0 +0.0) => (float 0.0 0.0)
922 ;; (float +0.0 -0.0) => (float 0.0 0.0)
923 (make-numeric-type :class (numeric-type-class type)
924 :format (numeric-type-format type)
930 ;; (float -0.0 x) => (float 0.0 x)
931 ((and (not (consp lo)) (minusp lo-float-zero-p))
932 (make-numeric-type :class (numeric-type-class type)
933 :format (numeric-type-format type)
935 :low (float 0.0 lo-val)
937 ;; (float (+0.0) x) => (float (0.0) x)
938 ((and (consp lo) (plusp lo-float-zero-p))
939 (make-numeric-type :class (numeric-type-class type)
940 :format (numeric-type-format type)
942 :low (list (float 0.0 lo-val))
945 ;; (float +0.0 x) => (or (member 0.0) (float (0.0) x))
946 ;; (float (-0.0) x) => (or (member 0.0) (float (0.0) x))
947 (list (make-member-type :members (list (float 0.0 lo-val)))
948 (make-numeric-type :class (numeric-type-class type)
949 :format (numeric-type-format type)
951 :low (list (float 0.0 lo-val))
955 ;; (float x +0.0) => (float x 0.0)
956 ((and (not (consp hi)) (plusp hi-float-zero-p))
957 (make-numeric-type :class (numeric-type-class type)
958 :format (numeric-type-format type)
961 :high (float 0.0 hi-val)))
962 ;; (float x (-0.0)) => (float x (0.0))
963 ((and (consp hi) (minusp hi-float-zero-p))
964 (make-numeric-type :class (numeric-type-class type)
965 :format (numeric-type-format type)
968 :high (list (float 0.0 hi-val))))
970 ;; (float x (+0.0)) => (or (member -0.0) (float x (0.0)))
971 ;; (float x -0.0) => (or (member -0.0) (float x (0.0)))
972 (list (make-member-type :members (list (float -0.0 hi-val)))
973 (make-numeric-type :class (numeric-type-class type)
974 :format (numeric-type-format type)
977 :high (list (float 0.0 hi-val)))))))
983 ;;; Convert back a possible list of numeric types.
984 #!-negative-zero-is-not-zero
985 (defun convert-back-numeric-type-list (type-list)
989 (dolist (type type-list)
990 (if (numeric-type-p type)
991 (let ((result (convert-back-numeric-type type)))
993 (setf results (append results result))
994 (push result results)))
995 (push type results)))
998 (convert-back-numeric-type type-list))
1000 (convert-back-numeric-type-list (union-type-types type-list)))
1004 ;;; Make-Canonical-Union-Type
1006 ;;; Take a list of types and return a canonical type specifier,
1007 ;;; combining any members types together. If both positive and
1008 ;;; negative members types are present they are converted to a float
1009 ;;; type. X This would be far simpler if the type-union methods could
1010 ;;; handle member/number unions.
1011 (defun make-canonical-union-type (type-list)
1014 (dolist (type type-list)
1015 (if (member-type-p type)
1016 (setf members (union members (member-type-members type)))
1017 (push type misc-types)))
1019 (when (null (set-difference '(-0l0 0l0) members))
1020 #!-negative-zero-is-not-zero
1021 (push (specifier-type '(long-float 0l0 0l0)) misc-types)
1022 #!+negative-zero-is-not-zero
1023 (push (specifier-type '(long-float -0l0 0l0)) misc-types)
1024 (setf members (set-difference members '(-0l0 0l0))))
1025 (when (null (set-difference '(-0d0 0d0) members))
1026 #!-negative-zero-is-not-zero
1027 (push (specifier-type '(double-float 0d0 0d0)) misc-types)
1028 #!+negative-zero-is-not-zero
1029 (push (specifier-type '(double-float -0d0 0d0)) misc-types)
1030 (setf members (set-difference members '(-0d0 0d0))))
1031 (when (null (set-difference '(-0f0 0f0) members))
1032 #!-negative-zero-is-not-zero
1033 (push (specifier-type '(single-float 0f0 0f0)) misc-types)
1034 #!+negative-zero-is-not-zero
1035 (push (specifier-type '(single-float -0f0 0f0)) misc-types)
1036 (setf members (set-difference members '(-0f0 0f0))))
1037 (cond ((null members)
1038 (let ((res (first misc-types)))
1039 (dolist (type (rest misc-types))
1040 (setq res (type-union res type)))
1043 (make-member-type :members members))
1045 (let ((res (first misc-types)))
1046 (dolist (type (rest misc-types))
1047 (setq res (type-union res type)))
1048 (dolist (type members)
1049 (setq res (type-union
1050 res (make-member-type :members (list type)))))
1053 ;;; Convert-Member-Type
1055 ;;; Convert a member type with a single member to a numeric type.
1056 (defun convert-member-type (arg)
1057 (let* ((members (member-type-members arg))
1058 (member (first members))
1059 (member-type (type-of member)))
1060 (assert (not (rest members)))
1061 (specifier-type `(,(if (subtypep member-type 'integer)
1066 ;;; ONE-ARG-DERIVE-TYPE
1068 ;;; This is used in defoptimizers for computing the resulting type of
1071 ;;; Given the continuation ARG, derive the resulting type using the
1072 ;;; DERIVE-FCN. DERIVE-FCN takes exactly one argument which is some
1073 ;;; "atomic" continuation type like numeric-type or member-type
1074 ;;; (containing just one element). It should return the resulting
1075 ;;; type, which can be a list of types.
1077 ;;; For the case of member types, if a member-fcn is given it is
1078 ;;; called to compute the result otherwise the member type is first
1079 ;;; converted to a numeric type and the derive-fcn is call.
1080 (defun one-arg-derive-type (arg derive-fcn member-fcn
1081 &optional (convert-type t))
1082 (declare (type function derive-fcn)
1083 (type (or null function) member-fcn)
1084 #!+negative-zero-is-not-zero (ignore convert-type))
1085 (let ((arg-list (prepare-arg-for-derive-type (continuation-type arg))))
1091 (with-float-traps-masked
1092 (:underflow :overflow :divide-by-zero)
1096 (first (member-type-members x))))))
1097 ;; Otherwise convert to a numeric type.
1098 (let ((result-type-list
1099 (funcall derive-fcn (convert-member-type x))))
1100 #!-negative-zero-is-not-zero
1102 (convert-back-numeric-type-list result-type-list)
1104 #!+negative-zero-is-not-zero
1107 #!-negative-zero-is-not-zero
1109 (convert-back-numeric-type-list
1110 (funcall derive-fcn (convert-numeric-type x)))
1111 (funcall derive-fcn x))
1112 #!+negative-zero-is-not-zero
1113 (funcall derive-fcn x))
1115 *universal-type*))))
1116 ;; Run down the list of args and derive the type of each one,
1117 ;; saving all of the results in a list.
1118 (let ((results nil))
1119 (dolist (arg arg-list)
1120 (let ((result (deriver arg)))
1122 (setf results (append results result))
1123 (push result results))))
1125 (make-canonical-union-type results)
1126 (first results)))))))
1128 ;;; TWO-ARG-DERIVE-TYPE
1130 ;;; Same as ONE-ARG-DERIVE-TYPE, except we assume the function takes
1131 ;;; two arguments. DERIVE-FCN takes 3 args in this case: the two
1132 ;;; original args and a third which is T to indicate if the two args
1133 ;;; really represent the same continuation. This is useful for
1134 ;;; deriving the type of things like (* x x), which should always be
1135 ;;; positive. If we didn't do this, we wouldn't be able to tell.
1136 (defun two-arg-derive-type (arg1 arg2 derive-fcn fcn
1137 &optional (convert-type t))
1138 #!+negative-zero-is-not-zero
1139 (declare (ignore convert-type))
1140 (flet (#!-negative-zero-is-not-zero
1141 (deriver (x y same-arg)
1142 (cond ((and (member-type-p x) (member-type-p y))
1143 (let* ((x (first (member-type-members x)))
1144 (y (first (member-type-members y)))
1145 (result (with-float-traps-masked
1146 (:underflow :overflow :divide-by-zero
1148 (funcall fcn x y))))
1149 (cond ((null result))
1150 ((and (floatp result) (float-nan-p result))
1153 :format (type-of result)
1156 (make-member-type :members (list result))))))
1157 ((and (member-type-p x) (numeric-type-p y))
1158 (let* ((x (convert-member-type x))
1159 (y (if convert-type (convert-numeric-type y) y))
1160 (result (funcall derive-fcn x y same-arg)))
1162 (convert-back-numeric-type-list result)
1164 ((and (numeric-type-p x) (member-type-p y))
1165 (let* ((x (if convert-type (convert-numeric-type x) x))
1166 (y (convert-member-type y))
1167 (result (funcall derive-fcn x y same-arg)))
1169 (convert-back-numeric-type-list result)
1171 ((and (numeric-type-p x) (numeric-type-p y))
1172 (let* ((x (if convert-type (convert-numeric-type x) x))
1173 (y (if convert-type (convert-numeric-type y) y))
1174 (result (funcall derive-fcn x y same-arg)))
1176 (convert-back-numeric-type-list result)
1180 #!+negative-zero-is-not-zero
1181 (deriver (x y same-arg)
1182 (cond ((and (member-type-p x) (member-type-p y))
1183 (let* ((x (first (member-type-members x)))
1184 (y (first (member-type-members y)))
1185 (result (with-float-traps-masked
1186 (:underflow :overflow :divide-by-zero)
1187 (funcall fcn x y))))
1189 (make-member-type :members (list result)))))
1190 ((and (member-type-p x) (numeric-type-p y))
1191 (let ((x (convert-member-type x)))
1192 (funcall derive-fcn x y same-arg)))
1193 ((and (numeric-type-p x) (member-type-p y))
1194 (let ((y (convert-member-type y)))
1195 (funcall derive-fcn x y same-arg)))
1196 ((and (numeric-type-p x) (numeric-type-p y))
1197 (funcall derive-fcn x y same-arg))
1199 *universal-type*))))
1200 (let ((same-arg (same-leaf-ref-p arg1 arg2))
1201 (a1 (prepare-arg-for-derive-type (continuation-type arg1)))
1202 (a2 (prepare-arg-for-derive-type (continuation-type arg2))))
1204 (let ((results nil))
1206 ;; Since the args are the same continuation, just run
1209 (let ((result (deriver x x same-arg)))
1211 (setf results (append results result))
1212 (push result results))))
1213 ;; Try all pairwise combinations.
1216 (let ((result (or (deriver x y same-arg)
1217 (numeric-contagion x y))))
1219 (setf results (append results result))
1220 (push result results))))))
1222 (make-canonical-union-type results)
1223 (first results)))))))
1227 #!-propagate-float-type
1229 (defoptimizer (+ derive-type) ((x y))
1230 (derive-integer-type
1237 (values (frob (numeric-type-low x) (numeric-type-low y))
1238 (frob (numeric-type-high x) (numeric-type-high y)))))))
1240 (defoptimizer (- derive-type) ((x y))
1241 (derive-integer-type
1248 (values (frob (numeric-type-low x) (numeric-type-high y))
1249 (frob (numeric-type-high x) (numeric-type-low y)))))))
1251 (defoptimizer (* derive-type) ((x y))
1252 (derive-integer-type
1255 (let ((x-low (numeric-type-low x))
1256 (x-high (numeric-type-high x))
1257 (y-low (numeric-type-low y))
1258 (y-high (numeric-type-high y)))
1259 (cond ((not (and x-low y-low))
1261 ((or (minusp x-low) (minusp y-low))
1262 (if (and x-high y-high)
1263 (let ((max (* (max (abs x-low) (abs x-high))
1264 (max (abs y-low) (abs y-high)))))
1265 (values (- max) max))
1268 (values (* x-low y-low)
1269 (if (and x-high y-high)
1273 (defoptimizer (/ derive-type) ((x y))
1274 (numeric-contagion (continuation-type x) (continuation-type y)))
1278 #!+propagate-float-type
1280 (defun +-derive-type-aux (x y same-arg)
1281 (if (and (numeric-type-real-p x)
1282 (numeric-type-real-p y))
1285 (let ((x-int (numeric-type->interval x)))
1286 (interval-add x-int x-int))
1287 (interval-add (numeric-type->interval x)
1288 (numeric-type->interval y))))
1289 (result-type (numeric-contagion x y)))
1290 ;; If the result type is a float, we need to be sure to coerce
1291 ;; the bounds into the correct type.
1292 (when (eq (numeric-type-class result-type) 'float)
1293 (setf result (interval-func
1295 (coerce x (or (numeric-type-format result-type)
1299 :class (if (and (eq (numeric-type-class x) 'integer)
1300 (eq (numeric-type-class y) 'integer))
1301 ;; The sum of integers is always an integer
1303 (numeric-type-class result-type))
1304 :format (numeric-type-format result-type)
1305 :low (interval-low result)
1306 :high (interval-high result)))
1307 ;; General contagion
1308 (numeric-contagion x y)))
1310 (defoptimizer (+ derive-type) ((x y))
1311 (two-arg-derive-type x y #'+-derive-type-aux #'+))
1313 (defun --derive-type-aux (x y same-arg)
1314 (if (and (numeric-type-real-p x)
1315 (numeric-type-real-p y))
1317 ;; (- x x) is always 0.
1319 (make-interval :low 0 :high 0)
1320 (interval-sub (numeric-type->interval x)
1321 (numeric-type->interval y))))
1322 (result-type (numeric-contagion x y)))
1323 ;; If the result type is a float, we need to be sure to coerce
1324 ;; the bounds into the correct type.
1325 (when (eq (numeric-type-class result-type) 'float)
1326 (setf result (interval-func
1328 (coerce x (or (numeric-type-format result-type)
1332 :class (if (and (eq (numeric-type-class x) 'integer)
1333 (eq (numeric-type-class y) 'integer))
1334 ;; The difference of integers is always an integer
1336 (numeric-type-class result-type))
1337 :format (numeric-type-format result-type)
1338 :low (interval-low result)
1339 :high (interval-high result)))
1340 ;; General contagion
1341 (numeric-contagion x y)))
1343 (defoptimizer (- derive-type) ((x y))
1344 (two-arg-derive-type x y #'--derive-type-aux #'-))
1346 (defun *-derive-type-aux (x y same-arg)
1347 (if (and (numeric-type-real-p x)
1348 (numeric-type-real-p y))
1350 ;; (* x x) is always positive, so take care to do it
1353 (interval-sqr (numeric-type->interval x))
1354 (interval-mul (numeric-type->interval x)
1355 (numeric-type->interval y))))
1356 (result-type (numeric-contagion x y)))
1357 ;; If the result type is a float, we need to be sure to coerce
1358 ;; the bounds into the correct type.
1359 (when (eq (numeric-type-class result-type) 'float)
1360 (setf result (interval-func
1362 (coerce x (or (numeric-type-format result-type)
1366 :class (if (and (eq (numeric-type-class x) 'integer)
1367 (eq (numeric-type-class y) 'integer))
1368 ;; The product of integers is always an integer
1370 (numeric-type-class result-type))
1371 :format (numeric-type-format result-type)
1372 :low (interval-low result)
1373 :high (interval-high result)))
1374 (numeric-contagion x y)))
1376 (defoptimizer (* derive-type) ((x y))
1377 (two-arg-derive-type x y #'*-derive-type-aux #'*))
1379 (defun /-derive-type-aux (x y same-arg)
1380 (if (and (numeric-type-real-p x)
1381 (numeric-type-real-p y))
1383 ;; (/ x x) is always 1, except if x can contain 0. In
1384 ;; that case, we shouldn't optimize the division away
1385 ;; because we want 0/0 to signal an error.
1387 (not (interval-contains-p
1388 0 (interval-closure (numeric-type->interval y)))))
1389 (make-interval :low 1 :high 1)
1390 (interval-div (numeric-type->interval x)
1391 (numeric-type->interval y))))
1392 (result-type (numeric-contagion x y)))
1393 ;; If the result type is a float, we need to be sure to coerce
1394 ;; the bounds into the correct type.
1395 (when (eq (numeric-type-class result-type) 'float)
1396 (setf result (interval-func
1398 (coerce x (or (numeric-type-format result-type)
1401 (make-numeric-type :class (numeric-type-class result-type)
1402 :format (numeric-type-format result-type)
1403 :low (interval-low result)
1404 :high (interval-high result)))
1405 (numeric-contagion x y)))
1407 (defoptimizer (/ derive-type) ((x y))
1408 (two-arg-derive-type x y #'/-derive-type-aux #'/))
1412 ;;; KLUDGE: All this ASH optimization is suppressed under CMU CL
1413 ;;; because as of version 2.4.6 for Debian, CMU CL blows up on (ASH
1414 ;;; 1000000000 -100000000000) (i.e. ASH of two bignums yielding zero)
1415 ;;; and it's hard to avoid that calculation in here.
1416 #-(and cmu sb-xc-host)
1418 #!-propagate-fun-type
1419 (defoptimizer (ash derive-type) ((n shift))
1420 (or (let ((n-type (continuation-type n)))
1421 (when (numeric-type-p n-type)
1422 (let ((n-low (numeric-type-low n-type))
1423 (n-high (numeric-type-high n-type)))
1424 (if (constant-continuation-p shift)
1425 (let ((shift (continuation-value shift)))
1426 (make-numeric-type :class 'integer
1428 :low (when n-low (ash n-low shift))
1429 :high (when n-high (ash n-high shift))))
1430 (let ((s-type (continuation-type shift)))
1431 (when (numeric-type-p s-type)
1432 (let ((s-low (numeric-type-low s-type))
1433 (s-high (numeric-type-high s-type)))
1434 (if (and s-low s-high (<= s-low 64) (<= s-high 64))
1435 (make-numeric-type :class 'integer
1438 (min (ash n-low s-high)
1441 (max (ash n-high s-high)
1442 (ash n-high s-low))))
1443 (make-numeric-type :class 'integer
1444 :complexp :real)))))))))
1446 #!+propagate-fun-type
1447 (defun ash-derive-type-aux (n-type shift same-arg)
1448 (declare (ignore same-arg))
1449 (or (and (csubtypep n-type (specifier-type 'integer))
1450 (csubtypep shift (specifier-type 'integer))
1451 (let ((n-low (numeric-type-low n-type))
1452 (n-high (numeric-type-high n-type))
1453 (s-low (numeric-type-low shift))
1454 (s-high (numeric-type-high shift)))
1455 ;; KLUDGE: The bare 64's here should be related to
1456 ;; symbolic machine word size values somehow.
1457 (if (and s-low s-high (<= s-low 64) (<= s-high 64))
1458 (make-numeric-type :class 'integer :complexp :real
1460 (min (ash n-low s-high)
1463 (max (ash n-high s-high)
1464 (ash n-high s-low))))
1465 (make-numeric-type :class 'integer
1468 #!+propagate-fun-type
1469 (defoptimizer (ash derive-type) ((n shift))
1470 (two-arg-derive-type n shift #'ash-derive-type-aux #'ash))
1473 #!-propagate-float-type
1474 (macrolet ((frob (fun)
1475 `#'(lambda (type type2)
1476 (declare (ignore type2))
1477 (let ((lo (numeric-type-low type))
1478 (hi (numeric-type-high type)))
1479 (values (if hi (,fun hi) nil) (if lo (,fun lo) nil))))))
1481 (defoptimizer (%negate derive-type) ((num))
1482 (derive-integer-type num num (frob -)))
1484 (defoptimizer (lognot derive-type) ((int))
1485 (derive-integer-type int int (frob lognot))))
1487 #!+propagate-float-type
1488 (defoptimizer (lognot derive-type) ((int))
1489 (derive-integer-type int int
1490 #'(lambda (type type2)
1491 (declare (ignore type2))
1492 (let ((lo (numeric-type-low type))
1493 (hi (numeric-type-high type)))
1494 (values (if hi (lognot hi) nil)
1495 (if lo (lognot lo) nil)
1496 (numeric-type-class type)
1497 (numeric-type-format type))))))
1499 #!+propagate-float-type
1500 (defoptimizer (%negate derive-type) ((num))
1501 (flet ((negate-bound (b)
1502 (set-bound (- (bound-value b)) (consp b))))
1503 (one-arg-derive-type num
1505 (let ((lo (numeric-type-low type))
1506 (hi (numeric-type-high type))
1507 (result (copy-numeric-type type)))
1508 (setf (numeric-type-low result)
1509 (if hi (negate-bound hi) nil))
1510 (setf (numeric-type-high result)
1511 (if lo (negate-bound lo) nil))
1515 #!-propagate-float-type
1516 (defoptimizer (abs derive-type) ((num))
1517 (let ((type (continuation-type num)))
1518 (if (and (numeric-type-p type)
1519 (eq (numeric-type-class type) 'integer)
1520 (eq (numeric-type-complexp type) :real))
1521 (let ((lo (numeric-type-low type))
1522 (hi (numeric-type-high type)))
1523 (make-numeric-type :class 'integer :complexp :real
1524 :low (cond ((and hi (minusp hi))
1530 :high (if (and hi lo)
1531 (max (abs hi) (abs lo))
1533 (numeric-contagion type type))))
1535 #!+propagate-float-type
1536 (defun abs-derive-type-aux (type)
1537 (cond ((eq (numeric-type-complexp type) :complex)
1538 ;; The absolute value of a complex number is always a
1539 ;; non-negative float.
1540 (let* ((format (case (numeric-type-class type)
1541 ((integer rational) 'single-float)
1542 (t (numeric-type-format type))))
1543 (bound-format (or format 'float)))
1544 (make-numeric-type :class 'float
1547 :low (coerce 0 bound-format)
1550 ;; The absolute value of a real number is a non-negative real
1551 ;; of the same type.
1552 (let* ((abs-bnd (interval-abs (numeric-type->interval type)))
1553 (class (numeric-type-class type))
1554 (format (numeric-type-format type))
1555 (bound-type (or format class 'real)))
1560 :low (coerce-numeric-bound (interval-low abs-bnd) bound-type)
1561 :high (coerce-numeric-bound
1562 (interval-high abs-bnd) bound-type))))))
1564 #!+propagate-float-type
1565 (defoptimizer (abs derive-type) ((num))
1566 (one-arg-derive-type num #'abs-derive-type-aux #'abs))
1568 #!-propagate-float-type
1569 (defoptimizer (truncate derive-type) ((number divisor))
1570 (let ((number-type (continuation-type number))
1571 (divisor-type (continuation-type divisor))
1572 (integer-type (specifier-type 'integer)))
1573 (if (and (numeric-type-p number-type)
1574 (csubtypep number-type integer-type)
1575 (numeric-type-p divisor-type)
1576 (csubtypep divisor-type integer-type))
1577 (let ((number-low (numeric-type-low number-type))
1578 (number-high (numeric-type-high number-type))
1579 (divisor-low (numeric-type-low divisor-type))
1580 (divisor-high (numeric-type-high divisor-type)))
1581 (values-specifier-type
1582 `(values ,(integer-truncate-derive-type number-low number-high
1583 divisor-low divisor-high)
1584 ,(integer-rem-derive-type number-low number-high
1585 divisor-low divisor-high))))
1588 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
1590 #!+propagate-float-type
1593 (defun rem-result-type (number-type divisor-type)
1594 ;; Figure out what the remainder type is. The remainder is an
1595 ;; integer if both args are integers; a rational if both args are
1596 ;; rational; and a float otherwise.
1597 (cond ((and (csubtypep number-type (specifier-type 'integer))
1598 (csubtypep divisor-type (specifier-type 'integer)))
1600 ((and (csubtypep number-type (specifier-type 'rational))
1601 (csubtypep divisor-type (specifier-type 'rational)))
1603 ((and (csubtypep number-type (specifier-type 'float))
1604 (csubtypep divisor-type (specifier-type 'float)))
1605 ;; Both are floats so the result is also a float, of
1606 ;; the largest type.
1607 (or (float-format-max (numeric-type-format number-type)
1608 (numeric-type-format divisor-type))
1610 ((and (csubtypep number-type (specifier-type 'float))
1611 (csubtypep divisor-type (specifier-type 'rational)))
1612 ;; One of the arguments is a float and the other is a
1613 ;; rational. The remainder is a float of the same
1615 (or (numeric-type-format number-type) 'float))
1616 ((and (csubtypep divisor-type (specifier-type 'float))
1617 (csubtypep number-type (specifier-type 'rational)))
1618 ;; One of the arguments is a float and the other is a
1619 ;; rational. The remainder is a float of the same
1621 (or (numeric-type-format divisor-type) 'float))
1623 ;; Some unhandled combination. This usually means both args
1624 ;; are REAL so the result is a REAL.
1627 (defun truncate-derive-type-quot (number-type divisor-type)
1628 (let* ((rem-type (rem-result-type number-type divisor-type))
1629 (number-interval (numeric-type->interval number-type))
1630 (divisor-interval (numeric-type->interval divisor-type)))
1631 ;;(declare (type (member '(integer rational float)) rem-type))
1632 ;; We have real numbers now.
1633 (cond ((eq rem-type 'integer)
1634 ;; Since the remainder type is INTEGER, both args are
1636 (let* ((res (integer-truncate-derive-type
1637 (interval-low number-interval)
1638 (interval-high number-interval)
1639 (interval-low divisor-interval)
1640 (interval-high divisor-interval))))
1641 (specifier-type (if (listp res) res 'integer))))
1643 (let ((quot (truncate-quotient-bound
1644 (interval-div number-interval
1645 divisor-interval))))
1646 (specifier-type `(integer ,(or (interval-low quot) '*)
1647 ,(or (interval-high quot) '*))))))))
1649 (defun truncate-derive-type-rem (number-type divisor-type)
1650 (let* ((rem-type (rem-result-type number-type divisor-type))
1651 (number-interval (numeric-type->interval number-type))
1652 (divisor-interval (numeric-type->interval divisor-type))
1653 (rem (truncate-rem-bound number-interval divisor-interval)))
1654 ;;(declare (type (member '(integer rational float)) rem-type))
1655 ;; We have real numbers now.
1656 (cond ((eq rem-type 'integer)
1657 ;; Since the remainder type is INTEGER, both args are
1659 (specifier-type `(,rem-type ,(or (interval-low rem) '*)
1660 ,(or (interval-high rem) '*))))
1662 (multiple-value-bind (class format)
1665 (values 'integer nil))
1667 (values 'rational nil))
1668 ((or single-float double-float #!+long-float long-float)
1669 (values 'float rem-type))
1671 (values 'float nil))
1674 (when (member rem-type '(float single-float double-float
1675 #!+long-float long-float))
1676 (setf rem (interval-func #'(lambda (x)
1677 (coerce x rem-type))
1679 (make-numeric-type :class class
1681 :low (interval-low rem)
1682 :high (interval-high rem)))))))
1684 (defun truncate-derive-type-quot-aux (num div same-arg)
1685 (declare (ignore same-arg))
1686 (if (and (numeric-type-real-p num)
1687 (numeric-type-real-p div))
1688 (truncate-derive-type-quot num div)
1691 (defun truncate-derive-type-rem-aux (num div same-arg)
1692 (declare (ignore same-arg))
1693 (if (and (numeric-type-real-p num)
1694 (numeric-type-real-p div))
1695 (truncate-derive-type-rem num div)
1698 (defoptimizer (truncate derive-type) ((number divisor))
1699 (let ((quot (two-arg-derive-type number divisor
1700 #'truncate-derive-type-quot-aux #'truncate))
1701 (rem (two-arg-derive-type number divisor
1702 #'truncate-derive-type-rem-aux #'rem)))
1703 (when (and quot rem)
1704 (make-values-type :required (list quot rem)))))
1706 (defun ftruncate-derive-type-quot (number-type divisor-type)
1707 ;; The bounds are the same as for truncate. However, the first
1708 ;; result is a float of some type. We need to determine what that
1709 ;; type is. Basically it's the more contagious of the two types.
1710 (let ((q-type (truncate-derive-type-quot number-type divisor-type))
1711 (res-type (numeric-contagion number-type divisor-type)))
1712 (make-numeric-type :class 'float
1713 :format (numeric-type-format res-type)
1714 :low (numeric-type-low q-type)
1715 :high (numeric-type-high q-type))))
1717 (defun ftruncate-derive-type-quot-aux (n d same-arg)
1718 (declare (ignore same-arg))
1719 (if (and (numeric-type-real-p n)
1720 (numeric-type-real-p d))
1721 (ftruncate-derive-type-quot n d)
1724 (defoptimizer (ftruncate derive-type) ((number divisor))
1726 (two-arg-derive-type number divisor
1727 #'ftruncate-derive-type-quot-aux #'ftruncate))
1728 (rem (two-arg-derive-type number divisor
1729 #'truncate-derive-type-rem-aux #'rem)))
1730 (when (and quot rem)
1731 (make-values-type :required (list quot rem)))))
1733 (defun %unary-truncate-derive-type-aux (number)
1734 (truncate-derive-type-quot number (specifier-type '(integer 1 1))))
1736 (defoptimizer (%unary-truncate derive-type) ((number))
1737 (one-arg-derive-type number
1738 #'%unary-truncate-derive-type-aux
1741 ;;; Define optimizers for FLOOR and CEILING.
1743 ((frob-opt (name q-name r-name)
1744 (let ((q-aux (symbolicate q-name "-AUX"))
1745 (r-aux (symbolicate r-name "-AUX")))
1747 ;; Compute type of quotient (first) result
1748 (defun ,q-aux (number-type divisor-type)
1749 (let* ((number-interval
1750 (numeric-type->interval number-type))
1752 (numeric-type->interval divisor-type))
1753 (quot (,q-name (interval-div number-interval
1754 divisor-interval))))
1755 (specifier-type `(integer ,(or (interval-low quot) '*)
1756 ,(or (interval-high quot) '*)))))
1757 ;; Compute type of remainder
1758 (defun ,r-aux (number-type divisor-type)
1759 (let* ((divisor-interval
1760 (numeric-type->interval divisor-type))
1761 (rem (,r-name divisor-interval))
1762 (result-type (rem-result-type number-type divisor-type)))
1763 (multiple-value-bind (class format)
1766 (values 'integer nil))
1768 (values 'rational nil))
1769 ((or single-float double-float #!+long-float long-float)
1770 (values 'float result-type))
1772 (values 'float nil))
1775 (when (member result-type '(float single-float double-float
1776 #!+long-float long-float))
1777 ;; Make sure the limits on the interval have
1779 (setf rem (interval-func #'(lambda (x)
1780 (coerce x result-type))
1782 (make-numeric-type :class class
1784 :low (interval-low rem)
1785 :high (interval-high rem)))))
1786 ;; The optimizer itself
1787 (defoptimizer (,name derive-type) ((number divisor))
1788 (flet ((derive-q (n d same-arg)
1789 (declare (ignore same-arg))
1790 (if (and (numeric-type-real-p n)
1791 (numeric-type-real-p d))
1794 (derive-r (n d same-arg)
1795 (declare (ignore same-arg))
1796 (if (and (numeric-type-real-p n)
1797 (numeric-type-real-p d))
1800 (let ((quot (two-arg-derive-type
1801 number divisor #'derive-q #',name))
1802 (rem (two-arg-derive-type
1803 number divisor #'derive-r #'mod)))
1804 (when (and quot rem)
1805 (make-values-type :required (list quot rem))))))
1808 ;; FIXME: DEF-FROB-OPT, not just FROB-OPT
1809 (frob-opt floor floor-quotient-bound floor-rem-bound)
1810 (frob-opt ceiling ceiling-quotient-bound ceiling-rem-bound))
1812 ;;; Define optimizers for FFLOOR and FCEILING
1814 ((frob-opt (name q-name r-name)
1815 (let ((q-aux (symbolicate "F" q-name "-AUX"))
1816 (r-aux (symbolicate r-name "-AUX")))
1818 ;; Compute type of quotient (first) result
1819 (defun ,q-aux (number-type divisor-type)
1820 (let* ((number-interval
1821 (numeric-type->interval number-type))
1823 (numeric-type->interval divisor-type))
1824 (quot (,q-name (interval-div number-interval
1826 (res-type (numeric-contagion number-type divisor-type)))
1828 :class (numeric-type-class res-type)
1829 :format (numeric-type-format res-type)
1830 :low (interval-low quot)
1831 :high (interval-high quot))))
1833 (defoptimizer (,name derive-type) ((number divisor))
1834 (flet ((derive-q (n d same-arg)
1835 (declare (ignore same-arg))
1836 (if (and (numeric-type-real-p n)
1837 (numeric-type-real-p d))
1840 (derive-r (n d same-arg)
1841 (declare (ignore same-arg))
1842 (if (and (numeric-type-real-p n)
1843 (numeric-type-real-p d))
1846 (let ((quot (two-arg-derive-type
1847 number divisor #'derive-q #',name))
1848 (rem (two-arg-derive-type
1849 number divisor #'derive-r #'mod)))
1850 (when (and quot rem)
1851 (make-values-type :required (list quot rem))))))))))
1853 ;; FIXME: DEF-FROB-OPT, not just FROB-OPT
1854 (frob-opt ffloor floor-quotient-bound floor-rem-bound)
1855 (frob-opt fceiling ceiling-quotient-bound ceiling-rem-bound))
1857 ;;; Functions to compute the bounds on the quotient and remainder for
1858 ;;; the FLOOR function.
1859 (defun floor-quotient-bound (quot)
1860 ;; Take the floor of the quotient and then massage it into what we
1862 (let ((lo (interval-low quot))
1863 (hi (interval-high quot)))
1864 ;; Take the floor of the lower bound. The result is always a
1865 ;; closed lower bound.
1867 (floor (bound-value lo))
1869 ;; For the upper bound, we need to be careful
1872 ;; An open bound. We need to be careful here because
1873 ;; the floor of '(10.0) is 9, but the floor of
1875 (multiple-value-bind (q r) (floor (first hi))
1880 ;; A closed bound, so the answer is obvious.
1884 (make-interval :low lo :high hi)))
1885 (defun floor-rem-bound (div)
1886 ;; The remainder depends only on the divisor. Try to get the
1887 ;; correct sign for the remainder if we can.
1888 (case (interval-range-info div)
1890 ;; Divisor is always positive.
1891 (let ((rem (interval-abs div)))
1892 (setf (interval-low rem) 0)
1893 (when (and (numberp (interval-high rem))
1894 (not (zerop (interval-high rem))))
1895 ;; The remainder never contains the upper bound. However,
1896 ;; watch out for the case where the high limit is zero!
1897 (setf (interval-high rem) (list (interval-high rem))))
1900 ;; Divisor is always negative
1901 (let ((rem (interval-neg (interval-abs div))))
1902 (setf (interval-high rem) 0)
1903 (when (numberp (interval-low rem))
1904 ;; The remainder never contains the lower bound.
1905 (setf (interval-low rem) (list (interval-low rem))))
1908 ;; The divisor can be positive or negative. All bets off.
1909 ;; The magnitude of remainder is the maximum value of the
1911 (let ((limit (bound-value (interval-high (interval-abs div)))))
1912 ;; The bound never reaches the limit, so make the interval open
1913 (make-interval :low (if limit
1916 :high (list limit))))))
1918 (floor-quotient-bound (make-interval :low 0.3 :high 10.3))
1919 => #S(INTERVAL :LOW 0 :HIGH 10)
1920 (floor-quotient-bound (make-interval :low 0.3 :high '(10.3)))
1921 => #S(INTERVAL :LOW 0 :HIGH 10)
1922 (floor-quotient-bound (make-interval :low 0.3 :high 10))
1923 => #S(INTERVAL :LOW 0 :HIGH 10)
1924 (floor-quotient-bound (make-interval :low 0.3 :high '(10)))
1925 => #S(INTERVAL :LOW 0 :HIGH 9)
1926 (floor-quotient-bound (make-interval :low '(0.3) :high 10.3))
1927 => #S(INTERVAL :LOW 0 :HIGH 10)
1928 (floor-quotient-bound (make-interval :low '(0.0) :high 10.3))
1929 => #S(INTERVAL :LOW 0 :HIGH 10)
1930 (floor-quotient-bound (make-interval :low '(-1.3) :high 10.3))
1931 => #S(INTERVAL :LOW -2 :HIGH 10)
1932 (floor-quotient-bound (make-interval :low '(-1.0) :high 10.3))
1933 => #S(INTERVAL :LOW -1 :HIGH 10)
1934 (floor-quotient-bound (make-interval :low -1.0 :high 10.3))
1935 => #S(INTERVAL :LOW -1 :HIGH 10)
1937 (floor-rem-bound (make-interval :low 0.3 :high 10.3))
1938 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
1939 (floor-rem-bound (make-interval :low 0.3 :high '(10.3)))
1940 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
1941 (floor-rem-bound (make-interval :low -10 :high -2.3))
1942 #S(INTERVAL :LOW (-10) :HIGH 0)
1943 (floor-rem-bound (make-interval :low 0.3 :high 10))
1944 => #S(INTERVAL :LOW 0 :HIGH '(10))
1945 (floor-rem-bound (make-interval :low '(-1.3) :high 10.3))
1946 => #S(INTERVAL :LOW '(-10.3) :HIGH '(10.3))
1947 (floor-rem-bound (make-interval :low '(-20.3) :high 10.3))
1948 => #S(INTERVAL :LOW (-20.3) :HIGH (20.3))
1951 ;;; same functions for CEILING
1952 (defun ceiling-quotient-bound (quot)
1953 ;; Take the ceiling of the quotient and then massage it into what we
1955 (let ((lo (interval-low quot))
1956 (hi (interval-high quot)))
1957 ;; Take the ceiling of the upper bound. The result is always a
1958 ;; closed upper bound.
1960 (ceiling (bound-value hi))
1962 ;; For the lower bound, we need to be careful
1965 ;; An open bound. We need to be careful here because
1966 ;; the ceiling of '(10.0) is 11, but the ceiling of
1968 (multiple-value-bind (q r) (ceiling (first lo))
1973 ;; A closed bound, so the answer is obvious.
1977 (make-interval :low lo :high hi)))
1978 (defun ceiling-rem-bound (div)
1979 ;; The remainder depends only on the divisor. Try to get the
1980 ;; correct sign for the remainder if we can.
1982 (case (interval-range-info div)
1984 ;; Divisor is always positive. The remainder is negative.
1985 (let ((rem (interval-neg (interval-abs div))))
1986 (setf (interval-high rem) 0)
1987 (when (and (numberp (interval-low rem))
1988 (not (zerop (interval-low rem))))
1989 ;; The remainder never contains the upper bound. However,
1990 ;; watch out for the case when the upper bound is zero!
1991 (setf (interval-low rem) (list (interval-low rem))))
1994 ;; Divisor is always negative. The remainder is positive
1995 (let ((rem (interval-abs div)))
1996 (setf (interval-low rem) 0)
1997 (when (numberp (interval-high rem))
1998 ;; The remainder never contains the lower bound.
1999 (setf (interval-high rem) (list (interval-high rem))))
2002 ;; The divisor can be positive or negative. All bets off.
2003 ;; The magnitude of remainder is the maximum value of the
2005 (let ((limit (bound-value (interval-high (interval-abs div)))))
2006 ;; The bound never reaches the limit, so make the interval open
2007 (make-interval :low (if limit
2010 :high (list limit))))))
2013 (ceiling-quotient-bound (make-interval :low 0.3 :high 10.3))
2014 => #S(INTERVAL :LOW 1 :HIGH 11)
2015 (ceiling-quotient-bound (make-interval :low 0.3 :high '(10.3)))
2016 => #S(INTERVAL :LOW 1 :HIGH 11)
2017 (ceiling-quotient-bound (make-interval :low 0.3 :high 10))
2018 => #S(INTERVAL :LOW 1 :HIGH 10)
2019 (ceiling-quotient-bound (make-interval :low 0.3 :high '(10)))
2020 => #S(INTERVAL :LOW 1 :HIGH 10)
2021 (ceiling-quotient-bound (make-interval :low '(0.3) :high 10.3))
2022 => #S(INTERVAL :LOW 1 :HIGH 11)
2023 (ceiling-quotient-bound (make-interval :low '(0.0) :high 10.3))
2024 => #S(INTERVAL :LOW 1 :HIGH 11)
2025 (ceiling-quotient-bound (make-interval :low '(-1.3) :high 10.3))
2026 => #S(INTERVAL :LOW -1 :HIGH 11)
2027 (ceiling-quotient-bound (make-interval :low '(-1.0) :high 10.3))
2028 => #S(INTERVAL :LOW 0 :HIGH 11)
2029 (ceiling-quotient-bound (make-interval :low -1.0 :high 10.3))
2030 => #S(INTERVAL :LOW -1 :HIGH 11)
2032 (ceiling-rem-bound (make-interval :low 0.3 :high 10.3))
2033 => #S(INTERVAL :LOW (-10.3) :HIGH 0)
2034 (ceiling-rem-bound (make-interval :low 0.3 :high '(10.3)))
2035 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
2036 (ceiling-rem-bound (make-interval :low -10 :high -2.3))
2037 => #S(INTERVAL :LOW 0 :HIGH (10))
2038 (ceiling-rem-bound (make-interval :low 0.3 :high 10))
2039 => #S(INTERVAL :LOW (-10) :HIGH 0)
2040 (ceiling-rem-bound (make-interval :low '(-1.3) :high 10.3))
2041 => #S(INTERVAL :LOW (-10.3) :HIGH (10.3))
2042 (ceiling-rem-bound (make-interval :low '(-20.3) :high 10.3))
2043 => #S(INTERVAL :LOW (-20.3) :HIGH (20.3))
2046 (defun truncate-quotient-bound (quot)
2047 ;; For positive quotients, truncate is exactly like floor. For
2048 ;; negative quotients, truncate is exactly like ceiling. Otherwise,
2049 ;; it's the union of the two pieces.
2050 (case (interval-range-info quot)
2053 (floor-quotient-bound quot))
2055 ;; Just like ceiling
2056 (ceiling-quotient-bound quot))
2058 ;; Split the interval into positive and negative pieces, compute
2059 ;; the result for each piece and put them back together.
2060 (destructuring-bind (neg pos) (interval-split 0 quot t t)
2061 (interval-merge-pair (ceiling-quotient-bound neg)
2062 (floor-quotient-bound pos))))))
2064 (defun truncate-rem-bound (num div)
2065 ;; This is significantly more complicated than floor or ceiling. We
2066 ;; need both the number and the divisor to determine the range. The
2067 ;; basic idea is to split the ranges of num and den into positive
2068 ;; and negative pieces and deal with each of the four possibilities
2070 (case (interval-range-info num)
2072 (case (interval-range-info div)
2074 (floor-rem-bound div))
2076 (ceiling-rem-bound div))
2078 (destructuring-bind (neg pos) (interval-split 0 div t t)
2079 (interval-merge-pair (truncate-rem-bound num neg)
2080 (truncate-rem-bound num pos))))))
2082 (case (interval-range-info div)
2084 (ceiling-rem-bound div))
2086 (floor-rem-bound div))
2088 (destructuring-bind (neg pos) (interval-split 0 div t t)
2089 (interval-merge-pair (truncate-rem-bound num neg)
2090 (truncate-rem-bound num pos))))))
2092 (destructuring-bind (neg pos) (interval-split 0 num t t)
2093 (interval-merge-pair (truncate-rem-bound neg div)
2094 (truncate-rem-bound pos div))))))
2097 ;;; Derive useful information about the range. Returns three values:
2098 ;;; - '+ if its positive, '- negative, or nil if it overlaps 0.
2099 ;;; - The abs of the minimal value (i.e. closest to 0) in the range.
2100 ;;; - The abs of the maximal value if there is one, or nil if it is
2102 (defun numeric-range-info (low high)
2103 (cond ((and low (not (minusp low)))
2104 (values '+ low high))
2105 ((and high (not (plusp high)))
2106 (values '- (- high) (if low (- low) nil)))
2108 (values nil 0 (and low high (max (- low) high))))))
2110 (defun integer-truncate-derive-type
2111 (number-low number-high divisor-low divisor-high)
2112 ;; The result cannot be larger in magnitude than the number, but the sign
2113 ;; might change. If we can determine the sign of either the number or
2114 ;; the divisor, we can eliminate some of the cases.
2115 (multiple-value-bind (number-sign number-min number-max)
2116 (numeric-range-info number-low number-high)
2117 (multiple-value-bind (divisor-sign divisor-min divisor-max)
2118 (numeric-range-info divisor-low divisor-high)
2119 (when (and divisor-max (zerop divisor-max))
2120 ;; We've got a problem: guaranteed division by zero.
2121 (return-from integer-truncate-derive-type t))
2122 (when (zerop divisor-min)
2123 ;; We'll assume that they aren't going to divide by zero.
2125 (cond ((and number-sign divisor-sign)
2126 ;; We know the sign of both.
2127 (if (eq number-sign divisor-sign)
2128 ;; Same sign, so the result will be positive.
2129 `(integer ,(if divisor-max
2130 (truncate number-min divisor-max)
2133 (truncate number-max divisor-min)
2135 ;; Different signs, the result will be negative.
2136 `(integer ,(if number-max
2137 (- (truncate number-max divisor-min))
2140 (- (truncate number-min divisor-max))
2142 ((eq divisor-sign '+)
2143 ;; The divisor is positive. Therefore, the number will just
2144 ;; become closer to zero.
2145 `(integer ,(if number-low
2146 (truncate number-low divisor-min)
2149 (truncate number-high divisor-min)
2151 ((eq divisor-sign '-)
2152 ;; The divisor is negative. Therefore, the absolute value of
2153 ;; the number will become closer to zero, but the sign will also
2155 `(integer ,(if number-high
2156 (- (truncate number-high divisor-min))
2159 (- (truncate number-low divisor-min))
2161 ;; The divisor could be either positive or negative.
2163 ;; The number we are dividing has a bound. Divide that by the
2164 ;; smallest posible divisor.
2165 (let ((bound (truncate number-max divisor-min)))
2166 `(integer ,(- bound) ,bound)))
2168 ;; The number we are dividing is unbounded, so we can't tell
2169 ;; anything about the result.
2172 #!-propagate-float-type
2173 (defun integer-rem-derive-type
2174 (number-low number-high divisor-low divisor-high)
2175 (if (and divisor-low divisor-high)
2176 ;; We know the range of the divisor, and the remainder must be smaller
2177 ;; than the divisor. We can tell the sign of the remainer if we know
2178 ;; the sign of the number.
2179 (let ((divisor-max (1- (max (abs divisor-low) (abs divisor-high)))))
2180 `(integer ,(if (or (null number-low)
2181 (minusp number-low))
2184 ,(if (or (null number-high)
2185 (plusp number-high))
2188 ;; The divisor is potentially either very positive or very negative.
2189 ;; Therefore, the remainer is unbounded, but we might be able to tell
2190 ;; something about the sign from the number.
2191 `(integer ,(if (and number-low (not (minusp number-low)))
2192 ;; The number we are dividing is positive. Therefore,
2193 ;; the remainder must be positive.
2196 ,(if (and number-high (not (plusp number-high)))
2197 ;; The number we are dividing is negative. Therefore,
2198 ;; the remainder must be negative.
2202 #!-propagate-float-type
2203 (defoptimizer (random derive-type) ((bound &optional state))
2204 (let ((type (continuation-type bound)))
2205 (when (numeric-type-p type)
2206 (let ((class (numeric-type-class type))
2207 (high (numeric-type-high type))
2208 (format (numeric-type-format type)))
2212 :low (coerce 0 (or format class 'real))
2213 :high (cond ((not high) nil)
2214 ((eq class 'integer) (max (1- high) 0))
2215 ((or (consp high) (zerop high)) high)
2218 #!+propagate-float-type
2219 (defun random-derive-type-aux (type)
2220 (let ((class (numeric-type-class type))
2221 (high (numeric-type-high type))
2222 (format (numeric-type-format type)))
2226 :low (coerce 0 (or format class 'real))
2227 :high (cond ((not high) nil)
2228 ((eq class 'integer) (max (1- high) 0))
2229 ((or (consp high) (zerop high)) high)
2232 #!+propagate-float-type
2233 (defoptimizer (random derive-type) ((bound &optional state))
2234 (one-arg-derive-type bound #'random-derive-type-aux nil))
2236 ;;;; logical derive-type methods
2238 ;;; Return the maximum number of bits an integer of the supplied type can take
2239 ;;; up, or NIL if it is unbounded. The second (third) value is T if the
2240 ;;; integer can be positive (negative) and NIL if not. Zero counts as
2242 (defun integer-type-length (type)
2243 (if (numeric-type-p type)
2244 (let ((min (numeric-type-low type))
2245 (max (numeric-type-high type)))
2246 (values (and min max (max (integer-length min) (integer-length max)))
2247 (or (null max) (not (minusp max)))
2248 (or (null min) (minusp min))))
2251 #!-propagate-fun-type
2253 (defoptimizer (logand derive-type) ((x y))
2254 (multiple-value-bind (x-len x-pos x-neg)
2255 (integer-type-length (continuation-type x))
2256 (declare (ignore x-pos))
2257 (multiple-value-bind (y-len y-pos y-neg)
2258 (integer-type-length (continuation-type y))
2259 (declare (ignore y-pos))
2261 ;; X must be positive.
2263 ;; The must both be positive.
2264 (cond ((or (null x-len) (null y-len))
2265 (specifier-type 'unsigned-byte))
2266 ((or (zerop x-len) (zerop y-len))
2267 (specifier-type '(integer 0 0)))
2269 (specifier-type `(unsigned-byte ,(min x-len y-len)))))
2270 ;; X is positive, but Y might be negative.
2272 (specifier-type 'unsigned-byte))
2274 (specifier-type '(integer 0 0)))
2276 (specifier-type `(unsigned-byte ,x-len)))))
2277 ;; X might be negative.
2279 ;; Y must be positive.
2281 (specifier-type 'unsigned-byte))
2283 (specifier-type '(integer 0 0)))
2286 `(unsigned-byte ,y-len))))
2287 ;; Either might be negative.
2288 (if (and x-len y-len)
2289 ;; The result is bounded.
2290 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2291 ;; We can't tell squat about the result.
2292 (specifier-type 'integer)))))))
2294 (defoptimizer (logior derive-type) ((x y))
2295 (multiple-value-bind (x-len x-pos x-neg)
2296 (integer-type-length (continuation-type x))
2297 (multiple-value-bind (y-len y-pos y-neg)
2298 (integer-type-length (continuation-type y))
2300 ((and (not x-neg) (not y-neg))
2301 ;; Both are positive.
2302 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2306 ;; X must be negative.
2308 ;; Both are negative. The result is going to be negative and be
2309 ;; the same length or shorter than the smaller.
2310 (if (and x-len y-len)
2312 (specifier-type `(integer ,(ash -1 (min x-len y-len)) -1))
2314 (specifier-type '(integer * -1)))
2315 ;; X is negative, but we don't know about Y. The result will be
2316 ;; negative, but no more negative than X.
2318 `(integer ,(or (numeric-type-low (continuation-type x)) '*)
2321 ;; X might be either positive or negative.
2323 ;; But Y is negative. The result will be negative.
2325 `(integer ,(or (numeric-type-low (continuation-type y)) '*)
2327 ;; We don't know squat about either. It won't get any bigger.
2328 (if (and x-len y-len)
2330 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2332 (specifier-type 'integer))))))))
2334 (defoptimizer (logxor derive-type) ((x y))
2335 (multiple-value-bind (x-len x-pos x-neg)
2336 (integer-type-length (continuation-type x))
2337 (multiple-value-bind (y-len y-pos y-neg)
2338 (integer-type-length (continuation-type y))
2340 ((or (and (not x-neg) (not y-neg))
2341 (and (not x-pos) (not y-pos)))
2342 ;; Either both are negative or both are positive. The result will be
2343 ;; positive, and as long as the longer.
2344 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2347 ((or (and (not x-pos) (not y-neg))
2348 (and (not y-neg) (not y-pos)))
2349 ;; Either X is negative and Y is positive of vice-verca. The result
2350 ;; will be negative.
2351 (specifier-type `(integer ,(if (and x-len y-len)
2352 (ash -1 (max x-len y-len))
2355 ;; We can't tell what the sign of the result is going to be. All we
2356 ;; know is that we don't create new bits.
2358 (specifier-type `(signed-byte ,(1+ (max x-len y-len)))))
2360 (specifier-type 'integer))))))
2364 #!+propagate-fun-type
2366 (defun logand-derive-type-aux (x y &optional same-leaf)
2367 (declare (ignore same-leaf))
2368 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2369 (declare (ignore x-pos))
2370 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2371 (declare (ignore y-pos))
2373 ;; X must be positive.
2375 ;; The must both be positive.
2376 (cond ((or (null x-len) (null y-len))
2377 (specifier-type 'unsigned-byte))
2378 ((or (zerop x-len) (zerop y-len))
2379 (specifier-type '(integer 0 0)))
2381 (specifier-type `(unsigned-byte ,(min x-len y-len)))))
2382 ;; X is positive, but Y might be negative.
2384 (specifier-type 'unsigned-byte))
2386 (specifier-type '(integer 0 0)))
2388 (specifier-type `(unsigned-byte ,x-len)))))
2389 ;; X might be negative.
2391 ;; Y must be positive.
2393 (specifier-type 'unsigned-byte))
2395 (specifier-type '(integer 0 0)))
2398 `(unsigned-byte ,y-len))))
2399 ;; Either might be negative.
2400 (if (and x-len y-len)
2401 ;; The result is bounded.
2402 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2403 ;; We can't tell squat about the result.
2404 (specifier-type 'integer)))))))
2406 (defun logior-derive-type-aux (x y &optional same-leaf)
2407 (declare (ignore same-leaf))
2408 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2409 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2411 ((and (not x-neg) (not y-neg))
2412 ;; Both are positive.
2413 (if (and x-len y-len (zerop x-len) (zerop y-len))
2414 (specifier-type '(integer 0 0))
2415 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2419 ;; X must be negative.
2421 ;; Both are negative. The result is going to be negative and be
2422 ;; the same length or shorter than the smaller.
2423 (if (and x-len y-len)
2425 (specifier-type `(integer ,(ash -1 (min x-len y-len)) -1))
2427 (specifier-type '(integer * -1)))
2428 ;; X is negative, but we don't know about Y. The result will be
2429 ;; negative, but no more negative than X.
2431 `(integer ,(or (numeric-type-low x) '*)
2434 ;; X might be either positive or negative.
2436 ;; But Y is negative. The result will be negative.
2438 `(integer ,(or (numeric-type-low y) '*)
2440 ;; We don't know squat about either. It won't get any bigger.
2441 (if (and x-len y-len)
2443 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2445 (specifier-type 'integer))))))))
2447 (defun logxor-derive-type-aux (x y &optional same-leaf)
2448 (declare (ignore same-leaf))
2449 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2450 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2452 ((or (and (not x-neg) (not y-neg))
2453 (and (not x-pos) (not y-pos)))
2454 ;; Either both are negative or both are positive. The result will be
2455 ;; positive, and as long as the longer.
2456 (if (and x-len y-len (zerop x-len) (zerop y-len))
2457 (specifier-type '(integer 0 0))
2458 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2461 ((or (and (not x-pos) (not y-neg))
2462 (and (not y-neg) (not y-pos)))
2463 ;; Either X is negative and Y is positive of vice-verca. The result
2464 ;; will be negative.
2465 (specifier-type `(integer ,(if (and x-len y-len)
2466 (ash -1 (max x-len y-len))
2469 ;; We can't tell what the sign of the result is going to be. All we
2470 ;; know is that we don't create new bits.
2472 (specifier-type `(signed-byte ,(1+ (max x-len y-len)))))
2474 (specifier-type 'integer))))))
2476 (macrolet ((frob (logfcn)
2477 (let ((fcn-aux (symbolicate logfcn "-DERIVE-TYPE-AUX")))
2478 `(defoptimizer (,logfcn derive-type) ((x y))
2479 (two-arg-derive-type x y #',fcn-aux #',logfcn)))))
2480 ;; FIXME: DEF-FROB, not just FROB
2485 (defoptimizer (integer-length derive-type) ((x))
2486 (let ((x-type (continuation-type x)))
2487 (when (and (numeric-type-p x-type)
2488 (csubtypep x-type (specifier-type 'integer)))
2489 ;; If the X is of type (INTEGER LO HI), then the integer-length
2490 ;; of X is (INTEGER (min lo hi) (max lo hi), basically. Be
2491 ;; careful about LO or HI being NIL, though. Also, if 0 is
2492 ;; contained in X, the lower bound is obviously 0.
2493 (flet ((null-or-min (a b)
2494 (and a b (min (integer-length a)
2495 (integer-length b))))
2497 (and a b (max (integer-length a)
2498 (integer-length b)))))
2499 (let* ((min (numeric-type-low x-type))
2500 (max (numeric-type-high x-type))
2501 (min-len (null-or-min min max))
2502 (max-len (null-or-max min max)))
2503 (when (ctypep 0 x-type)
2505 (specifier-type `(integer ,(or min-len '*) ,(or max-len '*))))))))
2508 ;;;; miscellaneous derive-type methods
2510 (defoptimizer (code-char derive-type) ((code))
2511 (specifier-type 'base-char))
2513 (defoptimizer (values derive-type) ((&rest values))
2514 (values-specifier-type
2515 `(values ,@(mapcar #'(lambda (x)
2516 (type-specifier (continuation-type x)))
2519 ;;;; byte operations
2521 ;;;; We try to turn byte operations into simple logical operations. First, we
2522 ;;;; convert byte specifiers into separate size and position arguments passed
2523 ;;;; to internal %FOO functions. We then attempt to transform the %FOO
2524 ;;;; functions into boolean operations when the size and position are constant
2525 ;;;; and the operands are fixnums.
2527 (macrolet (;; Evaluate body with Size-Var and Pos-Var bound to expressions that
2528 ;; evaluate to the Size and Position of the byte-specifier form
2529 ;; Spec. We may wrap a let around the result of the body to bind
2532 ;; If the spec is a Byte form, then bind the vars to the subforms.
2533 ;; otherwise, evaluate Spec and use the Byte-Size and Byte-Position.
2534 ;; The goal of this transformation is to avoid consing up byte
2535 ;; specifiers and then immediately throwing them away.
2536 (with-byte-specifier ((size-var pos-var spec) &body body)
2537 (once-only ((spec `(macroexpand ,spec))
2539 `(if (and (consp ,spec)
2540 (eq (car ,spec) 'byte)
2541 (= (length ,spec) 3))
2542 (let ((,size-var (second ,spec))
2543 (,pos-var (third ,spec)))
2545 (let ((,size-var `(byte-size ,,temp))
2546 (,pos-var `(byte-position ,,temp)))
2547 `(let ((,,temp ,,spec))
2550 (def-source-transform ldb (spec int)
2551 (with-byte-specifier (size pos spec)
2552 `(%ldb ,size ,pos ,int)))
2554 (def-source-transform dpb (newbyte spec int)
2555 (with-byte-specifier (size pos spec)
2556 `(%dpb ,newbyte ,size ,pos ,int)))
2558 (def-source-transform mask-field (spec int)
2559 (with-byte-specifier (size pos spec)
2560 `(%mask-field ,size ,pos ,int)))
2562 (def-source-transform deposit-field (newbyte spec int)
2563 (with-byte-specifier (size pos spec)
2564 `(%deposit-field ,newbyte ,size ,pos ,int))))
2566 (defoptimizer (%ldb derive-type) ((size posn num))
2567 (let ((size (continuation-type size)))
2568 (if (and (numeric-type-p size)
2569 (csubtypep size (specifier-type 'integer)))
2570 (let ((size-high (numeric-type-high size)))
2571 (if (and size-high (<= size-high sb!vm:word-bits))
2572 (specifier-type `(unsigned-byte ,size-high))
2573 (specifier-type 'unsigned-byte)))
2576 (defoptimizer (%mask-field derive-type) ((size posn num))
2577 (let ((size (continuation-type size))
2578 (posn (continuation-type posn)))
2579 (if (and (numeric-type-p size)
2580 (csubtypep size (specifier-type 'integer))
2581 (numeric-type-p posn)
2582 (csubtypep posn (specifier-type 'integer)))
2583 (let ((size-high (numeric-type-high size))
2584 (posn-high (numeric-type-high posn)))
2585 (if (and size-high posn-high
2586 (<= (+ size-high posn-high) sb!vm:word-bits))
2587 (specifier-type `(unsigned-byte ,(+ size-high posn-high)))
2588 (specifier-type 'unsigned-byte)))
2591 (defoptimizer (%dpb derive-type) ((newbyte size posn int))
2592 (let ((size (continuation-type size))
2593 (posn (continuation-type posn))
2594 (int (continuation-type int)))
2595 (if (and (numeric-type-p size)
2596 (csubtypep size (specifier-type 'integer))
2597 (numeric-type-p posn)
2598 (csubtypep posn (specifier-type 'integer))
2599 (numeric-type-p int)
2600 (csubtypep int (specifier-type 'integer)))
2601 (let ((size-high (numeric-type-high size))
2602 (posn-high (numeric-type-high posn))
2603 (high (numeric-type-high int))
2604 (low (numeric-type-low int)))
2605 (if (and size-high posn-high high low
2606 (<= (+ size-high posn-high) sb!vm:word-bits))
2608 (list (if (minusp low) 'signed-byte 'unsigned-byte)
2609 (max (integer-length high)
2610 (integer-length low)
2611 (+ size-high posn-high))))
2615 (defoptimizer (%deposit-field derive-type) ((newbyte size posn int))
2616 (let ((size (continuation-type size))
2617 (posn (continuation-type posn))
2618 (int (continuation-type int)))
2619 (if (and (numeric-type-p size)
2620 (csubtypep size (specifier-type 'integer))
2621 (numeric-type-p posn)
2622 (csubtypep posn (specifier-type 'integer))
2623 (numeric-type-p int)
2624 (csubtypep int (specifier-type 'integer)))
2625 (let ((size-high (numeric-type-high size))
2626 (posn-high (numeric-type-high posn))
2627 (high (numeric-type-high int))
2628 (low (numeric-type-low int)))
2629 (if (and size-high posn-high high low
2630 (<= (+ size-high posn-high) sb!vm:word-bits))
2632 (list (if (minusp low) 'signed-byte 'unsigned-byte)
2633 (max (integer-length high)
2634 (integer-length low)
2635 (+ size-high posn-high))))
2639 (deftransform %ldb ((size posn int)
2640 (fixnum fixnum integer)
2641 (unsigned-byte #.sb!vm:word-bits))
2642 "convert to inline logical ops"
2643 `(logand (ash int (- posn))
2644 (ash ,(1- (ash 1 sb!vm:word-bits))
2645 (- size ,sb!vm:word-bits))))
2647 (deftransform %mask-field ((size posn int)
2648 (fixnum fixnum integer)
2649 (unsigned-byte #.sb!vm:word-bits))
2650 "convert to inline logical ops"
2652 (ash (ash ,(1- (ash 1 sb!vm:word-bits))
2653 (- size ,sb!vm:word-bits))
2656 ;;; Note: for %DPB and %DEPOSIT-FIELD, we can't use
2657 ;;; (OR (SIGNED-BYTE N) (UNSIGNED-BYTE N))
2658 ;;; as the result type, as that would allow result types
2659 ;;; that cover the range -2^(n-1) .. 1-2^n, instead of allowing result types
2660 ;;; of (UNSIGNED-BYTE N) and result types of (SIGNED-BYTE N).
2662 (deftransform %dpb ((new size posn int)
2664 (unsigned-byte #.sb!vm:word-bits))
2665 "convert to inline logical ops"
2666 `(let ((mask (ldb (byte size 0) -1)))
2667 (logior (ash (logand new mask) posn)
2668 (logand int (lognot (ash mask posn))))))
2670 (deftransform %dpb ((new size posn int)
2672 (signed-byte #.sb!vm:word-bits))
2673 "convert to inline logical ops"
2674 `(let ((mask (ldb (byte size 0) -1)))
2675 (logior (ash (logand new mask) posn)
2676 (logand int (lognot (ash mask posn))))))
2678 (deftransform %deposit-field ((new size posn int)
2680 (unsigned-byte #.sb!vm:word-bits))
2681 "convert to inline logical ops"
2682 `(let ((mask (ash (ldb (byte size 0) -1) posn)))
2683 (logior (logand new mask)
2684 (logand int (lognot mask)))))
2686 (deftransform %deposit-field ((new size posn int)
2688 (signed-byte #.sb!vm:word-bits))
2689 "convert to inline logical ops"
2690 `(let ((mask (ash (ldb (byte size 0) -1) posn)))
2691 (logior (logand new mask)
2692 (logand int (lognot mask)))))
2694 ;;; miscellanous numeric transforms
2696 ;;; If a constant appears as the first arg, swap the args.
2697 (deftransform commutative-arg-swap ((x y) * * :defun-only t :node node)
2698 (if (and (constant-continuation-p x)
2699 (not (constant-continuation-p y)))
2700 `(,(continuation-function-name (basic-combination-fun node))
2702 ,(continuation-value x))
2703 (give-up-ir1-transform)))
2705 (dolist (x '(= char= + * logior logand logxor))
2706 (%deftransform x '(function * *) #'commutative-arg-swap
2707 "place constant arg last."))
2709 ;;; Handle the case of a constant BOOLE-CODE.
2710 (deftransform boole ((op x y) * * :when :both)
2711 "convert to inline logical ops"
2712 (unless (constant-continuation-p op)
2713 (give-up-ir1-transform "BOOLE code is not a constant."))
2714 (let ((control (continuation-value op)))
2720 (#.boole-c1 '(lognot x))
2721 (#.boole-c2 '(lognot y))
2722 (#.boole-and '(logand x y))
2723 (#.boole-ior '(logior x y))
2724 (#.boole-xor '(logxor x y))
2725 (#.boole-eqv '(logeqv x y))
2726 (#.boole-nand '(lognand x y))
2727 (#.boole-nor '(lognor x y))
2728 (#.boole-andc1 '(logandc1 x y))
2729 (#.boole-andc2 '(logandc2 x y))
2730 (#.boole-orc1 '(logorc1 x y))
2731 (#.boole-orc2 '(logorc2 x y))
2733 (abort-ir1-transform "~S is an illegal control arg to BOOLE."
2736 ;;;; converting special case multiply/divide to shifts
2738 ;;; If arg is a constant power of two, turn * into a shift.
2739 (deftransform * ((x y) (integer integer) * :when :both)
2740 "convert x*2^k to shift"
2741 (unless (constant-continuation-p y)
2742 (give-up-ir1-transform))
2743 (let* ((y (continuation-value y))
2745 (len (1- (integer-length y-abs))))
2746 (unless (= y-abs (ash 1 len))
2747 (give-up-ir1-transform))
2752 ;;; If both arguments and the result are (unsigned-byte 32), try to come up
2753 ;;; with a ``better'' multiplication using multiplier recoding. There are two
2754 ;;; different ways the multiplier can be recoded. The more obvious is to shift
2755 ;;; X by the correct amount for each bit set in Y and to sum the results. But
2756 ;;; if there is a string of bits that are all set, you can add X shifted by
2757 ;;; one more then the bit position of the first set bit and subtract X shifted
2758 ;;; by the bit position of the last set bit. We can't use this second method
2759 ;;; when the high order bit is bit 31 because shifting by 32 doesn't work
2761 (deftransform * ((x y)
2762 ((unsigned-byte 32) (unsigned-byte 32))
2764 "recode as shift and add"
2765 (unless (constant-continuation-p y)
2766 (give-up-ir1-transform))
2767 (let ((y (continuation-value y))
2770 (labels ((tub32 (x) `(truly-the (unsigned-byte 32) ,x))
2775 `(+ ,result ,(tub32 next-factor))
2777 (declare (inline add))
2778 (dotimes (bitpos 32)
2780 (when (not (logbitp bitpos y))
2781 (add (if (= (1+ first-one) bitpos)
2782 ;; There is only a single bit in the string.
2784 ;; There are at least two.
2785 `(- ,(tub32 `(ash x ,bitpos))
2786 ,(tub32 `(ash x ,first-one)))))
2787 (setf first-one nil))
2788 (when (logbitp bitpos y)
2789 (setf first-one bitpos))))
2791 (cond ((= first-one 31))
2795 (add `(- ,(tub32 '(ash x 31)) ,(tub32 `(ash x ,first-one))))))
2799 ;;; If arg is a constant power of two, turn FLOOR into a shift and mask.
2800 ;;; If CEILING, add in (1- (ABS Y)) and then do FLOOR.
2801 (flet ((frob (y ceil-p)
2802 (unless (constant-continuation-p y)
2803 (give-up-ir1-transform))
2804 (let* ((y (continuation-value y))
2806 (len (1- (integer-length y-abs))))
2807 (unless (= y-abs (ash 1 len))
2808 (give-up-ir1-transform))
2809 (let ((shift (- len))
2811 `(let ,(when ceil-p `((x (+ x ,(1- y-abs)))))
2813 `(values (ash (- x) ,shift)
2814 (- (logand (- x) ,mask)))
2815 `(values (ash x ,shift)
2816 (logand x ,mask))))))))
2817 (deftransform floor ((x y) (integer integer) *)
2818 "convert division by 2^k to shift"
2820 (deftransform ceiling ((x y) (integer integer) *)
2821 "convert division by 2^k to shift"
2824 ;;; Do the same for MOD.
2825 (deftransform mod ((x y) (integer integer) * :when :both)
2826 "convert remainder mod 2^k to LOGAND"
2827 (unless (constant-continuation-p y)
2828 (give-up-ir1-transform))
2829 (let* ((y (continuation-value y))
2831 (len (1- (integer-length y-abs))))
2832 (unless (= y-abs (ash 1 len))
2833 (give-up-ir1-transform))
2834 (let ((mask (1- y-abs)))
2836 `(- (logand (- x) ,mask))
2837 `(logand x ,mask)))))
2839 ;;; If arg is a constant power of two, turn TRUNCATE into a shift and mask.
2840 (deftransform truncate ((x y) (integer integer))
2841 "convert division by 2^k to shift"
2842 (unless (constant-continuation-p y)
2843 (give-up-ir1-transform))
2844 (let* ((y (continuation-value y))
2846 (len (1- (integer-length y-abs))))
2847 (unless (= y-abs (ash 1 len))
2848 (give-up-ir1-transform))
2849 (let* ((shift (- len))
2852 (values ,(if (minusp y)
2854 `(- (ash (- x) ,shift)))
2855 (- (logand (- x) ,mask)))
2856 (values ,(if (minusp y)
2857 `(- (ash (- x) ,shift))
2859 (logand x ,mask))))))
2861 ;;; And the same for REM.
2862 (deftransform rem ((x y) (integer integer) * :when :both)
2863 "convert remainder mod 2^k to LOGAND"
2864 (unless (constant-continuation-p y)
2865 (give-up-ir1-transform))
2866 (let* ((y (continuation-value y))
2868 (len (1- (integer-length y-abs))))
2869 (unless (= y-abs (ash 1 len))
2870 (give-up-ir1-transform))
2871 (let ((mask (1- y-abs)))
2873 (- (logand (- x) ,mask))
2874 (logand x ,mask)))))
2876 ;;;; arithmetic and logical identity operation elimination
2878 ;;;; Flush calls to various arith functions that convert to the identity
2879 ;;;; function or a constant.
2881 (dolist (stuff '((ash 0 x)
2886 (logxor -1 (lognot x))
2888 (destructuring-bind (name identity result) stuff
2889 (deftransform name ((x y) `(* (constant-argument (member ,identity))) '*
2890 :eval-name t :when :both)
2891 "fold identity operations"
2894 ;;; These are restricted to rationals, because (- 0 0.0) is 0.0, not -0.0, and
2895 ;;; (* 0 -4.0) is -0.0.
2896 (deftransform - ((x y) ((constant-argument (member 0)) rational) *
2898 "convert (- 0 x) to negate"
2900 (deftransform * ((x y) (rational (constant-argument (member 0))) *
2902 "convert (* x 0) to 0."
2905 ;;; Return T if in an arithmetic op including continuations X and Y, the
2906 ;;; result type is not affected by the type of X. That is, Y is at least as
2907 ;;; contagious as X.
2909 (defun not-more-contagious (x y)
2910 (declare (type continuation x y))
2911 (let ((x (continuation-type x))
2912 (y (continuation-type y)))
2913 (values (type= (numeric-contagion x y)
2914 (numeric-contagion y y)))))
2915 ;;; Patched version by Raymond Toy. dtc: Should be safer although it
2916 ;;; needs more work as valid transforms are missed; some cases are
2917 ;;; specific to particular transform functions so the use of this
2918 ;;; function may need a re-think.
2919 (defun not-more-contagious (x y)
2920 (declare (type continuation x y))
2921 (flet ((simple-numeric-type (num)
2922 (and (numeric-type-p num)
2923 ;; Return non-NIL if NUM is integer, rational, or a float
2924 ;; of some type (but not FLOAT)
2925 (case (numeric-type-class num)
2929 (numeric-type-format num))
2932 (let ((x (continuation-type x))
2933 (y (continuation-type y)))
2934 (if (and (simple-numeric-type x)
2935 (simple-numeric-type y))
2936 (values (type= (numeric-contagion x y)
2937 (numeric-contagion y y)))))))
2941 ;;; If y is not constant, not zerop, or is contagious, or a
2942 ;;; positive float +0.0 then give up.
2943 (deftransform + ((x y) (t (constant-argument t)) * :when :both)
2945 (let ((val (continuation-value y)))
2946 (unless (and (zerop val)
2947 (not (and (floatp val) (plusp (float-sign val))))
2948 (not-more-contagious y x))
2949 (give-up-ir1-transform)))
2954 ;;; If y is not constant, not zerop, or is contagious, or a
2955 ;;; negative float -0.0 then give up.
2956 (deftransform - ((x y) (t (constant-argument t)) * :when :both)
2958 (let ((val (continuation-value y)))
2959 (unless (and (zerop val)
2960 (not (and (floatp val) (minusp (float-sign val))))
2961 (not-more-contagious y x))
2962 (give-up-ir1-transform)))
2965 ;;; Fold (OP x +/-1)
2966 (dolist (stuff '((* x (%negate x))
2969 (destructuring-bind (name result minus-result) stuff
2970 (deftransform name ((x y) '(t (constant-argument real)) '* :eval-name t
2972 "fold identity operations"
2973 (let ((val (continuation-value y)))
2974 (unless (and (= (abs val) 1)
2975 (not-more-contagious y x))
2976 (give-up-ir1-transform))
2977 (if (minusp val) minus-result result)))))
2979 ;;; Fold (expt x n) into multiplications for small integral values of
2980 ;;; N; convert (expt x 1/2) to sqrt.
2981 (deftransform expt ((x y) (t (constant-argument real)) *)
2982 "recode as multiplication or sqrt"
2983 (let ((val (continuation-value y)))
2984 ;; If Y would cause the result to be promoted to the same type as
2985 ;; Y, we give up. If not, then the result will be the same type
2986 ;; as X, so we can replace the exponentiation with simple
2987 ;; multiplication and division for small integral powers.
2988 (unless (not-more-contagious y x)
2989 (give-up-ir1-transform))
2990 (cond ((zerop val) '(float 1 x))
2991 ((= val 2) '(* x x))
2992 ((= val -2) '(/ (* x x)))
2993 ((= val 3) '(* x x x))
2994 ((= val -3) '(/ (* x x x)))
2995 ((= val 1/2) '(sqrt x))
2996 ((= val -1/2) '(/ (sqrt x)))
2997 (t (give-up-ir1-transform)))))
2999 ;;; KLUDGE: Shouldn't (/ 0.0 0.0), etc. cause exceptions in these
3000 ;;; transformations?
3001 ;;; Perhaps we should have to prove that the denominator is nonzero before
3002 ;;; doing them? (Also the DOLIST over macro calls is weird. Perhaps
3003 ;;; just FROB?) -- WHN 19990917
3004 (dolist (name '(ash /))
3005 (deftransform name ((x y) '((constant-argument (integer 0 0)) integer) '*
3006 :eval-name t :when :both)
3009 (dolist (name '(truncate round floor ceiling))
3010 (deftransform name ((x y) '((constant-argument (integer 0 0)) integer) '*
3011 :eval-name t :when :both)
3015 ;;;; character operations
3017 (deftransform char-equal ((a b) (base-char base-char))
3019 '(let* ((ac (char-code a))
3021 (sum (logxor ac bc)))
3023 (when (eql sum #x20)
3024 (let ((sum (+ ac bc)))
3025 (and (> sum 161) (< sum 213)))))))
3027 (deftransform char-upcase ((x) (base-char))
3029 '(let ((n-code (char-code x)))
3030 (if (and (> n-code #o140) ; Octal 141 is #\a.
3031 (< n-code #o173)) ; Octal 172 is #\z.
3032 (code-char (logxor #x20 n-code))
3035 (deftransform char-downcase ((x) (base-char))
3037 '(let ((n-code (char-code x)))
3038 (if (and (> n-code 64) ; 65 is #\A.
3039 (< n-code 91)) ; 90 is #\Z.
3040 (code-char (logxor #x20 n-code))
3043 ;;;; equality predicate transforms
3045 ;;; Return true if X and Y are continuations whose only use is a reference
3046 ;;; to the same leaf, and the value of the leaf cannot change.
3047 (defun same-leaf-ref-p (x y)
3048 (declare (type continuation x y))
3049 (let ((x-use (continuation-use x))
3050 (y-use (continuation-use y)))
3053 (eq (ref-leaf x-use) (ref-leaf y-use))
3054 (constant-reference-p x-use))))
3056 ;;; If X and Y are the same leaf, then the result is true. Otherwise, if
3057 ;;; there is no intersection between the types of the arguments, then the
3058 ;;; result is definitely false.
3059 (deftransform simple-equality-transform ((x y) * * :defun-only t
3061 (cond ((same-leaf-ref-p x y)
3063 ((not (types-intersect (continuation-type x) (continuation-type y)))
3066 (give-up-ir1-transform))))
3068 (dolist (x '(eq char= equal))
3069 (%deftransform x '(function * *) #'simple-equality-transform))
3071 ;;; Similar to SIMPLE-EQUALITY-PREDICATE, except that we also try to convert
3072 ;;; to a type-specific predicate or EQ:
3073 ;;; -- If both args are characters, convert to CHAR=. This is better than just
3074 ;;; converting to EQ, since CHAR= may have special compilation strategies
3075 ;;; for non-standard representations, etc.
3076 ;;; -- If either arg is definitely not a number, then we can compare with EQ.
3077 ;;; -- Otherwise, we try to put the arg we know more about second. If X is
3078 ;;; constant then we put it second. If X is a subtype of Y, we put it
3079 ;;; second. These rules make it easier for the back end to match these
3080 ;;; interesting cases.
3081 ;;; -- If Y is a fixnum, then we quietly pass because the back end can handle
3082 ;;; that case, otherwise give an efficency note.
3083 (deftransform eql ((x y) * * :when :both)
3084 "convert to simpler equality predicate"
3085 (let ((x-type (continuation-type x))
3086 (y-type (continuation-type y))
3087 (char-type (specifier-type 'character))
3088 (number-type (specifier-type 'number)))
3089 (cond ((same-leaf-ref-p x y)
3091 ((not (types-intersect x-type y-type))
3093 ((and (csubtypep x-type char-type)
3094 (csubtypep y-type char-type))
3096 ((or (not (types-intersect x-type number-type))
3097 (not (types-intersect y-type number-type)))
3099 ((and (not (constant-continuation-p y))
3100 (or (constant-continuation-p x)
3101 (and (csubtypep x-type y-type)
3102 (not (csubtypep y-type x-type)))))
3105 (give-up-ir1-transform)))))
3107 ;;; Convert to EQL if both args are rational and complexp is specified
3108 ;;; and the same for both.
3109 (deftransform = ((x y) * * :when :both)
3111 (let ((x-type (continuation-type x))
3112 (y-type (continuation-type y)))
3113 (if (and (csubtypep x-type (specifier-type 'number))
3114 (csubtypep y-type (specifier-type 'number)))
3115 (cond ((or (and (csubtypep x-type (specifier-type 'float))
3116 (csubtypep y-type (specifier-type 'float)))
3117 (and (csubtypep x-type (specifier-type '(complex float)))
3118 (csubtypep y-type (specifier-type '(complex float)))))
3119 ;; They are both floats. Leave as = so that -0.0 is
3120 ;; handled correctly.
3121 (give-up-ir1-transform))
3122 ((or (and (csubtypep x-type (specifier-type 'rational))
3123 (csubtypep y-type (specifier-type 'rational)))
3124 (and (csubtypep x-type (specifier-type '(complex rational)))
3125 (csubtypep y-type (specifier-type '(complex rational)))))
3126 ;; They are both rationals and complexp is the same. Convert
3130 (give-up-ir1-transform
3131 "The operands might not be the same type.")))
3132 (give-up-ir1-transform
3133 "The operands might not be the same type."))))
3135 ;;; If Cont's type is a numeric type, then return the type, otherwise
3136 ;;; GIVE-UP-IR1-TRANSFORM.
3137 (defun numeric-type-or-lose (cont)
3138 (declare (type continuation cont))
3139 (let ((res (continuation-type cont)))
3140 (unless (numeric-type-p res) (give-up-ir1-transform))
3143 ;;; See whether we can statically determine (< X Y) using type information.
3144 ;;; If X's high bound is < Y's low, then X < Y. Similarly, if X's low is >=
3145 ;;; to Y's high, the X >= Y (so return NIL). If not, at least make sure any
3146 ;;; constant arg is second.
3148 ;;; KLUDGE: Why should constant argument be second? It would be nice to find
3149 ;;; out and explain. -- WHN 19990917
3150 #!-propagate-float-type
3151 (defun ir1-transform-< (x y first second inverse)
3152 (if (same-leaf-ref-p x y)
3154 (let* ((x-type (numeric-type-or-lose x))
3155 (x-lo (numeric-type-low x-type))
3156 (x-hi (numeric-type-high x-type))
3157 (y-type (numeric-type-or-lose y))
3158 (y-lo (numeric-type-low y-type))
3159 (y-hi (numeric-type-high y-type)))
3160 (cond ((and x-hi y-lo (< x-hi y-lo))
3162 ((and y-hi x-lo (>= x-lo y-hi))
3164 ((and (constant-continuation-p first)
3165 (not (constant-continuation-p second)))
3168 (give-up-ir1-transform))))))
3169 #!+propagate-float-type
3170 (defun ir1-transform-< (x y first second inverse)
3171 (if (same-leaf-ref-p x y)
3173 (let ((xi (numeric-type->interval (numeric-type-or-lose x)))
3174 (yi (numeric-type->interval (numeric-type-or-lose y))))
3175 (cond ((interval-< xi yi)
3177 ((interval->= xi yi)
3179 ((and (constant-continuation-p first)
3180 (not (constant-continuation-p second)))
3183 (give-up-ir1-transform))))))
3185 (deftransform < ((x y) (integer integer) * :when :both)
3186 (ir1-transform-< x y x y '>))
3188 (deftransform > ((x y) (integer integer) * :when :both)
3189 (ir1-transform-< y x x y '<))
3191 #!+propagate-float-type
3192 (deftransform < ((x y) (float float) * :when :both)
3193 (ir1-transform-< x y x y '>))
3195 #!+propagate-float-type
3196 (deftransform > ((x y) (float float) * :when :both)
3197 (ir1-transform-< y x x y '<))
3199 ;;;; converting N-arg comparisons
3201 ;;;; We convert calls to N-arg comparison functions such as < into
3202 ;;;; two-arg calls. This transformation is enabled for all such
3203 ;;;; comparisons in this file. If any of these predicates are not
3204 ;;;; open-coded, then the transformation should be removed at some
3205 ;;;; point to avoid pessimization.
3207 ;;; This function is used for source transformation of N-arg
3208 ;;; comparison functions other than inequality. We deal both with
3209 ;;; converting to two-arg calls and inverting the sense of the test,
3210 ;;; if necessary. If the call has two args, then we pass or return a
3211 ;;; negated test as appropriate. If it is a degenerate one-arg call,
3212 ;;; then we transform to code that returns true. Otherwise, we bind
3213 ;;; all the arguments and expand into a bunch of IFs.
3214 (declaim (ftype (function (symbol list boolean) *) multi-compare))
3215 (defun multi-compare (predicate args not-p)
3216 (let ((nargs (length args)))
3217 (cond ((< nargs 1) (values nil t))
3218 ((= nargs 1) `(progn ,@args t))
3221 `(if (,predicate ,(first args) ,(second args)) nil t)
3224 (do* ((i (1- nargs) (1- i))
3226 (current (gensym) (gensym))
3227 (vars (list current) (cons current vars))
3228 (result 't (if not-p
3229 `(if (,predicate ,current ,last)
3231 `(if (,predicate ,current ,last)
3234 `((lambda ,vars ,result) . ,args)))))))
3236 (def-source-transform = (&rest args) (multi-compare '= args nil))
3237 (def-source-transform < (&rest args) (multi-compare '< args nil))
3238 (def-source-transform > (&rest args) (multi-compare '> args nil))
3239 (def-source-transform <= (&rest args) (multi-compare '> args t))
3240 (def-source-transform >= (&rest args) (multi-compare '< args t))
3242 (def-source-transform char= (&rest args) (multi-compare 'char= args nil))
3243 (def-source-transform char< (&rest args) (multi-compare 'char< args nil))
3244 (def-source-transform char> (&rest args) (multi-compare 'char> args nil))
3245 (def-source-transform char<= (&rest args) (multi-compare 'char> args t))
3246 (def-source-transform char>= (&rest args) (multi-compare 'char< args t))
3248 (def-source-transform char-equal (&rest args) (multi-compare 'char-equal args nil))
3249 (def-source-transform char-lessp (&rest args) (multi-compare 'char-lessp args nil))
3250 (def-source-transform char-greaterp (&rest args) (multi-compare 'char-greaterp args nil))
3251 (def-source-transform char-not-greaterp (&rest args) (multi-compare 'char-greaterp args t))
3252 (def-source-transform char-not-lessp (&rest args) (multi-compare 'char-lessp args t))
3254 ;;; This function does source transformation of N-arg inequality
3255 ;;; functions such as /=. This is similar to Multi-Compare in the <3
3256 ;;; arg cases. If there are more than two args, then we expand into
3257 ;;; the appropriate n^2 comparisons only when speed is important.
3258 (declaim (ftype (function (symbol list) *) multi-not-equal))
3259 (defun multi-not-equal (predicate args)
3260 (let ((nargs (length args)))
3261 (cond ((< nargs 1) (values nil t))
3262 ((= nargs 1) `(progn ,@args t))
3264 `(if (,predicate ,(first args) ,(second args)) nil t))
3265 ((not (policy nil (>= speed space) (>= speed cspeed)))
3268 (let ((vars (make-gensym-list nargs)))
3269 (do ((var vars next)
3270 (next (cdr vars) (cdr next))
3273 `((lambda ,vars ,result) . ,args))
3274 (let ((v1 (first var)))
3276 (setq result `(if (,predicate ,v1 ,v2) nil ,result))))))))))
3278 (def-source-transform /= (&rest args) (multi-not-equal '= args))
3279 (def-source-transform char/= (&rest args) (multi-not-equal 'char= args))
3280 (def-source-transform char-not-equal (&rest args) (multi-not-equal 'char-equal args))
3282 ;;; Expand MAX and MIN into the obvious comparisons.
3283 (def-source-transform max (arg &rest more-args)
3284 (if (null more-args)
3286 (once-only ((arg1 arg)
3287 (arg2 `(max ,@more-args)))
3288 `(if (> ,arg1 ,arg2)
3290 (def-source-transform min (arg &rest more-args)
3291 (if (null more-args)
3293 (once-only ((arg1 arg)
3294 (arg2 `(min ,@more-args)))
3295 `(if (< ,arg1 ,arg2)
3298 ;;;; converting N-arg arithmetic functions
3300 ;;;; N-arg arithmetic and logic functions are associated into two-arg
3301 ;;;; versions, and degenerate cases are flushed.
3303 ;;; Left-associate First-Arg and More-Args using Function.
3304 (declaim (ftype (function (symbol t list) list) associate-arguments))
3305 (defun associate-arguments (function first-arg more-args)
3306 (let ((next (rest more-args))
3307 (arg (first more-args)))
3309 `(,function ,first-arg ,arg)
3310 (associate-arguments function `(,function ,first-arg ,arg) next))))
3312 ;;; Do source transformations for transitive functions such as +.
3313 ;;; One-arg cases are replaced with the arg and zero arg cases with
3314 ;;; the identity. If Leaf-Fun is true, then replace two-arg calls with
3315 ;;; a call to that function.
3316 (defun source-transform-transitive (fun args identity &optional leaf-fun)
3317 (declare (symbol fun leaf-fun) (list args))
3320 (1 `(values ,(first args)))
3322 `(,leaf-fun ,(first args) ,(second args))
3325 (associate-arguments fun (first args) (rest args)))))
3327 (def-source-transform + (&rest args) (source-transform-transitive '+ args 0))
3328 (def-source-transform * (&rest args) (source-transform-transitive '* args 1))
3329 (def-source-transform logior (&rest args) (source-transform-transitive 'logior args 0))
3330 (def-source-transform logxor (&rest args) (source-transform-transitive 'logxor args 0))
3331 (def-source-transform logand (&rest args) (source-transform-transitive 'logand args -1))
3333 (def-source-transform logeqv (&rest args)
3334 (if (evenp (length args))
3335 `(lognot (logxor ,@args))
3338 ;;; Note: we can't use SOURCE-TRANSFORM-TRANSITIVE for GCD and LCM
3339 ;;; because when they are given one argument, they return its absolute
3342 (def-source-transform gcd (&rest args)
3345 (1 `(abs (the integer ,(first args))))
3347 (t (associate-arguments 'gcd (first args) (rest args)))))
3349 (def-source-transform lcm (&rest args)
3352 (1 `(abs (the integer ,(first args))))
3354 (t (associate-arguments 'lcm (first args) (rest args)))))
3356 ;;; Do source transformations for intransitive n-arg functions such as
3357 ;;; /. With one arg, we form the inverse. With two args we pass.
3358 ;;; Otherwise we associate into two-arg calls.
3359 (declaim (ftype (function (symbol list t) list) source-transform-intransitive))
3360 (defun source-transform-intransitive (function args inverse)
3362 ((0 2) (values nil t))
3363 (1 `(,@inverse ,(first args)))
3364 (t (associate-arguments function (first args) (rest args)))))
3366 (def-source-transform - (&rest args)
3367 (source-transform-intransitive '- args '(%negate)))
3368 (def-source-transform / (&rest args)
3369 (source-transform-intransitive '/ args '(/ 1)))
3373 ;;; We convert APPLY into MULTIPLE-VALUE-CALL so that the compiler
3374 ;;; only needs to understand one kind of variable-argument call. It is
3375 ;;; more efficient to convert APPLY to MV-CALL than MV-CALL to APPLY.
3376 (def-source-transform apply (fun arg &rest more-args)
3377 (let ((args (cons arg more-args)))
3378 `(multiple-value-call ,fun
3379 ,@(mapcar #'(lambda (x)
3382 (values-list ,(car (last args))))))
3386 ;;;; If the control string is a compile-time constant, then replace it
3387 ;;;; with a use of the FORMATTER macro so that the control string is
3388 ;;;; ``compiled.'' Furthermore, if the destination is either a stream
3389 ;;;; or T and the control string is a function (i.e. FORMATTER), then
3390 ;;;; convert the call to FORMAT to just a FUNCALL of that function.
3392 (deftransform format ((dest control &rest args) (t simple-string &rest t) *
3393 :policy (> speed space))
3394 (unless (constant-continuation-p control)
3395 (give-up-ir1-transform "The control string is not a constant."))
3396 (let ((arg-names (make-gensym-list (length args))))
3397 `(lambda (dest control ,@arg-names)
3398 (declare (ignore control))
3399 (format dest (formatter ,(continuation-value control)) ,@arg-names))))
3401 (deftransform format ((stream control &rest args) (stream function &rest t) *
3402 :policy (> speed space))
3403 (let ((arg-names (make-gensym-list (length args))))
3404 `(lambda (stream control ,@arg-names)
3405 (funcall control stream ,@arg-names)
3408 (deftransform format ((tee control &rest args) ((member t) function &rest t) *
3409 :policy (> speed space))
3410 (let ((arg-names (make-gensym-list (length args))))
3411 `(lambda (tee control ,@arg-names)
3412 (declare (ignore tee))
3413 (funcall control *standard-output* ,@arg-names)