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