/[cmucl]/src/compiler/locall.lisp
ViewVC logotype

Contents of /src/compiler/locall.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (show annotations)
Fri Sep 10 19:09:16 1993 UTC (20 years, 7 months ago) by wlott
Branch: MAIN
Changes since 1.41: +6 -2 lines
Added support for &more args
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/locall.lisp,v 1.42 1993/09/10 19:09:16 wlott Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; This file implements local call analysis. A local call is a function
15 ;;; call between functions being compiled at the same time. If we can tell at
16 ;;; compile time that such a call is legal, then we change the combination
17 ;;; to call the correct lambda, mark it as local, and add this link to our call
18 ;;; graph. Once a call is local, it is then eligible for let conversion, which
19 ;;; places the body of the function inline.
20 ;;;
21 ;;; We cannot always do a local call even when we do have the function being
22 ;;; called. Calls that cannot be shown to have legal arg counts are not
23 ;;; converted.
24 ;;;
25 ;;; Written by Rob MacLachlan
26 ;;;
27 (in-package :c)
28
29
30 ;;; Propagate-To-Args -- Interface
31 ;;;
32 ;;; This function propagates information from the variables in the function
33 ;;; Fun to the actual arguments in Call. This is also called by the VALUES IR1
34 ;;; optimizer when it sleazily converts MV-BINDs to LETs.
35 ;;;
36 ;;; We flush all arguments to Call that correspond to unreferenced variables
37 ;;; in Fun. We leave NILs in the Combination-Args so that the remaining args
38 ;;; still match up with their vars.
39 ;;;
40 ;;; We also apply the declared variable type assertion to the argument
41 ;;; continuations.
42 ;;;
43 (defun propagate-to-args (call fun)
44 (declare (type combination call) (type clambda fun))
45 (do ((args (basic-combination-args call) (cdr args))
46 (vars (lambda-vars fun) (cdr vars)))
47 ((null args))
48 (let ((arg (car args))
49 (var (car vars)))
50 (cond ((leaf-refs var)
51 (assert-continuation-type arg (leaf-type var)))
52 (t
53 (flush-dest arg)
54 (setf (car args) nil)))))
55
56 (undefined-value))
57
58
59 ;;; Merge-Tail-Sets -- Interface
60 ;;;
61 ;;; This function handles merging the tail sets if Call is potentially
62 ;;; tail-recursive, and is a call to a function with a different TAIL-SET than
63 ;;; Call's Fun. This must be called whenever we alter IR1 so as to place a
64 ;;; local call in what might be a TR context. Note that any call which returns
65 ;;; its value to a RETURN is considered potentially TR, since any implicit
66 ;;; MV-PROG1 might be optimized away.
67 ;;;
68 ;;; We destructively modify the set for the calling function to represent both,
69 ;;; and then change all the functions in callee's set to reference the first.
70 ;;; If we do merge, we reoptimize the RETURN-RESULT continuation to cause
71 ;;; IR1-OPTIMIZE-RETURN to recompute the tail set type.
72 ;;;
73 (defun merge-tail-sets (call &optional (new-fun (combination-lambda call)))
74 (declare (type basic-combination call) (type clambda new-fun))
75 (let ((return (continuation-dest (node-cont call))))
76 (when (return-p return)
77 (let ((call-set (lambda-tail-set (node-home-lambda call)))
78 (fun-set (lambda-tail-set new-fun)))
79 (unless (eq call-set fun-set)
80 (let ((funs (tail-set-functions fun-set)))
81 (dolist (fun funs)
82 (setf (lambda-tail-set fun) call-set))
83 (setf (tail-set-functions call-set)
84 (nconc (tail-set-functions call-set) funs)))
85 (reoptimize-continuation (return-result return))
86 t)))))
87
88
89 ;;; Convert-Call -- Internal
90 ;;;
91 ;;; Convert a combination into a local call. We PROPAGATE-TO-ARGS, set the
92 ;;; combination kind to :Local, add Fun to the Calls of the function that the
93 ;;; call is in, call MERGE-TAIL-SETS, then replace the function in the Ref node
94 ;;; with the new function.
95 ;;;
96 ;;; We change the Ref last, since changing the reference can trigger let
97 ;;; conversion of the new function, but will only do so if the call is local.
98 ;;; Note that the replacement may trigger let conversion or other changes in
99 ;;; IR1. We must call MERGE-TAIL-SETS with NEW-FUN before the substitution,
100 ;;; since after the substitution (and let conversion), the call may no longer
101 ;;; be recognizable as tail-recursive.
102 ;;;
103 (defun convert-call (ref call fun)
104 (declare (type ref ref) (type combination call) (type clambda fun))
105 (propagate-to-args call fun)
106 (setf (basic-combination-kind call) :local)
107 (pushnew fun (lambda-calls (node-home-lambda call)))
108 (merge-tail-sets call fun)
109 (change-ref-leaf ref fun)
110 (undefined-value))
111
112
113 ;;;; External entry point creation:
114
115 ;;; Make-XEP-Lambda -- Internal
116 ;;;
117 ;;; Return a Lambda form that can be used as the definition of the XEP for
118 ;;; Fun.
119 ;;;
120 ;;; If Fun is a lambda, then we check the number of arguments (conditional
121 ;;; on policy) and call Fun with all the arguments.
122 ;;;
123 ;;; If Fun is an Optional-Dispatch, then we dispatch off of the number of
124 ;;; supplied arguments by doing do an = test for each entry-point, calling the
125 ;;; entry with the appropriate prefix of the passed arguments.
126 ;;;
127 ;;; If there is a more arg, then there are a couple of optimizations that we
128 ;;; make (more for space than anything else):
129 ;;; -- If Min-Args is 0, then we make the more entry a T clause, since no
130 ;;; argument count error is possible.
131 ;;; -- We can omit the = clause for the last entry-point, allowing the case of
132 ;;; 0 more args to fall through to the more entry.
133 ;;;
134 ;;; We don't bother to policy conditionalize wrong arg errors in optional
135 ;;; dispatches, since the additional overhead is negligible compared to the
136 ;;; other hair going down.
137 ;;;
138 ;;; Note that if policy indicates it, argument type declarations in Fun will
139 ;;; be verified. Since nothing is known about the type of the XEP arg vars,
140 ;;; type checks will be emitted when the XEP's arg vars are passed to the
141 ;;; actual function.
142 ;;;
143 (defun make-xep-lambda (fun)
144 (declare (type functional fun))
145 (etypecase fun
146 (clambda
147 (let ((nargs (length (lambda-vars fun)))
148 (n-supplied (gensym)))
149 (collect ((temps))
150 (dotimes (i nargs)
151 (temps (gensym)))
152 `(lambda (,n-supplied ,@(temps))
153 (declare (type index ,n-supplied))
154 ,(if (policy nil (zerop safety))
155 `(declare (ignore ,n-supplied))
156 `(%verify-argument-count ,n-supplied ,nargs))
157 (%funcall ,fun ,@(temps))))))
158 (optional-dispatch
159 (let* ((min (optional-dispatch-min-args fun))
160 (max (optional-dispatch-max-args fun))
161 (more (optional-dispatch-more-entry fun))
162 (n-supplied (gensym)))
163 (collect ((temps)
164 (entries))
165 (dotimes (i max)
166 (temps (gensym)))
167
168 (do ((eps (optional-dispatch-entry-points fun) (rest eps))
169 (n min (1+ n)))
170 ((null eps))
171 (entries `((= ,n-supplied ,n)
172 (%funcall ,(first eps) ,@(subseq (temps) 0 n)))))
173
174 `(lambda (,n-supplied ,@(temps))
175 (declare (type index ,n-supplied))
176 (cond
177 ,@(if more (butlast (entries)) (entries))
178 ,@(when more
179 `((,(if (zerop min) 't `(>= ,n-supplied ,max))
180 ,(let ((n-context (gensym))
181 (n-count (gensym)))
182 `(multiple-value-bind
183 (,n-context ,n-count)
184 (%more-arg-context ,n-supplied ,max)
185 (%funcall ,more ,@(temps) ,n-context ,n-count))))))
186 (t
187 (%argument-count-error ,n-supplied)))))))))
188
189
190 ;;; Make-External-Entry-Point -- Internal
191 ;;;
192 ;;; Make an external entry point (XEP) for Fun and return it. We convert
193 ;;; the result of Make-XEP-Lambda in the correct environment, then associate
194 ;;; this lambda with Fun as its XEP. After the conversion, we iterate over the
195 ;;; function's associated lambdas, redoing local call analysis so that the XEP
196 ;;; calls will get converted. We also bind *lexical-environment* to change the
197 ;;; compilation policy over to the interface policy.
198 ;;;
199 ;;; We set Reanalyze and Reoptimize in the component, just in case we
200 ;;; discover an XEP after the initial local call analyze pass.
201 ;;;
202 (defun make-external-entry-point (fun)
203 (declare (type functional fun))
204 (assert (not (functional-entry-function fun)))
205 (with-ir1-environment (lambda-bind (main-entry fun))
206 (let* ((*lexical-environment*
207 (make-lexenv :cookie
208 (make-interface-cookie *lexical-environment*)))
209 (res (ir1-convert-lambda (make-xep-lambda fun))))
210 (setf (functional-kind res) :external)
211 (setf (leaf-ever-used res) t)
212 (setf (functional-entry-function res) fun)
213 (setf (functional-entry-function fun) res)
214 (setf (component-reanalyze *current-component*) t)
215 (setf (component-reoptimize *current-component*) t)
216 (etypecase fun
217 (clambda (local-call-analyze-1 fun))
218 (optional-dispatch
219 (dolist (ep (optional-dispatch-entry-points fun))
220 (local-call-analyze-1 ep))
221 (when (optional-dispatch-more-entry fun)
222 (local-call-analyze-1 (optional-dispatch-more-entry fun)))))
223 res)))
224
225
226 ;;; Reference-Entry-Point -- Internal
227 ;;;
228 ;;; Notice a Ref that is not in a local-call context. If the Ref is already
229 ;;; to an XEP, then do nothing, otherwise change it to the XEP, making an XEP
230 ;;; if necessary.
231 ;;;
232 ;;; If Ref is to a special :Cleanup or :Escape function, then we treat it as
233 ;;; though it was not an XEP reference (i.e. leave it alone.)
234 ;;;
235 (defun reference-entry-point (ref)
236 (declare (type ref ref))
237 (let ((fun (ref-leaf ref)))
238 (unless (or (external-entry-point-p fun)
239 (member (functional-kind fun) '(:escape :cleanup)))
240 (change-ref-leaf ref (or (functional-entry-function fun)
241 (make-external-entry-point fun))))))
242
243
244
245 ;;; Local-Call-Analyze-1 -- Interface
246 ;;;
247 ;;; Attempt to convert all references to Fun to local calls. The reference
248 ;;; must be the function for a call, and the function continuation must be used
249 ;;; only once, since otherwise we cannot be sure what function is to be called.
250 ;;; The call continuation would be multiply used if there is hairy stuff such
251 ;;; as conditionals in the expression that computes the function.
252 ;;;
253 ;;; If we cannot convert a reference, then we mark the referenced function
254 ;;; as an entry-point, creating a new XEP if necessary. We don't try to
255 ;;; convert calls that are in error (:ERROR kind.)
256 ;;;
257 ;;; This is broken off from Local-Call-Analyze so that people can force
258 ;;; analysis of newly introduced calls. Note that we don't do let conversion
259 ;;; here.
260 ;;;
261 (defun local-call-analyze-1 (fun)
262 (declare (type functional fun))
263 (let ((refs (leaf-refs fun))
264 (first-time t))
265 (dolist (ref refs)
266 (let* ((cont (node-cont ref))
267 (dest (continuation-dest cont)))
268 (cond ((and (basic-combination-p dest)
269 (eq (basic-combination-fun dest) cont)
270 (eq (continuation-use cont) ref))
271
272 (convert-call-if-possible ref dest)
273
274 (unless (eq (basic-combination-kind dest) :local)
275 (reference-entry-point ref)))
276 (t
277 (reference-entry-point ref))))
278 (setq first-time nil)))
279
280 (undefined-value))
281
282
283 ;;; Local-Call-Analyze -- Interface
284 ;;;
285 ;;; We examine all New-Functions in component, attempting to convert calls
286 ;;; into local calls when it is legal. We also attempt to convert each lambda
287 ;;; to a let. Let conversion is also triggered by deletion of a function
288 ;;; reference, but functions that start out eligible for conversion must be
289 ;;; noticed sometime.
290 ;;;
291 ;;; Note that there is a lot of action going on behind the scenes here,
292 ;;; triggered by reference deletion. In particular, the Component-Lambdas are
293 ;;; being hacked to remove newly deleted and let converted lambdas, so it is
294 ;;; important that the lambda is added to the Component-Lambdas when it is.
295 ;;; Also, the COMPONENT-NEW-FUNCTIONS may contain all sorts of drivel, since it
296 ;;; is not updated when we delete functions, etc. Only COMPONENT-LAMBDAS is
297 ;;; updated.
298 ;;;
299 ;;; COMPONENT-REANALYZE-FUNCTIONS is treated similarly to NEW-FUNCTIONS, but we
300 ;;; don't add lambdas to the LAMBDAS.
301 ;;;
302 (defun local-call-analyze (component)
303 (declare (type component component))
304 (loop
305 (let* ((new (pop (component-new-functions component)))
306 (fun (or new (pop (component-reanalyze-functions component)))))
307 (unless fun (return))
308 (let ((kind (functional-kind fun)))
309 (cond ((member kind '(:deleted :let :mv-let :assignment)))
310 ((and (null (leaf-refs fun)) (eq kind nil)
311 (not (functional-entry-function fun)))
312 (delete-functional fun))
313 (t
314 (when (and new (lambda-p fun))
315 (push fun (component-lambdas component)))
316 (local-call-analyze-1 fun)
317 (when (lambda-p fun)
318 (maybe-let-convert fun)))))))
319
320 (undefined-value))
321
322
323 ;;; MAYBE-EXPAND-LOCAL-INLINE -- Internal
324 ;;;
325 ;;; If policy is auspicious, Call is not in an XEP, and we don't seem to be
326 ;;; in an infinite recursive loop, then change the reference to reference a
327 ;;; fresh copy. We return whichever function we decide to reference.
328 ;;;
329 (defun maybe-expand-local-inline (fun ref call)
330 (if (and (policy call (>= speed space) (>= speed cspeed))
331 (not (eq (functional-kind (node-home-lambda call)) :external))
332 (inline-expansion-ok call))
333 (with-ir1-environment call
334 (let* ((*lexical-environment* (functional-lexenv fun))
335 (won nil)
336 (res (catch 'local-call-lossage
337 (prog1
338 (ir1-convert-lambda (functional-inline-expansion fun))
339 (setq won t)))))
340 (cond (won
341 (change-ref-leaf ref res)
342 res)
343 (t
344 (let ((*compiler-error-context* call))
345 (compiler-note "Couldn't inline expand because expansion ~
346 calls this let-converted local function:~
347 ~% ~S"
348 (leaf-name res)))
349 fun))))
350 fun))
351
352
353 ;;; Convert-Call-If-Possible -- Interface
354 ;;;
355 ;;; Dispatch to the appropriate function to attempt to convert a call. This
356 ;;; is called in IR1 optimize as well as in local call analysis. If the call
357 ;;; is is already :Local, we do nothing. If the call is already scheduled for
358 ;;; deletion, also do nothing (in addition to saving time, this also avoids
359 ;;; some problems with optimizing collections of functions that are partially
360 ;;; deleted.)
361 ;;;
362 ;;; This is called both before and after FIND-INITIAL-DFO runs. When called
363 ;;; on a :INITIAL component, we don't care whether the caller and callee are in
364 ;;; the same component. Afterward, we must stick with whatever component
365 ;;; division we have chosen.
366 ;;;
367 ;;; Before attempting to convert a call, we see if the function is supposed
368 ;;; to be inline expanded. Call conversion proceeds as before after any
369 ;;; expansion.
370 ;;;
371 ;;; We bind *Compiler-Error-Context* to the node for the call so that
372 ;;; warnings will get the right context.
373 ;;;
374 ;;;
375 (defun convert-call-if-possible (ref call)
376 (declare (type ref ref) (type basic-combination call))
377 (let* ((block (node-block call))
378 (component (block-component block))
379 (original-fun (ref-leaf ref)))
380 (unless (or (member (basic-combination-kind call) '(:local :error))
381 (block-delete-p block)
382 (eq (functional-kind (block-home-lambda block)) :deleted)
383 (not (or (eq (component-kind component) :initial)
384 (eq (block-component
385 (node-block
386 (lambda-bind (main-entry original-fun))))
387 component))))
388 (let ((fun (if (external-entry-point-p original-fun)
389 (functional-entry-function original-fun)
390 original-fun))
391 (*compiler-error-context* call))
392
393 (when (and (eq (functional-inlinep fun) :inline)
394 (rest (leaf-refs original-fun)))
395 (setq fun (maybe-expand-local-inline fun ref call)))
396
397 (assert (member (functional-kind fun)
398 '(nil :escape :cleanup :optional)))
399 (cond ((mv-combination-p call)
400 (convert-mv-call ref call fun))
401 ((lambda-p fun)
402 (convert-lambda-call ref call fun))
403 (t
404 (convert-hairy-call ref call fun))))))
405
406 (undefined-value))
407
408
409 ;;; Convert-MV-Call -- Internal
410 ;;;
411 ;;; Attempt to convert a multiple-value call. The only interesting case is
412 ;;; a call to a function that Looks-Like-An-MV-Bind, has exactly one reference
413 ;;; and no XEP, and is called with one values continuation.
414 ;;;
415 ;;; We change the call to be to the last optional entry point and change the
416 ;;; call to be local. Due to our preconditions, the call should eventually be
417 ;;; converted to a let, but we can't do that now, since there may be stray
418 ;;; references to the e-p lambda due to optional defaulting code.
419 ;;;
420 ;;; We also use variable types for the called function to construct an
421 ;;; assertion for the values continuation.
422 ;;;
423 ;;; See CONVERT-CALL for additional notes on MERGE-TAIL-SETS, etc.
424 ;;;
425 (defun convert-mv-call (ref call fun)
426 (declare (type ref ref) (type mv-combination call) (type functional fun))
427 (when (and (looks-like-an-mv-bind fun)
428 (not (functional-entry-function fun))
429 (= (length (leaf-refs fun)) 1)
430 (= (length (basic-combination-args call)) 1))
431 (let ((ep (car (last (optional-dispatch-entry-points fun)))))
432 (setf (basic-combination-kind call) :local)
433 (pushnew ep (lambda-calls (node-home-lambda call)))
434 (merge-tail-sets call ep)
435 (change-ref-leaf ref ep)
436
437 (assert-continuation-type
438 (first (basic-combination-args call))
439 (make-values-type :optional (mapcar #'leaf-type (lambda-vars ep))
440 :rest *universal-type*))))
441 (undefined-value))
442
443
444 ;;; Convert-Lambda-Call -- Internal
445 ;;;
446 ;;; Attempt to convert a call to a lambda. If the number of args is wrong,
447 ;;; we give a warning and mark the call as :ERROR to remove it from future
448 ;;; consideration. If the argcount is O.K. then we just convert it.
449 ;;;
450 (defun convert-lambda-call (ref call fun)
451 (declare (type ref ref) (type combination call) (type clambda fun))
452 (let ((nargs (length (lambda-vars fun)))
453 (call-args (length (combination-args call))))
454 (cond ((= call-args nargs)
455 (convert-call ref call fun))
456 (t
457 (compiler-warning
458 "Function called with ~R argument~:P, but wants exactly ~R."
459 call-args nargs)
460 (setf (basic-combination-kind call) :error)))))
461
462
463
464 ;;;; Optional, more and keyword calls:
465
466 ;;; Convert-Hairy-Call -- Internal
467 ;;;
468 ;;; Similar to Convert-Lambda-Call, but deals with Optional-Dispatches. If
469 ;;; only fixed args are supplied, then convert a call to the correct entry
470 ;;; point. If keyword args are supplied, then dispatch to a subfunction. We
471 ;;; don't convert calls to functions that have a more (or rest) arg.
472 ;;;
473 (defun convert-hairy-call (ref call fun)
474 (declare (type ref ref) (type combination call)
475 (type optional-dispatch fun))
476 (let ((min-args (optional-dispatch-min-args fun))
477 (max-args (optional-dispatch-max-args fun))
478 (call-args (length (combination-args call))))
479 (cond ((< call-args min-args)
480 (compiler-warning "Function called with ~R argument~:P, but wants at least ~R."
481 call-args min-args)
482 (setf (basic-combination-kind call) :error))
483 ((<= call-args max-args)
484 (convert-call ref call
485 (elt (optional-dispatch-entry-points fun)
486 (- call-args min-args))))
487 ((optional-dispatch-more-entry fun)
488 (convert-more-call ref call fun))
489 (t
490 (compiler-warning "Function called with ~R argument~:P, but wants at most ~R."
491 call-args max-args)
492 (setf (basic-combination-kind call) :error))))
493 (undefined-value))
494
495
496 ;;; Convert-Hairy-Fun-Entry -- Internal
497 ;;;
498 ;;; This function is used to convert a call to an entry point when complex
499 ;;; transformations need to be done on the original arguments. Entry is the
500 ;;; entry point function that we are calling. Vars is a list of variable names
501 ;;; which are bound to the oringinal call arguments. Ignores is the subset of
502 ;;; Vars which are ignored. Args is the list of arguments to the entry point
503 ;;; function.
504 ;;;
505 ;;; In order to avoid gruesome graph grovelling, we introduce a new function
506 ;;; that rearranges the arguments and calls the entry point. We analyze the
507 ;;; new function and the entry point immediately so that everything gets
508 ;;; converted during the single pass.
509 ;;;
510 (defun convert-hairy-fun-entry (ref call entry vars ignores args)
511 (declare (list vars ignores args) (type ref ref) (type combination call)
512 (type clambda entry))
513 (let ((new-fun
514 (with-ir1-environment call
515 (ir1-convert-lambda
516 `(lambda ,vars
517 (declare (ignorable . ,ignores))
518 (%funcall ,entry . ,args))))))
519 (convert-call ref call new-fun)
520 (dolist (ref (leaf-refs entry))
521 (convert-call-if-possible ref (continuation-dest (node-cont ref))))))
522
523
524 ;;; Convert-More-Call -- Internal
525 ;;;
526 ;;; Use Convert-Hairy-Fun-Entry to convert a more-arg call to a known
527 ;;; function into a local call to the Main-Entry.
528 ;;;
529 ;;; First we verify that all keywords are constant and legal. If there
530 ;;; aren't, then we warn the user and don't attempt to convert the call.
531 ;;;
532 ;;; We massage the supplied keyword arguments into the order expected by the
533 ;;; main entry. This is done by binding all the arguments to the keyword call
534 ;;; to variables in the introduced lambda, then passing these values variables
535 ;;; in the correct order when calling the main entry. Unused arguments
536 ;;; (such as the keywords themselves) are discarded simply by not passing them
537 ;;; along.
538 ;;;
539 ;;; If there is a rest arg, then we bundle up the args and pass them to
540 ;;; LIST.
541 ;;;
542 (defun convert-more-call (ref call fun)
543 (declare (type ref ref) (type combination call) (type optional-dispatch fun))
544 (let* ((max (optional-dispatch-max-args fun))
545 (arglist (optional-dispatch-arglist fun))
546 (args (combination-args call))
547 (more (nthcdr max args))
548 (flame (policy call (or (> speed brevity) (> space brevity))))
549 (loser nil))
550 (collect ((temps)
551 (more-temps)
552 (ignores)
553 (supplied)
554 (key-vars))
555
556 (dolist (var arglist)
557 (let ((info (lambda-var-arg-info var)))
558 (when info
559 (ecase (arg-info-kind info)
560 (:keyword
561 (key-vars var))
562 ((:rest :optional))
563 ((:more-context :more-count)
564 (compiler-warning "Can't local-call functions with &MORE args.")
565 (setf (basic-combination-kind call) :error)
566 (return-from convert-more-call))))))
567
568 (dotimes (i max)
569 (temps (gensym "FIXED-ARG-TEMP-")))
570
571 (dotimes (i (length more))
572 (more-temps (gensym "MORE-ARG-TEMP-")))
573
574 (when (optional-dispatch-keyp fun)
575 (when (oddp (length more))
576 (compiler-warning "Function called with odd number of ~
577 arguments in keyword portion.")
578
579 (setf (basic-combination-kind call) :error)
580 (return-from convert-more-call))
581
582 (do ((key more (cddr key))
583 (temp (more-temps) (cddr temp)))
584 ((null key))
585 (let ((cont (first key)))
586 (unless (constant-continuation-p cont)
587 (when flame
588 (compiler-note "Non-constant keyword in keyword call."))
589 (setf (basic-combination-kind call) :error)
590 (return-from convert-more-call))
591
592 (let ((name (continuation-value cont))
593 (dummy (first temp))
594 (val (second temp)))
595 (dolist (var (key-vars)
596 (progn
597 (ignores dummy val)
598 (setq loser name)))
599 (let ((info (lambda-var-arg-info var)))
600 (when (eq (arg-info-keyword info) name)
601 (ignores dummy)
602 (supplied (cons var val))
603 (return)))))))
604
605 (when (and loser (not (optional-dispatch-allowp fun)))
606 (compiler-warning "Function called with unknown argument keyword ~S."
607 loser)
608 (setf (basic-combination-kind call) :error)
609 (return-from convert-more-call)))
610
611 (collect ((call-args))
612 (do ((var arglist (cdr var))
613 (temp (temps) (cdr temp)))
614 (())
615 (let ((info (lambda-var-arg-info (car var))))
616 (if info
617 (ecase (arg-info-kind info)
618 (:optional
619 (call-args (car temp))
620 (when (arg-info-supplied-p info)
621 (call-args t)))
622 (:rest
623 (call-args `(list ,@(more-temps)))
624 (return))
625 (:keyword
626 (return)))
627 (call-args (car temp)))))
628
629 (dolist (var (key-vars))
630 (let ((info (lambda-var-arg-info var))
631 (temp (cdr (assoc var (supplied)))))
632 (if temp
633 (call-args temp)
634 (call-args (arg-info-default info)))
635 (when (arg-info-supplied-p info)
636 (call-args (not (null temp))))))
637
638 (convert-hairy-fun-entry ref call (optional-dispatch-main-entry fun)
639 (append (temps) (more-temps))
640 (ignores) (call-args)))))
641
642 (undefined-value))
643
644
645 ;;;; Let conversion:
646 ;;;
647 ;;; Converting to a let has differing significance to various parts of the
648 ;;; compiler:
649 ;;; -- The body of a Let is spliced in immediately after the the corresponding
650 ;;; combination node, making the control transfer explicit and allowing lets
651 ;;; to mashed together into a single block. The value of the let is
652 ;;; delivered directly to the original continuation for the call,
653 ;;; eliminating the need to propagate information from the dummy result
654 ;;; continuation.
655 ;;; -- As far as IR1 optimization is concerned, it is interesting in that there
656 ;;; is only one expression that the variable can be bound to, and this is
657 ;;; easily substitited for.
658 ;;; -- Lets are interesting to environment analysis and the back end because in
659 ;;; most ways a let can be considered to be "the same function" as its home
660 ;;; function.
661 ;;; -- Let conversion has dynamic scope implications, since control transfers
662 ;;; within the same environment are local. In a local control transfer,
663 ;;; cleanup code must be emitted to remove dynamic bindings that are no
664 ;;; longer in effect.
665
666
667 ;;; Insert-Let-Body -- Internal
668 ;;;
669 ;;; Set up the control transfer to the called lambda. We split the call
670 ;;; block immediately after the call, and link the head of Fun to the call
671 ;;; block. The successor block after splitting (where we return to) is
672 ;;; returned.
673 ;;;
674 ;;; If the lambda is is a different component than the call, then we call
675 ;;; JOIN-COMPONENTS. This only happens in block compilation before
676 ;;; FIND-INITIAL-DFO.
677 ;;;
678 (defun insert-let-body (fun call)
679 (declare (type clambda fun) (type basic-combination call))
680 (let* ((call-block (node-block call))
681 (bind-block (node-block (lambda-bind fun)))
682 (component (block-component call-block)))
683 (let ((fun-component (block-component bind-block)))
684 (unless (eq fun-component component)
685 (assert (eq (component-kind component) :initial))
686 (join-components component fun-component)))
687
688 (let ((*current-component* component))
689 (node-ends-block call))
690 (assert (= (length (block-succ call-block)) 1))
691 (let ((next-block (first (block-succ call-block))))
692 (unlink-blocks call-block next-block)
693 (link-blocks call-block bind-block)
694 next-block)))
695
696
697 ;;; Merge-Lets -- Internal
698 ;;;
699 ;;; Handle the environment semantics of let conversion. We add the lambda
700 ;;; and its lets to lets for the Call's home function. We merge the calls for
701 ;;; Fun with the calls for the home function, removing Fun in the process. We
702 ;;; also merge the Entries.
703 ;;;
704 ;;; We also unlink the function head from the component head and set
705 ;;; Component-Reanalyze to true to indicate that the DFO should be recomputed.
706 ;;;
707 (defun merge-lets (fun call)
708 (declare (type clambda fun) (type basic-combination call))
709 (let ((component (block-component (node-block call))))
710 (unlink-blocks (component-head component) (node-block (lambda-bind fun)))
711 (setf (component-lambdas component)
712 (delete fun (component-lambdas component)))
713 (setf (component-reanalyze component) t))
714 (setf (lambda-call-lexenv fun) (node-lexenv call))
715 (let ((tails (lambda-tail-set fun)))
716 (setf (tail-set-functions tails)
717 (delete fun (tail-set-functions tails))))
718 (setf (lambda-tail-set fun) nil)
719 (let* ((home (node-home-lambda call))
720 (home-env (lambda-environment home)))
721 (push fun (lambda-lets home))
722 (setf (lambda-home fun) home)
723 (setf (lambda-environment fun) home-env)
724
725 (let ((lets (lambda-lets fun)))
726 (dolist (let lets)
727 (setf (lambda-home let) home)
728 (setf (lambda-environment let) home-env))
729
730 (setf (lambda-lets home) (nconc lets (lambda-lets home)))
731 (setf (lambda-lets fun) ()))
732
733 (setf (lambda-calls home)
734 (nunion (lambda-calls fun)
735 (delete fun (lambda-calls home))))
736 (setf (lambda-calls fun) ())
737
738 (setf (lambda-entries home)
739 (nconc (lambda-entries fun) (lambda-entries home)))
740 (setf (lambda-entries fun) ()))
741 (undefined-value))
742
743
744 ;;; Move-Return-Uses -- Internal
745 ;;;
746 ;;; Handle the value semantics of let conversion. Delete Fun's return node,
747 ;;; and change the control flow to transfer to Next-Block instead. Move all
748 ;;; the uses of the result continuation to Call's Cont.
749 ;;;
750 ;;; If the actual continuation is only used by the let call, then we
751 ;;; intersect the type assertion on the dummy continuation with the assertion
752 ;;; for the actual continuation; in all other cases assertions on the dummy
753 ;;; continuation are lost.
754 ;;;
755 ;;; We also intersect the derived type of the call with the derived type of
756 ;;; all the dummy continuation's uses. This serves mainly to propagate
757 ;;; TRULY-THE through lets.
758 ;;;
759 (defun move-return-uses (fun call next-block)
760 (declare (type clambda fun) (type basic-combination call)
761 (type cblock next-block))
762 (let* ((return (lambda-return fun))
763 (return-block (node-block return)))
764 (unlink-blocks return-block
765 (component-tail (block-component return-block)))
766 (link-blocks return-block next-block)
767 (unlink-node return)
768 (delete-return return)
769 (let ((result (return-result return))
770 (cont (node-cont call))
771 (call-type (node-derived-type call)))
772 (when (eq (continuation-use cont) call)
773 (assert-continuation-type cont (continuation-asserted-type result)))
774 (unless (eq call-type *wild-type*)
775 (do-uses (use result)
776 (derive-node-type use call-type)))
777 (substitute-continuation-uses cont result)))
778 (undefined-value))
779
780
781
782 ;;; MOVE-LET-CALL-CONT -- Internal
783 ;;;
784 ;;; Change all Cont for all the calls to Fun to be the start continuation
785 ;;; for the bind node. This allows the blocks to be joined if the caller count
786 ;;; ever goes to one.
787 ;;;
788 (defun move-let-call-cont (fun)
789 (declare (type clambda fun))
790 (let ((new-cont (node-prev (lambda-bind fun))))
791 (dolist (ref (leaf-refs fun))
792 (let ((dest (continuation-dest (node-cont ref))))
793 (delete-continuation-use dest)
794 (add-continuation-use dest new-cont))))
795 (undefined-value))
796
797
798 ;;; Unconvert-Tail-Calls -- Internal
799 ;;;
800 ;;; We are converting Fun to be a let when the call is in a non-tail
801 ;;; position. Any previously tail calls in Fun are no longer tail calls, and
802 ;;; must be restored to normal calls which transfer to Next-Block (Fun's
803 ;;; return point.) We can't do this by DO-USES on the RETURN-RESULT, because
804 ;;; the return might have been deleted (if all calls were TR.)
805 ;;;
806 ;;; The called function might be an assignment in the case where we are
807 ;;; currently converting that function. In steady-state, assignments never
808 ;;; appear in the lambda-calls.
809 ;;;
810 (defun unconvert-tail-calls (fun call next-block)
811 (dolist (called (lambda-calls fun))
812 (dolist (ref (leaf-refs called))
813 (let ((this-call (continuation-dest (node-cont ref))))
814 (when (and (node-tail-p this-call)
815 (eq (node-home-lambda this-call) fun))
816 (setf (node-tail-p this-call) nil)
817 (ecase (functional-kind called)
818 ((nil :cleanup :optional)
819 (let ((block (node-block this-call)))
820 (unlink-blocks block (first (block-succ block)))
821 (link-blocks block next-block)
822 (delete-continuation-use this-call)
823 (add-continuation-use this-call (node-cont call))))
824 (:deleted)
825 (:assignment
826 (assert (eq called fun))))))))
827 (undefined-value))
828
829
830 ;;; MOVE-RETURN-STUFF -- Internal
831 ;;;
832 ;;; Deal with returning from a let or assignment that we are converting.
833 ;;; FUN is the function we are calling, CALL is a call to FUN, and NEXT-BLOCK
834 ;;; is the return point for a non-tail call, or NULL if call is a tail call.
835 ;;;
836 ;;; If the call is not a tail call, then we must do UNCONVERT-TAIL-CALLS, since
837 ;;; a tail call is a call which returns its value out of the enclosing non-let
838 ;;; function. When call is non-TR, we must convert it back to an ordinary
839 ;;; local call, since the value must be delivered to the receiver of CALL's
840 ;;; value.
841 ;;;
842 ;;; We do different things depending on whether the caller and callee have
843 ;;; returns left:
844 ;;; -- If the callee has no return we just do MOVE-LET-CALL-CONT. Either the
845 ;;; function doesn't return, or all returns are via tail-recursive local
846 ;;; calls.
847 ;;; -- If CALL is a non-tail call, or if both have returns, then we
848 ;;; delete the callee's return, move its uses to the call's result
849 ;;; continuation, and transfer control to the appropriate return point.
850 ;;; -- If the callee has a return, but the caller doesn't, then we move the
851 ;;; return to the caller.
852 ;;;
853 (defun move-return-stuff (fun call next-block)
854 (declare (type clambda fun) (type basic-combination call)
855 (type (or cblock null) next-block))
856 (when next-block
857 (unconvert-tail-calls fun call next-block))
858 (let* ((return (lambda-return fun))
859 (call-fun (node-home-lambda call))
860 (call-return (lambda-return call-fun)))
861 (cond ((not return))
862 ((or next-block call-return)
863 (unless (block-delete-p (node-block return))
864 (move-return-uses fun call
865 (or next-block (node-block call-return)))))
866 (t
867 (assert (node-tail-p call))
868 (setf (lambda-return call-fun) return)
869 (setf (return-lambda return) call-fun))))
870 (move-let-call-cont fun)
871 (undefined-value))
872
873
874 ;;; Let-Convert -- Internal
875 ;;;
876 ;;; Actually do let conversion. We call subfunctions to do most of the
877 ;;; work. We change the Call's cont to be the continuation heading the bind
878 ;;; block, and also do Reoptimize-Continuation on the args and Cont so that
879 ;;; let-specific IR1 optimizations get a chance. We blow away any entry for
880 ;;; the function in *free-functions* so that nobody will create new reference
881 ;;; to it.
882 ;;;
883 (defun let-convert (fun call)
884 (declare (type clambda fun) (type basic-combination call))
885 (let ((next-block (if (node-tail-p call)
886 nil
887 (insert-let-body fun call))))
888 (move-return-stuff fun call next-block)
889 (merge-lets fun call)))
890
891
892 ;;; REOPTIMIZE-CALL -- Internal
893 ;;;
894 ;;; Reoptimize all of Call's args and its result.
895 ;;;
896 (defun reoptimize-call (call)
897 (declare (type basic-combination call))
898 (dolist (arg (basic-combination-args call))
899 (when arg
900 (reoptimize-continuation arg)))
901 (reoptimize-continuation (node-cont call))
902 (undefined-value))
903
904 ;;; OK-INITIAL-CONVERT-P -- Internal
905 ;;;
906 ;;; We also don't convert calls to named functions which appear in the initial
907 ;;; component, delaying this until optimization. This minimizes the likelyhood
908 ;;; that we well let-convert a function which may have references added due to
909 ;;; later local inline expansion
910 ;;;
911 (defun ok-initial-convert-p (fun)
912 (not (and (leaf-name fun)
913 (eq (component-kind
914 (block-component
915 (node-block (lambda-bind fun))))
916 :initial))))
917
918
919 ;;; Maybe-Let-Convert -- Interface
920 ;;;
921 ;;; This function is called when there is some reason to believe that
922 ;;; the lambda Fun might be converted into a let. This is done after local
923 ;;; call analysis, and also when a reference is deleted. We only convert to a
924 ;;; let when the function is a normal local function, has no XEP, and is
925 ;;; referenced in exactly one local call. Conversion is also inhibited if the
926 ;;; only reference is in a block about to be deleted. We return true if we
927 ;;; converted.
928 ;;;
929 ;;; These rules may seem unnecessarily restrictive, since there are some
930 ;;; cases where we could do the return with a jump that don't satisfy these
931 ;;; requirements. The reason for doing things this way is that it makes the
932 ;;; concept of a let much more useful at the level of IR1 semantics. The
933 ;;; :ASSIGNMENT function kind provides another way to optimize calls to
934 ;;; single-return/multiple call functions.
935 ;;;
936 ;;; We don't attempt to convert calls to functions that have an XEP, since
937 ;;; we might be embarrassed later when we want to convert a newly discovered
938 ;;; local call. Also, see OK-INITIAL-CONVERT-P.
939 ;;;
940 (defun maybe-let-convert (fun)
941 (declare (type clambda fun))
942 (let ((refs (leaf-refs fun)))
943 (when (and refs (null (rest refs))
944 (member (functional-kind fun) '(nil :assignment))
945 (not (functional-entry-function fun)))
946 (let* ((ref-cont (node-cont (first refs)))
947 (dest (continuation-dest ref-cont)))
948 (when (and (basic-combination-p dest)
949 (eq (basic-combination-fun dest) ref-cont)
950 (eq (basic-combination-kind dest) :local)
951 (not (block-delete-p (node-block dest)))
952 (cond ((ok-initial-convert-p fun) t)
953 (t
954 (reoptimize-continuation ref-cont)
955 nil)))
956 (unless (eq (functional-kind fun) :assignment)
957 (let-convert fun dest))
958 (reoptimize-call dest)
959 (setf (functional-kind fun)
960 (if (mv-combination-p dest) :mv-let :let))))
961 t)))
962
963
964 ;;;; Tail local calls and assignments:
965
966 ;;; ONLY-HARMLESS-CLEANUPS -- Internal
967 ;;;
968 ;;; Return T if there are no cleanups between Block1 and Block2, or if they
969 ;;; definitely won't generate any cleanup code. Currently we recognize lexical
970 ;;; entry points that are only used locally (if at all).
971 ;;;
972 (defun only-harmless-cleanups (block1 block2)
973 (declare (type cblock block1 block2))
974 (or (eq block1 block2)
975 (let ((cleanup2 (block-start-cleanup block2)))
976 (do ((cleanup (block-end-cleanup block1)
977 (node-enclosing-cleanup (cleanup-mess-up cleanup))))
978 ((eq cleanup cleanup2) t)
979 (case (cleanup-kind cleanup)
980 ((:block :tagbody)
981 (unless (null (entry-exits (cleanup-mess-up cleanup)))
982 (return nil)))
983 (t (return nil)))))))
984
985
986 ;;; MAYBE-CONVERT-TAIL-LOCAL-CALL -- Interface
987 ;;;
988 ;;; If a potentially TR local call really is TR, then convert it to jump
989 ;;; directly to the called function. We also call MAYBE-CONVERT-TO-ASSIGNMENT.
990 ;;; The first value is true if we tail-convert. The second is the value of
991 ;;; M-C-T-A. We can switch the succesor (potentially deleting the RETURN node)
992 ;;; unless:
993 ;;; -- The call has already been converted.
994 ;;; -- The call isn't TR (random implicit MV PROG1.)
995 ;;; -- The call is in an XEP (thus we might decide to make it non-tail so that
996 ;;; we can use known return inside the component.)
997 ;;; -- There is a change in the cleanup between the call in the return, so we
998 ;;; might need to introduce cleanup code.
999 ;;;
1000 (defun maybe-convert-tail-local-call (call)
1001 (declare (type combination call))
1002 (let ((return (continuation-dest (node-cont call))))
1003 (assert (return-p return))
1004 (when (and (not (node-tail-p call))
1005 (immediately-used-p (return-result return) call)
1006 (not (eq (functional-kind (node-home-lambda call))
1007 :external))
1008 (only-harmless-cleanups (node-block call)
1009 (node-block return)))
1010 (node-ends-block call)
1011 (let ((block (node-block call))
1012 (fun (combination-lambda call)))
1013 (setf (node-tail-p call) t)
1014 (unlink-blocks block (first (block-succ block)))
1015 (link-blocks block (node-block (lambda-bind fun)))
1016 (values t (maybe-convert-to-assignment fun))))))
1017
1018
1019 ;;; MAYBE-CONVERT-TO-ASSIGNMENT -- Interface
1020 ;;;
1021 ;;; Called when we believe it might make sense to convert Fun to an
1022 ;;; assignment. All this function really does is determine when a function
1023 ;;; with more than one call can still be combined with the calling function's
1024 ;;; environment. We can convert when:
1025 ;;; -- The function is a normal, non-entry function, and
1026 ;;; -- Except for one call, all calls must be tail recursive calls in the
1027 ;;; called function (i.e. are self-recursive tail calls)
1028 ;;; -- OK-INITIAL-CONVERT-P is true.
1029 ;;;
1030 ;;; There may be one outside call, and it need not be tail-recursive. Since
1031 ;;; all tail local calls have already been converted to direct transfers, the
1032 ;;; only control semantics needed are to splice in the body at the non-tail
1033 ;;; call. If there is no non-tail call, then we need only merge the
1034 ;;; environments. Both cases are handled by LET-CONVERT.
1035 ;;;
1036 ;;; ### It would actually be possible to allow any number of outside calls as
1037 ;;; long as they all return to the same place (i.e. have the same conceptual
1038 ;;; continuation.) A special case of this would be when all of the outside
1039 ;;; calls are tail recursive.
1040 ;;;
1041 (defun maybe-convert-to-assignment (fun)
1042 (declare (type clambda fun))
1043 (when (and (not (functional-kind fun))
1044 (not (functional-entry-function fun)))
1045 (let ((non-tail nil)
1046 (call-fun nil))
1047 (when (and (dolist (ref (leaf-refs fun) t)
1048 (let ((dest (continuation-dest (node-cont ref))))
1049 (when (block-delete-p (node-block dest)) (return nil))
1050 (let ((home (node-home-lambda ref)))
1051 (unless (eq home fun)
1052 (when call-fun (return nil))
1053 (setq call-fun home))
1054 (unless (node-tail-p dest)
1055 (when (or non-tail (eq home fun)) (return nil))
1056 (setq non-tail dest)))))
1057 (ok-initial-convert-p fun))
1058 (setf (functional-kind fun) :assignment)
1059 (let-convert fun (or non-tail
1060 (continuation-dest
1061 (node-cont (first (leaf-refs fun))))))
1062 (when non-tail (reoptimize-call non-tail))
1063 t))))

  ViewVC Help
Powered by ViewVC 1.1.5