0.pre7.38:
[sbcl.git] / src / compiler / knownfun.lisp
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.
5
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.
14
15 (in-package "SB!C")
16
17 (/show0 "knownfun.lisp 17")
18
19 ;;; IR1 boolean function attributes
20 ;;;
21 ;;; There are a number of boolean attributes of known functions which we like
22 ;;; to have in IR1. This information is mostly side effect information of a
23 ;;; sort, but it is different from the kind of information we want in IR2. We
24 ;;; aren't interested in a fine breakdown of side effects, since we do very
25 ;;; little code motion on IR1. We are interested in some deeper semantic
26 ;;; properties such as whether it is safe to pass stack closures to.
27 (def-boolean-attribute ir1
28   ;; May call functions that are passed as arguments. In order to determine
29   ;; what other effects are present, we must find the effects of all arguments
30   ;; that may be functions.
31   call
32   ;; May incorporate function or number arguments into the result or somehow
33   ;; pass them upward. Note that this applies to any argument that *might* be
34   ;; a function or number, not just the arguments that always are.
35   unsafe
36   ;; May fail to return during correct execution. Errors are O.K.
37   unwind
38   ;; The (default) worst case. Includes all the other bad things, plus any
39   ;; other possible bad thing. If this is present, the above bad attributes
40   ;; will be explicitly present as well.
41   any
42   ;; May be constant-folded. The function has no side effects, but may be
43   ;; affected by side effects on the arguments. e.g. SVREF, MAPC. Functions
44   ;; that side-effect their arguments are not considered to be foldable.
45   ;; Although it would be "legal" to constant fold them (since it "is an error"
46   ;; to modify a constant), we choose not to mark these functions as foldable
47   ;; in this database.
48   foldable
49   ;; May be eliminated if value is unused. The function has no side effects
50   ;; except possibly CONS. If a function is defined to signal errors, then it
51   ;; is not flushable even if it is movable or foldable.
52   flushable
53   ;; May be moved with impunity. Has no side effects except possibly CONS, and
54   ;; is affected only by its arguments.
55   movable
56   ;; Function is a true predicate likely to be open-coded. Convert any
57   ;; non-conditional uses into (IF <pred> T NIL).
58   predicate
59   ;; Inhibit any warning for compiling a recursive definition. (Normally the
60   ;; compiler warns when compiling a recursive definition for a known function,
61   ;; since it might be a botched interpreter stub.)
62   recursive
63   ;; Function does explicit argument type checking, so the declared type should
64   ;; not be asserted when a definition is compiled.
65   explicit-check)
66
67 (defstruct (function-info #-sb-xc-host (:pure t))
68   ;; Boolean attributes of this function.
69   (attributes (required-argument) :type attributes)
70   ;; A list of Transform structures describing transforms for this function.
71   (transforms () :type list)
72   ;; A function which computes the derived type for a call to this function by
73   ;; examining the arguments. This is null when there is no special method for
74   ;; this function.
75   (derive-type nil :type (or function null))
76   ;; A function that does various unspecified code transformations by directly
77   ;; hacking the IR. Returns true if further optimizations of the call
78   ;; shouldn't be attempted.
79   ;;
80   ;; KLUDGE: This return convention (non-NIL if you shouldn't do further
81   ;; optimiz'ns) is backwards from the return convention for transforms.
82   ;; -- WHN 19990917
83   (optimizer nil :type (or function null))
84   ;; If true, a special-case LTN annotation method that is used in place of the
85   ;; standard type/policy template selection. It may use arbitrary code to
86   ;; choose a template, decide to do a full call, or conspire with the
87   ;; IR2-Convert method to do almost anything. The Combination node is passed
88   ;; as the argument.
89   (ltn-annotate nil :type (or function null))
90   ;; If true, the special-case IR2 conversion method for this function. This
91   ;; deals with funny functions, and anything else that can't be handled using
92   ;; the template mechanism. The Combination node and the IR2-Block are passed
93   ;; as arguments.
94   (ir2-convert nil :type (or function null))
95   ;; A list of all the templates that could be used to translate this function
96   ;; into IR2, sorted by increasing cost.
97   (templates nil :type list)
98   ;; If non-null, then this function is a unary type predicate for this type.
99   (predicate-type nil :type (or ctype null))
100   ;; If non-null, use this function to annotate the known call for the byte
101   ;; compiler. If it returns NIL, then change the call to :full.
102   (byte-annotate nil :type (or function null))
103   ;; If non-null, use this function to generate the byte code for this known
104   ;; call. This function can only give up if there is a byte-annotate function
105   ;; that arranged for the functional to be pushed onto the stack.
106   (byte-compile nil :type (or function null)))
107
108 (defprinter (function-info)
109   (transforms :test transforms)
110   (derive-type :test derive-type)
111   (optimizer :test optimizer)
112   (ltn-annotate :test ltn-annotate)
113   (ir2-convert :test ir2-convert)
114   (templates :test templates)
115   (predicate-type :test predicate-type)
116   (byte-annotate :test byte-annotate)
117   (byte-compile :test byte-compile))
118 \f
119 ;;;; interfaces to defining macros
120
121 ;;; an IR1 transform
122 (defstruct (transform (:copier nil))
123   ;; the function-type which enables this transform
124   (type (required-argument) :type ctype)
125   ;; the transformation function. Takes the COMBINATION node and returns a
126   ;; lambda, or throws out.
127   (function (required-argument) :type function)
128   ;; string used in efficiency notes
129   (note (required-argument) :type string)
130   ;; T if we should emit a failure note even if SPEED=INHIBIT-WARNINGS.
131   (important nil :type (member t nil))
132   ;; usable for byte code, native code, or both
133   (when :native :type (member :byte :native :both)))
134
135 (defprinter (transform) type note important when)
136
137 ;;; Grab the FUNCTION-INFO and enter the function, replacing any old
138 ;;; one with the same type and note.
139 (declaim (ftype (function (t list function &optional (or string null)
140                              (member t nil) (member :native :byte :both))
141                           *)
142                 %deftransform))
143 (defun %deftransform (name type fun &optional note important (when :native))
144   (let* ((ctype (specifier-type type))
145          (note (or note "optimize"))
146          (info (function-info-or-lose name))
147          (old (find-if (lambda (x)
148                          (and (type= (transform-type x) ctype)
149                               (string-equal (transform-note x) note)
150                               (eq (transform-important x) important)
151                               (eq (transform-when x) when)))
152                        (function-info-transforms info))))
153     (if old
154         (setf (transform-function old) fun (transform-note old) note)
155         (push (make-transform :type ctype :function fun :note note
156                               :important important :when when)
157               (function-info-transforms info)))
158     name))
159
160 ;;; Make a FUNCTION-INFO structure with the specified type, attributes
161 ;;; and optimizers.
162 (declaim (ftype (function (list list attributes &key
163                                 (:derive-type (or function null))
164                                 (:optimizer (or function null)))
165                           *)
166                 %defknown))
167 (defun %defknown (names type attributes &key derive-type optimizer)
168   (let ((ctype (specifier-type type))
169         (info (make-function-info :attributes attributes
170                                   :derive-type derive-type
171                                   :optimizer optimizer))
172         (target-env *info-environment*))
173     (dolist (name names)
174       (let ((old-function-info (info :function :info name)))
175         (when old-function-info
176           ;; This is handled as an error because it's generally a bad
177           ;; thing to blow away all the old optimization stuff. It's
178           ;; also a potential source of sneaky bugs:
179           ;;    DEFKNOWN FOO
180           ;;    DEFTRANSFORM FOO
181           ;;    DEFKNOWN FOO ; possibly hidden inside some macroexpansion
182           ;;    ; Now the DEFTRANSFORM doesn't exist in the target Lisp.
183           ;; However, it's continuable because it might be useful to do
184           ;; it when testing new optimization stuff interactively.
185           (cerror "Go ahead, overwrite it."
186                   "~@<overwriting old FUNCTION-INFO ~2I~_~S ~I~_for ~S~:>"
187                   old-function-info name)))
188       (setf (info :function :type name target-env) ctype)
189       (setf (info :function :where-from name target-env) :declared)
190       (setf (info :function :kind name target-env) :function)
191       (setf (info :function :info name target-env) info)))
192   names)
193
194 ;;; Return the FUNCTION-INFO for NAME or die trying. Since this is
195 ;;; used by callers who want to modify the info, and the info may be
196 ;;; shared, we copy it. We don't have to copy the lists, since each
197 ;;; function that has generators or transforms has already been
198 ;;; through here.
199 (declaim (ftype (function (t) function-info) function-info-or-lose))
200 (defun function-info-or-lose (name)
201   (let (;; FIXME: Do we need this rebinding here? It's a literal
202         ;; translation of the old CMU CL rebinding to
203         ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
204         ;; and it's not obvious whether the rebinding to itself is
205         ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
206         (*info-environment* *info-environment*))
207     (let ((old (info :function :info name)))
208       (unless old (error "~S is not a known function." name))
209       (setf (info :function :info name) (copy-function-info old)))))
210 \f
211 ;;;; generic type inference methods
212
213 ;;; Derive the type to be the type of the xxx'th arg. This can normally
214 ;;; only be done when the result value is that argument.
215 (defun result-type-first-arg (call)
216   (declare (type combination call))
217   (let ((cont (first (combination-args call))))
218     (when cont (continuation-type cont))))
219 (defun result-type-last-arg (call)
220   (declare (type combination call))
221   (let ((cont (car (last (combination-args call)))))
222     (when cont (continuation-type cont))))
223
224 ;;; Derive the result type according to the float contagion rules, but
225 ;;; always return a float. This is used for irrational functions that preserve
226 ;;; realness of their arguments.
227 (defun result-type-float-contagion (call)
228   (declare (type combination call))
229   (reduce #'numeric-contagion (combination-args call)
230           :key #'continuation-type
231           :initial-value (specifier-type 'single-float)))
232
233 ;;; Return a closure usable as a derive-type method for accessing the N'th
234 ;;; argument. If arg is a list, result is a list. If arg is a vector, result
235 ;;; is a vector with the same element type.
236 (defun sequence-result-nth-arg (n)
237   #'(lambda (call)
238       (declare (type combination call))
239       (let ((cont (nth (1- n) (combination-args call))))
240         (when cont
241           (let ((type (continuation-type cont)))
242             (if (array-type-p type)
243                 (specifier-type
244                  `(vector ,(type-specifier (array-type-element-type type))))
245                 (let ((ltype (specifier-type 'list)))
246                   (when (csubtypep type ltype)
247                     ltype))))))))
248
249 ;;; Derive the type to be the type specifier which is the N'th arg.
250 (defun result-type-specifier-nth-arg (n)
251   (lambda (call)
252     (declare (type combination call))
253     (let ((cont (nth (1- n) (combination-args call))))
254       (when (and cont (constant-continuation-p cont))
255         (specifier-type (continuation-value cont))))))
256
257 (/show0 "knownfun.lisp end of file")