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