0.9.2.47:
[sbcl/lichteblau.git] / src / compiler / knownfun.lisp
blob8347fe574606df0f01e44c8605abe8e48938b2b5
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 ;; may be moved with impunity. Has no side effects except possibly
65 ;; consing, and is affected only by its arguments.
67 ;; Since it is not used now, its distribution in fndb.lisp is
68 ;; mere random; use with caution.
69 movable
70 ;; The function is a true predicate likely to be open-coded. Convert
71 ;; any non-conditional uses into (IF <pred> T NIL). Not usually
72 ;; specified to DEFKNOWN, since this is implementation dependent,
73 ;; and is usually automatically set by the DEFINE-VOP :CONDITIONAL
74 ;; option.
75 predicate
76 ;; Inhibit any warning for compiling a recursive definition.
77 ;; (Normally the compiler warns when compiling a recursive
78 ;; definition for a known function, since it might be a botched
79 ;; interpreter stub.)
80 recursive
81 ;; The function does explicit argument type checking, so the
82 ;; declared type should not be asserted when a definition is
83 ;; compiled.
84 explicit-check)
86 (defstruct (fun-info #-sb-xc-host (:pure t))
87 ;; boolean attributes of this function.
88 (attributes (missing-arg) :type attributes)
89 ;; TRANSFORM structures describing transforms for this function
90 (transforms () :type list)
91 ;; a function which computes the derived type for a call to this
92 ;; function by examining the arguments. This is null when there is
93 ;; no special method for this function.
94 (derive-type nil :type (or function null))
95 ;; a function that does various unspecified code transformations by
96 ;; directly hacking the IR. Returns true if further optimizations of
97 ;; the call shouldn't be attempted.
99 ;; KLUDGE: This return convention (non-NIL if you shouldn't do
100 ;; further optimiz'ns) is backwards from the return convention for
101 ;; transforms. -- WHN 19990917
102 (optimizer nil :type (or function null))
103 ;; If true, a special-case LTN annotation method that is used in
104 ;; place of the standard type/policy template selection. It may use
105 ;; arbitrary code to choose a template, decide to do a full call, or
106 ;; conspire with the IR2-CONVERT method to do almost anything. The
107 ;; COMBINATION node is passed as the argument.
108 (ltn-annotate nil :type (or function null))
109 ;; If true, the special-case IR2 conversion method for this
110 ;; function. This deals with funny functions, and anything else that
111 ;; can't be handled using the template mechanism. The COMBINATION
112 ;; node and the IR2-BLOCK are passed as arguments.
113 (ir2-convert nil :type (or function null))
114 ;; If true, the function can stack-allocate the result. The
115 ;; COMBINATION node is passed as an argument.
116 (stack-allocate-result nil :type (or function null))
117 ;; all the templates that could be used to translate this function
118 ;; into IR2, sorted by increasing cost.
119 (templates nil :type list)
120 ;; If non-null, then this function is a unary type predicate for
121 ;; this type.
122 (predicate-type nil :type (or ctype null)))
124 (defprinter (fun-info)
125 (attributes :test (not (zerop attributes))
126 :prin1 (decode-ir1-attributes attributes))
127 (transforms :test transforms)
128 (derive-type :test derive-type)
129 (optimizer :test optimizer)
130 (ltn-annotate :test ltn-annotate)
131 (ir2-convert :test ir2-convert)
132 (templates :test templates)
133 (predicate-type :test predicate-type))
135 ;;;; interfaces to defining macros
137 ;;; an IR1 transform
138 (defstruct (transform (:copier nil))
139 ;; the function type which enables this transform.
141 ;; (Note that declaring this :TYPE FUN-TYPE probably wouldn't
142 ;; work because some function types, like (SPECIFIER-TYPE 'FUNCTION0
143 ;; itself, are represented as BUILT-IN-TYPE, and at least as of
144 ;; sbcl-0.pre7.54 or so, that's inconsistent with being a
145 ;; FUN-TYPE.)
146 (type (missing-arg) :type ctype)
147 ;; the transformation function. Takes the COMBINATION node and
148 ;; returns a lambda expression, or throws out.
149 (function (missing-arg) :type function)
150 ;; string used in efficiency notes
151 (note (missing-arg) :type string)
152 ;; T if we should emit a failure note even if SPEED=INHIBIT-WARNINGS.
153 (important nil :type (member t nil)))
155 (defprinter (transform) type note important)
157 ;;; Grab the FUN-INFO and enter the function, replacing any old
158 ;;; one with the same type and note.
159 (declaim (ftype (function (t list function &optional (or string null)
160 (member t nil))
162 %deftransform))
163 (defun %deftransform (name type fun &optional note important)
164 (let* ((ctype (specifier-type type))
165 (note (or note "optimize"))
166 (info (fun-info-or-lose name))
167 (old (find-if (lambda (x)
168 (and (type= (transform-type x) ctype)
169 (string-equal (transform-note x) note)
170 (eq (transform-important x) important)))
171 (fun-info-transforms info))))
172 (cond (old
173 (style-warn "Overwriting ~S" old)
174 (setf (transform-function old) fun
175 (transform-note old) note))
177 (push (make-transform :type ctype :function fun :note note
178 :important important)
179 (fun-info-transforms info))))
180 name))
182 ;;; Make a FUN-INFO structure with the specified type, attributes
183 ;;; and optimizers.
184 (declaim (ftype (function (list list attributes &key
185 (:derive-type (or function null))
186 (:optimizer (or function null)))
188 %defknown))
189 (defun %defknown (names type attributes &key derive-type optimizer)
190 (let ((ctype (specifier-type type))
191 (info (make-fun-info :attributes attributes
192 :derive-type derive-type
193 :optimizer optimizer))
194 (target-env *info-environment*))
195 (dolist (name names)
196 (let ((old-fun-info (info :function :info name)))
197 (when old-fun-info
198 ;; This is handled as an error because it's generally a bad
199 ;; thing to blow away all the old optimization stuff. It's
200 ;; also a potential source of sneaky bugs:
201 ;; DEFKNOWN FOO
202 ;; DEFTRANSFORM FOO
203 ;; DEFKNOWN FOO ; possibly hidden inside some macroexpansion
204 ;; ; Now the DEFTRANSFORM doesn't exist in the target Lisp.
205 ;; However, it's continuable because it might be useful to do
206 ;; it when testing new optimization stuff interactively.
207 (cerror "Go ahead, overwrite it."
208 "~@<overwriting old FUN-INFO ~2I~_~S ~I~_for ~S~:>"
209 old-fun-info name)))
210 (setf (info :function :type name target-env) ctype)
211 (setf (info :function :where-from name target-env) :declared)
212 (setf (info :function :kind name target-env) :function)
213 (setf (info :function :info name target-env) info)))
214 names)
216 ;;; Return the FUN-INFO for NAME or die trying. Since this is
217 ;;; used by callers who want to modify the info, and the info may be
218 ;;; shared, we copy it. We don't have to copy the lists, since each
219 ;;; function that has generators or transforms has already been
220 ;;; through here.
221 (declaim (ftype (sfunction (t) fun-info) fun-info-or-lose))
222 (defun fun-info-or-lose (name)
223 (let (;; FIXME: Do we need this rebinding here? It's a literal
224 ;; translation of the old CMU CL rebinding to
225 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
226 ;; and it's not obvious whether the rebinding to itself is
227 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
228 (*info-environment* *info-environment*))
229 (let ((old (info :function :info name)))
230 (unless old (error "~S is not a known function." name))
231 (setf (info :function :info name) (copy-fun-info old)))))
233 ;;;; generic type inference methods
235 ;;; Derive the type to be the type of the xxx'th arg. This can normally
236 ;;; only be done when the result value is that argument.
237 (defun result-type-first-arg (call)
238 (declare (type combination call))
239 (let ((lvar (first (combination-args call))))
240 (when lvar (lvar-type lvar))))
241 (defun result-type-last-arg (call)
242 (declare (type combination call))
243 (let ((lvar (car (last (combination-args call)))))
244 (when lvar (lvar-type lvar))))
246 ;;; Derive the result type according to the float contagion rules, but
247 ;;; always return a float. This is used for irrational functions that
248 ;;; preserve realness of their arguments.
249 (defun result-type-float-contagion (call)
250 (declare (type combination call))
251 (reduce #'numeric-contagion (combination-args call)
252 :key #'lvar-type
253 :initial-value (specifier-type 'single-float)))
255 ;;; Return a closure usable as a derive-type method for accessing the
256 ;;; N'th argument. If arg is a list, result is a list. If arg is a
257 ;;; vector, result is a vector with the same element type.
258 (defun sequence-result-nth-arg (n)
259 (lambda (call)
260 (declare (type combination call))
261 (let ((lvar (nth (1- n) (combination-args call))))
262 (when lvar
263 (let ((type (lvar-type lvar)))
264 (if (array-type-p type)
265 (specifier-type
266 `(vector ,(type-specifier (array-type-element-type type))))
267 (let ((ltype (specifier-type 'list)))
268 (when (csubtypep type ltype)
269 ltype))))))))
271 ;;; Derive the type to be the type specifier which is the Nth arg.
272 (defun result-type-specifier-nth-arg (n)
273 (lambda (call)
274 (declare (type combination call))
275 (let ((lvar (nth (1- n) (combination-args call))))
276 (when (and lvar (constant-lvar-p lvar))
277 (careful-specifier-type (lvar-value lvar))))))
279 ;;; Derive the type to be the type specifier which is the Nth arg,
280 ;;; with the additional restriptions noted in the CLHS for STRING and
281 ;;; SIMPLE-STRING, defined to specialize on CHARACTER, and for VECTOR
282 ;;; (under the page for MAKE-SEQUENCE).
283 (defun creation-result-type-specifier-nth-arg (n)
284 (lambda (call)
285 (declare (type combination call))
286 (let ((lvar (nth (1- n) (combination-args call))))
287 (when (and lvar (constant-lvar-p lvar))
288 (let* ((specifier (lvar-value lvar))
289 (lspecifier (if (atom specifier) (list specifier) specifier)))
290 (cond
291 ((eq (car lspecifier) 'string)
292 (destructuring-bind (string &rest size)
293 lspecifier
294 (declare (ignore string))
295 (careful-specifier-type
296 `(vector character ,@(when size size)))))
297 ((eq (car lspecifier) 'simple-string)
298 (destructuring-bind (simple-string &rest size)
299 lspecifier
300 (declare (ignore simple-string))
301 (careful-specifier-type
302 `(simple-array character ,@(if size (list size) '((*)))))))
304 (let ((ctype (careful-specifier-type specifier)))
305 (if (and (array-type-p ctype)
306 (eq (array-type-specialized-element-type ctype)
307 *wild-type*))
308 ;; I don't think I'm allowed to modify what I get
309 ;; back from SPECIFIER-TYPE; it is, after all,
310 ;; cached. Better copy it, then.
311 (let ((real-ctype (copy-structure ctype)))
312 (setf (array-type-element-type real-ctype)
313 *universal-type*
314 (array-type-specialized-element-type real-ctype)
315 *universal-type*)
316 real-ctype)
317 ctype)))))))))
319 (/show0 "knownfun.lisp end of file")