1 ;;;; optimizers for list and sequence functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
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)
59 (let ((n-first (gensym)))
60 (dolist (a (if accumulate
62 `(,n-first
,@(rest arglists
))))
64 (do-clauses `(,v
,a
(cdr ,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))))
74 (map-result (gensym)))
76 ;; MUFFLE- is not injected when cross-compiling.
77 ;; See top of file for explanation.
80 (declare (muffle-conditions compiler-note
))
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
)))))))
88 (map-result (gensym)))
90 ;; MUFFLE- is not injected when cross-compiling.
91 ;; See top of file for explanation.
94 (declare (muffle-conditions compiler-note
))
96 (declare (truly-dynamic-extent ,map-result
))
97 (do-anonymous ((,temp
,map-result
) .
,(do-clauses))
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
108 `(let ((,n-first
,(first arglists
)))
109 (do-anonymous ,(do-clauses)
110 (,endtest
(truly-the list
,n-first
))
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
)
145 (let ((result-type-arg-value
146 (lvar-value result-type-arg
)))
147 (if (null result-type-arg-value
)
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.
157 ((not constant-result-type-arg-p
)
158 `(sequence-of-checked-length-given-type ,bare
161 (let ((result-ctype (ir1-transform-specifier-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
)))
170 `(vector-of-checked-length-given-length ,bare
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?
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
)
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)))
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
)
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"))))
232 (process-vector `(array-dimension ,into
0))))
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
))
243 (reduce (lambda (wrap body
) (append wrap
(list 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
))
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
258 (multiple-value-bind (subtype-p valid-p
) (sb!xc
:subtypep x y
)
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
)
267 ((1subtypep result-type-value
'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
))))
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
)
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
))
306 (declare (type list acc
))
307 (declare (ignorable acc
))
308 ,(build-sequence-iterator
312 :fast
(policy node
(> speed space
))))))
313 (if (and (null result-type-value
) (null seqs
))
314 '(%map-for-effect-arity-1 fun seq
)
318 (deftransform map-into
((result fun
&rest seqs
)
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
)
336 (declare (ignore ,end
))
337 ,(build-sequence-iterator
339 :result
'(when (array-has-fill-pointer-p result
)
340 (setf (fill-pointer result
) index
))
342 :body
`(locally (declare (optimize (insert-array-bounds-checks 0)))
343 (setf (aref ,data
(truly-the index
(+ index
,start
)))
346 (build-sequence-iterator
348 :result
'(when (array-has-fill-pointer-p result
)
349 (setf (fill-pointer result
) index
))
351 :body
'(locally (declare (optimize (insert-array-bounds-checks 0)))
352 (setf (aref result index
) funcall-result
))))
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
)
362 (let ((saetp (find-saetp-by-ctype (array-type-specialized-element-type result-type
))))
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
)
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 * (*)) *) *)
379 (deftransform elt
((s i
) (list *) * :policy
(< safety
3))
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
)
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
)
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.
412 (write-string (symbol-name function-name
) s
)
413 (dolist (f key-functions
)
415 (write-string (symbol-name f
) 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)))
431 ;; Ditto for KEY IDENTITY.
432 (when (and key
(lvar-fun-is key
'(identity)))
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
)
439 (let ((key-type (lvar-type key
))
440 (null-type (specifier-type 'null
)))
441 (cond ((csubtypep key-type null-type
)
443 ((csubtypep null-type key-type
)
445 (%coerce-callable-to-fun key
)
448 (values key
(ensure-lvar-fun-form key
'key
))))))
449 (let* ((c-test (cond ((and test
(lvar-fun-is test
'(eq)))
452 ((and (not test
) (not test-not
))
453 (when (eq-comparable-type-p (lvar-type item
))
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)
465 `(if (let ((this ',(car tail
)))
468 (let ((cxx (if (eq name
'assoc
) 'car
'cdr
)))
469 `(and this
(let ((target (,cxx this
)))
472 `(let ((target this
))
475 ((assoc rassoc
) (car tail
))
477 ,(open-code (cdr tail
)))))
479 (if (eq 'key
(second args
))
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
))
491 (if (eq name
'adjoin
)
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)))
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
)
508 (let ((key-type (lvar-type key
))
509 (null-type (specifier-type 'null
)))
510 (cond ((csubtypep key-type null-type
)
512 ((csubtypep null-type key-type
)
514 (%coerce-callable-to-fun key
)
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)
524 `(if (let ((this ',(car tail
)))
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
)))
530 ((member-if member-if-not
)
531 `(let ((target this
))
534 ((assoc-if assoc-if-not rassoc-if rassoc-if-not
)
536 ((member-if member-if-not
)
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!
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")))
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
))
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
)))))))))
578 (deftransform memq
((item list
) (t (constant-arg list
)))
581 `(if (eq item
',(car tail
))
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
) *)
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)))
600 (deftransform delete-if
((pred list
) (t list
))
602 '(do ((x list
(cdr x
))
605 (cond ((funcall pred
(car 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 (deftransform fill
((seq item
&key
(start 0) (end nil
))
616 (vector t
&key
(:start t
) (:end t
))
619 (let* ((type (lvar-type seq
))
620 (element-ctype (array-type-upgraded-element-type type
))
621 (element-type (type-specifier element-ctype
))
622 (saetp (unless (eq *wild-type
* element-ctype
)
623 (find-saetp-by-ctype element-ctype
))))
624 (cond ((eq *wild-type
* element-ctype
)
625 (delay-ir1-transform node
:constraint
)
626 `(vector-fill* seq item start end
))
627 ((and saetp
(sb!vm
::valid-bit-bash-saetp-p saetp
))
628 (let* ((n-bits (sb!vm
:saetp-n-bits saetp
))
629 (basher-name (format nil
"UB~D-BASH-FILL" n-bits
))
630 (basher (or (find-symbol basher-name
632 (find-package "SB!KERNEL") t
))
634 "Unknown fill basher, please report to sbcl-devel: ~A"
636 (kind (cond ((sb!vm
:saetp-fixnum-p saetp
) :tagged
)
637 ((member element-type
'(character base-char
)) :char
)
638 ((eq element-type
'single-float
) :single-float
)
640 ((eq element-type
'double-float
) :double-float
)
642 ((equal element-type
'(complex single-float
))
643 :complex-single-float
)
645 (aver (integer-type-p element-ctype
))
647 ;; BASH-VALUE is a word that we can repeatedly smash
648 ;; on the array: for less-than-word sized elements it
649 ;; contains multiple copies of the fill item.
651 (if (constant-lvar-p item
)
652 (let ((tmp (lvar-value item
)))
653 (unless (ctypep tmp element-ctype
)
654 (abort-ir1-transform "~S is not ~S" tmp element-type
))
659 (ash tmp sb
!vm
:n-fixnum-tag-bits
))
665 (single-float-bits tmp
))
668 (logior (ash (double-float-high-bits tmp
) 32)
669 (double-float-low-bits tmp
)))
671 (:complex-single-float
672 (logior (ash (single-float-bits (imagpart tmp
)) 32)
674 (single-float-bits (realpart tmp
))))))))
676 (loop for i of-type sb
!vm
:word from n-bits by n-bits
677 until
(= i sb
!vm
:n-word-bits
)
678 do
(setf res
(ldb (byte sb
!vm
:n-word-bits
0)
679 (logior res
(ash bits i
)))))
682 (delay-ir1-transform node
:constraint
)
683 `(let* ((bits (ldb (byte ,n-bits
0)
686 `(ash item
,sb
!vm
:n-fixnum-tag-bits
))
692 `(single-float-bits item
))
695 `(logior (ash (double-float-high-bits item
) 32)
696 (double-float-low-bits item
)))
698 (:complex-single-float
699 `(logior (ash (single-float-bits (imagpart item
)) 32)
701 (single-float-bits (realpart item
))))))))
703 (declare (type sb
!vm
:word res
))
704 ,@(unless (= sb
!vm
:n-word-bits n-bits
)
705 `((loop for i of-type sb
!vm
:word from
,n-bits by
,n-bits
706 until
(= i sb
!vm
:n-word-bits
)
708 (ldb (byte ,sb
!vm
:n-word-bits
0)
709 (logior res
(ash bits
(truly-the (integer 0 ,(- sb
!vm
:n-word-bits n-bits
)) i
))))))))
712 ;; KLUDGE: WITH-ARRAY data in its full glory is going to mess up
713 ;; dynamic-extent for MAKE-ARRAY :INITIAL-ELEMENT initialization.
714 (if (csubtypep (lvar-type seq
) (specifier-type '(simple-array * (*))))
715 `(let* ((len (length seq
))
718 ;; Minor abuse CHECK-BOUND for bounds checking.
719 ;; (- END START) may still end up negative, but
720 ;; the basher handle that.
721 (,basher
,bash-value seq
722 (check-bound seq bound start
)
723 (- (if end
(check-bound seq bound end
) len
)
725 `(with-array-data ((data seq
)
728 :check-fill-pointer t
)
729 (declare (type (simple-array ,element-type
1) data
))
730 (declare (type index start end
))
731 (declare (optimize (safety 0) (speed 3)))
732 (,basher
,bash-value data start
(- end start
))
734 `((declare (type ,element-type item
))))))
735 ((policy node
(> speed space
))
737 `(with-array-data ((data seq
)
740 :check-fill-pointer t
)
741 (declare (type (simple-array ,element-type
1) data
))
742 (declare (type index start end
))
743 ;; WITH-ARRAY-DATA did our range checks once and for all, so
744 ;; it'd be wasteful to check again on every AREF...
745 (declare (optimize (safety 0) (speed 3)))
746 (do ((i start
(1+ i
)))
748 (declare (type index i
))
749 (setf (aref data i
) item
)))
750 ;; ... though we still need to check that the new element can fit
751 ;; into the vector in safe code. -- CSR, 2002-07-05
752 `((declare (type ,element-type item
)))))
753 ((csubtypep type
(specifier-type 'string
))
754 '(string-fill* seq item start end
))
756 '(vector-fill* seq item start end
)))))
758 (deftransform fill
((seq item
&key
(start 0) (end nil
))
759 ((and sequence
(not vector
) (not list
)) t
&key
(:start t
) (:end t
)))
760 `(sb!sequence
:fill seq item
762 :end
(%check-generic-sequence-bounds seq start end
)))
764 ;;;; hairy sequence transforms
766 ;;; FIXME: no hairy sequence transforms in SBCL?
768 ;;; There used to be a bunch of commented out code about here,
769 ;;; containing the (apparent) beginning of hairy sequence transform
770 ;;; infrastructure. People interested in implementing better sequence
771 ;;; transforms might want to look at it for inspiration, even though
772 ;;; the actual code is ancient CMUCL -- and hence bitrotted. The code
773 ;;; was deleted in 1.0.7.23.
775 ;;;; string operations
777 ;;; We transform the case-sensitive string predicates into a non-keyword
778 ;;; version. This is an IR1 transform so that we don't have to worry about
779 ;;; changing the order of evaluation.
780 (macrolet ((def (fun pred
*)
781 `(deftransform ,fun
((string1 string2
&key
(start1 0) end1
784 `(,',pred
* string1 string2 start1 end1 start2 end2
))))
785 (def string
< string
<*)
786 (def string
> string
>*)
787 (def string
<= string
<=*)
788 (def string
>= string
>=*)
789 (def string
= string
=*)
790 (def string
/= string
/=*))
792 ;;; Return a form that tests the free variables STRING1 and STRING2
793 ;;; for the ordering relationship specified by LESSP and EQUALP. The
794 ;;; start and end are also gotten from the environment. Both strings
795 ;;; must be SIMPLE-BASE-STRINGs.
796 (macrolet ((def (name lessp equalp
)
797 `(deftransform ,name
((string1 string2 start1 end1 start2 end2
)
798 (simple-base-string simple-base-string t t t t
) *)
799 `(let* ((end1 (if (not end1
) (length string1
) end1
))
800 (end2 (if (not end2
) (length string2
) end2
))
801 (index (sb!impl
::%sp-string-compare
802 string1 start1 end1 string2 start2 end2
)))
804 (cond ((= index end1
)
805 ,(if ',lessp
'index nil
))
806 ((= (+ index
(- start2 start1
)) end2
)
807 ,(if ',lessp nil
'index
))
808 ((,(if ',lessp
'char
< 'char
>)
809 (schar string1 index
)
818 ,(if ',equalp
'end1 nil
))))))
821 (def string
>* nil nil
)
822 (def string
>=* nil t
))
824 (deftransform string
=* ((string1 string2 start1 end1 start2 end2
)
826 (constant-arg (eql 0))
828 (constant-arg (eql 0))
829 (constant-arg null
)))
830 (cond ((and (constant-lvar-p string1
)
831 (equal (lvar-value string1
) ""))
832 `(zerop (length string2
)))
833 ((and (constant-lvar-p string2
)
834 (equal (lvar-value string2
) ""))
835 `(zerop (length string1
)))
837 (give-up-ir1-transform))))
839 (deftransform string
/=* ((string1 string2 start1 end1 start2 end2
)
841 (constant-arg (eql 0))
843 (constant-arg (eql 0))
844 (constant-arg null
)))
845 (cond ((and (constant-lvar-p string1
)
846 (equal (lvar-value string1
) ""))
848 `(and (plusp (length string2
))
850 ((and (constant-lvar-p string2
)
851 (equal (lvar-value string2
) ""))
852 `(and (plusp (length string1
))
855 (give-up-ir1-transform))))
857 (macrolet ((def (name result-fun
)
858 `(deftransform ,name
((string1 string2 start1 end1 start2 end2
)
859 (simple-base-string simple-base-string t t t t
) *)
861 (sb!impl
::%sp-string-compare
862 string1 start1
(or end1
(length string1
))
863 string2 start2
(or end2
(length string2
)))))))
864 (def string
=* not
) ; FIXME: this xform looks counterproductive.
865 (def string
/=* identity
))
867 (deftransform string
/=* ((str1 str2 start1 end1 start2 end2
) * * :node node
869 ;; An IF node doesn't care about the mismatch index.
870 ;; Transforming to (not (string= ..)) would lead to internal confusion
871 ;; due to incorrect typing: STRING/= can't return T, so return 0 for true.
872 (if (if-p (node-dest node
))
873 `(if (string=* str1 str2 start1 end1 start2 end2
) nil
0)
874 (give-up-ir1-transform)))
876 (deftransform string
((x) (symbol)) '(symbol-name x
))
878 ;;;; transforms for sequence functions
880 ;;; Moved here from generic/vm-tran.lisp to satisfy clisp. Only applies
881 ;;; to vectors based on simple arrays.
882 (def!constant vector-data-bit-offset
883 (* sb
!vm
:vector-data-offset sb
!vm
:n-word-bits
))
885 ;;; FIXME: In the copy loops below, we code the loops in a strange
888 ;;; (do ((i (+ src-offset length) (1- i)))
890 ;;; (... (aref foo (1- i)) ...))
892 ;;; rather than the more natural (and seemingly more efficient):
894 ;;; (do ((i (1- (+ src-offset length)) (1- i)))
896 ;;; (... (aref foo i) ...))
898 ;;; (more efficient because we don't have to do the index adjusting on
899 ;;; every iteration of the loop)
901 ;;; We do this to avoid a suboptimality in SBCL's backend. In the
902 ;;; latter case, the backend thinks I is a FIXNUM (which it is), but
903 ;;; when used as an array index, the backend thinks I is a
904 ;;; POSITIVE-FIXNUM (which it is). However, since the backend thinks of
905 ;;; these as distinct storage classes, it cannot coerce a move from a
906 ;;; FIXNUM TN to a POSITIVE-FIXNUM TN. The practical effect of this
907 ;;; deficiency is that we have two extra moves and increased register
908 ;;; pressure, which can lead to some spectacularly bad register
909 ;;; allocation. (sub-FIXME: the register allocation even with the
910 ;;; strangely written loops is not always excellent, either...). Doing
911 ;;; it the first way, above, means that I is always thought of as a
912 ;;; POSITIVE-FIXNUM and there are no issues.
914 ;;; Besides, the *-WITH-OFFSET machinery will fold those index
915 ;;; adjustments in the first version into the array addressing at no
916 ;;; performance penalty!
918 ;;; This transform is critical to the performance of string streams. If
919 ;;; you tweak it, make sure that you compare the disassembly, if not the
920 ;;; performance of, the functions implementing string streams
921 ;;; (e.g. SB!IMPL::STRING-OUCH).
922 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
923 (defun !make-replace-transform
(saetp sequence-type1 sequence-type2
)
924 `(deftransform replace
((seq1 seq2
&key
(start1 0) (start2 0) end1 end2
)
925 (,sequence-type1
,sequence-type2
&rest t
)
928 `(let* ((len1 (length seq1
))
930 (end1 (or end1 len1
))
931 (end2 (or end2 len2
))
932 (replace-len (min (- end1 start1
) (- end2 start2
))))
933 ,(unless (policy node
(= insert-array-bounds-checks
0))
935 (unless (<= 0 start1 end1 len1
)
936 (sequence-bounding-indices-bad-error seq1 start1 end1
))
937 (unless (<= 0 start2 end2 len2
)
938 (sequence-bounding-indices-bad-error seq2 start2 end2
))))
940 ((and saetp
(sb!vm
:valid-bit-bash-saetp-p saetp
))
941 (let* ((n-element-bits (sb!vm
:saetp-n-bits saetp
))
942 (bash-function (intern (format nil
"UB~D-BASH-COPY"
944 (find-package "SB!KERNEL"))))
945 `(funcall (function ,bash-function
) seq2 start2
946 seq1 start1 replace-len
)))
949 ;; If the sequence types are different, SEQ1 and
950 ;; SEQ2 must be distinct arrays.
951 ,(eql sequence-type1 sequence-type2
)
952 (eq seq1 seq2
) (> start1 start2
))
953 (do ((i (truly-the index
(+ start1 replace-len -
1))
955 (j (truly-the index
(+ start2 replace-len -
1))
958 (declare (optimize (insert-array-bounds-checks 0)))
959 (setf (aref seq1 i
) (aref seq2 j
)))
960 (do ((i start1
(1+ i
))
962 (end (+ start1 replace-len
)))
964 (declare (optimize (insert-array-bounds-checks 0)))
965 (setf (aref seq1 i
) (aref seq2 j
))))))
969 ((define-replace-transforms ()
970 (loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
971 for sequence-type
= `(simple-array ,(sb!vm
:saetp-specifier saetp
) (*))
972 unless
(= (sb!vm
:saetp-typecode saetp
) sb
!vm
::simple-array-nil-widetag
)
973 collect
(!make-replace-transform saetp sequence-type sequence-type
)
975 finally
(return `(progn ,@forms
))))
976 (define-one-transform (sequence-type1 sequence-type2
)
977 (!make-replace-transform nil sequence-type1 sequence-type2
)))
978 (define-replace-transforms)
981 (define-one-transform (simple-array base-char
(*)) (simple-array character
(*)))
982 (define-one-transform (simple-array character
(*)) (simple-array base-char
(*)))))
984 ;;; Expand simple cases of UB<SIZE>-BASH-COPY inline. "simple" is
985 ;;; defined as those cases where we are doing word-aligned copies from
986 ;;; both the source and the destination and we are copying from the same
987 ;;; offset from both the source and the destination. (The last
988 ;;; condition is there so we can determine the direction to copy at
989 ;;; compile time rather than runtime. Remember that UB<SIZE>-BASH-COPY
990 ;;; acts like memmove, not memcpy.) These conditions may seem rather
991 ;;; restrictive, but they do catch common cases, like allocating a (* 2
992 ;;; N)-size buffer and blitting in the old N-size buffer in.
994 (defun frob-bash-transform (src src-offset
996 length n-elems-per-word
)
997 (declare (ignore src dst length
))
998 (let ((n-bits-per-elem (truncate sb
!vm
:n-word-bits n-elems-per-word
)))
999 (multiple-value-bind (src-word src-elt
)
1000 (truncate (lvar-value src-offset
) n-elems-per-word
)
1001 (multiple-value-bind (dst-word dst-elt
)
1002 (truncate (lvar-value dst-offset
) n-elems-per-word
)
1003 ;; Avoid non-word aligned copies.
1004 (unless (and (zerop src-elt
) (zerop dst-elt
))
1005 (give-up-ir1-transform))
1006 ;; Avoid copies where we would have to insert code for
1007 ;; determining the direction of copying.
1008 (unless (= src-word dst-word
)
1009 (give-up-ir1-transform))
1010 ;; FIXME: The cross-compiler doesn't optimize TRUNCATE properly,
1011 ;; so we have to do its work here.
1012 `(let ((end (+ ,src-word
,(if (= n-elems-per-word
1)
1014 `(truncate (the index length
) ,n-elems-per-word
)))))
1015 (declare (type index end
))
1016 ;; Handle any bits at the end.
1017 (when (logtest length
(1- ,n-elems-per-word
))
1018 (let* ((extra (mod length
,n-elems-per-word
))
1019 ;; FIXME: The shift amount on this ASH is
1020 ;; *always* negative, but the backend doesn't
1021 ;; have a NEGATIVE-FIXNUM primitive type, so we
1022 ;; wind up with a pile of code that tests the
1023 ;; sign of the shift count prior to shifting when
1024 ;; all we need is a simple negate and shift
1026 (mask (ash #.
(1- (ash 1 sb
!vm
:n-word-bits
))
1027 (* (- extra
,n-elems-per-word
)
1028 ,n-bits-per-elem
))))
1029 (setf (%vector-raw-bits dst end
)
1031 (logandc2 (%vector-raw-bits dst end
)
1033 ,(ecase *backend-byte-order
*
1035 (:big-endian
`(* (- ,n-elems-per-word extra
)
1036 ,n-bits-per-elem
)))))
1037 (logand (%vector-raw-bits src end
)
1039 ,(ecase *backend-byte-order
*
1041 (:big-endian
`(* (- ,n-elems-per-word extra
)
1042 ,n-bits-per-elem
)))))))))
1043 ;; Copy from the end to save a register.
1044 (do ((i end
(1- i
)))
1046 (setf (%vector-raw-bits dst
(1- i
))
1047 (%vector-raw-bits src
(1- i
))))
1051 (let ((arglist '((src src-offset dst dst-offset length
)
1052 ((simple-unboxed-array (*)) (constant-arg index
)
1053 (simple-unboxed-array (*)) (constant-arg index
)
1056 (loop for i
= 1 then
(* i
2)
1057 for name
= (intern (format nil
"UB~D-BASH-COPY" i
) "SB!KERNEL")
1058 collect
`(deftransform ,name
,arglist
1059 (frob-bash-transform src src-offset
1060 dst dst-offset length
1061 ,(truncate sb
!vm
:n-word-bits i
))) into forms
1062 until
(= i sb
!vm
:n-word-bits
)
1063 finally
(return `(progn ,@forms
))))
1065 ;;; We expand copy loops inline in SUBSEQ and COPY-SEQ if we're copying
1066 ;;; arrays with elements of size >= the word size. We do this because
1067 ;;; we know the arrays cannot alias (one was just consed), therefore we
1068 ;;; can determine at compile time the direction to copy, and for
1069 ;;; word-sized elements, UB<WORD-SIZE>-BASH-COPY will do a bit of
1070 ;;; needless checking to figure out what's going on. The same
1071 ;;; considerations apply if we are copying elements larger than the word
1072 ;;; size, with the additional twist that doing it inline is likely to
1073 ;;; cons far less than calling REPLACE and letting generic code do the
1076 ;;; However, we do not do this for elements whose size is < than the
1077 ;;; word size because we don't want to deal with any alignment issues
1078 ;;; inline. The UB*-BASH-COPY transforms might fix things up later
1081 (defun inlineable-copy-vector-p (type)
1082 (and (array-type-p type
)
1083 ;; The two transforms that use this test already specify that their
1084 ;; sequence argument is a VECTOR,
1085 ;; so this seems like it would be more efficient as
1086 ;; and (not (array-type-complexp type))
1087 ;; (not (eq (array-type-element-type type) *wild-type*))
1088 ;; Anyway it no longer works to write this as a single specifier
1089 ;; '(or (simple-unboxed-array (*)) simple-vector) because that
1090 ;; type is just (simple-array * (*)) which isn't amenable to
1091 ;; inline copying since we don't know what it holds.
1092 (or (csubtypep type
(specifier-type '(simple-unboxed-array (*))))
1093 (csubtypep type
(specifier-type 'simple-vector
)))))
1095 (defun maybe-expand-copy-loop-inline (src src-offset dst dst-offset length
1097 (let ((saetp (find-saetp element-type
)))
1099 (if (>= (sb!vm
:saetp-n-bits saetp
) sb
!vm
:n-word-bits
)
1100 (expand-aref-copy-loop src src-offset dst dst-offset length
)
1101 `(locally (declare (optimize (safety 0)))
1102 (replace ,dst
,src
:start1
,dst-offset
:start2
,src-offset
:end1
,length
)))))
1104 (defun expand-aref-copy-loop (src src-offset dst dst-offset length
)
1105 (if (eql src-offset dst-offset
)
1106 `(do ((i (+ ,src-offset
,length
) (1- i
)))
1107 ((<= i
,src-offset
))
1108 (declare (optimize (insert-array-bounds-checks 0)))
1109 (setf (aref ,dst
(1- i
)) (aref ,src
(1- i
))))
1110 ;; KLUDGE: The compiler is not able to derive that (+ offset
1111 ;; length) must be a fixnum, but arrives at (unsigned-byte 29).
1112 ;; We, however, know it must be so, as by this point the bounds
1113 ;; have already been checked.
1114 `(do ((i (truly-the fixnum
(+ ,src-offset
,length
)) (1- i
))
1115 (j (+ ,dst-offset
,length
) (1- j
)))
1116 ((<= i
,src-offset
))
1117 (declare (optimize (insert-array-bounds-checks 0))
1118 (type (integer 0 #.sb
!xc
:array-dimension-limit
) j i
))
1119 (setf (aref ,dst
(1- j
)) (aref ,src
(1- i
))))))
1121 ;;; MAKE-SEQUENCE, SUBSEQ, COPY-SEQ
1123 (deftransform make-sequence
((result-type size
&key initial-element
) * *)
1124 (multiple-value-bind (spec type
)
1125 (and (constant-lvar-p result-type
)
1126 (let ((spec (lvar-value result-type
)))
1127 (values spec
(ir1-transform-specifier-type spec
))))
1129 (give-up-ir1-transform))
1130 (if (type= type
(specifier-type 'list
))
1131 `(%make-list size initial-element
)
1132 (multiple-value-bind (elt-type dim complexp
)
1133 (cond ((and (union-type-p type
)
1134 (csubtypep type
(specifier-type 'string
)))
1135 (let* ((types (union-type-types type
))
1136 (first (first types
)))
1137 (when (array-type-p first
)
1138 (let ((dim (first (array-type-dimensions first
)))
1139 (complexp (array-type-complexp first
)))
1140 ;; Require sameness of dim and complexp. Give up on
1141 ;; (OR (VECTOR CHARACTER) (VECTOR BASE-CHAR 2))
1142 ;; which eventually fails in the call to the function.
1143 (when (every (lambda (x)
1144 (and (array-type-p x
)
1145 (eql (first (array-type-dimensions x
))
1147 (eq (array-type-complexp x
) complexp
)))
1152 (type-specifier (array-type-element-type x
)))
1155 ((and (array-type-p type
)
1156 (csubtypep type
(specifier-type 'vector
)))
1157 (when (contains-unknown-type-p (array-type-element-type type
))
1158 (give-up-ir1-transform "~S is an unknown vector type" spec
))
1159 (values (let ((et (array-type-element-type type
)))
1160 ;; VECTOR means (VECTOR T)
1161 (if (type= et
*wild-type
*)
1163 (type-specifier et
)))
1164 (first (array-type-dimensions type
))
1165 (array-type-complexp type
))))
1166 ;; Don't transform if size is present in the specifier
1167 ;; and the SIZE argument is not known to be equal.
1168 (cond ((and (or (eq '* dim
)
1169 (and dim
(constant-lvar-p size
) (eql (lvar-value size
) dim
)))
1170 ;; not sure what it would mean to make it non-simple
1172 `(make-array size
:element-type
',elt-type
1173 ,@(when initial-element
1174 `(:initial-element initial-element
))))
1175 ;; no transform, but we can detect some style issues
1177 (when dim
; was a recognizable vector subtype
1178 (let* ((elt-ctype (specifier-type elt-type
))
1179 (saetp (find-saetp-by-ctype elt-ctype
)))
1180 (cond ((not initial-element
)
1181 (let ((default-initial-element
1182 (sb!vm
:saetp-initial-element-default saetp
)))
1183 (unless (ctypep default-initial-element elt-ctype
)
1184 ;; As with MAKE-ARRAY, this is merely undefined
1185 ;; behavior, not an error.
1186 (compiler-style-warn
1187 "The default initial element ~S is not a ~S."
1188 default-initial-element elt-type
))))
1189 ;; In would be possible in some cases,
1190 ;; like :INITIAL-ELEMENT (IF X #\x #\y) in a call
1191 ;; to MAKE-SEQUENCE '(VECTOR (MEMBER #\A #\B))
1192 ;; to detect erroneous non-constants initializers,
1193 ;; but it is not important enough to bother with.
1194 ((and (constant-lvar-p initial-element
)
1195 (not (ctypep (lvar-value initial-element
)
1197 ;; MAKE-ARRAY considers this a warning, not an error.
1198 (compiler-warn "~S ~S is not a ~S"
1200 (lvar-value initial-element
)
1202 (give-up-ir1-transform)))))))
1204 (deftransform subseq
((seq start
&optional end
)
1205 (vector t
&optional t
)
1208 (let ((type (lvar-type seq
)))
1210 ((and (inlineable-copy-vector-p type
)
1211 (policy node
(> speed space
)))
1212 (let ((element-type (type-specifier (array-type-specialized-element-type type
))))
1213 `(let* ((length (length seq
))
1214 (end (or end length
)))
1215 ,(unless (policy node
(zerop insert-array-bounds-checks
))
1217 (unless (<= 0 start end length
)
1218 (sequence-bounding-indices-bad-error seq start end
))))
1219 (let* ((size (- end start
))
1220 (result (make-array size
:element-type
',element-type
)))
1221 ,(maybe-expand-copy-loop-inline 'seq
(if (constant-lvar-p start
)
1224 'result
0 'size element-type
)
1227 '(vector-subseq* seq start end
)))))
1229 (deftransform subseq
((seq start
&optional end
)
1230 (list t
&optional t
))
1231 `(list-subseq* seq start end
))
1233 (deftransform subseq
((seq start
&optional end
)
1234 ((and sequence
(not vector
) (not list
)) t
&optional t
))
1235 '(sb!sequence
:subseq seq start end
))
1237 (deftransform copy-seq
((seq) (vector))
1238 (let ((type (lvar-type seq
)))
1239 (cond ((inlineable-copy-vector-p type
)
1240 (let ((element-type (type-specifier (array-type-specialized-element-type type
))))
1241 `(let* ((length (length seq
))
1242 (result (make-array length
:element-type
',element-type
)))
1243 ,(maybe-expand-copy-loop-inline 'seq
0 'result
0 'length element-type
)
1246 '(vector-subseq* seq
0 nil
)))))
1248 (deftransform copy-seq
((seq) (list))
1249 '(list-copy-seq* seq
))
1251 (deftransform copy-seq
((seq) ((and sequence
(not vector
) (not list
))))
1252 '(sb!sequence
:copy-seq seq
))
1254 ;;; FIXME: it really should be possible to take advantage of the
1255 ;;; macros used in code/seq.lisp here to avoid duplication of code,
1256 ;;; and enable even funkier transformations.
1257 (deftransform search
((pattern text
&key
(start1 0) (start2 0) end1 end2
1261 (vector vector
&rest t
)
1264 :policy
(> speed
(max space safety
)))
1268 (if (constant-lvar-p x
)
1269 (when (lvar-value x
)
1272 (let ((from-end (when (lvar-p from-end
)
1273 (unless (constant-lvar-p from-end
)
1274 (give-up-ir1-transform ":FROM-END is not constant."))
1275 (lvar-value from-end
)))
1277 (test?
(maybe test
))
1278 (check-bounds-p (policy node
(plusp insert-array-bounds-checks
))))
1280 (flet ((oops (vector start end
)
1281 (sequence-bounding-indices-bad-error vector start end
)))
1282 (declare (ignorable #'oops
))
1283 (let* ((len1 (length pattern
))
1284 (len2 (length text
))
1285 (end1 (or end1 len1
))
1286 (end2 (or end2 len2
))
1288 (:yes
`((key (%coerce-callable-to-fun key
))))
1289 (:maybe
`((key (when key
1290 (%coerce-callable-to-fun key
))))))
1292 `((test (%coerce-callable-to-fun test
)))))
1293 (declare (type index start1 start2 end1 end2
))
1294 ,@(when check-bounds-p
1295 `((unless (<= start1 end1 len1
)
1296 (oops pattern start1 end1
))
1297 (unless (<= start2 end2 len2
)
1298 (oops pattern start2 end2
))))
1299 (when (= end1 start1
)
1300 (return-from search
(if from-end
1304 '(index2 (- end2
(- end1 start1
)) (1- index2
))
1305 '(index2 start2
(1+ index2
))))
1310 ;; INDEX2 is FIXNUM, not an INDEX, as right before the loop
1311 ;; terminates is hits -1 when :FROM-END is true and :START2
1313 (declare (type fixnum index2
))
1314 (when (do ((index1 start1
(1+ index1
))
1315 (index2 index2
(1+ index2
)))
1316 ((>= index1 end1
) t
)
1317 (declare (type index index1 index2
)
1318 (optimize (insert-array-bounds-checks 0)))
1320 '((when (= index2 end2
)
1321 (return-from search nil
))))
1322 (unless (,@(if test?
1326 (:yes
`(funcall key
(aref pattern index1
)))
1327 (:maybe
`(let ((elt (aref pattern index1
)))
1331 (otherwise `(aref pattern index1
)))
1333 (:yes
`(funcall key
(aref text index2
)))
1334 (:maybe
`(let ((elt (aref text index2
)))
1338 (otherwise `(aref text index2
))))
1340 (return index2
)))))))))
1343 ;;; Open-code CONCATENATE for strings. It would be possible to extend
1344 ;;; this transform to non-strings, but I chose to just do the case that
1345 ;;; should cover 95% of CONCATENATE performance complaints for now.
1346 ;;; -- JES, 2007-11-17
1348 ;;; Only handle the simple result type cases. If somebody does (CONCATENATE
1349 ;;; '(STRING 6) ...) their code won't be optimized, but nobody does that in
1352 ;;; Limit full open coding based on length of constant sequences. Default
1353 ;;; value is chosen so that other parts of the compiler (constraint propagation
1354 ;;; mainly) won't go nonlinear too badly. It's not an exact number -- but
1355 ;;; in the right ballpark.
1356 (defvar *concatenate-open-code-limit
* 129)
1358 (defun string-concatenate-transform (node type lvars
)
1359 (let ((vars (make-gensym-list (length lvars
))))
1360 (if (policy node
(<= speed space
))
1362 (let ((constants-to-string
1363 ;; Strings are handled more efficiently by
1364 ;; %concatenate-to-* functions
1365 (loop for var in vars
1367 collect
(if (and (constant-lvar-p lvar
)
1368 (every #'characterp
(lvar-value lvar
)))
1369 (coerce (lvar-value lvar
) 'string
)
1371 `(lambda (.dummy.
,@vars
)
1372 (declare (ignore .dummy.
)
1375 ((string simple-string
)
1376 `(%concatenate-to-string
,@constants-to-string
))
1377 ((base-string simple-base-string
)
1378 `(%concatenate-to-base-string
,@constants-to-string
)))))
1380 (let* ((element-type (ecase type
1381 ((string simple-string
) 'character
)
1382 ((base-string simple-base-string
) 'base-char
)))
1383 (lvar-values (loop for lvar in lvars
1384 collect
(when (constant-lvar-p lvar
)
1385 (lvar-value lvar
))))
1387 (loop for value in lvar-values
1391 `(sb!impl
::string-dispatch
((simple-array * (*))
1395 (declare (muffle-conditions compiler-note
))
1398 (loop for value in lvar-values
1399 while
(and (stringp value
)
1400 (< (length value
) *concatenate-open-code-limit
*))
1401 sum
(length value
))))
1402 `(lambda (.dummy.
,@vars
)
1403 (declare (ignore .dummy.
)
1405 (declare (optimize (insert-array-bounds-checks 0)))
1406 (let* ((.length.
(+ ,@lengths
))
1407 (.pos.
,non-constant-start
)
1408 (.string.
(make-string .length.
:element-type
',element-type
)))
1409 (declare (type index .length. .pos.
)
1410 #-sb-xc-host
(muffle-conditions compiler-note
)
1412 ,@(loop with constants
= -
1
1413 for value in lvar-values
1416 (cond ((and (stringp value
)
1417 (< (length value
) *concatenate-open-code-limit
*))
1418 ;; Fold the array reads for constant arguments
1420 ,@(loop for c across value
1423 ;; Without truly-the we get massive numbers
1424 ;; of pointless error traps.
1425 `(setf (aref .string.
1426 (truly-the index
,(if constants
1431 `(incf (truly-the index .pos.
) ,(length value
)))))
1434 `(sb!impl
::string-dispatch
1436 (simple-array character
(*))
1437 (simple-array base-char
(*))
1440 (replace .string.
,var
1441 ,@(cond ((not constants
)
1443 ((plusp non-constant-start
)
1444 `(:start1
,non-constant-start
))))
1445 (incf (truly-the index .pos.
) (length ,var
)))
1446 (setf constants nil
)))))
1449 (defun vector-specifier-widetag (type)
1450 ;; FIXME: This only accepts vectors without dimensions even though
1451 ;; it's not that hard to support them for the concatenate transform,
1452 ;; but it's probably not used often enough to bother.
1453 (cond ((and (array-type-p type
)
1454 (equal (array-type-dimensions type
) '(*)))
1455 (let* ((el-ctype (array-type-element-type type
))
1456 (el-ctype (if (eq el-ctype
*wild-type
*)
1459 (saetp (find-saetp-by-ctype el-ctype
)))
1461 (sb!vm
:saetp-typecode saetp
))))
1462 ((and (union-type-p type
)
1463 (csubtypep type
(specifier-type 'string
))
1464 (loop for type in
(union-type-types type
)
1465 always
(equal (array-type-dimensions type
) '(*))))
1467 sb
!vm
:simple-character-string-widetag
1469 sb
!vm
:simple-base-string-widetag
)))
1471 (deftransform concatenate
((result-type &rest lvars
)
1475 (let* ((type (ir1-transform-specifier-type (lvar-value result-type
)))
1476 (vector-widetag (vector-specifier-widetag type
)))
1477 (flet ((coerce-constants (vars type
)
1478 ;; Lists are faster to iterate over than vectors of
1480 (loop for var in vars
1482 collect
(if (constant-lvar-p lvar
)
1483 `',(coerce (lvar-value lvar
) type
)
1486 (cond ((type= type
(specifier-type 'list
))
1487 (let ((vars (make-gensym-list (length lvars
))))
1488 `(lambda (type ,@vars
)
1489 (declare (ignore type
)
1491 (%concatenate-to-list
,@(coerce-constants vars
'list
)))))
1492 ((not vector-widetag
)
1493 (give-up-ir1-transform))
1494 ((= vector-widetag sb
!vm
:simple-base-string-widetag
)
1495 (string-concatenate-transform node
'simple-base-string lvars
))
1497 ((= vector-widetag sb
!vm
:simple-character-string-widetag
)
1498 (string-concatenate-transform node
'string lvars
))
1499 ;; FIXME: other vectors may use inlined expansion from
1500 ;; STRING-CONCATENATE-TRANSFORM as well.
1502 (let ((vars (make-gensym-list (length lvars
))))
1503 `(lambda (type ,@vars
)
1504 (declare (ignore type
)
1506 ,(if (= vector-widetag sb
!vm
:simple-vector-widetag
)
1507 `(%concatenate-to-simple-vector
1508 ,@(coerce-constants vars
'vector
))
1509 `(%concatenate-to-vector
1510 ,vector-widetag
,@(coerce-constants vars
'list
))))))))))
1512 ;;;; CONS accessor DERIVE-TYPE optimizers
1514 (defoptimizer (car derive-type
) ((cons))
1515 ;; This and CDR needs to use LVAR-CONSERVATIVE-TYPE because type inference
1516 ;; gets confused by things like (SETF CAR).
1517 (let ((type (lvar-conservative-type cons
))
1518 (null-type (specifier-type 'null
)))
1519 (cond ((eq type null-type
)
1522 (cons-type-car-type type
)))))
1524 (defoptimizer (cdr derive-type
) ((cons))
1525 (let ((type (lvar-conservative-type cons
))
1526 (null-type (specifier-type 'null
)))
1527 (cond ((eq type null-type
)
1530 (cons-type-cdr-type type
)))))
1532 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
1534 ;;; We want to make sure that %FIND-POSITION is inline-expanded into
1535 ;;; %FIND-POSITION-IF only when %FIND-POSITION-IF has an inline
1536 ;;; expansion, so we factor out the condition into this function.
1537 (defun check-inlineability-of-find-position-if (sequence from-end
)
1538 (let ((ctype (lvar-type sequence
)))
1539 (cond ((csubtypep ctype
(specifier-type 'vector
))
1540 ;; It's not worth trying to inline vector code unless we
1541 ;; know a fair amount about it at compile time.
1542 (upgraded-element-type-specifier-or-give-up sequence
)
1543 (unless (constant-lvar-p from-end
)
1544 (give-up-ir1-transform
1545 "FROM-END argument value not known at compile time")))
1546 ((csubtypep ctype
(specifier-type 'list
))
1547 ;; Inlining on lists is generally worthwhile.
1550 (give-up-ir1-transform
1551 "sequence type not known at compile time")))))
1553 ;;; %FIND-POSITION-IF and %FIND-POSITION-IF-NOT for LIST data
1554 (defun %find
/position-if-list-expansion
(sense from-end start end node
)
1555 (declare (ignore from-end
))
1556 ;; Circularity detection slows things down. It is permissible not to.
1557 ;; In fact, FIND is given as an archetypal example of a function that
1558 ;; "should be prepared to signal an error" but might not [CLHS 1.4.2].
1559 ;; We relax the definition of "safe" from safety=3 to >=2.
1560 (let ((safe (policy node
(>= safety
2)))
1561 ;; The secondary value is inconsequential when flowing into a non-MV
1562 ;; combination, so we avoid counting loop iterations if possible.
1563 ;; This is limited in power, but good enough, for want of a proper
1564 ;; dead-code-elimination phase of the compiler.
1566 (not (and (lvar-single-value-p (node-lvar node
))
1567 (constant-lvar-p start
)
1568 (eql (lvar-value start
) 0)
1569 (constant-lvar-p end
)
1570 (null (lvar-value end
))))))
1573 (flet ((bounds-error ()
1574 (sequence-bounding-indices-bad-error sequence start end
)))
1575 (if (and end
(> start end
))
1577 (do ((slow sequence
(cdr slow
))
1578 ,@(when safe
'((fast (cdr sequence
) (cddr fast
))))
1579 ,@(when indexed
'((index 0 (+ index
1)))))
1582 '(if (and end
(> end index
)) (bounds-error))
1584 (return (values find position
))))
1586 '(((and end
(>= index end
))
1587 (return (values find position
)))))
1590 (circular-list-error sequence
)))))
1592 (declare (list slow
,@(and safe
'(fast)))
1593 ;; If you have as many as INDEX conses on a 32-bit build,
1594 ;; then you've either used up 4GB of memory (impossible)
1595 ;; or you're stuck in a circular list in unsafe code.
1596 ;; Correspondingly larger limit for 64-bit.
1597 ,@(and indexed
'((index index
))))
1598 (,@(if indexed
'(when (>= index start
)) '(progn))
1599 (let ((element (car slow
)))
1600 ;; This hack of dealing with non-NIL FROM-END for list data
1601 ;; by iterating forward through the list and keeping track of
1602 ;; the last time we found a match might be more screwy than
1603 ;; what the user expects, but it seems to be allowed by the
1604 ;; ANSI standard. (And if the user is screwy enough to ask
1605 ;; for FROM-END behavior on list data, turnabout is fair play.)
1607 ;; It's also not enormously efficient, calling PREDICATE
1608 ;; and KEY more often than necessary; but all the alternatives
1609 ;; seem to have their own efficiency problems.
1610 (,sense
(funcall predicate
(funcall key element
))
1612 (setf find element position
,(and indexed
'index
))
1613 (return (values element
,(and indexed
'index
)))))))))))))
1615 (macrolet ((def (name condition
)
1616 `(deftransform ,name
((predicate sequence from-end start end key
)
1617 (function list t t t function
)
1620 :policy
(> speed space
))
1622 (%find
/position-if-list-expansion
',condition
1623 from-end start end node
))))
1624 (def %find-position-if when
)
1625 (def %find-position-if-not unless
))
1627 ;;; %FIND-POSITION for LIST data can be expanded into %FIND-POSITION-IF
1628 ;;; without loss of efficiency. (I.e., the optimizer should be able
1629 ;;; to straighten everything out.)
1630 (deftransform %find-position
((item sequence from-end start end key test
)
1633 :policy
(> speed space
))
1635 '(%find-position-if
(let ((test-fun (%coerce-callable-to-fun test
)))
1636 ;; The order of arguments for asymmetric tests
1637 ;; (e.g. #'<, as opposed to order-independent
1638 ;; tests like #'=) is specified in the spec
1639 ;; section 17.2.1 -- the O/Zi stuff there.
1641 (funcall test-fun item i
)))
1646 (%coerce-callable-to-fun key
)))
1648 ;;; The inline expansions for the VECTOR case are saved as macros so
1649 ;;; that we can share them between the DEFTRANSFORMs and the default
1650 ;;; cases in the DEFUNs. (This isn't needed for the LIST case, because
1651 ;;; the DEFTRANSFORMs for LIST are less choosy about when to expand.)
1652 (defun %find-position-or-find-position-if-vector-expansion
(sequence-arg
1658 (with-unique-names (offset block index n-sequence sequence end
)
1659 `(let* ((,n-sequence
,sequence-arg
))
1660 (with-array-data ((,sequence
,n-sequence
:offset-var
,offset
)
1663 :check-fill-pointer t
)
1665 (macrolet ((maybe-return ()
1666 ;; WITH-ARRAY-DATA has already performed bounds
1667 ;; checking, so we can safely elide the checks
1668 ;; in the inner loop.
1669 '(let ((,element
(locally (declare (optimize (insert-array-bounds-checks 0)))
1670 (aref ,sequence
,index
))))
1674 (- ,index
,offset
)))))))
1677 ;; (If we aren't fastidious about declaring that
1678 ;; INDEX might be -1, then (FIND 1 #() :FROM-END T)
1679 ;; can send us off into never-never land, since
1680 ;; INDEX is initialized to -1.)
1681 of-type index-or-minus-1
1682 from
(1- ,end
) downto
,start do
1684 (loop for
,index of-type index from
,start below
,end do
1686 (values nil nil
))))))
1688 (sb!xc
:defmacro %find-position-vector-macro
(item sequence
1689 from-end start end key test
)
1690 (with-unique-names (element)
1691 (%find-position-or-find-position-if-vector-expansion
1697 ;; (See the LIST transform for a discussion of the correct
1698 ;; argument order, i.e. whether the searched-for ,ITEM goes before
1699 ;; or after the checked sequence element.)
1700 `(funcall ,test
,item
(funcall ,key
,element
)))))
1702 (sb!xc
:defmacro %find-position-if-vector-macro
(predicate sequence
1703 from-end start end key
)
1704 (with-unique-names (element)
1705 (%find-position-or-find-position-if-vector-expansion
1711 `(funcall ,predicate
(funcall ,key
,element
)))))
1713 (sb!xc
:defmacro %find-position-if-not-vector-macro
(predicate sequence
1714 from-end start end key
)
1715 (with-unique-names (element)
1716 (%find-position-or-find-position-if-vector-expansion
1722 `(not (funcall ,predicate
(funcall ,key
,element
))))))
1724 ;;; %FIND-POSITION, %FIND-POSITION-IF and %FIND-POSITION-IF-NOT for
1726 (deftransform %find-position-if
((predicate sequence from-end start end key
)
1727 (function vector t t t function
)
1729 :policy
(> speed space
))
1731 (check-inlineability-of-find-position-if sequence from-end
)
1732 '(%find-position-if-vector-macro predicate sequence
1733 from-end start end key
))
1735 (deftransform %find-position-if-not
((predicate sequence from-end start end key
)
1736 (function vector t t t function
)
1738 :policy
(> speed space
))
1740 (check-inlineability-of-find-position-if sequence from-end
)
1741 '(%find-position-if-not-vector-macro predicate sequence
1742 from-end start end key
))
1744 (deftransform %find-position
((item sequence from-end start end key test
)
1745 (t vector t t t function function
)
1747 :policy
(> speed space
))
1749 (check-inlineability-of-find-position-if sequence from-end
)
1750 '(%find-position-vector-macro item sequence
1751 from-end start end key test
))
1753 (deftransform %find-position
((item sequence from-end start end key test
)
1754 (t bit-vector t t t t t
)
1756 (when (and test
(lvar-fun-is test
'(eq eql equal
)))
1758 (when (and key
(lvar-fun-is key
'(identity)))
1761 (delay-ir1-transform node
:optimize
)
1762 (give-up-ir1-transform "non-trivial :KEY or :TEST"))
1764 `(with-array-data ((bits sequence
:offset-var offset
)
1767 :check-fill-pointer t
)
1768 (let ((p ,(if (constant-lvar-p item
)
1769 (case (lvar-value item
)
1770 (0 `(%bit-position
/0 bits from-end start end
))
1771 (1 `(%bit-position
/1 bits from-end start end
))
1772 (otherwise (throw 'not-a-bit
`(values nil nil
))))
1773 `(%bit-position item bits from-end start end
))))
1775 (values item
(the index
(- (truly-the index p
) offset
)))
1776 (values nil nil
))))))
1778 (deftransform %find-position
((item sequence from-end start end key test
)
1779 (character string t t t function function
)
1781 :policy
(> speed space
))
1782 (if (eq '* (upgraded-element-type-specifier sequence
))
1784 `(sb!impl
::string-dispatch
((simple-array character
(*))
1785 (simple-array base-char
(*))
1786 (simple-array nil
(*)))
1788 (%find-position item sequence from-end start end key test
))))
1789 (if (csubtypep (lvar-type sequence
) (specifier-type 'simple-string
))
1791 ;; Otherwise we'd get three instances of WITH-ARRAY-DATA from
1793 `(with-array-data ((sequence sequence
:offset-var offset
)
1796 :check-fill-pointer t
)
1797 (multiple-value-bind (elt index
) ,form
1798 (values elt
(when (fixnump index
) (- index offset
)))))))
1799 ;; The type is known exactly, other transforms will take care of it.
1800 (give-up-ir1-transform)))
1802 ;;; logic to unravel :TEST, :TEST-NOT, and :KEY options in FIND,
1803 ;;; POSITION-IF, etc.
1804 (define-source-transform effective-find-position-test
(test test-not
)
1805 (once-only ((test test
)
1806 (test-not test-not
))
1808 ((and ,test
,test-not
)
1809 (error "can't specify both :TEST and :TEST-NOT"))
1810 (,test
(%coerce-callable-to-fun
,test
))
1812 ;; (Without DYNAMIC-EXTENT, this is potentially horribly
1813 ;; inefficient, but since the TEST-NOT option is deprecated
1814 ;; anyway, we don't care.)
1815 (complement (%coerce-callable-to-fun
,test-not
)))
1817 (define-source-transform effective-find-position-key
(key)
1818 (once-only ((key key
))
1820 (%coerce-callable-to-fun
,key
)
1823 (macrolet ((define-find-position (fun-name values-index
)
1824 `(deftransform ,fun-name
((item sequence
&key
1825 from-end
(start 0) end
1827 (t (or list vector
) &rest t
))
1828 '(nth-value ,values-index
1829 (%find-position item sequence
1832 (effective-find-position-key key
)
1833 (effective-find-position-test
1835 (define-find-position find
0)
1836 (define-find-position position
1))
1838 (macrolet ((define-find-position-if (fun-name values-index
)
1839 `(deftransform ,fun-name
((predicate sequence
&key
1842 (t (or list vector
) &rest t
))
1845 (%find-position-if
(%coerce-callable-to-fun predicate
)
1848 (effective-find-position-key key
))))))
1849 (define-find-position-if find-if
0)
1850 (define-find-position-if position-if
1))
1852 ;;; the deprecated functions FIND-IF-NOT and POSITION-IF-NOT. We
1853 ;;; didn't bother to worry about optimizing them, except note that on
1854 ;;; Sat, Oct 06, 2001 at 04:22:38PM +0100, Christophe Rhodes wrote on
1857 ;;; My understanding is that while the :test-not argument is
1858 ;;; deprecated in favour of :test (complement #'foo) because of
1859 ;;; semantic difficulties (what happens if both :test and :test-not
1860 ;;; are supplied, etc) the -if-not variants, while officially
1861 ;;; deprecated, would be undeprecated were X3J13 actually to produce
1862 ;;; a revised standard, as there are perfectly legitimate idiomatic
1863 ;;; reasons for allowing the -if-not versions equal status,
1864 ;;; particularly remove-if-not (== filter).
1866 ;;; This is only an informal understanding, I grant you, but
1867 ;;; perhaps it's worth optimizing the -if-not versions in the same
1868 ;;; way as the others?
1870 ;;; FIXME: Maybe remove uses of these deprecated functions within the
1871 ;;; implementation of SBCL.
1872 (macrolet ((define-find-position-if-not (fun-name values-index
)
1873 `(deftransform ,fun-name
((predicate sequence
&key
1876 (t (or list vector
) &rest t
))
1879 (%find-position-if-not
(%coerce-callable-to-fun predicate
)
1882 (effective-find-position-key key
))))))
1883 (define-find-position-if-not find-if-not
0)
1884 (define-find-position-if-not position-if-not
1))
1886 (macrolet ((define-trimmer-transform (fun-name leftp rightp
)
1887 `(deftransform ,fun-name
((char-bag string
)
1890 (if (constant-lvar-p char-bag
)
1891 ;; If the bag is constant, use MEMBER
1892 ;; instead of FIND, since we have a
1893 ;; deftransform for MEMBER that can
1894 ;; open-code all of the comparisons when
1895 ;; the list is constant. -- JES, 2007-12-10
1896 `(not (member (schar string index
)
1897 ',(coerce (lvar-value char-bag
) 'list
)
1899 '(not (find (schar string index
) char-bag
:test
#'char
=)))))
1900 `(flet ((char-not-in-bag (index)
1902 (let* ((end (length string
))
1903 (left-end (if ,',leftp
1904 (do ((index 0 (1+ index
)))
1905 ((or (= index
(the fixnum end
))
1906 (char-not-in-bag index
))
1908 (declare (fixnum index
)))
1910 (right-end (if ,',rightp
1911 (do ((index (1- end
) (1- index
)))
1912 ((or (< index left-end
)
1913 (char-not-in-bag index
))
1915 (declare (fixnum index
)))
1917 (if (and (eql left-end
0)
1918 (eql right-end
(length string
)))
1920 (subseq string left-end right-end
))))))))
1921 (define-trimmer-transform string-left-trim t nil
)
1922 (define-trimmer-transform string-right-trim nil t
)
1923 (define-trimmer-transform string-trim t t
))
1926 ;;; (partially) constant-fold backq-* functions, or convert to their
1927 ;;; plain CL equivalent (now that they're not needed for pprinting).
1929 ;; Pop constant values from the end, list/list* them if any, and link
1930 ;; the remainder with list* at runtime.
1931 (defun transform-backq-list-or-list* (function values
)
1932 (let ((gensyms (make-gensym-list (length values
)))
1933 (reverse (reverse values
))
1935 (loop while
(and reverse
1936 (constant-lvar-p (car reverse
)))
1937 do
(push (lvar-value (pop reverse
))
1939 (if (null constants
)
1941 (,function
,@gensyms
))
1942 (let ((tail (apply function constants
)))
1945 (let* ((nvariants (length reverse
))
1946 (variants (subseq gensyms
0 nvariants
)))
1948 (declare (ignore ,@(subseq gensyms nvariants
)))
1950 `(list* ,@variants
',tail
)
1951 `(list ,@variants
)))))))))
1953 (deftransform sb
!impl
::|List|
((&rest elts
))
1954 (transform-backq-list-or-list* 'list elts
))
1956 (deftransform sb
!impl
::|List
*|
((&rest elts
))
1957 (transform-backq-list-or-list* 'list
* elts
))
1959 ;; Merge adjacent constant values
1960 (deftransform sb
!impl
::|Append|
((&rest elts
))
1961 (let ((gensyms (make-gensym-list (length elts
)))
1965 (flet ((convert-accumulator ()
1966 (let ((constant (apply 'append
(nreverse (shiftf acc nil
)))))
1968 (push `',constant arguments
)))))
1969 (loop for gensym in gensyms
1970 for
(elt . next
) on elts by
#'cdr
1971 do
(cond ((constant-lvar-p elt
)
1972 (let ((elt (lvar-value elt
)))
1973 (when (and next
(not (proper-list-p elt
)))
1974 (abort-ir1-transform
1975 "Non-list or improper list spliced in ~
1976 the middle of a backquoted list."))
1977 (push gensym ignored
)
1980 (convert-accumulator)
1981 (push gensym arguments
)))
1982 finally
(convert-accumulator)))
1983 (let ((arguments (nreverse arguments
)))
1985 (declare (ignore ,@ignored
))
1986 (append ,@arguments
)))))
1988 (deftransform reverse
((sequence) (vector) * :important nil
)
1989 `(sb!impl
::vector-reverse sequence
))
1991 (deftransform reverse
((sequence) (list) * :important nil
)
1992 `(sb!impl
::list-reverse sequence
))
1994 (deftransform nreverse
((sequence) (vector) * :important nil
)
1995 `(sb!impl
::vector-nreverse sequence
))
1997 (deftransform nreverse
((sequence) (list) * :important nil
)
1998 `(sb!impl
::list-nreverse sequence
))