Fix REPLACE and REPLACE transforms when copying zero elements.
[sbcl.git] / src / compiler / seqtran.lisp
blob8fed4ce34c8dd6ce4955e5d7cf88b5192e4caa65
1 ;;;; optimizers for list and sequence functions
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!C")
14 ;;; Regarding several reader-conditionalized MUFFLE-CONDITION declarations
15 ;;; throughout this file, here's why: Transforms produce sexprs that as far
16 ;;; as the compiler is concerned could have been user-supplied.
17 ;;; In as much as the forms mention a type-name, that name had best be
18 ;;; recognized, or else it's a style-warning (or worse).
19 ;;; And in cross-compilation, COMPILER-NOTE isn't known early enough for
20 ;;; the transforms in this file to refer to a known type.
21 ;;; But mysteriously the xc would report a style-warning about COMPILER-NOTE
22 ;;; - or any descendant - being unknown, and then go on with life.
23 ;;; How was this possible? Well, it's only trying to _parse_ the type
24 ;;; on account of the declarations saying to muffle things of that type.
25 ;;; Indeed the declaration merits a warning.
26 ;;; But this is an extremely sad and confusing state of affairs,
27 ;;; because while we expect some CODE-DELETION-NOTEs, we don't expect to see
28 ;;; that CODE-DELETION-NOTE is an undefined type.
29 ;;; Alternatively, we could invent DEFINE!-CONDITION which would cause
30 ;;; the cross-compiler to be born knowing all the required types.
31 ;;; Moreover, it would be nice if some of the declarations were commented
32 ;;; with their reason for existence.
35 ;;;; mapping onto lists: the MAPFOO functions
37 ;; This expander allows a compiler-macro for FN to take effect by eliding
38 ;; a LET binding of it. Attempting to self-optimize like that isn't the usual
39 ;; SBCL way, however this is a countermeasure to an inhibition of a later
40 ;; optimization, and it is not an onerous change to the expander.
41 ;; We've gone to the trouble of inlining MAPfoo, but the inlined code
42 ;; prevented use of a compiler-macro because %FUNCALL (as opposed to FUNCALL)
43 ;; is not recognized. "Fixing" the compiler to understand %FUNCALL being
44 ;; the same isn't enough: the funarg must be a literal form because we can't
45 ;; know that a variable arg is a never-modified binding of 'F or #'F
46 ;; until IR1 has figured that out, at which point it is too late.
47 ;; [However, see lp# 632368 which asks for something like that.]
49 ;; Also, you might think there to be a subtle difference in behavior from
50 ;; delaying the reference to #'F versus referencing it once. But there is no
51 ;; difference - either way will use the #<fdefn> of F in the call.
52 ;; Of course, it would be ridiculously unportable to to rely on the
53 ;; fact that F can be changed (for its next call) while funcalling it.
55 (defun mapfoo-transform (fn arglists accumulate take-car)
56 (collect ((do-clauses)
57 (args-to-fn)
58 (tests))
59 (let ((n-first (gensym)))
60 (dolist (a (if accumulate
61 arglists
62 `(,n-first ,@(rest arglists))))
63 (let ((v (gensym)))
64 (do-clauses `(,v ,a (cdr ,v)))
65 (tests `(endp ,v))
66 (args-to-fn (if take-car `(car ,v) v))))
68 (binding* (((fn-binding call) (funarg-bind/call-forms fn (args-to-fn)))
69 (endtest `(or ,@(tests))))
70 `(let ,fn-binding
71 ,(ecase accumulate
72 (:nconc
73 (let ((temp (gensym))
74 (map-result (gensym)))
75 `(let ((,map-result
76 ;; MUFFLE- is not injected when cross-compiling.
77 ;; See top of file for explanation.
78 (locally
79 #-sb-xc-host
80 (declare (muffle-conditions compiler-note))
81 (list nil))))
82 (declare (truly-dynamic-extent ,map-result))
83 (do-anonymous ((,temp ,map-result) . ,(do-clauses))
84 (,endtest (cdr ,map-result))
85 (setq ,temp (last (nconc ,temp ,call)))))))
86 (:list
87 (let ((temp (gensym))
88 (map-result (gensym)))
89 `(let ((,map-result
90 ;; MUFFLE- is not injected when cross-compiling.
91 ;; See top of file for explanation.
92 (locally
93 #-sb-xc-host
94 (declare (muffle-conditions compiler-note))
95 (list nil))))
96 (declare (truly-dynamic-extent ,map-result))
97 (do-anonymous ((,temp ,map-result) . ,(do-clauses))
98 (,endtest
99 (%rplacd ,temp nil) ;; replace the 0
100 (truly-the list (cdr ,map-result)))
101 ;; Accumulate using %RPLACD. RPLACD becomes (SETF CDR)
102 ;; which becomes %RPLACD but relies on "defsetfs".
103 ;; This is for effect, not value, so makes no difference.
104 (%rplacd ,temp (setq ,temp
105 ;; 0 is not written to the heap
106 (cons ,call 0)))))))
107 ((nil)
108 `(let ((,n-first ,(first arglists)))
109 (do-anonymous ,(do-clauses)
110 (,endtest (truly-the list ,n-first))
111 ,call)))))))))
113 (define-source-transform mapc (function list &rest more-lists)
114 (mapfoo-transform function (cons list more-lists) nil t))
116 (define-source-transform mapcar (function list &rest more-lists)
117 (mapfoo-transform function (cons list more-lists) :list t))
119 (define-source-transform mapcan (function list &rest more-lists)
120 (mapfoo-transform function (cons list more-lists) :nconc t))
122 (define-source-transform mapl (function list &rest more-lists)
123 (mapfoo-transform function (cons list more-lists) nil nil))
125 (define-source-transform maplist (function list &rest more-lists)
126 (mapfoo-transform function (cons list more-lists) :list nil))
128 (define-source-transform mapcon (function list &rest more-lists)
129 (mapfoo-transform function (cons list more-lists) :nconc nil))
131 ;;;; mapping onto sequences: the MAP function
133 ;;; MAP is %MAP plus a check to make sure that any length specified in
134 ;;; the result type matches the actual result. We also wrap it in a
135 ;;; TRULY-THE for the most specific type we can determine.
136 (deftransform map ((result-type-arg fun seq &rest seqs) * * :node node)
137 (let* ((seq-names (make-gensym-list (1+ (length seqs))))
138 (bare `(%map result-type-arg fun ,@seq-names))
139 (constant-result-type-arg-p (constant-lvar-p result-type-arg))
140 ;; what we know about the type of the result. (Note that the
141 ;; "result type" argument is not necessarily the type of the
142 ;; result, since NIL means the result has NULL type.)
143 (result-type (if (not constant-result-type-arg-p)
144 'consed-sequence
145 (let ((result-type-arg-value
146 (lvar-value result-type-arg)))
147 (if (null result-type-arg-value)
148 'null
149 result-type-arg-value)))))
150 `(lambda (result-type-arg fun ,@seq-names)
151 (truly-the ,result-type
152 ,(cond ((policy node (< safety 3))
153 ;; ANSI requires the length-related type check only
154 ;; when the SAFETY quality is 3... in other cases, we
155 ;; skip it, because it could be expensive.
156 bare)
157 ((not constant-result-type-arg-p)
158 `(sequence-of-checked-length-given-type ,bare
159 result-type-arg))
161 (let ((result-ctype (ir1-transform-specifier-type
162 result-type)))
163 (if (array-type-p result-ctype)
164 (let ((dims (array-type-dimensions result-ctype)))
165 (unless (singleton-p dims)
166 (give-up-ir1-transform "invalid sequence type"))
167 (let ((dim (first dims)))
168 (if (eq dim '*)
169 bare
170 `(vector-of-checked-length-given-length ,bare
171 ,dim))))
172 ;; FIXME: this is wrong, as not all subtypes of
173 ;; VECTOR are ARRAY-TYPEs [consider, for
174 ;; example, (OR (VECTOR T 3) (VECTOR T
175 ;; 4))]. However, it's difficult to see what we
176 ;; should put here... maybe we should
177 ;; GIVE-UP-IR1-TRANSFORM if the type is a
178 ;; subtype of VECTOR but not an ARRAY-TYPE?
179 bare))))))))
181 ;;; Return a DO loop, mapping a function FUN to elements of
182 ;;; sequences. SEQS is a list of lvars, SEQ-NAMES - list of variables,
183 ;;; bound to sequences, INTO - a variable, which is used in
184 ;;; MAP-INTO. RESULT and BODY are forms, which can use variables
185 ;;; FUNCALL-RESULT, containing the result of application of FUN, and
186 ;;; INDEX, containing the current position in sequences.
187 (defun build-sequence-iterator (seqs seq-names &key result into body fast)
188 (declare (type list seqs seq-names)
189 (type symbol into))
190 (collect ((bindings)
191 (declarations)
192 (vector-lengths)
193 (tests)
194 (places)
195 (around))
196 (let ((found-vector-p nil))
197 (flet ((process-vector (length)
198 (unless found-vector-p
199 (setq found-vector-p t)
200 (bindings `(index 0 (1+ index)))
201 (declarations `(type index index)))
202 (vector-lengths length)))
203 (loop for seq of-type lvar in seqs
204 for seq-name in seq-names
205 for type = (lvar-type seq)
206 do (cond ((csubtypep type (specifier-type 'list))
207 (with-unique-names (index)
208 (bindings `(,index ,seq-name (cdr ,index)))
209 (declarations `(type list ,index))
210 (places `(car ,index))
211 (tests `(endp ,index))))
212 ((or (csubtypep type (specifier-type '(simple-array * 1)))
213 (and (not fast)
214 (csubtypep type (specifier-type 'vector))))
215 (process-vector `(length ,seq-name))
216 (places `(locally (declare (optimize (insert-array-bounds-checks 0)))
217 (aref ,seq-name index))))
218 ((csubtypep type (specifier-type 'vector))
219 (let ((data (gensym "DATA"))
220 (start (gensym "START"))
221 (end (gensym "END")))
222 (around `(with-array-data ((,data ,seq-name)
223 (,start)
224 (,end (length ,seq-name)))))
225 (process-vector `(- ,end ,start))
226 (places `(locally (declare (optimize (insert-array-bounds-checks 0)))
227 (aref ,data (truly-the index (+ index ,start)))))))
229 (give-up-ir1-transform
230 "can't determine sequence argument type"))))
231 (when into
232 (process-vector `(array-dimension ,into 0))))
233 (when found-vector-p
234 (bindings `(length (min ,@(vector-lengths))))
235 (tests `(>= index length)))
236 (let ((body `(do (,@(bindings))
237 ((or ,@(tests)) ,result)
238 (declare ,@(declarations))
239 (let ((funcall-result (funcall fun ,@(places))))
240 (declare (ignorable funcall-result))
241 ,body))))
242 (if (around)
243 (reduce (lambda (wrap body) (append wrap (list body)))
244 (around)
245 :from-end t
246 :initial-value body)
247 body)))))
249 ;;; Try to compile %MAP efficiently when we can determine sequence
250 ;;; argument types at compile time.
251 (deftransform %map ((result-type fun seq &rest seqs) * *
252 :node node :policy (>= speed space))
253 "open code"
254 (unless (constant-lvar-p result-type)
255 (give-up-ir1-transform "RESULT-TYPE argument not constant"))
256 (flet ( ;; 1-valued SUBTYPEP, fails unless second value of SUBTYPEP is true
257 (1subtypep (x y)
258 (multiple-value-bind (subtype-p valid-p)
259 (csubtypep x (specifier-type y))
260 (if valid-p
261 subtype-p
262 (give-up-ir1-transform
263 "can't analyze sequence type relationship")))))
264 (let* ((result-type-value (lvar-value result-type))
265 (result-type-ctype (ir1-transform-specifier-type result-type-value))
266 (result-supertype (cond ((null result-type-value) 'null)
267 ((1subtypep result-type-ctype 'vector)
268 'vector)
269 ((1subtypep result-type-ctype 'list)
270 'list)
272 (give-up-ir1-transform
273 "result type unsuitable")))))
274 (cond ((and (eq result-supertype 'list) (null seqs))
275 ;; The consing arity-1 cases can be implemented
276 ;; reasonably efficiently as function calls, and the cost
277 ;; of consing should be significantly larger than
278 ;; function call overhead, so we always compile these
279 ;; cases as full calls regardless of speed-versus-space
280 ;; optimization policy.
281 '(%map-to-list-arity-1 fun seq))
282 ;; (We use the same idiom, of returning a LAMBDA from
283 ;; DEFTRANSFORM, as is used in the DEFTRANSFORMs for
284 ;; FUNCALL and ALIEN-FUNCALL, and for the same
285 ;; reason: we need to get the runtime values of each
286 ;; of the &REST vars.)
287 ((eq result-supertype 'vector)
288 (let* ((all-seqs (cons seq seqs))
289 (seq-args (make-gensym-list (length all-seqs))))
290 `(lambda (result-type fun ,@seq-args)
291 (map-into (make-sequence result-type
292 (min ,@(loop for arg in seq-args
293 collect `(length ,arg))))
294 fun ,@seq-args))))
296 (let* ((all-seqs (cons seq seqs))
297 (seq-args (make-gensym-list (length all-seqs))))
298 (multiple-value-bind (push-dacc result)
299 (ecase result-supertype
300 (null (values nil nil))
301 (list (values `(push funcall-result acc)
302 `(nreverse acc))))
303 (catch-give-up-ir1-transform
304 (`(lambda (result-type fun ,@seq-args)
305 (declare (ignore result-type))
306 (let ((fun (%coerce-callable-to-fun fun))
307 (acc nil))
308 (declare (type list acc))
309 (declare (ignorable acc))
310 ,(build-sequence-iterator
311 all-seqs seq-args
312 :result result
313 :body push-dacc
314 :fast (policy node (> speed space))))))
315 (if (and (null result-type-value) (null seqs))
316 '(%map-for-effect-arity-1 fun seq)
317 (%give-up))))))))))
319 ;;; MAP-INTO
320 (deftransform map-into ((result fun &rest seqs)
321 (vector * &rest *)
322 * :node node)
323 "open code"
324 (let* ((seqs-names (make-gensym-list (length seqs)))
325 (result-type (lvar-type result))
326 (non-complex-vector-type-p (csubtypep result-type
327 (specifier-type '(simple-array * 1)))))
328 (catch-give-up-ir1-transform
329 (`(lambda (result fun ,@seqs-names)
330 ,(if (and (policy node (> speed space))
331 (not non-complex-vector-type-p))
332 (let ((data (gensym "DATA"))
333 (start (gensym "START"))
334 (end (gensym "END")))
335 `(with-array-data ((,data result)
336 (,start)
337 (,end))
338 (declare (ignore ,end))
339 ,(build-sequence-iterator
340 seqs seqs-names
341 :result '(when (array-has-fill-pointer-p result)
342 (setf (fill-pointer result) index))
343 :into 'result
344 :body `(locally (declare (optimize (insert-array-bounds-checks 0)))
345 (setf (aref ,data (truly-the index (+ index ,start)))
346 funcall-result))
347 :fast t)))
348 (build-sequence-iterator
349 seqs seqs-names
350 :result '(when (array-has-fill-pointer-p result)
351 (setf (fill-pointer result) index))
352 :into 'result
353 :body '(locally (declare (optimize (insert-array-bounds-checks 0)))
354 (setf (aref result index) funcall-result))))
355 result))
356 (cond #-sb-xc-host
357 ;; %%vector-map-into-funs%% is not defined in xc
358 ;; if something needs to be faster in the compiler, it
359 ;; should declare the input sequences instead.
360 ((and non-complex-vector-type-p
361 (array-type-p result-type)
362 (not (eq (array-type-specialized-element-type result-type)
363 *wild-type*)))
364 (let ((saetp (find-saetp-by-ctype (array-type-specialized-element-type result-type))))
365 (unless saetp
366 (give-up-ir1-transform "Uknown upgraded array element type of the result"))
367 (let ((mapper (%fun-name (svref sb!impl::%%vector-map-into-funs%%
368 (sb!vm:saetp-typecode saetp)))))
369 `(progn (,mapper result 0 (length result)
370 (%coerce-callable-to-fun fun) seqs)
371 result))))
373 (%give-up))))))
376 ;;; FIXME: once the confusion over doing transforms with known-complex
377 ;;; arrays is over, we should also transform the calls to (AND (ARRAY
378 ;;; * (*)) (NOT (SIMPLE-ARRAY * (*)))) objects.
379 (deftransform elt ((s i) ((simple-array * (*)) *) *)
380 '(aref s i))
382 (deftransform elt ((s i) (list *) * :policy (< safety 3))
383 '(nth i s))
385 (deftransform %setelt ((s i v) ((simple-array * (*)) * *) *)
386 '(setf (aref s i) v))
388 (deftransform %setelt ((s i v) (list * *) * :policy (< safety 3))
389 '(setf (car (nthcdr i s)) v))
391 (deftransform %check-vector-sequence-bounds ((vector start end)
392 (vector * *) *
393 :node node)
394 (if (policy node (= 0 insert-array-bounds-checks))
395 '(or end (length vector))
396 '(let ((length (length vector)))
397 (if (<= 0 start (or end length) length)
398 (or end length)
399 (sequence-bounding-indices-bad-error vector start end)))))
401 (def!type eq-comparable-type ()
402 '(or fixnum #!+64-bit single-float (not number)))
404 ;;; True if EQL comparisons involving type can be simplified to EQ.
405 (defun eq-comparable-type-p (type)
406 (csubtypep type (specifier-type 'eq-comparable-type)))
408 (defun specialized-list-seek-function-name (function-name key-functions &optional variant)
409 (or (find-symbol (with-simple-output-to-string (s)
410 ;; Write "%NAME-FUN1-FUN2-FUN3", etc. Not only is
411 ;; this ever so slightly faster then FORMAT, this
412 ;; way we are also proof against *PRINT-CASE*
413 ;; frobbing and such.
414 (write-char #\% s)
415 (write-string (symbol-name function-name) s)
416 (dolist (f key-functions)
417 (write-char #\- s)
418 (write-string (symbol-name f) s))
419 (when variant
420 (write-char #\- s)
421 (write-string (symbol-name variant) s)))
422 (load-time-value (find-package "SB!KERNEL") t))
423 (bug "Unknown list item seek transform: name=~S, key-functions=~S variant=~S"
424 function-name key-functions variant)))
426 (defparameter *list-open-code-limit* 128)
428 (defun transform-list-item-seek (name item list key test test-not node)
429 (when (and test test-not)
430 (abort-ir1-transform "Both ~S and ~S supplied to ~S." :test :test-not name))
431 ;; If TEST is EQL, drop it.
432 (when (and test (lvar-fun-is test '(eql)))
433 (setf test nil))
434 ;; Ditto for KEY IDENTITY.
435 (when (and key (lvar-fun-is key '(identity)))
436 (setf key nil))
437 ;; Key can legally be NIL, but if it's NIL for sure we pretend it's
438 ;; not there at all. If it might be NIL, make up a form to that
439 ;; ensures it is a function.
440 (multiple-value-bind (key key-form)
441 (when key
442 (let ((key-type (lvar-type key))
443 (null-type (specifier-type 'null)))
444 (cond ((csubtypep key-type null-type)
445 (values nil nil))
446 ((csubtypep null-type key-type)
447 (values key '(if key
448 (%coerce-callable-to-fun key)
449 #'identity)))
451 (values key (ensure-lvar-fun-form key 'key))))))
452 (let* ((c-test (cond ((and test (lvar-fun-is test '(eq)))
453 (setf test nil)
454 'eq)
455 ((and (not test) (not test-not))
456 (when (eq-comparable-type-p (lvar-type item))
457 'eq))))
458 (funs (delete nil (list (when key (list key 'key))
459 (when test (list test 'test))
460 (when test-not (list test-not 'test-not)))))
461 (target-expr (if key '(%funcall key target) 'target))
462 (test-expr (cond (test `(%funcall test item ,target-expr))
463 (test-not `(not (%funcall test-not item ,target-expr)))
464 (c-test `(,c-test item ,target-expr))
465 (t `(eql item ,target-expr)))))
466 (labels ((open-code (tail)
467 (when tail
468 `(if (let ((this ',(car tail)))
469 ,(ecase name
470 ((assoc rassoc)
471 (let ((cxx (if (eq name 'assoc) 'car 'cdr)))
472 `(and this (let ((target (,cxx this)))
473 ,test-expr))))
474 (member
475 `(let ((target this))
476 ,test-expr))))
477 ',(ecase name
478 ((assoc rassoc) (car tail))
479 (member tail))
480 ,(open-code (cdr tail)))))
481 (ensure-fun (args)
482 (if (eq 'key (second args))
483 key-form
484 (apply #'ensure-lvar-fun-form args))))
485 (let* ((cp (constant-lvar-p list))
486 (c-list (when cp (lvar-value list))))
487 (cond ((and cp c-list (member name '(assoc rassoc member))
488 (policy node (>= speed space))
489 (not (nthcdr *list-open-code-limit* c-list)))
490 `(let ,(mapcar (lambda (fun) `(,(second fun) ,(ensure-fun fun))) funs)
491 ,(open-code c-list)))
492 ((and cp (not c-list))
493 ;; constant nil list
494 (if (eq name 'adjoin)
495 '(list item)
496 nil))
498 ;; specialized out-of-line version
499 `(,(specialized-list-seek-function-name name (mapcar #'second funs) c-test)
500 item list ,@(mapcar #'ensure-fun funs)))))))))
502 (defun transform-list-pred-seek (name pred list key node)
503 ;; If KEY is IDENTITY, drop it.
504 (when (and key (lvar-fun-is key '(identity)))
505 (setf key nil))
506 ;; Key can legally be NIL, but if it's NIL for sure we pretend it's
507 ;; not there at all. If it might be NIL, make up a form to that
508 ;; ensures it is a function.
509 (multiple-value-bind (key key-form)
510 (when key
511 (let ((key-type (lvar-type key))
512 (null-type (specifier-type 'null)))
513 (cond ((csubtypep key-type null-type)
514 (values nil nil))
515 ((csubtypep null-type key-type)
516 (values key '(if key
517 (%coerce-callable-to-fun key)
518 #'identity)))
520 (values key (ensure-lvar-fun-form key 'key))))))
521 (let ((test-expr `(%funcall pred ,(if key '(%funcall key target) 'target)))
522 (pred-expr (ensure-lvar-fun-form pred 'pred)))
523 (when (member name '(member-if-not assoc-if-not rassoc-if-not))
524 (setf test-expr `(not ,test-expr)))
525 (labels ((open-code (tail)
526 (when tail
527 `(if (let ((this ',(car tail)))
528 ,(ecase name
529 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
530 (let ((cxx (if (member name '(assoc-if assoc-if-not)) 'car 'cdr)))
531 `(and this (let ((target (,cxx this)))
532 ,test-expr))))
533 ((member-if member-if-not)
534 `(let ((target this))
535 ,test-expr))))
536 ',(ecase name
537 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
538 (car tail))
539 ((member-if member-if-not)
540 tail))
541 ,(open-code (cdr tail))))))
542 (let* ((cp (constant-lvar-p list))
543 (c-list (when cp (lvar-value list))))
544 (cond ((and cp c-list (policy node (>= speed space))
545 (not (nthcdr *list-open-code-limit* c-list)))
546 `(let ((pred ,pred-expr)
547 ,@(when key `((key ,key-form))))
548 ,(open-code c-list)))
549 ((and cp (not c-list))
550 ;; constant nil list -- nothing to find!
551 nil)
553 ;; specialized out-of-line version
554 `(,(specialized-list-seek-function-name name (when key '(key)))
555 ,pred-expr list ,@(when key (list key-form))))))))))
557 (macrolet ((def (name &optional if/if-not)
558 (let ((basic (symbolicate "%" name))
559 (basic-eq (symbolicate "%" name "-EQ"))
560 (basic-key (symbolicate "%" name "-KEY"))
561 (basic-key-eq (symbolicate "%" name "-KEY-EQ")))
562 `(progn
563 (deftransform ,name ((item list &key key test test-not) * * :node node)
564 (transform-list-item-seek ',name item list key test test-not node))
565 (deftransform ,basic ((item list) (eq-comparable-type t))
566 `(,',basic-eq item list))
567 (deftransform ,basic-key ((item list) (eq-comparable-type t))
568 `(,',basic-key-eq item list))
569 ,@(when if/if-not
570 (let ((if-name (symbolicate name "-IF"))
571 (if-not-name (symbolicate name "-IF-NOT")))
572 `((deftransform ,if-name ((pred list &key key) * * :node node)
573 (transform-list-pred-seek ',if-name pred list key node))
574 (deftransform ,if-not-name ((pred list &key key) * * :node node)
575 (transform-list-pred-seek ',if-not-name pred list key node)))))))))
576 (def adjoin)
577 (def assoc t)
578 (def member t)
579 (def rassoc t))
581 (deftransform memq ((item list) (t (constant-arg list)))
582 (labels ((rec (tail)
583 (if tail
584 `(if (eq item ',(car tail))
585 ',tail
586 ,(rec (cdr tail)))
587 nil)))
588 (rec (lvar-value list))))
590 ;;; A similar transform used to apply to MEMBER and ASSOC, but since
591 ;;; TRANSFORM-LIST-ITEM-SEEK now takes care of them those transform
592 ;;; would never fire, and (%MEMBER-TEST ITEM LIST #'EQ) should be
593 ;;; almost as fast as MEMQ.
594 (deftransform delete ((item list &key test) (t list &rest t) *)
595 "convert to EQ test"
596 (let ((type (lvar-type item)))
597 (unless (or (and test (lvar-fun-is test '(eq)))
598 (and (eq-comparable-type-p type)
599 (or (not test) (lvar-fun-is test '(eql)))))
600 (give-up-ir1-transform)))
601 `(delq item list))
603 (deftransform delete-if ((pred list) (t list))
604 "open code"
605 '(do ((x list (cdr x))
606 (splice '()))
607 ((endp x) list)
608 (cond ((funcall pred (car x))
609 (if (null splice)
610 (setq list (cdr x))
611 (rplacd splice (cdr x))))
612 (t (setq splice x)))))
614 (deftransform fill ((seq item &key (start 0) (end nil))
615 (list t &key (:start t) (:end t)))
616 '(list-fill* seq item start end))
618 (defun find-basher (saetp &optional item node)
619 (let* ((element-type (sb!vm:saetp-specifier saetp))
620 (element-ctype (sb!vm:saetp-ctype saetp))
621 (n-bits (sb!vm:saetp-n-bits saetp))
622 (basher-name (format nil "UB~D-BASH-FILL" n-bits))
623 (basher (or (find-symbol basher-name
624 (load-time-value
625 (find-package "SB!KERNEL") t))
626 (abort-ir1-transform
627 "Unknown fill basher, please report to sbcl-devel: ~A"
628 basher-name)))
629 (kind (cond ((sb!vm:saetp-fixnum-p saetp) :tagged)
630 ((member element-type '(character base-char)) :char)
631 ((eq element-type 'single-float) :single-float)
632 #!+64-bit
633 ((eq element-type 'double-float) :double-float)
634 #!+64-bit
635 ((equal element-type '(complex single-float))
636 :complex-single-float)
638 (aver (integer-type-p element-ctype))
639 :bits)))
640 ;; BASH-VALUE is a word that we can repeatedly smash
641 ;; on the array: for less-than-word sized elements it
642 ;; contains multiple copies of the fill item.
643 (bash-value
644 (if (constant-lvar-p item)
645 (let ((tmp (lvar-value item)))
646 (unless (ctypep tmp element-ctype)
647 (abort-ir1-transform "~S is not ~S" tmp element-type))
648 (let* ((bits
649 (ldb (byte n-bits 0)
650 (ecase kind
651 (:tagged
652 (ash tmp sb!vm:n-fixnum-tag-bits))
653 (:char
654 (char-code tmp))
655 (:bits
656 tmp)
657 (:single-float
658 (single-float-bits tmp))
659 #!+64-bit
660 (:double-float
661 (logior (ash (double-float-high-bits tmp) 32)
662 (double-float-low-bits tmp)))
663 #!+64-bit
664 (:complex-single-float
665 (logior (ash (single-float-bits (imagpart tmp)) 32)
666 (ldb (byte 32 0)
667 (single-float-bits (realpart tmp))))))))
668 (res bits))
669 (loop for i of-type sb!vm:word from n-bits by n-bits
670 until (= i sb!vm:n-word-bits)
671 do (setf res (ldb (byte sb!vm:n-word-bits 0)
672 (logior res (ash bits i)))))
673 res))
674 (progn
675 (when node
676 (delay-ir1-transform node :constraint))
677 (if (and (eq kind :bits)
678 (= n-bits 1))
679 `(ldb (byte ,sb!vm:n-word-bits 0) (- item))
680 `(let ((res (ldb (byte ,n-bits 0)
681 ,(ecase kind
682 (:tagged
683 `(ash item ,sb!vm:n-fixnum-tag-bits))
684 (:char
685 `(char-code item))
686 (:bits
687 `item)
688 (:single-float
689 `(single-float-bits item))
690 #!+64-bit
691 (:double-float
692 `(logior (ash (double-float-high-bits item) 32)
693 (double-float-low-bits item)))
694 #!+64-bit
695 (:complex-single-float
696 `(logior (ash (single-float-bits (imagpart item)) 32)
697 (ldb (byte 32 0)
698 (single-float-bits (realpart item)))))))))
699 (declare (type sb!vm:word res))
700 ,@(loop for i of-type sb!vm:word = n-bits then (* 2 i)
701 until (= i sb!vm:n-word-bits)
702 collect
703 `(setf res (dpb res (byte ,i ,i) res)))
704 res))))))
705 (values basher bash-value)))
707 (deftransform fill ((seq item &key (start 0) (end nil))
708 (vector t &key (:start t) (:end t))
710 :node node)
711 (let* ((type (lvar-type seq))
712 (element-ctype (array-type-upgraded-element-type type))
713 (element-type (type-specifier element-ctype))
714 (saetp (unless (eq *wild-type* element-ctype)
715 (find-saetp-by-ctype element-ctype))))
716 (cond ((eq *wild-type* element-ctype)
717 (delay-ir1-transform node :constraint)
718 `(vector-fill* seq item start end))
719 ((and saetp (sb!vm:valid-bit-bash-saetp-p saetp))
720 (multiple-value-bind (basher bash-value) (find-basher saetp item node)
721 (values
722 ;; KLUDGE: WITH-ARRAY data in its full glory is going to mess up
723 ;; dynamic-extent for MAKE-ARRAY :INITIAL-ELEMENT initialization.
724 (if (csubtypep (lvar-type seq) (specifier-type '(simple-array * (*))))
725 `(let* ((len (length seq))
726 (end (or end len))
727 (bound (1+ end)))
728 ;; Minor abuse CHECK-BOUND for bounds checking.
729 ;; (- END START) may still end up negative, but
730 ;; the basher handle that.
731 (,basher ,bash-value seq
732 (check-bound seq bound start)
733 (- (if end (check-bound seq bound end) len)
734 start)))
735 `(with-array-data ((data seq)
736 (start start)
737 (end end)
738 :check-fill-pointer t)
739 (declare (type (simple-array ,element-type 1) data))
740 (declare (type index start end))
741 (declare (optimize (safety 0) (speed 3)))
742 (,basher ,bash-value data start (- end start))
743 seq))
744 `((declare (type ,element-type item))))))
745 ((policy node (> speed space))
746 (values
747 `(with-array-data ((data seq)
748 (start start)
749 (end end)
750 :check-fill-pointer t)
751 (declare (type (simple-array ,element-type 1) data))
752 (declare (type index start end))
753 ;; WITH-ARRAY-DATA did our range checks once and for all, so
754 ;; it'd be wasteful to check again on every AREF...
755 ;; Force bounds-checks to 0 even if local policy had it >0.
756 (declare (optimize (safety 0) (speed 3)
757 (insert-array-bounds-checks 0)))
758 ,(cond #!+x86-64
759 ((type= element-ctype *universal-type*)
760 '(%primitive sb!vm::fill-vector/t
761 data item start end))
763 `(do ((i start (1+ i)))
764 ((= i end))
765 (declare (type index i))
766 (setf (aref data i) item))))
767 seq)
768 ;; ... though we still need to check that the new element can fit
769 ;; into the vector in safe code. -- CSR, 2002-07-05
770 `((declare (type ,element-type item)))))
771 ((csubtypep type (specifier-type 'string))
772 '(string-fill* seq item start end))
774 '(vector-fill* seq item start end)))))
776 (deftransform fill ((seq item &key (start 0) (end nil))
777 ((and sequence (not vector) (not list)) t &key (:start t) (:end t)))
778 `(sb!sequence:fill seq item
779 :start start
780 :end (%check-generic-sequence-bounds seq start end)))
782 ;;;; hairy sequence transforms
784 ;;; FIXME: no hairy sequence transforms in SBCL?
786 ;;; There used to be a bunch of commented out code about here,
787 ;;; containing the (apparent) beginning of hairy sequence transform
788 ;;; infrastructure. People interested in implementing better sequence
789 ;;; transforms might want to look at it for inspiration, even though
790 ;;; the actual code is ancient CMUCL -- and hence bitrotted. The code
791 ;;; was deleted in 1.0.7.23.
793 ;;;; string operations
795 ;;; We transform the case-sensitive string predicates into a non-keyword
796 ;;; version. This is an IR1 transform so that we don't have to worry about
797 ;;; changing the order of evaluation.
798 (macrolet ((def (fun pred*)
799 `(deftransform ,fun ((string1 string2 &key (start1 0) end1
800 (start2 0) end2)
801 * *)
802 `(,',pred* string1 string2 start1 end1 start2 end2))))
803 (def string< string<*)
804 (def string> string>*)
805 (def string<= string<=*)
806 (def string>= string>=*)
807 (def string= string=*)
808 (def string/= string/=*))
810 ;;; Return a form that tests the free variables STRING1 and STRING2
811 ;;; for the ordering relationship specified by LESSP and EQUALP. The
812 ;;; start and end are also gotten from the environment. Both strings
813 ;;; must be SIMPLE-BASE-STRINGs.
814 (macrolet ((def (name lessp equalp)
815 `(deftransform ,name ((string1 string2 start1 end1 start2 end2)
816 (simple-base-string simple-base-string t t t t) *)
817 `(let* ((end1 (if (not end1) (length string1) end1))
818 (end2 (if (not end2) (length string2) end2))
819 (index (sb!impl::%sp-string-compare
820 string1 start1 end1 string2 start2 end2)))
821 (if index
822 (cond ((= index end1)
823 ,(if ',lessp 'index nil))
824 ((= (+ index (- start2 start1)) end2)
825 ,(if ',lessp nil 'index))
826 ((,(if ',lessp 'char< 'char>)
827 (schar string1 index)
828 (schar string2
829 (truly-the index
830 (+ index
831 (truly-the fixnum
832 (- start2
833 start1))))))
834 index)
835 (t nil))
836 ,(if ',equalp 'end1 nil))))))
837 (def string<* t nil)
838 (def string<=* t t)
839 (def string>* nil nil)
840 (def string>=* nil t))
842 (deftransform string=* ((string1 string2 start1 end1 start2 end2)
843 (string string
844 (constant-arg (eql 0))
845 (constant-arg null)
846 (constant-arg (eql 0))
847 (constant-arg null)))
848 (cond ((and (constant-lvar-p string1)
849 (equal (lvar-value string1) ""))
850 `(zerop (length string2)))
851 ((and (constant-lvar-p string2)
852 (equal (lvar-value string2) ""))
853 `(zerop (length string1)))
855 (give-up-ir1-transform))))
857 (deftransform string/=* ((string1 string2 start1 end1 start2 end2)
858 (string string
859 (constant-arg (eql 0))
860 (constant-arg null)
861 (constant-arg (eql 0))
862 (constant-arg null)))
863 (cond ((and (constant-lvar-p string1)
864 (equal (lvar-value string1) ""))
865 `(and (plusp (length string2))
867 ((and (constant-lvar-p string2)
868 (equal (lvar-value string2) ""))
869 `(and (plusp (length string1))
872 (give-up-ir1-transform))))
874 (macrolet ((def (name result-fun)
875 `(deftransform ,name ((string1 string2 start1 end1 start2 end2)
876 (simple-base-string simple-base-string t t t t) *)
877 `(,',result-fun
878 (sb!impl::%sp-string-compare
879 string1 start1 (or end1 (length string1))
880 string2 start2 (or end2 (length string2)))))))
881 (def string=* not) ; FIXME: this xform looks counterproductive.
882 (def string/=* identity))
884 (deftransform string/=* ((str1 str2 start1 end1 start2 end2) * * :node node
885 :important nil)
886 ;; An IF node doesn't care about the mismatch index.
887 ;; Transforming to (not (string= ..)) would lead to internal confusion
888 ;; due to incorrect typing: STRING/= can't return T, so return 0 for true.
889 (if (if-p (node-dest node))
890 `(if (string=* str1 str2 start1 end1 start2 end2) nil 0)
891 (give-up-ir1-transform)))
893 (deftransform string ((x) (symbol)) '(symbol-name x))
894 (deftransform string ((x) (string)) '(progn x))
896 ;;;; transforms for sequence functions
898 ;;; FIXME: In the copy loops below, we code the loops in a strange
899 ;;; fashion:
901 ;;; (do ((i (+ src-offset length) (1- i)))
902 ;;; ((<= i 0) ...)
903 ;;; (... (aref foo (1- i)) ...))
905 ;;; rather than the more natural (and seemingly more efficient):
907 ;;; (do ((i (1- (+ src-offset length)) (1- i)))
908 ;;; ((< i 0) ...)
909 ;;; (... (aref foo i) ...))
911 ;;; (more efficient because we don't have to do the index adjusting on
912 ;;; every iteration of the loop)
914 ;;; We do this to avoid a suboptimality in SBCL's backend. In the
915 ;;; latter case, the backend thinks I is a FIXNUM (which it is), but
916 ;;; when used as an array index, the backend thinks I is a
917 ;;; POSITIVE-FIXNUM (which it is). However, since the backend thinks of
918 ;;; these as distinct storage classes, it cannot coerce a move from a
919 ;;; FIXNUM TN to a POSITIVE-FIXNUM TN. The practical effect of this
920 ;;; deficiency is that we have two extra moves and increased register
921 ;;; pressure, which can lead to some spectacularly bad register
922 ;;; allocation. (sub-FIXME: the register allocation even with the
923 ;;; strangely written loops is not always excellent, either...). Doing
924 ;;; it the first way, above, means that I is always thought of as a
925 ;;; POSITIVE-FIXNUM and there are no issues.
927 ;;; Besides, the *-WITH-OFFSET machinery will fold those index
928 ;;; adjustments in the first version into the array addressing at no
929 ;;; performance penalty!
931 ;;; This transform is critical to the performance of string streams. If
932 ;;; you tweak it, make sure that you compare the disassembly, if not the
933 ;;; performance of, the functions implementing string streams
934 ;;; (e.g. SB!IMPL::STRING-OUCH).
935 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
936 (defun !make-replace-transform (saetp sequence-type1 sequence-type2)
937 `(deftransform replace ((seq1 seq2 &key (start1 0) (start2 0) end1 end2)
938 (,sequence-type1 ,sequence-type2 &rest t)
939 ,sequence-type1
940 :node node)
941 `(let* ((len1 (length seq1))
942 (len2 (length seq2))
943 (end1 (or end1 len1))
944 (end2 (or end2 len2))
945 (replace-len (min (- end1 start1) (- end2 start2))))
946 ,(unless (policy node (= insert-array-bounds-checks 0))
947 `(progn
948 (unless (<= 0 start1 end1 len1)
949 (sequence-bounding-indices-bad-error seq1 start1 end1))
950 (unless (<= 0 start2 end2 len2)
951 (sequence-bounding-indices-bad-error seq2 start2 end2))))
952 ,',(cond
953 ((and saetp (sb!vm:valid-bit-bash-saetp-p saetp))
954 (let* ((n-element-bits (sb!vm:saetp-n-bits saetp))
955 (bash-function (intern (format nil "UB~D-BASH-COPY"
956 n-element-bits)
957 (find-package "SB!KERNEL"))))
958 `(funcall (function ,bash-function) seq2 start2
959 seq1 start1 replace-len)))
961 `(if (and
962 ;; If the sequence types are different, SEQ1 and
963 ;; SEQ2 must be distinct arrays.
964 ,(eql sequence-type1 sequence-type2)
965 (eq seq1 seq2) (> start1 start2))
966 (do ((i (truly-the (or (eql -1) index) (+ start1 replace-len -1))
967 (1- i))
968 (j (truly-the (or (eql -1) index) (+ start2 replace-len -1))
969 (1- j)))
970 ((< i start1))
971 (declare (optimize (insert-array-bounds-checks 0)))
972 (setf (aref seq1 i) (aref seq2 j)))
973 (do ((i start1 (1+ i))
974 (j start2 (1+ j))
975 (end (+ start1 replace-len)))
976 ((>= i end))
977 (declare (optimize (insert-array-bounds-checks 0)))
978 (setf (aref seq1 i) (aref seq2 j))))))
979 seq1))))
981 (macrolet
982 ((define-replace-transforms ()
983 (loop for saetp across sb!vm:*specialized-array-element-type-properties*
984 for sequence-type = `(simple-array ,(sb!vm:saetp-specifier saetp) (*))
985 unless (= (sb!vm:saetp-typecode saetp) sb!vm::simple-array-nil-widetag)
986 collect (!make-replace-transform saetp sequence-type sequence-type)
987 into forms
988 finally (return `(progn ,@forms))))
989 (define-one-transform (sequence-type1 sequence-type2)
990 (!make-replace-transform nil sequence-type1 sequence-type2)))
991 (define-replace-transforms)
992 #!+sb-unicode
993 (progn
994 (define-one-transform (simple-array base-char (*)) (simple-array character (*)))
995 (define-one-transform (simple-array character (*)) (simple-array base-char (*)))))
997 ;;; Expand simple cases of UB<SIZE>-BASH-COPY inline. "simple" is
998 ;;; defined as those cases where we are doing word-aligned copies from
999 ;;; both the source and the destination and we are copying from the same
1000 ;;; offset from both the source and the destination. (The last
1001 ;;; condition is there so we can determine the direction to copy at
1002 ;;; compile time rather than runtime. Remember that UB<SIZE>-BASH-COPY
1003 ;;; acts like memmove, not memcpy.) These conditions may seem rather
1004 ;;; restrictive, but they do catch common cases, like allocating a (* 2
1005 ;;; N)-size buffer and blitting in the old N-size buffer in.
1007 (defun frob-bash-transform (src src-offset
1008 dst dst-offset
1009 length n-elems-per-word)
1010 (declare (ignore src dst length))
1011 (let ((n-bits-per-elem (truncate sb!vm:n-word-bits n-elems-per-word)))
1012 (multiple-value-bind (src-word src-elt)
1013 (truncate (lvar-value src-offset) n-elems-per-word)
1014 (multiple-value-bind (dst-word dst-elt)
1015 (truncate (lvar-value dst-offset) n-elems-per-word)
1016 ;; Avoid non-word aligned copies.
1017 (unless (and (zerop src-elt) (zerop dst-elt))
1018 (give-up-ir1-transform))
1019 ;; Avoid copies where we would have to insert code for
1020 ;; determining the direction of copying.
1021 (unless (= src-word dst-word)
1022 (give-up-ir1-transform))
1023 ;; FIXME: The cross-compiler doesn't optimize TRUNCATE properly,
1024 ;; so we have to do its work here.
1025 `(let ((end (+ ,src-word ,(if (= n-elems-per-word 1)
1026 'length
1027 `(truncate (the index length) ,n-elems-per-word)))))
1028 (declare (type index end))
1029 ;; Handle any bits at the end.
1030 (when (logtest length (1- ,n-elems-per-word))
1031 (let* ((extra (mod length ,n-elems-per-word))
1032 ;; FIXME: The shift amount on this ASH is
1033 ;; *always* negative, but the backend doesn't
1034 ;; have a NEGATIVE-FIXNUM primitive type, so we
1035 ;; wind up with a pile of code that tests the
1036 ;; sign of the shift count prior to shifting when
1037 ;; all we need is a simple negate and shift
1038 ;; right. Yuck.
1039 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
1040 (* (- extra ,n-elems-per-word)
1041 ,n-bits-per-elem))))
1042 (setf (%vector-raw-bits dst end)
1043 (logior
1044 (logandc2 (%vector-raw-bits dst end)
1045 (ash mask
1046 ,(ecase *backend-byte-order*
1047 (:little-endian 0)
1048 (:big-endian `(* (- ,n-elems-per-word extra)
1049 ,n-bits-per-elem)))))
1050 (logand (%vector-raw-bits src end)
1051 (ash mask
1052 ,(ecase *backend-byte-order*
1053 (:little-endian 0)
1054 (:big-endian `(* (- ,n-elems-per-word extra)
1055 ,n-bits-per-elem)))))))))
1056 ;; Copy from the end to save a register.
1057 (do ((i end (1- i)))
1058 ((<= i ,src-word))
1059 (setf (%vector-raw-bits dst (1- i))
1060 (%vector-raw-bits src (1- i))))
1061 (values))))))
1064 (let ((arglist '((src src-offset dst dst-offset length)
1065 ((simple-unboxed-array (*)) (constant-arg index)
1066 (simple-unboxed-array (*)) (constant-arg index)
1067 index)
1068 *)))
1069 (loop for i = 1 then (* i 2)
1070 for name = (intern (format nil "UB~D-BASH-COPY" i) "SB!KERNEL")
1071 collect `(deftransform ,name ,arglist
1072 (frob-bash-transform src src-offset
1073 dst dst-offset length
1074 ,(truncate sb!vm:n-word-bits i))) into forms
1075 until (= i sb!vm:n-word-bits)
1076 finally (return `(progn ,@forms))))
1078 ;;; We expand copy loops inline in SUBSEQ and COPY-SEQ if we're copying
1079 ;;; arrays with elements of size >= the word size. We do this because
1080 ;;; we know the arrays cannot alias (one was just consed), therefore we
1081 ;;; can determine at compile time the direction to copy, and for
1082 ;;; word-sized elements, UB<WORD-SIZE>-BASH-COPY will do a bit of
1083 ;;; needless checking to figure out what's going on. The same
1084 ;;; considerations apply if we are copying elements larger than the word
1085 ;;; size, with the additional twist that doing it inline is likely to
1086 ;;; cons far less than calling REPLACE and letting generic code do the
1087 ;;; work.
1089 ;;; However, we do not do this for elements whose size is < than the
1090 ;;; word size because we don't want to deal with any alignment issues
1091 ;;; inline. The UB*-BASH-COPY transforms might fix things up later
1092 ;;; anyway.
1094 (defun inlineable-copy-vector-p (type)
1095 (and (array-type-p type)
1096 ;; The two transforms that use this test already specify that their
1097 ;; sequence argument is a VECTOR,
1098 ;; so this seems like it would be more efficient as
1099 ;; and (not (array-type-complexp type))
1100 ;; (not (eq (array-type-element-type type) *wild-type*))
1101 ;; Anyway it no longer works to write this as a single specifier
1102 ;; '(or (simple-unboxed-array (*)) simple-vector) because that
1103 ;; type is just (simple-array * (*)) which isn't amenable to
1104 ;; inline copying since we don't know what it holds.
1105 (or (csubtypep type (specifier-type '(simple-unboxed-array (*))))
1106 (csubtypep type (specifier-type 'simple-vector)))))
1108 (defun maybe-expand-copy-loop-inline (src src-offset dst dst-offset length
1109 element-type)
1110 (let ((saetp (find-saetp element-type)))
1111 (aver saetp)
1112 (if (>= (sb!vm:saetp-n-bits saetp) sb!vm:n-word-bits)
1113 (expand-aref-copy-loop src src-offset dst dst-offset length)
1114 `(locally (declare (optimize (safety 0)))
1115 (replace ,dst ,src :start1 ,dst-offset :start2 ,src-offset :end1 ,length)))))
1117 (defun expand-aref-copy-loop (src src-offset dst dst-offset length)
1118 (if (eql src-offset dst-offset)
1119 `(do ((i (+ ,src-offset ,length) (1- i)))
1120 ((<= i ,src-offset))
1121 (declare (optimize (insert-array-bounds-checks 0)))
1122 (setf (aref ,dst (1- i)) (aref ,src (1- i))))
1123 ;; KLUDGE: The compiler is not able to derive that (+ offset
1124 ;; length) must be a fixnum, but arrives at (unsigned-byte 29).
1125 ;; We, however, know it must be so, as by this point the bounds
1126 ;; have already been checked.
1127 `(do ((i (truly-the fixnum (+ ,src-offset ,length)) (1- i))
1128 (j (+ ,dst-offset ,length) (1- j)))
1129 ((<= i ,src-offset))
1130 (declare (optimize (insert-array-bounds-checks 0))
1131 (type (integer 0 #.sb!xc:array-dimension-limit) j i))
1132 (setf (aref ,dst (1- j)) (aref ,src (1- i))))))
1134 ;;; MAKE-SEQUENCE, SUBSEQ, COPY-SEQ
1136 (deftransform make-sequence ((result-type size &key initial-element) * *)
1137 (multiple-value-bind (spec type)
1138 (and (constant-lvar-p result-type)
1139 (let ((spec (lvar-value result-type)))
1140 (values spec (ir1-transform-specifier-type spec))))
1141 (unless type
1142 (give-up-ir1-transform))
1143 (if (type= type (specifier-type 'list))
1144 `(%make-list size initial-element)
1145 (multiple-value-bind (elt-type dim complexp)
1146 (cond ((and (union-type-p type)
1147 (csubtypep type (specifier-type 'string)))
1148 (let* ((types (union-type-types type))
1149 (first (first types)))
1150 (when (array-type-p first)
1151 (let ((dim (first (array-type-dimensions first)))
1152 (complexp (array-type-complexp first)))
1153 ;; Require sameness of dim and complexp. Give up on
1154 ;; (OR (VECTOR CHARACTER) (VECTOR BASE-CHAR 2))
1155 ;; which eventually fails in the call to the function.
1156 (when (every (lambda (x)
1157 (and (array-type-p x)
1158 (eql (first (array-type-dimensions x))
1159 dim)
1160 (eq (array-type-complexp x) complexp)))
1161 (rest types))
1162 (values
1163 `(or ,@(mapcar
1164 (lambda (x)
1165 (type-specifier (array-type-element-type x)))
1166 types))
1167 dim complexp))))))
1168 ((and (array-type-p type)
1169 (csubtypep type (specifier-type 'vector)))
1170 (when (contains-unknown-type-p (array-type-element-type type))
1171 (give-up-ir1-transform "~S is an unknown vector type" spec))
1172 (values (let ((et (array-type-element-type type)))
1173 ;; VECTOR means (VECTOR T)
1174 (if (type= et *wild-type*)
1176 (type-specifier et)))
1177 (first (array-type-dimensions type))
1178 (array-type-complexp type))))
1179 ;; Don't transform if size is present in the specifier
1180 ;; and the SIZE argument is not known to be equal.
1181 (cond ((and (or (eq '* dim)
1182 (and dim (constant-lvar-p size) (eql (lvar-value size) dim)))
1183 ;; not sure what it would mean to make it non-simple
1184 (neq complexp t))
1185 `(make-array size :element-type ',elt-type
1186 ,@(when initial-element
1187 `(:initial-element initial-element))))
1188 ;; no transform, but we can detect some style issues
1190 (when dim ; was a recognizable vector subtype
1191 (let* ((elt-ctype (specifier-type elt-type))
1192 (saetp (find-saetp-by-ctype elt-ctype)))
1193 (cond ((not initial-element)
1194 (let ((default-initial-element
1195 (sb!vm:saetp-initial-element-default saetp)))
1196 (unless (ctypep default-initial-element elt-ctype)
1197 ;; As with MAKE-ARRAY, this is merely undefined
1198 ;; behavior, not an error.
1199 (compiler-style-warn
1200 "The default initial element ~S is not a ~S."
1201 default-initial-element elt-type))))
1202 ;; In would be possible in some cases,
1203 ;; like :INITIAL-ELEMENT (IF X #\x #\y) in a call
1204 ;; to MAKE-SEQUENCE '(VECTOR (MEMBER #\A #\B))
1205 ;; to detect erroneous non-constants initializers,
1206 ;; but it is not important enough to bother with.
1207 ((and (constant-lvar-p initial-element)
1208 (not (ctypep (lvar-value initial-element)
1209 elt-ctype)))
1210 ;; MAKE-ARRAY considers this a warning, not an error.
1211 (compiler-warn "~S ~S is not a ~S"
1212 :initial-element
1213 (lvar-value initial-element)
1214 elt-type)))))
1215 (give-up-ir1-transform)))))))
1217 (deftransform subseq ((seq start &optional end)
1218 (vector t &optional t)
1220 :node node)
1221 (let ((type (lvar-type seq)))
1222 (cond
1223 ((and (inlineable-copy-vector-p type)
1224 (policy node (> speed space)))
1225 (let ((element-type (type-specifier (array-type-specialized-element-type type))))
1226 `(let* ((length (length seq))
1227 (end (or end length)))
1228 ,(unless (policy node (zerop insert-array-bounds-checks))
1229 '(progn
1230 (unless (<= 0 start end length)
1231 (sequence-bounding-indices-bad-error seq start end))))
1232 (let* ((size (- end start))
1233 (result (make-array size :element-type ',element-type)))
1234 ,(maybe-expand-copy-loop-inline 'seq (if (constant-lvar-p start)
1235 (lvar-value start)
1236 'start)
1237 'result 0 'size element-type)
1238 result))))
1240 '(vector-subseq* seq start end)))))
1242 (deftransform subseq ((seq start &optional end)
1243 (list t &optional t))
1244 `(list-subseq* seq start end))
1246 (deftransform subseq ((seq start &optional end)
1247 ((and sequence (not vector) (not list)) t &optional t))
1248 '(sb!sequence:subseq seq start end))
1250 (deftransform copy-seq ((seq) (vector))
1251 (let ((type (lvar-type seq)))
1252 (cond ((inlineable-copy-vector-p type)
1253 (let ((element-type (type-specifier (array-type-specialized-element-type type))))
1254 `(let* ((length (length seq))
1255 (result (make-array length :element-type ',element-type)))
1256 ,(maybe-expand-copy-loop-inline 'seq 0 'result 0 'length element-type)
1257 result)))
1259 '(vector-subseq* seq 0 nil)))))
1261 (deftransform copy-seq ((seq) (list))
1262 '(list-copy-seq* seq))
1264 (deftransform copy-seq ((seq) ((and sequence (not vector) (not list))))
1265 '(sb!sequence:copy-seq seq))
1267 (deftransform search ((pattern text &key start1 start2 end1 end2 test test-not
1268 key from-end)
1269 ((constant-arg sequence) * &rest *))
1270 (if key
1271 (give-up-ir1-transform)
1272 (let* ((pattern (lvar-value pattern))
1273 (pattern-start (cond ((constant-lvar-p start1)
1274 (lvar-value start1))
1275 ((not start1)
1278 (give-up-ir1-transform))))
1279 (pattern-end (cond ((constant-lvar-p end1)
1280 (lvar-value end1))
1281 ((not end1)
1282 (length pattern))
1284 (give-up-ir1-transform))))
1285 (pattern (if (= (- pattern-end pattern-start) 1)
1286 (elt pattern pattern-start)
1287 (give-up-ir1-transform))))
1288 (macrolet ((maybe-arg (arg &optional (key (keywordicate arg)))
1289 `(and ,arg `(,,key ,',arg))))
1290 `(position ',pattern text
1291 ,@(maybe-arg start2 :start)
1292 ,@(maybe-arg end2 :end)
1293 ,@(maybe-arg test)
1294 ,@(maybe-arg test-not)
1295 ,@(maybe-arg from-end))))))
1297 ;;; FIXME: it really should be possible to take advantage of the
1298 ;;; macros used in code/seq.lisp here to avoid duplication of code,
1299 ;;; and enable even funkier transformations.
1300 (deftransform search ((pattern text &key (start1 0) (start2 0) end1 end2
1301 (test #'eql)
1302 (key #'identity)
1303 from-end)
1304 (vector vector &rest t)
1306 :node node
1307 :policy (> speed (max space safety)))
1308 "open code"
1309 (flet ((maybe (x)
1310 (when (lvar-p x)
1311 (if (constant-lvar-p x)
1312 (when (lvar-value x)
1313 :yes)
1314 :maybe))))
1315 (let ((from-end (when (lvar-p from-end)
1316 (unless (constant-lvar-p from-end)
1317 (give-up-ir1-transform ":FROM-END is not constant."))
1318 (lvar-value from-end)))
1319 (key? (maybe key))
1320 (test? (maybe test))
1321 (check-bounds-p (policy node (plusp insert-array-bounds-checks))))
1322 `(block search
1323 (flet ((oops (vector start end)
1324 (sequence-bounding-indices-bad-error vector start end)))
1325 (declare (ignorable #'oops))
1326 (let* ((len1 (length pattern))
1327 (len2 (length text))
1328 (end1 (or end1 len1))
1329 (end2 (or end2 len2))
1330 ,@(case key?
1331 (:yes `((key (%coerce-callable-to-fun key))))
1332 (:maybe `((key (when key
1333 (%coerce-callable-to-fun key))))))
1334 ,@(when test?
1335 `((test (%coerce-callable-to-fun test)))))
1336 (declare (type index start1 start2 end1 end2))
1337 ,@(when check-bounds-p
1338 `((unless (<= start1 end1 len1)
1339 (oops pattern start1 end1))
1340 (unless (<= start2 end2 len2)
1341 (oops pattern start2 end2))))
1342 (when (= end1 start1)
1343 (return-from search (if from-end
1344 end2
1345 start2)))
1346 (do (,(if from-end
1347 '(index2 (- end2 (- end1 start1)) (1- index2))
1348 '(index2 start2 (1+ index2))))
1349 (,(if from-end
1350 '(< index2 start2)
1351 '(>= index2 end2))
1352 nil)
1353 ;; INDEX2 is FIXNUM, not an INDEX, as right before the loop
1354 ;; terminates is hits -1 when :FROM-END is true and :START2
1355 ;; is 0.
1356 (declare (type fixnum index2))
1357 (when (do ((index1 start1 (1+ index1))
1358 (index2 index2 (1+ index2)))
1359 ((>= index1 end1) t)
1360 (declare (type index index1 index2)
1361 (optimize (insert-array-bounds-checks 0)))
1362 ,@(unless from-end
1363 '((when (= index2 end2)
1364 (return-from search nil))))
1365 (unless (,@(if test?
1366 `(funcall test)
1367 `(eql))
1368 ,(case key?
1369 (:yes `(funcall key (aref pattern index1)))
1370 (:maybe `(let ((elt (aref pattern index1)))
1371 (if key
1372 (funcall key elt)
1373 elt)))
1374 (otherwise `(aref pattern index1)))
1375 ,(case key?
1376 (:yes `(funcall key (aref text index2)))
1377 (:maybe `(let ((elt (aref text index2)))
1378 (if key
1379 (funcall key elt)
1380 elt)))
1381 (otherwise `(aref text index2))))
1382 (return nil)))
1383 (return index2)))))))))
1385 (defoptimizer (search derive-type) ((sequence1 sequence2
1386 &key start1 end1 start2 end2
1387 &allow-other-keys))
1388 (let* ((constant-start1 (and (constant-lvar-p start1)
1389 (lvar-value start1)))
1390 (constant-end1 (and (constant-lvar-p end1)
1391 (lvar-value end1)))
1392 (constant-start2 (and (constant-lvar-p start2)
1393 (lvar-value start2)))
1394 (constant-end2 (and (constant-lvar-p end2)
1395 (lvar-value end2)))
1396 (min-result (or constant-start2 0))
1397 (max-result (or constant-end2 (1- sb!xc:array-dimension-limit)))
1398 (max2 (sequence-lvar-dimensions sequence2))
1399 (max-result (if (integerp max2)
1400 (min max-result max2)
1401 max-result))
1402 (min1 (nth-value 1 (sequence-lvar-dimensions sequence1)))
1403 (min-sequence1-length (cond ((and constant-start1 constant-end1)
1404 (- constant-end1 constant-start1))
1405 ((and constant-end1 (not start1))
1406 constant-end1)
1407 ((and constant-start1
1408 (not end1)
1409 (integerp min1))
1410 (- min1 constant-start1))
1411 ((or start1 end1 (not (integerp min1)))
1412 ;; The result can be equal to MAX-RESULT only when
1413 ;; searching for "" and :start2 being equal to :end2
1414 (if (or (and start2
1415 (not constant-start2))
1416 (= max-result min-result))
1420 min1))))
1421 (specifier-type `(or (integer ,min-result
1422 ,(- max-result min-sequence1-length))
1423 null))))
1425 (defun index-into-sequence-derive-type (sequence start end &key (inclusive t))
1426 (let* ((constant-start (and (constant-lvar-p start)
1427 (lvar-value start)))
1428 (constant-end (and (constant-lvar-p end)
1429 (lvar-value end)))
1430 (min-result (or constant-start 0))
1431 (max-result (or constant-end (1- sb!xc:array-dimension-limit)))
1432 (max (sequence-lvar-dimensions sequence))
1433 (max-result (if (integerp max)
1434 (min max-result max)
1435 max-result)))
1436 (values min-result (if inclusive
1437 max-result
1438 (1- max-result)))))
1440 (defoptimizer (mismatch derive-type) ((sequence1 sequence2
1441 &key start1 end1
1442 &allow-other-keys))
1443 (declare (ignorable sequence2))
1444 ;; Could be as smart as the SEARCH one above but I ran out of steam.
1445 (multiple-value-bind (min max) (index-into-sequence-derive-type sequence1 start1 end1)
1446 (specifier-type `(or (integer ,min ,max) null))))
1448 (defoptimizer (position derive-type) ((item sequence
1449 &key start end
1450 &allow-other-keys))
1451 (declare (ignore item))
1452 (multiple-value-bind (min max)
1453 (index-into-sequence-derive-type sequence start end :inclusive nil)
1454 (specifier-type `(or (integer ,min ,max) null))))
1456 (defoptimizer (position-if derive-type) ((function sequence
1457 &key start end
1458 &allow-other-keys))
1459 (declare (ignore function))
1460 (multiple-value-bind (min max)
1461 (index-into-sequence-derive-type sequence start end :inclusive nil)
1462 (specifier-type `(or (integer ,min ,max) null))))
1464 (defoptimizer (position-if-not derive-type) ((function sequence
1465 &key start end
1466 &allow-other-keys))
1467 (declare (ignore function))
1468 (multiple-value-bind (min max)
1469 (index-into-sequence-derive-type sequence start end :inclusive nil)
1470 (specifier-type `(or (integer ,min ,max) null))))
1472 (defoptimizer (count derive-type) ((item sequence
1473 &key start end
1474 &allow-other-keys))
1475 (declare (ignore item))
1476 (multiple-value-bind (min max)
1477 (index-into-sequence-derive-type sequence start end)
1478 (specifier-type `(integer 0 ,(- max min)))))
1480 (defoptimizer (count-if derive-type) ((function sequence
1481 &key start end
1482 &allow-other-keys))
1483 (declare (ignore function))
1484 (multiple-value-bind (min max)
1485 (index-into-sequence-derive-type sequence start end)
1486 (specifier-type `(integer 0 ,(- max min)))))
1488 (defoptimizer (count-if-not derive-type) ((function sequence
1489 &key start end
1490 &allow-other-keys))
1491 (declare (ignore function))
1492 (multiple-value-bind (min max)
1493 (index-into-sequence-derive-type sequence start end)
1494 (specifier-type `(integer 0 ,(- max min)))))
1496 (defoptimizer (subseq derive-type) ((sequence start &optional end) node)
1497 (let* ((sequence-type (lvar-type sequence))
1498 (constant-start (and (constant-lvar-p start)
1499 (lvar-value start)))
1500 (constant-end (and (constant-lvar-p end)
1501 (lvar-value end)))
1502 (index-length (and constant-start constant-end
1503 (- constant-end constant-start)))
1504 (list-type (specifier-type 'list)))
1505 (flet ((bad ()
1506 (let ((*compiler-error-context* node))
1507 (compiler-warn "Bad bounding indices ~s, ~s for ~
1508 ~/sb!impl:print-type/"
1509 constant-start constant-end sequence-type))))
1510 (cond ((and index-length
1511 (minusp index-length))
1512 ;; Would be a good idea to transform to something like
1513 ;; %compile-time-type-error
1514 (bad))
1515 ((csubtypep sequence-type list-type)
1516 (let ((null-type (specifier-type 'null)))
1517 (cond ((csubtypep sequence-type null-type)
1518 (cond ((or (and constant-start
1519 (plusp constant-start))
1520 (and index-length
1521 (plusp index-length)))
1522 (bad))
1523 ((eql constant-start 0)
1524 null-type)
1526 list-type)))
1527 ((not index-length)
1528 list-type)
1529 ((zerop index-length)
1530 null-type)
1532 (specifier-type 'cons)))))
1533 ((csubtypep sequence-type (specifier-type 'vector))
1534 (let* ((dimensions
1535 ;; Can't trust lengths from non-simple vectors due to
1536 ;; fill-pointer and adjust-array
1537 (and (csubtypep sequence-type (specifier-type 'simple-array))
1538 (ctype-array-dimensions sequence-type)))
1539 (dimensions-length
1540 (and (singleton-p dimensions)
1541 (integerp (car dimensions))
1542 (car dimensions)))
1543 (length (cond (index-length)
1544 ((and dimensions-length
1545 (not end)
1546 constant-start)
1547 (- dimensions-length constant-start))))
1548 (simplified (simplify-vector-type sequence-type)))
1549 (cond ((and dimensions-length
1551 (and constant-start
1552 (> constant-start dimensions-length))
1553 (and constant-end
1554 (> constant-end dimensions-length))))
1555 (bad))
1556 (length
1557 (type-intersection simplified
1558 (specifier-type `(simple-array * (,length)))))
1560 simplified))))
1561 ((not index-length)
1562 nil)
1563 ((zerop index-length)
1564 (specifier-type '(not cons)))
1566 (specifier-type '(not null)))))))
1568 ;;; Open-code CONCATENATE for strings. It would be possible to extend
1569 ;;; this transform to non-strings, but I chose to just do the case that
1570 ;;; should cover 95% of CONCATENATE performance complaints for now.
1571 ;;; -- JES, 2007-11-17
1573 ;;; Only handle the simple result type cases. If somebody does (CONCATENATE
1574 ;;; '(STRING 6) ...) their code won't be optimized, but nobody does that in
1575 ;;; practice.
1577 ;;; Limit full open coding based on length of constant sequences. Default
1578 ;;; value is chosen so that other parts of the compiler (constraint propagation
1579 ;;; mainly) won't go nonlinear too badly. It's not an exact number -- but
1580 ;;; in the right ballpark.
1581 (defvar *concatenate-open-code-limit* 129)
1583 (defun string-concatenate-transform (node type lvars)
1584 (let ((vars (make-gensym-list (length lvars))))
1585 (if (policy node (<= speed space))
1586 ;; Out-of-line
1587 (let ((constants-to-string
1588 ;; Strings are handled more efficiently by
1589 ;; %concatenate-to-* functions
1590 (loop for var in vars
1591 for lvar in lvars
1592 collect (if (and (constant-lvar-p lvar)
1593 (every #'characterp (lvar-value lvar)))
1594 (coerce (lvar-value lvar) 'string)
1595 var))))
1596 `(lambda (.dummy. ,@vars)
1597 (declare (ignore .dummy.)
1598 (ignorable ,@vars))
1599 ,(ecase type
1600 ((string simple-string)
1601 `(%concatenate-to-string ,@constants-to-string))
1602 ((base-string simple-base-string)
1603 `(%concatenate-to-base-string ,@constants-to-string)))))
1604 ;; Inline
1605 (let* ((element-type (ecase type
1606 ((string simple-string) 'character)
1607 ((base-string simple-base-string) 'base-char)))
1608 (lvar-values (loop for lvar in lvars
1609 collect (when (constant-lvar-p lvar)
1610 (lvar-value lvar))))
1611 (lengths
1612 (loop for value in lvar-values
1613 for var in vars
1614 collect (if value
1615 (length value)
1616 `(sb!impl::string-dispatch ((simple-array * (*))
1617 sequence)
1618 ,var
1619 #-sb-xc-host
1620 (declare (muffle-conditions compiler-note))
1621 (length ,var)))))
1622 (non-constant-start
1623 (loop for value in lvar-values
1624 while (and (stringp value)
1625 (< (length value) *concatenate-open-code-limit*))
1626 sum (length value))))
1627 `(lambda (.dummy. ,@vars)
1628 (declare (ignore .dummy.)
1629 (ignorable ,@vars))
1630 (declare (optimize (insert-array-bounds-checks 0)))
1631 (let* ((.length. (+ ,@lengths))
1632 (.pos. ,non-constant-start)
1633 (.string. (make-string .length. :element-type ',element-type)))
1634 (declare (type index .length. .pos.)
1635 #-sb-xc-host (muffle-conditions compiler-note)
1636 (ignorable .pos.))
1637 ,@(loop with constants = -1
1638 for value in lvar-values
1639 for var in vars
1640 collect
1641 (cond ((and (stringp value)
1642 (< (length value) *concatenate-open-code-limit*))
1643 ;; Fold the array reads for constant arguments
1644 `(progn
1645 ,@(loop for c across value
1646 for i from 0
1647 collect
1648 ;; Without truly-the we get massive numbers
1649 ;; of pointless error traps.
1650 `(setf (aref .string.
1651 (truly-the index ,(if constants
1652 (incf constants)
1653 `(+ .pos. ,i))))
1654 ,c))
1655 ,(unless constants
1656 `(incf (truly-the index .pos.) ,(length value)))))
1658 (prog1
1659 `(sb!impl::string-dispatch
1660 (#!+sb-unicode
1661 (simple-array character (*))
1662 (simple-array base-char (*))
1664 ,var
1665 (replace .string. ,var
1666 ,@(cond ((not constants)
1667 '(:start1 .pos.))
1668 ((plusp non-constant-start)
1669 `(:start1 ,non-constant-start))))
1670 (incf (truly-the index .pos.) (length ,var)))
1671 (setf constants nil)))))
1672 .string.))))))
1674 (defun vector-specifier-widetag (type)
1675 ;; FIXME: This only accepts vectors without dimensions even though
1676 ;; it's not that hard to support them for the concatenate transform,
1677 ;; but it's probably not used often enough to bother.
1678 (cond ((and (array-type-p type)
1679 (equal (array-type-dimensions type) '(*)))
1680 (let* ((el-ctype (array-type-element-type type))
1681 (el-ctype (if (eq el-ctype *wild-type*)
1682 *universal-type*
1683 el-ctype))
1684 (saetp (find-saetp-by-ctype el-ctype)))
1685 (when saetp
1686 (sb!vm:saetp-typecode saetp))))
1687 ((and (union-type-p type)
1688 (csubtypep type (specifier-type 'string))
1689 (loop for type in (union-type-types type)
1690 always (and (array-type-p type)
1691 (equal (array-type-dimensions type) '(*)))))
1692 #!+sb-unicode
1693 sb!vm:simple-character-string-widetag
1694 #!-sb-unicode
1695 sb!vm:simple-base-string-widetag)))
1697 (deftransform concatenate ((result-type &rest lvars)
1698 ((constant-arg t)
1699 &rest sequence)
1700 * :node node)
1701 (let* ((type (ir1-transform-specifier-type (lvar-value result-type)))
1702 (vector-widetag (vector-specifier-widetag type)))
1703 (flet ((coerce-constants (vars type)
1704 ;; Lists are faster to iterate over than vectors of
1705 ;; unknown type.
1706 (loop for var in vars
1707 for lvar in lvars
1708 collect (if (constant-lvar-p lvar)
1709 `',(coerce (lvar-value lvar) type)
1710 var))))
1712 (cond ((type= type (specifier-type 'list))
1713 (let ((vars (make-gensym-list (length lvars))))
1714 `(lambda (type ,@vars)
1715 (declare (ignore type)
1716 (ignorable ,@vars))
1717 (%concatenate-to-list ,@(coerce-constants vars 'list)))))
1718 ((not vector-widetag)
1719 (give-up-ir1-transform))
1720 ((= vector-widetag sb!vm:simple-base-string-widetag)
1721 (string-concatenate-transform node 'simple-base-string lvars))
1722 #!+sb-unicode
1723 ((= vector-widetag sb!vm:simple-character-string-widetag)
1724 (string-concatenate-transform node 'string lvars))
1725 ;; FIXME: other vectors may use inlined expansion from
1726 ;; STRING-CONCATENATE-TRANSFORM as well.
1728 (let ((vars (make-gensym-list (length lvars))))
1729 `(lambda (type ,@vars)
1730 (declare (ignore type)
1731 (ignorable ,@vars))
1732 ,(if (= vector-widetag sb!vm:simple-vector-widetag)
1733 `(%concatenate-to-simple-vector
1734 ,@(coerce-constants vars 'vector))
1735 `(%concatenate-to-vector
1736 ,vector-widetag ,@(coerce-constants vars 'list))))))))))
1738 ;;;; CONS accessor DERIVE-TYPE optimizers
1740 (defoptimizer (car derive-type) ((cons))
1741 ;; This and CDR needs to use LVAR-CONSERVATIVE-TYPE because type inference
1742 ;; gets confused by things like (SETF CAR).
1743 (let ((type (lvar-conservative-type cons))
1744 (null-type (specifier-type 'null)))
1745 (cond ((eq type null-type)
1746 null-type)
1747 ((cons-type-p type)
1748 (cons-type-car-type type)))))
1750 (defoptimizer (cdr derive-type) ((cons))
1751 (let ((type (lvar-conservative-type cons))
1752 (null-type (specifier-type 'null)))
1753 (cond ((eq type null-type)
1754 null-type)
1755 ((cons-type-p type)
1756 (cons-type-cdr-type type)))))
1758 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
1760 (defoptimizer (find derive-type) ((item sequence &key key test
1761 start end from-end))
1762 (declare (ignore sequence start end from-end))
1763 (let ((key-fun (or (and key (lvar-fun-name* key)) 'identity)))
1764 ;; If :KEY is a known function, then regardless of the :TEST,
1765 ;; FIND returns an object of the type that KEY accepts, or nil.
1766 ;; If LVAR-FUN-NAME can't be determined, it returns NIL.
1767 ;; :KEY NIL is valid, and means #'IDENTITY.
1768 ;; So either way, we get IDENTITY which skips this code.
1769 (unless (eq key-fun 'identity)
1770 (acond ((info :function :info key-fun)
1771 (let ((type (info :function :type key-fun)))
1772 (return-from find-derive-type-optimizer
1773 (awhen (and (fun-type-p type)
1774 (fun-type-required type))
1775 (type-union (first it) (specifier-type 'null))))))
1776 ((structure-instance-accessor-p key-fun)
1777 (return-from find-derive-type-optimizer
1778 (specifier-type `(or ,(dd-name (car it)) null)))))))
1779 ;; Otherwise maybe FIND returns ITEM itself (or an EQL number).
1780 ;; :TEST is allowed only if EQ or EQL (where NIL means EQL).
1781 ;; :KEY is allowed only if IDENTITY or NIL.
1782 (when (and (or (not test)
1783 (lvar-fun-is test '(eq eql))
1784 (and (constant-lvar-p test) (null (lvar-value test))))
1785 (or (not key)
1786 (lvar-fun-is key '(identity))
1787 (and (constant-lvar-p key) (null (lvar-value key)))))
1788 (type-union (lvar-type item) (specifier-type 'null))))
1790 ;;; We want to make sure that %FIND-POSITION is inline-expanded into
1791 ;;; %FIND-POSITION-IF only when %FIND-POSITION-IF has an inline
1792 ;;; expansion, so we factor out the condition into this function.
1793 (defun check-inlineability-of-find-position-if (sequence from-end)
1794 (let ((ctype (lvar-type sequence)))
1795 (cond ((csubtypep ctype (specifier-type 'vector))
1796 ;; It's not worth trying to inline vector code unless we
1797 ;; know a fair amount about it at compile time.
1798 (upgraded-element-type-specifier-or-give-up sequence)
1799 (unless (constant-lvar-p from-end)
1800 (give-up-ir1-transform
1801 "FROM-END argument value not known at compile time")))
1802 ((csubtypep ctype (specifier-type 'list))
1803 ;; Inlining on lists is generally worthwhile.
1806 (give-up-ir1-transform
1807 "sequence type not known at compile time")))))
1809 ;;; %FIND-POSITION-IF and %FIND-POSITION-IF-NOT for LIST data
1810 (defun %find/position-if-list-expansion (sense from-end start end node)
1811 (declare (ignore from-end))
1812 ;; Circularity detection slows things down. It is permissible not to.
1813 ;; In fact, FIND is given as an archetypal example of a function that
1814 ;; "should be prepared to signal an error" but might not [CLHS 1.4.2].
1815 ;; We relax the definition of "safe" from safety=3 to >=2.
1816 (let ((safe (policy node (>= safety 2)))
1817 ;; The secondary value is inconsequential when flowing into a non-MV
1818 ;; combination, so we avoid counting loop iterations if possible.
1819 ;; This is limited in power, but good enough, for want of a proper
1820 ;; dead-code-elimination phase of the compiler.
1821 (indexed
1822 (not (and (lvar-single-value-p (node-lvar node))
1823 (constant-lvar-p start)
1824 (eql (lvar-value start) 0)
1825 (constant-lvar-p end)
1826 (null (lvar-value end))))))
1827 `(let ((find nil)
1828 (position nil))
1829 (flet ((bounds-error ()
1830 (sequence-bounding-indices-bad-error sequence start end)))
1831 (if (and end (> start end))
1832 (bounds-error)
1833 (do ((slow sequence (cdr slow))
1834 ,@(when safe '((fast (cdr sequence) (cddr fast))))
1835 ,@(when indexed '((index 0 (+ index 1)))))
1836 ((cond ((null slow)
1837 (,@(if indexed
1838 '(if (and end (> end index)) (bounds-error))
1839 '(progn))
1840 (return (values find position))))
1841 ,@(when indexed
1842 '(((and end (>= index end))
1843 (return (values find position)))))
1844 ,@(when safe
1845 '(((eq slow fast)
1846 (circular-list-error sequence)))))
1847 (bug "never"))
1848 (declare (list slow ,@(and safe '(fast)))
1849 ;; If you have as many as INDEX conses on a 32-bit build,
1850 ;; then you've either used up 4GB of memory (impossible)
1851 ;; or you're stuck in a circular list in unsafe code.
1852 ;; Correspondingly larger limit for 64-bit.
1853 ,@(and indexed '((index index))))
1854 (,@(if indexed '(when (>= index start)) '(progn))
1855 (let ((element (car slow)))
1856 ;; This hack of dealing with non-NIL FROM-END for list data
1857 ;; by iterating forward through the list and keeping track of
1858 ;; the last time we found a match might be more screwy than
1859 ;; what the user expects, but it seems to be allowed by the
1860 ;; ANSI standard. (And if the user is screwy enough to ask
1861 ;; for FROM-END behavior on list data, turnabout is fair play.)
1863 ;; It's also not enormously efficient, calling PREDICATE
1864 ;; and KEY more often than necessary; but all the alternatives
1865 ;; seem to have their own efficiency problems.
1866 (,sense (funcall predicate (funcall key element))
1867 (if from-end
1868 (setf find element position ,(and indexed 'index))
1869 (return (values element ,(and indexed 'index)))))))))))))
1871 (macrolet ((def (name condition)
1872 `(deftransform ,name ((predicate sequence from-end start end key)
1873 (function list t t t function)
1875 :node node
1876 :policy (> speed space))
1877 "expand inline"
1878 (%find/position-if-list-expansion ',condition
1879 from-end start end node))))
1880 (def %find-position-if when)
1881 (def %find-position-if-not unless))
1883 ;;; %FIND-POSITION for LIST data can be expanded into %FIND-POSITION-IF
1884 ;;; without loss of efficiency. (I.e., the optimizer should be able
1885 ;;; to straighten everything out.)
1886 (deftransform %find-position ((item sequence from-end start end key test)
1887 (t list t t t t t)
1889 :policy (> speed space))
1890 "expand inline"
1891 '(%find-position-if (let ((test-fun (%coerce-callable-to-fun test)))
1892 ;; The order of arguments for asymmetric tests
1893 ;; (e.g. #'<, as opposed to order-independent
1894 ;; tests like #'=) is specified in the spec
1895 ;; section 17.2.1 -- the O/Zi stuff there.
1896 (lambda (i)
1897 (funcall test-fun item i)))
1898 sequence
1899 from-end
1900 start
1902 (%coerce-callable-to-fun key)))
1904 ;;; The inline expansions for the VECTOR case are saved as macros so
1905 ;;; that we can share them between the DEFTRANSFORMs and the default
1906 ;;; cases in the DEFUNs. (This isn't needed for the LIST case, because
1907 ;;; the DEFTRANSFORMs for LIST are less choosy about when to expand.)
1908 (defun %find-position-or-find-position-if-vector-expansion (sequence-arg
1909 from-end
1910 start
1911 end-arg
1912 element
1913 done-p-expr)
1914 (with-unique-names (offset block index n-sequence sequence end)
1915 `(let* ((,n-sequence ,sequence-arg))
1916 (with-array-data ((,sequence ,n-sequence :offset-var ,offset)
1917 (,start ,start)
1918 (,end ,end-arg)
1919 :check-fill-pointer t)
1920 (block ,block
1921 (macrolet ((maybe-return ()
1922 ;; WITH-ARRAY-DATA has already performed bounds
1923 ;; checking, so we can safely elide the checks
1924 ;; in the inner loop.
1925 '(let ((,element (locally (declare (optimize (insert-array-bounds-checks 0)))
1926 (aref ,sequence ,index))))
1927 (when ,done-p-expr
1928 (return-from ,block
1929 (values ,element
1930 (- ,index ,offset)))))))
1931 (if ,from-end
1932 (loop for ,index
1933 ;; (If we aren't fastidious about declaring that
1934 ;; INDEX might be -1, then (FIND 1 #() :FROM-END T)
1935 ;; can send us off into never-never land, since
1936 ;; INDEX is initialized to -1.)
1937 of-type index-or-minus-1
1938 from (1- ,end) downto ,start do
1939 (maybe-return))
1940 (loop for ,index of-type index from ,start below ,end do
1941 (maybe-return))))
1942 (values nil nil))))))
1944 (sb!xc:defmacro %find-position-vector-macro (item sequence
1945 from-end start end key test)
1946 (with-unique-names (element)
1947 (%find-position-or-find-position-if-vector-expansion
1948 sequence
1949 from-end
1950 start
1952 element
1953 ;; (See the LIST transform for a discussion of the correct
1954 ;; argument order, i.e. whether the searched-for ,ITEM goes before
1955 ;; or after the checked sequence element.)
1956 `(funcall ,test ,item (funcall ,key ,element)))))
1958 (sb!xc:defmacro %find-position-if-vector-macro (predicate sequence
1959 from-end start end key)
1960 (with-unique-names (element)
1961 (%find-position-or-find-position-if-vector-expansion
1962 sequence
1963 from-end
1964 start
1966 element
1967 `(funcall ,predicate (funcall ,key ,element)))))
1969 (sb!xc:defmacro %find-position-if-not-vector-macro (predicate sequence
1970 from-end start end key)
1971 (with-unique-names (element)
1972 (%find-position-or-find-position-if-vector-expansion
1973 sequence
1974 from-end
1975 start
1977 element
1978 `(not (funcall ,predicate (funcall ,key ,element))))))
1980 ;;; %FIND-POSITION, %FIND-POSITION-IF and %FIND-POSITION-IF-NOT for
1981 ;;; VECTOR data
1982 (deftransform %find-position-if ((predicate sequence from-end start end key)
1983 (function vector t t t function)
1985 :policy (> speed space))
1986 "expand inline"
1987 (check-inlineability-of-find-position-if sequence from-end)
1988 '(%find-position-if-vector-macro predicate sequence
1989 from-end start end key))
1991 (deftransform %find-position-if-not ((predicate sequence from-end start end key)
1992 (function vector t t t function)
1994 :policy (> speed space))
1995 "expand inline"
1996 (check-inlineability-of-find-position-if sequence from-end)
1997 '(%find-position-if-not-vector-macro predicate sequence
1998 from-end start end key))
2000 (deftransform %find-position ((item sequence from-end start end key test)
2001 (t vector t t t function function)
2003 :policy (> speed space))
2004 "expand inline"
2005 (check-inlineability-of-find-position-if sequence from-end)
2006 '(%find-position-vector-macro item sequence
2007 from-end start end key test))
2009 (deftransform %find-position ((item sequence from-end start end key test)
2010 (t bit-vector t t t t t)
2011 * :node node)
2012 (when (and test (lvar-fun-is test '(eq eql equal)))
2013 (setf test nil))
2014 (when (and key (lvar-fun-is key '(identity)))
2015 (setf key nil))
2016 (when (or test key)
2017 (delay-ir1-transform node :optimize)
2018 (give-up-ir1-transform "non-trivial :KEY or :TEST"))
2019 (catch 'not-a-bit
2020 `(with-array-data ((bits sequence :offset-var offset)
2021 (start start)
2022 (end end)
2023 :check-fill-pointer t)
2024 (let ((p ,(if (constant-lvar-p item)
2025 (case (lvar-value item)
2026 (0 `(%bit-position/0 bits from-end start end))
2027 (1 `(%bit-position/1 bits from-end start end))
2028 (otherwise (throw 'not-a-bit `(values nil nil))))
2029 `(%bit-position item bits from-end start end))))
2030 (if p
2031 (values item (the index (- (truly-the index p) offset)))
2032 (values nil nil))))))
2034 (deftransform %find-position ((item sequence from-end start end key test)
2035 (character string t t t function function)
2037 :policy (> speed space))
2038 (if (eq '* (upgraded-element-type-specifier sequence))
2039 (let ((form
2040 `(sb!impl::string-dispatch ((simple-array character (*))
2041 (simple-array base-char (*))
2042 (simple-array nil (*)))
2043 sequence
2044 (%find-position item sequence from-end start end key test))))
2045 (if (csubtypep (lvar-type sequence) (specifier-type 'simple-string))
2046 form
2047 ;; Otherwise we'd get three instances of WITH-ARRAY-DATA from
2048 ;; %FIND-POSITION.
2049 `(with-array-data ((sequence sequence :offset-var offset)
2050 (start start)
2051 (end end)
2052 :check-fill-pointer t)
2053 (multiple-value-bind (elt index) ,form
2054 (values elt (when (fixnump index) (- index offset)))))))
2055 ;; The type is known exactly, other transforms will take care of it.
2056 (give-up-ir1-transform)))
2058 ;;; logic to unravel :TEST, :TEST-NOT, and :KEY options in FIND,
2059 ;;; POSITION-IF, etc.
2060 (define-source-transform effective-find-position-test (test test-not)
2061 (once-only ((test test)
2062 (test-not test-not))
2063 `(cond
2064 ((and ,test ,test-not)
2065 (error "can't specify both :TEST and :TEST-NOT"))
2066 (,test (%coerce-callable-to-fun ,test))
2067 (,test-not
2068 ;; (Without DYNAMIC-EXTENT, this is potentially horribly
2069 ;; inefficient, but since the TEST-NOT option is deprecated
2070 ;; anyway, we don't care.)
2071 (complement (%coerce-callable-to-fun ,test-not)))
2072 ;; :TEST of NIL (whether implicit or explicit) means #'EQL.
2073 ;; This behavior is not specified by CLHS, but is fairly conventional.
2074 ;; (KEY is expressly specified as allowing NIL, but TEST is not)
2075 ;; In our implementation, it has to be this way because we don't track
2076 ;; whether the :TEST and :TEST-NOT args were actually present.
2077 (t #'eql))))
2078 (define-source-transform effective-find-position-key (key)
2079 (once-only ((key key))
2080 `(if ,key
2081 (%coerce-callable-to-fun ,key)
2082 #'identity)))
2084 (macrolet ((define-find-position (fun-name values-index)
2085 `(deftransform ,fun-name ((item sequence &key
2086 from-end (start 0) end
2087 key test test-not)
2088 (t (or list vector) &rest t))
2089 '(nth-value ,values-index
2090 (%find-position item sequence
2091 from-end start
2093 (effective-find-position-key key)
2094 (effective-find-position-test
2095 test test-not))))))
2096 (define-find-position find 0)
2097 (define-find-position position 1))
2099 (macrolet ((define-find-position-if (fun-name values-index)
2100 `(deftransform ,fun-name ((predicate sequence &key
2101 from-end (start 0)
2102 end key)
2103 (t (or list vector) &rest t))
2104 '(nth-value
2105 ,values-index
2106 (%find-position-if (%coerce-callable-to-fun predicate)
2107 sequence from-end
2108 start end
2109 (effective-find-position-key key))))))
2110 (define-find-position-if find-if 0)
2111 (define-find-position-if position-if 1))
2113 ;;; the deprecated functions FIND-IF-NOT and POSITION-IF-NOT. We
2114 ;;; didn't bother to worry about optimizing them, except note that on
2115 ;;; Sat, Oct 06, 2001 at 04:22:38PM +0100, Christophe Rhodes wrote on
2116 ;;; sbcl-devel
2118 ;;; My understanding is that while the :test-not argument is
2119 ;;; deprecated in favour of :test (complement #'foo) because of
2120 ;;; semantic difficulties (what happens if both :test and :test-not
2121 ;;; are supplied, etc) the -if-not variants, while officially
2122 ;;; deprecated, would be undeprecated were X3J13 actually to produce
2123 ;;; a revised standard, as there are perfectly legitimate idiomatic
2124 ;;; reasons for allowing the -if-not versions equal status,
2125 ;;; particularly remove-if-not (== filter).
2127 ;;; This is only an informal understanding, I grant you, but
2128 ;;; perhaps it's worth optimizing the -if-not versions in the same
2129 ;;; way as the others?
2131 ;;; FIXME: Maybe remove uses of these deprecated functions within the
2132 ;;; implementation of SBCL.
2133 (macrolet ((define-find-position-if-not (fun-name values-index)
2134 `(deftransform ,fun-name ((predicate sequence &key
2135 from-end (start 0)
2136 end key)
2137 (t (or list vector) &rest t))
2138 '(nth-value
2139 ,values-index
2140 (%find-position-if-not (%coerce-callable-to-fun predicate)
2141 sequence from-end
2142 start end
2143 (effective-find-position-key key))))))
2144 (define-find-position-if-not find-if-not 0)
2145 (define-find-position-if-not position-if-not 1))
2147 (macrolet ((define-trimmer-transform (fun-name leftp rightp)
2148 `(deftransform ,fun-name ((char-bag string)
2149 (t simple-string))
2150 (let ((find-expr
2151 (if (constant-lvar-p char-bag)
2152 ;; If the bag is constant, use MEMBER
2153 ;; instead of FIND, since we have a
2154 ;; deftransform for MEMBER that can
2155 ;; open-code all of the comparisons when
2156 ;; the list is constant. -- JES, 2007-12-10
2157 `(not (member (schar string index)
2158 ',(coerce (lvar-value char-bag) 'list)
2159 :test #'char=))
2160 '(not (find (schar string index) char-bag :test #'char=)))))
2161 `(flet ((char-not-in-bag (index)
2162 ,find-expr))
2163 (let* ((end (length string))
2164 (left-end (if ,',leftp
2165 (do ((index 0 (1+ index)))
2166 ((or (= index (the fixnum end))
2167 (char-not-in-bag index))
2168 index)
2169 (declare (fixnum index)))
2171 (right-end (if ,',rightp
2172 (do ((index (1- end) (1- index)))
2173 ((or (< index left-end)
2174 (char-not-in-bag index))
2175 (1+ index))
2176 (declare (fixnum index)))
2177 end)))
2178 (if (and (eql left-end 0)
2179 (eql right-end (length string)))
2180 string
2181 (subseq string left-end right-end))))))))
2182 (define-trimmer-transform string-left-trim t nil)
2183 (define-trimmer-transform string-right-trim nil t)
2184 (define-trimmer-transform string-trim t t))
2187 ;;; (partially) constant-fold backq-* functions, or convert to their
2188 ;;; plain CL equivalent (now that they're not needed for pprinting).
2190 ;; Pop constant values from the end, list/list* them if any, and link
2191 ;; the remainder with list* at runtime.
2192 (defun transform-backq-list-or-list* (function values)
2193 (let ((gensyms (make-gensym-list (length values)))
2194 (reverse (reverse values))
2195 (constants '()))
2196 (loop while (and reverse
2197 (constant-lvar-p (car reverse)))
2198 do (push (lvar-value (pop reverse))
2199 constants))
2200 (if (null constants)
2201 `(lambda ,gensyms
2202 (,function ,@gensyms))
2203 (let ((tail (apply function constants)))
2204 (if (null reverse)
2205 `',tail
2206 (let* ((nvariants (length reverse))
2207 (variants (subseq gensyms 0 nvariants)))
2208 `(lambda ,gensyms
2209 (declare (ignore ,@(subseq gensyms nvariants)))
2210 ,(if tail
2211 `(list* ,@variants ',tail)
2212 `(list ,@variants)))))))))
2214 (deftransform sb!impl::|List| ((&rest elts))
2215 (transform-backq-list-or-list* 'list elts))
2217 (deftransform sb!impl::|List*| ((&rest elts))
2218 (transform-backq-list-or-list* 'list* elts))
2220 ;; Merge adjacent constant values
2221 (deftransform sb!impl::|Append| ((&rest elts))
2222 (let ((gensyms (make-gensym-list (length elts)))
2223 (acc nil)
2224 (ignored '())
2225 (arguments '()))
2226 (flet ((convert-accumulator ()
2227 (let ((constant (apply 'append (nreverse (shiftf acc nil)))))
2228 (when constant
2229 (push `',constant arguments)))))
2230 (loop for gensym in gensyms
2231 for (elt . next) on elts by #'cdr
2232 do (cond ((constant-lvar-p elt)
2233 (let ((elt (lvar-value elt)))
2234 (when (and next (not (proper-list-p elt)))
2235 (abort-ir1-transform
2236 "Non-list or improper list spliced in ~
2237 the middle of a backquoted list."))
2238 (push gensym ignored)
2239 (push elt acc)))
2241 (convert-accumulator)
2242 (push gensym arguments)))
2243 finally (convert-accumulator)))
2244 (let ((arguments (nreverse arguments)))
2245 `(lambda ,gensyms
2246 (declare (ignore ,@ignored))
2247 (append ,@arguments)))))
2249 (deftransform reverse ((sequence) (vector) * :important nil)
2250 `(sb!impl::vector-reverse sequence))
2252 (deftransform reverse ((sequence) (list) * :important nil)
2253 `(sb!impl::list-reverse sequence))
2255 (deftransform nreverse ((sequence) (vector) * :important nil)
2256 `(sb!impl::vector-nreverse sequence))
2258 (deftransform nreverse ((sequence) (list) * :important nil)
2259 `(sb!impl::list-nreverse sequence))
2261 (deftransforms (intersection nintersection)
2262 ((list1 list2 &key key test test-not))
2263 (let ((null-type (specifier-type 'null)))
2264 (cond ((or (csubtypep (lvar-type list1) null-type)
2265 (csubtypep (lvar-type list2) null-type))
2266 nil)
2267 ((and (same-leaf-ref-p list1 list2)
2268 (not test-not)
2269 (not key)
2270 (or (not test)
2271 (lvar-fun-is test '(eq eql equal equalp))))
2272 'list1)
2274 (give-up-ir1-transform)))))
2276 (deftransforms (union nunion) ((list1 list2 &key key test test-not))
2277 (let ((null-type (specifier-type 'null)))
2278 (cond ((csubtypep (lvar-type list1) null-type)
2279 'list2)
2280 ((csubtypep (lvar-type list2) null-type)
2281 'list1)
2282 ((and (same-leaf-ref-p list1 list2)
2283 (not test-not)
2284 (not key)
2285 (or (not test)
2286 (lvar-fun-is test '(eq eql equal equalp))))
2287 'list1)
2289 (give-up-ir1-transform)))))
2291 (defoptimizer (union derive-type) ((list1 list2 &rest args))
2292 (declare (ignore args))
2293 (let ((cons-type (specifier-type 'cons)))
2294 (if (or (csubtypep (lvar-type list1) cons-type)
2295 (csubtypep (lvar-type list2) cons-type))
2296 cons-type
2297 (specifier-type 'list))))
2299 (defoptimizer (nunion derive-type) ((list1 list2 &rest args))
2300 (declare (ignore args))
2301 (let ((cons-type (specifier-type 'cons)))
2302 (if (or (csubtypep (lvar-type list1) cons-type)
2303 (csubtypep (lvar-type list2) cons-type))
2304 cons-type
2305 (specifier-type 'list))))
2307 (deftransforms (set-difference nset-difference)
2308 ((list1 list2 &key key test test-not))
2309 (let ((null-type (specifier-type 'null)))
2310 (cond ((csubtypep (lvar-type list1) null-type)
2311 nil)
2312 ((csubtypep (lvar-type list2) null-type)
2313 'list1)
2314 ((and (same-leaf-ref-p list1 list2)
2315 (not test-not)
2316 (not key)
2317 (or (not test)
2318 (lvar-fun-is test '(eq eql equal equalp))))
2319 nil)
2321 (give-up-ir1-transform)))))
2323 (deftransform subsetp ((list1 list2 &key key test test-not))
2324 (cond ((csubtypep (lvar-type list1) (specifier-type 'null))
2326 ((and (same-leaf-ref-p list1 list2)
2327 (not test-not)
2328 (not key)
2329 (or (not test)
2330 (lvar-fun-is test '(eq eql equal equalp))))
2333 (give-up-ir1-transform))))
2335 (deftransforms (set-exclusive-or nset-exclusive-or)
2336 ((list1 list2 &key key test test-not))
2337 (let ((null-type (specifier-type 'null)))
2338 (cond ((csubtypep (lvar-type list1) null-type)
2339 'list2)
2340 ((csubtypep (lvar-type list2) null-type)
2341 'list1)
2342 ((and (same-leaf-ref-p list1 list2)
2343 (not test-not)
2344 (not key)
2345 (or (not test)
2346 (lvar-fun-is test '(eq eql equal equalp))))
2347 'list1)
2349 (give-up-ir1-transform)))))
2351 (deftransform tree-equal ((list1 list2 &key test test-not))
2352 (cond ((and (same-leaf-ref-p list1 list2)
2353 (not test-not)
2354 (or (not test)
2355 (lvar-fun-is test '(eq eql equal equalp))))
2357 ((and (not test-not)
2358 (or (not test)
2359 (lvar-fun-is test '(eql))))
2360 `(sb!impl::tree-equal-eql list1 list2))
2362 (give-up-ir1-transform))))