1 ;;;; This file contains stuff for maintaining a database of special
2 ;;;; information about functions known to the compiler. This includes
3 ;;;; semantic information such as side effects and type inference
4 ;;;; functions as well as transforms and IR2 translators.
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
17 (/show0
"knownfun.lisp 17")
19 ;;;; interfaces to defining macros
22 (defstruct (transform (:copier nil
))
23 ;; the function type which enables this transform.
25 ;; (Note that declaring this :TYPE FUN-TYPE probably wouldn't
26 ;; work because some function types, like (SPECIFIER-TYPE 'FUNCTION0
27 ;; itself, are represented as BUILT-IN-TYPE, and at least as of
28 ;; sbcl-0.pre7.54 or so, that's inconsistent with being a
30 (type (missing-arg) :type ctype
)
31 ;; the transformation function. Takes the COMBINATION node and
32 ;; returns a lambda expression, or THROWs out.
33 (function (missing-arg) :type function
)
34 ;; T if we should emit a failure note even if SPEED=INHIBIT-WARNINGS.
35 (important nil
:type
(member nil
:slightly t
))
36 ;; A function with NODE as an argument that checks wheteher the
37 ;; transform applies in its policy.
38 ;; It used to be checked in the FUNCTION body but it would produce
39 ;; notes about failed transformation due to types even though it
40 ;; wouldn't have been applied with the right types anyway,
41 ;; or if another transform could be applied with the right policy.
42 (policy nil
:type
(or null function
)))
44 ;;; A transform inserted at the front of fun-info-transforms and stops
45 ;;; other from firing if it has a VOP that can do the job.
46 (defstruct (vop-transform (:copier nil
)
48 (:include transform
)))
50 (defun transform-note (transform)
51 (or #+sb-xc-host
(documentation (transform-function transform
) 'function
)
52 #-sb-xc-host
(and (fboundp 'sb-pcl
::fun-doc
)
53 (funcall 'sb-pcl
::fun-doc
(transform-function transform
)))
56 (defmethod print-object ((x transform
) stream
)
57 (print-unreadable-object (x stream
:type t
:identity t
)
58 (princ (type-specifier (transform-type x
)) stream
)))
60 ;;; Grab the FUN-INFO and enter the function, replacing any old
61 ;;; one with the same type and note.
62 ;;; Argument order is: policy constraint, ftype constraint, consequent.
63 ;;; (think "qualifiers + specializers -> method")
64 (defun %deftransform
(name policy type fun
&optional
(important :slightly
))
65 (let* ((ctype (specifier-type type
))
66 (info (fun-info-or-lose name
))
67 (transforms (fun-info-transforms info
))
68 (old (find-if (lambda (transform)
69 (and (if (eq important
:vop
)
70 (typep transform
'vop-transform
)
71 (not (typep transform
'vop-transform
)))
72 (type= (transform-type transform
)
76 (style-warn 'redefinition-with-deftransform
:transform old
)
77 (setf (transform-function old
) fun
78 (transform-policy old
) policy
)
79 (unless (eq important
:vop
)
80 (setf (transform-important old
) important
)))
82 ;; Put vop-transform at the front.
83 (if (eq important
:vop
)
84 (push (make-vop-transform :type ctype
:function fun
86 (fun-info-transforms info
))
87 (let ((normal (member-if (lambda (transform)
88 (not (typep transform
'vop-transform
)))
90 (transform (make-transform :type ctype
:function fun
93 (setf (fun-info-transforms info
)
94 (append (ldiff transforms normal
) (list* transform normal
)))))))
97 ;;; Make a FUN-INFO structure with the specified type, attributes
99 (defun %defknown
(names type attributes location
100 &key derive-type optimizer result-arg
101 overwrite-fndb-silently
105 (let* ((ctype (specifier-type type
))
106 (type-to-store (if (contains-unknown-type-p ctype
)
107 ;; unparse it, so SFUNCTION -> FUNCTION
108 (type-specifier ctype
)
111 (let ((old-fun-info (info :function
:info name
))
114 (unless overwrite-fndb-silently
116 ;; This is handled as an error because it's generally a bad
117 ;; thing to blow away all the old optimization stuff. It's
118 ;; also a potential source of sneaky bugs:
121 ;; DEFKNOWN FOO ; possibly hidden inside some macroexpansion
122 ;; ; Now the DEFTRANSFORM doesn't exist in the target Lisp.
123 ;; However, it's continuable because it might be useful to do
124 ;; it when testing new optimization stuff interactively.
126 (cerror "Go ahead, overwrite it."
127 "~@<overwriting old FUN-INFO ~2I~_~S ~I~_for ~S~:>"
130 :report
"Inherit templates and optimizers"
133 (return-from ignore
)))))
134 (setf (info :function
:type name
) type-to-store
)
135 (setf (info :function
:where-from name
) :declared
)
136 (setf (info :function
:kind name
) :function
)
139 (setf (fun-info-optimizer old-fun-info
) optimizer
))
141 (setf (fun-info-derive-type old-fun-info
) derive-type
))
142 (setf (fun-info-attributes old-fun-info
) attributes
143 (fun-info-result-arg old-fun-info
) result-arg
144 (fun-info-annotation old-fun-info
) annotation
145 (fun-info-call-type-deriver old-fun-info
) call-type-deriver
146 (fun-info-folder old-fun-info
) folder
))
148 (setf (info :function
:info name
)
149 (make-fun-info :attributes attributes
150 :derive-type derive-type
152 :result-arg result-arg
153 :call-type-deriver call-type-deriver
154 :annotation annotation
157 (setf (getf (info :source-location
:declaration name
) 'defknown
)
159 (remf (info :source-location
:declaration name
) 'defknown
))))))
163 ;;; This macro should be the way that all implementation independent
164 ;;; information about functions is made known to the compiler.
166 ;;; FIXME: The comment above suggests that perhaps some of my added
167 ;;; FTYPE declarations are in poor taste. Should I change my
168 ;;; declarations, or change the comment, or what?
170 ;;; FIXME: DEFKNOWN is needed only at build-the-system time. Figure
171 ;;; out some way to keep it from appearing in the target system.
173 ;;; Declare the function NAME to be a known function. We construct a
174 ;;; type specifier for the function by wrapping (FUNCTION ...) around
175 ;;; the ARG-TYPES and RESULT-TYPE. ATTRIBUTES is an unevaluated list
176 ;;; of boolean attributes of the function. See their description in
177 ;;; (!DEF-BOOLEAN-ATTRIBUTE IR1). NAME may also be a list of names, in
178 ;;; which case the same information is given to all the names. The
179 ;;; keywords specify the initial values for various optimizers that
180 ;;; the function might have.
181 (defmacro defknown
(name arg-types result-type
&optional
(attributes '(any))
184 (when (member 'unsafe attributes
)
185 (style-warn "Ignoring legacy attribute UNSAFE. Replaced by its inverse: DX-SAFE.")
186 (setf attributes
(remove 'unsafe attributes
)))
187 (when (and (intersection attributes
'(any call unwind
))
188 (intersection attributes
'(movable)))
189 (error "function cannot have both good and bad attributes: ~S" attributes
))
191 (when (member 'any attributes
)
192 (setq attributes
(union '(unwind) attributes
)))
193 (when (member 'flushable attributes
)
194 (pushnew 'unsafely-flushable attributes
))
195 ;; Needs to be supported by the call VOPs
197 (setf attributes
(remove 'no-verify-arg-count attributes
))
199 (setf attributes
(remove 'unboxed-return attributes
))
200 #-
(or arm64 x86-64
) ;; Needs to be supported by the call VOPs, sb-vm::fixed-call-arg-location
201 (setf attributes
(remove 'fixed-args attributes
))
202 (when (or (memq 'fixed-args attributes
)
203 (memq 'unboxed-return attributes
))
204 (pushnew 'no-verify-arg-count attributes
))
206 (multiple-value-bind (type annotation
)
207 (split-type-info arg-types result-type
)
208 `(%defknown
',(if (and (consp name
)
209 (not (legal-fun-name-p name
)))
213 (ir1-attributes ,@attributes
)
215 :annotation
,annotation
217 :folder
,(and (memq 'foldable attributes
)
218 (not (getf keys
:folder
))
220 (memq 'fixed-args attributes
)
221 (memq 'unboxed-return attributes
))
222 (let ((args (make-gensym-list (length arg-types
))))
223 `(lambda ,args
(funcall ',name
,@args
)))))))
225 (defstruct (fun-type-annotation
227 positional
;; required and &optional
232 (defun split-type-info (arg-types result-type
)
233 (if (eq arg-types
'*)
234 `(sfunction ,arg-types
,result-type
)
235 (multiple-value-bind (llks required optional rest keys
)
238 :context
:function-type
239 :accept
(lambda-list-keyword-mask
240 '(&optional
&rest
&key
&allow-other-keys
))
243 positional-annotation
247 (labels ((annotation-p (x)
248 (typep x
'(or (cons (member function function-designator modifying
250 (member type-specifier proper-sequence proper-list
251 proper-or-dotted-list proper-or-circular-list
))))
252 (strip-annotation (x)
255 ((function function-designator
) (car x
))
256 ((modifying inhibit-flushing
) (cadr x
)))
258 (proper-sequence 'sequence
)
259 ((proper-list proper-or-dotted-list proper-or-circular-list
) 'list
)
261 (process-positional (type)
263 (cond ((annotation-p type
)
264 (push (cons i
(ensure-list type
)) positional-annotation
)
265 (strip-annotation type
))
269 (cond ((annotation-p (cadr pair
))
270 (destructuring-bind (key value
) pair
271 (setf (getf key-annotation key
) (ensure-list value
))
272 (list key
(strip-annotation value
))))
276 (cond ((annotation-p type
)
277 (setf rest-annotation
(ensure-list type
))
278 (strip-annotation type
))
281 (process-return (type)
282 (cond ((annotation-p type
)
283 (setf return-annotation
(ensure-list type
))
284 (strip-annotation type
))
287 (let ((required (mapcar #'process-positional required
))
288 (optional (mapcar #'process-positional optional
))
289 (rest (process-rest (car rest
)))
290 (key (mapcar #'process-key keys
))
291 (return (process-return result-type
)))
295 ,@(and optional
`(&optional
,@optional
))
296 ,@(and (ll-kwds-restp llks
) `(&rest
,rest
))
297 ,@(and (ll-kwds-keyp llks
) `(&key
,@key
))
298 ,@(and (ll-kwds-allowp llks
) '(&allow-other-keys
)))
300 (when (or positional-annotation rest-annotation
301 key-annotation return-annotation
)
302 `(make-fun-type-annotation :positional
',positional-annotation
303 :rest
',rest-annotation
304 :key
',key-annotation
305 :returns
',return-annotation
)))))))))
307 ;;; Return the FUN-INFO for NAME or die trying.
308 (declaim (ftype (sfunction (t) fun-info
) fun-info-or-lose
))
309 (defun fun-info-or-lose (name)
310 (or (info :function
:info name
) (error "~S is not a known function." name
)))
312 ;;;; generic type inference methods
314 (defun maybe-find-free-var (name)
315 (let ((found (gethash name
(free-vars *ir1-namespace
*))))
316 (unless (eq found
:deprecated
)
319 (defun symbol-value-derive-type (node &aux
(args (basic-combination-args node
))
321 (unless (and lvar
(endp args
))
322 (return-from symbol-value-derive-type
))
323 (if (constant-lvar-p lvar
)
324 (let* ((sym (lvar-value lvar
))
325 (var (maybe-find-free-var sym
))
326 (local-type (when var
327 (lexenv-find var type-restrictions
:lexenv
(node-lexenv node
))))
328 (global-type (info :variable
:type sym
)))
330 (type-intersection local-type global-type
)
334 ;;; Derive the type to be the type of the xxx'th arg. This can normally
335 ;;; only be done when the result value is that argument.
336 (defun result-type-first-arg (call)
337 (declare (type combination call
))
338 (let ((lvar (first (combination-args call
))))
339 (when lvar
(lvar-type lvar
))))
340 (defun result-type-last-arg (call)
341 (declare (type combination call
))
342 (let ((lvar (car (last (combination-args call
)))))
343 (when lvar
(lvar-type lvar
))))
345 (defun result-type-nth-arg (n)
347 (let ((lvar (nth n
(combination-args call
))))
348 (when lvar
(lvar-type lvar
)))))
350 (defun simplify-list-type (type &key preserve-dimensions
)
351 ;; Preserve all the list types without dragging
352 ;; (cons (eql 10)) stuff in.
353 (let ((cons-type (specifier-type 'cons
))
354 (list-type (specifier-type 'list
))
355 (null-type (specifier-type 'null
)))
356 (cond ((and preserve-dimensions
357 (csubtypep type cons-type
))
359 ((and preserve-dimensions
360 (csubtypep type null-type
))
362 ((csubtypep type list-type
)
365 ;;; Return a closure usable as a derive-type method for accessing the
366 ;;; N'th argument. If arg is a list, result is a list. If arg is a
367 ;;; vector, result is a vector with the same element type.
368 (defun sequence-result-nth-arg (n &key preserve-dimensions
372 (declare (type combination call
))
373 (let ((lvar (nth n
(combination-args call
))))
375 (let ((type (lvar-type lvar
)))
376 (cond ((and (not string-designator
)
377 (simplify-list-type type
378 :preserve-dimensions preserve-dimensions
)))
379 ((not (csubtypep type
(specifier-type 'vector
)))
380 (cond ((not string-designator
) nil
)
381 ((csubtypep type
(specifier-type 'character
))
382 (specifier-type `(simple-string 1)))
383 ((and (constant-lvar-p lvar
)
384 (symbolp (lvar-value lvar
)))
385 (ctype-of (symbol-name (lvar-value lvar
))))))
386 (preserve-vector-type
389 (let ((simplified (simplify-vector-type type
)))
390 (if (and preserve-dimensions
391 (csubtypep simplified
(specifier-type 'simple-array
)))
392 (type-intersection (specifier-type
393 `(simple-array * ,(ctype-array-dimensions type
)))
397 ;;; Derive the type to be the type specifier which is the Nth arg.
398 (defun result-type-specifier-nth-arg (n)
400 (declare (type combination call
))
401 (let ((lvar (nth n
(combination-args call
))))
402 (when (and lvar
(constant-lvar-p lvar
))
403 (careful-specifier-type (lvar-value lvar
))))))
405 ;;; Derive the type to be the type specifier which is the Nth arg,
406 ;;; with the additional restriptions noted in the CLHS for STRING and
407 ;;; SIMPLE-STRING, defined to specialize on CHARACTER, and for VECTOR
408 ;;; (under the page for MAKE-SEQUENCE).
409 ;;; At present this is used to derive the output type of CONCATENATE,
410 ;;; MAKE-SEQUENCE, and MERGE. Two things seem slightly amiss:
411 ;;; 1. The sequence type actually produced might not be exactly that specified.
412 ;;; (TYPE-OF (MAKE-SEQUENCE '(AND (NOT SIMPLE-ARRAY) (VECTOR BIT)) 9))
413 ;;; => (SIMPLE-BIT-VECTOR 9)
414 ;;; 2. Because we *know* that a hairy array won't be produced,
415 ;;; why does derivation preserve the non-simpleness, if so specified?
416 (defun creation-result-type-specifier-nth-arg (n)
418 (declare (type combination call
))
419 (let ((lvar (nth n
(combination-args call
))))
420 (when (and lvar
(constant-lvar-p lvar
))
421 (let* ((specifier (lvar-value lvar
))
422 (lspecifier (if (atom specifier
) (list specifier
) specifier
)))
424 ((eq (car lspecifier
) 'string
)
425 (destructuring-bind (string &rest size
)
427 (declare (ignore string
))
428 (careful-specifier-type
429 `(vector character
,@(when size size
)))))
430 ((eq (car lspecifier
) 'simple-string
)
431 (destructuring-bind (simple-string &rest size
)
433 (declare (ignore simple-string
))
434 (careful-specifier-type
435 `(simple-array character
,@(if size
(list size
) '((*)))))))
437 (let ((ctype (careful-specifier-type specifier
)))
438 (cond ((not (array-type-p ctype
))
440 ((unknown-type-p (array-type-element-type ctype
))
441 (make-array-type (array-type-dimensions ctype
)
442 :complexp
(array-type-complexp ctype
)
443 :element-type
*wild-type
*
444 :specialized-element-type
*wild-type
*))
445 ((eq (array-type-specialized-element-type ctype
)
447 (make-array-type (array-type-dimensions ctype
)
448 :complexp
(array-type-complexp ctype
)
449 :element-type
*universal-type
*
450 :specialized-element-type
*universal-type
*))
454 (defun read-elt-type-deriver (skip-arg-p element-type-spec no-hang
)
456 (let* ((element-type (specifier-type element-type-spec
))
457 (null-type (specifier-type 'null
))
458 (err-args (if skip-arg-p
; for PEEK-CHAR, skip 'peek-type' + 'stream'
459 (cddr (combination-args call
))
460 (cdr (combination-args call
)))) ; else just 'stream'
461 (eof-error-p (first err-args
))
462 (eof-value (second err-args
))
463 (unexceptional-type ; the normally returned thing
465 (types-equal-or-intersect (lvar-type eof-error-p
)
467 ;; (READ-elt stream nil <x>) returns (OR (EQL <x>) elt-type)
468 (type-union (if eof-value
(lvar-type eof-value
) null-type
)
470 ;; If eof-error is unsupplied, or was but couldn't be nil
473 (type-union unexceptional-type null-type
)
474 unexceptional-type
))))
477 (defun sequence-lvar-dimensions (lvar)
478 (if (constant-lvar-p lvar
)
479 (let ((value (lvar-value lvar
)))
480 (and (proper-sequence-p value
)
481 (let ((length (length value
)))
482 (values length length
))))
483 (let ((max 0) (min array-total-size-limit
))
485 (labels ((max-dim (type)
486 ;; This can deal with just enough hair to handle type STRING,
487 ;; but might be made to use GENERIC-ABSTRACT-TYPE-FUNCTION
488 ;; if we really want to be more clever.
491 (mapc #'max-dim
(union-type-types type
)))
492 (array-type (if (array-type-complexp type
)
494 (process-dim (array-type-dimensions type
))))
497 (if (typep dim
'(cons integer null
))
498 (let ((length (car dim
)))
499 (setf max
(max max length
)
500 min
(min min length
)))
502 ;; If type derivation were able to notice that non-simple arrays can
503 ;; be mutated (changing the type), we could safely use LVAR-TYPE on
504 ;; any vector type. But it doesn't notice.
505 ;; We could use LVAR-CONSERVATIVE-TYPE to get a conservative answer.
506 ;; However that's probably not an important use, so the above
507 ;; logic restricts itself to simple arrays.
508 (max-dim (lvar-type lvar
))
509 (values max min
))))))
511 ;;; This used to be done in DEFOPTIMIZER DERIVE-TYPE, but
512 ;;; ASSERT-CALL-TYPE already asserts the ARRAY type, so it gets an extra
513 ;;; assertion that may not get eliminated and requires extra work.
514 (defun array-call-type-deriver (call trusted
&optional set row-major-aref
)
515 (let* ((fun (combination-fun call
))
516 (type (lvar-fun-type fun
))
517 (policy (lexenv-policy (node-lexenv call
)))
518 (args (combination-args call
)))
519 (when (fun-type-p type
)
520 (flet ((assert-type (arg type
&optional set index
)
522 (assert-array-index-lvar-type arg type policy
))
526 (make-lvar-modified-annotation :caller
(lvar-fun-name fun
))))
527 (assert-lvar-type arg type policy
)))
528 (unless trusted
(reoptimize-lvar arg
)))))
529 (let ((required (fun-type-required type
)))
531 (assert-type (pop args
)
533 (assert-type (pop args
)
538 (let ((rank (length args
)))
539 (when (>= rank array-rank-limit
)
540 (setf (combination-kind call
) :error
)
541 (compiler-warn "More subscripts for ~a (~a) than ~a (~a)"
542 (combination-fun-debug-name call
)
546 (return-from array-call-type-deriver
))
547 (specifier-type `(array * ,rank
)))))
549 (loop for type in required
551 (assert-type (pop args
) type nil
(or (not (and set row-major-aref
))
553 (loop for type in
(fun-type-optional type
)
554 do
(assert-type (pop args
) type nil t
))
555 (loop for subscript in args
556 do
(assert-type subscript
(fun-type-rest type
) nil t
)))))))
558 (defun append-call-type-deriver (call trusted
)
559 (let* ((policy (lexenv-policy (node-lexenv call
)))
560 (args (combination-args call
))
561 (list-type (specifier-type 'list
)))
562 ;; All but the last argument should be proper lists
563 (loop for
(arg next
) on args
568 (make-lvar-proper-sequence-annotation
570 (when (and (assert-lvar-type arg list-type policy
)
572 (reoptimize-lvar arg
)))))