cross-host-friendly version of uninterned symbol dumping
[sbcl.git] / src / compiler / ltn.lisp
blob7305b73027530bebe2e9b7fcde6d1051feb6c931
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 (declare (ignore escape cleanup 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 (declare (ignore %lvar node ltn-policy)))
432 (defoptimizer (%nip-values ltn-annotate) ((last-nipped last-preserved
433 &rest moved)
434 node ltn-policy)
435 (declare (ignore last-nipped last-preserved moved node ltn-policy)))
436 (defoptimizer (%dummy-dx-alloc ltn-annotate) ((target source)
437 node ltn-policy)
438 (declare (ignore target source node ltn-policy)))
441 ;;;; known call annotation
443 ;;; Return true if RESTR is satisfied by TYPE. If T-OK is true, then a
444 ;;; T restriction allows any operand type. This is also called by IR2
445 ;;; translation when it determines whether a result temporary needs to
446 ;;; be made, and by representation selection when it is deciding which
447 ;;; move VOP to use. LVAR and TN are used to test for constant
448 ;;; arguments.
449 (defun operand-restriction-ok (restr type &key lvar tn (t-ok t))
450 (declare (type (or (member *) cons) restr)
451 (type primitive-type type)
452 (type (or lvar null) lvar)
453 (type (or tn null) tn))
454 (if (eq restr '*)
456 (ecase (first restr)
457 (:or
458 (dolist (mem (rest restr) nil)
459 (when (or (and t-ok (eq mem *backend-t-primitive-type*))
460 (eq mem type))
461 (return t))))
462 (:constant
463 (cond (lvar
464 (and (constant-lvar-p lvar)
465 (funcall (second restr) (lvar-value lvar))))
467 (and (eq (tn-kind tn) :constant)
468 (funcall (second restr) (tn-value tn))))
470 (error "Neither LVAR nor TN supplied.")))))))
472 ;;; Check that the argument type restriction for TEMPLATE are
473 ;;; satisfied in call. If an argument's TYPE-CHECK is :NO-CHECK and
474 ;;; our policy is safe, then only :SAFE templates are OK.
475 (defun template-args-ok (template call safe-p)
476 (declare (type template template)
477 (type combination call))
478 (declare (ignore safe-p))
479 (let ((mtype (template-more-args-type template)))
480 (do ((args (basic-combination-args call) (cdr args))
481 (types (template-arg-types template) (cdr types)))
482 ((null types)
483 (cond ((null args) t)
484 ((not mtype) nil)
486 (dolist (arg args t)
487 (unless (operand-restriction-ok mtype
488 (lvar-ptype arg))
489 (return nil))))))
490 (when (null args) (return nil))
491 (let ((arg (car args))
492 (type (car types)))
493 (unless (operand-restriction-ok type (lvar-ptype arg)
494 :lvar arg)
495 (return nil))))))
497 ;;; Check that TEMPLATE can be used with the specifed RESULT-TYPE.
498 ;;; Result type checking is pretty different from argument type
499 ;;; checking due to the relaxed rules for values count. We succeed if
500 ;;; for each required result, there is a positional restriction on the
501 ;;; value that is at least as good. If we run out of result types
502 ;;; before we run out of restrictions, then we only succeed if the
503 ;;; leftover restrictions are *. If we run out of restrictions before
504 ;;; we run out of result types, then we always win.
505 (defun template-results-ok (template result-type)
506 (declare (type template template)
507 (type ctype result-type))
508 (when (template-more-results-type template)
509 (error "~S has :MORE results with :TRANSLATE." (template-name template)))
510 (let ((types (template-result-types template)))
511 (cond
512 ((values-type-p result-type)
513 (do ((ltypes (append (args-type-required result-type)
514 (args-type-optional result-type))
515 (rest ltypes))
516 (types types (rest types)))
517 ((null ltypes)
518 (dolist (type types t)
519 (unless (eq type '*)
520 (return nil))))
521 (when (null types) (return t))
522 (let ((type (first types)))
523 (unless (operand-restriction-ok type
524 (primitive-type (first ltypes)))
525 (return nil)))))
526 (types
527 (operand-restriction-ok (first types) (primitive-type result-type)))
528 (t t))))
530 ;;; Return true if CALL is an ok use of TEMPLATE according to SAFE-P.
531 ;;; -- If the template has a GUARD that isn't true, then we ignore the
532 ;;; template, not even considering it to be rejected.
533 ;;; -- If the argument type restrictions aren't satisfied, then we
534 ;;; reject the template.
535 ;;; -- If the template is :CONDITIONAL, then we accept it only when the
536 ;;; destination of the value is an immediately following IF node.
537 ;;; -- If either the template is safe or the policy is unsafe (i.e. we
538 ;;; can believe output assertions), then we test against the
539 ;;; intersection of the node derived type and the lvar
540 ;;; asserted type. Otherwise, we just use the node type. If
541 ;;; TYPE-CHECK is null, there is no point in doing the intersection,
542 ;;; since the node type must be a subtype of the assertion.
544 ;;; If the template is *not* ok, then the second value is a keyword
545 ;;; indicating which aspect failed.
546 (defun is-ok-template-use (template call safe-p)
547 (declare (type template template) (type combination call))
548 (let* ((guard (template-guard template))
549 (lvar (node-lvar call))
550 (dtype (node-derived-type call)))
551 (cond ((and guard (not (funcall guard)))
552 (values nil :guard))
553 ((not (template-args-ok template call safe-p))
554 (values nil
555 (if (and safe-p (template-args-ok template call nil))
556 :arg-check
557 :arg-types)))
558 ((template-conditional-p template)
559 (let ((dest (lvar-dest lvar)))
560 (if (and (if-p dest)
561 (immediately-used-p (if-test dest) call))
562 (values t nil)
563 (values nil :conditional))))
564 ((template-results-ok template dtype)
565 (values t nil))
567 (values nil :result-types)))))
569 ;;; Use operand type information to choose a template from the list
570 ;;; TEMPLATES for a known CALL. We return three values:
571 ;;; 1. The template we found.
572 ;;; 2. Some template that we rejected due to unsatisfied type restrictions, or
573 ;;; NIL if none.
574 ;;; 3. The tail of Templates for templates we haven't examined yet.
576 ;;; We just call IS-OK-TEMPLATE-USE until it returns true.
577 (defun find-template (templates call safe-p)
578 (declare (list templates) (type combination call))
579 (do ((templates templates (rest templates))
580 (rejected nil))
581 ((null templates)
582 (values nil rejected nil))
583 (let ((template (first templates)))
584 (when (is-ok-template-use template call safe-p)
585 (return (values template rejected (rest templates))))
586 (setq rejected template))))
588 ;;; Given a partially annotated known call and a translation policy,
589 ;;; return the appropriate template, or NIL if none can be found. We
590 ;;; scan the templates (ordered by increasing cost) looking for a
591 ;;; template whose restrictions are satisfied and that has our policy.
593 ;;; If we find a template that doesn't have our policy, but has a
594 ;;; legal alternate policy, then we also record that to return as a
595 ;;; last resort. If our policy is safe, then only safe policies are
596 ;;; O.K., otherwise anything goes.
598 ;;; If we find a template with :SAFE policy, then we return it, or any
599 ;;; cheaper fallback template. The theory behind this is that if it is
600 ;;; cheapest, small and safe, we can't lose. If it is not cheapest,
601 ;;; then we use the fallback, which won't have the desired policy, but
602 ;;; :SAFE isn't desired either, so we might as well go with the
603 ;;; cheaper one. The main reason for doing this is to make sure that
604 ;;; cheap safe templates are used when they apply and the current
605 ;;; policy is something else. This is useful because :SAFE has the
606 ;;; additional semantics of implicit argument type checking, so we may
607 ;;; be forced to define a template with :SAFE policy when it is really
608 ;;; small and fast as well.
609 (defun find-template-for-ltn-policy (call ltn-policy)
610 (declare (type combination call)
611 (type ltn-policy ltn-policy))
612 (let ((safe-p (ltn-policy-safe-p ltn-policy))
613 (current (fun-info-templates (basic-combination-fun-info call)))
614 (fallback nil)
615 (rejected nil))
616 (loop
617 (multiple-value-bind (template this-reject more)
618 (find-template current call safe-p)
619 (unless rejected
620 (setq rejected this-reject))
621 (setq current more)
622 (unless template
623 (return (values fallback rejected)))
624 (let ((tcpolicy (template-ltn-policy template)))
625 (cond ((eq tcpolicy ltn-policy)
626 (return (values template rejected)))
627 ((eq tcpolicy :safe)
628 (return (values (or fallback template) rejected)))
629 ((or (not safe-p) (eq tcpolicy :fast-safe))
630 (unless fallback
631 (setq fallback template)))))))))
633 (defvar *efficiency-note-limit* 2
634 #!+sb-doc
635 "This is the maximum number of possible optimization alternatives will be
636 mentioned in a particular efficiency note. NIL means no limit.")
637 (declaim (type (or index null) *efficiency-note-limit*))
639 (defvar *efficiency-note-cost-threshold* 5
640 #!+sb-doc
641 "This is the minimum cost difference between the chosen implementation and
642 the next alternative that justifies an efficiency note.")
643 (declaim (type index *efficiency-note-cost-threshold*))
645 ;;; This function is called by NOTE-REJECTED-TEMPLATES when it can't
646 ;;; figure out any reason why TEMPLATE was rejected. Users should
647 ;;; never see these messages, but they can happen in situations where
648 ;;; the VM definition is messed up somehow.
649 (defun strange-template-failure (template call ltn-policy frob)
650 (declare (type template template) (type combination call)
651 (type ltn-policy ltn-policy) (type function frob))
652 (funcall frob "This shouldn't happen! Bug?")
653 (multiple-value-bind (win why)
654 (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
655 (aver (not win))
656 (ecase why
657 (:guard
658 (funcall frob "template guard failed"))
659 (:arg-check
660 (funcall frob "The template isn't safe, yet we were counting on it."))
661 (:arg-types
662 (funcall frob "argument types invalid")
663 (funcall frob "argument primitive types:~% ~S"
664 (mapcar (lambda (x)
665 (primitive-type-name
666 (lvar-ptype x)))
667 (combination-args call)))
668 (funcall frob "argument type assertions:~% ~S"
669 (mapcar (lambda (x)
670 (if (atom x)
672 (ecase (car x)
673 (:or `(:or .,(mapcar #'primitive-type-name
674 (cdr x))))
675 (:constant `(:constant ,(third x))))))
676 (template-arg-types template))))
677 (:conditional
678 (funcall frob "conditional in a non-conditional context"))
679 (:result-types
680 (funcall frob "result types invalid")))))
682 ;;; This function emits efficiency notes describing all of the
683 ;;; templates better (faster) than TEMPLATE that we might have been
684 ;;; able to use if there were better type declarations. Template is
685 ;;; null when we didn't find any template, and thus must do a full
686 ;;; call.
688 ;;; In order to be worth complaining about, a template must:
689 ;;; -- be allowed by its guard,
690 ;;; -- be safe if the current policy is safe,
691 ;;; -- have argument/result type restrictions consistent with the
692 ;;; known type information, e.g. we don't consider float templates
693 ;;; when an operand is known to be an integer,
694 ;;; -- be disallowed by the stricter operand subtype test (which
695 ;;; resembles, but is not identical to the test done by
696 ;;; FIND-TEMPLATE.)
698 ;;; Note that there may not be any possibly applicable templates,
699 ;;; since we are called whenever any template is rejected. That
700 ;;; template might have the wrong policy or be inconsistent with the
701 ;;; known type.
703 ;;; We go to some trouble to make the whole multi-line output into a
704 ;;; single call to COMPILER-NOTIFY so that repeat messages are
705 ;;; suppressed, etc.
706 (defun note-rejected-templates (call ltn-policy template)
707 (declare (type combination call) (type ltn-policy ltn-policy)
708 (type (or template null) template))
710 (collect ((losers))
711 (let ((safe-p (ltn-policy-safe-p ltn-policy))
712 (verbose-p (policy call (= inhibit-warnings 0)))
713 (max-cost (- (template-cost
714 (or template
715 (template-or-lose 'call-named)))
716 *efficiency-note-cost-threshold*)))
717 (dolist (try (fun-info-templates (basic-combination-fun-info call)))
718 (when (> (template-cost try) max-cost) (return)) ; FIXME: UNLESS'd be cleaner.
719 (let ((guard (template-guard try)))
720 (when (and (or (not guard) (funcall guard))
721 (or (not safe-p)
722 (ltn-policy-safe-p (template-ltn-policy try)))
723 ;; :SAFE is also considered to be :SMALL-SAFE,
724 ;; while the template cost describes time cost;
725 ;; so the fact that (< (t-cost try) (t-cost
726 ;; template)) does not mean that TRY is better
727 (not (and (eq ltn-policy :safe)
728 (eq (template-ltn-policy try) :fast-safe)))
729 (or verbose-p
730 (and (template-note try)
731 (valid-fun-use
732 call (template-type try)
733 :argument-test #'types-equal-or-intersect
734 :result-test
735 #'values-types-equal-or-intersect))))
736 (losers try)))))
738 (when (losers)
739 (collect ((messages)
740 (notes 0 +))
741 (flet ((lose1 (string &rest stuff)
742 (messages string)
743 (messages stuff)))
744 (dolist (loser (losers))
745 (when (and *efficiency-note-limit*
746 (>= (notes) *efficiency-note-limit*))
747 (lose1 "etc.")
748 (return))
749 (let* ((type (template-type loser))
750 (valid (valid-fun-use call type))
751 (strict-valid (valid-fun-use call type)))
752 (lose1 "unable to do ~A (cost ~W) because:"
753 (or (template-note loser) (template-name loser))
754 (template-cost loser))
755 (cond
756 ((and valid strict-valid)
757 (strange-template-failure loser call ltn-policy #'lose1))
758 ((not valid)
759 (aver (not (valid-fun-use call type
760 :lossage-fun #'lose1
761 :unwinnage-fun #'lose1))))
763 (aver (ltn-policy-safe-p ltn-policy))
764 (lose1 "can't trust output type assertion under safe policy")))
765 (notes 1))))
767 (let ((*compiler-error-context* call))
768 (compiler-notify "~{~?~^~&~6T~}"
769 (if template
770 `("forced to do ~A (cost ~W)"
771 (,(or (template-note template)
772 (template-name template))
773 ,(template-cost template))
774 . ,(messages))
775 `("forced to do full call"
777 . ,(messages))))))))
778 (values))
780 ;;; If a function has a special-case annotation method use that,
781 ;;; otherwise annotate the argument lvars and try to find a template
782 ;;; corresponding to the type signature. If there is none, convert a
783 ;;; full call.
784 (defun ltn-analyze-known-call (call)
785 (declare (type combination call))
786 (let ((ltn-policy (node-ltn-policy call))
787 (method (fun-info-ltn-annotate (basic-combination-fun-info call)))
788 (args (basic-combination-args call)))
789 (when method
790 (funcall method call ltn-policy)
791 (return-from ltn-analyze-known-call (values)))
793 (dolist (arg args)
794 (setf (lvar-info arg)
795 (make-ir2-lvar (primitive-type (lvar-type arg)))))
797 (multiple-value-bind (template rejected)
798 (find-template-for-ltn-policy call ltn-policy)
799 ;; If we are unable to use some templates due to unsatisfied
800 ;; operand type restrictions and our policy enables efficiency
801 ;; notes, then we call NOTE-REJECTED-TEMPLATES.
802 (when (and rejected
803 (policy call (> speed inhibit-warnings)))
804 (note-rejected-templates call ltn-policy template))
805 ;; If we are forced to do a full call, we check to see whether
806 ;; the function called is the same as the current function. If
807 ;; so, we give a warning, as this is probably a botched attempt
808 ;; to implement an out-of-line version in terms of inline
809 ;; transforms or VOPs or whatever.
810 (unless template
811 (when (let ((funleaf (physenv-lambda (node-physenv call))))
812 (and (leaf-has-source-name-p funleaf)
813 (eq (lvar-fun-name (combination-fun call))
814 (leaf-source-name funleaf))
815 (let ((info (basic-combination-fun-info call)))
816 (not (or (fun-info-ir2-convert info)
817 (ir1-attributep (fun-info-attributes info)
818 recursive))))))
819 (let ((*compiler-error-context* call))
820 (compiler-warn "~@<recursion in known function definition~2I ~
821 ~_policy=~S ~_arg types=~S~:>"
822 (lexenv-policy (node-lexenv call))
823 (mapcar (lambda (arg)
824 (type-specifier (lvar-type arg)))
825 args))))
826 (ltn-default-call call)
827 (return-from ltn-analyze-known-call (values)))
828 (setf (basic-combination-info call) template)
829 (setf (node-tail-p call) nil)
831 (dolist (arg args)
832 (annotate-1-value-lvar arg))))
834 (values))
836 ;;; CASTs are merely lvar annotations than nodes. So we wait until
837 ;;; value consumer deside how values should be passed, and after that
838 ;;; we propagate this decision backwards through CAST chain. The
839 ;;; exception is a dangling CAST with a type check, which we process
840 ;;; immediately.
841 (defun ltn-analyze-cast (cast)
842 (declare (type cast cast))
843 (setf (node-tail-p cast) nil)
844 (when (and (cast-type-check cast)
845 (not (node-lvar cast)))
846 ;; FIXME
847 (bug "IR2 type checking of unused values is not implemented.")
849 (values))
851 (defun ltn-annotate-casts (lvar)
852 (declare (type lvar lvar))
853 (do-uses (node lvar)
854 (when (cast-p node)
855 (ltn-annotate-cast node))))
857 (defun ltn-annotate-cast (cast)
858 (declare (type cast))
859 (let ((2lvar (lvar-info (node-lvar cast)))
860 (value (cast-value cast)))
861 (aver 2lvar)
862 ;; XXX
863 (ecase (ir2-lvar-kind 2lvar)
864 (:unknown
865 (annotate-unknown-values-lvar value))
866 (:fixed
867 (let* ((count (length (ir2-lvar-locs 2lvar)))
868 (ctype (lvar-derived-type value)))
869 (multiple-value-bind (types rest)
870 (values-type-types ctype (specifier-type 'null))
871 (annotate-fixed-values-lvar
872 value
873 (mapcar #'primitive-type
874 (adjust-list types count rest))))))))
875 (values))
878 ;;;; interfaces
880 ;;; most of the guts of the two interface functions: Compute the
881 ;;; policy and dispatch to the appropriate node-specific function.
883 ;;; Note: we deliberately don't use the DO-NODES macro, since the
884 ;;; block can be split out from underneath us, and DO-NODES would scan
885 ;;; past the block end in that case.
886 (defun ltn-analyze-block (block)
887 (do* ((node (block-start-node block)
888 (ctran-next ctran))
889 (ctran (node-next node) (node-next node)))
890 (nil)
891 (etypecase node
892 (ref)
893 (combination
894 (ecase (basic-combination-kind node)
895 (:local (ltn-analyze-local-call node))
896 ((:full :error) (ltn-default-call node))
897 (:known
898 (ltn-analyze-known-call node))))
899 (cif (ltn-analyze-if node))
900 (creturn (ltn-analyze-return node))
901 ((or bind entry))
902 (exit (ltn-analyze-exit node))
903 (cset (ltn-analyze-set node))
904 (cast (ltn-analyze-cast node))
905 (mv-combination
906 (ecase (basic-combination-kind node)
907 (:local
908 (ltn-analyze-mv-bind node))
909 ((:full :error)
910 (ltn-analyze-mv-call node)))))
911 (when (eq node (block-last block))
912 (return))))
914 ;;; Loop over the blocks in COMPONENT, doing stuff to nodes that
915 ;;; receive values. In addition to the stuff done by FROB, we also see
916 ;;; whether there are any unknown values receivers, making notations
917 ;;; in the components' GENERATORS and RECEIVERS as appropriate.
919 ;;; If any unknown-values lvars are received by this block (as
920 ;;; indicated by IR2-BLOCK-POPPED), then we add the block to the
921 ;;; IR2-COMPONENT-VALUES-RECEIVERS.
923 ;;; This is where we allocate IR2 blocks because it is the first place
924 ;;; we need them.
925 (defun ltn-analyze (component)
926 (declare (type component component))
927 (let ((2comp (component-info component)))
928 (do-blocks (block component)
929 ;; Set up the IR2 blocks in a separate pass, because CAST nodes
930 ;; could be out-of-order with respect to their result LVAR
931 ;; DESTs, and we need their IR1 blocks to have associated IR2
932 ;; blocks.
933 (aver (not (block-info block)))
934 (setf (block-info block) (make-ir2-block block)))
935 (do-blocks (block component)
936 (ltn-analyze-block block))
937 (do-blocks (block component)
938 (let ((2block (block-info block)))
939 (let ((popped (ir2-block-popped 2block)))
940 (when popped
941 (push block (ir2-component-values-receivers 2comp)))))))
942 (values))
944 ;;; This function is used to analyze blocks that must be added to the
945 ;;; flow graph after the normal LTN phase runs. Such code is
946 ;;; constrained not to use weird unknown values (and probably in lots
947 ;;; of other ways).
948 (defun ltn-analyze-belated-block (block)
949 (declare (type cblock block))
950 (ltn-analyze-block block)
951 (aver (not (ir2-block-popped (block-info block))))
952 (values))