Remove "-HEADER-" from SYMBOL and VALUE-CELL widetag names
[sbcl.git] / src / compiler / ltn.lisp
blobf2c6fad2baa5c4b1e46d49bf7d15f390a8a4e7e8
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 (let ((args (basic-combination-args call)))
311 (if (singleton-p args)
312 (annotate-fixed-values-lvar
313 (first args)
314 (mapcar (lambda (var)
315 (primitive-type (basic-var-type var)))
316 (lambda-vars
317 (ref-leaf (lvar-use (basic-combination-fun call))))))
318 (let ((types (mapcar (lambda (var)
319 (primitive-type (basic-var-type var)))
320 (lambda-vars
321 (ref-leaf (lvar-use (basic-combination-fun call)))))))
322 (dolist (arg args)
323 (annotate-fixed-values-lvar
325 (loop repeat (nth-value 1 (values-types
326 (lvar-derived-type arg)))
327 collect (if types
328 (pop types)
329 *wild-type*)))))))
330 (values))
332 ;;; We force all the argument lvars to use the unknown values
333 ;;; convention. The lvars are annotated in reverse order, since the
334 ;;; last argument is on top, thus must be popped first. We disallow
335 ;;; delayed evaluation of the function lvar to simplify IR2 conversion
336 ;;; of MV call.
338 ;;; We could be cleverer when we know the number of values returned by
339 ;;; the lvars, but optimizations of MV call are probably unworthwhile.
341 ;;; We are also responsible for handling THROW, which is represented
342 ;;; in IR1 as an MV call to the %THROW funny function. We annotate the
343 ;;; tag lvar for a single value and the values lvar for unknown
344 ;;; values.
345 (defun ltn-analyze-mv-call (call)
346 (declare (type mv-combination call))
347 (let ((fun (basic-combination-fun call))
348 (args (basic-combination-args call)))
349 (cond ((eq (lvar-fun-name fun) '%throw)
350 (setf (basic-combination-info call) :funny)
351 (annotate-ordinary-lvar (first args))
352 (annotate-unknown-values-lvar (second args))
353 (setf (node-tail-p call) nil))
355 (setf (basic-combination-info call) :full)
356 (annotate-fun-lvar (basic-combination-fun call) nil)
357 (dolist (arg (reverse args))
358 (annotate-unknown-values-lvar arg))
359 (flush-full-call-tail-transfer call))))
361 (values))
363 ;;; Annotate the arguments as ordinary single-value lvars. And check
364 ;;; the successor.
365 (defun ltn-analyze-local-call (call)
366 (declare (type combination call))
367 (setf (basic-combination-info call) :local)
368 (dolist (arg (basic-combination-args call))
369 (when arg
370 (annotate-ordinary-lvar arg)))
371 (when (node-tail-p call)
372 (set-tail-local-call-successor call))
373 (values))
375 ;;; Make sure that a tail local call is linked directly to the bind
376 ;;; node. Usually it will be, but calls from XEPs and calls that might have
377 ;;; needed a cleanup after them won't have been swung over yet, since we
378 ;;; weren't sure they would really be TR until now.
379 (defun set-tail-local-call-successor (call)
380 (let ((caller (node-home-lambda call))
381 (callee (combination-lambda call)))
382 (aver (eq (lambda-tail-set caller)
383 (lambda-tail-set (lambda-home callee))))
384 (node-ends-block call)
385 (let* ((block (node-block call))
386 (new-block (first (block-succ block))))
387 (fixup-ir2-blocks-for-split-block block new-block)
388 (unlink-blocks block new-block)
389 (link-blocks block (lambda-block callee))))
390 (values))
392 ;;; Annotate the value lvar.
393 (defun ltn-analyze-set (node)
394 (declare (type cset node))
395 (setf (node-tail-p node) nil)
396 (annotate-ordinary-lvar (set-value node))
397 (values))
399 ;;; If the only use of the TEST lvar is a combination annotated with a
400 ;;; conditional template, then don't annotate the lvar so that IR2
401 ;;; conversion knows not to emit any code, otherwise annotate as an
402 ;;; ordinary lvar. Since we only use a conditional template if the
403 ;;; call immediately precedes the IF node in the same block, we know
404 ;;; that any predicate will already be annotated.
405 (defun ltn-analyze-if (node)
406 (declare (type cif node))
407 (setf (node-tail-p node) nil)
408 (let* ((test (if-test node))
409 (use (lvar-uses test)))
410 (unless (and (combination-p use)
411 (let ((info (basic-combination-info use)))
412 (and (template-p info)
413 (template-conditional-p info))))
414 (annotate-ordinary-lvar test)))
415 (values))
417 ;;; If there is a value lvar, then annotate it for unknown values. In
418 ;;; this case, the exit is non-local, since all other exits are
419 ;;; deleted or degenerate by this point.
420 (defun ltn-analyze-exit (node)
421 (setf (node-tail-p node) nil)
422 (let ((value (exit-value node)))
423 (when value
424 (annotate-unknown-values-lvar value)))
425 (values))
427 ;;; We need a special method for %UNWIND-PROTECT that ignores the
428 ;;; cleanup function. We don't annotate either arg, since we don't
429 ;;; need them at run-time.
431 ;;; (The default is o.k. for %CATCH, since environment analysis
432 ;;; converted the reference to the escape function into a constant
433 ;;; reference to the NLX-INFO.)
434 (defoptimizer (%unwind-protect ltn-annotate) ((escape cleanup)
435 node
436 ltn-policy)
437 (declare (ignore escape cleanup ltn-policy))
438 (setf (basic-combination-info node) :funny)
439 (setf (node-tail-p node) nil))
441 ;;; Make sure that arguments of magic functions are not annotated.
442 ;;; (Otherwise the compiler may dump its internal structures as
443 ;;; constants :-()
444 (defoptimizer (%pop-values ltn-annotate) ((%lvar) node ltn-policy)
445 (declare (ignore %lvar node ltn-policy)))
446 (defoptimizer (%nip-values ltn-annotate) ((last-nipped last-preserved
447 &rest moved)
448 node ltn-policy)
449 (declare (ignore last-nipped last-preserved moved node ltn-policy)))
450 (defoptimizer (%dummy-dx-alloc ltn-annotate) ((target source)
451 node ltn-policy)
452 (declare (ignore target source node ltn-policy)))
455 ;;;; known call annotation
457 ;;; Return true if RESTR is satisfied by TYPE. If T-OK is true, then a
458 ;;; T restriction allows any operand type. This is also called by IR2
459 ;;; translation when it determines whether a result temporary needs to
460 ;;; be made, and by representation selection when it is deciding which
461 ;;; move VOP to use. LVAR and TN are used to test for constant
462 ;;; arguments.
463 (defun operand-restriction-ok (restr type &key lvar tn (t-ok t))
464 (declare (type (or (member *) cons) restr)
465 (type primitive-type type)
466 (type (or lvar null) lvar)
467 (type (or tn null) tn))
468 (if (eq restr '*)
470 (ecase (first restr)
471 (:or
472 (dolist (mem (rest restr) nil)
473 (when (or (and t-ok (eq mem *backend-t-primitive-type*))
474 (eq mem type))
475 (return t))))
476 (:constant
477 (cond (lvar
478 ;; Can't use constant-lvar-p, because it returns T for
479 ;; things for which the derived type is an EQL type,
480 ;; but there may be already a variable allocated for
481 ;; it, which can cause problems when there's a closure
482 ;; over it.
483 ;; See :vop-on-eql-type test in compiler.pure for an example.
485 ;; And the value is already loaded into a register,
486 ;; which is usually cheaper/more compactly encoded
487 ;; than a constant.
488 (and (strictly-constant-lvar-p lvar)
489 (funcall (second restr) (lvar-value lvar))))
491 (and (eq (tn-kind tn) :constant)
492 (funcall (second restr) (tn-value tn))))
494 (error "Neither LVAR nor TN supplied.")))))))
496 ;;; Check that the argument type restriction for TEMPLATE are
497 ;;; satisfied in call. If an argument's TYPE-CHECK is :NO-CHECK and
498 ;;; our policy is safe, then only :SAFE templates are OK.
499 (defun template-args-ok (template call safe-p)
500 (declare (type template template)
501 (type combination call))
502 (declare (ignore safe-p))
503 (let ((mtype (template-more-args-type template)))
504 (do ((args (basic-combination-args call) (cdr args))
505 (types (template-arg-types template) (cdr types)))
506 ((null types)
507 (cond ((null args) t)
508 ((not mtype) nil)
510 (dolist (arg args t)
511 (unless (operand-restriction-ok mtype
512 (lvar-ptype arg))
513 (return nil))))))
514 (when (null args) (return nil))
515 (let ((arg (car args))
516 (type (car types)))
517 (unless (operand-restriction-ok type (lvar-ptype arg)
518 :lvar arg)
519 (return nil))))))
521 ;;; Check that TEMPLATE can be used with the specifed RESULT-TYPE.
522 ;;; Result type checking is pretty different from argument type
523 ;;; checking due to the relaxed rules for values count. We succeed if
524 ;;; for each required result, there is a positional restriction on the
525 ;;; value that is at least as good. If we run out of result types
526 ;;; before we run out of restrictions, then we only succeed if the
527 ;;; leftover restrictions are *. If we run out of restrictions before
528 ;;; we run out of result types, then we always win.
529 (defun template-results-ok (template result-type)
530 (declare (type template template)
531 (type ctype result-type))
532 (when (template-more-results-type template)
533 (error "~S has :MORE results with :TRANSLATE." (template-name template)))
534 (let ((types (template-result-types template)))
535 (cond
536 ((values-type-p result-type)
537 (do ((ltypes (append (args-type-required result-type)
538 (args-type-optional result-type))
539 (rest ltypes))
540 (types types (rest types)))
541 ((null ltypes)
542 (dolist (type types t)
543 (unless (eq type '*)
544 (return nil))))
545 (when (null types) (return t))
546 (let ((type (first types)))
547 (unless (operand-restriction-ok type
548 (primitive-type (first ltypes)))
549 (return nil)))))
550 (types
551 (operand-restriction-ok (first types) (primitive-type result-type)))
552 (t t))))
554 ;;; Return true if CALL is an ok use of TEMPLATE according to SAFE-P.
555 ;;; -- If the template has a GUARD that isn't true, then we ignore the
556 ;;; template, not even considering it to be rejected.
557 ;;; -- If the argument type restrictions aren't satisfied, then we
558 ;;; reject the template.
559 ;;; -- If the template is :CONDITIONAL, then we accept it only when the
560 ;;; destination of the value is an immediately following IF node.
561 ;;; -- If either the template is safe or the policy is unsafe (i.e. we
562 ;;; can believe output assertions), then we test against the
563 ;;; intersection of the node derived type and the lvar
564 ;;; asserted type. Otherwise, we just use the node type. If
565 ;;; TYPE-CHECK is null, there is no point in doing the intersection,
566 ;;; since the node type must be a subtype of the assertion.
568 ;;; If the template is *not* ok, then the second value is a keyword
569 ;;; indicating which aspect failed.
570 (defun is-ok-template-use (template call safe-p)
571 (declare (type template template) (type combination call))
572 (let* ((guard (template-guard template))
573 (lvar (node-lvar call))
574 (dtype (node-derived-type call)))
575 (cond ((and guard (not (funcall guard)))
576 (values nil :guard))
577 ((not (template-args-ok template call safe-p))
578 (values nil
579 (if (and safe-p (template-args-ok template call nil))
580 :arg-check
581 :arg-types)))
582 ((template-conditional-p template)
583 (let ((dest (lvar-dest lvar)))
584 (if (and (if-p dest)
585 (immediately-used-p (if-test dest) call))
586 (values t nil)
587 (values nil :conditional))))
588 ((template-results-ok template dtype)
589 (values t nil))
591 (values nil :result-types)))))
593 ;;; Use operand type information to choose a template from the list
594 ;;; TEMPLATES for a known CALL. We return three values:
595 ;;; 1. The template we found.
596 ;;; 2. Some template that we rejected due to unsatisfied type restrictions, or
597 ;;; NIL if none.
598 ;;; 3. The tail of Templates for templates we haven't examined yet.
600 ;;; We just call IS-OK-TEMPLATE-USE until it returns true.
601 (defun find-template (templates call safe-p)
602 (declare (list templates) (type combination call))
603 (do ((templates templates (rest templates))
604 (rejected nil))
605 ((null templates)
606 (values nil rejected nil))
607 (let ((template (first templates)))
608 (when (is-ok-template-use template call safe-p)
609 (return (values template rejected (rest templates))))
610 (setq rejected template))))
612 ;;; Given a partially annotated known call and a translation policy,
613 ;;; return the appropriate template, or NIL if none can be found. We
614 ;;; scan the templates (ordered by increasing cost) looking for a
615 ;;; template whose restrictions are satisfied and that has our policy.
617 ;;; If we find a template that doesn't have our policy, but has a
618 ;;; legal alternate policy, then we also record that to return as a
619 ;;; last resort. If our policy is safe, then only safe policies are
620 ;;; O.K., otherwise anything goes.
622 ;;; If we find a template with :SAFE policy, then we return it, or any
623 ;;; cheaper fallback template. The theory behind this is that if it is
624 ;;; cheapest, small and safe, we can't lose. If it is not cheapest,
625 ;;; then we use the fallback, which won't have the desired policy, but
626 ;;; :SAFE isn't desired either, so we might as well go with the
627 ;;; cheaper one. The main reason for doing this is to make sure that
628 ;;; cheap safe templates are used when they apply and the current
629 ;;; policy is something else. This is useful because :SAFE has the
630 ;;; additional semantics of implicit argument type checking, so we may
631 ;;; be forced to define a template with :SAFE policy when it is really
632 ;;; small and fast as well.
633 (defun find-template-for-ltn-policy (call ltn-policy)
634 (declare (type combination call)
635 (type ltn-policy ltn-policy))
636 (let ((safe-p (ltn-policy-safe-p ltn-policy))
637 (current (fun-info-templates (basic-combination-fun-info call)))
638 (fallback nil)
639 (rejected nil))
640 (loop
641 (multiple-value-bind (template this-reject more)
642 (find-template current call safe-p)
643 (unless rejected
644 (setq rejected this-reject))
645 (setq current more)
646 (unless template
647 (return (values fallback rejected)))
648 (let ((tcpolicy (template-ltn-policy template)))
649 (cond ((eq tcpolicy ltn-policy)
650 (return (values template rejected)))
651 ((eq tcpolicy :safe)
652 (return (values (or fallback template) rejected)))
653 ((or (not safe-p) (eq tcpolicy :fast-safe))
654 (unless fallback
655 (setq fallback template)))))))))
657 (defvar *efficiency-note-limit* 2
658 "This is the maximum number of possible optimization alternatives will be
659 mentioned in a particular efficiency note. NIL means no limit.")
660 (declaim (type (or index null) *efficiency-note-limit*))
662 (defvar *efficiency-note-cost-threshold* 5
663 "This is the minimum cost difference between the chosen implementation and
664 the next alternative that justifies an efficiency note.")
665 (declaim (type index *efficiency-note-cost-threshold*))
667 ;;; This function is called by NOTE-REJECTED-TEMPLATES when it can't
668 ;;; figure out any reason why TEMPLATE was rejected. Users should
669 ;;; never see these messages, but they can happen in situations where
670 ;;; the VM definition is messed up somehow.
671 (defun strange-template-failure (template call ltn-policy frob)
672 (declare (type template template) (type combination call)
673 (type ltn-policy ltn-policy) (type function frob))
674 (funcall frob "This shouldn't happen! Bug?")
675 (multiple-value-bind (win why)
676 (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
677 (aver (not win))
678 (ecase why
679 (:guard
680 (funcall frob "template guard failed"))
681 (:arg-check
682 (funcall frob "The template isn't safe, yet we were counting on it."))
683 (:arg-types
684 (funcall frob "argument types invalid")
685 (funcall frob "argument primitive types:~% ~S"
686 (mapcar (lambda (x)
687 (primitive-type-name
688 (lvar-ptype x)))
689 (combination-args call)))
690 (funcall frob "argument type assertions:~% ~S"
691 (mapcar (lambda (x)
692 (if (atom x)
694 (ecase (car x)
695 (:or `(:or .,(mapcar #'primitive-type-name
696 (cdr x))))
697 (:constant `(:constant ,(third x))))))
698 (template-arg-types template))))
699 (:conditional
700 (funcall frob "conditional in a non-conditional context"))
701 (:result-types
702 (funcall frob "result types invalid")))))
704 ;;; This function emits efficiency notes describing all of the
705 ;;; templates better (faster) than TEMPLATE that we might have been
706 ;;; able to use if there were better type declarations. Template is
707 ;;; null when we didn't find any template, and thus must do a full
708 ;;; call.
710 ;;; In order to be worth complaining about, a template must:
711 ;;; -- be allowed by its guard,
712 ;;; -- be safe if the current policy is safe,
713 ;;; -- have argument/result type restrictions consistent with the
714 ;;; known type information, e.g. we don't consider float templates
715 ;;; when an operand is known to be an integer,
716 ;;; -- be disallowed by the stricter operand subtype test (which
717 ;;; resembles, but is not identical to the test done by
718 ;;; FIND-TEMPLATE.)
720 ;;; Note that there may not be any possibly applicable templates,
721 ;;; since we are called whenever any template is rejected. That
722 ;;; template might have the wrong policy or be inconsistent with the
723 ;;; known type.
725 ;;; We go to some trouble to make the whole multi-line output into a
726 ;;; single call to COMPILER-NOTIFY so that repeat messages are
727 ;;; suppressed, etc.
728 (defun note-rejected-templates (call ltn-policy template)
729 (declare (type combination call) (type ltn-policy ltn-policy)
730 (type (or template null) template))
732 (collect ((losers))
733 (let ((safe-p (ltn-policy-safe-p ltn-policy))
734 (verbose-p (policy call (= inhibit-warnings 0)))
735 (max-cost (- (template-cost
736 (or template
737 (template-or-lose 'call-named)))
738 *efficiency-note-cost-threshold*)))
739 (dolist (try (fun-info-templates (basic-combination-fun-info call)))
740 (when (> (template-cost try) max-cost) (return)) ; FIXME: UNLESS'd be cleaner.
741 (let ((guard (template-guard try)))
742 (when (and (or (not guard) (funcall guard))
743 (or (not safe-p)
744 (ltn-policy-safe-p (template-ltn-policy try)))
745 ;; :SAFE is also considered to be :SMALL-SAFE,
746 ;; while the template cost describes time cost;
747 ;; so the fact that (< (t-cost try) (t-cost
748 ;; template)) does not mean that TRY is better
749 (not (and (eq ltn-policy :safe)
750 (eq (template-ltn-policy try) :fast-safe)))
751 (or verbose-p
752 (and (template-note try)
753 (valid-fun-use
754 call (template-type try)
755 :argument-test #'types-equal-or-intersect
756 :result-test
757 #'values-types-equal-or-intersect))))
758 (losers try)))))
760 (when (losers)
761 (collect ((messages)
762 (notes 0 +))
763 (flet ((lose1 (string &rest stuff)
764 (messages string)
765 (messages stuff)))
766 (dolist (loser (losers))
767 (when (and *efficiency-note-limit*
768 (>= (notes) *efficiency-note-limit*))
769 (lose1 "etc.")
770 (return))
771 (let* ((type (template-type loser))
772 (valid (valid-fun-use call type))
773 (strict-valid (valid-fun-use call type)))
774 (lose1 "unable to do ~A (cost ~W) because:"
775 (or (template-note loser) (template-name loser))
776 (template-cost loser))
777 (cond
778 ((and valid strict-valid)
779 (strange-template-failure loser call ltn-policy #'lose1))
780 ((not valid)
781 (aver (not (valid-fun-use call type
782 :lossage-fun #'lose1
783 :unwinnage-fun #'lose1))))
785 (aver (ltn-policy-safe-p ltn-policy))
786 (lose1 "can't trust output type assertion under safe policy")))
787 (notes 1))))
789 (let ((*compiler-error-context* call))
790 (compiler-notify "~{~?~^~&~6T~}"
791 (if template
792 `("forced to do ~A (cost ~W)"
793 (,(or (template-note template)
794 (template-name template))
795 ,(template-cost template))
796 . ,(messages))
797 `("forced to do full call"
799 . ,(messages))))))))
800 (values))
802 ;;; If a function has a special-case annotation method use that,
803 ;;; otherwise annotate the argument lvars and try to find a template
804 ;;; corresponding to the type signature. If there is none, convert a
805 ;;; full call.
806 (defun ltn-analyze-known-call (call)
807 (declare (type combination call))
808 (let ((ltn-policy (node-ltn-policy call))
809 (method (fun-info-ltn-annotate (basic-combination-fun-info call)))
810 (args (basic-combination-args call)))
811 (when method
812 (funcall method call ltn-policy)
813 (return-from ltn-analyze-known-call (values)))
815 (dolist (arg args)
816 (setf (lvar-info arg)
817 (make-ir2-lvar (primitive-type (lvar-type arg)))))
819 (multiple-value-bind (template rejected)
820 (find-template-for-ltn-policy call ltn-policy)
821 ;; If we are unable to use some templates due to unsatisfied
822 ;; operand type restrictions and our policy enables efficiency
823 ;; notes, then we call NOTE-REJECTED-TEMPLATES.
824 (when (and rejected
825 (policy call (> speed inhibit-warnings)))
826 (note-rejected-templates call ltn-policy template))
827 ;; If we are forced to do a full call, we check to see whether
828 ;; the function called is the same as the current function. If
829 ;; so, we give a warning, as this is probably a botched attempt
830 ;; to implement an out-of-line version in terms of inline
831 ;; transforms or VOPs or whatever.
832 (unless template
833 (when (let ((funleaf (physenv-lambda (node-physenv call))))
834 (and (leaf-has-source-name-p funleaf)
835 (eq (lvar-fun-name (combination-fun call))
836 (leaf-source-name funleaf))
837 (let ((info (basic-combination-fun-info call)))
838 (not (or (fun-info-ir2-convert info)
839 (ir1-attributep (fun-info-attributes info)
840 recursive))))))
841 (let ((*compiler-error-context* call))
842 (compiler-warn "~@<recursion in known function definition~2I ~
843 ~_policy=~S ~_arg types=~S~:>"
844 (lexenv-policy (node-lexenv call))
845 (mapcar (lambda (arg)
846 (type-specifier (lvar-type arg)))
847 args))))
848 (ltn-default-call call)
849 (return-from ltn-analyze-known-call (values)))
850 (setf (basic-combination-info call) template)
851 (setf (node-tail-p call) nil)
853 (dolist (arg args)
854 (annotate-1-value-lvar arg))))
856 (values))
858 ;;; CASTs are merely lvar annotations than nodes. So we wait until
859 ;;; value consumer deside how values should be passed, and after that
860 ;;; we propagate this decision backwards through CAST chain. The
861 ;;; exception is a dangling CAST with a type check, which we process
862 ;;; immediately.
863 (defun ltn-analyze-cast (cast)
864 (declare (type cast cast))
865 (setf (node-tail-p cast) nil)
866 (when (and (cast-type-check cast)
867 (not (node-lvar cast)))
868 ;; FIXME
869 (bug "IR2 type checking of unused values is not implemented.")
871 (values))
873 (defun ltn-annotate-casts (lvar)
874 (declare (type lvar lvar))
875 (do-uses (node lvar)
876 (when (cast-p node)
877 (ltn-annotate-cast node))))
879 (defun ltn-annotate-cast (cast)
880 (declare (type cast))
881 (let ((2lvar (lvar-info (node-lvar cast)))
882 (value (cast-value cast)))
883 (aver 2lvar)
884 ;; XXX
885 (ecase (ir2-lvar-kind 2lvar)
886 (:unknown
887 (annotate-unknown-values-lvar value))
888 (:fixed
889 (let* ((count (length (ir2-lvar-locs 2lvar)))
890 (ctype (lvar-derived-type value)))
891 (multiple-value-bind (types rest)
892 (values-type-types ctype (specifier-type 'null))
893 (annotate-fixed-values-lvar
894 value
895 (mapcar #'primitive-type
896 (adjust-list types count rest))))))))
897 (values))
900 ;;;; interfaces
902 ;;; most of the guts of the two interface functions: Compute the
903 ;;; policy and dispatch to the appropriate node-specific function.
905 ;;; Note: we deliberately don't use the DO-NODES macro, since the
906 ;;; block can be split out from underneath us, and DO-NODES would scan
907 ;;; past the block end in that case.
908 (defun ltn-analyze-block (block)
909 (do* ((node (block-start-node block)
910 (ctran-next ctran))
911 (ctran (node-next node) (node-next node)))
912 (nil)
913 (etypecase node
914 (ref)
915 (combination
916 (ecase (basic-combination-kind node)
917 (:local (ltn-analyze-local-call node))
918 ((:full :error) (ltn-default-call node))
919 (:known
920 (ltn-analyze-known-call node))))
921 (cif (ltn-analyze-if node))
922 (creturn (ltn-analyze-return node))
923 ((or bind entry))
924 (exit (ltn-analyze-exit node))
925 (cset (ltn-analyze-set node))
926 (cast (ltn-analyze-cast node))
927 (mv-combination
928 (ecase (basic-combination-kind node)
929 (:local
930 (ltn-analyze-mv-bind node))
931 ((:full :error)
932 (ltn-analyze-mv-call node)))))
933 (when (eq node (block-last block))
934 (return))))
936 ;;; Loop over the blocks in COMPONENT, doing stuff to nodes that
937 ;;; receive values. In addition to the stuff done by FROB, we also see
938 ;;; whether there are any unknown values receivers, making notations
939 ;;; in the components' GENERATORS and RECEIVERS as appropriate.
941 ;;; If any unknown-values lvars are received by this block (as
942 ;;; indicated by IR2-BLOCK-POPPED), then we add the block to the
943 ;;; IR2-COMPONENT-VALUES-RECEIVERS.
945 ;;; This is where we allocate IR2 blocks because it is the first place
946 ;;; we need them.
947 (defun ltn-analyze (component)
948 (declare (type component component))
949 (let ((2comp (component-info component)))
950 (do-blocks (block component)
951 ;; Set up the IR2 blocks in a separate pass, because CAST nodes
952 ;; could be out-of-order with respect to their result LVAR
953 ;; DESTs, and we need their IR1 blocks to have associated IR2
954 ;; blocks.
955 (aver (not (block-info block)))
956 (setf (block-info block) (make-ir2-block block)))
957 (do-blocks (block component)
958 (ltn-analyze-block block))
959 (do-blocks (block component)
960 (let ((2block (block-info block)))
961 (let ((popped (ir2-block-popped 2block)))
962 (when popped
963 (push block (ir2-component-values-receivers 2comp)))))))
964 (values))
966 ;;; This function is used to analyze blocks that must be added to the
967 ;;; flow graph after the normal LTN phase runs. Such code is
968 ;;; constrained not to use weird unknown values (and probably in lots
969 ;;; of other ways).
970 (defun ltn-analyze-belated-block (block)
971 (declare (type cblock block))
972 (ltn-analyze-block block)
973 (aver (not (ir2-block-popped (block-info block))))
974 (values))