1.0.18.16: many STYLE-WARNING changes.
[sbcl/pkhuong.git] / src / compiler / knownfun.lisp
blob863dbb395e5e0ece6f2ddbc569bc62a18de73277
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 (!def-boolean-attribute ir1
29 ;; may call functions that are passed as arguments. In order to
30 ;; determine what other effects are present, we must find the
31 ;; effects of all arguments that may be functions.
32 call
33 ;; may incorporate function or number arguments into the result or
34 ;; somehow pass them upward. Note that this applies to any argument
35 ;; that *might* be a function or number, not just the arguments that
36 ;; always are.
37 unsafe
38 ;; may fail to return during correct execution. Errors are O.K.
39 unwind
40 ;; the (default) worst case. Includes all the other bad things, plus
41 ;; any other possible bad thing. If this is present, the above bad
42 ;; attributes will be explicitly present as well.
43 any
44 ;; may be constant-folded. The function has no side effects, but may
45 ;; be affected by side effects on the arguments. e.g. SVREF, MAPC.
46 ;; Functions that side-effect their arguments are not considered to
47 ;; be foldable. Although it would be "legal" to constant fold them
48 ;; (since it "is an error" to modify a constant), we choose not to
49 ;; mark these functions as foldable in this database.
50 foldable
51 ;; may be eliminated if value is unused. The function has no side
52 ;; effects except possibly cons. If a function might signal errors,
53 ;; then it is not flushable even if it is movable, foldable or
54 ;; unsafely-flushable. Implies UNSAFELY-FLUSHABLE. (In safe code
55 ;; type checking of arguments is always performed by the caller, so
56 ;; a function which SHOULD signal an error if arguments are not of
57 ;; declared types may be FLUSHABLE.)
58 flushable
59 ;; unsafe call may be eliminated if value is unused. The function
60 ;; has no side effects except possibly cons and signalling an error
61 ;; in the safe code. If a function MUST signal errors, then it is
62 ;; not unsafely-flushable even if it is movable or foldable.
63 unsafely-flushable
64 ;; return value is important, and ignoring it is probably a mistake.
65 ;; Unlike the other attributes, this is used only for style
66 ;; warnings and has no effect on optimization.
67 important-result
68 ;; may be moved with impunity. Has no side effects except possibly
69 ;; consing, and is affected only by its arguments.
71 ;; Since it is not used now, its distribution in fndb.lisp is
72 ;; mere random; use with caution.
73 movable
74 ;; The function is a true predicate likely to be open-coded. Convert
75 ;; any non-conditional uses into (IF <pred> T NIL). Not usually
76 ;; specified to DEFKNOWN, since this is implementation dependent,
77 ;; and is usually automatically set by the DEFINE-VOP :CONDITIONAL
78 ;; option.
79 predicate
80 ;; Inhibit any warning for compiling a recursive definition.
81 ;; (Normally the compiler warns when compiling a recursive
82 ;; definition for a known function, since it might be a botched
83 ;; interpreter stub.)
84 recursive
85 ;; The function does explicit argument type checking, so the
86 ;; declared type should not be asserted when a definition is
87 ;; compiled.
88 explicit-check
89 ;; The function should always be translated by a VOP (i.e. it should
90 ;; should never be converted into a full call). This is used strictly
91 ;; as a consistency checking mechanism inside the compiler during IR2
92 ;; transformation.
93 always-translatable)
95 (defstruct (fun-info #-sb-xc-host (:pure t))
96 ;; boolean attributes of this function.
97 (attributes (missing-arg) :type attributes)
98 ;; TRANSFORM structures describing transforms for this function
99 (transforms () :type list)
100 ;; a function which computes the derived type for a call to this
101 ;; function by examining the arguments. This is null when there is
102 ;; no special method for this function.
103 (derive-type nil :type (or function null))
104 ;; a function that does various unspecified code transformations by
105 ;; directly hacking the IR. Returns true if further optimizations of
106 ;; the call shouldn't be attempted.
108 ;; KLUDGE: This return convention (non-NIL if you shouldn't do
109 ;; further optimiz'ns) is backwards from the return convention for
110 ;; transforms. -- WHN 19990917
111 (optimizer nil :type (or function null))
112 ;; a function computing the constant or literal arguments which are
113 ;; destructively modified by the call.
114 (destroyed-constant-args nil :type (or function null))
115 ;; If true, a special-case LTN annotation method that is used in
116 ;; place of the standard type/policy template selection. It may use
117 ;; arbitrary code to choose a template, decide to do a full call, or
118 ;; conspire with the IR2-CONVERT method to do almost anything. The
119 ;; COMBINATION node is passed as the argument.
120 (ltn-annotate nil :type (or function null))
121 ;; If true, the special-case IR2 conversion method for this
122 ;; function. This deals with funny functions, and anything else that
123 ;; can't be handled using the template mechanism. The COMBINATION
124 ;; node and the IR2-BLOCK are passed as arguments.
125 (ir2-convert nil :type (or function null))
126 ;; If true, the function can stack-allocate the result. The
127 ;; COMBINATION node is passed as an argument.
128 (stack-allocate-result nil :type (or function null))
129 ;; all the templates that could be used to translate this function
130 ;; into IR2, sorted by increasing cost.
131 (templates nil :type list)
132 ;; If non-null, then this function is a unary type predicate for
133 ;; this type.
134 (predicate-type nil :type (or ctype null)))
136 (defprinter (fun-info)
137 (attributes :test (not (zerop attributes))
138 :prin1 (decode-ir1-attributes attributes))
139 (transforms :test transforms)
140 (derive-type :test derive-type)
141 (optimizer :test optimizer)
142 (ltn-annotate :test ltn-annotate)
143 (ir2-convert :test ir2-convert)
144 (templates :test templates)
145 (predicate-type :test predicate-type))
147 ;;;; interfaces to defining macros
149 ;;; an IR1 transform
150 (defstruct (transform (:copier nil))
151 ;; the function type which enables this transform.
153 ;; (Note that declaring this :TYPE FUN-TYPE probably wouldn't
154 ;; work because some function types, like (SPECIFIER-TYPE 'FUNCTION0
155 ;; itself, are represented as BUILT-IN-TYPE, and at least as of
156 ;; sbcl-0.pre7.54 or so, that's inconsistent with being a
157 ;; FUN-TYPE.)
158 (type (missing-arg) :type ctype)
159 ;; the transformation function. Takes the COMBINATION node and
160 ;; returns a lambda expression, or throws out.
161 (function (missing-arg) :type function)
162 ;; string used in efficiency notes
163 (note (missing-arg) :type string)
164 ;; T if we should emit a failure note even if SPEED=INHIBIT-WARNINGS.
165 (important nil :type (member t nil)))
167 (defprinter (transform) type note important)
169 ;;; Grab the FUN-INFO and enter the function, replacing any old
170 ;;; one with the same type and note.
171 (declaim (ftype (function (t list function &optional (or string null)
172 (member t nil))
174 %deftransform))
175 (defun %deftransform (name type fun &optional note important)
176 (let* ((ctype (specifier-type type))
177 (note (or note "optimize"))
178 (info (fun-info-or-lose name))
179 (old (find-if (lambda (x)
180 (and (type= (transform-type x) ctype)
181 (string-equal (transform-note x) note)
182 (eq (transform-important x) important)))
183 (fun-info-transforms info))))
184 (cond (old
185 (style-warn 'sb!kernel:redefinition-with-deftransform
186 :transform old)
187 (setf (transform-function old) fun
188 (transform-note old) note))
190 (push (make-transform :type ctype :function fun :note note
191 :important important)
192 (fun-info-transforms info))))
193 name))
195 ;;; Make a FUN-INFO structure with the specified type, attributes
196 ;;; and optimizers.
197 (declaim (ftype (function (list list attributes &key
198 (:derive-type (or function null))
199 (:optimizer (or function null))
200 (:destroyed-constant-args (or function null)))
202 %defknown))
203 (defun %defknown (names type attributes &key derive-type optimizer destroyed-constant-args)
204 (let ((ctype (specifier-type type))
205 (info (make-fun-info :attributes attributes
206 :derive-type derive-type
207 :optimizer optimizer
208 :destroyed-constant-args destroyed-constant-args))
209 (target-env *info-environment*))
210 (dolist (name names)
211 (let ((old-fun-info (info :function :info name)))
212 (when old-fun-info
213 ;; This is handled as an error because it's generally a bad
214 ;; thing to blow away all the old optimization stuff. It's
215 ;; also a potential source of sneaky bugs:
216 ;; DEFKNOWN FOO
217 ;; DEFTRANSFORM FOO
218 ;; DEFKNOWN FOO ; possibly hidden inside some macroexpansion
219 ;; ; Now the DEFTRANSFORM doesn't exist in the target Lisp.
220 ;; However, it's continuable because it might be useful to do
221 ;; it when testing new optimization stuff interactively.
222 (cerror "Go ahead, overwrite it."
223 "~@<overwriting old FUN-INFO ~2I~_~S ~I~_for ~S~:>"
224 old-fun-info name)))
225 (setf (info :function :type name target-env) ctype)
226 (setf (info :function :where-from name target-env) :declared)
227 (setf (info :function :kind name target-env) :function)
228 (setf (info :function :info name target-env) info)))
229 names)
231 ;;; Return the FUN-INFO for NAME or die trying. Since this is
232 ;;; used by callers who want to modify the info, and the info may be
233 ;;; shared, we copy it. We don't have to copy the lists, since each
234 ;;; function that has generators or transforms has already been
235 ;;; through here.
236 (declaim (ftype (sfunction (t) fun-info) fun-info-or-lose))
237 (defun fun-info-or-lose (name)
238 (let (;; FIXME: Do we need this rebinding here? It's a literal
239 ;; translation of the old CMU CL rebinding to
240 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
241 ;; and it's not obvious whether the rebinding to itself is
242 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
243 (*info-environment* *info-environment*))
244 (let ((old (info :function :info name)))
245 (unless old (error "~S is not a known function." name))
246 (setf (info :function :info name) (copy-fun-info old)))))
248 ;;;; generic type inference methods
250 ;;; Derive the type to be the type of the xxx'th arg. This can normally
251 ;;; only be done when the result value is that argument.
252 (defun result-type-first-arg (call)
253 (declare (type combination call))
254 (let ((lvar (first (combination-args call))))
255 (when lvar (lvar-type lvar))))
256 (defun result-type-last-arg (call)
257 (declare (type combination call))
258 (let ((lvar (car (last (combination-args call)))))
259 (when lvar (lvar-type lvar))))
261 ;;; Derive the result type according to the float contagion rules, but
262 ;;; always return a float. This is used for irrational functions that
263 ;;; preserve realness of their arguments.
264 (defun result-type-float-contagion (call)
265 (declare (type combination call))
266 (reduce #'numeric-contagion (combination-args call)
267 :key #'lvar-type
268 :initial-value (specifier-type 'single-float)))
270 ;;; Return a closure usable as a derive-type method for accessing the
271 ;;; N'th argument. If arg is a list, result is a list. If arg is a
272 ;;; vector, result is a vector with the same element type.
273 (defun sequence-result-nth-arg (n)
274 (lambda (call)
275 (declare (type combination call))
276 (let ((lvar (nth (1- n) (combination-args call))))
277 (when lvar
278 (let ((type (lvar-type lvar)))
279 (if (array-type-p type)
280 (specifier-type
281 `(vector ,(type-specifier (array-type-element-type type))))
282 (let ((ltype (specifier-type 'list)))
283 (when (csubtypep type ltype)
284 ltype))))))))
286 ;;; Derive the type to be the type specifier which is the Nth arg.
287 (defun result-type-specifier-nth-arg (n)
288 (lambda (call)
289 (declare (type combination call))
290 (let ((lvar (nth (1- n) (combination-args call))))
291 (when (and lvar (constant-lvar-p lvar))
292 (careful-specifier-type (lvar-value lvar))))))
294 ;;; Derive the type to be the type specifier which is the Nth arg,
295 ;;; with the additional restriptions noted in the CLHS for STRING and
296 ;;; SIMPLE-STRING, defined to specialize on CHARACTER, and for VECTOR
297 ;;; (under the page for MAKE-SEQUENCE).
298 (defun creation-result-type-specifier-nth-arg (n)
299 (lambda (call)
300 (declare (type combination call))
301 (let ((lvar (nth (1- n) (combination-args call))))
302 (when (and lvar (constant-lvar-p lvar))
303 (let* ((specifier (lvar-value lvar))
304 (lspecifier (if (atom specifier) (list specifier) specifier)))
305 (cond
306 ((eq (car lspecifier) 'string)
307 (destructuring-bind (string &rest size)
308 lspecifier
309 (declare (ignore string))
310 (careful-specifier-type
311 `(vector character ,@(when size size)))))
312 ((eq (car lspecifier) 'simple-string)
313 (destructuring-bind (simple-string &rest size)
314 lspecifier
315 (declare (ignore simple-string))
316 (careful-specifier-type
317 `(simple-array character ,@(if size (list size) '((*)))))))
319 (let ((ctype (careful-specifier-type specifier)))
320 (if (and (array-type-p ctype)
321 (eq (array-type-specialized-element-type ctype)
322 *wild-type*))
323 ;; I don't think I'm allowed to modify what I get
324 ;; back from SPECIFIER-TYPE; it is, after all,
325 ;; cached. Better copy it, then.
326 (let ((real-ctype (copy-structure ctype)))
327 (setf (array-type-element-type real-ctype)
328 *universal-type*
329 (array-type-specialized-element-type real-ctype)
330 *universal-type*)
331 real-ctype)
332 ctype)))))))))
334 (defun remove-non-constants-and-nils (fun)
335 (lambda (list)
336 (remove-if-not #'lvar-value
337 (remove-if-not #'constant-lvar-p (funcall fun list)))))
339 ;;; FIXME: bad name (first because it uses 1-based indexing; second
340 ;;; because it doesn't get the nth constant arguments)
341 (defun nth-constant-args (&rest indices)
342 (lambda (list)
343 (let (result)
344 (do ((i 1 (1+ i))
345 (list list (cdr list))
346 (indices indices))
347 ((null indices) (nreverse result))
348 (when (= i (car indices))
349 (when (constant-lvar-p (car list))
350 (push (car list) result))
351 (setf indices (cdr indices)))))))
353 ;;; FIXME: a number of the sequence functions not only do not destroy
354 ;;; their argument if it is empty, but also leave it alone if :start
355 ;;; and :end bound a null sequence, or if :count is 0. This test is a
356 ;;; bit complicated to implement, verging on the impossible, but for
357 ;;; extra points (fill #\1 "abc" :start 0 :end 0) should not cause a
358 ;;; warning.
359 (defun nth-constant-nonempty-sequence-args (&rest indices)
360 (lambda (list)
361 (let (result)
362 (do ((i 1 (1+ i))
363 (list list (cdr list))
364 (indices indices))
365 ((null indices) (nreverse result))
366 (when (= i (car indices))
367 (when (constant-lvar-p (car list))
368 (let ((value (lvar-value (car list))))
369 (unless (or (typep value 'null)
370 (typep value '(vector * 0)))
371 (push (car list) result))))
372 (setf indices (cdr indices)))))))
374 (/show0 "knownfun.lisp end of file")