Fix make-sequence type derivation with unknown types.
[sbcl.git] / src / compiler / node.lisp
blob442dfb65258da1f65f1a1c039ad2bab20f484def
1 ;;;; structures for the first intermediate representation in the
2 ;;;; compiler, IR1
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!C")
15 ;;; The front-end data structure (IR1) is composed of nodes,
16 ;;; representing actual evaluations. Linear sequences of nodes in
17 ;;; control-flow order are combined into blocks (but see
18 ;;; JOIN-SUCCESSOR-IF-POSSIBLE for precise conditions); control
19 ;;; transfers inside a block are represented with CTRANs and between
20 ;;; blocks -- with BLOCK-SUCC/BLOCK-PRED lists; data transfers are
21 ;;; represented with LVARs.
23 ;;; FIXME: this file contains a ton of DEF!STRUCT definitions most of which
24 ;;; could be DEFSTRUCT, except for the fact that we use def!struct as
25 ;;; a workaround for the compiler's inability to cope with mutally referential
26 ;;; structures, even ones within the same file. The IR1 structures are tightly
27 ;;; knitted together - for example, starting from a CRETURN, you can reach
28 ;;; at least 15 other structure objects, not counting some like HASH-TABLE
29 ;;; which are fundamental. e.g. we have:
30 ;;; CRETURN -> {NODE,CTRAN,CLAMBDA,LVAR}
31 ;;; CTRAN -> {BLOCK},
32 ;;; CLAMBDA -> {FUNCTIONAL,COMBINATION,BIND,PHYSENV,OPTIONAL-DISPATCH}
33 ;;; and so on. DEF!STRUCT solves this problem by way of a terrible hack
34 ;;; that works only for compiling the compiler.
36 ;;; "Lead-in" Control TRANsfer [to some node]
37 (def!struct (ctran
38 (:make-load-form-fun ignore-it)
39 (:constructor make-ctran))
40 ;; an indication of the way that this continuation is currently used
42 ;; :UNUSED
43 ;; A continuation for which all control-related slots have the
44 ;; default values. A continuation is unused during IR1 conversion
45 ;; until it is assigned a block, and may be also be temporarily
46 ;; unused during later manipulations of IR1. In a consistent
47 ;; state there should never be any mention of :UNUSED
48 ;; continuations. NEXT can have a non-null value if the next node
49 ;; has already been determined.
51 ;; :BLOCK-START
52 ;; The continuation that is the START of BLOCK.
54 ;; :INSIDE-BLOCK
55 ;; A continuation that is the NEXT of some node in BLOCK.
56 (kind :unused :type (member :unused :inside-block :block-start))
57 ;; A NODE which is to be evaluated next. Null only temporary.
58 (next nil :type (or node null))
59 ;; the node where this CTRAN is used, if unique. This is always null
60 ;; in :UNUSED and :BLOCK-START CTRANs, and is never null in
61 ;; :INSIDE-BLOCK continuations.
62 (use nil :type (or node null))
63 ;; the basic block this continuation is in. This is null only in
64 ;; :UNUSED continuations.
65 (block nil :type (or cblock null)))
67 (def!method print-object ((x ctran) stream)
68 (print-unreadable-object (x stream :type t :identity t)
69 (format stream "~D" (cont-num x))))
71 ;;; Linear VARiable. Multiple-value (possibly of unknown number)
72 ;;; temporal storage.
73 (def!struct (lvar
74 (:make-load-form-fun ignore-it)
75 (:constructor make-lvar (&optional dest)))
76 ;; The node which receives this value. NIL only temporarily.
77 (dest nil :type (or node null))
78 ;; cached type of this lvar's value. If NIL, then this must be
79 ;; recomputed: see LVAR-DERIVED-TYPE.
80 (%derived-type nil :type (or ctype null))
81 ;; the node (if unique) or a list of nodes where this lvar is used.
82 (uses nil :type (or node list))
83 ;; set to true when something about this lvar's value has
84 ;; changed. See REOPTIMIZE-LVAR. This provides a way for IR1
85 ;; optimize to determine which operands to a node have changed. If
86 ;; the optimizer for this node type doesn't care, it can elect not
87 ;; to clear this flag.
88 (reoptimize t :type boolean)
89 ;; Cached type which is checked by DEST. If NIL, then this must be
90 ;; recomputed: see LVAR-EXTERNALLY-CHECKABLE-TYPE.
91 (%externally-checkable-type nil :type (or null ctype))
92 ;; if the LVAR value is DYNAMIC-EXTENT, CLEANUP protecting it.
93 (dynamic-extent nil :type (or null cleanup))
94 ;; something or other that the back end annotates this lvar with
95 (info nil))
97 (def!method print-object ((x lvar) stream)
98 (print-unreadable-object (x stream :type t :identity t)
99 (format stream "~D" (cont-num x))))
101 #!-sb-fluid (declaim (inline lvar-has-single-use-p))
102 (defun lvar-has-single-use-p (lvar)
103 (typep (lvar-uses lvar) '(not list)))
105 ;;; Return the unique node, delivering a value to LVAR.
106 #!-sb-fluid (declaim (inline lvar-use))
107 (defun lvar-use (lvar)
108 (the (not list) (lvar-uses lvar)))
110 #!-sb-fluid (declaim (inline lvar-derived-type))
111 (defun lvar-derived-type (lvar)
112 (declare (type lvar lvar))
113 (or (lvar-%derived-type lvar)
114 (setf (lvar-%derived-type lvar)
115 (%lvar-derived-type lvar))))
117 #!-sb-fluid(declaim (inline flush-lvar-externally-checkable-type))
118 (defun flush-lvar-externally-checkable-type (lvar)
119 (declare (type lvar lvar))
120 (setf (lvar-%externally-checkable-type lvar) nil))
122 (def!struct (node (:constructor nil)
123 (:include sset-element (number (incf *compiler-sset-counter*)))
124 (:copier nil))
125 ;; unique ID for debugging
126 #!+sb-show (id (new-object-id) :read-only t)
127 ;; True if this node needs to be optimized. This is set to true
128 ;; whenever something changes about the value of an lvar whose DEST
129 ;; is this node.
130 (reoptimize t :type boolean)
131 ;; the ctran indicating what we do controlwise after evaluating this
132 ;; node. This is null if the node is the last in its block.
133 (next nil :type (or ctran null))
134 ;; the ctran that this node is the NEXT of. This is null during IR1
135 ;; conversion when we haven't linked the node in yet or in nodes
136 ;; that have been deleted from the IR1 by UNLINK-NODE.
137 (prev nil :type (or ctran null))
138 ;; the lexical environment this node was converted in
139 (lexenv *lexenv* :type lexenv)
140 ;; a representation of the source code responsible for generating
141 ;; this node
143 ;; For a form introduced by compilation (does not appear in the
144 ;; original source), the path begins with a list of all the
145 ;; enclosing introduced forms. This list is from the inside out,
146 ;; with the form immediately responsible for this node at the head
147 ;; of the list.
149 ;; Following the introduced forms is a representation of the
150 ;; location of the enclosing original source form. This transition
151 ;; is indicated by the magic ORIGINAL-SOURCE-START marker. The first
152 ;; element of the original source is the "form number", which is the
153 ;; ordinal number of this form in a depth-first, left-to-right walk
154 ;; of the truly-top-level form in which this appears.
156 ;; Following is a list of integers describing the path taken through
157 ;; the source to get to this point:
158 ;; (K L M ...) => (NTH K (NTH L (NTH M ...)))
160 ;; The last element in the list is the top level form number, which
161 ;; is the ordinal number (in this call to the compiler) of the truly
162 ;; top level form containing the original source.
163 (source-path *current-path* :type list)
164 ;; If this node is in a tail-recursive position, then this is set to
165 ;; T. At the end of IR1 (in physical environment analysis) this is
166 ;; computed for all nodes (after cleanup code has been emitted).
167 ;; Before then, a non-null value indicates that IR1 optimization has
168 ;; converted a tail local call to a direct transfer.
170 ;; If the back-end breaks tail-recursion for some reason, then it
171 ;; can null out this slot.
172 (tail-p nil :type boolean))
174 #!-sb-fluid (declaim (inline node-block))
175 (defun node-block (node)
176 (ctran-block (node-prev node)))
178 (defun %with-ir1-environment-from-node (node fun)
179 (declare (type node node) (type function fun))
180 (let ((*current-component* (node-component node))
181 (*lexenv* (node-lexenv node))
182 (*current-path* (node-source-path node)))
183 (aver-live-component *current-component*)
184 (funcall fun)))
186 (def!struct (valued-node (:conc-name node-)
187 (:include node)
188 (:constructor nil)
189 (:copier nil))
190 ;; the bottom-up derived type for this node.
191 (derived-type *wild-type* :type ctype)
192 ;; Lvar, receiving the values, produced by this node. May be NIL if
193 ;; the value is unused.
194 (lvar nil :type (or lvar null)))
196 #!-sb-fluid (declaim (inline node-dest))
197 (defun node-dest (node)
198 (awhen (node-lvar node) (lvar-dest it)))
200 ;;; Flags that are used to indicate various things about a block, such
201 ;;; as what optimizations need to be done on it:
202 ;;; -- REOPTIMIZE is set when something interesting happens the uses of a
203 ;;; lvar whose DEST is in this block. This indicates that the
204 ;;; value-driven (forward) IR1 optimizations should be done on this block.
205 ;;; -- FLUSH-P is set when code in this block becomes potentially flushable,
206 ;;; usually due to an lvar's DEST becoming null.
207 ;;; -- TYPE-CHECK is true when the type check phase should be run on this
208 ;;; block. IR1 optimize can introduce new blocks after type check has
209 ;;; already run. We need to check these blocks, but there is no point in
210 ;;; checking blocks we have already checked.
211 ;;; -- DELETE-P is true when this block is used to indicate that this block
212 ;;; has been determined to be unreachable and should be deleted. IR1
213 ;;; phases should not attempt to examine or modify blocks with DELETE-P
214 ;;; set, since they may:
215 ;;; - be in the process of being deleted, or
216 ;;; - have no successors.
217 ;;; -- TYPE-ASSERTED, TEST-MODIFIED
218 ;;; These flags are used to indicate that something in this block
219 ;;; might be of interest to constraint propagation. TYPE-ASSERTED
220 ;;; is set when an lvar type assertion is strengthened.
221 ;;; TEST-MODIFIED is set whenever the test for the ending IF has
222 ;;; changed (may be true when there is no IF.)
223 (!def-boolean-attribute block
224 reoptimize flush-p type-check delete-p type-asserted test-modified)
226 (macrolet ((defattr (block-slot)
227 `(defmacro ,block-slot (block)
228 `(block-attributep
229 (block-flags ,block)
230 ,(symbolicate (subseq (string ',block-slot) 6))))))
231 (defattr block-reoptimize)
232 (defattr block-flush-p)
233 (defattr block-type-check)
234 (defattr block-delete-p)
235 (defattr block-type-asserted)
236 (defattr block-test-modified))
238 (def!struct (cloop (:conc-name loop-)
239 (:predicate loop-p)
240 (:constructor make-loop)
241 (:copier copy-loop))
242 ;; The kind of loop that this is. These values are legal:
244 ;; :OUTER
245 ;; This is the outermost loop structure, and represents all the
246 ;; code in a component.
248 ;; :NATURAL
249 ;; A normal loop with only one entry.
251 ;; :STRANGE
252 ;; A segment of a "strange loop" in a non-reducible flow graph.
253 (kind (missing-arg) :type (member :outer :natural :strange))
254 ;; The first and last blocks in the loop. There may be more than one tail,
255 ;; since there may be multiple back branches to the same head.
256 (head nil :type (or cblock null))
257 (tail nil :type list)
258 ;; A list of all the blocks in this loop or its inferiors that have a
259 ;; successor outside of the loop.
260 (exits nil :type list)
261 ;; The loop that this loop is nested within. This is null in the outermost
262 ;; loop structure.
263 (superior nil :type (or cloop null))
264 ;; A list of the loops nested directly within this one.
265 (inferiors nil :type list)
266 (depth 0 :type fixnum)
267 ;; The head of the list of blocks directly within this loop. We must recurse
268 ;; on INFERIORS to find all the blocks.
269 (blocks nil :type (or null cblock))
270 ;; Backend saves the first emitted block of each loop here.
271 (info nil))
273 (defprinter (cloop :conc-name loop-)
274 kind
275 head
276 tail
277 exits
278 depth)
280 ;;; The CBLOCK structure represents a basic block. We include
281 ;;; SSET-ELEMENT so that we can have sets of blocks. Initially the
282 ;;; SSET-ELEMENT-NUMBER is null, DFO analysis numbers in reverse DFO.
283 ;;; During IR2 conversion, IR1 blocks are re-numbered in forward emit
284 ;;; order. This latter numbering also forms the basis of the block
285 ;;; numbering in the debug-info (though that is relative to the start
286 ;;; of the function.)
287 (def!struct (cblock (:include sset-element)
288 (:constructor make-block (start))
289 (:constructor make-block-key)
290 (:conc-name block-)
291 (:predicate block-p))
292 ;; a list of all the blocks that are predecessors/successors of this
293 ;; block. In well-formed IR1, most blocks will have one successor.
294 ;; The only exceptions are:
295 ;; 1. component head blocks (any number)
296 ;; 2. blocks ending in an IF (1 or 2)
297 ;; 3. blocks with DELETE-P set (zero)
298 (pred nil :type list)
299 (succ nil :type list)
300 ;; the ctran which heads this block (a :BLOCK-START), or NIL when we
301 ;; haven't made the start ctran yet (and in the dummy component head
302 ;; and tail blocks)
303 (start nil :type (or ctran null))
304 ;; the last node in this block. This is NIL when we are in the
305 ;; process of building a block (and in the dummy component head and
306 ;; tail blocks.)
307 (last nil :type (or node null))
308 ;; the forward and backward links in the depth-first ordering of the
309 ;; blocks. These slots are NIL at beginning/end.
310 (next nil :type (or null cblock))
311 (prev nil :type (or null cblock))
312 ;; This block's attributes: see above.
313 (flags (block-attributes reoptimize flush-p type-check type-asserted
314 test-modified)
315 :type attributes)
316 ;; in constraint propagation: list of LAMBDA-VARs killed in this block
317 ;; in copy propagation: list of killed TNs
318 (kill nil)
319 ;; other sets used in constraint propagation and/or copy propagation
320 (gen nil)
321 (in nil)
322 (out nil)
323 ;; Set of all blocks that dominate this block. NIL is interpreted
324 ;; as "all blocks in component".
325 (dominators nil :type (or null sset))
326 ;; the LOOP that this block belongs to
327 (loop nil :type (or null cloop))
328 ;; next block in the loop.
329 (loop-next nil :type (or null cblock))
330 ;; the component this block is in, or NIL temporarily during IR1
331 ;; conversion and in deleted blocks
332 (component (progn
333 (aver-live-component *current-component*)
334 *current-component*)
335 :type (or component null))
336 ;; a flag used by various graph-walking code to determine whether
337 ;; this block has been processed already or what. We make this
338 ;; initially NIL so that FIND-INITIAL-DFO doesn't have to scan the
339 ;; entire initial component just to clear the flags.
340 (flag nil)
341 ;; some kind of info used by the back end
342 (info nil)
343 ;; what macroexpansions and source transforms happened "in" this block, used
344 ;; for xref
345 (xrefs nil :type list)
346 ;; Cache the physenv of a block during lifetime analysis. :NONE if
347 ;; no cached value has been stored yet.
348 (physenv-cache :none :type (or null physenv (member :none))))
349 (def!method print-object ((cblock cblock) stream)
350 (print-unreadable-object (cblock stream :type t :identity t)
351 (format stream "~W :START c~W"
352 (block-number cblock)
353 (cont-num (block-start cblock)))))
355 ;;; The BLOCK-ANNOTATION class is inherited (via :INCLUDE) by
356 ;;; different BLOCK-INFO annotation structures so that code
357 ;;; (specifically control analysis) can be shared.
358 (def!struct (block-annotation (:constructor nil)
359 (:copier nil))
360 ;; The IR1 block that this block is in the INFO for.
361 (block (missing-arg) :type cblock)
362 ;; the next and previous block in emission order (not DFO). This
363 ;; determines which block we drop though to, and is also used to
364 ;; chain together overflow blocks that result from splitting of IR2
365 ;; blocks in lifetime analysis.
366 (next nil :type (or block-annotation null))
367 (prev nil :type (or block-annotation null)))
369 ;;; A COMPONENT structure provides a handle on a connected piece of
370 ;;; the flow graph. Most of the passes in the compiler operate on
371 ;;; COMPONENTs rather than on the entire flow graph.
373 ;;; According to the CMU CL internals/front.tex, the reason for
374 ;;; separating compilation into COMPONENTs is
375 ;;; to increase the efficiency of large block compilations. In
376 ;;; addition to improving locality of reference and reducing the
377 ;;; size of flow analysis problems, this allows back-end data
378 ;;; structures to be reclaimed after the compilation of each
379 ;;; component.
380 (locally
381 ;; This is really taking the low road. I couldn't think of a way to
382 ;; avoid a style warning regarding IR2-COMPONENT other than to declare
383 ;; the INFO slot as :type (or (satisfies ir2-component-p) ...)
384 ;; During make-host-2, the solution to this is the same hack
385 ;; as for everything else: use DEF!STRUCT for IR2-COMPONENT.
386 #!+(and (host-feature sb-xc-host) (host-feature sbcl))
387 (declare (sb-ext:muffle-conditions style-warning))
388 (def!struct (component (:copier nil)
389 (:constructor
390 make-component
391 (head
392 tail &aux
393 (last-block tail)
394 (outer-loop (make-loop :kind :outer :head head)))))
395 ;; unique ID for debugging
396 #!+sb-show (id (new-object-id) :read-only t)
397 ;; the kind of component
399 ;; (The terminology here is left over from before
400 ;; sbcl-0.pre7.34.flaky5.2, when there was no such thing as
401 ;; FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P, so that Python was
402 ;; incapable of building standalone :EXTERNAL functions, but instead
403 ;; had to implement things like #'CL:COMPILE as FUNCALL of a little
404 ;; toplevel stub whose sole purpose was to return an :EXTERNAL
405 ;; function.)
407 ;; The possibilities are:
408 ;; NIL
409 ;; an ordinary component, containing non-top-level code
410 ;; :TOPLEVEL
411 ;; a component containing only load-time code
412 ;; :COMPLEX-TOPLEVEL
413 ;; In the old system, before FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P
414 ;; was defined, this was necessarily a component containing both
415 ;; top level and run-time code. Now this state is also used for
416 ;; a component with HAS-EXTERNAL-REFERENCES-P functionals in it.
417 ;; :INITIAL
418 ;; the result of initial IR1 conversion, on which component
419 ;; analysis has not been done
420 ;; :DELETED
421 ;; debris left over from component analysis
423 ;; See also COMPONENT-TOPLEVELISH-P.
424 (kind nil :type (member nil :toplevel :complex-toplevel :initial :deleted))
425 ;; the blocks that are the dummy head and tail of the DFO
427 ;; Entry/exit points have these blocks as their
428 ;; predecessors/successors. The start and return from each
429 ;; non-deleted function is linked to the component head and
430 ;; tail. Until physical environment analysis links NLX entry stubs
431 ;; to the component head, every successor of the head is a function
432 ;; start (i.e. begins with a BIND node.)
433 (head (missing-arg) :type cblock)
434 (tail (missing-arg) :type cblock)
435 ;; New blocks are inserted before this.
436 (last-block (missing-arg) :type cblock)
437 ;; This becomes a list of the CLAMBDA structures for all functions
438 ;; in this component. OPTIONAL-DISPATCHes are represented only by
439 ;; their XEP and other associated lambdas. This doesn't contain any
440 ;; deleted or LET lambdas.
442 ;; Note that logical associations between CLAMBDAs and COMPONENTs
443 ;; seem to exist for a while before this is initialized. See e.g.
444 ;; the NEW-FUNCTIONALS slot. In particular, I got burned by writing
445 ;; some code to use this value to decide which components need
446 ;; LOCALL-ANALYZE-COMPONENT, when it turns out that
447 ;; LOCALL-ANALYZE-COMPONENT had a role in initializing this value
448 ;; (and DFO stuff does too, maybe). Also, even after it's
449 ;; initialized, it might change as CLAMBDAs are deleted or merged.
450 ;; -- WHN 2001-09-30
451 (lambdas () :type list)
452 ;; a list of FUNCTIONALs for functions that are newly converted, and
453 ;; haven't been local-call analyzed yet. Initially functions are not
454 ;; in the LAMBDAS list. Local call analysis moves them there
455 ;; (possibly as LETs, or implicitly as XEPs if an OPTIONAL-DISPATCH.)
456 ;; Between runs of local call analysis there may be some debris of
457 ;; converted or even deleted functions in this list.
458 (new-functionals () :type list)
459 ;; If this is :MAYBE, then there is stuff in this component that
460 ;; could benefit from further IR1 optimization. T means that
461 ;; reoptimization is necessary.
462 (reoptimize t :type (member nil :maybe t))
463 ;; If this is true, then the control flow in this component was
464 ;; messed up by IR1 optimizations, so the DFO should be recomputed.
465 (reanalyze nil :type boolean)
466 ;; some sort of name for the code in this component
467 (name "<unknown>" :type t)
468 ;; When I am a child, this is :NO-IR2-YET.
469 ;; In my adulthood, IR2 stores notes to itself here.
470 ;; After I have left the great wheel and am staring into the GC, this
471 ;; is set to :DEAD to indicate that it's a gruesome error to operate
472 ;; on me (e.g. by using me as *CURRENT-COMPONENT*, or by pushing
473 ;; LAMBDAs onto my NEW-FUNCTIONALS, as in sbcl-0.pre7.115).
474 (info :no-ir2-yet :type (or ir2-component (member :no-ir2-yet :dead)))
475 ;; count of the number of inline expansions we have done while
476 ;; compiling this component, to detect infinite or exponential
477 ;; blowups
478 (inline-expansions 0 :type index)
479 ;; a map from combination nodes to things describing how an
480 ;; optimization of the node failed. The description is an alist
481 ;; (TRANSFORM . ARGS), where TRANSFORM is the structure describing
482 ;; the transform that failed, and ARGS is either a list of format
483 ;; arguments for the note, or the FUN-TYPE that would have
484 ;; enabled the transformation but failed to match.
485 (failed-optimizations (make-hash-table :test 'eq) :type hash-table)
486 ;; This is similar to NEW-FUNCTIONALS, but is used when a function
487 ;; has already been analyzed, but new references have been added by
488 ;; inline expansion. Unlike NEW-FUNCTIONALS, this is not disjoint
489 ;; from COMPONENT-LAMBDAS.
490 (reanalyze-functionals nil :type list)
491 (delete-blocks nil :type list)
492 (nlx-info-generated-p nil :type boolean)
493 ;; this is filled by physical environment analysis
494 (dx-lvars nil :type list)
495 ;; The default LOOP in the component.
496 (outer-loop (missing-arg) :type cloop)
497 ;; The current sset index
498 (sset-number 0 :type fixnum)))
499 (defprinter (component :identity t)
500 name
501 #!+sb-show id
502 (reanalyze :test reanalyze))
504 (declaim (inline reoptimize-component))
505 (defun reoptimize-component (component kind)
506 (declare (type component component)
507 (type (member nil :maybe t) kind))
508 (aver kind)
509 (unless (eq (component-reoptimize component) t)
510 (setf (component-reoptimize component) kind)))
512 ;;; Check that COMPONENT is suitable for roles which involve adding
513 ;;; new code. (gotta love imperative programming with lotso in-place
514 ;;; side effects...)
515 (defun aver-live-component (component)
516 ;; FIXME: As of sbcl-0.pre7.115, we're asserting that
517 ;; COMPILE-COMPONENT hasn't happened yet. Might it be even better
518 ;; (certainly stricter, possibly also correct...) to assert that
519 ;; IR1-FINALIZE hasn't happened yet?
520 #+sb-xc-host (declare (notinline component-info)) ; unknown type
521 (aver (not (eql (component-info component) :dead))))
523 ;;; A CLEANUP structure represents some dynamic binding action. Blocks
524 ;;; are annotated with the current CLEANUP so that dynamic bindings
525 ;;; can be removed when control is transferred out of the binding
526 ;;; environment. We arrange for changes in dynamic bindings to happen
527 ;;; at block boundaries, so that cleanup code may easily be inserted.
528 ;;; The "mess-up" action is explicitly represented by a funny function
529 ;;; call or ENTRY node.
531 ;;; We guarantee that CLEANUPs only need to be done at block
532 ;;; boundaries by requiring that the exit ctrans initially head their
533 ;;; blocks, and then by not merging blocks when there is a cleanup
534 ;;; change.
535 (def!struct (cleanup (:copier nil))
536 ;; the kind of thing that has to be cleaned up
537 (kind (missing-arg)
538 :type (member :special-bind :catch :unwind-protect
539 :block :tagbody :dynamic-extent))
540 ;; the node that messes things up. This is the last node in the
541 ;; non-messed-up environment. Null only temporarily. This could be
542 ;; deleted due to unreachability.
543 (mess-up nil :type (or node null))
544 ;; For all kinds, except :DYNAMIC-EXTENT: a list of all the NLX-INFO
545 ;; structures whose NLX-INFO-CLEANUP is this cleanup. This is filled
546 ;; in by physical environment analysis.
548 ;; For :DYNAMIC-EXTENT: a list of all DX LVARs, preserved by this
549 ;; cleanup. This is filled when the cleanup is created (now by
550 ;; locall call analysis) and is rechecked by physical environment
551 ;; analysis. (For closures this is a list of the allocating node -
552 ;; during IR1, and a list of the argument LVAR of the allocator -
553 ;; after physical environment analysis.)
554 (info nil :type list))
555 (defprinter (cleanup :identity t)
556 kind
557 mess-up
558 (info :test info))
560 ;;; A PHYSENV represents the result of physical environment analysis.
562 ;;; As far as I can tell from reverse engineering, this IR1 structure
563 ;;; represents the physical environment (which is probably not the
564 ;;; standard Lispy term for this concept, but I dunno what is the
565 ;;; standard term): those things in the lexical environment which a
566 ;;; LAMBDA actually interacts with. Thus in
567 ;;; (DEFUN FROB-THINGS (THINGS)
568 ;;; (DOLIST (THING THINGS)
569 ;;; (BLOCK FROBBING-ONE-THING
570 ;;; (MAPCAR (LAMBDA (PATTERN)
571 ;;; (WHEN (FITS-P THING PATTERN)
572 ;;; (RETURN-FROM FROB-THINGS (LIST :FIT THING PATTERN))))
573 ;;; *PATTERNS*))))
574 ;;; the variables THINGS, THING, and PATTERN and the block names
575 ;;; FROB-THINGS and FROBBING-ONE-THING are all in the inner LAMBDA's
576 ;;; lexical environment, but of those only THING, PATTERN, and
577 ;;; FROB-THINGS are in its physical environment. In IR1, we largely
578 ;;; just collect the names of these things; in IR2 an IR2-PHYSENV
579 ;;; structure is attached to INFO and used to keep track of
580 ;;; associations between these names and less-abstract things (like
581 ;;; TNs, or eventually stack slots and registers). -- WHN 2001-09-29
582 (def!struct (physenv (:copier nil))
583 ;; the function that allocates this physical environment
584 (lambda (missing-arg) :type clambda :read-only t)
585 ;; This ultimately converges to a list of all the LAMBDA-VARs and
586 ;; NLX-INFOs needed from enclosing environments by code in this
587 ;; physical environment. In the meantime, it may be
588 ;; * NIL at object creation time
589 ;; * a superset of the correct result, generated somewhat later
590 ;; * smaller and smaller sets converging to the correct result as
591 ;; we notice and delete unused elements in the superset
592 (closure nil :type list)
593 ;; a list of NLX-INFO structures describing all the non-local exits
594 ;; into this physical environment
595 (nlx-info nil :type list)
596 ;; some kind of info used by the back end
597 (info nil))
598 (defprinter (physenv :identity t)
599 lambda
600 (closure :test closure)
601 (nlx-info :test nlx-info))
603 ;;; An TAIL-SET structure is used to accumulate information about
604 ;;; tail-recursive local calls. The "tail set" is effectively the
605 ;;; transitive closure of the "is called tail-recursively by"
606 ;;; relation.
608 ;;; All functions in the same tail set share the same TAIL-SET
609 ;;; structure. Initially each function has its own TAIL-SET, but when
610 ;;; IR1-OPTIMIZE-RETURN notices a tail local call, it joins the tail
611 ;;; sets of the called function and the calling function.
613 ;;; The tail set is somewhat approximate, because it is too early to
614 ;;; be sure which calls will be tail-recursive. Any call that *might*
615 ;;; end up tail-recursive causes TAIL-SET merging.
616 (def!struct (tail-set)
617 ;; a list of all the LAMBDAs in this tail set
618 (funs nil :type list)
619 ;; our current best guess of the type returned by these functions.
620 ;; This is the union across all the functions of the return node's
621 ;; RESULT-TYPE, excluding local calls.
622 (type *wild-type* :type ctype)
623 ;; some info used by the back end
624 (info nil))
625 (defprinter (tail-set :identity t)
626 funs
627 type
628 (info :test info))
630 ;;; An NLX-INFO structure is used to collect various information about
631 ;;; non-local exits. This is effectively an annotation on the
632 ;;; continuation, although it is accessed by searching in the
633 ;;; PHYSENV-NLX-INFO.
634 (def!struct (nlx-info
635 (:constructor make-nlx-info (cleanup
636 exit
637 &aux
638 (block (first (block-succ
639 (node-block exit))))))
640 (:make-load-form-fun ignore-it))
641 ;; the cleanup associated with this exit. In a catch or
642 ;; unwind-protect, this is the :CATCH or :UNWIND-PROTECT cleanup,
643 ;; and not the cleanup for the escape block. The CLEANUP-KIND of
644 ;; this thus provides a good indication of what kind of exit is
645 ;; being done.
646 (cleanup (missing-arg) :type cleanup)
647 ;; the ``continuation'' exited to (the block, succeeding the EXIT
648 ;; nodes). If this exit is from an escape function (CATCH or
649 ;; UNWIND-PROTECT), then physical environment analysis deletes the
650 ;; escape function and instead has the %NLX-ENTRY use this
651 ;; continuation.
653 ;; This slot is used as a sort of name to allow us to find the
654 ;; NLX-INFO that corresponds to a given exit. For this purpose, the
655 ;; ENTRY must also be used to disambiguate, since exits to different
656 ;; places may deliver their result to the same continuation.
657 (block (missing-arg) :type cblock)
658 ;; the entry stub inserted by physical environment analysis. This is
659 ;; a block containing a call to the %NLX-ENTRY funny function that
660 ;; has the original exit destination as its successor. Null only
661 ;; temporarily.
662 (target nil :type (or cblock null))
663 ;; for a lexical exit it determines whether tag existence check is
664 ;; needed
665 (safe-p nil :type boolean)
666 ;; some kind of info used by the back end
667 info)
668 (defprinter (nlx-info :identity t)
669 block
670 target
671 info)
673 ;;;; LEAF structures
675 ;;; Variables, constants and functions are all represented by LEAF
676 ;;; structures. A reference to a LEAF is indicated by a REF node. This
677 ;;; allows us to easily substitute one for the other without actually
678 ;;; hacking the flow graph.
679 (def!struct (leaf (:make-load-form-fun ignore-it)
680 (:include sset-element (number (incf *compiler-sset-counter*)))
681 (:constructor nil))
682 ;; unique ID for debugging
683 #!+sb-show (id (new-object-id) :read-only t)
684 ;; (For public access to this slot, use LEAF-SOURCE-NAME.)
686 ;; the name of LEAF as it appears in the source, e.g. 'FOO or '(SETF
687 ;; FOO) or 'N or '*Z*, or the special .ANONYMOUS. value if there's
688 ;; no name for this thing in the source (as can happen for
689 ;; FUNCTIONALs, e.g. for anonymous LAMBDAs or for functions for
690 ;; top-level forms; and can also happen for anonymous constants) or
691 ;; perhaps also if the match between the name and the thing is
692 ;; skewed enough (e.g. for macro functions or method functions) that
693 ;; we don't want to have that name affect compilation
695 ;; (We use .ANONYMOUS. here more or less the way we'd ordinarily use
696 ;; NIL, but we're afraid to use NIL because it's a symbol which could
697 ;; be the name of a leaf, if only the constant named NIL.)
699 ;; The value of this slot in can affect ordinary runtime behavior,
700 ;; e.g. of special variables and known functions, not just debugging.
702 ;; See also the LEAF-DEBUG-NAME function and the
703 ;; FUNCTIONAL-%DEBUG-NAME slot.
704 (%source-name (missing-arg)
705 ;; I guess we state the type this way to avoid calling
706 ;; LEGAL-FUN-NAME-P unless absolutely necessary,
707 ;; but this seems a bit of a premature optimization.
708 :type (or symbol (and cons (satisfies legal-fun-name-p)))
709 :read-only t)
710 ;; the type which values of this leaf must have
711 (type *universal-type* :type ctype)
712 ;; the type which values of this leaf have last been defined to have
713 ;; (but maybe won't have in future, in case of redefinition)
714 (defined-type *universal-type* :type ctype)
715 ;; where the TYPE information came from (in order, from strongest to weakest):
716 ;; :DECLARED, from a declaration.
717 ;; :DEFINED-HERE, from examination of the definition in the same file.
718 ;; :DEFINED, from examination of the definition elsewhere.
719 ;; :DEFINED-METHOD, implicit, piecemeal declarations from CLOS.
720 ;; :ASSUMED, from uses of the object.
721 (where-from :assumed :type (member :declared :assumed :defined-here :defined :defined-method))
722 ;; list of the REF nodes for this leaf
723 (refs () :type list)
724 ;; true if there was ever a REF or SET node for this leaf. This may
725 ;; be true when REFS and SETS are null, since code can be deleted.
726 (ever-used nil :type boolean)
727 ;; is it declared dynamic-extent, or truly-dynamic-extent?
728 (extent nil :type (member nil :maybe-dynamic :always-dynamic :indefinite))
729 ;; some kind of info used by the back end
730 (info nil))
732 (defun leaf-dynamic-extent (leaf)
733 (let ((extent (leaf-extent leaf)))
734 (unless (member extent '(nil :indefinite))
735 extent)))
737 ;;; LEAF name operations
739 ;;; KLUDGE: wants CLOS..
740 (defun leaf-has-source-name-p (leaf)
741 (not (eq (leaf-%source-name leaf)
742 '.anonymous.)))
743 (defun leaf-source-name (leaf)
744 (aver (leaf-has-source-name-p leaf))
745 (leaf-%source-name leaf))
747 ;;; The BASIC-VAR structure represents information common to all
748 ;;; variables which don't correspond to known local functions.
749 (def!struct (basic-var (:include leaf)
750 (:constructor nil))
751 ;; Lists of the set nodes for this variable.
752 (sets () :type list))
754 ;;; The GLOBAL-VAR structure represents a value hung off of the symbol
755 ;;; NAME.
756 (def!struct (global-var (:include basic-var))
757 ;; kind of variable described
758 (kind (missing-arg)
759 :type (member :special :global-function :global :unknown)))
760 (defprinter (global-var :identity t)
761 %source-name
762 #!+sb-show id
763 (type :test (not (eq type *universal-type*)))
764 (defined-type :test (not (eq defined-type *universal-type*)))
765 (where-from :test (not (eq where-from :assumed)))
766 kind)
768 (defun fun-locally-defined-p (name env)
769 (typecase env
770 (null nil)
771 #!+(and sb-fasteval (host-feature sb-xc))
772 (sb!interpreter:basic-env
773 (values (sb!interpreter:find-lexical-fun env name)))
775 (let ((fun (cdr (assoc name (lexenv-funs env) :test #'equal))))
776 (and fun (not (global-var-p fun)))))))
778 ;;; A DEFINED-FUN represents a function that is defined in the same
779 ;;; compilation block, or that has an inline expansion, or that has a
780 ;;; non-NIL INLINEP value. Whenever we change the INLINEP state (i.e.
781 ;;; an inline proclamation) we copy the structure so that former
782 ;;; INLINEP values are preserved.
783 (def!struct (defined-fun (:include global-var
784 (where-from :defined)
785 (kind :global-function)))
786 ;; The values of INLINEP and INLINE-EXPANSION initialized from the
787 ;; global environment.
788 (inlinep nil :type inlinep)
789 (inline-expansion nil :type (or cons null))
790 ;; List of functionals corresponding to this DEFINED-FUN: either from the
791 ;; conversion of a NAMED-LAMBDA, or from inline-expansion (see
792 ;; RECOGNIZE-KNOWN-CALL) - we need separate functionals for each policy in
793 ;; which the function is used.
794 (functionals nil :type list))
795 (defprinter (defined-fun :identity t)
796 %source-name
797 #!+sb-show id
798 inlinep
799 (functionals :test functionals))
801 ;;;; function stuff
803 ;;; We default the WHERE-FROM and TYPE slots to :DEFINED and FUNCTION.
804 ;;; We don't normally manipulate function types for defined functions,
805 ;;; but if someone wants to know, an approximation is there.
806 (def!struct (functional (:include leaf
807 (%source-name '.anonymous.)
808 (where-from :defined)
809 (type (specifier-type 'function))))
810 ;; (For public access to this slot, use LEAF-DEBUG-NAME.)
812 ;; the name of FUNCTIONAL for debugging purposes, or NIL if we
813 ;; should just let the SOURCE-NAME fall through
815 ;; Unlike the SOURCE-NAME slot, this slot's value should never
816 ;; affect ordinary code behavior, only debugging/diagnostic behavior.
818 ;; Ha. Ah, the starry-eyed idealism of the writer of the above
819 ;; paragraph. FUNCTION-LAMBDA-EXPRESSION's behaviour, as of
820 ;; sbcl-0.7.11.x, differs if the name of the a function is a string
821 ;; or not, as if it is a valid function name then it can look for an
822 ;; inline expansion.
824 ;; E.g. for the function which implements (DEFUN FOO ...), we could
825 ;; have
826 ;; %SOURCE-NAME=FOO
827 ;; %DEBUG-NAME=NIL
828 ;; for the function which implements the top level form
829 ;; (IN-PACKAGE :FOO) we could have
830 ;; %SOURCE-NAME=NIL
831 ;; %DEBUG-NAME=(TOP-LEVEL-FORM (IN-PACKAGE :FOO)
832 ;; for the function which implements FOO in
833 ;; (DEFUN BAR (...) (FLET ((FOO (...) ...)) ...))
834 ;; we could have
835 ;; %SOURCE-NAME=FOO
836 ;; %DEBUG-NAME=(FLET FOO)
837 ;; and for the function which implements FOO in
838 ;; (DEFMACRO FOO (...) ...)
839 ;; we could have
840 ;; %SOURCE-NAME=FOO (or maybe .ANONYMOUS.?)
841 ;; %DEBUG-NAME=(MACRO-FUNCTION FOO)
842 (%debug-name nil
843 :type (or null (not (satisfies legal-fun-name-p)))
844 :read-only t)
845 ;; some information about how this function is used. These values
846 ;; are meaningful:
848 ;; NIL
849 ;; an ordinary function, callable using local call
851 ;; :LET
852 ;; a lambda that is used in only one local call, and has in
853 ;; effect been substituted directly inline. The return node is
854 ;; deleted, and the result is computed with the actual result
855 ;; lvar for the call.
857 ;; :MV-LET
858 ;; Similar to :LET (as per FUNCTIONAL-LETLIKE-P), but the call
859 ;; is an MV-CALL.
861 ;; :ASSIGNMENT
862 ;; similar to a LET (as per FUNCTIONAL-SOMEWHAT-LETLIKE-P), but
863 ;; can have other than one call as long as there is at most
864 ;; one non-tail call.
866 ;; :OPTIONAL
867 ;; a lambda that is an entry point for an OPTIONAL-DISPATCH.
868 ;; Similar to NIL, but requires greater caution, since local call
869 ;; analysis may create new references to this function. Also, the
870 ;; function cannot be deleted even if it has *no* references. The
871 ;; OPTIONAL-DISPATCH is in the LAMDBA-OPTIONAL-DISPATCH.
873 ;; :EXTERNAL
874 ;; an external entry point lambda. The function it is an entry
875 ;; for is in the ENTRY-FUN slot.
877 ;; :TOPLEVEL
878 ;; a top level lambda, holding a compiled top level form.
879 ;; Compiled very much like NIL, but provides an indication of
880 ;; top level context. A :TOPLEVEL lambda should have *no*
881 ;; references. Its ENTRY-FUN is a self-pointer.
883 ;; :TOPLEVEL-XEP
884 ;; After a component is compiled, we clobber any top level code
885 ;; references to its non-closure XEPs with dummy FUNCTIONAL
886 ;; structures having this kind. This prevents the retained
887 ;; top level code from holding onto the IR for the code it
888 ;; references.
890 ;; :ESCAPE
891 ;; :CLEANUP
892 ;; special functions used internally by CATCH and UNWIND-PROTECT.
893 ;; These are pretty much like a normal function (NIL), but are
894 ;; treated specially by local call analysis and stuff. Neither
895 ;; kind should ever be given an XEP even though they appear as
896 ;; args to funny functions. An :ESCAPE function is never actually
897 ;; called, and thus doesn't need to have code generated for it.
899 ;; :DELETED
900 ;; This function has been found to be uncallable, and has been
901 ;; marked for deletion.
903 ;; :ZOMBIE
904 ;; Effectless [MV-]LET; has no BIND node.
905 (kind nil :type (member nil :optional :deleted :external :toplevel
906 :escape :cleanup :let :mv-let :assignment
907 :zombie :toplevel-xep))
908 ;; Is this a function that some external entity (e.g. the fasl dumper)
909 ;; refers to, so that even when it appears to have no references, it
910 ;; shouldn't be deleted? In the old days (before
911 ;; sbcl-0.pre7.37.flaky5.2) this was sort of implicitly true when
912 ;; KIND was :TOPLEVEL. Now it must be set explicitly, both for
913 ;; :TOPLEVEL functions and for any other kind of functions that we
914 ;; want to dump or return from #'CL:COMPILE or whatever.
915 (has-external-references-p nil)
916 ;; In a normal function, this is the external entry point (XEP)
917 ;; lambda for this function, if any. Each function that is used
918 ;; other than in a local call has an XEP, and all of the
919 ;; non-local-call references are replaced with references to the
920 ;; XEP.
922 ;; In an XEP lambda (indicated by the :EXTERNAL kind), this is the
923 ;; function that the XEP is an entry-point for. The body contains
924 ;; local calls to all the actual entry points in the function. In a
925 ;; :TOPLEVEL lambda (which is its own XEP) this is a self-pointer.
927 ;; With all other kinds, this is null.
928 (entry-fun nil :type (or functional null))
929 ;; the value of any inline/notinline declaration for a local
930 ;; function (or NIL in any case if no inline expansion is available)
931 (inlinep nil :type inlinep)
932 ;; If we have a lambda that can be used as in inline expansion for
933 ;; this function, then this is it. If there is no source-level
934 ;; lambda corresponding to this function then this is null (but then
935 ;; INLINEP will always be NIL as well.)
936 (inline-expansion nil :type list)
937 ;; the lexical environment that the INLINE-EXPANSION should be converted in
938 (lexenv *lexenv* :type lexenv)
939 ;; the original function or macro lambda list, or :UNSPECIFIED if
940 ;; this is a compiler created function
941 (arg-documentation nil :type (or list (member :unspecified)))
942 ;; the documentation string for the lambda
943 (documentation nil :type (or null string))
944 ;; Node, allocating closure for this lambda. May be NIL when we are
945 ;; sure that no closure is needed.
946 (allocator nil :type (or null combination))
947 ;; various rare miscellaneous info that drives code generation & stuff
948 (plist () :type list)
949 ;; xref information for this functional (only used for functions with an
950 ;; XEP)
951 (xref () :type list)
952 ;; True if this functional was created from an inline expansion. This
953 ;; is either T, or the GLOBAL-VAR for which it is an expansion.
954 (inline-expanded nil))
955 (defprinter (functional :identity t)
956 %source-name
957 %debug-name
958 #!+sb-show id)
960 (defun leaf-debug-name (leaf)
961 (if (functional-p leaf)
962 ;; FUNCTIONALs have additional %DEBUG-NAME behavior.
963 (functional-debug-name leaf)
964 ;; Other objects just use their source name.
966 ;; (As of sbcl-0.pre7.85, there are a few non-FUNCTIONAL
967 ;; anonymous objects, (anonymous constants..) and those would
968 ;; fail here if we ever tried to get debug names from them, but
969 ;; it looks as though it's never interesting to get debug names
970 ;; from them, so it's moot. -- WHN)
971 (leaf-source-name leaf)))
972 (defun leaf-%debug-name (leaf)
973 (when (functional-p leaf)
974 (functional-%debug-name leaf)))
976 ;;; Is FUNCTIONAL LET-converted? (where we're indifferent to whether
977 ;;; it returns one value or multiple values)
978 (defun functional-letlike-p (functional)
979 (member (functional-kind functional)
980 '(:let :mv-let)))
982 ;;; Is FUNCTIONAL sorta LET-converted? (where even an :ASSIGNMENT counts)
984 ;;; FIXME: I (WHN) don't understand this one well enough to give a good
985 ;;; definition or even a good function name, it's just a literal copy
986 ;;; of a CMU CL idiom. Does anyone have a better name or explanation?
987 (defun functional-somewhat-letlike-p (functional)
988 (or (functional-letlike-p functional)
989 (eql (functional-kind functional) :assignment)))
991 ;;; FUNCTIONAL name operations
992 (defun functional-debug-name (functional)
993 ;; FUNCTIONAL-%DEBUG-NAME takes precedence over FUNCTIONAL-SOURCE-NAME
994 ;; here because we want different debug names for the functions in
995 ;; DEFUN FOO and FLET FOO even though they have the same source name.
996 (or (functional-%debug-name functional)
997 ;; Note that this will cause an error if the function is
998 ;; anonymous. In SBCL (as opposed to CMU CL) we make all
999 ;; FUNCTIONALs have debug names. The CMU CL code didn't bother
1000 ;; in many FUNCTIONALs, especially those which were likely to be
1001 ;; optimized away before the user saw them. However, getting
1002 ;; that right requires a global understanding of the code,
1003 ;; which seems bad, so we just require names for everything.
1004 (leaf-source-name functional)))
1006 ;;; The CLAMBDA only deals with required lexical arguments. Special,
1007 ;;; optional, keyword and rest arguments are handled by transforming
1008 ;;; into simpler stuff.
1009 (def!struct (clambda (:include functional)
1010 (:conc-name lambda-)
1011 (:predicate lambda-p)
1012 (:constructor make-lambda)
1013 (:copier copy-lambda))
1014 ;; list of LAMBDA-VAR descriptors for arguments
1015 (vars nil :type list :read-only t)
1016 ;; If this function was ever a :OPTIONAL function (an entry-point
1017 ;; for an OPTIONAL-DISPATCH), then this is that OPTIONAL-DISPATCH.
1018 ;; The optional dispatch will be :DELETED if this function is no
1019 ;; longer :OPTIONAL.
1020 (optional-dispatch nil :type (or optional-dispatch null))
1021 ;; the BIND node for this LAMBDA. This node marks the beginning of
1022 ;; the lambda, and serves to explicitly represent the lambda binding
1023 ;; semantics within the flow graph representation. This is null in
1024 ;; deleted functions, and also in LETs where we deleted the call and
1025 ;; bind (because there are no variables left), but have not yet
1026 ;; actually deleted the LAMBDA yet.
1027 (bind nil :type (or bind null))
1028 ;; the RETURN node for this LAMBDA, or NIL if it has been
1029 ;; deleted. This marks the end of the lambda, receiving the result
1030 ;; of the body. In a LET, the return node is deleted, and the body
1031 ;; delivers the value to the actual lvar. The return may also be
1032 ;; deleted if it is unreachable.
1033 (return nil :type (or creturn null))
1034 ;; If this CLAMBDA is a LET, then this slot holds the LAMBDA whose
1035 ;; LETS list we are in, otherwise it is a self-pointer.
1036 (home nil :type (or clambda null))
1037 ;; all the lambdas that have been LET-substituted in this lambda.
1038 ;; This is only non-null in lambdas that aren't LETs.
1039 (lets nil :type list)
1040 ;; all the ENTRY nodes in this function and its LETs, or null in a LET
1041 (entries nil :type list)
1042 ;; CLAMBDAs which are locally called by this lambda, and other
1043 ;; objects (closed-over LAMBDA-VARs and XEPs) which this lambda
1044 ;; depends on in such a way that DFO shouldn't put them in separate
1045 ;; components.
1046 (calls-or-closes (make-sset) :type (or null sset))
1047 ;; the TAIL-SET that this LAMBDA is in. This is null during creation.
1049 ;; In CMU CL, and old SBCL, this was also NILed out when LET
1050 ;; conversion happened. That caused some problems, so as of
1051 ;; sbcl-0.pre7.37.flaky5.2 when I was trying to get the compiler to
1052 ;; emit :EXTERNAL functions directly, and so now the value
1053 ;; is no longer NILed out in LET conversion, but instead copied
1054 ;; (so that any further optimizations on the rest of the tail
1055 ;; set won't modify the value) if necessary.
1056 (tail-set nil :type (or tail-set null))
1057 ;; the structure which represents the phsical environment that this
1058 ;; function's variables are allocated in. This is filled in by
1059 ;; physical environment analysis. In a LET, this is EQ to our home's
1060 ;; physical environment.
1061 (physenv nil :type (or physenv null))
1062 ;; In a LET, this is the NODE-LEXENV of the combination node. We
1063 ;; retain it so that if the LET is deleted (due to a lack of vars),
1064 ;; we will still have caller's lexenv to figure out which cleanup is
1065 ;; in effect.
1066 (call-lexenv nil :type (or lexenv null))
1067 ;; list of embedded lambdas
1068 (children nil :type list)
1069 (parent nil :type (or clambda null))
1070 (allow-instrumenting *allow-instrumenting* :type boolean)
1071 ;; True if this is a system introduced lambda: it may contain user code, but
1072 ;; the lambda itself is not, and the bindings introduced by it are considered
1073 ;; transparent by the nested DX analysis.
1074 (system-lambda-p nil :type boolean))
1075 (defprinter (clambda :conc-name lambda- :identity t)
1076 %source-name
1077 %debug-name
1078 #!+sb-show id
1079 kind
1080 (type :test (not (eq type *universal-type*)))
1081 (where-from :test (not (eq where-from :assumed)))
1082 (vars :prin1 (mapcar #'leaf-source-name vars)))
1084 ;;; Before sbcl-0.7.0, there were :TOPLEVEL things which were magical
1085 ;;; in multiple ways. That's since been refactored into the orthogonal
1086 ;;; properties "optimized for locall with no arguments" and "externally
1087 ;;; visible/referenced (so don't delete it)". The code <0.7.0 did a lot
1088 ;;; of tests a la (EQ KIND :TOP_LEVEL) in the "don't delete it?" sense;
1089 ;;; this function is a sort of literal translation of those tests into
1090 ;;; the new world.
1092 ;;; FIXME: After things settle down, bare :TOPLEVEL might go away, at
1093 ;;; which time it might be possible to replace the COMPONENT-KIND
1094 ;;; :TOPLEVEL mess with a flag COMPONENT-HAS-EXTERNAL-REFERENCES-P
1095 ;;; along the lines of FUNCTIONAL-HAS-EXTERNAL-REFERENCES-P.
1096 (defun lambda-toplevelish-p (clambda)
1097 (or (eql (lambda-kind clambda) :toplevel)
1098 (lambda-has-external-references-p clambda)))
1099 (defun component-toplevelish-p (component)
1100 (member (component-kind component)
1101 '(:toplevel :complex-toplevel)))
1103 ;;; The OPTIONAL-DISPATCH leaf is used to represent hairy lambdas. It
1104 ;;; is a FUNCTIONAL, like LAMBDA. Each legal number of arguments has a
1105 ;;; function which is called when that number of arguments is passed.
1106 ;;; The function is called with all the arguments actually passed. If
1107 ;;; additional arguments are legal, then the LEXPR style MORE-ENTRY
1108 ;;; handles them. The value returned by the function is the value
1109 ;;; which results from calling the OPTIONAL-DISPATCH.
1111 ;;; The theory is that each entry-point function calls the next entry
1112 ;;; point tail-recursively, passing all the arguments passed in and
1113 ;;; the default for the argument the entry point is for. The last
1114 ;;; entry point calls the real body of the function. In the presence
1115 ;;; of SUPPLIED-P args and other hair, things are more complicated. In
1116 ;;; general, there is a distinct internal function that takes the
1117 ;;; SUPPLIED-P args as parameters. The preceding entry point calls
1118 ;;; this function with NIL filled in for the SUPPLIED-P args, while
1119 ;;; the current entry point calls it with T in the SUPPLIED-P
1120 ;;; positions.
1122 ;;; Note that it is easy to turn a call with a known number of
1123 ;;; arguments into a direct call to the appropriate entry-point
1124 ;;; function, so functions that are compiled together can avoid doing
1125 ;;; the dispatch.
1126 (def!struct (optional-dispatch (:include functional))
1127 ;; the original parsed argument list, for anyone who cares
1128 (arglist nil :type list)
1129 ;; true if &ALLOW-OTHER-KEYS was supplied
1130 (allowp nil :type boolean)
1131 ;; true if &KEY was specified (which doesn't necessarily mean that
1132 ;; there are any &KEY arguments..)
1133 (keyp nil :type boolean)
1134 ;; the number of required arguments. This is the smallest legal
1135 ;; number of arguments.
1136 (min-args 0 :type unsigned-byte)
1137 ;; the total number of required and optional arguments. Args at
1138 ;; positions >= to this are &REST, &KEY or illegal args.
1139 (max-args 0 :type unsigned-byte)
1140 ;; list of the (maybe delayed) LAMBDAs which are the entry points
1141 ;; for non-rest, non-key calls. The entry for MIN-ARGS is first,
1142 ;; MIN-ARGS+1 second, ... MAX-ARGS last. The last entry-point always
1143 ;; calls the main entry; in simple cases it may be the main entry.
1144 (entry-points nil :type list)
1145 ;; an entry point which takes MAX-ARGS fixed arguments followed by
1146 ;; an argument context pointer and an argument count. This entry
1147 ;; point deals with listifying rest args and parsing keywords. This
1148 ;; is null when extra arguments aren't legal.
1149 (more-entry nil :type (or clambda null))
1150 ;; the main entry-point into the function, which takes all arguments
1151 ;; including keywords as fixed arguments. The format of the
1152 ;; arguments must be determined by examining the arglist. This may
1153 ;; be used by callers that supply at least MAX-ARGS arguments and
1154 ;; know what they are doing.
1155 (main-entry nil :type (or clambda null)))
1156 (defprinter (optional-dispatch :identity t)
1157 %source-name
1158 %debug-name
1159 #!+sb-show id
1160 (type :test (not (eq type *universal-type*)))
1161 (where-from :test (not (eq where-from :assumed)))
1162 arglist
1163 allowp
1164 keyp
1165 min-args
1166 max-args
1167 (entry-points :test entry-points)
1168 (more-entry :test more-entry)
1169 main-entry)
1171 ;;; The ARG-INFO structure allows us to tack various information onto
1172 ;;; LAMBDA-VARs during IR1 conversion. If we use one of these things,
1173 ;;; then the var will have to be massaged a bit before it is simple
1174 ;;; and lexical.
1175 (def!struct arg-info
1176 ;; true if this arg is to be specially bound
1177 (specialp nil :type boolean)
1178 ;; the kind of argument being described. Required args only have arg
1179 ;; info structures if they are special.
1180 (kind (missing-arg)
1181 :type (member :required :optional :keyword :rest
1182 :more-context :more-count))
1183 ;; If true, this is the VAR for SUPPLIED-P variable of a keyword or
1184 ;; optional arg. This is true for keywords with non-constant
1185 ;; defaults even when there is no user-specified supplied-p var.
1186 (supplied-p nil :type (or lambda-var null))
1187 ;; NIL if supplied-p is only used for directing evaluation of init forms
1188 (supplied-used-p t :type boolean)
1189 ;; the default for a keyword or optional, represented as the
1190 ;; original Lisp code. This is set to NIL in &KEY arguments that are
1191 ;; defaulted using the SUPPLIED-P arg.
1193 ;; For &REST arguments this may contain information about more context
1194 ;; the rest list comes from.
1195 (default nil :type t)
1196 ;; the actual key for a &KEY argument. Note that in ANSI CL this is
1197 ;; not necessarily a keyword: (DEFUN FOO (&KEY ((BAR BAR))) ...).
1198 (key nil :type symbol))
1199 (defprinter (arg-info :identity t)
1200 (specialp :test specialp)
1201 kind
1202 (supplied-p :test supplied-p)
1203 (default :test default)
1204 (key :test key))
1206 ;;; The LAMBDA-VAR structure represents a lexical lambda variable.
1207 ;;; This structure is also used during IR1 conversion to describe
1208 ;;; lambda arguments which may ultimately turn out not to be simple
1209 ;;; and lexical.
1211 ;;; LAMBDA-VARs with no REFs are considered to be deleted; physical
1212 ;;; environment analysis isn't done on these variables, so the back
1213 ;;; end must check for and ignore unreferenced variables. Note that a
1214 ;;; deleted LAMBDA-VAR may have sets; in this case the back end is
1215 ;;; still responsible for propagating the SET-VALUE to the set's CONT.
1216 (!def-boolean-attribute lambda-var
1217 ;; true if this variable has been declared IGNORE
1218 ignore
1219 ;; This is set by physical environment analysis if it chooses an
1220 ;; indirect (value cell) representation for this variable because it
1221 ;; is both set and closed over.
1222 indirect
1223 ;; true if the last reference has been deleted (and new references
1224 ;; should not be made)
1225 deleted
1226 ;; This is set by physical environment analysis if, should it be an
1227 ;; indirect lambda-var, an actual value cell object must be
1228 ;; allocated for this variable because one or more of the closures
1229 ;; that refer to it are not dynamic-extent. Note that both
1230 ;; attributes must be set for the value-cell object to be created.
1231 explicit-value-cell
1234 (def!struct (lambda-var (:include basic-var))
1235 (flags (lambda-var-attributes)
1236 :type attributes)
1237 ;; the CLAMBDA that this var belongs to. This may be null when we are
1238 ;; building a lambda during IR1 conversion.
1239 (home nil :type (or null clambda))
1240 ;; The following two slots are only meaningful during IR1 conversion
1241 ;; of hairy lambda vars:
1243 ;; The ARG-INFO structure which holds information obtained from
1244 ;; &keyword parsing.
1245 (arg-info nil :type (or arg-info null))
1246 ;; if true, the GLOBAL-VAR structure for the special variable which
1247 ;; is to be bound to the value of this argument
1248 (specvar nil :type (or global-var null))
1249 ;; Set of the CONSTRAINTs on this variable. Used by constraint
1250 ;; propagation. This is left null by the lambda pre-pass if it
1251 ;; determine that this is a set closure variable, and is thus not a
1252 ;; good subject for flow analysis.
1253 (constraints nil :type (or null t #| FIXME: conset |#))
1254 ;; Content-addressed indices for the CONSTRAINTs on this variable.
1255 ;; These are solely used by FIND-CONSTRAINT
1256 (ctype-constraints nil :type (or null hash-table))
1257 (eq-constraints nil :type (or null hash-table))
1258 ;; sorted sets of constraints we like to iterate over
1259 (eql-var-constraints nil :type (or null (array t 1)))
1260 (inheritable-constraints nil :type (or null (array t 1)))
1261 (private-constraints nil :type (or null (array t 1)))
1262 ;; Initial type of a LET variable as last seen by PROPAGATE-FROM-SETS.
1263 (last-initial-type *universal-type* :type ctype)
1264 ;; The FOP handle of the lexical variable represented by LAMBDA-VAR
1265 ;; in the fopcompiler.
1266 (fop-value nil))
1267 (defprinter (lambda-var :identity t)
1268 %source-name
1269 #!+sb-show id
1270 (type :test (not (eq type *universal-type*)))
1271 (where-from :test (not (eq where-from :assumed)))
1272 (flags :test (not (zerop flags))
1273 :prin1 (decode-lambda-var-attributes flags))
1274 (arg-info :test arg-info)
1275 (specvar :test specvar))
1277 (defmacro lambda-var-ignorep (var)
1278 `(lambda-var-attributep (lambda-var-flags ,var) ignore))
1279 (defmacro lambda-var-indirect (var)
1280 `(lambda-var-attributep (lambda-var-flags ,var) indirect))
1281 (defmacro lambda-var-deleted (var)
1282 `(lambda-var-attributep (lambda-var-flags ,var) deleted))
1283 (defmacro lambda-var-explicit-value-cell (var)
1284 `(lambda-var-attributep (lambda-var-flags ,var) explicit-value-cell))
1286 ;;;; basic node types
1288 ;;; A REF represents a reference to a LEAF. REF-REOPTIMIZE is
1289 ;;; initially (and forever) NIL, since REFs don't receive any values
1290 ;;; and don't have any IR1 optimizer.
1291 (def!struct (ref (:include valued-node (reoptimize nil))
1292 (:constructor make-ref
1293 (leaf
1294 &optional (%source-name '.anonymous.)
1295 &aux (leaf-type (leaf-type leaf))
1296 (derived-type
1297 (make-single-value-type leaf-type))))
1298 (:copier nil))
1299 ;; The leaf referenced.
1300 (leaf nil :type leaf)
1301 ;; CONSTANT nodes are always anonymous, since we wish to coalesce named and
1302 ;; unnamed constants that are equivalent, we need to keep track of the
1303 ;; reference name for XREF.
1304 (%source-name (missing-arg) :type symbol :read-only t))
1305 (defprinter (ref :identity t)
1306 #!+sb-show id
1307 (%source-name :test (neq %source-name '.anonymous.))
1308 leaf)
1310 ;;; Naturally, the IF node always appears at the end of a block.
1311 (def!struct (cif (:include node)
1312 (:conc-name if-)
1313 (:predicate if-p)
1314 (:constructor make-if)
1315 (:copier copy-if))
1316 ;; LVAR for the predicate
1317 (test (missing-arg) :type lvar)
1318 ;; the blocks that we execute next in true and false case,
1319 ;; respectively (may be the same)
1320 (consequent (missing-arg) :type cblock)
1321 (consequent-constraints nil :type (or null t #| FIXME: conset |#))
1322 (alternative (missing-arg) :type cblock)
1323 (alternative-constraints nil :type (or null t #| FIXME: conset |#)))
1324 (defprinter (cif :conc-name if- :identity t)
1325 (test :prin1 (lvar-uses test))
1326 consequent
1327 alternative)
1329 (def!struct (cset (:include valued-node
1330 (derived-type (make-single-value-type
1331 *universal-type*)))
1332 (:conc-name set-)
1333 (:predicate set-p)
1334 (:constructor make-set)
1335 (:copier copy-set))
1336 ;; descriptor for the variable set
1337 (var (missing-arg) :type basic-var)
1338 ;; LVAR for the value form
1339 (value (missing-arg) :type lvar))
1340 (defprinter (cset :conc-name set- :identity t)
1342 (value :prin1 (lvar-uses value)))
1344 ;;; The BASIC-COMBINATION structure is used to represent both normal
1345 ;;; and multiple value combinations. In a let-like function call, this
1346 ;;; node appears at the end of its block and the body of the called
1347 ;;; function appears as the successor; the NODE-LVAR is null.
1348 (def!struct (basic-combination (:include valued-node)
1349 (:constructor nil)
1350 (:copier nil))
1351 ;; LVAR for the function
1352 (fun (missing-arg) :type lvar)
1353 ;; list of LVARs for the args. In a local call, an argument lvar may
1354 ;; be replaced with NIL to indicate that the corresponding variable
1355 ;; is unreferenced, and thus no argument value need be passed.
1356 (args nil :type list)
1357 ;; the kind of function call being made. :LOCAL means that this is a
1358 ;; local call to a function in the same component, and that argument
1359 ;; syntax checking has been done, etc. Calls to known global
1360 ;; functions are represented by storing :KNOWN in this slot and the
1361 ;; FUN-INFO for that function in the FUN-INFO slot. :FULL is a call
1362 ;; to an (as yet) unknown function, or to a known function declared
1363 ;; NOTINLINE. :ERROR is like :FULL, but means that we have
1364 ;; discovered that the call contains an error, and should not be
1365 ;; reconsidered for optimization.
1366 (kind :full :type (member :local :full :error :known))
1367 ;; if a call to a known global function, contains the FUN-INFO.
1368 (fun-info nil :type (or fun-info null))
1369 ;; Untrusted type we have asserted for this combination.
1370 (type-validated-for-leaf nil)
1371 ;; some kind of information attached to this node by the back end
1372 (info nil)
1373 (step-info))
1375 ;;; The COMBINATION node represents all normal function calls,
1376 ;;; including FUNCALL. This is distinct from BASIC-COMBINATION so that
1377 ;;; an MV-COMBINATION isn't COMBINATION-P.
1378 (def!struct (combination (:include basic-combination)
1379 (:constructor make-combination (fun))
1380 (:copier nil)))
1381 (defprinter (combination :identity t)
1382 #!+sb-show id
1383 (fun :prin1 (lvar-uses fun))
1384 (args :prin1 (mapcar (lambda (x)
1385 (if x
1386 (lvar-uses x)
1387 "<deleted>"))
1388 args)))
1390 ;;; An MV-COMBINATION is to MULTIPLE-VALUE-CALL as a COMBINATION is to
1391 ;;; FUNCALL. This is used to implement all the multiple-value
1392 ;;; receiving forms.
1393 (def!struct (mv-combination (:include basic-combination)
1394 (:constructor make-mv-combination (fun))
1395 (:copier nil)))
1396 (defprinter (mv-combination)
1397 (fun :prin1 (lvar-uses fun))
1398 (args :prin1 (mapcar #'lvar-uses args)))
1400 ;;; The BIND node marks the beginning of a lambda body and represents
1401 ;;; the creation and initialization of the variables.
1402 (def!struct (bind (:include node)
1403 (:copier nil))
1404 ;; the lambda we are binding variables for. Null when we are
1405 ;; creating the LAMBDA during IR1 translation.
1406 (lambda nil :type (or clambda null)))
1407 (defprinter (bind)
1408 lambda)
1410 ;;; The RETURN node marks the end of a lambda body. It collects the
1411 ;;; return values and represents the control transfer on return. This
1412 ;;; is also where we stick information used for TAIL-SET type
1413 ;;; inference.
1414 (def!struct (creturn (:include node)
1415 (:conc-name return-)
1416 (:predicate return-p)
1417 (:constructor make-return)
1418 (:copier copy-return))
1419 ;; the lambda we are returning from. Null temporarily during
1420 ;; ir1tran.
1421 (lambda nil :type (or clambda null))
1422 ;; the lvar which yields the value of the lambda
1423 (result (missing-arg) :type lvar)
1424 ;; the union of the node-derived-type of all uses of the result
1425 ;; other than by a local call, intersected with the result's
1426 ;; asserted-type. If there are no non-call uses, this is
1427 ;; *EMPTY-TYPE*
1428 (result-type *wild-type* :type ctype))
1429 (defprinter (creturn :conc-name return- :identity t)
1430 lambda
1431 result-type)
1433 ;;; The CAST node represents type assertions. The check for
1434 ;;; TYPE-TO-CHECK is performed and then the VALUE is declared to be of
1435 ;;; type ASSERTED-TYPE.
1436 (def!struct (cast (:include valued-node)
1437 (:constructor %make-cast))
1438 (asserted-type (missing-arg) :type ctype)
1439 (type-to-check (missing-arg) :type ctype)
1440 ;; an indication of what we have proven about how this type
1441 ;; assertion is satisfied:
1443 ;; NIL
1444 ;; No type check is necessary (VALUE type is a subtype of the TYPE-TO-CHECK.)
1446 ;; :EXTERNAL
1447 ;; Type check will be performed by NODE-DEST.
1449 ;; T
1450 ;; A type check is needed.
1451 (%type-check t :type (member t :external nil))
1452 ;; the LEXENV for the deleted EXIT node for which this is the
1453 ;; remaining value semantics. If NULL, we do not have exit value
1454 ;; semantics and may be deleted based on type information.
1455 (vestigial-exit-lexenv nil :type (or lexenv null))
1456 ;; the LEXENV for the ENTRY node for the deleted EXIT node mentioned
1457 ;; above. NULL if we do not have exit value semantics.
1458 (vestigial-exit-entry-lexenv nil :type (or lexenv null))
1459 ;; the lvar which is checked
1460 (value (missing-arg) :type lvar))
1461 (defprinter (cast :identity t)
1462 %type-check
1463 value
1464 asserted-type
1465 type-to-check
1466 vestigial-exit-lexenv
1467 vestigial-exit-entry-lexenv)
1469 ;;; A cast that always follows %check-bound and they are deleted together.
1470 ;;; Created via BOUND-CAST ir1-translator by chaining it together with %check-bound.
1471 ;;; IR1-OPTIMIZE-CAST handles propagation from BOUND to CAST-ASSERTED-TYPE
1472 ;;; DELETE-CAST deletes BOUND-CAST-CHECK
1473 ;;; GENERATE-TYPE-CHECKS ignores it, it never translates to a type check,
1474 ;;; %CHECK-BOUND does all the checking.
1475 (def!struct (bound-cast (:include cast (%type-check nil)))
1476 ;; %check-bound combination before the cast
1477 (check (missing-arg) :type (or null combination))
1478 ;; Tells whether the type information is in a state where it can be
1479 ;; optimized away, i.e. when BOUND is a constant.
1480 (derived nil :type boolean)
1481 (array (missing-arg) :type lvar)
1482 (bound (missing-arg) :type lvar))
1485 ;;;; non-local exit support
1486 ;;;;
1487 ;;;; In IR1, we insert special nodes to mark potentially non-local
1488 ;;;; lexical exits.
1490 ;;; The ENTRY node serves to mark the start of the dynamic extent of a
1491 ;;; lexical exit. It is the mess-up node for the corresponding :ENTRY
1492 ;;; cleanup.
1493 (def!struct (entry (:include node)
1494 (:copier nil))
1495 ;; All of the EXIT nodes for potential non-local exits to this point.
1496 (exits nil :type list)
1497 ;; The cleanup for this entry. NULL only temporarily.
1498 (cleanup nil :type (or cleanup null)))
1499 (defprinter (entry :identity t)
1500 #!+sb-show id)
1502 ;;; The EXIT node marks the place at which exit code would be emitted,
1503 ;;; if necessary. This is interposed between the uses of the exit
1504 ;;; continuation and the exit continuation's DEST. Instead of using
1505 ;;; the returned value being delivered directly to the exit
1506 ;;; continuation, it is delivered to our VALUE lvar. The original exit
1507 ;;; lvar is the exit node's LVAR; physenv analysis also makes it the
1508 ;;; lvar of %NLX-ENTRY call.
1509 (def!struct (exit (:include valued-node)
1510 (:copier nil))
1511 ;; the ENTRY node that this is an exit for. If null, this is a
1512 ;; degenerate exit. A degenerate exit is used to "fill" an empty
1513 ;; block (which isn't allowed in IR1.) In a degenerate exit, Value
1514 ;; is always also null.
1515 (entry nil :type (or entry null))
1516 ;; the lvar yielding the value we are to exit with. If NIL, then no
1517 ;; value is desired (as in GO).
1518 (value nil :type (or lvar null))
1519 (nlx-info nil :type (or nlx-info null)))
1520 (defprinter (exit :identity t)
1521 #!+sb-show id
1522 (entry :test entry)
1523 (value :test value))
1525 ;;; a helper for the POLICY macro, defined late here so that the
1526 ;;; various type tests can be inlined
1527 ;;; You might think that NIL as a policy becomes *POLICY*,
1528 ;;; but no, NIL was always an empty alist representing no qualities,
1529 ;;; which is a valid policy that makes each quality read as 1.
1530 ;;; In contrast, a LEXENV with NIL policy _does_ become *POLICY*.
1531 ;;; The reason for NIL mapping to baseline is that all nodes are annotated
1532 ;;; with a LEXENV, and the only object type that can be a LEXENV is LEXENV.
1533 ;;; An indicator is needed that a LEXENV is devoid of a policy, so this is
1534 ;;; what the NIL is for in lexenv-policy. But sometimes the compiler needs
1535 ;;; a policy without reference to an IR object - which is weird - and in that
1536 ;;; case it has nothing better to go with but the baseline policy.
1537 ;;; It still seems like a bug though.
1538 (defun %coerce-to-policy (thing)
1539 (typecase thing
1540 (policy thing)
1541 #!+(and sb-fasteval (host-feature sb-xc))
1542 (sb!interpreter:basic-env (sb!interpreter:env-policy thing))
1543 (null **baseline-policy**)
1544 (t (lexenv-policy (etypecase thing
1545 (lexenv thing)
1546 (node (node-lexenv thing))
1547 (functional (functional-lexenv thing)))))))
1549 ;;;; Freeze some structure types to speed type testing.
1551 ;; FIXME: the frozen-ness can't actually help optimize anything
1552 ;; until this file is compiled by the cross-compiler.
1553 ;; Anything compiled prior to then uses the non-frozen classoid as existed
1554 ;; at load-time of the cross-compiler. SB!XC:PROCLAIM would likely work here.
1555 #!-sb-fluid
1556 (declaim (freeze-type node lexenv ctran lvar cblock component cleanup
1557 physenv tail-set nlx-info))