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.
20 ;;; IR1 boolean function attributes
22 ;;; There are a number of boolean attributes of known functions which we like
23 ;;; to have in IR1. This information is mostly side effect information of a
24 ;;; sort, but it is different from the kind of information we want in IR2. We
25 ;;; aren't interested in a fine breakdown of side effects, since we do very
26 ;;; little code motion on IR1. We are interested in some deeper semantic
27 ;;; properties such as 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 determine
30 ;; what other effects are present, we must find the effects of all arguments
31 ;; that may be functions.
33 ;; May incorporate function or number arguments into the result or somehow
34 ;; pass them upward. Note that this applies to any argument that *might* be
35 ;; a function or number, not just the arguments that always are.
37 ;; May fail to return during correct execution. Errors are O.K.
39 ;; The (default) worst case. Includes all the other bad things, plus any
40 ;; other possible bad thing. If this is present, the above bad attributes
41 ;; will be explicitly present as well.
43 ;; May be constant-folded. The function has no side effects, but may be
44 ;; affected by side effects on the arguments. e.g. SVREF, MAPC. Functions
45 ;; that side-effect their arguments are not considered to be foldable.
46 ;; Although it would be "legal" to constant fold them (since it "is an error"
47 ;; to modify a constant), we choose not to mark these functions as foldable
50 ;; May be eliminated if value is unused. The function has no side effects
51 ;; except possibly CONS. If a function is defined to signal errors, then it
52 ;; is not flushable even if it is movable or foldable.
54 ;; May be moved with impunity. Has no side effects except possibly CONS, and
55 ;; is affected only by its arguments.
57 ;; Function is a true predicate likely to be open-coded. Convert any
58 ;; non-conditional uses into (IF <pred> T NIL).
60 ;; Inhibit any warning for compiling a recursive definition. (Normally the
61 ;; compiler warns when compiling a recursive definition for a known function,
62 ;; since it might be a botched interpreter stub.)
64 ;; Function does explicit argument type checking, so the declared type should
65 ;; not be asserted when a definition is compiled.
68 (defstruct (function-info #-sb-xc-host (:pure t))
69 ;; Boolean attributes of this function.
70 (attributes (required-argument) :type attributes)
71 ;; A list of Transform structures describing transforms for this function.
72 (transforms () :type list)
73 ;; A function which computes the derived type for a call to this function by
74 ;; examining the arguments. This is null when there is no special method for
76 (derive-type nil :type (or function null))
77 ;; A function that does various unspecified code transformations by directly
78 ;; hacking the IR. Returns true if further optimizations of the call
79 ;; shouldn't be attempted.
81 ;; KLUDGE: This return convention (non-NIL if you shouldn't do further
82 ;; optimiz'ns) is backwards from the return convention for transforms.
84 (optimizer nil :type (or function null))
85 ;; If true, a special-case LTN annotation method that is used in place of the
86 ;; standard type/policy template selection. It may use arbitrary code to
87 ;; choose a template, decide to do a full call, or conspire with the
88 ;; IR2-Convert method to do almost anything. The Combination node is passed
90 (ltn-annotate nil :type (or function null))
91 ;; If true, the special-case IR2 conversion method for this function. This
92 ;; deals with funny functions, and anything else that can't be handled using
93 ;; the template mechanism. The Combination node and the IR2-Block are passed
95 (ir2-convert nil :type (or function null))
96 ;; A list of all the templates that could be used to translate this function
97 ;; into IR2, sorted by increasing cost.
98 (templates nil :type list)
99 ;; If non-null, then this function is a unary type predicate for this type.
100 (predicate-type nil :type (or ctype null))
101 ;; If non-null, use this function to annotate the known call for the byte
102 ;; compiler. If it returns NIL, then change the call to :full.
103 (byte-annotate nil :type (or function null))
104 ;; If non-null, use this function to generate the byte code for this known
105 ;; call. This function can only give up if there is a byte-annotate function
106 ;; that arranged for the functional to be pushed onto the stack.
107 (byte-compile nil :type (or function null)))
109 (defprinter (function-info)
110 (transforms :test transforms)
111 (derive-type :test derive-type)
112 (optimizer :test optimizer)
113 (ltn-annotate :test ltn-annotate)
114 (ir2-convert :test ir2-convert)
115 (templates :test templates)
116 (predicate-type :test predicate-type)
117 (byte-annotate :test byte-annotate)
118 (byte-compile :test byte-compile))
120 ;;;; interfaces to defining macros
122 ;;; The TRANSFORM structure represents an IR1 transform.
124 ;; The function-type which enables this transform.
125 (type (required-argument) :type ctype)
126 ;; The transformation function. Takes the Combination node and Returns a
127 ;; lambda, or throws out.
128 (function (required-argument) :type function)
129 ;; String used in efficency notes.
130 (note (required-argument) :type string)
131 ;; T if we should spew a failure note even if speed=brevity.
132 (important nil :type (member t nil))
133 ;; Usable for byte code, native code, or both.
134 (when :native :type (member :byte :native :both)))
136 (defprinter (transform) type note important when)
138 ;;; Grab the FUNCTION-INFO and enter the function, replacing any old
139 ;;; one with the same type and note.
140 (declaim (ftype (function (t list function &optional (or string null)
141 (member t nil) (member :native :byte :both))
144 (defun %deftransform (name type fun &optional note important (when :native))
145 (let* ((ctype (specifier-type type))
146 (note (or note "optimize"))
147 (info (function-info-or-lose name))
148 (old (find-if (lambda (x)
149 (and (type= (transform-type x) ctype)
150 (string-equal (transform-note x) note)
151 (eq (transform-important x) important)
152 (eq (transform-when x) when)))
153 (function-info-transforms info))))
155 (setf (transform-function old) fun (transform-note old) note)
156 (push (make-transform :type ctype :function fun :note note
157 :important important :when when)
158 (function-info-transforms info)))
161 ;;; Make a FUNCTION-INFO structure with the specified type, attributes
163 (declaim (ftype (function (list list attributes &key
164 (:derive-type (or function null))
165 (:optimizer (or function null)))
168 (defun %defknown (names type attributes &key derive-type optimizer)
169 (let ((ctype (specifier-type type))
170 (info (make-function-info :attributes attributes
171 :derive-type derive-type
172 :optimizer optimizer))
173 (target-env (or *backend-info-environment* *info-environment*)))
175 (setf (info :function :type name target-env) ctype)
176 (setf (info :function :where-from name target-env) :declared)
177 (setf (info :function :kind name target-env) :function)
178 (setf (info :function :info name target-env) info)))
181 ;;; Return the FUNCTION-INFO for NAME or die trying. Since this is
182 ;;; used by people who want to modify the info, and the info may be
183 ;;; shared, we copy it. We don't have to copy the lists, since each
184 ;;; function that has generators or transforms has already been
186 (declaim (ftype (function (t) function-info) function-info-or-lose))
187 (defun function-info-or-lose (name)
188 (let ((*info-environment* (or *backend-info-environment*
189 *info-environment*)))
190 (let ((old (info :function :info name)))
191 (unless old (error "~S is not a known function." name))
192 (setf (info :function :info name) (copy-function-info old)))))
194 ;;;; generic type inference methods
196 ;;; Derive the type to be the type of the xxx'th arg. This can normally
197 ;;; only be done when the result value is that argument.
198 (defun result-type-first-arg (call)
199 (declare (type combination call))
200 (let ((cont (first (combination-args call))))
201 (when cont (continuation-type cont))))
202 (defun result-type-last-arg (call)
203 (declare (type combination call))
204 (let ((cont (car (last (combination-args call)))))
205 (when cont (continuation-type cont))))
207 ;;; Derive the result type according to the float contagion rules, but
208 ;;; always return a float. This is used for irrational functions that preserve
209 ;;; realness of their arguments.
210 (defun result-type-float-contagion (call)
211 (declare (type combination call))
212 (reduce #'numeric-contagion (combination-args call)
213 :key #'continuation-type
214 :initial-value (specifier-type 'single-float)))
216 ;;; Return a closure usable as a derive-type method for accessing the N'th
217 ;;; argument. If arg is a list, result is a list. If arg is a vector, result
218 ;;; is a vector with the same element type.
219 (defun sequence-result-nth-arg (n)
221 (declare (type combination call))
222 (let ((cont (nth (1- n) (combination-args call))))
224 (let ((type (continuation-type cont)))
225 (if (array-type-p type)
227 `(vector ,(type-specifier (array-type-element-type type))))
228 (let ((ltype (specifier-type 'list)))
229 (when (csubtypep type ltype)
232 ;;; Derive the type to be the type specifier which is the N'th arg.
233 (defun result-type-specifier-nth-arg (n)
235 (declare (type combination call))
236 (let ((cont (nth (1- n) (combination-args call))))
237 (when (and cont (constant-continuation-p cont))
238 (specifier-type (continuation-value cont))))))