Fix make-sequence type derivation with unknown types.
[sbcl.git] / src / compiler / fun-info.lisp
blobf881761ae6ab667a105b417f5c898f3ac4604b0c
1 ;;;; Define IR1 boolean attributes and the FUN-INFO structure
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!C")
14 ;;; IR1 boolean function attributes
15 ;;;
16 ;;; There are a number of boolean attributes of known functions which
17 ;;; we like to have in IR1. This information is mostly side effect
18 ;;; information of a sort, but it is different from the kind of
19 ;;; information we want in IR2. We aren't interested in a fine
20 ;;; breakdown of side effects, since we do very little code motion on
21 ;;; IR1. We are interested in some deeper semantic properties such as
22 ;;; whether it is safe to pass stack closures to.
23 ;;;
24 ;;; FIXME: This whole notion of "bad" explicit attributes is bad for
25 ;;; maintenance. How confident are we that we have no defknowns for functions
26 ;;; with functional arguments that are missing the CALL attribute? Much better
27 ;;; to have NO-CALLS, as it is much less likely to break accidentally.
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 fail to return during correct execution. Errors are O.K.
34 ;; UNUSED, BEWARE OF BITROT.
35 unwind
36 ;; the (default) worst case. Includes all the other bad things, plus
37 ;; any other possible bad thing. If this is present, the above bad
38 ;; attributes will be explicitly present as well.
39 any
40 ;; all arguments are safe for dynamic extent.
41 ;; (We used to have an UNSAFE attribute, which was basically the inverse
42 ;; of this, but it was unused and bitrotted, so when we started making
43 ;; use of the information we flipped the name and meaning the safe way
44 ;; around.)
45 dx-safe
46 ;; may be constant-folded. The function has no side effects, but may
47 ;; be affected by side effects on the arguments. e.g. SVREF, MAPC.
48 ;; Functions that side-effect their arguments are not considered to
49 ;; be foldable. Although it would be "legal" to constant fold them
50 ;; (since it "is an error" to modify a constant), we choose not to
51 ;; mark these functions as foldable in this database.
52 foldable
53 ;; may be eliminated if value is unused. The function has no side
54 ;; effects except possibly cons. If a function might signal errors,
55 ;; then it is not flushable even if it is movable, foldable or
56 ;; unsafely-flushable. Implies UNSAFELY-FLUSHABLE. (In safe code
57 ;; type checking of arguments is always performed by the caller, so
58 ;; a function which SHOULD signal an error if arguments are not of
59 ;; declared types may be FLUSHABLE.)
60 flushable
61 ;; unsafe call may be eliminated if value is unused. The function
62 ;; has no side effects except possibly cons and signalling an error
63 ;; in the safe code. If a function MUST signal errors, then it is
64 ;; not unsafely-flushable even if it is movable or foldable.
65 unsafely-flushable
66 ;; return value is important, and ignoring it is probably a mistake.
67 ;; Unlike the other attributes, this is used only for style
68 ;; warnings and has no effect on optimization.
69 important-result
70 ;; may be moved with impunity. Has no side effects except possibly
71 ;; consing, and is affected only by its arguments.
72 ;; UNUSED, BEWARE OF BITROT.
73 movable
74 ;; The function is a true predicate likely to be open-coded. Convert
75 ;; any non-conditional uses into (IF <pred> T NIL). Not usually
76 ;; specified to DEFKNOWN, since this is implementation dependent,
77 ;; and is usually automatically set by the DEFINE-VOP :CONDITIONAL
78 ;; option.
79 predicate
80 ;; Inhibit any warning for compiling a recursive definition.
81 ;; (Normally the compiler warns when compiling a recursive
82 ;; definition for a known function, since it might be a botched
83 ;; interpreter stub.)
84 recursive
85 ;; The function should always be translated by a VOP (i.e. it should
86 ;; should never be converted into a full call). This is used strictly
87 ;; as a consistency checking mechanism inside the compiler during IR2
88 ;; transformation.
89 always-translatable
90 ;; If a function is called with two arguments and the first one is a
91 ;; constant, then the arguments will be swapped.
92 commutative)
94 (defstruct (fun-info #-sb-xc-host (:pure t))
95 ;; boolean attributes of this function.
96 (attributes (missing-arg) :type attributes)
97 ;; TRANSFORM structures describing transforms for this function
98 (transforms () :type list)
99 ;; a function which computes the derived type for a call to this
100 ;; function by examining the arguments. This is null when there is
101 ;; no special method for this function.
102 (derive-type nil :type (or function null))
103 ;; a function that does various unspecified code transformations by
104 ;; directly hacking the IR. Returns true if further optimizations of
105 ;; the call shouldn't be attempted.
107 ;; KLUDGE: This return convention (non-NIL if you shouldn't do
108 ;; further optimiz'ns) is backwards from the return convention for
109 ;; transforms. -- WHN 19990917
110 (optimizer nil :type (or function null))
111 ;; a function computing the constant or literal arguments which are
112 ;; destructively modified by the call.
113 (destroyed-constant-args nil :type (or function null))
114 ;; If true, a special-case LTN annotation method that is used in
115 ;; place of the standard type/policy template selection. It may use
116 ;; arbitrary code to choose a template, decide to do a full call, or
117 ;; conspire with the IR2-CONVERT method to do almost anything. The
118 ;; COMBINATION node is passed as the argument.
119 (ltn-annotate nil :type (or function null))
120 ;; If true, the special-case IR2 conversion method for this
121 ;; function. This deals with funny functions, and anything else that
122 ;; can't be handled using the template mechanism. The COMBINATION
123 ;; node and the IR2-BLOCK are passed as arguments.
124 (ir2-convert nil :type (or function null))
125 ;; If true, the function can stack-allocate the result. The
126 ;; COMBINATION node is passed as an argument.
127 (stack-allocate-result nil :type (or function null))
128 ;; If true, the function can add flow-sensitive type information
129 ;; about the state of the world after its execution. The COMBINATION
130 ;; node is passed as an argument, along with the current set of
131 ;; active constraints for the block. The function returns a
132 ;; sequence of constraints; a constraint is a triplet of a
133 ;; constraint kind (a symbol, see (defstruct (constraint ...)) in
134 ;; constraint.lisp) and arguments, either LVARs, LAMBDA-VARs, or
135 ;; CTYPEs. If any of these arguments is NIL, the constraint is
136 ;; skipped. This simplifies integration with OK-LVAR-LAMBDA-VAR,
137 ;; which maps LVARs to LAMBDA-VARs. An optional fourth value in
138 ;; each constraint flips the meaning of the constraint if it is
139 ;; non-NIL.
140 (constraint-propagate nil :type (or function null))
141 ;; If true, the function can add flow-sensitive type information
142 ;; depending on the truthiness of its return value. Returns two
143 ;; values, a LVAR and a CTYPE. The LVAR is of that CTYPE iff the
144 ;; function returns true.
145 ;; It may also return additional third and fourth values. Each is
146 ;; a sequence of constraints (see CONSTRAINT-PROPAGATE), for the
147 ;; consequent and alternative branches, respectively.
148 (constraint-propagate-if nil :type (or function null))
149 ;; all the templates that could be used to translate this function
150 ;; into IR2, sorted by increasing cost.
151 (templates nil :type list)
152 ;; If non-null, then this function is a unary type predicate for
153 ;; this type.
154 (predicate-type nil :type (or ctype null))
155 ;; If non-null, the index of the argument which becomes the result
156 ;; of the function.
157 (result-arg nil :type (or index null))
158 ;; For functions with attributes FOLDABLE & CALL check that the
159 ;; arguments declared as CALLABLE or FUNCTION are foldable as well.
160 (foldable-call-check nil :type (or function null))
161 ;; A function that is called with lvars to check that the functions
162 ;; passed to CALLABLE arguments have the right argument counts.
163 (callable-check nil :type (or function null)))
165 (defprinter (fun-info)
166 (attributes :test (not (zerop attributes))
167 :prin1 (decode-ir1-attributes attributes))
168 (transforms :test transforms)
169 (derive-type :test derive-type)
170 (optimizer :test optimizer)
171 (ltn-annotate :test ltn-annotate)
172 (ir2-convert :test ir2-convert)
173 (templates :test templates)
174 (predicate-type :test predicate-type))