Localize a macro
[sbcl.git] / src / compiler / knownfun.lisp
blob7e0317b02f80e4d6cb79c454f99712159f33d9be
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
7 ;;;; more information.
8 ;;;;
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.
15 (in-package "SB-C")
17 (/show0 "knownfun.lisp 17")
19 ;;;; interfaces to defining macros
21 ;;; an IR1 transform
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
29 ;; FUN-TYPE.)
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)
47 (:predicate 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)))
54 "optimize"))
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)
73 ctype)))
74 transforms)))
75 (cond (old
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
85 :policy policy)
86 (fun-info-transforms info))
87 (let ((normal (member-if (lambda (transform)
88 (not (typep transform 'vop-transform)))
89 transforms))
90 (transform (make-transform :type ctype :function fun
91 :important important
92 :policy policy)))
93 (setf (fun-info-transforms info)
94 (append (ldiff transforms normal) (list* transform normal)))))))
95 name))
97 ;;; Make a FUN-INFO structure with the specified type, attributes
98 ;;; and optimizers.
99 (defun %defknown (names type attributes location
100 &key derive-type optimizer result-arg
101 overwrite-fndb-silently
102 call-type-deriver
103 annotation
104 folder)
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)
109 ctype)))
110 (dolist (name names)
111 (let ((old-fun-info (info :function :info name))
112 inherit)
113 (block ignore
114 (unless overwrite-fndb-silently
115 (when old-fun-info
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:
119 ;; DEFKNOWN FOO
120 ;; DEFTRANSFORM FOO
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.
125 (restart-case
126 (cerror "Go ahead, overwrite it."
127 "~@<overwriting old FUN-INFO ~2I~_~S ~I~_for ~S~:>"
128 old-fun-info name)
129 (continue ()
130 :report "Inherit templates and optimizers"
131 (setf inherit t))
132 (ignore ()
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)
137 (cond (inherit
138 (when optimizer
139 (setf (fun-info-optimizer old-fun-info) optimizer))
140 (when derive-type
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
151 :optimizer optimizer
152 :result-arg result-arg
153 :call-type-deriver call-type-deriver
154 :annotation annotation
155 :folder folder))))
156 (if location
157 (setf (getf (info :source-location :declaration name) 'defknown)
158 location)
159 (remf (info :source-location :declaration name) 'defknown))))))
160 names)
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))
182 &body keys)
183 #-sb-xc-host
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
196 #-(or arm64 x86-64)
197 (setf attributes (remove 'no-verify-arg-count attributes))
198 #-(or arm64 x86-64)
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)))
210 name
211 (list name))
212 ',type
213 (ir1-attributes ,@attributes)
214 (source-location)
215 :annotation ,annotation
216 ,@keys
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
226 (:copier nil))
227 positional ;; required and &optional
228 rest
230 returns)
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)
236 (parse-lambda-list
237 arg-types
238 :context :function-type
239 :accept (lambda-list-keyword-mask
240 '(&optional &rest &key &allow-other-keys))
241 :silent t)
242 (let ((i -1)
243 positional-annotation
244 rest-annotation
245 key-annotation
246 return-annotation)
247 (labels ((annotation-p (x)
248 (typep x '(or (cons (member function function-designator modifying
249 inhibit-flushing))
250 (member type-specifier proper-sequence proper-list
251 proper-or-dotted-list proper-or-circular-list))))
252 (strip-annotation (x)
253 (if (consp x)
254 (ecase (car x)
255 ((function function-designator) (car x))
256 ((modifying inhibit-flushing) (cadr x)))
257 (case x
258 (proper-sequence 'sequence)
259 ((proper-list proper-or-dotted-list proper-or-circular-list) 'list)
260 (t x))))
261 (process-positional (type)
262 (incf i)
263 (cond ((annotation-p type)
264 (push (cons i (ensure-list type)) positional-annotation)
265 (strip-annotation type))
267 type)))
268 (process-key (pair)
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))))
274 pair)))
275 (process-rest (type)
276 (cond ((annotation-p type)
277 (setf rest-annotation (ensure-list type))
278 (strip-annotation type))
280 type)))
281 (process-return (type)
282 (cond ((annotation-p type)
283 (setf return-annotation (ensure-list type))
284 (strip-annotation type))
286 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)))
292 (values
293 `(sfunction
294 (,@required
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)))
299 ,return)
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)
317 found)))
319 (defun symbol-value-derive-type (node &aux (args (basic-combination-args node))
320 (lvar (pop args)))
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)))
329 (if local-type
330 (type-intersection local-type global-type)
331 global-type))
332 *universal-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)
346 (lambda (call)
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))
358 cons-type)
359 ((and preserve-dimensions
360 (csubtypep type null-type))
361 null-type)
362 ((csubtypep type list-type)
363 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
369 preserve-vector-type
370 string-designator)
371 (lambda (call)
372 (declare (type combination call))
373 (let ((lvar (nth n (combination-args call))))
374 (when lvar
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
387 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)))
394 simplified)
395 simplified)))))))))
397 ;;; Derive the type to be the type specifier which is the Nth arg.
398 (defun result-type-specifier-nth-arg (n)
399 (lambda (call)
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)
417 (lambda (call)
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)))
423 (cond
424 ((eq (car lspecifier) 'string)
425 (destructuring-bind (string &rest size)
426 lspecifier
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)
432 lspecifier
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))
439 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)
446 *wild-type*)
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*))
452 ctype))))))))))
454 (defun read-elt-type-deriver (skip-arg-p element-type-spec no-hang)
455 (lambda (call)
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
464 (if (and eof-error-p
465 (types-equal-or-intersect (lvar-type eof-error-p)
466 null-type))
467 ;; (READ-elt stream nil <x>) returns (OR (EQL <x>) elt-type)
468 (type-union (if eof-value (lvar-type eof-value) null-type)
469 element-type)
470 ;; If eof-error is unsupplied, or was but couldn't be nil
471 element-type)))
472 (if no-hang
473 (type-union unexceptional-type null-type)
474 unexceptional-type))))
476 ;;; Return MAX MIN
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))
484 (block nil
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.
489 (typecase type
490 (union-type
491 (mapc #'max-dim (union-type-types type)))
492 (array-type (if (array-type-complexp type)
493 (return '*)
494 (process-dim (array-type-dimensions type))))
495 (t (return '*))))
496 (process-dim (dim)
497 (if (typep dim '(cons integer null))
498 (let ((length (car dim)))
499 (setf max (max max length)
500 min (min min length)))
501 (return '*))))
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)
521 (when (cond (index
522 (assert-array-index-lvar-type arg type policy))
524 (when set
525 (add-annotation arg
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)))
530 (when set
531 (assert-type (pop args)
532 (pop required)))
533 (assert-type (pop args)
534 (if row-major-aref
535 (pop required)
536 (type-intersection
537 (pop required)
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)
543 rank
544 'array-rank-limit
545 array-rank-limit)
546 (return-from array-call-type-deriver))
547 (specifier-type `(array * ,rank)))))
548 set)
549 (loop for type in required
551 (assert-type (pop args) type nil (or (not (and set row-major-aref))
552 args)))
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
564 while next
566 (add-annotation
568 (make-lvar-proper-sequence-annotation
569 :kind 'proper-list))
570 (when (and (assert-lvar-type arg list-type policy)
571 (not trusted))
572 (reoptimize-lvar arg)))))