Check more thoroughly for duplicate variables in LOOP.
[sbcl.git] / src / compiler / ltn.lisp
blob20d1099ed2ecaa3474a73fb1560b4ae0de86d0b5
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
6 ;;;; more information.
7 ;;;;
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.
14 (in-package "SB!C")
16 ;;;; utilities
18 ;;; Return the LTN-POLICY indicated by the node policy.
19 ;;;
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
24 ;;; things like
25 ;;; (EQ (TEMPLATE-LTN-POLICY TEMPLATE) :SAFE)
26 ;;; don't get overlooked.
27 ;;;
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))
43 (policy 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):
50 compilation-speed)))
51 (if (zerop safety)
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)
57 (ecase ltn-policy
58 ((:safe :fast-safe) t)
59 ((:small :fast) nil)))
61 ;;; For possibly-new blocks, make sure that there is an associated
62 ;;; IR2-BLOCK.
63 (defun ensure-block-has-ir2-block (new-block)
64 ;; If BLOCK-START is NIL then it's the component-tail block (which
65 ;; we don't care about), and if BLOCK-INFO is not null then it
66 ;; doesn't need to be overwritten.
67 (when (and (block-start new-block)
68 (not (block-info new-block)))
69 (setf (block-info new-block)
70 (make-ir2-block new-block))))
72 ;;; When splitting an existing block, make sure that unknown-values
73 ;;; killed information is distributed appropriately.
74 (defun fixup-ir2-blocks-for-split-block (old-block new-block)
75 (ensure-block-has-ir2-block new-block)
76 (let* ((old-ir2-block (block-info old-block))
77 (new-ir2-block (block-info new-block)))
78 (collect ((old-popped) (new-popped))
79 (dolist (lvar (ir2-block-popped old-ir2-block))
80 (if (eq (node-block (lvar-dest lvar)) old-block)
81 (old-popped lvar)
82 (new-popped lvar))
83 (setf (ir2-block-popped old-ir2-block) (old-popped))
84 (setf (ir2-block-popped new-ir2-block) (new-popped))))))
86 ;;; an annotated lvar's primitive-type
87 #!-sb-fluid (declaim (inline lvar-ptype))
88 (defun lvar-ptype (lvar)
89 (declare (type lvar lvar))
90 (ir2-lvar-primitive-type (lvar-info lvar)))
92 ;;; If LVAR is used only by a REF to a leaf that can be delayed, then
93 ;;; return the leaf, otherwise return NIL.
94 (defun lvar-delayed-leaf (lvar)
95 (declare (type lvar lvar))
96 (unless (lvar-dynamic-extent lvar)
97 (let ((use (lvar-uses lvar)))
98 (and (ref-p use)
99 (let ((leaf (ref-leaf use)))
100 (etypecase leaf
101 (lambda-var (if (null (lambda-var-sets leaf)) leaf nil))
102 (constant leaf)
103 ((or functional global-var) nil)))))))
105 ;;; Annotate a normal single-value lvar. If its only use is a ref that
106 ;;; we are allowed to delay the evaluation of, then we mark the lvar
107 ;;; for delayed evaluation, otherwise we assign a TN to hold the
108 ;;; lvar's value.
109 (defun annotate-1-value-lvar (lvar)
110 (declare (type lvar lvar))
111 (let ((info (lvar-info lvar)))
112 (aver (eq (ir2-lvar-kind info) :fixed))
113 (cond
114 ((lvar-delayed-leaf lvar)
115 (setf (ir2-lvar-kind info) :delayed))
116 (t (let ((tn (make-normal-tn (ir2-lvar-primitive-type info))))
117 (setf (ir2-lvar-locs info) (list tn))
118 (when (lvar-dynamic-extent lvar)
119 (setf (ir2-lvar-stack-pointer info)
120 (make-stack-pointer-tn)))))))
121 (ltn-annotate-casts lvar)
122 (values))
124 ;;; Make an IR2-LVAR corresponding to the lvar type and then do
125 ;;; ANNOTATE-1-VALUE-LVAR.
126 (defun annotate-ordinary-lvar (lvar)
127 (declare (type lvar lvar))
128 (let ((info (make-ir2-lvar
129 (primitive-type (lvar-type lvar)))))
130 (setf (lvar-info lvar) info)
131 (annotate-1-value-lvar lvar))
132 (values))
134 ;;; Annotate the function lvar for a full call. If the only reference
135 ;;; is to a global function and DELAY is true, then we delay the
136 ;;; reference, otherwise we annotate for a single value.
137 (defun annotate-fun-lvar (lvar &optional (delay t))
138 (declare (type lvar lvar))
139 (aver (not (lvar-dynamic-extent lvar)))
140 (let* ((tn-ptype (primitive-type (lvar-type lvar)))
141 (info (make-ir2-lvar tn-ptype)))
142 (setf (lvar-info lvar) info)
143 (let ((name (lvar-fun-name lvar t)))
144 (if (and delay name)
145 (setf (ir2-lvar-kind info) :delayed)
146 (setf (ir2-lvar-locs info)
147 (list (make-normal-tn tn-ptype))))))
148 (ltn-annotate-casts lvar)
149 (values))
151 ;;; If TAIL-P is true, then we check to see whether the call can
152 ;;; really be a tail call by seeing if this function's return
153 ;;; convention is :UNKNOWN. If so, we move the call block successor
154 ;;; link from the return block to the component tail (after ensuring
155 ;;; that they are in separate blocks.) This allows the return to be
156 ;;; deleted when there are no non-tail uses.
157 (defun flush-full-call-tail-transfer (call)
158 (declare (type basic-combination call))
159 (let ((tails (and (node-tail-p call)
160 (lambda-tail-set (node-home-lambda call)))))
161 (when tails
162 (cond ((eq (return-info-kind (tail-set-info tails)) :unknown)
163 (node-ends-block call)
164 (let* ((block (node-block call))
165 (new-block (first (block-succ block))))
166 (fixup-ir2-blocks-for-split-block block new-block)
167 (unlink-blocks block new-block)
168 (link-blocks block (component-tail (block-component block)))))
170 (setf (node-tail-p call) nil)))))
171 (values))
173 ;;; We set the kind to :FULL or :FUNNY, depending on whether there is
174 ;;; an IR2-CONVERT method. If a funny function, then we inhibit tail
175 ;;; recursion normally, since the IR2 convert method is going to want
176 ;;; to deliver values normally. We still annotate the function lvar,
177 ;;; since IR2tran might decide to call after all.
179 ;;; Note that args may already be annotated because template selection
180 ;;; can bail out to here.
181 (defun ltn-default-call (call)
182 (declare (type combination call))
183 (let ((kind (basic-combination-kind call))
184 (info (basic-combination-fun-info call)))
185 (annotate-fun-lvar (basic-combination-fun call))
187 (dolist (arg (basic-combination-args call))
188 (unless (lvar-info arg)
189 (setf (lvar-info arg)
190 (make-ir2-lvar (primitive-type (lvar-type arg)))))
191 (annotate-1-value-lvar arg))
193 (cond
194 ((and (eq kind :known)
195 (fun-info-p info)
196 (fun-info-ir2-convert info))
197 (setf (basic-combination-info call) :funny)
198 (setf (node-tail-p call) nil))
200 (when (eq kind :error)
201 (setf (basic-combination-kind call) :full))
202 (setf (basic-combination-info call) :full)
203 (flush-full-call-tail-transfer call))))
205 (values))
207 ;;; Annotate an lvar for unknown multiple values:
208 ;;; -- Add the lvar to the IR2-BLOCK-POPPED if it is used across a
209 ;;; block boundary.
210 ;;; -- Assign an :UNKNOWN IR2-LVAR.
212 ;;; Note: it is critical that this be called only during LTN analysis
213 ;;; of LVAR's DEST, and called in the order that the lvarss are
214 ;;; received. Otherwise the IR2-BLOCK-POPPED and
215 ;;; IR2-COMPONENT-VALUES-FOO would get all messed up.
216 (defun annotate-unknown-values-lvar (lvar)
217 (declare (type lvar lvar))
219 (aver (not (lvar-dynamic-extent lvar)))
220 (let ((2lvar (make-ir2-lvar nil)))
221 (setf (ir2-lvar-kind 2lvar) :unknown)
222 (setf (ir2-lvar-locs 2lvar) (make-unknown-values-locations))
223 (setf (lvar-info lvar) 2lvar))
225 ;; The CAST chain with corresponding lvars constitute the same
226 ;; "principal lvar", so we must preserve only inner annotation order
227 ;; and the order of the whole p.l. with other lvars. -- APD,
228 ;; 2003-02-27
229 (ltn-annotate-casts lvar)
231 (let* ((block (node-block (lvar-dest lvar)))
232 (use (lvar-uses lvar))
233 (2block (block-info block)))
234 (unless (and (not (listp use)) (eq (node-block use) block))
235 (setf (ir2-block-popped 2block)
236 (nconc (ir2-block-popped 2block) (list lvar)))))
238 (values))
240 ;;; Annotate LVAR for a fixed, but arbitrary number of values, of the
241 ;;; specified primitive TYPES.
242 (defun annotate-fixed-values-lvar (lvar types)
243 (declare (type lvar lvar) (list types))
244 (let ((info (make-ir2-lvar nil)))
245 (setf (ir2-lvar-locs info) (mapcar #'make-normal-tn types))
246 (setf (lvar-info lvar) info)
247 (when (lvar-dynamic-extent lvar)
248 (aver (proper-list-of-length-p types 1))
249 (setf (ir2-lvar-stack-pointer info)
250 (make-stack-pointer-tn))))
251 (ltn-annotate-casts lvar)
252 (values))
254 ;;;; node-specific analysis functions
256 ;;; Annotate the result lvar for a function. We use the RETURN-INFO
257 ;;; computed by GTN to determine how to represent the return values
258 ;;; within the function:
259 ;;; * If the TAIL-SET has a fixed values count, then use that many
260 ;;; values.
261 ;;; * If the actual uses of the result lvar in this function
262 ;;; have a fixed number of values (after intersection with the
263 ;;; assertion), then use that number. We throw out TAIL-P :FULL
264 ;;; and :LOCAL calls, since we know they will truly end up as TR
265 ;;; calls. We can use the BASIC-COMBINATION-INFO even though it
266 ;;; is assigned by this phase, since the initial value NIL doesn't
267 ;;; look like a TR call.
268 ;;; If there are *no* non-tail-call uses, then it falls out
269 ;;; that we annotate for one value (type is NIL), but the return
270 ;;; will end up being deleted.
271 ;;; In non-perverse code, the DFO walk will reach all uses of the
272 ;;; result lvar before it reaches the RETURN. In perverse code, we
273 ;;; may annotate for unknown values when we didn't have to.
274 ;;; * Otherwise, we must annotate the lvar for unknown values.
275 (defun ltn-analyze-return (node)
276 (declare (type creturn node))
277 (let* ((lvar (return-result node))
278 (fun (return-lambda node))
279 (returns (tail-set-info (lambda-tail-set fun)))
280 (types (return-info-types returns)))
281 (if (eq (return-info-count returns) :unknown)
282 (collect ((res *empty-type* values-type-union))
283 (do-uses (use (return-result node))
284 (unless (and (node-tail-p use)
285 (basic-combination-p use)
286 (member (basic-combination-info use) '(:local :full)))
287 (res (node-derived-type use))))
289 (let ((int (res)))
290 (multiple-value-bind (types kind)
291 (if (eq int *empty-type*)
292 (values nil :unknown)
293 (values-types int))
294 (if (eq kind :unknown)
295 (annotate-unknown-values-lvar lvar)
296 (annotate-fixed-values-lvar
297 lvar (mapcar #'primitive-type types))))))
298 (annotate-fixed-values-lvar lvar types)))
300 (values))
302 ;;; Annotate the single argument lvar as a fixed-values lvar. We look
303 ;;; at the called lambda to determine number and type of return values
304 ;;; desired. It is assumed that only a function that
305 ;;; LOOKS-LIKE-AN-MV-BIND will be converted to a local call.
306 (defun ltn-analyze-mv-bind (call)
307 (declare (type mv-combination call))
308 (setf (basic-combination-kind call) :local)
309 (setf (node-tail-p call) nil)
310 (annotate-fixed-values-lvar
311 (first (basic-combination-args call))
312 (mapcar (lambda (var)
313 (primitive-type (basic-var-type var)))
314 (lambda-vars
315 (ref-leaf (lvar-use (basic-combination-fun call))))))
316 (values))
318 ;;; We force all the argument lvars to use the unknown values
319 ;;; convention. The lvars are annotated in reverse order, since the
320 ;;; last argument is on top, thus must be popped first. We disallow
321 ;;; delayed evaluation of the function lvar to simplify IR2 conversion
322 ;;; of MV call.
324 ;;; We could be cleverer when we know the number of values returned by
325 ;;; the lvars, but optimizations of MV call are probably unworthwhile.
327 ;;; We are also responsible for handling THROW, which is represented
328 ;;; in IR1 as an MV call to the %THROW funny function. We annotate the
329 ;;; tag lvar for a single value and the values lvar for unknown
330 ;;; values.
331 (defun ltn-analyze-mv-call (call)
332 (declare (type mv-combination call))
333 (let ((fun (basic-combination-fun call))
334 (args (basic-combination-args call)))
335 (cond ((eq (lvar-fun-name fun) '%throw)
336 (setf (basic-combination-info call) :funny)
337 (annotate-ordinary-lvar (first args))
338 (annotate-unknown-values-lvar (second args))
339 (setf (node-tail-p call) nil))
341 (setf (basic-combination-info call) :full)
342 (annotate-fun-lvar (basic-combination-fun call) nil)
343 (dolist (arg (reverse args))
344 (annotate-unknown-values-lvar arg))
345 (flush-full-call-tail-transfer call))))
347 (values))
349 ;;; Annotate the arguments as ordinary single-value lvars. And check
350 ;;; the successor.
351 (defun ltn-analyze-local-call (call)
352 (declare (type combination call))
353 (setf (basic-combination-info call) :local)
354 (dolist (arg (basic-combination-args call))
355 (when arg
356 (annotate-ordinary-lvar arg)))
357 (when (node-tail-p call)
358 (set-tail-local-call-successor call))
359 (values))
361 ;;; Make sure that a tail local call is linked directly to the bind
362 ;;; node. Usually it will be, but calls from XEPs and calls that might have
363 ;;; needed a cleanup after them won't have been swung over yet, since we
364 ;;; weren't sure they would really be TR until now.
365 (defun set-tail-local-call-successor (call)
366 (let ((caller (node-home-lambda call))
367 (callee (combination-lambda call)))
368 (aver (eq (lambda-tail-set caller)
369 (lambda-tail-set (lambda-home callee))))
370 (node-ends-block call)
371 (let* ((block (node-block call))
372 (new-block (first (block-succ block))))
373 (fixup-ir2-blocks-for-split-block block new-block)
374 (unlink-blocks block new-block)
375 (link-blocks block (lambda-block callee))))
376 (values))
378 ;;; Annotate the value lvar.
379 (defun ltn-analyze-set (node)
380 (declare (type cset node))
381 (setf (node-tail-p node) nil)
382 (annotate-ordinary-lvar (set-value node))
383 (values))
385 ;;; If the only use of the TEST lvar is a combination annotated with a
386 ;;; conditional template, then don't annotate the lvar so that IR2
387 ;;; conversion knows not to emit any code, otherwise annotate as an
388 ;;; ordinary lvar. Since we only use a conditional template if the
389 ;;; call immediately precedes the IF node in the same block, we know
390 ;;; that any predicate will already be annotated.
391 (defun ltn-analyze-if (node)
392 (declare (type cif node))
393 (setf (node-tail-p node) nil)
394 (let* ((test (if-test node))
395 (use (lvar-uses test)))
396 (unless (and (combination-p use)
397 (let ((info (basic-combination-info use)))
398 (and (template-p info)
399 (template-conditional-p info))))
400 (annotate-ordinary-lvar test)))
401 (values))
403 ;;; If there is a value lvar, then annotate it for unknown values. In
404 ;;; this case, the exit is non-local, since all other exits are
405 ;;; deleted or degenerate by this point.
406 (defun ltn-analyze-exit (node)
407 (setf (node-tail-p node) nil)
408 (let ((value (exit-value node)))
409 (when value
410 (annotate-unknown-values-lvar value)))
411 (values))
413 ;;; We need a special method for %UNWIND-PROTECT that ignores the
414 ;;; cleanup function. We don't annotate either arg, since we don't
415 ;;; need them at run-time.
417 ;;; (The default is o.k. for %CATCH, since environment analysis
418 ;;; converted the reference to the escape function into a constant
419 ;;; reference to the NLX-INFO.)
420 (defoptimizer (%unwind-protect ltn-annotate) ((escape cleanup)
421 node
422 ltn-policy)
423 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
424 (setf (basic-combination-info node) :funny)
425 (setf (node-tail-p node) nil))
427 ;;; Make sure that arguments of magic functions are not annotated.
428 ;;; (Otherwise the compiler may dump its internal structures as
429 ;;; constants :-()
430 (defoptimizer (%pop-values ltn-annotate) ((%lvar) node ltn-policy)
431 %lvar node ltn-policy)
432 (defoptimizer (%nip-values ltn-annotate) ((last-nipped last-preserved
433 &rest moved)
434 node ltn-policy)
435 last-nipped last-preserved moved node ltn-policy)
438 ;;;; known call annotation
440 ;;; Return true if RESTR is satisfied by TYPE. If T-OK is true, then a
441 ;;; T restriction allows any operand type. This is also called by IR2
442 ;;; translation when it determines whether a result temporary needs to
443 ;;; be made, and by representation selection when it is deciding which
444 ;;; move VOP to use. LVAR and TN are used to test for constant
445 ;;; arguments.
446 (defun operand-restriction-ok (restr type &key lvar tn (t-ok t))
447 (declare (type (or (member *) cons) restr)
448 (type primitive-type type)
449 (type (or lvar null) lvar)
450 (type (or tn null) tn))
451 (if (eq restr '*)
453 (ecase (first restr)
454 (:or
455 (dolist (mem (rest restr) nil)
456 (when (or (and t-ok (eq mem *backend-t-primitive-type*))
457 (eq mem type))
458 (return t))))
459 (:constant
460 (cond (lvar
461 (and (constant-lvar-p lvar)
462 (funcall (second restr) (lvar-value lvar))))
464 (and (eq (tn-kind tn) :constant)
465 (funcall (second restr) (tn-value tn))))
467 (error "Neither LVAR nor TN supplied.")))))))
469 ;;; Check that the argument type restriction for TEMPLATE are
470 ;;; satisfied in call. If an argument's TYPE-CHECK is :NO-CHECK and
471 ;;; our policy is safe, then only :SAFE templates are OK.
472 (defun template-args-ok (template call safe-p)
473 (declare (type template template)
474 (type combination call))
475 (declare (ignore safe-p))
476 (let ((mtype (template-more-args-type template)))
477 (do ((args (basic-combination-args call) (cdr args))
478 (types (template-arg-types template) (cdr types)))
479 ((null types)
480 (cond ((null args) t)
481 ((not mtype) nil)
483 (dolist (arg args t)
484 (unless (operand-restriction-ok mtype
485 (lvar-ptype arg))
486 (return nil))))))
487 (when (null args) (return nil))
488 (let ((arg (car args))
489 (type (car types)))
490 (unless (operand-restriction-ok type (lvar-ptype arg)
491 :lvar arg)
492 (return nil))))))
494 ;;; Check that TEMPLATE can be used with the specifed RESULT-TYPE.
495 ;;; Result type checking is pretty different from argument type
496 ;;; checking due to the relaxed rules for values count. We succeed if
497 ;;; for each required result, there is a positional restriction on the
498 ;;; value that is at least as good. If we run out of result types
499 ;;; before we run out of restrictions, then we only succeed if the
500 ;;; leftover restrictions are *. If we run out of restrictions before
501 ;;; we run out of result types, then we always win.
502 (defun template-results-ok (template result-type)
503 (declare (type template template)
504 (type ctype result-type))
505 (when (template-more-results-type template)
506 (error "~S has :MORE results with :TRANSLATE." (template-name template)))
507 (let ((types (template-result-types template)))
508 (cond
509 ((values-type-p result-type)
510 (do ((ltypes (append (args-type-required result-type)
511 (args-type-optional result-type))
512 (rest ltypes))
513 (types types (rest types)))
514 ((null ltypes)
515 (dolist (type types t)
516 (unless (eq type '*)
517 (return nil))))
518 (when (null types) (return t))
519 (let ((type (first types)))
520 (unless (operand-restriction-ok type
521 (primitive-type (first ltypes)))
522 (return nil)))))
523 (types
524 (operand-restriction-ok (first types) (primitive-type result-type)))
525 (t t))))
527 ;;; Return true if CALL is an ok use of TEMPLATE according to SAFE-P.
528 ;;; -- If the template has a GUARD that isn't true, then we ignore the
529 ;;; template, not even considering it to be rejected.
530 ;;; -- If the argument type restrictions aren't satisfied, then we
531 ;;; reject the template.
532 ;;; -- If the template is :CONDITIONAL, then we accept it only when the
533 ;;; destination of the value is an immediately following IF node.
534 ;;; -- If either the template is safe or the policy is unsafe (i.e. we
535 ;;; can believe output assertions), then we test against the
536 ;;; intersection of the node derived type and the lvar
537 ;;; asserted type. Otherwise, we just use the node type. If
538 ;;; TYPE-CHECK is null, there is no point in doing the intersection,
539 ;;; since the node type must be a subtype of the assertion.
541 ;;; If the template is *not* ok, then the second value is a keyword
542 ;;; indicating which aspect failed.
543 (defun is-ok-template-use (template call safe-p)
544 (declare (type template template) (type combination call))
545 (let* ((guard (template-guard template))
546 (lvar (node-lvar call))
547 (dtype (node-derived-type call)))
548 (cond ((and guard (not (funcall guard)))
549 (values nil :guard))
550 ((not (template-args-ok template call safe-p))
551 (values nil
552 (if (and safe-p (template-args-ok template call nil))
553 :arg-check
554 :arg-types)))
555 ((template-conditional-p template)
556 (let ((dest (lvar-dest lvar)))
557 (if (and (if-p dest)
558 (immediately-used-p (if-test dest) call))
559 (values t nil)
560 (values nil :conditional))))
561 ((template-results-ok template dtype)
562 (values t nil))
564 (values nil :result-types)))))
566 ;;; Use operand type information to choose a template from the list
567 ;;; TEMPLATES for a known CALL. We return three values:
568 ;;; 1. The template we found.
569 ;;; 2. Some template that we rejected due to unsatisfied type restrictions, or
570 ;;; NIL if none.
571 ;;; 3. The tail of Templates for templates we haven't examined yet.
573 ;;; We just call IS-OK-TEMPLATE-USE until it returns true.
574 (defun find-template (templates call safe-p)
575 (declare (list templates) (type combination call))
576 (do ((templates templates (rest templates))
577 (rejected nil))
578 ((null templates)
579 (values nil rejected nil))
580 (let ((template (first templates)))
581 (when (is-ok-template-use template call safe-p)
582 (return (values template rejected (rest templates))))
583 (setq rejected template))))
585 ;;; Given a partially annotated known call and a translation policy,
586 ;;; return the appropriate template, or NIL if none can be found. We
587 ;;; scan the templates (ordered by increasing cost) looking for a
588 ;;; template whose restrictions are satisfied and that has our policy.
590 ;;; If we find a template that doesn't have our policy, but has a
591 ;;; legal alternate policy, then we also record that to return as a
592 ;;; last resort. If our policy is safe, then only safe policies are
593 ;;; O.K., otherwise anything goes.
595 ;;; If we find a template with :SAFE policy, then we return it, or any
596 ;;; cheaper fallback template. The theory behind this is that if it is
597 ;;; cheapest, small and safe, we can't lose. If it is not cheapest,
598 ;;; then we use the fallback, which won't have the desired policy, but
599 ;;; :SAFE isn't desired either, so we might as well go with the
600 ;;; cheaper one. The main reason for doing this is to make sure that
601 ;;; cheap safe templates are used when they apply and the current
602 ;;; policy is something else. This is useful because :SAFE has the
603 ;;; additional semantics of implicit argument type checking, so we may
604 ;;; be forced to define a template with :SAFE policy when it is really
605 ;;; small and fast as well.
606 (defun find-template-for-ltn-policy (call ltn-policy)
607 (declare (type combination call)
608 (type ltn-policy ltn-policy))
609 (let ((safe-p (ltn-policy-safe-p ltn-policy))
610 (current (fun-info-templates (basic-combination-fun-info call)))
611 (fallback nil)
612 (rejected nil))
613 (loop
614 (multiple-value-bind (template this-reject more)
615 (find-template current call safe-p)
616 (unless rejected
617 (setq rejected this-reject))
618 (setq current more)
619 (unless template
620 (return (values fallback rejected)))
621 (let ((tcpolicy (template-ltn-policy template)))
622 (cond ((eq tcpolicy ltn-policy)
623 (return (values template rejected)))
624 ((eq tcpolicy :safe)
625 (return (values (or fallback template) rejected)))
626 ((or (not safe-p) (eq tcpolicy :fast-safe))
627 (unless fallback
628 (setq fallback template)))))))))
630 (defvar *efficiency-note-limit* 2
631 #!+sb-doc
632 "This is the maximum number of possible optimization alternatives will be
633 mentioned in a particular efficiency note. NIL means no limit.")
634 (declaim (type (or index null) *efficiency-note-limit*))
636 (defvar *efficiency-note-cost-threshold* 5
637 #!+sb-doc
638 "This is the minimum cost difference between the chosen implementation and
639 the next alternative that justifies an efficiency note.")
640 (declaim (type index *efficiency-note-cost-threshold*))
642 ;;; This function is called by NOTE-REJECTED-TEMPLATES when it can't
643 ;;; figure out any reason why TEMPLATE was rejected. Users should
644 ;;; never see these messages, but they can happen in situations where
645 ;;; the VM definition is messed up somehow.
646 (defun strange-template-failure (template call ltn-policy frob)
647 (declare (type template template) (type combination call)
648 (type ltn-policy ltn-policy) (type function frob))
649 (funcall frob "This shouldn't happen! Bug?")
650 (multiple-value-bind (win why)
651 (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
652 (aver (not win))
653 (ecase why
654 (:guard
655 (funcall frob "template guard failed"))
656 (:arg-check
657 (funcall frob "The template isn't safe, yet we were counting on it."))
658 (:arg-types
659 (funcall frob "argument types invalid")
660 (funcall frob "argument primitive types:~% ~S"
661 (mapcar (lambda (x)
662 (primitive-type-name
663 (lvar-ptype x)))
664 (combination-args call)))
665 (funcall frob "argument type assertions:~% ~S"
666 (mapcar (lambda (x)
667 (if (atom x)
669 (ecase (car x)
670 (:or `(:or .,(mapcar #'primitive-type-name
671 (cdr x))))
672 (:constant `(:constant ,(third x))))))
673 (template-arg-types template))))
674 (:conditional
675 (funcall frob "conditional in a non-conditional context"))
676 (:result-types
677 (funcall frob "result types invalid")))))
679 ;;; This function emits efficiency notes describing all of the
680 ;;; templates better (faster) than TEMPLATE that we might have been
681 ;;; able to use if there were better type declarations. Template is
682 ;;; null when we didn't find any template, and thus must do a full
683 ;;; call.
685 ;;; In order to be worth complaining about, a template must:
686 ;;; -- be allowed by its guard,
687 ;;; -- be safe if the current policy is safe,
688 ;;; -- have argument/result type restrictions consistent with the
689 ;;; known type information, e.g. we don't consider float templates
690 ;;; when an operand is known to be an integer,
691 ;;; -- be disallowed by the stricter operand subtype test (which
692 ;;; resembles, but is not identical to the test done by
693 ;;; FIND-TEMPLATE.)
695 ;;; Note that there may not be any possibly applicable templates,
696 ;;; since we are called whenever any template is rejected. That
697 ;;; template might have the wrong policy or be inconsistent with the
698 ;;; known type.
700 ;;; We go to some trouble to make the whole multi-line output into a
701 ;;; single call to COMPILER-NOTIFY so that repeat messages are
702 ;;; suppressed, etc.
703 (defun note-rejected-templates (call ltn-policy template)
704 (declare (type combination call) (type ltn-policy ltn-policy)
705 (type (or template null) template))
707 (collect ((losers))
708 (let ((safe-p (ltn-policy-safe-p ltn-policy))
709 (verbose-p (policy call (= inhibit-warnings 0)))
710 (max-cost (- (template-cost
711 (or template
712 (template-or-lose 'call-named)))
713 *efficiency-note-cost-threshold*)))
714 (dolist (try (fun-info-templates (basic-combination-fun-info call)))
715 (when (> (template-cost try) max-cost) (return)) ; FIXME: UNLESS'd be cleaner.
716 (let ((guard (template-guard try)))
717 (when (and (or (not guard) (funcall guard))
718 (or (not safe-p)
719 (ltn-policy-safe-p (template-ltn-policy try)))
720 ;; :SAFE is also considered to be :SMALL-SAFE,
721 ;; while the template cost describes time cost;
722 ;; so the fact that (< (t-cost try) (t-cost
723 ;; template)) does not mean that TRY is better
724 (not (and (eq ltn-policy :safe)
725 (eq (template-ltn-policy try) :fast-safe)))
726 (or verbose-p
727 (and (template-note try)
728 (valid-fun-use
729 call (template-type try)
730 :argument-test #'types-equal-or-intersect
731 :result-test
732 #'values-types-equal-or-intersect))))
733 (losers try)))))
735 (when (losers)
736 (collect ((messages)
737 (notes 0 +))
738 (flet ((lose1 (string &rest stuff)
739 (messages string)
740 (messages stuff)))
741 (dolist (loser (losers))
742 (when (and *efficiency-note-limit*
743 (>= (notes) *efficiency-note-limit*))
744 (lose1 "etc.")
745 (return))
746 (let* ((type (template-type loser))
747 (valid (valid-fun-use call type))
748 (strict-valid (valid-fun-use call type)))
749 (lose1 "unable to do ~A (cost ~W) because:"
750 (or (template-note loser) (template-name loser))
751 (template-cost loser))
752 (cond
753 ((and valid strict-valid)
754 (strange-template-failure loser call ltn-policy #'lose1))
755 ((not valid)
756 (aver (not (valid-fun-use call type
757 :lossage-fun #'lose1
758 :unwinnage-fun #'lose1))))
760 (aver (ltn-policy-safe-p ltn-policy))
761 (lose1 "can't trust output type assertion under safe policy")))
762 (notes 1))))
764 (let ((*compiler-error-context* call))
765 (compiler-notify "~{~?~^~&~6T~}"
766 (if template
767 `("forced to do ~A (cost ~W)"
768 (,(or (template-note template)
769 (template-name template))
770 ,(template-cost template))
771 . ,(messages))
772 `("forced to do full call"
774 . ,(messages))))))))
775 (values))
777 ;;; If a function has a special-case annotation method use that,
778 ;;; otherwise annotate the argument lvars and try to find a template
779 ;;; corresponding to the type signature. If there is none, convert a
780 ;;; full call.
781 (defun ltn-analyze-known-call (call)
782 (declare (type combination call))
783 (let ((ltn-policy (node-ltn-policy call))
784 (method (fun-info-ltn-annotate (basic-combination-fun-info call)))
785 (args (basic-combination-args call)))
786 (when method
787 (funcall method call ltn-policy)
788 (return-from ltn-analyze-known-call (values)))
790 (dolist (arg args)
791 (setf (lvar-info arg)
792 (make-ir2-lvar (primitive-type (lvar-type arg)))))
794 (multiple-value-bind (template rejected)
795 (find-template-for-ltn-policy call ltn-policy)
796 ;; If we are unable to use some templates due to unsatisfied
797 ;; operand type restrictions and our policy enables efficiency
798 ;; notes, then we call NOTE-REJECTED-TEMPLATES.
799 (when (and rejected
800 (policy call (> speed inhibit-warnings)))
801 (note-rejected-templates call ltn-policy template))
802 ;; If we are forced to do a full call, we check to see whether
803 ;; the function called is the same as the current function. If
804 ;; so, we give a warning, as this is probably a botched attempt
805 ;; to implement an out-of-line version in terms of inline
806 ;; transforms or VOPs or whatever.
807 (unless template
808 (when (let ((funleaf (physenv-lambda (node-physenv call))))
809 (and (leaf-has-source-name-p funleaf)
810 (eq (lvar-fun-name (combination-fun call))
811 (leaf-source-name funleaf))
812 (let ((info (basic-combination-fun-info call)))
813 (not (or (fun-info-ir2-convert info)
814 (ir1-attributep (fun-info-attributes info)
815 recursive))))))
816 (let ((*compiler-error-context* call))
817 (compiler-warn "~@<recursion in known function definition~2I ~
818 ~_policy=~S ~_arg types=~S~:>"
819 (lexenv-policy (node-lexenv call))
820 (mapcar (lambda (arg)
821 (type-specifier (lvar-type arg)))
822 args))))
823 (ltn-default-call call)
824 (return-from ltn-analyze-known-call (values)))
825 (setf (basic-combination-info call) template)
826 (setf (node-tail-p call) nil)
828 (dolist (arg args)
829 (annotate-1-value-lvar arg))))
831 (values))
833 ;;; CASTs are merely lvar annotations than nodes. So we wait until
834 ;;; value consumer deside how values should be passed, and after that
835 ;;; we propagate this decision backwards through CAST chain. The
836 ;;; exception is a dangling CAST with a type check, which we process
837 ;;; immediately.
838 (defun ltn-analyze-cast (cast)
839 (declare (type cast cast))
840 (setf (node-tail-p cast) nil)
841 (when (and (cast-type-check cast)
842 (not (node-lvar cast)))
843 ;; FIXME
844 (bug "IR2 type checking of unused values is not implemented.")
846 (values))
848 (defun ltn-annotate-casts (lvar)
849 (declare (type lvar lvar))
850 (do-uses (node lvar)
851 (when (cast-p node)
852 (ltn-annotate-cast node))))
854 (defun ltn-annotate-cast (cast)
855 (declare (type cast))
856 (let ((2lvar (lvar-info (node-lvar cast)))
857 (value (cast-value cast)))
858 (aver 2lvar)
859 ;; XXX
860 (ecase (ir2-lvar-kind 2lvar)
861 (:unknown
862 (annotate-unknown-values-lvar value))
863 (:fixed
864 (let* ((count (length (ir2-lvar-locs 2lvar)))
865 (ctype (lvar-derived-type value)))
866 (multiple-value-bind (types rest)
867 (values-type-types ctype (specifier-type 'null))
868 (annotate-fixed-values-lvar
869 value
870 (mapcar #'primitive-type
871 (adjust-list types count rest))))))))
872 (values))
875 ;;;; interfaces
877 ;;; most of the guts of the two interface functions: Compute the
878 ;;; policy and dispatch to the appropriate node-specific function.
880 ;;; Note: we deliberately don't use the DO-NODES macro, since the
881 ;;; block can be split out from underneath us, and DO-NODES would scan
882 ;;; past the block end in that case.
883 (defun ltn-analyze-block (block)
884 (do* ((node (block-start-node block)
885 (ctran-next ctran))
886 (ctran (node-next node) (node-next node)))
887 (nil)
888 (etypecase node
889 (ref)
890 (combination
891 (ecase (basic-combination-kind node)
892 (:local (ltn-analyze-local-call node))
893 ((:full :error) (ltn-default-call node))
894 (:known
895 (ltn-analyze-known-call node))))
896 (cif (ltn-analyze-if node))
897 (creturn (ltn-analyze-return node))
898 ((or bind entry))
899 (exit (ltn-analyze-exit node))
900 (cset (ltn-analyze-set node))
901 (cast (ltn-analyze-cast node))
902 (mv-combination
903 (ecase (basic-combination-kind node)
904 (:local
905 (ltn-analyze-mv-bind node))
906 ((:full :error)
907 (ltn-analyze-mv-call node)))))
908 (when (eq node (block-last block))
909 (return))))
911 ;;; Loop over the blocks in COMPONENT, doing stuff to nodes that
912 ;;; receive values. In addition to the stuff done by FROB, we also see
913 ;;; whether there are any unknown values receivers, making notations
914 ;;; in the components' GENERATORS and RECEIVERS as appropriate.
916 ;;; If any unknown-values lvars are received by this block (as
917 ;;; indicated by IR2-BLOCK-POPPED), then we add the block to the
918 ;;; IR2-COMPONENT-VALUES-RECEIVERS.
920 ;;; This is where we allocate IR2 blocks because it is the first place
921 ;;; we need them.
922 (defun ltn-analyze (component)
923 (declare (type component component))
924 (let ((2comp (component-info component)))
925 (do-blocks (block component)
926 ;; Set up the IR2 blocks in a separate pass, because CAST nodes
927 ;; could be out-of-order with respect to their result LVAR
928 ;; DESTs, and we need their IR1 blocks to have associated IR2
929 ;; blocks.
930 (aver (not (block-info block)))
931 (setf (block-info block) (make-ir2-block block)))
932 (do-blocks (block component)
933 (ltn-analyze-block block))
934 (do-blocks (block component)
935 (let ((2block (block-info block)))
936 (let ((popped (ir2-block-popped 2block)))
937 (when popped
938 (push block (ir2-component-values-receivers 2comp)))))))
939 (values))
941 ;;; This function is used to analyze blocks that must be added to the
942 ;;; flow graph after the normal LTN phase runs. Such code is
943 ;;; constrained not to use weird unknown values (and probably in lots
944 ;;; of other ways).
945 (defun ltn-analyze-belated-block (block)
946 (declare (type cblock block))
947 (ltn-analyze-block block)
948 (aver (not (ir2-block-popped (block-info block))))
949 (values))