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.
416 (defun set-tail-local-call-successor (call)
417 (let ((caller (node-home-lambda call))
418 (callee (combination-lambda call)))
419 (aver (eq (lambda-tail-set caller)
420 (lambda-tail-set (lambda-home callee))))
421 (node-ends-block call)
422 (let ((block (node-block call)))
423 (unlink-blocks block (first (block-succ block)))
424 (link-blocks block (lambda-block callee))))
427 ;;; Annotate the value continuation.
428 (defun ltn-analyze-set (node ltn-policy)
429 (declare (type cset node) (type ltn-policy ltn-policy))
430 (setf (node-tail-p node) nil)
431 (annotate-ordinary-continuation (set-value node) ltn-policy)
434 ;;; If the only use of the TEST continuation is a combination
435 ;;; annotated with a conditional template, then don't annotate the
436 ;;; continuation so that IR2 conversion knows not to emit any code,
437 ;;; otherwise annotate as an ordinary continuation. Since we only use
438 ;;; a conditional template if the call immediately precedes the IF
439 ;;; node in the same block, we know that any predicate will already be
441 (defun ltn-analyze-if (node ltn-policy)
442 (declare (type cif node) (type ltn-policy ltn-policy))
443 (setf (node-tail-p node) nil)
444 (let* ((test (if-test node))
445 (use (continuation-use test)))
446 (unless (and (combination-p use)
447 (let ((info (basic-combination-info use)))
448 (and (template-p info)
449 (eq (template-result-types info) :conditional))))
450 (annotate-ordinary-continuation test ltn-policy)))
453 ;;; If there is a value continuation, then annotate it for unknown
454 ;;; values. In this case, the exit is non-local, since all other exits
455 ;;; are deleted or degenerate by this point.
456 (defun ltn-analyze-exit (node ltn-policy)
457 (setf (node-tail-p node) nil)
458 (let ((value (exit-value node)))
460 (annotate-unknown-values-continuation value ltn-policy)))
463 ;;; We need a special method for %UNWIND-PROTECT that ignores the
464 ;;; cleanup function. We don't annotate either arg, since we don't
465 ;;; need them at run-time.
467 ;;; (The default is o.k. for %CATCH, since environment analysis
468 ;;; converted the reference to the escape function into a constant
469 ;;; reference to the NLX-INFO.)
470 (defoptimizer (%unwind-protect ltn-annotate) ((escape cleanup)
473 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
474 (setf (basic-combination-info node) :funny)
475 (setf (node-tail-p node) nil))
477 ;;;; known call annotation
479 ;;; Return true if RESTR is satisfied by TYPE. If T-OK is true, then a
480 ;;; T restriction allows any operand type. This is also called by IR2
481 ;;; translation when it determines whether a result temporary needs to
482 ;;; be made, and by representation selection when it is deciding which
483 ;;; move VOP to use. CONT and TN are used to test for constant
485 (defun operand-restriction-ok (restr type &key cont tn (t-ok t))
486 (declare (type (or (member *) cons) restr)
487 (type primitive-type type)
488 (type (or continuation null) cont)
489 (type (or tn null) tn))
494 (dolist (mem (rest restr) nil)
495 (when (or (and t-ok (eq mem *backend-t-primitive-type*))
500 (and (constant-continuation-p cont)
501 (funcall (second restr) (continuation-value cont))))
503 (and (eq (tn-kind tn) :constant)
504 (funcall (second restr) (tn-value tn))))
506 (error "Neither CONT nor TN supplied.")))))))
508 ;;; Check that the argument type restriction for TEMPLATE are
509 ;;; satisfied in call. If an argument's TYPE-CHECK is :NO-CHECK and
510 ;;; our policy is safe, then only :SAFE templates are OK.
511 (defun template-args-ok (template call safe-p)
512 (declare (type template template)
513 (type combination call))
514 (let ((mtype (template-more-args-type template)))
515 (do ((args (basic-combination-args call) (cdr args))
516 (types (template-arg-types template) (cdr types)))
518 (cond ((null args) t)
522 (unless (operand-restriction-ok mtype
523 (continuation-ptype arg))
525 (when (null args) (return nil))
526 (let ((arg (car args))
528 (when (and (eq (continuation-type-check arg) :no-check)
530 (not (eq (template-ltn-policy template) :safe)))
532 (unless (operand-restriction-ok type (continuation-ptype arg)
536 ;;; Check that TEMPLATE can be used with the specifed RESULT-TYPE.
537 ;;; Result type checking is pretty different from argument type
538 ;;; checking due to the relaxed rules for values count. We succeed if
539 ;;; for each required result, there is a positional restriction on the
540 ;;; value that is at least as good. If we run out of result types
541 ;;; before we run out of restrictions, then we only succeed if the
542 ;;; leftover restrictions are *. If we run out of restrictions before
543 ;;; we run out of result types, then we always win.
544 (defun template-results-ok (template result-type)
545 (declare (type template template)
546 (type ctype result-type))
547 (when (template-more-results-type template)
548 (error "~S has :MORE results with :TRANSLATE." (template-name template)))
549 (let ((types (template-result-types template)))
551 ((values-type-p result-type)
552 (do ((ltypes (append (args-type-required result-type)
553 (args-type-optional result-type))
555 (types types (rest types)))
557 (dolist (type types t)
560 (when (null types) (return t))
561 (let ((type (first types)))
562 (unless (operand-restriction-ok type
563 (primitive-type (first ltypes)))
566 (operand-restriction-ok (first types) (primitive-type result-type)))
569 ;;; Return true if CALL is an ok use of TEMPLATE according to SAFE-P.
570 ;;; -- If the template has a GUARD that isn't true, then we ignore the
571 ;;; template, not even considering it to be rejected.
572 ;;; -- If the argument type restrictions aren't satisfied, then we
573 ;;; reject the template.
574 ;;; -- If the template is :CONDITIONAL, then we accept it only when the
575 ;;; destination of the value is an immediately following IF node.
576 ;;; -- If either the template is safe or the policy is unsafe (i.e. we
577 ;;; can believe output assertions), then we test against the
578 ;;; intersection of the node derived type and the continuation
579 ;;; asserted type. Otherwise, we just use the node type. If
580 ;;; TYPE-CHECK is null, there is no point in doing the intersection,
581 ;;; since the node type must be a subtype of the assertion.
583 ;;; If the template is *not* ok, then the second value is a keyword
584 ;;; indicating which aspect failed.
585 (defun is-ok-template-use (template call safe-p)
586 (declare (type template template) (type combination call))
587 (let* ((guard (template-guard template))
588 (cont (node-cont call))
589 (atype (continuation-asserted-type cont))
590 (dtype (node-derived-type call)))
591 (cond ((and guard (not (funcall guard)))
593 ((not (template-args-ok template call safe-p))
595 (if (and safe-p (template-args-ok template call nil))
598 ((eq (template-result-types template) :conditional)
599 (let ((dest (continuation-dest cont)))
601 (immediately-used-p (if-test dest) call))
603 (values nil :conditional))))
604 ((template-results-ok
606 (if (and (or (eq (template-ltn-policy template) :safe)
608 (continuation-type-check cont))
609 (values-type-intersection dtype atype)
613 (values nil :result-types)))))
615 ;;; Use operand type information to choose a template from the list
616 ;;; TEMPLATES for a known CALL. We return three values:
617 ;;; 1. The template we found.
618 ;;; 2. Some template that we rejected due to unsatisfied type restrictions, or
620 ;;; 3. The tail of Templates for templates we haven't examined yet.
622 ;;; We just call IS-OK-TEMPLATE-USE until it returns true.
623 (defun find-template (templates call safe-p)
624 (declare (list templates) (type combination call))
625 (do ((templates templates (rest templates))
628 (values nil rejected nil))
629 (let ((template (first templates)))
630 (when (is-ok-template-use template call safe-p)
631 (return (values template rejected (rest templates))))
632 (setq rejected template))))
634 ;;; Given a partially annotated known call and a translation policy,
635 ;;; return the appropriate template, or NIL if none can be found. We
636 ;;; scan the templates (ordered by increasing cost) looking for a
637 ;;; template whose restrictions are satisfied and that has our policy.
639 ;;; If we find a template that doesn't have our policy, but has a
640 ;;; legal alternate policy, then we also record that to return as a
641 ;;; last resort. If our policy is safe, then only safe policies are
642 ;;; O.K., otherwise anything goes.
644 ;;; If we find a template with :SAFE policy, then we return it, or any
645 ;;; cheaper fallback template. The theory behind this is that if it is
646 ;;; cheapest, small and safe, we can't lose. If it is not cheapest,
647 ;;; then we use the fallback, which won't have the desired policy, but
648 ;;; :SAFE isn't desired either, so we might as well go with the
649 ;;; cheaper one. The main reason for doing this is to make sure that
650 ;;; cheap safe templates are used when they apply and the current
651 ;;; policy is something else. This is useful because :SAFE has the
652 ;;; additional semantics of implicit argument type checking, so we may
653 ;;; be forced to define a template with :SAFE policy when it is really
654 ;;; small and fast as well.
655 (defun find-template-for-ltn-policy (call ltn-policy)
656 (declare (type combination call)
657 (type ltn-policy ltn-policy))
658 (let ((safe-p (ltn-policy-safe-p ltn-policy))
659 (current (fun-info-templates (basic-combination-kind call)))
663 (multiple-value-bind (template this-reject more)
664 (find-template current call safe-p)
666 (setq rejected this-reject))
669 (return (values fallback rejected)))
670 (let ((tcpolicy (template-ltn-policy template)))
671 (cond ((eq tcpolicy ltn-policy)
672 (return (values template rejected)))
674 (return (values (or fallback template) rejected)))
675 ((or (not safe-p) (eq tcpolicy :fast-safe))
677 (setq fallback template)))))))))
679 (defvar *efficiency-note-limit* 2
681 "This is the maximum number of possible optimization alternatives will be
682 mentioned in a particular efficiency note. NIL means no limit.")
683 (declaim (type (or index null) *efficiency-note-limit*))
685 (defvar *efficiency-note-cost-threshold* 5
687 "This is the minumum cost difference between the chosen implementation and
688 the next alternative that justifies an efficiency note.")
689 (declaim (type index *efficiency-note-cost-threshold*))
691 ;;; This function is called by NOTE-REJECTED-TEMPLATES when it can't
692 ;;; figure out any reason why TEMPLATE was rejected. Users should
693 ;;; never see these messages, but they can happen in situations where
694 ;;; the VM definition is messed up somehow.
695 (defun strange-template-failure (template call ltn-policy frob)
696 (declare (type template template) (type combination call)
697 (type ltn-policy ltn-policy) (type function frob))
698 (funcall frob "This shouldn't happen! Bug?")
699 (multiple-value-bind (win why)
700 (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
704 (funcall frob "template guard failed"))
706 (funcall frob "The template isn't safe, yet we were counting on it."))
708 (funcall frob "argument types invalid")
709 (funcall frob "argument primitive types:~% ~S"
712 (continuation-ptype x)))
713 (combination-args call)))
714 (funcall frob "argument type assertions:~% ~S"
719 (:or `(:or .,(mapcar #'primitive-type-name
721 (:constant `(:constant ,(third x))))))
722 (template-arg-types template))))
724 (funcall frob "conditional in a non-conditional context"))
726 (funcall frob "result types invalid")))))
728 ;;; This function emits efficiency notes describing all of the
729 ;;; templates better (faster) than TEMPLATE that we might have been
730 ;;; able to use if there were better type declarations. Template is
731 ;;; null when we didn't find any template, and thus must do a full
734 ;;; In order to be worth complaining about, a template must:
735 ;;; -- be allowed by its guard,
736 ;;; -- be safe if the current policy is safe,
737 ;;; -- have argument/result type restrictions consistent with the
738 ;;; known type information, e.g. we don't consider float templates
739 ;;; when an operand is known to be an integer,
740 ;;; -- be disallowed by the stricter operand subtype test (which
741 ;;; resembles, but is not identical to the test done by
744 ;;; Note that there may not be any possibly applicable templates,
745 ;;; since we are called whenever any template is rejected. That
746 ;;; template might have the wrong policy or be inconsistent with the
749 ;;; We go to some trouble to make the whole multi-line output into a
750 ;;; single call to COMPILER-NOTE so that repeat messages are
752 (defun note-rejected-templates (call ltn-policy template)
753 (declare (type combination call) (type ltn-policy ltn-policy)
754 (type (or template null) template))
757 (let ((safe-p (ltn-policy-safe-p ltn-policy))
758 (verbose-p (policy call (= inhibit-warnings 0)))
759 (max-cost (- (template-cost
761 (template-or-lose 'call-named)))
762 *efficiency-note-cost-threshold*)))
763 (dolist (try (fun-info-templates (basic-combination-kind call)))
764 (when (> (template-cost try) max-cost) (return)) ; FIXME: UNLESS'd be cleaner.
765 (let ((guard (template-guard try)))
766 (when (and (or (not guard) (funcall guard))
768 (ltn-policy-safe-p (template-ltn-policy try)))
770 (and (template-note try)
772 call (template-type try)
773 :argument-test #'types-equal-or-intersect
775 #'values-types-equal-or-intersect))))
781 (flet ((lose1 (string &rest stuff)
784 (dolist (loser (losers))
785 (when (and *efficiency-note-limit*
786 (>= (count) *efficiency-note-limit*))
789 (let* ((type (template-type loser))
790 (valid (valid-fun-use call type))
791 (strict-valid (valid-fun-use call type
793 (lose1 "unable to do ~A (cost ~W) because:"
794 (or (template-note loser) (template-name loser))
795 (template-cost loser))
797 ((and valid strict-valid)
798 (strange-template-failure loser call ltn-policy #'lose1))
800 (aver (not (valid-fun-use call type
802 :unwinnage-fun #'lose1))))
804 (aver (ltn-policy-safe-p ltn-policy))
805 (lose1 "can't trust output type assertion under safe policy")))
808 (let ((*compiler-error-context* call))
809 (compiler-note "~{~?~^~&~6T~}"
811 `("forced to do ~A (cost ~W)"
812 (,(or (template-note template)
813 (template-name template))
814 ,(template-cost template))
816 `("forced to do full call"
821 ;;; Flush type checks according to policy. If the policy is
822 ;;; unsafe, then we never do any checks. If our policy is safe, and
823 ;;; we are using a safe template, then we can also flush arg and
824 ;;; result type checks. Result type checks are only flushed when the
825 ;;; continuation has a single use. Result type checks are not flush if
826 ;;; the policy is safe because the selection of template for results
827 ;;; readers assumes the type check is done (uses the derived type
828 ;;; which is the intersection of the proven and asserted types).
829 (defun flush-type-checks-according-to-ltn-policy (call ltn-policy template)
830 (declare (type combination call) (type ltn-policy ltn-policy)
831 (type template template))
832 (let ((safe-op (eq (template-ltn-policy template) :safe)))
833 (when (or (not (ltn-policy-safe-p ltn-policy)) safe-op)
834 (dolist (arg (basic-combination-args call))
835 (flush-type-check arg)))
837 (let ((cont (node-cont call)))
838 (when (and (eq (continuation-use cont) call)
839 (not (ltn-policy-safe-p ltn-policy)))
840 (flush-type-check cont)))))
844 ;;; If a function has a special-case annotation method use that,
845 ;;; otherwise annotate the argument continuations and try to find a
846 ;;; template corresponding to the type signature. If there is none,
847 ;;; convert a full call.
848 (defun ltn-analyze-known-call (call ltn-policy)
849 (declare (type combination call)
850 (type ltn-policy ltn-policy))
851 (let ((method (fun-info-ltn-annotate (basic-combination-kind call)))
852 (args (basic-combination-args call)))
854 (funcall method call ltn-policy)
855 (return-from ltn-analyze-known-call (values)))
858 (setf (continuation-info arg)
859 (make-ir2-continuation (primitive-type (continuation-type arg)))))
861 (multiple-value-bind (template rejected)
862 (find-template-for-ltn-policy call ltn-policy)
863 ;; If we are unable to use some templates due to unsatisfied
864 ;; operand type restrictions and our policy enables efficiency
865 ;; notes, then we call NOTE-REJECTED-TEMPLATES.
867 (policy call (> speed inhibit-warnings)))
868 (note-rejected-templates call ltn-policy template))
869 ;; If we are forced to do a full call, we check to see whether
870 ;; the function called is the same as the current function. If
871 ;; so, we give a warning, as this is probably a botched attempt
872 ;; to implement an out-of-line version in terms of inline
873 ;; transforms or VOPs or whatever.
875 (when (let ((funleaf (physenv-lambda (node-physenv call))))
876 (and (leaf-has-source-name-p funleaf)
877 (eq (continuation-fun-name (combination-fun call))
878 (leaf-source-name funleaf))
879 (let ((info (basic-combination-kind call)))
880 (not (or (fun-info-ir2-convert info)
881 (ir1-attributep (fun-info-attributes info)
883 (let ((*compiler-error-context* call))
884 (compiler-warn "~@<recursion in known function definition~2I ~
885 ~_policy=~S ~_arg types=~S~:>"
886 (lexenv-policy (node-lexenv call))
887 (mapcar (lambda (arg)
888 (type-specifier (continuation-type arg)))
890 (ltn-default-call call ltn-policy)
891 (return-from ltn-analyze-known-call (values)))
892 (setf (basic-combination-info call) template)
893 (setf (node-tail-p call) nil)
895 (flush-type-checks-according-to-ltn-policy call ltn-policy template)
898 (annotate-1-value-continuation arg))))
904 ;;; most of the guts of the two interface functions: Compute the
905 ;;; policy and dispatch to the appropriate node-specific function.
907 ;;; Note: we deliberately don't use the DO-NODES macro, since the
908 ;;; block can be split out from underneath us, and DO-NODES would scan
909 ;;; past the block end in that case.
910 (defun ltn-analyze-block (block)
911 (do* ((node (continuation-next (block-start block))
912 (continuation-next cont))
913 (cont (node-cont node) (node-cont node))
914 (ltn-policy (node-ltn-policy node) (node-ltn-policy node)))
919 (case (basic-combination-kind node)
920 (:local (ltn-analyze-local-call node ltn-policy))
921 ((:full :error) (ltn-default-call node ltn-policy))
923 (ltn-analyze-known-call node ltn-policy))))
925 (ltn-analyze-if node ltn-policy))
927 (ltn-analyze-return node ltn-policy))
930 (ltn-analyze-exit node ltn-policy))
931 (cset (ltn-analyze-set node ltn-policy))
933 (ecase (basic-combination-kind node)
935 (ltn-analyze-mv-bind node ltn-policy))
937 (ltn-analyze-mv-call node ltn-policy)))))
938 (when (eq node (block-last block))
941 ;;; Loop over the blocks in COMPONENT, doing stuff to nodes that
942 ;;; receive values. In addition to the stuff done by FROB, we also see
943 ;;; whether there are any unknown values receivers, making notations
944 ;;; in the components' GENERATORS and RECEIVERS as appropriate.
946 ;;; If any unknown-values continations are received by this block (as
947 ;;; indicated by IR2-BLOCK-POPPED), then we add the block to the
948 ;;; IR2-COMPONENT-VALUES-RECEIVERS.
950 ;;; This is where we allocate IR2 blocks because it is the first place
952 (defun ltn-analyze (component)
953 (declare (type component component))
954 (let ((2comp (component-info component)))
955 (do-blocks (block component)
956 ;; This assertion seems to protect us from compiling a component
957 ;; twice. As noted above, "this is where we allocate IR2-BLOCKS
958 ;; because it is the first place we need them", so if one is
959 ;; already allocated here, something is wrong. -- WHN 2001-09-14
960 (aver (not (block-info block)))
961 (let ((2block (make-ir2-block block)))
962 (setf (block-info block) 2block)
963 (ltn-analyze-block block)
964 (let ((popped (ir2-block-popped 2block)))
966 (push block (ir2-component-values-receivers 2comp)))))))
969 ;;; This function is used to analyze blocks that must be added to the
970 ;;; flow graph after the normal LTN phase runs. Such code is
971 ;;; constrained not to use weird unknown values (and probably in lots
973 (defun ltn-analyze-belated-block (block)
974 (declare (type cblock block))
975 (ltn-analyze-block block)
976 (aver (not (ir2-block-popped (block-info block))))