1 ;;;; This file contains the LTN pass in the compiler. LTN allocates
2 ;;;; expression evaluation TNs, makes nearly all the implementation
3 ;;;; policy decisions, and also does a few other miscellaneous things.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
18 ;;; Return the LTN-POLICY indicated by the node policy.
20 ;;; FIXME: It would be tidier to use an LTN-POLICY object (an instance
21 ;;; of DEFSTRUCT LTN-POLICY) instead of a keyword, and have queries
22 ;;; like LTN-POLICY-SAFE-P become slot accessors. If we do this,
23 ;;; grep for and carefully review use of literal keywords, so that
25 ;;; (EQ (TEMPLATE-LTN-POLICY TEMPLATE) :SAFE)
26 ;;; don't get overlooked.
28 ;;; FIXME: Classic CMU CL went to some trouble to cache LTN-POLICY
29 ;;; values in LTN-ANALYZE so that they didn't have to be recomputed on
30 ;;; every block. I stripped that out (the whole DEFMACRO FROB thing)
31 ;;; because I found it too confusing. Thus, it might be that the
32 ;;; new uncached code spends an unreasonable amount of time in
33 ;;; this lookup function. This function should be profiled, and if
34 ;;; it's a significant contributor to runtime, we can cache it in
35 ;;; some more local way, e.g. by adding a CACHED-LTN-POLICY slot to
36 ;;; the NODE structure, and doing something like
37 ;;; (DEFUN NODE-LTN-POLICY (NODE)
38 ;;; (OR (NODE-CACHED-LTN-POLICY NODE)
39 ;;; (SETF (NODE-CACHED-LTN-POLICY NODE)
40 ;;; (NODE-UNCACHED-LTN-POLICY NODE)))
41 (defun node-ltn-policy (node)
42 (declare (type node node))
44 (let ((eff-space (max space
45 ;; on the theory that if the code is
46 ;; smaller, it will take less time to
47 ;; compile (could lose if the smallest
48 ;; case is out of line, and must
49 ;; allocate many linkage registers):
52 (if (>= speed eff-space) :fast :small)
53 (if (>= speed eff-space) :fast-safe :safe)))))
55 ;;; Return true if LTN-POLICY is a safe policy.
56 (defun ltn-policy-safe-p (ltn-policy)
58 ((:safe :fast-safe) t)
59 ((:small :fast) nil)))
61 ;;; Called when an unsafe policy indicates that no type check should
62 ;;; be done on CONT. We delete the type check unless it is :ERROR
63 ;;; (indicating a compile-time type error.)
64 (defun flush-type-check (cont)
65 (declare (type continuation cont))
66 (when (member (continuation-type-check cont) '(t :no-check))
67 (setf (continuation-%type-check cont) :deleted))
70 ;;; an annotated continuation's primitive-type
71 #!-sb-fluid (declaim (inline continuation-ptype))
72 (defun continuation-ptype (cont)
73 (declare (type continuation cont))
74 (ir2-continuation-primitive-type (continuation-info cont)))
76 ;;; Return true if a constant LEAF is of a type which we can legally
77 ;;; directly reference in code. Named constants with arbitrary pointer
78 ;;; values cannot, since we must preserve EQLness.
79 (defun legal-immediate-constant-p (leaf)
80 (declare (type constant leaf))
81 (or (not (leaf-has-source-name-p leaf))
82 (typecase (constant-value leaf)
83 ((or number character) t)
84 (symbol (symbol-package (constant-value leaf)))
87 ;;; If CONT is used only by a REF to a leaf that can be delayed, then
88 ;;; return the leaf, otherwise return NIL.
89 (defun continuation-delayed-leaf (cont)
90 (declare (type continuation cont))
91 (let ((use (continuation-use cont)))
93 (let ((leaf (ref-leaf use)))
95 (lambda-var (if (null (lambda-var-sets leaf)) leaf nil))
96 (constant (if (legal-immediate-constant-p leaf) leaf nil))
97 ((or functional global-var) nil))))))
99 ;;; Annotate a normal single-value continuation. If its only use is a
100 ;;; ref that we are allowed to delay the evaluation of, then we mark
101 ;;; the continuation for delayed evaluation, otherwise we assign a TN
102 ;;; to hold the continuation's value. If the continuation has a type
103 ;;; check, we make the TN according to the proven type to ensure that
104 ;;; the possibly erroneous value can be represented.
105 (defun annotate-1-value-continuation (cont)
106 (declare (type continuation cont))
107 (let ((info (continuation-info cont)))
108 (aver (eq (ir2-continuation-kind info) :fixed))
110 ((continuation-delayed-leaf cont)
111 (setf (ir2-continuation-kind info) :delayed))
112 ((member (continuation-type-check cont) '(:deleted nil))
113 (setf (ir2-continuation-locs info)
114 (list (make-normal-tn (ir2-continuation-primitive-type info)))))
116 (setf (ir2-continuation-locs info)
117 (list (make-normal-tn
119 (single-value-type (continuation-proven-type cont)))))))))
122 ;;; Make an IR2-CONTINUATION corresponding to the continuation type
123 ;;; and then do ANNOTATE-1-VALUE-CONTINUATION. If POLICY-KEYWORD isn't
124 ;;; a safe policy keyword, then we clear the TYPE-CHECK flag.
125 (defun annotate-ordinary-continuation (cont ltn-policy)
126 (declare (type continuation cont)
127 (type ltn-policy ltn-policy))
128 (let ((info (make-ir2-continuation
129 (primitive-type (continuation-type cont)))))
130 (setf (continuation-info cont) info)
131 (unless (ltn-policy-safe-p ltn-policy)
132 (flush-type-check cont))
133 (annotate-1-value-continuation cont))
136 ;;; Annotate the function continuation for a full call. If the only
137 ;;; reference is to a global function and DELAY is true, then we delay
138 ;;; the reference, otherwise we annotate for a single value.
140 ;;; Unlike for an argument, we only clear the type check flag when the
141 ;;; LTN-POLICY is unsafe, since the check for a valid function
142 ;;; object must be done before the call.
143 (defun annotate-fun-continuation (cont ltn-policy &optional (delay t))
144 (declare (type continuation cont) (type ltn-policy ltn-policy))
145 (unless (ltn-policy-safe-p ltn-policy)
146 (flush-type-check cont))
147 (let* ((ptype (primitive-type (continuation-type cont)))
148 (tn-ptype (if (member (continuation-type-check cont) '(:deleted nil))
152 (continuation-proven-type cont)))))
153 (info (make-ir2-continuation ptype)))
154 (setf (continuation-info cont) info)
155 (let ((name (continuation-fun-name cont t)))
157 (setf (ir2-continuation-kind info) :delayed)
158 (setf (ir2-continuation-locs info)
159 (list (make-normal-tn tn-ptype))))))
162 ;;; If TAIL-P is true, then we check to see whether the call can really
163 ;;; be a tail call by seeing if this function's return convention is :UNKNOWN.
164 ;;; If so, we move the call block succssor link from the return block to
165 ;;; the component tail (after ensuring that they are in separate blocks.)
166 ;;; This allows the return to be deleted when there are no non-tail uses.
167 (defun flush-full-call-tail-transfer (call)
168 (declare (type basic-combination call))
169 (let ((tails (and (node-tail-p call)
170 (lambda-tail-set (node-home-lambda call)))))
172 (cond ((eq (return-info-kind (tail-set-info tails)) :unknown)
173 (node-ends-block call)
174 (let ((block (node-block call)))
175 (unlink-blocks block (first (block-succ block)))
176 (link-blocks block (component-tail (block-component block)))))
178 (setf (node-tail-p call) nil)))))
181 ;;; We set the kind to :FULL or :FUNNY, depending on whether there is an
182 ;;; IR2-CONVERT method. If a funny function, then we inhibit tail recursion
183 ;;; and type check normally, since the IR2 convert method is going to want to
184 ;;; deliver values normally. We still annotate the function continuation,
185 ;;; since IR2tran might decide to call after all.
187 ;;; If not funny, we flush arg type checks, when LTN-POLICY is not
190 ;;; Note that args may already be annotated because template selection can
191 ;;; bail out to here.
192 (defun ltn-default-call (call ltn-policy)
193 (declare (type combination call) (type ltn-policy ltn-policy))
194 (let ((kind (basic-combination-kind call)))
195 (annotate-fun-continuation (basic-combination-fun call) ltn-policy)
198 ((and (fun-info-p kind)
199 (fun-info-ir2-convert kind))
200 (setf (basic-combination-info call) :funny)
201 (setf (node-tail-p call) nil)
202 (dolist (arg (basic-combination-args call))
203 (unless (continuation-info arg)
204 (setf (continuation-info arg)
205 (make-ir2-continuation
207 (continuation-type arg)))))
208 (annotate-1-value-continuation arg)))
210 (let ((safe-p (ltn-policy-safe-p ltn-policy)))
211 (dolist (arg (basic-combination-args call))
212 (unless safe-p (flush-type-check arg))
213 (unless (continuation-info arg)
214 (setf (continuation-info arg)
215 (make-ir2-continuation
217 (continuation-type arg)))))
218 (annotate-1-value-continuation arg)))
219 (when (eq kind :error)
220 (setf (basic-combination-kind call) :full))
221 (setf (basic-combination-info call) :full)
222 (flush-full-call-tail-transfer call))))
226 ;;; Annotate a continuation for unknown multiple values:
227 ;;; -- Delete any type check, regardless of LTN-POLICY, since IR2
228 ;;; conversion isn't prepared to check unknown-values continuations.
229 ;;; If we delete a type check when the policy is safe, then we emit
231 ;;; -- Add the continuation to the IR2-BLOCK-POPPED if it is used
232 ;;; across a block boundary.
233 ;;; -- Assign an :UNKNOWN IR2-CONTINUATION.
235 ;;; Note: it is critical that this be called only during LTN analysis
236 ;;; of CONT's DEST, and called in the order that the continuations are
237 ;;; received. Otherwise the IR2-BLOCK-POPPED and
238 ;;; IR2-COMPONENT-VALUES-FOO would get all messed up.
239 (defun annotate-unknown-values-continuation (cont ltn-policy)
240 (declare (type continuation cont) (type ltn-policy ltn-policy))
241 (when (eq (continuation-type-check cont) t)
242 (let* ((dest (continuation-dest cont))
243 (*compiler-error-context* dest))
244 (when (and (ltn-policy-safe-p ltn-policy)
245 (policy dest (>= safety inhibit-warnings)))
246 (compiler-note "compiler limitation: ~
247 unable to check type assertion in ~
248 unknown-values context:~% ~S"
249 (continuation-asserted-type cont))))
250 (setf (continuation-%type-check cont) :deleted))
252 (let* ((block (node-block (continuation-dest cont)))
253 (use (continuation-use cont))
254 (2block (block-info block)))
255 (unless (and use (eq (node-block use) block))
256 (setf (ir2-block-popped 2block)
257 (nconc (ir2-block-popped 2block) (list cont)))))
259 (let ((2cont (make-ir2-continuation nil)))
260 (setf (ir2-continuation-kind 2cont) :unknown)
261 (setf (ir2-continuation-locs 2cont) (make-unknown-values-locations))
262 (setf (continuation-info cont) 2cont))
266 ;;; Annotate CONT for a fixed, but arbitrary number of values, of the
267 ;;; specified primitive TYPES. If the continuation has a type check,
268 ;;; we annotate for the number of values indicated by TYPES, but only
269 ;;; use proven type information.
270 (defun annotate-fixed-values-continuation (cont ltn-policy types)
271 (declare (type continuation cont) (type ltn-policy ltn-policy) (list types))
272 (unless (ltn-policy-safe-p ltn-policy)
273 (flush-type-check cont))
274 (let ((res (make-ir2-continuation nil)))
275 (if (member (continuation-type-check cont) '(:deleted nil))
276 (setf (ir2-continuation-locs res) (mapcar #'make-normal-tn types))
277 (let* ((proven (mapcar (lambda (x)
278 (make-normal-tn (primitive-type x)))
280 (continuation-proven-type cont))))
281 (num-proven (length proven))
282 (num-types (length types)))
283 (setf (ir2-continuation-locs res)
285 ((< num-proven num-types)
287 (make-n-tns (- num-types num-proven)
288 *backend-t-primitive-type*)))
289 ((> num-proven num-types)
290 (subseq proven 0 num-types))
293 (setf (continuation-info cont) res))
296 ;;;; node-specific analysis functions
298 ;;; Annotate the result continuation for a function. We use the
299 ;;; RETURN-INFO computed by GTN to determine how to represent the
300 ;;; return values within the function:
301 ;;; * If the TAIL-SET has a fixed values count, then use that
303 ;;; * If the actual uses of the result continuation in this function
304 ;;; have a fixed number of values (after intersection with the
305 ;;; assertion), then use that number. We throw out TAIL-P :FULL
306 ;;; and :LOCAL calls, since we know they will truly end up as TR
307 ;;; calls. We can use the BASIC-COMBINATION-INFO even though it
308 ;;; is assigned by this phase, since the initial value NIL doesn't
309 ;;; look like a TR call.
310 ;;; If there are *no* non-tail-call uses, then it falls out
311 ;;; that we annotate for one value (type is NIL), but the return
312 ;;; will end up being deleted.
313 ;;; In non-perverse code, the DFO walk will reach all uses of
314 ;;; the result continuation before it reaches the RETURN. In
315 ;;; perverse code, we may annotate for unknown values when we
317 ;;; * Otherwise, we must annotate the continuation for unknown values.
318 (defun ltn-analyze-return (node ltn-policy)
319 (declare (type creturn node) (type ltn-policy ltn-policy))
320 (let* ((cont (return-result node))
321 (fun (return-lambda node))
322 (returns (tail-set-info (lambda-tail-set fun)))
323 (types (return-info-types returns)))
324 (if (eq (return-info-count returns) :unknown)
325 (collect ((res *empty-type* values-type-union))
326 (do-uses (use (return-result node))
327 (unless (and (node-tail-p use)
328 (basic-combination-p use)
329 (member (basic-combination-info use) '(:local :full)))
330 (res (node-derived-type use))))
332 (let ((int (values-type-intersection
334 (continuation-asserted-type cont))))
335 (multiple-value-bind (types kind)
336 (values-types (if (eq int *empty-type*) (res) int))
337 (if (eq kind :unknown)
338 (annotate-unknown-values-continuation cont ltn-policy)
339 (annotate-fixed-values-continuation
340 cont ltn-policy (mapcar #'primitive-type types))))))
341 (annotate-fixed-values-continuation cont ltn-policy types)))
345 ;;; Annotate the single argument continuation as a fixed-values
346 ;;; continuation. We look at the called lambda to determine number and
347 ;;; type of return values desired. It is assumed that only a function
348 ;;; that LOOKS-LIKE-AN-MV-BIND will be converted to a local call.
349 (defun ltn-analyze-mv-bind (call ltn-policy)
350 (declare (type mv-combination call)
351 (type ltn-policy ltn-policy))
352 (setf (basic-combination-kind call) :local)
353 (setf (node-tail-p call) nil)
354 (annotate-fixed-values-continuation
355 (first (basic-combination-args call))
357 (mapcar (lambda (var)
358 (primitive-type (basic-var-type var)))
362 (basic-combination-fun call))))))
365 ;;; We force all the argument continuations to use the unknown values
366 ;;; convention. The continuations are annotated in reverse order,
367 ;;; since the last argument is on top, thus must be popped first. We
368 ;;; disallow delayed evaluation of the function continuation to
369 ;;; simplify IR2 conversion of MV call.
371 ;;; We could be cleverer when we know the number of values returned by
372 ;;; the continuations, but optimizations of MV call are probably
375 ;;; We are also responsible for handling THROW, which is represented
376 ;;; in IR1 as an MV call to the %THROW funny function. We annotate the
377 ;;; tag continuation for a single value and the values continuation
378 ;;; for unknown values.
379 (defun ltn-analyze-mv-call (call ltn-policy)
380 (declare (type mv-combination call) (type ltn-policy ltn-policy))
381 (let ((fun (basic-combination-fun call))
382 (args (basic-combination-args call)))
383 (cond ((eq (continuation-fun-name fun) '%throw)
384 (setf (basic-combination-info call) :funny)
385 (annotate-ordinary-continuation (first args) ltn-policy)
386 (annotate-unknown-values-continuation (second args) ltn-policy)
387 (setf (node-tail-p call) nil))
389 (setf (basic-combination-info call) :full)
390 (annotate-fun-continuation (basic-combination-fun call)
393 (dolist (arg (reverse args))
394 (annotate-unknown-values-continuation arg ltn-policy))
395 (flush-full-call-tail-transfer call))))
399 ;;; Annotate the arguments as ordinary single-value continuations. And
400 ;;; check the successor.
401 (defun ltn-analyze-local-call (call ltn-policy)
402 (declare (type combination call)
403 (type ltn-policy ltn-policy))
404 (setf (basic-combination-info call) :local)
405 (dolist (arg (basic-combination-args call))
407 (annotate-ordinary-continuation arg ltn-policy)))
408 (when (node-tail-p call)
409 (set-tail-local-call-successor call))
412 ;;; Make sure that a tail local call is linked directly to the bind
413 ;;; node. Usually it will be, but calls from XEPs and calls that might have
414 ;;; needed a cleanup after them won't have been swung over yet, since we
415 ;;; weren't sure they would really be TR until now. Also called by byte
417 (defun set-tail-local-call-successor (call)
418 (let ((caller (node-home-lambda call))
419 (callee (combination-lambda call)))
420 (aver (eq (lambda-tail-set caller)
421 (lambda-tail-set (lambda-home callee))))
422 (node-ends-block call)
423 (let ((block (node-block call)))
424 (unlink-blocks block (first (block-succ block)))
425 (link-blocks block (lambda-block callee))))
428 ;;; Annotate the value continuation.
429 (defun ltn-analyze-set (node ltn-policy)
430 (declare (type cset node) (type ltn-policy ltn-policy))
431 (setf (node-tail-p node) nil)
432 (annotate-ordinary-continuation (set-value node) ltn-policy)
435 ;;; If the only use of the TEST continuation is a combination
436 ;;; annotated with a conditional template, then don't annotate the
437 ;;; continuation so that IR2 conversion knows not to emit any code,
438 ;;; otherwise annotate as an ordinary continuation. Since we only use
439 ;;; a conditional template if the call immediately precedes the IF
440 ;;; node in the same block, we know that any predicate will already be
442 (defun ltn-analyze-if (node ltn-policy)
443 (declare (type cif node) (type ltn-policy ltn-policy))
444 (setf (node-tail-p node) nil)
445 (let* ((test (if-test node))
446 (use (continuation-use test)))
447 (unless (and (combination-p use)
448 (let ((info (basic-combination-info use)))
449 (and (template-p info)
450 (eq (template-result-types info) :conditional))))
451 (annotate-ordinary-continuation test ltn-policy)))
454 ;;; If there is a value continuation, then annotate it for unknown
455 ;;; values. In this case, the exit is non-local, since all other exits
456 ;;; are deleted or degenerate by this point.
457 (defun ltn-analyze-exit (node ltn-policy)
458 (setf (node-tail-p node) nil)
459 (let ((value (exit-value node)))
461 (annotate-unknown-values-continuation value ltn-policy)))
464 ;;; We need a special method for %UNWIND-PROTECT that ignores the
465 ;;; cleanup function. We don't annotate either arg, since we don't
466 ;;; need them at run-time.
468 ;;; (The default is o.k. for %CATCH, since environment analysis
469 ;;; converted the reference to the escape function into a constant
470 ;;; reference to the NLX-INFO.)
471 (defoptimizer (%unwind-protect ltn-annotate) ((escape cleanup)
474 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
475 (setf (basic-combination-info node) :funny)
476 (setf (node-tail-p node) nil))
478 ;;;; known call annotation
480 ;;; Return true if RESTR is satisfied by TYPE. If T-OK is true, then a
481 ;;; T restriction allows any operand type. This is also called by IR2
482 ;;; translation when it determines whether a result temporary needs to
483 ;;; be made, and by representation selection when it is deciding which
484 ;;; move VOP to use. CONT and TN are used to test for constant
486 (defun operand-restriction-ok (restr type &key cont tn (t-ok t))
487 (declare (type (or (member *) cons) restr)
488 (type primitive-type type)
489 (type (or continuation null) cont)
490 (type (or tn null) tn))
495 (dolist (mem (rest restr) nil)
496 (when (or (and t-ok (eq mem *backend-t-primitive-type*))
501 (and (constant-continuation-p cont)
502 (funcall (second restr) (continuation-value cont))))
504 (and (eq (tn-kind tn) :constant)
505 (funcall (second restr) (tn-value tn))))
507 (error "Neither CONT nor TN supplied.")))))))
509 ;;; Check that the argument type restriction for TEMPLATE are
510 ;;; satisfied in call. If an argument's TYPE-CHECK is :NO-CHECK and
511 ;;; our policy is safe, then only :SAFE templates are OK.
512 (defun template-args-ok (template call safe-p)
513 (declare (type template template)
514 (type combination call))
515 (let ((mtype (template-more-args-type template)))
516 (do ((args (basic-combination-args call) (cdr args))
517 (types (template-arg-types template) (cdr types)))
519 (cond ((null args) t)
523 (unless (operand-restriction-ok mtype
524 (continuation-ptype arg))
526 (when (null args) (return nil))
527 (let ((arg (car args))
529 (when (and (eq (continuation-type-check arg) :no-check)
531 (not (eq (template-ltn-policy template) :safe)))
533 (unless (operand-restriction-ok type (continuation-ptype arg)
537 ;;; Check that TEMPLATE can be used with the specifed RESULT-TYPE.
538 ;;; Result type checking is pretty different from argument type
539 ;;; checking due to the relaxed rules for values count. We succeed if
540 ;;; for each required result, there is a positional restriction on the
541 ;;; value that is at least as good. If we run out of result types
542 ;;; before we run out of restrictions, then we only succeed if the
543 ;;; leftover restrictions are *. If we run out of restrictions before
544 ;;; we run out of result types, then we always win.
545 (defun template-results-ok (template result-type)
546 (declare (type template template)
547 (type ctype result-type))
548 (when (template-more-results-type template)
549 (error "~S has :MORE results with :TRANSLATE." (template-name template)))
550 (let ((types (template-result-types template)))
552 ((values-type-p result-type)
553 (do ((ltypes (append (args-type-required result-type)
554 (args-type-optional result-type))
556 (types types (rest types)))
558 (dolist (type types t)
561 (when (null types) (return t))
562 (let ((type (first types)))
563 (unless (operand-restriction-ok type
564 (primitive-type (first ltypes)))
567 (operand-restriction-ok (first types) (primitive-type result-type)))
570 ;;; Return true if CALL is an ok use of TEMPLATE according to SAFE-P.
571 ;;; -- If the template has a GUARD that isn't true, then we ignore the
572 ;;; template, not even considering it to be rejected.
573 ;;; -- If the argument type restrictions aren't satisfied, then we
574 ;;; reject the template.
575 ;;; -- If the template is :CONDITIONAL, then we accept it only when the
576 ;;; destination of the value is an immediately following IF node.
577 ;;; -- If either the template is safe or the policy is unsafe (i.e. we
578 ;;; can believe output assertions), then we test against the
579 ;;; intersection of the node derived type and the continuation
580 ;;; asserted type. Otherwise, we just use the node type. If
581 ;;; TYPE-CHECK is null, there is no point in doing the intersection,
582 ;;; since the node type must be a subtype of the assertion.
584 ;;; If the template is *not* ok, then the second value is a keyword
585 ;;; indicating which aspect failed.
586 (defun is-ok-template-use (template call safe-p)
587 (declare (type template template) (type combination call))
588 (let* ((guard (template-guard template))
589 (cont (node-cont call))
590 (atype (continuation-asserted-type cont))
591 (dtype (node-derived-type call)))
592 (cond ((and guard (not (funcall guard)))
594 ((not (template-args-ok template call safe-p))
596 (if (and safe-p (template-args-ok template call nil))
599 ((eq (template-result-types template) :conditional)
600 (let ((dest (continuation-dest cont)))
602 (immediately-used-p (if-test dest) call))
604 (values nil :conditional))))
605 ((template-results-ok
607 (if (and (or (eq (template-ltn-policy template) :safe)
609 (continuation-type-check cont))
610 (values-type-intersection dtype atype)
614 (values nil :result-types)))))
616 ;;; Use operand type information to choose a template from the list
617 ;;; TEMPLATES for a known CALL. We return three values:
618 ;;; 1. The template we found.
619 ;;; 2. Some template that we rejected due to unsatisfied type restrictions, or
621 ;;; 3. The tail of Templates for templates we haven't examined yet.
623 ;;; We just call IS-OK-TEMPLATE-USE until it returns true.
624 (defun find-template (templates call safe-p)
625 (declare (list templates) (type combination call))
626 (do ((templates templates (rest templates))
629 (values nil rejected nil))
630 (let ((template (first templates)))
631 (when (is-ok-template-use template call safe-p)
632 (return (values template rejected (rest templates))))
633 (setq rejected template))))
635 ;;; Given a partially annotated known call and a translation policy,
636 ;;; return the appropriate template, or NIL if none can be found. We
637 ;;; scan the templates (ordered by increasing cost) looking for a
638 ;;; template whose restrictions are satisfied and that has our policy.
640 ;;; If we find a template that doesn't have our policy, but has a
641 ;;; legal alternate policy, then we also record that to return as a
642 ;;; last resort. If our policy is safe, then only safe policies are
643 ;;; O.K., otherwise anything goes.
645 ;;; If we find a template with :SAFE policy, then we return it, or any
646 ;;; cheaper fallback template. The theory behind this is that if it is
647 ;;; cheapest, small and safe, we can't lose. If it is not cheapest,
648 ;;; then we use the fallback, which won't have the desired policy, but
649 ;;; :SAFE isn't desired either, so we might as well go with the
650 ;;; cheaper one. The main reason for doing this is to make sure that
651 ;;; cheap safe templates are used when they apply and the current
652 ;;; policy is something else. This is useful because :SAFE has the
653 ;;; additional semantics of implicit argument type checking, so we may
654 ;;; be forced to define a template with :SAFE policy when it is really
655 ;;; small and fast as well.
656 (defun find-template-for-ltn-policy (call ltn-policy)
657 (declare (type combination call)
658 (type ltn-policy ltn-policy))
659 (let ((safe-p (ltn-policy-safe-p ltn-policy))
660 (current (fun-info-templates (basic-combination-kind call)))
664 (multiple-value-bind (template this-reject more)
665 (find-template current call safe-p)
667 (setq rejected this-reject))
670 (return (values fallback rejected)))
671 (let ((tcpolicy (template-ltn-policy template)))
672 (cond ((eq tcpolicy ltn-policy)
673 (return (values template rejected)))
675 (return (values (or fallback template) rejected)))
676 ((or (not safe-p) (eq tcpolicy :fast-safe))
678 (setq fallback template)))))))))
680 (defvar *efficiency-note-limit* 2
682 "This is the maximum number of possible optimization alternatives will be
683 mentioned in a particular efficiency note. NIL means no limit.")
684 (declaim (type (or index null) *efficiency-note-limit*))
686 (defvar *efficiency-note-cost-threshold* 5
688 "This is the minumum cost difference between the chosen implementation and
689 the next alternative that justifies an efficiency note.")
690 (declaim (type index *efficiency-note-cost-threshold*))
692 ;;; This function is called by NOTE-REJECTED-TEMPLATES when it can't
693 ;;; figure out any reason why TEMPLATE was rejected. Users should
694 ;;; never see these messages, but they can happen in situations where
695 ;;; the VM definition is messed up somehow.
696 (defun strange-template-failure (template call ltn-policy frob)
697 (declare (type template template) (type combination call)
698 (type ltn-policy ltn-policy) (type function frob))
699 (funcall frob "This shouldn't happen! Bug?")
700 (multiple-value-bind (win why)
701 (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
705 (funcall frob "template guard failed"))
707 (funcall frob "The template isn't safe, yet we were counting on it."))
709 (funcall frob "argument types invalid")
710 (funcall frob "argument primitive types:~% ~S"
713 (continuation-ptype x)))
714 (combination-args call)))
715 (funcall frob "argument type assertions:~% ~S"
720 (:or `(:or .,(mapcar #'primitive-type-name
722 (:constant `(:constant ,(third x))))))
723 (template-arg-types template))))
725 (funcall frob "conditional in a non-conditional context"))
727 (funcall frob "result types invalid")))))
729 ;;; This function emits efficiency notes describing all of the
730 ;;; templates better (faster) than TEMPLATE that we might have been
731 ;;; able to use if there were better type declarations. Template is
732 ;;; null when we didn't find any template, and thus must do a full
735 ;;; In order to be worth complaining about, a template must:
736 ;;; -- be allowed by its guard,
737 ;;; -- be safe if the current policy is safe,
738 ;;; -- have argument/result type restrictions consistent with the
739 ;;; known type information, e.g. we don't consider float templates
740 ;;; when an operand is known to be an integer,
741 ;;; -- be disallowed by the stricter operand subtype test (which
742 ;;; resembles, but is not identical to the test done by
745 ;;; Note that there may not be any possibly applicable templates,
746 ;;; since we are called whenever any template is rejected. That
747 ;;; template might have the wrong policy or be inconsistent with the
750 ;;; We go to some trouble to make the whole multi-line output into a
751 ;;; single call to COMPILER-NOTE so that repeat messages are
753 (defun note-rejected-templates (call ltn-policy template)
754 (declare (type combination call) (type ltn-policy ltn-policy)
755 (type (or template null) template))
758 (let ((safe-p (ltn-policy-safe-p ltn-policy))
759 (verbose-p (policy call (= inhibit-warnings 0)))
760 (max-cost (- (template-cost
762 (template-or-lose 'call-named)))
763 *efficiency-note-cost-threshold*)))
764 (dolist (try (fun-info-templates (basic-combination-kind call)))
765 (when (> (template-cost try) max-cost) (return)) ; FIXME: UNLESS'd be cleaner.
766 (let ((guard (template-guard try)))
767 (when (and (or (not guard) (funcall guard))
769 (ltn-policy-safe-p (template-ltn-policy try)))
771 (and (template-note try)
773 call (template-type try)
774 :argument-test #'types-equal-or-intersect
776 #'values-types-equal-or-intersect))))
782 (flet ((lose1 (string &rest stuff)
785 (dolist (loser (losers))
786 (when (and *efficiency-note-limit*
787 (>= (count) *efficiency-note-limit*))
790 (let* ((type (template-type loser))
791 (valid (valid-fun-use call type))
792 (strict-valid (valid-fun-use call type
794 (lose1 "unable to do ~A (cost ~W) because:"
795 (or (template-note loser) (template-name loser))
796 (template-cost loser))
798 ((and valid strict-valid)
799 (strange-template-failure loser call ltn-policy #'lose1))
801 (aver (not (valid-fun-use call type
803 :unwinnage-fun #'lose1))))
805 (aver (ltn-policy-safe-p ltn-policy))
806 (lose1 "can't trust output type assertion under safe policy")))
809 (let ((*compiler-error-context* call))
810 (compiler-note "~{~?~^~&~6T~}"
812 `("forced to do ~A (cost ~W)"
813 (,(or (template-note template)
814 (template-name template))
815 ,(template-cost template))
817 `("forced to do full call"
822 ;;; Flush type checks according to policy. If the policy is
823 ;;; unsafe, then we never do any checks. If our policy is safe, and
824 ;;; we are using a safe template, then we can also flush arg and
825 ;;; result type checks. Result type checks are only flushed when the
826 ;;; continuation as a single use. Result type checks are not flush if
827 ;;; the policy is safe because the selection of template for results
828 ;;; readers assumes the type check is done (uses the derived type
829 ;;; which is the intersection of the proven and asserted types).
830 (defun flush-type-checks-according-to-ltn-policy (call ltn-policy template)
831 (declare (type combination call) (type ltn-policy ltn-policy)
832 (type template template))
833 (let ((safe-op (eq (template-ltn-policy template) :safe)))
834 (when (or (not (ltn-policy-safe-p ltn-policy)) safe-op)
835 (dolist (arg (basic-combination-args call))
836 (flush-type-check arg)))
838 (let ((cont (node-cont call)))
839 (when (and (eq (continuation-use cont) call)
840 (not (ltn-policy-safe-p ltn-policy)))
841 (flush-type-check cont)))))
845 ;;; If a function has a special-case annotation method use that,
846 ;;; otherwise annotate the argument continuations and try to find a
847 ;;; template corresponding to the type signature. If there is none,
848 ;;; convert a full call.
849 (defun ltn-analyze-known-call (call ltn-policy)
850 (declare (type combination call)
851 (type ltn-policy ltn-policy))
852 (let ((method (fun-info-ltn-annotate (basic-combination-kind call)))
853 (args (basic-combination-args call)))
855 (funcall method call ltn-policy)
856 (return-from ltn-analyze-known-call (values)))
859 (setf (continuation-info arg)
860 (make-ir2-continuation (primitive-type (continuation-type arg)))))
862 (multiple-value-bind (template rejected)
863 (find-template-for-ltn-policy call ltn-policy)
864 ;; If we are unable to use some templates due to unsatisfied
865 ;; operand type restrictions and our policy enables efficiency
866 ;; notes, then we call NOTE-REJECTED-TEMPLATES.
868 (policy call (> speed inhibit-warnings)))
869 (note-rejected-templates call ltn-policy template))
870 ;; If we are forced to do a full call, we check to see whether
871 ;; the function called is the same as the current function. If
872 ;; so, we give a warning, as this is probably a botched attempt
873 ;; to implement an out-of-line version in terms of inline
874 ;; transforms or VOPs or whatever.
876 (when (let ((funleaf (physenv-lambda (node-physenv call))))
877 (and (leaf-has-source-name-p funleaf)
878 (eq (continuation-fun-name (combination-fun call))
879 (leaf-source-name funleaf))
880 (let ((info (basic-combination-kind call)))
881 (not (or (fun-info-ir2-convert info)
882 (ir1-attributep (fun-info-attributes info)
884 (let ((*compiler-error-context* call))
885 (compiler-warn "~@<recursion in known function definition~2I ~
886 ~_policy=~S ~_arg types=~S~:>"
887 (lexenv-policy (node-lexenv call))
888 (mapcar (lambda (arg)
889 (type-specifier (continuation-type arg)))
891 (ltn-default-call call ltn-policy)
892 (return-from ltn-analyze-known-call (values)))
893 (setf (basic-combination-info call) template)
894 (setf (node-tail-p call) nil)
896 (flush-type-checks-according-to-ltn-policy call ltn-policy template)
899 (annotate-1-value-continuation arg))))
905 ;;; most of the guts of the two interface functions: Compute the
906 ;;; policy and dispatch to the appropriate node-specific function.
908 ;;; Note: we deliberately don't use the DO-NODES macro, since the
909 ;;; block can be split out from underneath us, and DO-NODES would scan
910 ;;; past the block end in that case.
911 (defun ltn-analyze-block (block)
912 (do* ((node (continuation-next (block-start block))
913 (continuation-next cont))
914 (cont (node-cont node) (node-cont node))
915 (ltn-policy (node-ltn-policy node) (node-ltn-policy node)))
920 (case (basic-combination-kind node)
921 (:local (ltn-analyze-local-call node ltn-policy))
922 ((:full :error) (ltn-default-call node ltn-policy))
924 (ltn-analyze-known-call node ltn-policy))))
926 (ltn-analyze-if node ltn-policy))
928 (ltn-analyze-return node ltn-policy))
931 (ltn-analyze-exit node ltn-policy))
932 (cset (ltn-analyze-set node ltn-policy))
934 (ecase (basic-combination-kind node)
936 (ltn-analyze-mv-bind node ltn-policy))
938 (ltn-analyze-mv-call node ltn-policy)))))
939 (when (eq node (block-last block))
942 ;;; Loop over the blocks in COMPONENT, doing stuff to nodes that
943 ;;; receive values. In addition to the stuff done by FROB, we also see
944 ;;; whether there are any unknown values receivers, making notations
945 ;;; in the components' GENERATORS and RECEIVERS as appropriate.
947 ;;; If any unknown-values continations are received by this block (as
948 ;;; indicated by IR2-BLOCK-POPPED), then we add the block to the
949 ;;; IR2-COMPONENT-VALUES-RECEIVERS.
951 ;;; This is where we allocate IR2 blocks because it is the first place
953 (defun ltn-analyze (component)
954 (declare (type component component))
955 (let ((2comp (component-info component)))
956 (do-blocks (block component)
957 ;; This assertion seems to protect us from compiling a component
958 ;; twice. As noted above, "this is where we allocate IR2-BLOCKS
959 ;; because it is the first place we need them", so if one is
960 ;; already allocated here, something is wrong. -- WHN 2001-09-14
961 (aver (not (block-info block)))
962 (let ((2block (make-ir2-block block)))
963 (setf (block-info block) 2block)
964 (ltn-analyze-block block)
965 (let ((popped (ir2-block-popped 2block)))
967 (push block (ir2-component-values-receivers 2comp)))))))
970 ;;; This function is used to analyze blocks that must be added to the
971 ;;; flow graph after the normal LTN phase runs. Such code is
972 ;;; constrained not to use weird unknown values (and probably in lots
974 (defun ltn-analyze-belated-block (block)
975 (declare (type cblock block))
976 (ltn-analyze-block block)
977 (aver (not (ir2-block-popped (block-info block))))