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 ;;; IR1 boolean function attributes
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.
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
38 ;; may fail to return during correct execution. Errors are O.K.
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.
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.
51 ;; may be eliminated if value is unused. The function has no side
52 ;; effects except possibly CONS. If a function is defined to signal
53 ;; errors, then it is not flushable even if it is movable or
56 ;; may be moved with impunity. Has no side effects except possibly
57 ;; consing, and is affected only by its arguments.
59 ;; The function is a true predicate likely to be open-coded. Convert
60 ;; any non-conditional uses into (IF <pred> T NIL).
62 ;; Inhibit any warning for compiling a recursive definition.
63 ;; (Normally the compiler warns when compiling a recursive
64 ;; definition for a known function, since it might be a botched
67 ;; The function does explicit argument type checking, so the
68 ;; declared type should not be asserted when a definition is
72 (defstruct (fun-info #-sb-xc-host (:pure t))
73 ;; boolean attributes of this function.
74 (attributes (missing-arg) :type attributes)
75 ;; TRANSFORM structures describing transforms for this function
76 (transforms () :type list)
77 ;; a function which computes the derived type for a call to this
78 ;; function by examining the arguments. This is null when there is
79 ;; no special method for this function.
80 (derive-type nil :type (or function null))
81 ;; a function that does various unspecified code transformations by
82 ;; directly hacking the IR. Returns true if further optimizations of
83 ;; the call shouldn't be attempted.
85 ;; KLUDGE: This return convention (non-NIL if you shouldn't do
86 ;; further optimiz'ns) is backwards from the return convention for
87 ;; transforms. -- WHN 19990917
88 (optimizer nil :type (or function null))
89 ;; If true, a special-case LTN annotation method that is used in
90 ;; place of the standard type/policy template selection. It may use
91 ;; arbitrary code to choose a template, decide to do a full call, or
92 ;; conspire with the IR2-Convert method to do almost anything. The
93 ;; Combination node is passed as the argument.
94 (ltn-annotate nil :type (or function null))
95 ;; If true, the special-case IR2 conversion method for this
96 ;; function. This deals with funny functions, and anything else that
97 ;; can't be handled using the template mechanism. The Combination
98 ;; node and the IR2-Block are passed as arguments.
99 (ir2-convert nil :type (or function null))
100 ;; all the templates that could be used to translate this function
101 ;; into IR2, sorted by increasing cost.
102 (templates nil :type list)
103 ;; If non-null, then this function is a unary type predicate for
105 (predicate-type nil :type (or ctype null))
106 ;; If non-null, use this function to annotate the known call for the
107 ;; byte compiler. If it returns NIL, then change the call to :full.
108 (byte-annotate nil :type (or function null)))
110 (defprinter (fun-info)
111 (transforms :test transforms)
112 (derive-type :test derive-type)
113 (optimizer :test optimizer)
114 (ltn-annotate :test ltn-annotate)
115 (ir2-convert :test ir2-convert)
116 (templates :test templates)
117 (predicate-type :test predicate-type)
118 (byte-annotate :test byte-annotate))
120 ;;;; interfaces to defining macros
123 (defstruct (transform (:copier nil))
124 ;; the function type which enables this transform.
126 ;; (Note that declaring this :TYPE FUN-TYPE probably wouldn't
127 ;; work because some function types, like (SPECIFIER-TYPE 'FUNCTION0
128 ;; itself, are represented as BUILT-IN-TYPE, and at least as of
129 ;; sbcl-0.pre7.54 or so, that's inconsistent with being a
131 (type (missing-arg) :type ctype)
132 ;; the transformation function. Takes the COMBINATION node and
133 ;; returns a lambda expression, or throws out.
134 (function (missing-arg) :type function)
135 ;; string used in efficiency notes
136 (note (missing-arg) :type string)
137 ;; T if we should emit a failure note even if SPEED=INHIBIT-WARNINGS.
138 (important nil :type (member t nil))
139 ;; usable for byte code, native code, or both?
141 ;; FIXME: Now that there's no byte compiler, this is stale and could
143 (when :native :type (member :byte :native :both)))
145 (defprinter (transform) type note important when)
147 ;;; Grab the FUN-INFO and enter the function, replacing any old
148 ;;; one with the same type and note.
149 (declaim (ftype (function (t list function &optional (or string null)
150 (member t nil) (member :native :byte :both))
153 (defun %deftransform (name type fun &optional note important (when :native))
154 (let* ((ctype (specifier-type type))
155 (note (or note "optimize"))
156 (info (fun-info-or-lose name))
157 (old (find-if (lambda (x)
158 (and (type= (transform-type x) ctype)
159 (string-equal (transform-note x) note)
160 (eq (transform-important x) important)
161 (eq (transform-when x) when)))
162 (fun-info-transforms info))))
164 (setf (transform-function old) fun
165 (transform-note old) note)
166 (push (make-transform :type ctype :function fun :note note
167 :important important :when when)
168 (fun-info-transforms info)))
171 ;;; Make a FUN-INFO structure with the specified type, attributes
173 (declaim (ftype (function (list list attributes &key
174 (:derive-type (or function null))
175 (:optimizer (or function null)))
178 (defun %defknown (names type attributes &key derive-type optimizer)
179 (let ((ctype (specifier-type type))
180 (info (make-fun-info :attributes attributes
181 :derive-type derive-type
182 :optimizer optimizer))
183 (target-env *info-environment*))
185 (let ((old-fun-info (info :function :info name)))
187 ;; This is handled as an error because it's generally a bad
188 ;; thing to blow away all the old optimization stuff. It's
189 ;; also a potential source of sneaky bugs:
192 ;; DEFKNOWN FOO ; possibly hidden inside some macroexpansion
193 ;; ; Now the DEFTRANSFORM doesn't exist in the target Lisp.
194 ;; However, it's continuable because it might be useful to do
195 ;; it when testing new optimization stuff interactively.
196 (cerror "Go ahead, overwrite it."
197 "~@<overwriting old FUN-INFO ~2I~_~S ~I~_for ~S~:>"
199 (setf (info :function :type name target-env) ctype)
200 (setf (info :function :where-from name target-env) :declared)
201 (setf (info :function :kind name target-env) :function)
202 (setf (info :function :info name target-env) info)))
205 ;;; Return the FUN-INFO for NAME or die trying. Since this is
206 ;;; used by callers who want to modify the info, and the info may be
207 ;;; shared, we copy it. We don't have to copy the lists, since each
208 ;;; function that has generators or transforms has already been
210 (declaim (ftype (function (t) fun-info) fun-info-or-lose))
211 (defun fun-info-or-lose (name)
212 (let (;; FIXME: Do we need this rebinding here? It's a literal
213 ;; translation of the old CMU CL rebinding to
214 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
215 ;; and it's not obvious whether the rebinding to itself is
216 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
217 (*info-environment* *info-environment*))
218 (let ((old (info :function :info name)))
219 (unless old (error "~S is not a known function." name))
220 (setf (info :function :info name) (copy-fun-info old)))))
222 ;;;; generic type inference methods
224 ;;; Derive the type to be the type of the xxx'th arg. This can normally
225 ;;; only be done when the result value is that argument.
226 (defun result-type-first-arg (call)
227 (declare (type combination call))
228 (let ((cont (first (combination-args call))))
229 (when cont (continuation-type cont))))
230 (defun result-type-last-arg (call)
231 (declare (type combination call))
232 (let ((cont (car (last (combination-args call)))))
233 (when cont (continuation-type cont))))
235 ;;; Derive the result type according to the float contagion rules, but
236 ;;; always return a float. This is used for irrational functions that
237 ;;; preserve realness of their arguments.
238 (defun result-type-float-contagion (call)
239 (declare (type combination call))
240 (reduce #'numeric-contagion (combination-args call)
241 :key #'continuation-type
242 :initial-value (specifier-type 'single-float)))
244 ;;; Return a closure usable as a derive-type method for accessing the
245 ;;; N'th argument. If arg is a list, result is a list. If arg is a
246 ;;; vector, result is a vector with the same element type.
247 (defun sequence-result-nth-arg (n)
249 (declare (type combination call))
250 (let ((cont (nth (1- n) (combination-args call))))
252 (let ((type (continuation-type cont)))
253 (if (array-type-p type)
255 `(vector ,(type-specifier (array-type-element-type type))))
256 (let ((ltype (specifier-type 'list)))
257 (when (csubtypep type ltype)
260 ;;; Derive the type to be the type specifier which is the N'th arg.
261 (defun result-type-specifier-nth-arg (n)
263 (declare (type combination call))
264 (let ((cont (nth (1- n) (combination-args call))))
265 (when (and cont (constant-continuation-p cont))
266 (specifier-type (continuation-value cont))))))
268 (/show0 "knownfun.lisp end of file")