Eliminate COLD-FSET. It's just fop-funcall of %DEFUN
[sbcl.git] / src / compiler / seqtran.lisp
blob1687749193ffa122014417caf572a70c1597e137
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 ;;;; mapping onto lists: the MAPFOO functions
16 ;; This expander allows a compiler-macro for FN to take effect by eliding
17 ;; a LET binding of it. Attempting to self-optimize like that isn't the usual
18 ;; SBCL way, however this is a countermeasure to an inhibition of a later
19 ;; optimization, and it is not an onerous change to the expander.
20 ;; We've gone to the trouble of inlining MAPfoo, but the inlined code
21 ;; prevented use of a compiler-macro because %FUNCALL (as opposed to FUNCALL)
22 ;; is not recognized. "Fixing" the compiler to understand %FUNCALL being
23 ;; the same isn't enough: the funarg must be a literal form because we can't
24 ;; know that a variable arg is a never-modified binding of 'F or #'F
25 ;; until IR1 has figured that out, at which point it is too late.
26 ;; [However, see lp# 632368 which asks for something like that.]
28 ;; Also, you might think there to be a subtle difference in behavior from
29 ;; delaying the reference to #'F versus referencing it once. But there is no
30 ;; difference - either way will use the #<fdefn> of F in the call.
31 ;; Of course, it would be ridiculously unportable to to rely on the
32 ;; fact that F can be changed (for its next call) while funcalling it.
34 (defun mapfoo-transform (fn arglists accumulate take-car)
35 (collect ((do-clauses)
36 (args-to-fn)
37 (tests))
38 (let ((n-first (gensym)))
39 (dolist (a (if accumulate
40 arglists
41 `(,n-first ,@(rest arglists))))
42 (let ((v (gensym)))
43 (do-clauses `(,v ,a (cdr ,v)))
44 (tests `(endp ,v))
45 (args-to-fn (if take-car `(car ,v) v))))
47 (binding* (((fn-binding call) (funarg-bind/call-forms fn (args-to-fn)))
48 (endtest `(or ,@(tests))))
49 `(let ,fn-binding
50 ,(ecase accumulate
51 (:nconc
52 (let ((temp (gensym))
53 (map-result (gensym)))
54 `(let ((,map-result
55 (locally (declare (muffle-conditions compiler-note))
56 (list nil))))
57 (declare (truly-dynamic-extent ,map-result))
58 (do-anonymous ((,temp ,map-result) . ,(do-clauses))
59 (,endtest (cdr ,map-result))
60 (setq ,temp (last (nconc ,temp ,call)))))))
61 (:list
62 (let ((temp (gensym))
63 (map-result (gensym)))
64 `(let ((,map-result
65 (locally (declare (muffle-conditions compiler-note))
66 (list nil))))
67 (declare (truly-dynamic-extent ,map-result))
68 (do-anonymous ((,temp ,map-result) . ,(do-clauses))
69 (,endtest (truly-the list (cdr ,map-result)))
70 ;; Accumulate using %RPLACD. RPLACD becomes (SETF CDR)
71 ;; which becomes %RPLACD but relies on "defsetfs".
72 ;; This is for effect, not value, so makes no difference.
73 (%rplacd ,temp (setq ,temp (list ,call)))))))
74 ((nil)
75 `(let ((,n-first ,(first arglists)))
76 (do-anonymous ,(do-clauses)
77 (,endtest (truly-the list ,n-first))
78 ,call)))))))))
80 (define-source-transform mapc (function list &rest more-lists)
81 (mapfoo-transform function (cons list more-lists) nil t))
83 (define-source-transform mapcar (function list &rest more-lists)
84 (mapfoo-transform function (cons list more-lists) :list t))
86 (define-source-transform mapcan (function list &rest more-lists)
87 (mapfoo-transform function (cons list more-lists) :nconc t))
89 (define-source-transform mapl (function list &rest more-lists)
90 (mapfoo-transform function (cons list more-lists) nil nil))
92 (define-source-transform maplist (function list &rest more-lists)
93 (mapfoo-transform function (cons list more-lists) :list nil))
95 (define-source-transform mapcon (function list &rest more-lists)
96 (mapfoo-transform function (cons list more-lists) :nconc nil))
98 ;;;; mapping onto sequences: the MAP function
100 ;;; MAP is %MAP plus a check to make sure that any length specified in
101 ;;; the result type matches the actual result. We also wrap it in a
102 ;;; TRULY-THE for the most specific type we can determine.
103 (deftransform map ((result-type-arg fun seq &rest seqs) * * :node node)
104 (let* ((seq-names (make-gensym-list (1+ (length seqs))))
105 (bare `(%map result-type-arg fun ,@seq-names))
106 (constant-result-type-arg-p (constant-lvar-p result-type-arg))
107 ;; what we know about the type of the result. (Note that the
108 ;; "result type" argument is not necessarily the type of the
109 ;; result, since NIL means the result has NULL type.)
110 (result-type (if (not constant-result-type-arg-p)
111 'consed-sequence
112 (let ((result-type-arg-value
113 (lvar-value result-type-arg)))
114 (if (null result-type-arg-value)
115 'null
116 result-type-arg-value)))))
117 `(lambda (result-type-arg fun ,@seq-names)
118 (truly-the ,result-type
119 ,(cond ((policy node (< safety 3))
120 ;; ANSI requires the length-related type check only
121 ;; when the SAFETY quality is 3... in other cases, we
122 ;; skip it, because it could be expensive.
123 bare)
124 ((not constant-result-type-arg-p)
125 `(sequence-of-checked-length-given-type ,bare
126 result-type-arg))
128 (let ((result-ctype (ir1-transform-specifier-type
129 result-type)))
130 (if (array-type-p result-ctype)
131 (let ((dims (array-type-dimensions result-ctype)))
132 (unless (and (listp dims) (= (length dims) 1))
133 (give-up-ir1-transform "invalid sequence type"))
134 (let ((dim (first dims)))
135 (if (eq dim '*)
136 bare
137 `(vector-of-checked-length-given-length ,bare
138 ,dim))))
139 ;; FIXME: this is wrong, as not all subtypes of
140 ;; VECTOR are ARRAY-TYPEs [consider, for
141 ;; example, (OR (VECTOR T 3) (VECTOR T
142 ;; 4))]. However, it's difficult to see what we
143 ;; should put here... maybe we should
144 ;; GIVE-UP-IR1-TRANSFORM if the type is a
145 ;; subtype of VECTOR but not an ARRAY-TYPE?
146 bare))))))))
148 ;;; Return a DO loop, mapping a function FUN to elements of
149 ;;; sequences. SEQS is a list of lvars, SEQ-NAMES - list of variables,
150 ;;; bound to sequences, INTO - a variable, which is used in
151 ;;; MAP-INTO. RESULT and BODY are forms, which can use variables
152 ;;; FUNCALL-RESULT, containing the result of application of FUN, and
153 ;;; INDEX, containing the current position in sequences.
154 (defun build-sequence-iterator (seqs seq-names &key result into body fast)
155 (declare (type list seqs seq-names)
156 (type symbol into))
157 (collect ((bindings)
158 (declarations)
159 (vector-lengths)
160 (tests)
161 (places)
162 (around))
163 (let ((found-vector-p nil))
164 (flet ((process-vector (length)
165 (unless found-vector-p
166 (setq found-vector-p t)
167 (bindings `(index 0 (1+ index)))
168 (declarations `(type index index)))
169 (vector-lengths length)))
170 (loop for seq of-type lvar in seqs
171 for seq-name in seq-names
172 for type = (lvar-type seq)
173 do (cond ((csubtypep type (specifier-type 'list))
174 (with-unique-names (index)
175 (bindings `(,index ,seq-name (cdr ,index)))
176 (declarations `(type list ,index))
177 (places `(car ,index))
178 (tests `(endp ,index))))
179 ((or (csubtypep type (specifier-type '(simple-array * 1)))
180 (and (not fast)
181 (csubtypep type (specifier-type 'vector))))
182 (process-vector `(length ,seq-name))
183 (places `(locally (declare (optimize (insert-array-bounds-checks 0)))
184 (aref ,seq-name index))))
185 ((csubtypep type (specifier-type 'vector))
186 (let ((data (gensym "DATA"))
187 (start (gensym "START"))
188 (end (gensym "END")))
189 (around `(with-array-data ((,data ,seq-name)
190 (,start)
191 (,end (length ,seq-name)))))
192 (process-vector `(- ,end ,start))
193 (places `(locally (declare (optimize (insert-array-bounds-checks 0)))
194 (aref ,data (truly-the index (+ index ,start)))))))
196 (give-up-ir1-transform
197 "can't determine sequence argument type"))))
198 (when into
199 (process-vector `(array-dimension ,into 0))))
200 (when found-vector-p
201 (bindings `(length (min ,@(vector-lengths))))
202 (tests `(>= index length)))
203 (let ((body `(do (,@(bindings))
204 ((or ,@(tests)) ,result)
205 (declare ,@(declarations))
206 (let ((funcall-result (funcall fun ,@(places))))
207 (declare (ignorable funcall-result))
208 ,body))))
209 (if (around)
210 (reduce (lambda (wrap body) (append wrap (list body)))
211 (around)
212 :from-end t
213 :initial-value body)
214 body)))))
216 ;;; Try to compile %MAP efficiently when we can determine sequence
217 ;;; argument types at compile time.
219 ;;; Note: This transform was written to allow open coding of
220 ;;; quantifiers by expressing them in terms of (MAP NIL ..). For
221 ;;; non-NIL values of RESULT-TYPE, it's still useful, but not
222 ;;; necessarily as efficient as possible. In particular, it will be
223 ;;; inefficient when RESULT-TYPE is a SIMPLE-ARRAY with specialized
224 ;;; numeric element types. It should be straightforward to make it
225 ;;; handle that case more efficiently, but it's left as an exercise to
226 ;;; the reader, because the code is complicated enough already and I
227 ;;; don't happen to need that functionality right now. -- WHN 20000410
228 (deftransform %map ((result-type fun seq &rest seqs) * *
229 :node node :policy (>= speed space))
230 "open code"
231 (unless (constant-lvar-p result-type)
232 (give-up-ir1-transform "RESULT-TYPE argument not constant"))
233 (labels ( ;; 1-valued SUBTYPEP, fails unless second value of SUBTYPEP is true
234 (fn-1subtypep (fn x y)
235 (multiple-value-bind (subtype-p valid-p) (funcall fn x y)
236 (if valid-p
237 subtype-p
238 (give-up-ir1-transform
239 "can't analyze sequence type relationship"))))
240 (1subtypep (x y) (fn-1subtypep #'sb!xc:subtypep x y)))
241 (let* ((result-type-value (lvar-value result-type))
242 (result-supertype (cond ((null result-type-value) 'null)
243 ((1subtypep result-type-value 'vector)
244 'vector)
245 ((1subtypep result-type-value 'list)
246 'list)
248 (give-up-ir1-transform
249 "result type unsuitable")))))
250 (cond ((and result-type-value (null seqs))
251 ;; The consing arity-1 cases can be implemented
252 ;; reasonably efficiently as function calls, and the cost
253 ;; of consing should be significantly larger than
254 ;; function call overhead, so we always compile these
255 ;; cases as full calls regardless of speed-versus-space
256 ;; optimization policy.
257 (cond ((subtypep result-type-value 'list)
258 '(%map-to-list-arity-1 fun seq))
259 ( ;; (This one can be inefficient due to COERCE, but
260 ;; the current open-coded implementation has the
261 ;; same problem.)
262 (subtypep result-type-value 'vector)
263 `(coerce (%map-to-simple-vector-arity-1 fun seq)
264 ',result-type-value))
265 (t (bug "impossible (?) sequence type"))))
267 (let* ((all-seqs (cons seq seqs))
268 (seq-args (make-gensym-list (length all-seqs))))
269 (multiple-value-bind (push-dacc result)
270 (ecase result-supertype
271 (null (values nil nil))
272 (list (values `(push funcall-result acc)
273 `(nreverse acc)))
274 (vector (values `(push funcall-result acc)
275 `(coerce (nreverse acc)
276 ',result-type-value))))
277 ;; (We use the same idiom, of returning a LAMBDA from
278 ;; DEFTRANSFORM, as is used in the DEFTRANSFORMs for
279 ;; FUNCALL and ALIEN-FUNCALL, and for the same
280 ;; reason: we need to get the runtime values of each
281 ;; of the &REST vars.)
282 (block nil
283 (let ((gave-up
284 (catch 'give-up-ir1-transform
285 (return
286 `(lambda (result-type fun ,@seq-args)
287 (declare (ignore result-type))
288 (let ((fun (%coerce-callable-to-fun fun))
289 (acc nil))
290 (declare (type list acc))
291 (declare (ignorable acc))
292 ,(build-sequence-iterator
293 all-seqs seq-args
294 :result result
295 :body push-dacc
296 :fast (policy node (> speed space)))))))))
297 (if (and (null result-type-value) (null seqs))
298 '(%map-for-effect-arity-1 fun seq)
299 (throw 'give-up-ir1-transform gave-up)))))))))))
301 ;;; MAP-INTO
302 (deftransform map-into ((result fun &rest seqs)
303 (vector * &rest *)
304 * :node node)
305 "open code"
306 (let ((seqs-names (mapcar (lambda (x)
307 (declare (ignore x))
308 (gensym))
309 seqs)))
310 `(lambda (result fun ,@seqs-names)
311 ,(if (and (policy node (> speed space))
312 (not (csubtypep (lvar-type result)
313 (specifier-type '(simple-array * 1)))))
314 (let ((data (gensym "DATA"))
315 (start (gensym "START"))
316 (end (gensym "END")))
317 `(with-array-data ((,data result)
318 (,start)
319 (,end))
320 (declare (ignore ,end))
321 ,(build-sequence-iterator
322 seqs seqs-names
323 :result '(when (array-has-fill-pointer-p result)
324 (setf (fill-pointer result) index))
325 :into 'result
326 :body `(locally (declare (optimize (insert-array-bounds-checks 0)))
327 (setf (aref ,data (truly-the index (+ index ,start)))
328 funcall-result))
329 :fast t)))
330 (build-sequence-iterator
331 seqs seqs-names
332 :result '(when (array-has-fill-pointer-p result)
333 (setf (fill-pointer result) index))
334 :into 'result
335 :body '(locally (declare (optimize (insert-array-bounds-checks 0)))
336 (setf (aref result index) funcall-result))))
337 result)))
340 ;;; FIXME: once the confusion over doing transforms with known-complex
341 ;;; arrays is over, we should also transform the calls to (AND (ARRAY
342 ;;; * (*)) (NOT (SIMPLE-ARRAY * (*)))) objects.
343 (deftransform elt ((s i) ((simple-array * (*)) *) *)
344 '(aref s i))
346 (deftransform elt ((s i) (list *) * :policy (< safety 3))
347 '(nth i s))
349 (deftransform %setelt ((s i v) ((simple-array * (*)) * *) *)
350 '(setf (aref s i) v))
352 (deftransform %setelt ((s i v) (list * *) * :policy (< safety 3))
353 '(setf (car (nthcdr i s)) v))
355 (deftransform %check-vector-sequence-bounds ((vector start end)
356 (vector * *) *
357 :node node)
358 (if (policy node (= 0 insert-array-bounds-checks))
359 '(or end (length vector))
360 '(let ((length (length vector)))
361 (if (<= 0 start (or end length) length)
362 (or end length)
363 (sequence-bounding-indices-bad-error vector start end)))))
365 (def!type eq-comparable-type ()
366 '(or fixnum (not number)))
368 ;;; True if EQL comparisons involving type can be simplified to EQ.
369 (defun eq-comparable-type-p (type)
370 (csubtypep type (specifier-type 'eq-comparable-type)))
372 (defun specialized-list-seek-function-name (function-name key-functions &optional variant)
373 (or (find-symbol (with-simple-output-to-string (s)
374 ;; Write "%NAME-FUN1-FUN2-FUN3", etc. Not only is
375 ;; this ever so slightly faster then FORMAT, this
376 ;; way we are also proof against *PRINT-CASE*
377 ;; frobbing and such.
378 (write-char #\% s)
379 (write-string (symbol-name function-name) s)
380 (dolist (f key-functions)
381 (write-char #\- s)
382 (write-string (symbol-name f) s))
383 (when variant
384 (write-char #\- s)
385 (write-string (symbol-name variant) s)))
386 (load-time-value (find-package "SB!KERNEL")))
387 (bug "Unknown list item seek transform: name=~S, key-functions=~S variant=~S"
388 function-name key-functions variant)))
390 (defparameter *list-open-code-limit* 128)
392 (defun transform-list-item-seek (name item list key test test-not node)
393 (when (and test test-not)
394 (abort-ir1-transform "Both ~S and ~S supplied to ~S." :test :test-not name))
395 ;; If TEST is EQL, drop it.
396 (when (and test (lvar-fun-is test '(eql)))
397 (setf test nil))
398 ;; Ditto for KEY IDENTITY.
399 (when (and key (lvar-fun-is key '(identity)))
400 (setf key nil))
401 ;; Key can legally be NIL, but if it's NIL for sure we pretend it's
402 ;; not there at all. If it might be NIL, make up a form to that
403 ;; ensures it is a function.
404 (multiple-value-bind (key key-form)
405 (when key
406 (let ((key-type (lvar-type key))
407 (null-type (specifier-type 'null)))
408 (cond ((csubtypep key-type null-type)
409 (values nil nil))
410 ((csubtypep null-type key-type)
411 (values key '(if key
412 (%coerce-callable-to-fun key)
413 #'identity)))
415 (values key (ensure-lvar-fun-form key 'key))))))
416 (let* ((c-test (cond ((and test (lvar-fun-is test '(eq)))
417 (setf test nil)
418 'eq)
419 ((and (not test) (not test-not))
420 (when (eq-comparable-type-p (lvar-type item))
421 'eq))))
422 (funs (delete nil (list (when key (list key 'key))
423 (when test (list test 'test))
424 (when test-not (list test-not 'test-not)))))
425 (target-expr (if key '(%funcall key target) 'target))
426 (test-expr (cond (test `(%funcall test item ,target-expr))
427 (test-not `(not (%funcall test-not item ,target-expr)))
428 (c-test `(,c-test item ,target-expr))
429 (t `(eql item ,target-expr)))))
430 (labels ((open-code (tail)
431 (when tail
432 `(if (let ((this ',(car tail)))
433 ,(ecase name
434 ((assoc rassoc)
435 (let ((cxx (if (eq name 'assoc) 'car 'cdr)))
436 `(and this (let ((target (,cxx this)))
437 ,test-expr))))
438 (member
439 `(let ((target this))
440 ,test-expr))))
441 ',(ecase name
442 ((assoc rassoc) (car tail))
443 (member tail))
444 ,(open-code (cdr tail)))))
445 (ensure-fun (args)
446 (if (eq 'key (second args))
447 key-form
448 (apply #'ensure-lvar-fun-form args))))
449 (let* ((cp (constant-lvar-p list))
450 (c-list (when cp (lvar-value list))))
451 (cond ((and cp c-list (member name '(assoc rassoc member))
452 (policy node (>= speed space))
453 (not (nthcdr *list-open-code-limit* c-list)))
454 `(let ,(mapcar (lambda (fun) `(,(second fun) ,(ensure-fun fun))) funs)
455 ,(open-code c-list)))
456 ((and cp (not c-list))
457 ;; constant nil list
458 (if (eq name 'adjoin)
459 '(list item)
460 nil))
462 ;; specialized out-of-line version
463 `(,(specialized-list-seek-function-name name (mapcar #'second funs) c-test)
464 item list ,@(mapcar #'ensure-fun funs)))))))))
466 (defun transform-list-pred-seek (name pred list key node)
467 ;; If KEY is IDENTITY, drop it.
468 (when (and key (lvar-fun-is key '(identity)))
469 (setf key nil))
470 ;; Key can legally be NIL, but if it's NIL for sure we pretend it's
471 ;; not there at all. If it might be NIL, make up a form to that
472 ;; ensures it is a function.
473 (multiple-value-bind (key key-form)
474 (when key
475 (let ((key-type (lvar-type key))
476 (null-type (specifier-type 'null)))
477 (cond ((csubtypep key-type null-type)
478 (values nil nil))
479 ((csubtypep null-type key-type)
480 (values key '(if key
481 (%coerce-callable-to-fun key)
482 #'identity)))
484 (values key (ensure-lvar-fun-form key 'key))))))
485 (let ((test-expr `(%funcall pred ,(if key '(%funcall key target) 'target)))
486 (pred-expr (ensure-lvar-fun-form pred 'pred)))
487 (when (member name '(member-if-not assoc-if-not rassoc-if-not))
488 (setf test-expr `(not ,test-expr)))
489 (labels ((open-code (tail)
490 (when tail
491 `(if (let ((this ',(car tail)))
492 ,(ecase name
493 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
494 (let ((cxx (if (member name '(assoc-if assoc-if-not)) 'car 'cdr)))
495 `(and this (let ((target (,cxx this)))
496 ,test-expr))))
497 ((member-if member-if-not)
498 `(let ((target this))
499 ,test-expr))))
500 ',(ecase name
501 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
502 (car tail))
503 ((member-if member-if-not)
504 tail))
505 ,(open-code (cdr tail))))))
506 (let* ((cp (constant-lvar-p list))
507 (c-list (when cp (lvar-value list))))
508 (cond ((and cp c-list (policy node (>= speed space))
509 (not (nthcdr *list-open-code-limit* c-list)))
510 `(let ((pred ,pred-expr)
511 ,@(when key `((key ,key-form))))
512 ,(open-code c-list)))
513 ((and cp (not c-list))
514 ;; constant nil list -- nothing to find!
515 nil)
517 ;; specialized out-of-line version
518 `(,(specialized-list-seek-function-name name (when key '(key)))
519 ,pred-expr list ,@(when key (list key-form))))))))))
521 (macrolet ((def (name &optional if/if-not)
522 (let ((basic (symbolicate "%" name))
523 (basic-eq (symbolicate "%" name "-EQ"))
524 (basic-key (symbolicate "%" name "-KEY"))
525 (basic-key-eq (symbolicate "%" name "-KEY-EQ")))
526 `(progn
527 (deftransform ,name ((item list &key key test test-not) * * :node node)
528 (transform-list-item-seek ',name item list key test test-not node))
529 (deftransform ,basic ((item list) (eq-comparable-type t))
530 `(,',basic-eq item list))
531 (deftransform ,basic-key ((item list) (eq-comparable-type t))
532 `(,',basic-key-eq item list))
533 ,@(when if/if-not
534 (let ((if-name (symbolicate name "-IF"))
535 (if-not-name (symbolicate name "-IF-NOT")))
536 `((deftransform ,if-name ((pred list &key key) * * :node node)
537 (transform-list-pred-seek ',if-name pred list key node))
538 (deftransform ,if-not-name ((pred list &key key) * * :node node)
539 (transform-list-pred-seek ',if-not-name pred list key node)))))))))
540 (def adjoin)
541 (def assoc t)
542 (def member t)
543 (def rassoc t))
545 (deftransform memq ((item list) (t (constant-arg list)))
546 (labels ((rec (tail)
547 (if tail
548 `(if (eq item ',(car tail))
549 ',tail
550 ,(rec (cdr tail)))
551 nil)))
552 (rec (lvar-value list))))
554 ;;; A similar transform used to apply to MEMBER and ASSOC, but since
555 ;;; TRANSFORM-LIST-ITEM-SEEK now takes care of them those transform
556 ;;; would never fire, and (%MEMBER-TEST ITEM LIST #'EQ) should be
557 ;;; almost as fast as MEMQ.
558 (deftransform delete ((item list &key test) (t list &rest t) *)
559 "convert to EQ test"
560 (let ((type (lvar-type item)))
561 (unless (or (and test (lvar-fun-is test '(eq)))
562 (and (eq-comparable-type-p type)
563 (or (not test) (lvar-fun-is test '(eql)))))
564 (give-up-ir1-transform)))
565 `(delq item list))
567 (deftransform delete-if ((pred list) (t list))
568 "open code"
569 '(do ((x list (cdr x))
570 (splice '()))
571 ((endp x) list)
572 (cond ((funcall pred (car x))
573 (if (null splice)
574 (setq list (cdr x))
575 (rplacd splice (cdr x))))
576 (t (setq splice x)))))
578 (deftransform fill ((seq item &key (start 0) (end nil))
579 (list t &key (:start t) (:end t)))
580 '(list-fill* seq item start end))
582 (deftransform fill ((seq item &key (start 0) (end nil))
583 (vector t &key (:start t) (:end t))
585 :node node)
586 (let* ((type (lvar-type seq))
587 (element-ctype (array-type-upgraded-element-type type))
588 (element-type (type-specifier element-ctype))
589 (saetp (unless (eq *wild-type* element-ctype)
590 (find-saetp-by-ctype element-ctype))))
591 (cond ((eq *wild-type* element-ctype)
592 (delay-ir1-transform node :constraint)
593 `(vector-fill* seq item start end))
594 ((and saetp (sb!vm::valid-bit-bash-saetp-p saetp))
595 (let* ((n-bits (sb!vm:saetp-n-bits saetp))
596 (basher-name (format nil "UB~D-BASH-FILL" n-bits))
597 (basher (or (find-symbol basher-name
598 (load-time-value (find-package "SB!KERNEL")))
599 (abort-ir1-transform
600 "Unknown fill basher, please report to sbcl-devel: ~A"
601 basher-name)))
602 (kind (cond ((sb!vm:saetp-fixnum-p saetp) :tagged)
603 ((member element-type '(character base-char)) :char)
604 ((eq element-type 'single-float) :single-float)
605 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
606 ((eq element-type 'double-float) :double-float)
607 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
608 ((equal element-type '(complex single-float))
609 :complex-single-float)
611 (aver (integer-type-p element-ctype))
612 :bits)))
613 ;; BASH-VALUE is a word that we can repeatedly smash
614 ;; on the array: for less-than-word sized elements it
615 ;; contains multiple copies of the fill item.
616 (bash-value
617 (if (constant-lvar-p item)
618 (let ((tmp (lvar-value item)))
619 (unless (ctypep tmp element-ctype)
620 (abort-ir1-transform "~S is not ~S" tmp element-type))
621 (let* ((bits
622 (ldb (byte n-bits 0)
623 (ecase kind
624 (:tagged
625 (ash tmp sb!vm:n-fixnum-tag-bits))
626 (:char
627 (char-code tmp))
628 (:bits
629 tmp)
630 (:single-float
631 (single-float-bits tmp))
632 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
633 (:double-float
634 (logior (ash (double-float-high-bits tmp) 32)
635 (double-float-low-bits tmp)))
636 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
637 (:complex-single-float
638 (logior (ash (single-float-bits (imagpart tmp)) 32)
639 (ldb (byte 32 0)
640 (single-float-bits (realpart tmp))))))))
641 (res bits))
642 (loop for i of-type sb!vm:word from n-bits by n-bits
643 until (= i sb!vm:n-word-bits)
644 do (setf res (ldb (byte sb!vm:n-word-bits 0)
645 (logior res (ash bits i)))))
646 res))
647 (progn
648 (delay-ir1-transform node :constraint)
649 `(let* ((bits (ldb (byte ,n-bits 0)
650 ,(ecase kind
651 (:tagged
652 `(ash item ,sb!vm:n-fixnum-tag-bits))
653 (:char
654 `(char-code item))
655 (:bits
656 `item)
657 (:single-float
658 `(single-float-bits item))
659 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
660 (:double-float
661 `(logior (ash (double-float-high-bits item) 32)
662 (double-float-low-bits item)))
663 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
664 (:complex-single-float
665 `(logior (ash (single-float-bits (imagpart item)) 32)
666 (ldb (byte 32 0)
667 (single-float-bits (realpart item))))))))
668 (res bits))
669 (declare (type sb!vm:word res))
670 ,@(unless (= sb!vm:n-word-bits n-bits)
671 `((loop for i of-type sb!vm:word from ,n-bits by ,n-bits
672 until (= i sb!vm:n-word-bits)
673 do (setf res
674 (ldb (byte ,sb!vm:n-word-bits 0)
675 (logior res (ash bits (truly-the (integer 0 ,(- sb!vm:n-word-bits n-bits)) i))))))))
676 res)))))
677 (values
678 ;; KLUDGE: WITH-ARRAY data in its full glory is going to mess up
679 ;; dynamic-extent for MAKE-ARRAY :INITIAL-ELEMENT initialization.
680 (if (csubtypep (lvar-type seq) (specifier-type '(simple-array * (*))))
681 `(let* ((len (length seq))
682 (end (or end len))
683 (bound (1+ end)))
684 ;; Minor abuse %CHECK-BOUND for bounds checking.
685 ;; (- END START) may still end up negative, but
686 ;; the basher handle that.
687 (,basher ,bash-value seq
688 (%check-bound seq bound start)
689 (- (if end (%check-bound seq bound end) len)
690 start)))
691 `(with-array-data ((data seq)
692 (start start)
693 (end end)
694 :check-fill-pointer t)
695 (declare (type (simple-array ,element-type 1) data))
696 (declare (type index start end))
697 (declare (optimize (safety 0) (speed 3)))
698 (,basher ,bash-value data start (- end start))
699 seq))
700 `((declare (type ,element-type item))))))
701 ((policy node (> speed space))
702 (values
703 `(with-array-data ((data seq)
704 (start start)
705 (end end)
706 :check-fill-pointer t)
707 (declare (type (simple-array ,element-type 1) data))
708 (declare (type index start end))
709 ;; WITH-ARRAY-DATA did our range checks once and for all, so
710 ;; it'd be wasteful to check again on every AREF...
711 (declare (optimize (safety 0) (speed 3)))
712 (do ((i start (1+ i)))
713 ((= i end) seq)
714 (declare (type index i))
715 (setf (aref data i) item)))
716 ;; ... though we still need to check that the new element can fit
717 ;; into the vector in safe code. -- CSR, 2002-07-05
718 `((declare (type ,element-type item)))))
719 ((csubtypep type (specifier-type 'string))
720 '(string-fill* seq item start end))
722 '(vector-fill* seq item start end)))))
724 (deftransform fill ((seq item &key (start 0) (end nil))
725 ((and sequence (not vector) (not list)) t &key (:start t) (:end t)))
726 `(sb!sequence:fill seq item
727 :start start
728 :end (%check-generic-sequence-bounds seq start end)))
730 ;;;; hairy sequence transforms
732 ;;; FIXME: no hairy sequence transforms in SBCL?
734 ;;; There used to be a bunch of commented out code about here,
735 ;;; containing the (apparent) beginning of hairy sequence transform
736 ;;; infrastructure. People interested in implementing better sequence
737 ;;; transforms might want to look at it for inspiration, even though
738 ;;; the actual code is ancient CMUCL -- and hence bitrotted. The code
739 ;;; was deleted in 1.0.7.23.
741 ;;;; string operations
743 ;;; We transform the case-sensitive string predicates into a non-keyword
744 ;;; version. This is an IR1 transform so that we don't have to worry about
745 ;;; changing the order of evaluation.
746 (macrolet ((def (fun pred*)
747 `(deftransform ,fun ((string1 string2 &key (start1 0) end1
748 (start2 0) end2)
749 * *)
750 `(,',pred* string1 string2 start1 end1 start2 end2))))
751 (def string< string<*)
752 (def string> string>*)
753 (def string<= string<=*)
754 (def string>= string>=*)
755 (def string= string=*)
756 (def string/= string/=*))
758 ;;; Return a form that tests the free variables STRING1 and STRING2
759 ;;; for the ordering relationship specified by LESSP and EQUALP. The
760 ;;; start and end are also gotten from the environment. Both strings
761 ;;; must be SIMPLE-BASE-STRINGs.
762 (macrolet ((def (name lessp equalp)
763 `(deftransform ,name ((string1 string2 start1 end1 start2 end2)
764 (simple-base-string simple-base-string t t t t) *)
765 `(let* ((end1 (if (not end1) (length string1) end1))
766 (end2 (if (not end2) (length string2) end2))
767 (index (sb!impl::%sp-string-compare
768 string1 start1 end1 string2 start2 end2)))
769 (if index
770 (cond ((= index end1)
771 ,(if ',lessp 'index nil))
772 ((= (+ index (- start2 start1)) end2)
773 ,(if ',lessp nil 'index))
774 ((,(if ',lessp 'char< 'char>)
775 (schar string1 index)
776 (schar string2
777 (truly-the index
778 (+ index
779 (truly-the fixnum
780 (- start2
781 start1))))))
782 index)
783 (t nil))
784 ,(if ',equalp 'end1 nil))))))
785 (def string<* t nil)
786 (def string<=* t t)
787 (def string>* nil nil)
788 (def string>=* nil t))
790 (deftransform string=* ((string1 string2 start1 end1 start2 end2)
791 (string string
792 (constant-arg (eql 0))
793 (constant-arg null)
794 (constant-arg (eql 0))
795 (constant-arg null)))
796 (cond ((and (constant-lvar-p string1)
797 (equal (lvar-value string1) ""))
798 `(zerop (length string2)))
799 ((and (constant-lvar-p string2)
800 (equal (lvar-value string2) ""))
801 `(zerop (length string1)))
803 (give-up-ir1-transform))))
805 (deftransform string/=* ((string1 string2 start1 end1 start2 end2)
806 (string string
807 (constant-arg (eql 0))
808 (constant-arg null)
809 (constant-arg (eql 0))
810 (constant-arg null)))
811 (cond ((and (constant-lvar-p string1)
812 (equal (lvar-value string1) ""))
813 (lvar-type string2)
814 `(and (plusp (length string2))
816 ((and (constant-lvar-p string2)
817 (equal (lvar-value string2) ""))
818 `(and (plusp (length string1))
821 (give-up-ir1-transform))))
823 (macrolet ((def (name result-fun)
824 `(deftransform ,name ((string1 string2 start1 end1 start2 end2)
825 (simple-base-string simple-base-string t t t t) *)
826 `(,',result-fun
827 (sb!impl::%sp-string-compare
828 string1 start1 (or end1 (length string1))
829 string2 start2 (or end2 (length string2)))))))
830 (def string=* not) ; FIXME: this xform looks counterproductive.
831 (def string/=* identity))
833 (deftransform string/=* ((str1 str2 start1 end1 start2 end2) * * :node node
834 :important nil)
835 ;; An IF node doesn't care about the mismatch index.
836 ;; Transforming to (not (string= ..)) would lead to internal confusion
837 ;; due to incorrect typing: STRING/= can't return T, so return 0 for true.
838 (if (if-p (node-dest node))
839 `(if (string=* str1 str2 start1 end1 start2 end2) nil 0)
840 (give-up-ir1-transform)))
842 (deftransform string ((x) (symbol)) '(symbol-name x))
844 ;;;; transforms for sequence functions
846 ;;; Moved here from generic/vm-tran.lisp to satisfy clisp. Only applies
847 ;;; to vectors based on simple arrays.
848 (def!constant vector-data-bit-offset
849 (* sb!vm:vector-data-offset sb!vm:n-word-bits))
851 ;;; FIXME: In the copy loops below, we code the loops in a strange
852 ;;; fashion:
854 ;;; (do ((i (+ src-offset length) (1- i)))
855 ;;; ((<= i 0) ...)
856 ;;; (... (aref foo (1- i)) ...))
858 ;;; rather than the more natural (and seemingly more efficient):
860 ;;; (do ((i (1- (+ src-offset length)) (1- i)))
861 ;;; ((< i 0) ...)
862 ;;; (... (aref foo i) ...))
864 ;;; (more efficient because we don't have to do the index adjusting on
865 ;;; every iteration of the loop)
867 ;;; We do this to avoid a suboptimality in SBCL's backend. In the
868 ;;; latter case, the backend thinks I is a FIXNUM (which it is), but
869 ;;; when used as an array index, the backend thinks I is a
870 ;;; POSITIVE-FIXNUM (which it is). However, since the backend thinks of
871 ;;; these as distinct storage classes, it cannot coerce a move from a
872 ;;; FIXNUM TN to a POSITIVE-FIXNUM TN. The practical effect of this
873 ;;; deficiency is that we have two extra moves and increased register
874 ;;; pressure, which can lead to some spectacularly bad register
875 ;;; allocation. (sub-FIXME: the register allocation even with the
876 ;;; strangely written loops is not always excellent, either...). Doing
877 ;;; it the first way, above, means that I is always thought of as a
878 ;;; POSITIVE-FIXNUM and there are no issues.
880 ;;; Besides, the *-WITH-OFFSET machinery will fold those index
881 ;;; adjustments in the first version into the array addressing at no
882 ;;; performance penalty!
884 ;;; This transform is critical to the performance of string streams. If
885 ;;; you tweak it, make sure that you compare the disassembly, if not the
886 ;;; performance of, the functions implementing string streams
887 ;;; (e.g. SB!IMPL::STRING-OUCH).
888 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
889 (defun make-replace-transform (saetp sequence-type1 sequence-type2)
890 `(deftransform replace ((seq1 seq2 &key (start1 0) (start2 0) end1 end2)
891 (,sequence-type1 ,sequence-type2 &rest t)
892 ,sequence-type1
893 :node node)
894 `(let* ((len1 (length seq1))
895 (len2 (length seq2))
896 (end1 (or end1 len1))
897 (end2 (or end2 len2))
898 (replace-len (min (- end1 start1) (- end2 start2))))
899 ,(unless (policy node (= insert-array-bounds-checks 0))
900 `(progn
901 (unless (<= 0 start1 end1 len1)
902 (sequence-bounding-indices-bad-error seq1 start1 end1))
903 (unless (<= 0 start2 end2 len2)
904 (sequence-bounding-indices-bad-error seq2 start2 end2))))
905 ,',(cond
906 ((and saetp (sb!vm:valid-bit-bash-saetp-p saetp))
907 (let* ((n-element-bits (sb!vm:saetp-n-bits saetp))
908 (bash-function (intern (format nil "UB~D-BASH-COPY"
909 n-element-bits)
910 (find-package "SB!KERNEL"))))
911 `(funcall (function ,bash-function) seq2 start2
912 seq1 start1 replace-len)))
914 `(if (and
915 ;; If the sequence types are different, SEQ1 and
916 ;; SEQ2 must be distinct arrays.
917 ,(eql sequence-type1 sequence-type2)
918 (eq seq1 seq2) (> start1 start2))
919 (do ((i (truly-the index (+ start1 replace-len -1))
920 (1- i))
921 (j (truly-the index (+ start2 replace-len -1))
922 (1- j)))
923 ((< i start1))
924 (declare (optimize (insert-array-bounds-checks 0)))
925 (setf (aref seq1 i) (aref seq2 j)))
926 (do ((i start1 (1+ i))
927 (j start2 (1+ j))
928 (end (+ start1 replace-len)))
929 ((>= i end))
930 (declare (optimize (insert-array-bounds-checks 0)))
931 (setf (aref seq1 i) (aref seq2 j))))))
932 seq1))))
934 (macrolet
935 ((define-replace-transforms ()
936 (loop for saetp across sb!vm:*specialized-array-element-type-properties*
937 for sequence-type = `(simple-array ,(sb!vm:saetp-specifier saetp) (*))
938 unless (= (sb!vm:saetp-typecode saetp) sb!vm::simple-array-nil-widetag)
939 collect (make-replace-transform saetp sequence-type sequence-type)
940 into forms
941 finally (return `(progn ,@forms))))
942 (define-one-transform (sequence-type1 sequence-type2)
943 (make-replace-transform nil sequence-type1 sequence-type2)))
944 (define-replace-transforms)
945 #!+sb-unicode
946 (progn
947 (define-one-transform (simple-array base-char (*)) (simple-array character (*)))
948 (define-one-transform (simple-array character (*)) (simple-array base-char (*)))))
950 ;;; Expand simple cases of UB<SIZE>-BASH-COPY inline. "simple" is
951 ;;; defined as those cases where we are doing word-aligned copies from
952 ;;; both the source and the destination and we are copying from the same
953 ;;; offset from both the source and the destination. (The last
954 ;;; condition is there so we can determine the direction to copy at
955 ;;; compile time rather than runtime. Remember that UB<SIZE>-BASH-COPY
956 ;;; acts like memmove, not memcpy.) These conditions may seem rather
957 ;;; restrictive, but they do catch common cases, like allocating a (* 2
958 ;;; N)-size buffer and blitting in the old N-size buffer in.
960 (defun frob-bash-transform (src src-offset
961 dst dst-offset
962 length n-elems-per-word)
963 (declare (ignore src dst length))
964 (let ((n-bits-per-elem (truncate sb!vm:n-word-bits n-elems-per-word)))
965 (multiple-value-bind (src-word src-elt)
966 (truncate (lvar-value src-offset) n-elems-per-word)
967 (multiple-value-bind (dst-word dst-elt)
968 (truncate (lvar-value dst-offset) n-elems-per-word)
969 ;; Avoid non-word aligned copies.
970 (unless (and (zerop src-elt) (zerop dst-elt))
971 (give-up-ir1-transform))
972 ;; Avoid copies where we would have to insert code for
973 ;; determining the direction of copying.
974 (unless (= src-word dst-word)
975 (give-up-ir1-transform))
976 ;; FIXME: The cross-compiler doesn't optimize TRUNCATE properly,
977 ;; so we have to do its work here.
978 `(let ((end (+ ,src-word ,(if (= n-elems-per-word 1)
979 'length
980 `(truncate (the index length) ,n-elems-per-word)))))
981 (declare (type index end))
982 ;; Handle any bits at the end.
983 (when (logtest length (1- ,n-elems-per-word))
984 (let* ((extra (mod length ,n-elems-per-word))
985 ;; FIXME: The shift amount on this ASH is
986 ;; *always* negative, but the backend doesn't
987 ;; have a NEGATIVE-FIXNUM primitive type, so we
988 ;; wind up with a pile of code that tests the
989 ;; sign of the shift count prior to shifting when
990 ;; all we need is a simple negate and shift
991 ;; right. Yuck.
992 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
993 (* (- extra ,n-elems-per-word)
994 ,n-bits-per-elem))))
995 (setf (%vector-raw-bits dst end)
996 (logior
997 (logandc2 (%vector-raw-bits dst end)
998 (ash mask
999 ,(ecase *backend-byte-order*
1000 (:little-endian 0)
1001 (:big-endian `(* (- ,n-elems-per-word extra)
1002 ,n-bits-per-elem)))))
1003 (logand (%vector-raw-bits src end)
1004 (ash mask
1005 ,(ecase *backend-byte-order*
1006 (:little-endian 0)
1007 (:big-endian `(* (- ,n-elems-per-word extra)
1008 ,n-bits-per-elem)))))))))
1009 ;; Copy from the end to save a register.
1010 (do ((i end (1- i)))
1011 ((<= i ,src-word))
1012 (setf (%vector-raw-bits dst (1- i))
1013 (%vector-raw-bits src (1- i))))
1014 (values))))))
1016 #.(loop for i = 1 then (* i 2)
1017 collect `(deftransform ,(intern (format nil "UB~D-BASH-COPY" i)
1018 "SB!KERNEL")
1019 ((src src-offset
1020 dst dst-offset
1021 length)
1022 ((simple-unboxed-array (*))
1023 (constant-arg index)
1024 (simple-unboxed-array (*))
1025 (constant-arg index)
1026 index)
1028 (frob-bash-transform src src-offset
1029 dst dst-offset length
1030 ,(truncate sb!vm:n-word-bits i))) into forms
1031 until (= i sb!vm:n-word-bits)
1032 finally (return `(progn ,@forms)))
1034 ;;; We expand copy loops inline in SUBSEQ and COPY-SEQ if we're copying
1035 ;;; arrays with elements of size >= the word size. We do this because
1036 ;;; we know the arrays cannot alias (one was just consed), therefore we
1037 ;;; can determine at compile time the direction to copy, and for
1038 ;;; word-sized elements, UB<WORD-SIZE>-BASH-COPY will do a bit of
1039 ;;; needless checking to figure out what's going on. The same
1040 ;;; considerations apply if we are copying elements larger than the word
1041 ;;; size, with the additional twist that doing it inline is likely to
1042 ;;; cons far less than calling REPLACE and letting generic code do the
1043 ;;; work.
1045 ;;; However, we do not do this for elements whose size is < than the
1046 ;;; word size because we don't want to deal with any alignment issues
1047 ;;; inline. The UB*-BASH-COPY transforms might fix things up later
1048 ;;; anyway.
1050 (defun inlineable-copy-vector-p (type)
1051 (and (array-type-p type)
1052 ;; The two transforms that use this test already specify that their
1053 ;; sequence argument is a VECTOR,
1054 ;; so this seems like it would be more efficient as
1055 ;; and (not (array-type-complexp type))
1056 ;; (not (eq (array-type-element-type type) *wild-type*))
1057 ;; Anyway it no longer works to write this as a single specifier
1058 ;; '(or (simple-unboxed-array (*)) simple-vector) because that
1059 ;; type is just (simple-array * (*)) which isn't amenable to
1060 ;; inline copying since we don't know what it holds.
1061 (or (csubtypep type (specifier-type '(simple-unboxed-array (*))))
1062 (csubtypep type (specifier-type 'simple-vector)))))
1064 (defun maybe-expand-copy-loop-inline (src src-offset dst dst-offset length
1065 element-type)
1066 (let ((saetp (find-saetp element-type)))
1067 (aver saetp)
1068 (if (>= (sb!vm:saetp-n-bits saetp) sb!vm:n-word-bits)
1069 (expand-aref-copy-loop src src-offset dst dst-offset length)
1070 `(locally (declare (optimize (safety 0)))
1071 (replace ,dst ,src :start1 ,dst-offset :start2 ,src-offset :end1 ,length)))))
1073 (defun expand-aref-copy-loop (src src-offset dst dst-offset length)
1074 (if (eql src-offset dst-offset)
1075 `(do ((i (+ ,src-offset ,length) (1- i)))
1076 ((<= i ,src-offset))
1077 (declare (optimize (insert-array-bounds-checks 0)))
1078 (setf (aref ,dst (1- i)) (aref ,src (1- i))))
1079 ;; KLUDGE: The compiler is not able to derive that (+ offset
1080 ;; length) must be a fixnum, but arrives at (unsigned-byte 29).
1081 ;; We, however, know it must be so, as by this point the bounds
1082 ;; have already been checked.
1083 `(do ((i (truly-the fixnum (+ ,src-offset ,length)) (1- i))
1084 (j (+ ,dst-offset ,length) (1- j)))
1085 ((<= i ,src-offset))
1086 (declare (optimize (insert-array-bounds-checks 0))
1087 (type (integer 0 #.sb!xc:array-dimension-limit) j i))
1088 (setf (aref ,dst (1- j)) (aref ,src (1- i))))))
1090 ;;; MAKE-SEQUENCE, SUBSEQ, COPY-SEQ
1092 (deftransform make-sequence ((result-type size &key initial-element) * *)
1093 (multiple-value-bind (spec type)
1094 (and (constant-lvar-p result-type)
1095 (let ((spec (lvar-value result-type)))
1096 (values spec (ir1-transform-specifier-type spec))))
1097 (unless type
1098 (give-up-ir1-transform))
1099 (multiple-value-bind (elt-type dim complexp)
1100 (cond ((and (union-type-p type)
1101 (csubtypep type (specifier-type 'string)))
1102 (let* ((types (union-type-types type))
1103 (first (first types)))
1104 (when (array-type-p first)
1105 (let ((dim (first (array-type-dimensions first)))
1106 (complexp (array-type-complexp first)))
1107 ;; Require sameness of dim and complexp. Give up on
1108 ;; (OR (VECTOR CHARACTER) (VECTOR BASE-CHAR 2))
1109 ;; which eventually fails in the call to the function.
1110 (when (every (lambda (x)
1111 (and (array-type-p x)
1112 (eql (first (array-type-dimensions x))
1113 dim)
1114 (eq (array-type-complexp x) complexp)))
1115 (rest types))
1116 (values
1117 `(or ,@(mapcar
1118 (lambda (x)
1119 (type-specifier (array-type-element-type x)))
1120 types))
1121 dim complexp))))))
1122 ((and (array-type-p type)
1123 (csubtypep type (specifier-type 'vector)))
1124 (when (contains-unknown-type-p (array-type-element-type type))
1125 (give-up-ir1-transform "~S is an unknown vector type" spec))
1126 (values (let ((et (array-type-element-type type)))
1127 ;; VECTOR means (VECTOR T)
1128 (if (type= et *wild-type*) 't (type-specifier et)))
1129 (first (array-type-dimensions type))
1130 (array-type-complexp type))))
1131 ;; Don't transform if size is present in the specifier
1132 ;; and the SIZE argument is not known to be equal.
1133 (if (and (or (eq '* dim)
1134 (and dim (constant-lvar-p size) (eql (lvar-value size) dim)))
1135 ;; not sure what it would mean to make it non-simple
1136 (neq complexp t))
1137 `(make-array size :element-type ',elt-type
1138 ,@(when initial-element
1139 `(:initial-element initial-element)))
1140 ;; no transform, but we can detect some style issues
1141 (progn
1142 (when dim ; was a recognizable vector subtype
1143 (let* ((elt-ctype (specifier-type elt-type))
1144 (saetp (find-saetp-by-ctype elt-ctype)))
1145 (cond ((not initial-element)
1146 (let ((default-initial-element
1147 (sb!vm:saetp-initial-element-default saetp)))
1148 (unless (ctypep default-initial-element elt-ctype)
1149 ;; As with MAKE-ARRAY, this is merely undefined
1150 ;; behavior, not an error.
1151 (compiler-style-warn
1152 "The default initial element ~S is not a ~S."
1153 default-initial-element elt-type))))
1154 ;; In would be possible in some cases,
1155 ;; like :INITIAL-ELEMENT (IF X #\x #\y) in a call
1156 ;; to MAKE-SEQUENCE '(VECTOR (MEMBER #\A #\B))
1157 ;; to detect erroneous non-constants initializers,
1158 ;; but it is not important enough to bother with.
1159 ((and (constant-lvar-p initial-element)
1160 (not (ctypep (lvar-value initial-element)
1161 elt-ctype)))
1162 ;; MAKE-ARRAY considers this a warning, not an error.
1163 (compiler-warn "~S ~S is not a ~S"
1164 :initial-element
1165 (lvar-value initial-element)
1166 elt-type)))))
1167 (give-up-ir1-transform))))))
1169 (deftransform subseq ((seq start &optional end)
1170 (vector t &optional t)
1172 :node node)
1173 (let ((type (lvar-type seq)))
1174 (cond
1175 ((and (inlineable-copy-vector-p type)
1176 (policy node (> speed space)))
1177 (let ((element-type (type-specifier (array-type-specialized-element-type type))))
1178 `(let* ((length (length seq))
1179 (end (or end length)))
1180 ,(unless (policy node (zerop insert-array-bounds-checks))
1181 '(progn
1182 (unless (<= 0 start end length)
1183 (sequence-bounding-indices-bad-error seq start end))))
1184 (let* ((size (- end start))
1185 (result (make-array size :element-type ',element-type)))
1186 ,(maybe-expand-copy-loop-inline 'seq (if (constant-lvar-p start)
1187 (lvar-value start)
1188 'start)
1189 'result 0 'size element-type)
1190 result))))
1192 '(vector-subseq* seq start end)))))
1194 (deftransform subseq ((seq start &optional end)
1195 (list t &optional t))
1196 `(list-subseq* seq start end))
1198 (deftransform subseq ((seq start &optional end)
1199 ((and sequence (not vector) (not list)) t &optional t))
1200 '(sb!sequence:subseq seq start end))
1202 (deftransform copy-seq ((seq) (vector))
1203 (let ((type (lvar-type seq)))
1204 (cond ((inlineable-copy-vector-p type)
1205 (let ((element-type (type-specifier (array-type-specialized-element-type type))))
1206 `(let* ((length (length seq))
1207 (result (make-array length :element-type ',element-type)))
1208 ,(maybe-expand-copy-loop-inline 'seq 0 'result 0 'length element-type)
1209 result)))
1211 '(vector-subseq* seq 0 nil)))))
1213 (deftransform copy-seq ((seq) (list))
1214 '(list-copy-seq* seq))
1216 (deftransform copy-seq ((seq) ((and sequence (not vector) (not list))))
1217 '(sb!sequence:copy-seq seq))
1219 ;;; FIXME: it really should be possible to take advantage of the
1220 ;;; macros used in code/seq.lisp here to avoid duplication of code,
1221 ;;; and enable even funkier transformations.
1222 (deftransform search ((pattern text &key (start1 0) (start2 0) end1 end2
1223 (test #'eql)
1224 (key #'identity)
1225 from-end)
1226 (vector vector &rest t)
1228 :node node
1229 :policy (> speed (max space safety)))
1230 "open code"
1231 (flet ((maybe (x)
1232 (when (lvar-p x)
1233 (if (constant-lvar-p x)
1234 (when (lvar-value x)
1235 :yes)
1236 :maybe))))
1237 (let ((from-end (when (lvar-p from-end)
1238 (unless (constant-lvar-p from-end)
1239 (give-up-ir1-transform ":FROM-END is not constant."))
1240 (lvar-value from-end)))
1241 (key? (maybe key))
1242 (test? (maybe test))
1243 (check-bounds-p (policy node (plusp insert-array-bounds-checks))))
1244 `(block search
1245 (flet ((oops (vector start end)
1246 (sequence-bounding-indices-bad-error vector start end)))
1247 (declare (ignorable #'oops))
1248 (let* ((len1 (length pattern))
1249 (len2 (length text))
1250 (end1 (or end1 len1))
1251 (end2 (or end2 len2))
1252 ,@(case key?
1253 (:yes `((key (%coerce-callable-to-fun key))))
1254 (:maybe `((key (when key
1255 (%coerce-callable-to-fun key))))))
1256 ,@(when test?
1257 `((test (%coerce-callable-to-fun test)))))
1258 (declare (type index start1 start2 end1 end2))
1259 ,@(when check-bounds-p
1260 `((unless (<= start1 end1 len1)
1261 (oops pattern start1 end1))
1262 (unless (<= start2 end2 len2)
1263 (oops pattern start2 end2))))
1264 (when (= end1 start1)
1265 (return-from search (if from-end
1266 end2
1267 start2)))
1268 (do (,(if from-end
1269 '(index2 (- end2 (- end1 start1)) (1- index2))
1270 '(index2 start2 (1+ index2))))
1271 (,(if from-end
1272 '(< index2 start2)
1273 '(>= index2 end2))
1274 nil)
1275 ;; INDEX2 is FIXNUM, not an INDEX, as right before the loop
1276 ;; terminates is hits -1 when :FROM-END is true and :START2
1277 ;; is 0.
1278 (declare (type fixnum index2))
1279 (when (do ((index1 start1 (1+ index1))
1280 (index2 index2 (1+ index2)))
1281 ((>= index1 end1) t)
1282 (declare (type index index1 index2)
1283 (optimize (insert-array-bounds-checks 0)))
1284 ,@(unless from-end
1285 '((when (= index2 end2)
1286 (return-from search nil))))
1287 (unless (,@(if test?
1288 `(funcall test)
1289 `(eql))
1290 ,(case key?
1291 (:yes `(funcall key (aref pattern index1)))
1292 (:maybe `(let ((elt (aref pattern index1)))
1293 (if key
1294 (funcall key elt)
1295 elt)))
1296 (otherwise `(aref pattern index1)))
1297 ,(case key?
1298 (:yes `(funcall key (aref text index2)))
1299 (:maybe `(let ((elt (aref text index2)))
1300 (if key
1301 (funcall key elt)
1302 elt)))
1303 (otherwise `(aref text index2))))
1304 (return nil)))
1305 (return index2)))))))))
1308 ;;; Open-code CONCATENATE for strings. It would be possible to extend
1309 ;;; this transform to non-strings, but I chose to just do the case that
1310 ;;; should cover 95% of CONCATENATE performance complaints for now.
1311 ;;; -- JES, 2007-11-17
1313 ;;; Only handle the simple result type cases. If somebody does (CONCATENATE
1314 ;;; '(STRING 6) ...) their code won't be optimized, but nobody does that in
1315 ;;; practice.
1317 ;;; Limit full open coding based on length of constant sequences. Default
1318 ;;; value is chosen so that other parts of the compiler (constraint propagation
1319 ;;; mainly) won't go nonlinear too badly. It's not an exact number -- but
1320 ;;; in the right ballpark.
1321 (defvar *concatenate-open-code-limit* 129)
1323 (deftransform concatenate ((result-type &rest lvars)
1324 ((constant-arg
1325 (member string simple-string base-string simple-base-string))
1326 &rest sequence)
1327 * :node node)
1328 (let ((vars (make-gensym-list (length lvars)))
1329 (type (lvar-value result-type)))
1330 (if (policy node (<= speed space))
1331 ;; Out-of-line
1332 `(lambda (.dummy. ,@vars)
1333 (declare (ignore .dummy.))
1334 ,(ecase type
1335 ((string simple-string)
1336 `(%concatenate-to-string ,@vars))
1337 ((base-string simple-base-string)
1338 `(%concatenate-to-base-string ,@vars))))
1339 ;; Inline
1340 (let* ((element-type (ecase type
1341 ((string simple-string) 'character)
1342 ((base-string simple-base-string) 'base-char)))
1343 (lvar-values (loop for lvar in lvars
1344 collect (when (constant-lvar-p lvar)
1345 (lvar-value lvar))))
1346 (lengths
1347 (loop for value in lvar-values
1348 for var in vars
1349 collect (if value
1350 (length value)
1351 `(sb!impl::string-dispatch ((simple-array * (*))
1352 sequence)
1353 ,var
1354 (declare (muffle-conditions compiler-note))
1355 (length ,var)))))
1356 (non-constant-start
1357 (loop for value in lvar-values
1358 while (and (stringp value)
1359 (< (length value) *concatenate-open-code-limit*))
1360 sum (length value))))
1361 `(apply
1362 (lambda ,vars
1363 (declare (ignorable ,@vars))
1364 (declare (optimize (insert-array-bounds-checks 0)))
1365 (let* ((.length. (+ ,@lengths))
1366 (.pos. ,non-constant-start)
1367 (.string. (make-string .length. :element-type ',element-type)))
1368 (declare (type index .length. .pos.)
1369 (muffle-conditions compiler-note)
1370 (ignorable .pos.))
1371 ,@(loop with constants = -1
1372 for first = t then nil
1373 for value in lvar-values
1374 for var in vars
1375 collect
1376 (cond ((and (stringp value)
1377 (< (length value) *concatenate-open-code-limit*))
1378 ;; Fold the array reads for constant arguments
1379 `(progn
1380 ,@(loop for c across value
1381 for i from 0
1382 collect
1383 ;; Without truly-the we get massive numbers
1384 ;; of pointless error traps.
1385 `(setf (aref .string.
1386 (truly-the index ,(if constants
1387 (incf constants)
1388 `(+ .pos. ,i))))
1389 ,c))
1390 ,(unless constants
1391 `(incf (truly-the index .pos.) ,(length value)))))
1393 (prog1
1394 `(sb!impl::string-dispatch
1395 (#!+sb-unicode
1396 (simple-array character (*))
1397 (simple-array base-char (*))
1399 ,var
1400 (replace .string. ,var
1401 ,@(cond ((not constants)
1402 '(:start1 .pos.))
1403 ((plusp non-constant-start)
1404 `(:start1 ,non-constant-start))))
1405 (incf (truly-the index .pos.) (length ,var)))
1406 (setf constants nil)))))
1407 .string.))
1408 lvars)))))
1410 ;;;; CONS accessor DERIVE-TYPE optimizers
1412 (defoptimizer (car derive-type) ((cons))
1413 ;; This and CDR needs to use LVAR-CONSERVATIVE-TYPE because type inference
1414 ;; gets confused by things like (SETF CAR).
1415 (let ((type (lvar-conservative-type cons))
1416 (null-type (specifier-type 'null)))
1417 (cond ((eq type null-type)
1418 null-type)
1419 ((cons-type-p type)
1420 (cons-type-car-type type)))))
1422 (defoptimizer (cdr derive-type) ((cons))
1423 (let ((type (lvar-conservative-type cons))
1424 (null-type (specifier-type 'null)))
1425 (cond ((eq type null-type)
1426 null-type)
1427 ((cons-type-p type)
1428 (cons-type-cdr-type type)))))
1430 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
1432 ;;; We want to make sure that %FIND-POSITION is inline-expanded into
1433 ;;; %FIND-POSITION-IF only when %FIND-POSITION-IF has an inline
1434 ;;; expansion, so we factor out the condition into this function.
1435 (defun check-inlineability-of-find-position-if (sequence from-end)
1436 (let ((ctype (lvar-type sequence)))
1437 (cond ((csubtypep ctype (specifier-type 'vector))
1438 ;; It's not worth trying to inline vector code unless we
1439 ;; know a fair amount about it at compile time.
1440 (upgraded-element-type-specifier-or-give-up sequence)
1441 (unless (constant-lvar-p from-end)
1442 (give-up-ir1-transform
1443 "FROM-END argument value not known at compile time")))
1444 ((csubtypep ctype (specifier-type 'list))
1445 ;; Inlining on lists is generally worthwhile.
1448 (give-up-ir1-transform
1449 "sequence type not known at compile time")))))
1451 ;;; %FIND-POSITION-IF and %FIND-POSITION-IF-NOT for LIST data
1452 (defun %find/position-if-list-expansion (sense from-end start end node)
1453 (declare (ignore from-end))
1454 ;; Circularity detection slows things down. It is permissible not to.
1455 ;; In fact, FIND is given as an archetypal example of a function that
1456 ;; "should be prepared to signal an error" but might not [CLHS 1.4.2].
1457 ;; We relax the definition of "safe" from safety=3 to >=2.
1458 (let ((safe (policy node (>= safety 2)))
1459 ;; The secondary value is inconsequential when flowing into a non-MV
1460 ;; combination, so we avoid counting loop iterations if possible.
1461 ;; This is limited in power, but good enough, for want of a proper
1462 ;; dead-code-elimination phase of the compiler.
1463 (indexed
1464 (not (and (lvar-single-value-p (node-lvar node))
1465 (constant-lvar-p start)
1466 (eql (lvar-value start) 0)
1467 (constant-lvar-p end)
1468 (null (lvar-value end))))))
1469 `(let ((find nil)
1470 (position nil))
1471 (flet ((bounds-error ()
1472 (sequence-bounding-indices-bad-error sequence start end)))
1473 (if (and end (> start end))
1474 (bounds-error)
1475 (do ((slow sequence (cdr slow))
1476 ,@(when safe '((fast (cdr sequence) (cddr fast))))
1477 ,@(when indexed '((index 0 (+ index 1)))))
1478 ((cond ((null slow)
1479 (,@(if indexed
1480 '(if (and end (> end index)) (bounds-error))
1481 '(progn))
1482 (return (values find position))))
1483 ,@(when indexed
1484 '(((and end (>= index end))
1485 (return (values find position)))))
1486 ,@(when safe
1487 '(((eq slow fast)
1488 (circular-list-error sequence)))))
1489 (bug "never"))
1490 (declare (list slow ,@(and safe '(fast)))
1491 ;; If you have as many as INDEX conses on a 32-bit build,
1492 ;; then you've either used up 4GB of memory (impossible)
1493 ;; or you're stuck in a circular list in unsafe code.
1494 ;; Correspondingly larger limit for 64-bit.
1495 ,@(and indexed '((index index))))
1496 (,@(if indexed '(when (>= index start)) '(progn))
1497 (let ((element (car slow)))
1498 ;; This hack of dealing with non-NIL FROM-END for list data
1499 ;; by iterating forward through the list and keeping track of
1500 ;; the last time we found a match might be more screwy than
1501 ;; what the user expects, but it seems to be allowed by the
1502 ;; ANSI standard. (And if the user is screwy enough to ask
1503 ;; for FROM-END behavior on list data, turnabout is fair play.)
1505 ;; It's also not enormously efficient, calling PREDICATE
1506 ;; and KEY more often than necessary; but all the alternatives
1507 ;; seem to have their own efficiency problems.
1508 (,sense (funcall predicate (funcall key element))
1509 (if from-end
1510 (setf find element position ,(and indexed 'index))
1511 (return (values element ,(and indexed 'index)))))))))))))
1513 (macrolet ((def (name condition)
1514 `(deftransform ,name ((predicate sequence from-end start end key)
1515 (function list t t t function)
1517 :node node
1518 :policy (> speed space))
1519 "expand inline"
1520 (%find/position-if-list-expansion ',condition
1521 from-end start end node))))
1522 (def %find-position-if when)
1523 (def %find-position-if-not unless))
1525 ;;; %FIND-POSITION for LIST data can be expanded into %FIND-POSITION-IF
1526 ;;; without loss of efficiency. (I.e., the optimizer should be able
1527 ;;; to straighten everything out.)
1528 (deftransform %find-position ((item sequence from-end start end key test)
1529 (t list t t t t t)
1531 :policy (> speed space))
1532 "expand inline"
1533 '(%find-position-if (let ((test-fun (%coerce-callable-to-fun test)))
1534 ;; The order of arguments for asymmetric tests
1535 ;; (e.g. #'<, as opposed to order-independent
1536 ;; tests like #'=) is specified in the spec
1537 ;; section 17.2.1 -- the O/Zi stuff there.
1538 (lambda (i)
1539 (funcall test-fun item i)))
1540 sequence
1541 from-end
1542 start
1544 (%coerce-callable-to-fun key)))
1546 ;;; The inline expansions for the VECTOR case are saved as macros so
1547 ;;; that we can share them between the DEFTRANSFORMs and the default
1548 ;;; cases in the DEFUNs. (This isn't needed for the LIST case, because
1549 ;;; the DEFTRANSFORMs for LIST are less choosy about when to expand.)
1550 (defun %find-position-or-find-position-if-vector-expansion (sequence-arg
1551 from-end
1552 start
1553 end-arg
1554 element
1555 done-p-expr)
1556 (with-unique-names (offset block index n-sequence sequence end)
1557 `(let* ((,n-sequence ,sequence-arg))
1558 (with-array-data ((,sequence ,n-sequence :offset-var ,offset)
1559 (,start ,start)
1560 (,end ,end-arg)
1561 :check-fill-pointer t)
1562 (block ,block
1563 (macrolet ((maybe-return ()
1564 ;; WITH-ARRAY-DATA has already performed bounds
1565 ;; checking, so we can safely elide the checks
1566 ;; in the inner loop.
1567 '(let ((,element (locally (declare (optimize (insert-array-bounds-checks 0)))
1568 (aref ,sequence ,index))))
1569 (when ,done-p-expr
1570 (return-from ,block
1571 (values ,element
1572 (- ,index ,offset)))))))
1573 (if ,from-end
1574 (loop for ,index
1575 ;; (If we aren't fastidious about declaring that
1576 ;; INDEX might be -1, then (FIND 1 #() :FROM-END T)
1577 ;; can send us off into never-never land, since
1578 ;; INDEX is initialized to -1.)
1579 of-type index-or-minus-1
1580 from (1- ,end) downto ,start do
1581 (maybe-return))
1582 (loop for ,index of-type index from ,start below ,end do
1583 (maybe-return))))
1584 (values nil nil))))))
1586 (def!macro %find-position-vector-macro (item sequence
1587 from-end start end key test)
1588 (with-unique-names (element)
1589 (%find-position-or-find-position-if-vector-expansion
1590 sequence
1591 from-end
1592 start
1594 element
1595 ;; (See the LIST transform for a discussion of the correct
1596 ;; argument order, i.e. whether the searched-for ,ITEM goes before
1597 ;; or after the checked sequence element.)
1598 `(funcall ,test ,item (funcall ,key ,element)))))
1600 (def!macro %find-position-if-vector-macro (predicate sequence
1601 from-end start end key)
1602 (with-unique-names (element)
1603 (%find-position-or-find-position-if-vector-expansion
1604 sequence
1605 from-end
1606 start
1608 element
1609 `(funcall ,predicate (funcall ,key ,element)))))
1611 (def!macro %find-position-if-not-vector-macro (predicate sequence
1612 from-end start end key)
1613 (with-unique-names (element)
1614 (%find-position-or-find-position-if-vector-expansion
1615 sequence
1616 from-end
1617 start
1619 element
1620 `(not (funcall ,predicate (funcall ,key ,element))))))
1622 ;;; %FIND-POSITION, %FIND-POSITION-IF and %FIND-POSITION-IF-NOT for
1623 ;;; VECTOR data
1624 (deftransform %find-position-if ((predicate sequence from-end start end key)
1625 (function vector t t t function)
1627 :policy (> speed space))
1628 "expand inline"
1629 (check-inlineability-of-find-position-if sequence from-end)
1630 '(%find-position-if-vector-macro predicate sequence
1631 from-end start end key))
1633 (deftransform %find-position-if-not ((predicate sequence from-end start end key)
1634 (function vector t t t function)
1636 :policy (> speed space))
1637 "expand inline"
1638 (check-inlineability-of-find-position-if sequence from-end)
1639 '(%find-position-if-not-vector-macro predicate sequence
1640 from-end start end key))
1642 (deftransform %find-position ((item sequence from-end start end key test)
1643 (t vector t t t function function)
1645 :policy (> speed space))
1646 "expand inline"
1647 (check-inlineability-of-find-position-if sequence from-end)
1648 '(%find-position-vector-macro item sequence
1649 from-end start end key test))
1651 (deftransform %find-position ((item sequence from-end start end key test)
1652 (t bit-vector t t t t t)
1653 * :node node)
1654 (when (and test (lvar-fun-is test '(eq eql equal)))
1655 (setf test nil))
1656 (when (and key (lvar-fun-is key '(identity)))
1657 (setf key nil))
1658 (when (or test key)
1659 (delay-ir1-transform node :optimize)
1660 (give-up-ir1-transform "non-trivial :KEY or :TEST"))
1661 (catch 'not-a-bit
1662 `(with-array-data ((bits sequence :offset-var offset)
1663 (start start)
1664 (end end)
1665 :check-fill-pointer t)
1666 (let ((p ,(if (constant-lvar-p item)
1667 (case (lvar-value item)
1668 (0 `(%bit-position/0 bits from-end start end))
1669 (1 `(%bit-position/1 bits from-end start end))
1670 (otherwise (throw 'not-a-bit `(values nil nil))))
1671 `(%bit-position item bits from-end start end))))
1672 (if p
1673 (values item (the index (- (truly-the index p) offset)))
1674 (values nil nil))))))
1676 (deftransform %find-position ((item sequence from-end start end key test)
1677 (character string t t t function function)
1679 :policy (> speed space))
1680 (if (eq '* (upgraded-element-type-specifier sequence))
1681 (let ((form
1682 `(sb!impl::string-dispatch ((simple-array character (*))
1683 (simple-array base-char (*))
1684 (simple-array nil (*)))
1685 sequence
1686 (%find-position item sequence from-end start end key test))))
1687 (if (csubtypep (lvar-type sequence) (specifier-type 'simple-string))
1688 form
1689 ;; Otherwise we'd get three instances of WITH-ARRAY-DATA from
1690 ;; %FIND-POSITION.
1691 `(with-array-data ((sequence sequence :offset-var offset)
1692 (start start)
1693 (end end)
1694 :check-fill-pointer t)
1695 (multiple-value-bind (elt index) ,form
1696 (values elt (when (fixnump index) (- index offset)))))))
1697 ;; The type is known exactly, other transforms will take care of it.
1698 (give-up-ir1-transform)))
1700 ;;; logic to unravel :TEST, :TEST-NOT, and :KEY options in FIND,
1701 ;;; POSITION-IF, etc.
1702 (define-source-transform effective-find-position-test (test test-not)
1703 (once-only ((test test)
1704 (test-not test-not))
1705 `(cond
1706 ((and ,test ,test-not)
1707 (error "can't specify both :TEST and :TEST-NOT"))
1708 (,test (%coerce-callable-to-fun ,test))
1709 (,test-not
1710 ;; (Without DYNAMIC-EXTENT, this is potentially horribly
1711 ;; inefficient, but since the TEST-NOT option is deprecated
1712 ;; anyway, we don't care.)
1713 (complement (%coerce-callable-to-fun ,test-not)))
1714 (t #'eql))))
1715 (define-source-transform effective-find-position-key (key)
1716 (once-only ((key key))
1717 `(if ,key
1718 (%coerce-callable-to-fun ,key)
1719 #'identity)))
1721 (macrolet ((define-find-position (fun-name values-index)
1722 `(deftransform ,fun-name ((item sequence &key
1723 from-end (start 0) end
1724 key test test-not)
1725 (t (or list vector) &rest t))
1726 '(nth-value ,values-index
1727 (%find-position item sequence
1728 from-end start
1730 (effective-find-position-key key)
1731 (effective-find-position-test
1732 test test-not))))))
1733 (define-find-position find 0)
1734 (define-find-position position 1))
1736 (macrolet ((define-find-position-if (fun-name values-index)
1737 `(deftransform ,fun-name ((predicate sequence &key
1738 from-end (start 0)
1739 end key)
1740 (t (or list vector) &rest t))
1741 '(nth-value
1742 ,values-index
1743 (%find-position-if (%coerce-callable-to-fun predicate)
1744 sequence from-end
1745 start end
1746 (effective-find-position-key key))))))
1747 (define-find-position-if find-if 0)
1748 (define-find-position-if position-if 1))
1750 ;;; the deprecated functions FIND-IF-NOT and POSITION-IF-NOT. We
1751 ;;; didn't bother to worry about optimizing them, except note that on
1752 ;;; Sat, Oct 06, 2001 at 04:22:38PM +0100, Christophe Rhodes wrote on
1753 ;;; sbcl-devel
1755 ;;; My understanding is that while the :test-not argument is
1756 ;;; deprecated in favour of :test (complement #'foo) because of
1757 ;;; semantic difficulties (what happens if both :test and :test-not
1758 ;;; are supplied, etc) the -if-not variants, while officially
1759 ;;; deprecated, would be undeprecated were X3J13 actually to produce
1760 ;;; a revised standard, as there are perfectly legitimate idiomatic
1761 ;;; reasons for allowing the -if-not versions equal status,
1762 ;;; particularly remove-if-not (== filter).
1764 ;;; This is only an informal understanding, I grant you, but
1765 ;;; perhaps it's worth optimizing the -if-not versions in the same
1766 ;;; way as the others?
1768 ;;; FIXME: Maybe remove uses of these deprecated functions within the
1769 ;;; implementation of SBCL.
1770 (macrolet ((define-find-position-if-not (fun-name values-index)
1771 `(deftransform ,fun-name ((predicate sequence &key
1772 from-end (start 0)
1773 end key)
1774 (t (or list vector) &rest t))
1775 '(nth-value
1776 ,values-index
1777 (%find-position-if-not (%coerce-callable-to-fun predicate)
1778 sequence from-end
1779 start end
1780 (effective-find-position-key key))))))
1781 (define-find-position-if-not find-if-not 0)
1782 (define-find-position-if-not position-if-not 1))
1784 (macrolet ((define-trimmer-transform (fun-name leftp rightp)
1785 `(deftransform ,fun-name ((char-bag string)
1786 (t simple-string))
1787 (let ((find-expr
1788 (if (constant-lvar-p char-bag)
1789 ;; If the bag is constant, use MEMBER
1790 ;; instead of FIND, since we have a
1791 ;; deftransform for MEMBER that can
1792 ;; open-code all of the comparisons when
1793 ;; the list is constant. -- JES, 2007-12-10
1794 `(not (member (schar string index)
1795 ',(coerce (lvar-value char-bag) 'list)
1796 :test #'char=))
1797 '(not (find (schar string index) char-bag :test #'char=)))))
1798 `(flet ((char-not-in-bag (index)
1799 ,find-expr))
1800 (let* ((end (length string))
1801 (left-end (if ,',leftp
1802 (do ((index 0 (1+ index)))
1803 ((or (= index (the fixnum end))
1804 (char-not-in-bag index))
1805 index)
1806 (declare (fixnum index)))
1808 (right-end (if ,',rightp
1809 (do ((index (1- end) (1- index)))
1810 ((or (< index left-end)
1811 (char-not-in-bag index))
1812 (1+ index))
1813 (declare (fixnum index)))
1814 end)))
1815 (if (and (eql left-end 0)
1816 (eql right-end (length string)))
1817 string
1818 (subseq string left-end right-end))))))))
1819 (define-trimmer-transform string-left-trim t nil)
1820 (define-trimmer-transform string-right-trim nil t)
1821 (define-trimmer-transform string-trim t t))
1824 ;;; (partially) constant-fold backq-* functions, or convert to their
1825 ;;; plain CL equivalent (now that they're not needed for pprinting).
1827 ;; Pop constant values from the end, list/list* them if any, and link
1828 ;; the remainder with list* at runtime.
1829 (defun transform-backq-list-or-list* (function values)
1830 (let ((gensyms (make-gensym-list (length values)))
1831 (reverse (reverse values))
1832 (constants '()))
1833 (loop while (and reverse
1834 (constant-lvar-p (car reverse)))
1835 do (push (lvar-value (pop reverse))
1836 constants))
1837 (if (null constants)
1838 `(lambda ,gensyms
1839 (,function ,@gensyms))
1840 (let ((tail (apply function constants)))
1841 (if (null reverse)
1842 `',tail
1843 (let* ((nvariants (length reverse))
1844 (variants (subseq gensyms 0 nvariants)))
1845 `(lambda ,gensyms
1846 (declare (ignore ,@(subseq gensyms nvariants)))
1847 ,(if tail
1848 `(list* ,@variants ',tail)
1849 `(list ,@variants)))))))))
1851 (deftransform sb!impl::|List| ((&rest elts))
1852 (transform-backq-list-or-list* 'list elts))
1854 (deftransform sb!impl::|List*| ((&rest elts))
1855 (transform-backq-list-or-list* 'list* elts))
1857 ;; Merge adjacent constant values
1858 (deftransform sb!impl::|Append| ((&rest elts))
1859 (let ((gensyms (make-gensym-list (length elts)))
1860 (acc nil)
1861 (ignored '())
1862 (arguments '()))
1863 (flet ((convert-accumulator ()
1864 (let ((constant (apply 'append (nreverse (shiftf acc nil)))))
1865 (when constant
1866 (push `',constant arguments)))))
1867 (loop for gensym in gensyms
1868 for (elt . next) on elts by #'cdr
1869 do (cond ((constant-lvar-p elt)
1870 (let ((elt (lvar-value elt)))
1871 (when (and next (not (proper-list-p elt)))
1872 (abort-ir1-transform
1873 "Non-list or improper list spliced in ~
1874 the middle of a backquoted list."))
1875 (push gensym ignored)
1876 (push elt acc)))
1878 (convert-accumulator)
1879 (push gensym arguments)))
1880 finally (convert-accumulator)))
1881 (let ((arguments (nreverse arguments)))
1882 `(lambda ,gensyms
1883 (declare (ignore ,@ignored))
1884 (append ,@arguments)))))