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