Section on writing tests in HACKING
[sbcl.git] / src / compiler / knownfun.lisp
blob741fa4d7501706ad5458b77dddbfea81b8e22273
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 ;;; IR1 boolean function attributes
20 ;;;
21 ;;; There are a number of boolean attributes of known functions which
22 ;;; we like to have in IR1. This information is mostly side effect
23 ;;; information of a sort, but it is different from the kind of
24 ;;; information we want in IR2. We aren't interested in a fine
25 ;;; breakdown of side effects, since we do very little code motion on
26 ;;; IR1. We are interested in some deeper semantic properties such as
27 ;;; whether it is safe to pass stack closures to.
28 ;;;
29 ;;; FIXME: This whole notion of "bad" explicit attributes is bad for
30 ;;; maintenance. How confident are we that we have no defknowns for functions
31 ;;; with functional arguments that are missing the CALL attribute? Much better
32 ;;; to have NO-CALLS, as it is much less likely to break accidentally.
33 (!def-boolean-attribute ir1
34 ;; may call functions that are passed as arguments. In order to
35 ;; determine what other effects are present, we must find the
36 ;; effects of all arguments that may be functions.
37 call
38 ;; may fail to return during correct execution. Errors are O.K.
39 ;; UNUSED, BEWARE OF BITROT.
40 unwind
41 ;; the (default) worst case. Includes all the other bad things, plus
42 ;; any other possible bad thing. If this is present, the above bad
43 ;; attributes will be explicitly present as well.
44 any
45 ;; all arguments are safe for dynamic extent.
46 ;; (We used to have an UNSAFE attribute, which was basically the inverse
47 ;; of this, but it was unused and bitrotted, so when we started making
48 ;; use of the information we flipped the name and meaning the safe way
49 ;; around.)
50 dx-safe
51 ;; may be constant-folded. The function has no side effects, but may
52 ;; be affected by side effects on the arguments. e.g. SVREF, MAPC.
53 ;; Functions that side-effect their arguments are not considered to
54 ;; be foldable. Although it would be "legal" to constant fold them
55 ;; (since it "is an error" to modify a constant), we choose not to
56 ;; mark these functions as foldable in this database.
57 foldable
58 ;; may be eliminated if value is unused. The function has no side
59 ;; effects except possibly cons. If a function might signal errors,
60 ;; then it is not flushable even if it is movable, foldable or
61 ;; unsafely-flushable. Implies UNSAFELY-FLUSHABLE. (In safe code
62 ;; type checking of arguments is always performed by the caller, so
63 ;; a function which SHOULD signal an error if arguments are not of
64 ;; declared types may be FLUSHABLE.)
65 flushable
66 ;; unsafe call may be eliminated if value is unused. The function
67 ;; has no side effects except possibly cons and signalling an error
68 ;; in the safe code. If a function MUST signal errors, then it is
69 ;; not unsafely-flushable even if it is movable or foldable.
70 unsafely-flushable
71 ;; return value is important, and ignoring it is probably a mistake.
72 ;; Unlike the other attributes, this is used only for style
73 ;; warnings and has no effect on optimization.
74 important-result
75 ;; may be moved with impunity. Has no side effects except possibly
76 ;; consing, and is affected only by its arguments.
77 ;; UNUSED, BEWARE OF BITROT.
78 movable
79 ;; The function is a true predicate likely to be open-coded. Convert
80 ;; any non-conditional uses into (IF <pred> T NIL). Not usually
81 ;; specified to DEFKNOWN, since this is implementation dependent,
82 ;; and is usually automatically set by the DEFINE-VOP :CONDITIONAL
83 ;; option.
84 predicate
85 ;; Inhibit any warning for compiling a recursive definition.
86 ;; (Normally the compiler warns when compiling a recursive
87 ;; definition for a known function, since it might be a botched
88 ;; interpreter stub.)
89 recursive
90 ;; The function does explicit argument type checking, so the
91 ;; declared type should not be asserted when a definition is
92 ;; compiled.
93 explicit-check
94 ;; The function should always be translated by a VOP (i.e. it should
95 ;; should never be converted into a full call). This is used strictly
96 ;; as a consistency checking mechanism inside the compiler during IR2
97 ;; transformation.
98 always-translatable
99 ;; If a function is called with two arguments and the first one is a
100 ;; constant, then the arguments will be swapped.
101 commutative)
103 (defstruct (fun-info #-sb-xc-host (:pure t))
104 ;; boolean attributes of this function.
105 (attributes (missing-arg) :type attributes)
106 ;; TRANSFORM structures describing transforms for this function
107 (transforms () :type list)
108 ;; a function which computes the derived type for a call to this
109 ;; function by examining the arguments. This is null when there is
110 ;; no special method for this function.
111 (derive-type nil :type (or function null))
112 ;; a function that does various unspecified code transformations by
113 ;; directly hacking the IR. Returns true if further optimizations of
114 ;; the call shouldn't be attempted.
116 ;; KLUDGE: This return convention (non-NIL if you shouldn't do
117 ;; further optimiz'ns) is backwards from the return convention for
118 ;; transforms. -- WHN 19990917
119 (optimizer nil :type (or function null))
120 ;; a function computing the constant or literal arguments which are
121 ;; destructively modified by the call.
122 (destroyed-constant-args nil :type (or function null))
123 ;; If true, a special-case LTN annotation method that is used in
124 ;; place of the standard type/policy template selection. It may use
125 ;; arbitrary code to choose a template, decide to do a full call, or
126 ;; conspire with the IR2-CONVERT method to do almost anything. The
127 ;; COMBINATION node is passed as the argument.
128 (ltn-annotate nil :type (or function null))
129 ;; If true, the special-case IR2 conversion method for this
130 ;; function. This deals with funny functions, and anything else that
131 ;; can't be handled using the template mechanism. The COMBINATION
132 ;; node and the IR2-BLOCK are passed as arguments.
133 (ir2-convert nil :type (or function null))
134 ;; If true, the function can stack-allocate the result. The
135 ;; COMBINATION node is passed as an argument.
136 (stack-allocate-result nil :type (or function null))
137 ;; If true, the function can add flow-sensitive type information
138 ;; about the state of the world after its execution. The COMBINATION
139 ;; node is passed as an argument, along with the current set of
140 ;; active constraints for the block. The function returns a
141 ;; sequence of constraints; a constraint is a triplet of a
142 ;; constraint kind (a symbol, see (defstruct (constraint ...)) in
143 ;; constraint.lisp) and arguments, either LVARs, LAMBDA-VARs, or
144 ;; CTYPEs. If any of these arguments is NIL, the constraint is
145 ;; skipped. This simplifies integration with OK-LVAR-LAMBDA-VAR,
146 ;; which maps LVARs to LAMBDA-VARs. An optional fourth value in
147 ;; each constraint flips the meaning of the constraint if it is
148 ;; non-NIL.
149 (constraint-propagate nil :type (or function null))
150 ;; If true, the function can add flow-sensitive type information
151 ;; depending on the truthiness of its return value. Returns two
152 ;; values, a LVAR and a CTYPE. The LVAR is of that CTYPE iff the
153 ;; function returns true.
154 ;; It may also return additional third and fourth values. Each is
155 ;; a sequence of constraints (see CONSTRAINT-PROPAGATE), for the
156 ;; consequent and alternative branches, respectively.
157 (constraint-propagate-if nil :type (or function null))
158 ;; all the templates that could be used to translate this function
159 ;; into IR2, sorted by increasing cost.
160 (templates nil :type list)
161 ;; If non-null, then this function is a unary type predicate for
162 ;; this type.
163 (predicate-type nil :type (or ctype null))
164 ;; If non-null, the index of the argument which becomes the result
165 ;; of the function.
166 (result-arg nil :type (or index null)))
168 (defprinter (fun-info)
169 (attributes :test (not (zerop attributes))
170 :prin1 (decode-ir1-attributes attributes))
171 (transforms :test transforms)
172 (derive-type :test derive-type)
173 (optimizer :test optimizer)
174 (ltn-annotate :test ltn-annotate)
175 (ir2-convert :test ir2-convert)
176 (templates :test templates)
177 (predicate-type :test predicate-type))
179 ;;;; interfaces to defining macros
181 ;;; an IR1 transform
182 (defstruct (transform (:copier nil))
183 ;; the function type which enables this transform.
185 ;; (Note that declaring this :TYPE FUN-TYPE probably wouldn't
186 ;; work because some function types, like (SPECIFIER-TYPE 'FUNCTION0
187 ;; itself, are represented as BUILT-IN-TYPE, and at least as of
188 ;; sbcl-0.pre7.54 or so, that's inconsistent with being a
189 ;; FUN-TYPE.)
190 (type (missing-arg) :type ctype)
191 ;; the transformation function. Takes the COMBINATION node and
192 ;; returns a lambda expression, or throws out.
193 (function (missing-arg) :type function)
194 ;; string used in efficiency notes
195 (note (missing-arg) :type string)
196 ;; T if we should emit a failure note even if SPEED=INHIBIT-WARNINGS.
197 (important nil :type (member t nil)))
199 (defprinter (transform) type note important)
201 ;;; Grab the FUN-INFO and enter the function, replacing any old
202 ;;; one with the same type and note.
203 (declaim (ftype (function (t list function &optional (or string null)
204 (member t nil))
206 %deftransform))
207 (defun %deftransform (name type fun &optional note important)
208 (let* ((ctype (specifier-type type))
209 (note (or note "optimize"))
210 (info (fun-info-or-lose name))
211 (old (find-if (lambda (x)
212 (and (type= (transform-type x) ctype)
213 (string-equal (transform-note x) note)
214 (eq (transform-important x) important)))
215 (fun-info-transforms info))))
216 (cond (old
217 (style-warn 'redefinition-with-deftransform
218 :transform old)
219 (setf (transform-function old) fun
220 (transform-note old) note))
222 (push (make-transform :type ctype :function fun :note note
223 :important important)
224 (fun-info-transforms info))))
225 name))
227 ;;; Make a FUN-INFO structure with the specified type, attributes
228 ;;; and optimizers.
229 (declaim (ftype (function (list list attributes &key
230 (:derive-type (or function null))
231 (:optimizer (or function null))
232 (:destroyed-constant-args (or function null))
233 (:result-arg (or index null))
234 (:overwrite-fndb-silently boolean))
236 %defknown))
237 (defun %defknown (names type attributes
238 &key derive-type optimizer destroyed-constant-args result-arg
239 overwrite-fndb-silently)
240 (let ((ctype (specifier-type type))
241 (info (make-fun-info :attributes attributes
242 :derive-type derive-type
243 :optimizer optimizer
244 :destroyed-constant-args destroyed-constant-args
245 :result-arg result-arg)))
246 (dolist (name names)
247 (unless overwrite-fndb-silently
248 (let ((old-fun-info (info :function :info name)))
249 (when old-fun-info
250 ;; This is handled as an error because it's generally a bad
251 ;; thing to blow away all the old optimization stuff. It's
252 ;; also a potential source of sneaky bugs:
253 ;; DEFKNOWN FOO
254 ;; DEFTRANSFORM FOO
255 ;; DEFKNOWN FOO ; possibly hidden inside some macroexpansion
256 ;; ; Now the DEFTRANSFORM doesn't exist in the target Lisp.
257 ;; However, it's continuable because it might be useful to do
258 ;; it when testing new optimization stuff interactively.
259 (cerror "Go ahead, overwrite it."
260 "~@<overwriting old FUN-INFO ~2I~_~S ~I~_for ~S~:>"
261 old-fun-info name))))
262 (setf (info :function :type name) ctype)
263 (setf (info :function :where-from name) :declared)
264 (setf (info :function :kind name) :function)
265 (setf (info :function :info name) info)))
266 names)
268 ;;; Return the FUN-INFO for NAME or die trying. Since this is
269 ;;; used by callers who want to modify the info, and the info may be
270 ;;; shared, we copy it. We don't have to copy the lists, since each
271 ;;; function that has generators or transforms has already been
272 ;;; through here.
274 ;;; Note that this operation is somewhat garbage-producing in the current
275 ;;; globaldb implementation. Setting a piece of INFO for a name makes
276 ;;; a shallow copy of the name's info-vector. FUN-INFO-OR-LOSE sounds
277 ;;; like a data reader, and you might be disinclined to think that it
278 ;;; copies at all, but:
279 ;;; (TIME (LOOP REPEAT 1000 COUNT (FUN-INFO-OR-LOSE '*)))
280 ;;; 294,160 bytes consed
281 ;;; whereas just copying the info per se is not half as bad:
282 ;;; (LET ((X (INFO :FUNCTION :INFO '*)))
283 ;;; (TIME (LOOP REPEAT 1000 COUNT (COPY-FUN-INFO X))))
284 ;;; 130,992 bytes consed
286 (declaim (ftype (sfunction (t) fun-info) fun-info-or-lose))
287 (defun fun-info-or-lose (name)
288 (let ((old (info :function :info name)))
289 (unless old (error "~S is not a known function." name))
290 (setf (info :function :info name) (copy-fun-info old))))
292 ;;;; generic type inference methods
294 ;;; Derive the type to be the type of the xxx'th arg. This can normally
295 ;;; only be done when the result value is that argument.
296 (defun result-type-first-arg (call)
297 (declare (type combination call))
298 (let ((lvar (first (combination-args call))))
299 (when lvar (lvar-type lvar))))
300 (defun result-type-last-arg (call)
301 (declare (type combination call))
302 (let ((lvar (car (last (combination-args call)))))
303 (when lvar (lvar-type lvar))))
305 ;;; Derive the result type according to the float contagion rules, but
306 ;;; always return a float. This is used for irrational functions that
307 ;;; preserve realness of their arguments.
308 (defun result-type-float-contagion (call)
309 (declare (type combination call))
310 (reduce #'numeric-contagion (combination-args call)
311 :key #'lvar-type
312 :initial-value (specifier-type 'single-float)))
314 ;;; Return a closure usable as a derive-type method for accessing the
315 ;;; N'th argument. If arg is a list, result is a list. If arg is a
316 ;;; vector, result is a vector with the same element type.
317 (defun sequence-result-nth-arg (n)
318 (lambda (call)
319 (declare (type combination call))
320 (let ((lvar (nth (1- n) (combination-args call))))
321 (when lvar
322 (let ((type (lvar-type lvar)))
323 (if (array-type-p type)
324 (specifier-type
325 `(vector ,(type-specifier (array-type-element-type type))))
326 (let ((ltype (specifier-type 'list)))
327 (when (csubtypep type ltype)
328 ltype))))))))
330 ;;; Derive the type to be the type specifier which is the Nth arg.
331 (defun result-type-specifier-nth-arg (n)
332 (lambda (call)
333 (declare (type combination call))
334 (let ((lvar (nth (1- n) (combination-args call))))
335 (when (and lvar (constant-lvar-p lvar))
336 (careful-specifier-type (lvar-value lvar))))))
338 ;;; Derive the type to be the type specifier which is the Nth arg,
339 ;;; with the additional restriptions noted in the CLHS for STRING and
340 ;;; SIMPLE-STRING, defined to specialize on CHARACTER, and for VECTOR
341 ;;; (under the page for MAKE-SEQUENCE).
342 (defun creation-result-type-specifier-nth-arg (n)
343 (lambda (call)
344 (declare (type combination call))
345 (let ((lvar (nth (1- n) (combination-args call))))
346 (when (and lvar (constant-lvar-p lvar))
347 (let* ((specifier (lvar-value lvar))
348 (lspecifier (if (atom specifier) (list specifier) specifier)))
349 (cond
350 ((eq (car lspecifier) 'string)
351 (destructuring-bind (string &rest size)
352 lspecifier
353 (declare (ignore string))
354 (careful-specifier-type
355 `(vector character ,@(when size size)))))
356 ((eq (car lspecifier) 'simple-string)
357 (destructuring-bind (simple-string &rest size)
358 lspecifier
359 (declare (ignore simple-string))
360 (careful-specifier-type
361 `(simple-array character ,@(if size (list size) '((*)))))))
363 (let ((ctype (careful-specifier-type specifier)))
364 (if (and (array-type-p ctype)
365 (eq (array-type-specialized-element-type ctype)
366 *wild-type*))
367 ;; I don't think I'm allowed to modify what I get
368 ;; back from SPECIFIER-TYPE; it is, after all,
369 ;; cached. Better copy it, then.
370 (let ((real-ctype (copy-structure ctype)))
371 (setf (array-type-element-type real-ctype)
372 *universal-type*
373 (array-type-specialized-element-type real-ctype)
374 *universal-type*)
375 real-ctype)
376 ctype)))))))))
378 (defun remove-non-constants-and-nils (fun)
379 (lambda (list)
380 (remove-if-not #'lvar-value
381 (remove-if-not #'constant-lvar-p (funcall fun list)))))
383 ;;; FIXME: bad name (first because it uses 1-based indexing; second
384 ;;; because it doesn't get the nth constant arguments)
385 (defun nth-constant-args (&rest indices)
386 (lambda (list)
387 (let (result)
388 (do ((i 1 (1+ i))
389 (list list (cdr list))
390 (indices indices))
391 ((null indices) (nreverse result))
392 (when (= i (car indices))
393 (when (constant-lvar-p (car list))
394 (push (car list) result))
395 (setf indices (cdr indices)))))))
397 ;;; FIXME: a number of the sequence functions not only do not destroy
398 ;;; their argument if it is empty, but also leave it alone if :start
399 ;;; and :end bound a null sequence, or if :count is 0. This test is a
400 ;;; bit complicated to implement, verging on the impossible, but for
401 ;;; extra points (fill #\1 "abc" :start 0 :end 0) should not cause a
402 ;;; warning.
403 (defun nth-constant-nonempty-sequence-args (&rest indices)
404 (lambda (list)
405 (let (result)
406 (do ((i 1 (1+ i))
407 (list list (cdr list))
408 (indices indices))
409 ((null indices) (nreverse result))
410 (when (= i (car indices))
411 (when (constant-lvar-p (car list))
412 (let ((value (lvar-value (car list))))
413 (unless (or (typep value 'null)
414 (typep value '(vector * 0)))
415 (push (car list) result))))
416 (setf indices (cdr indices)))))))
418 (defun read-elt-type-deriver (skip-arg-p element-type-spec no-hang)
419 (lambda (call)
420 (let* ((element-type (specifier-type element-type-spec))
421 (null-type (specifier-type 'null))
422 (err-args (if skip-arg-p ; for PEEK-CHAR, skip 'peek-type' + 'stream'
423 (cddr (combination-args call))
424 (cdr (combination-args call)))) ; else just 'stream'
425 (eof-error-p (first err-args))
426 (eof-value (second err-args))
427 (unexceptional-type ; the normally returned thing
428 (if (and eof-error-p
429 (types-equal-or-intersect (lvar-type eof-error-p)
430 null-type))
431 ;; (READ-elt stream nil <x>) returns (OR (EQL <x>) elt-type)
432 (type-union (if eof-value (lvar-type eof-value) null-type)
433 element-type)
434 ;; If eof-error is unsupplied, or was but couldn't be nil
435 element-type)))
436 (if no-hang
437 (type-union unexceptional-type null-type)
438 unexceptional-type))))
440 (/show0 "knownfun.lisp end of file")