Fix minor corner case bugs in byte compilation and pcase.
[bpt/emacs.git] / lisp / emacs-lisp / byte-opt.el
1 ;;; byte-opt.el --- the optimization passes of the emacs-lisp byte compiler -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1991, 1994, 2000-2012 Free Software Foundation, Inc.
4
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: FSF
8 ;; Keywords: internal
9 ;; Package: emacs
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; ========================================================================
29 ;; "No matter how hard you try, you can't make a racehorse out of a pig.
30 ;; You can, however, make a faster pig."
31 ;;
32 ;; Or, to put it another way, the Emacs byte compiler is a VW Bug. This code
33 ;; makes it be a VW Bug with fuel injection and a turbocharger... You're
34 ;; still not going to make it go faster than 70 mph, but it might be easier
35 ;; to get it there.
36 ;;
37
38 ;; TO DO:
39 ;;
40 ;; (apply (lambda (x &rest y) ...) 1 (foo))
41 ;;
42 ;; maintain a list of functions known not to access any global variables
43 ;; (actually, give them a 'dynamically-safe property) and then
44 ;; (let ( v1 v2 ... vM vN ) <...dynamically-safe...> ) ==>
45 ;; (let ( v1 v2 ... vM ) vN <...dynamically-safe...> )
46 ;; by recursing on this, we might be able to eliminate the entire let.
47 ;; However certain variables should never have their bindings optimized
48 ;; away, because they affect everything.
49 ;; (put 'debug-on-error 'binding-is-magic t)
50 ;; (put 'debug-on-abort 'binding-is-magic t)
51 ;; (put 'debug-on-next-call 'binding-is-magic t)
52 ;; (put 'inhibit-quit 'binding-is-magic t)
53 ;; (put 'quit-flag 'binding-is-magic t)
54 ;; (put 't 'binding-is-magic t)
55 ;; (put 'nil 'binding-is-magic t)
56 ;; possibly also
57 ;; (put 'gc-cons-threshold 'binding-is-magic t)
58 ;; (put 'track-mouse 'binding-is-magic t)
59 ;; others?
60 ;;
61 ;; Simple defsubsts often produce forms like
62 ;; (let ((v1 (f1)) (v2 (f2)) ...)
63 ;; (FN v1 v2 ...))
64 ;; It would be nice if we could optimize this to
65 ;; (FN (f1) (f2) ...)
66 ;; but we can't unless FN is dynamically-safe (it might be dynamically
67 ;; referring to the bindings that the lambda arglist established.)
68 ;; One of the uncountable lossages introduced by dynamic scope...
69 ;;
70 ;; Maybe there should be a control-structure that says "turn on
71 ;; fast-and-loose type-assumptive optimizations here." Then when
72 ;; we see a form like (car foo) we can from then on assume that
73 ;; the variable foo is of type cons, and optimize based on that.
74 ;; But, this won't win much because of (you guessed it) dynamic
75 ;; scope. Anything down the stack could change the value.
76 ;; (Another reason it doesn't work is that it is perfectly valid
77 ;; to call car with a null argument.) A better approach might
78 ;; be to allow type-specification of the form
79 ;; (put 'foo 'arg-types '(float (list integer) dynamic))
80 ;; (put 'foo 'result-type 'bool)
81 ;; It should be possible to have these types checked to a certain
82 ;; degree.
83 ;;
84 ;; collapse common subexpressions
85 ;;
86 ;; It would be nice if redundant sequences could be factored out as well,
87 ;; when they are known to have no side-effects:
88 ;; (list (+ a b c) (+ a b c)) --> a b add c add dup list-2
89 ;; but beware of traps like
90 ;; (cons (list x y) (list x y))
91 ;;
92 ;; Tail-recursion elimination is not really possible in Emacs Lisp.
93 ;; Tail-recursion elimination is almost always impossible when all variables
94 ;; have dynamic scope, but given that the "return" byteop requires the
95 ;; binding stack to be empty (rather than emptying it itself), there can be
96 ;; no truly tail-recursive Emacs Lisp functions that take any arguments or
97 ;; make any bindings.
98 ;;
99 ;; Here is an example of an Emacs Lisp function which could safely be
100 ;; byte-compiled tail-recursively:
101 ;;
102 ;; (defun tail-map (fn list)
103 ;; (cond (list
104 ;; (funcall fn (car list))
105 ;; (tail-map fn (cdr list)))))
106 ;;
107 ;; However, if there was even a single let-binding around the COND,
108 ;; it could not be byte-compiled, because there would be an "unbind"
109 ;; byte-op between the final "call" and "return." Adding a
110 ;; Bunbind_all byteop would fix this.
111 ;;
112 ;; (defun foo (x y z) ... (foo a b c))
113 ;; ... (const foo) (varref a) (varref b) (varref c) (call 3) END: (return)
114 ;; ... (varref a) (varbind x) (varref b) (varbind y) (varref c) (varbind z) (goto 0) END: (unbind-all) (return)
115 ;; ... (varref a) (varset x) (varref b) (varset y) (varref c) (varset z) (goto 0) END: (return)
116 ;;
117 ;; this also can be considered tail recursion:
118 ;;
119 ;; ... (const foo) (varref a) (call 1) (goto X) ... X: (return)
120 ;; could generalize this by doing the optimization
121 ;; (goto X) ... X: (return) --> (return)
122 ;;
123 ;; But this doesn't solve all of the problems: although by doing tail-
124 ;; recursion elimination in this way, the call-stack does not grow, the
125 ;; binding-stack would grow with each recursive step, and would eventually
126 ;; overflow. I don't believe there is any way around this without lexical
127 ;; scope.
128 ;;
129 ;; Wouldn't it be nice if Emacs Lisp had lexical scope.
130 ;;
131 ;; Idea: the form (lexical-scope) in a file means that the file may be
132 ;; compiled lexically. This proclamation is file-local. Then, within
133 ;; that file, "let" would establish lexical bindings, and "let-dynamic"
134 ;; would do things the old way. (Or we could use CL "declare" forms.)
135 ;; We'd have to notice defvars and defconsts, since those variables should
136 ;; always be dynamic, and attempting to do a lexical binding of them
137 ;; should simply do a dynamic binding instead.
138 ;; But! We need to know about variables that were not necessarily defvared
139 ;; in the file being compiled (doing a boundp check isn't good enough.)
140 ;; Fdefvar() would have to be modified to add something to the plist.
141 ;;
142 ;; A major disadvantage of this scheme is that the interpreter and compiler
143 ;; would have different semantics for files compiled with (dynamic-scope).
144 ;; Since this would be a file-local optimization, there would be no way to
145 ;; modify the interpreter to obey this (unless the loader was hacked
146 ;; in some grody way, but that's a really bad idea.)
147
148 ;; Other things to consider:
149
150 ;; ;; Associative math should recognize subcalls to identical function:
151 ;; (disassemble (lambda (x) (+ (+ (foo) 1) (+ (bar) 2))))
152 ;; ;; This should generate the same as (1+ x) and (1- x)
153
154 ;; (disassemble (lambda (x) (cons (+ x 1) (- x 1))))
155 ;; ;; An awful lot of functions always return a non-nil value. If they're
156 ;; ;; error free also they may act as true-constants.
157
158 ;; (disassemble (lambda (x) (and (point) (foo))))
159 ;; ;; When
160 ;; ;; - all but one arguments to a function are constant
161 ;; ;; - the non-constant argument is an if-expression (cond-expression?)
162 ;; ;; then the outer function can be distributed. If the guarding
163 ;; ;; condition is side-effect-free [assignment-free] then the other
164 ;; ;; arguments may be any expressions. Since, however, the code size
165 ;; ;; can increase this way they should be "simple". Compare:
166
167 ;; (disassemble (lambda (x) (eq (if (point) 'a 'b) 'c)))
168 ;; (disassemble (lambda (x) (if (point) (eq 'a 'c) (eq 'b 'c))))
169
170 ;; ;; (car (cons A B)) -> (prog1 A B)
171 ;; (disassemble (lambda (x) (car (cons (foo) 42))))
172
173 ;; ;; (cdr (cons A B)) -> (progn A B)
174 ;; (disassemble (lambda (x) (cdr (cons 42 (foo)))))
175
176 ;; ;; (car (list A B ...)) -> (prog1 A B ...)
177 ;; (disassemble (lambda (x) (car (list (foo) 42 (bar)))))
178
179 ;; ;; (cdr (list A B ...)) -> (progn A (list B ...))
180 ;; (disassemble (lambda (x) (cdr (list 42 (foo) (bar)))))
181
182
183 ;;; Code:
184
185 (require 'bytecomp)
186 (eval-when-compile (require 'cl))
187
188 (defun byte-compile-log-lap-1 (format &rest args)
189 ;; Newer byte codes for stack-ref make the slot 0 non-nil again.
190 ;; But the "old disassembler" is *really* ancient by now.
191 ;; (if (aref byte-code-vector 0)
192 ;; (error "The old version of the disassembler is loaded. Reload new-bytecomp as well"))
193 (byte-compile-log-1
194 (apply 'format format
195 (let (c a)
196 (mapcar (lambda (arg)
197 (if (not (consp arg))
198 (if (and (symbolp arg)
199 (string-match "^byte-" (symbol-name arg)))
200 (intern (substring (symbol-name arg) 5))
201 arg)
202 (if (integerp (setq c (car arg)))
203 (error "non-symbolic byte-op %s" c))
204 (if (eq c 'TAG)
205 (setq c arg)
206 (setq a (cond ((memq c byte-goto-ops)
207 (car (cdr (cdr arg))))
208 ((memq c byte-constref-ops)
209 (car (cdr arg)))
210 (t (cdr arg))))
211 (setq c (symbol-name c))
212 (if (string-match "^byte-." c)
213 (setq c (intern (substring c 5)))))
214 (if (eq c 'constant) (setq c 'const))
215 (if (and (eq (cdr arg) 0)
216 (not (memq c '(unbind call const))))
217 c
218 (format "(%s %s)" c a))))
219 args)))))
220
221 (defmacro byte-compile-log-lap (format-string &rest args)
222 `(and (memq byte-optimize-log '(t byte))
223 (byte-compile-log-lap-1 ,format-string ,@args)))
224
225 \f
226 ;;; byte-compile optimizers to support inlining
227
228 (put 'inline 'byte-optimizer 'byte-optimize-inline-handler)
229
230 (defun byte-optimize-inline-handler (form)
231 "byte-optimize-handler for the `inline' special-form."
232 (cons 'progn
233 (mapcar
234 (lambda (sexp)
235 (let ((f (car-safe sexp)))
236 (if (and (symbolp f)
237 (or (cdr (assq f byte-compile-function-environment))
238 (not (or (not (fboundp f))
239 (cdr (assq f byte-compile-macro-environment))
240 (and (consp (setq f (symbol-function f)))
241 (eq (car f) 'macro))
242 (subrp f)))))
243 (byte-compile-inline-expand sexp)
244 sexp)))
245 (cdr form))))
246
247 (defun byte-compile-inline-expand (form)
248 (let* ((name (car form))
249 (localfn (cdr (assq name byte-compile-function-environment)))
250 (fn (or localfn (and (fboundp name) (symbol-function name)))))
251 (when (and (consp fn) (eq (car fn) 'autoload))
252 (load (nth 1 fn))
253 (setq fn (or (and (fboundp name) (symbol-function name))
254 (cdr (assq name byte-compile-function-environment)))))
255 (pcase fn
256 (`nil
257 (byte-compile-warn "attempt to inline `%s' before it was defined"
258 name)
259 form)
260 (`(autoload . ,_)
261 (error "File `%s' didn't define `%s'" (nth 1 fn) name))
262 ((and (pred symbolp) (guard (not (eq fn t)))) ;A function alias.
263 (byte-compile-inline-expand (cons fn (cdr form))))
264 ((pred byte-code-function-p)
265 ;; (message "Inlining byte-code for %S!" name)
266 ;; The byte-code will be really inlined in byte-compile-unfold-bcf.
267 `(,fn ,@(cdr form)))
268 ((or (and `(lambda ,args . ,body) (let env nil))
269 `(closure ,env ,args . ,body))
270 (if (not (or (eq fn localfn) ;From the same file => same mode.
271 (eq (not lexical-binding) (not env)))) ;Same mode.
272 ;; While byte-compile-unfold-bcf can inline dynbind byte-code into
273 ;; letbind byte-code (or any other combination for that matter), we
274 ;; can only inline dynbind source into dynbind source or letbind
275 ;; source into letbind source.
276 ;; FIXME: we could of course byte-compile the inlined function
277 ;; first, and then inline its byte-code.
278 form
279 (let ((renv ()))
280 ;; Turn the function's closed vars (if any) into local let bindings.
281 (dolist (binding env)
282 (cond
283 ((consp binding)
284 ;; We check shadowing by the args, so that the `let' can be
285 ;; moved within the lambda, which can then be unfolded.
286 ;; FIXME: Some of those bindings might be unused in `body'.
287 (unless (memq (car binding) args) ;Shadowed.
288 (push `(,(car binding) ',(cdr binding)) renv)))
289 ((eq binding t))
290 (t (push `(defvar ,binding) body))))
291 (let ((newfn (if (eq fn localfn)
292 ;; If `fn' is from the same file, it has already
293 ;; been preprocessed!
294 `(function ,fn)
295 (byte-compile-preprocess
296 (if (null renv)
297 `(lambda ,args ,@body)
298 `(lambda ,args (let ,(nreverse renv) ,@body)))))))
299 (if (eq (car-safe newfn) 'function)
300 (byte-compile-unfold-lambda `(,(cadr newfn) ,@(cdr form)))
301 (byte-compile-log-warning
302 (format "Inlining closure %S failed" name))
303 form)))))
304
305 (t ;; Give up on inlining.
306 form))))
307
308 ;; ((lambda ...) ...)
309 (defun byte-compile-unfold-lambda (form &optional name)
310 ;; In lexical-binding mode, let and functions don't bind vars in the same way
311 ;; (let obey special-variable-p, but functions don't). But luckily, this
312 ;; doesn't matter here, because function's behavior is underspecified so it
313 ;; can safely be turned into a `let', even though the reverse is not true.
314 (or name (setq name "anonymous lambda"))
315 (let ((lambda (car form))
316 (values (cdr form)))
317 (let ((arglist (nth 1 lambda))
318 (body (cdr (cdr lambda)))
319 optionalp restp
320 bindings)
321 (if (and (stringp (car body)) (cdr body))
322 (setq body (cdr body)))
323 (if (and (consp (car body)) (eq 'interactive (car (car body))))
324 (setq body (cdr body)))
325 ;; FIXME: The checks below do not belong in an optimization phase.
326 (while arglist
327 (cond ((eq (car arglist) '&optional)
328 ;; ok, I'll let this slide because funcall_lambda() does...
329 ;; (if optionalp (error "multiple &optional keywords in %s" name))
330 (if restp (error "&optional found after &rest in %s" name))
331 (if (null (cdr arglist))
332 (error "nothing after &optional in %s" name))
333 (setq optionalp t))
334 ((eq (car arglist) '&rest)
335 ;; ...but it is by no stretch of the imagination a reasonable
336 ;; thing that funcall_lambda() allows (&rest x y) and
337 ;; (&rest x &optional y) in arglists.
338 (if (null (cdr arglist))
339 (error "nothing after &rest in %s" name))
340 (if (cdr (cdr arglist))
341 (error "multiple vars after &rest in %s" name))
342 (setq restp t))
343 (restp
344 (setq bindings (cons (list (car arglist)
345 (and values (cons 'list values)))
346 bindings)
347 values nil))
348 ((and (not optionalp) (null values))
349 (byte-compile-warn "attempt to open-code `%s' with too few arguments" name)
350 (setq arglist nil values 'too-few))
351 (t
352 (setq bindings (cons (list (car arglist) (car values))
353 bindings)
354 values (cdr values))))
355 (setq arglist (cdr arglist)))
356 (if values
357 (progn
358 (or (eq values 'too-few)
359 (byte-compile-warn
360 "attempt to open-code `%s' with too many arguments" name))
361 form)
362
363 ;; The following leads to infinite recursion when loading a
364 ;; file containing `(defsubst f () (f))', and then trying to
365 ;; byte-compile that file.
366 ;(setq body (mapcar 'byte-optimize-form body)))
367
368 (let ((newform
369 (if bindings
370 (cons 'let (cons (nreverse bindings) body))
371 (cons 'progn body))))
372 (byte-compile-log " %s\t==>\t%s" form newform)
373 newform)))))
374
375 \f
376 ;;; implementing source-level optimizers
377
378 (defun byte-optimize-form-code-walker (form for-effect)
379 ;;
380 ;; For normal function calls, We can just mapcar the optimizer the cdr. But
381 ;; we need to have special knowledge of the syntax of the special forms
382 ;; like let and defun (that's why they're special forms :-). (Actually,
383 ;; the important aspect is that they are subrs that don't evaluate all of
384 ;; their args.)
385 ;;
386 (let ((fn (car-safe form))
387 tmp)
388 (cond ((not (consp form))
389 (if (not (and for-effect
390 (or byte-compile-delete-errors
391 (not (symbolp form))
392 (eq form t))))
393 form))
394 ((eq fn 'quote)
395 (if (cdr (cdr form))
396 (byte-compile-warn "malformed quote form: `%s'"
397 (prin1-to-string form)))
398 ;; map (quote nil) to nil to simplify optimizer logic.
399 ;; map quoted constants to nil if for-effect (just because).
400 (and (nth 1 form)
401 (not for-effect)
402 form))
403 ((eq 'lambda (car-safe fn))
404 (let ((newform (byte-compile-unfold-lambda form)))
405 (if (eq newform form)
406 ;; Some error occurred, avoid infinite recursion
407 form
408 (byte-optimize-form-code-walker newform for-effect))))
409 ((memq fn '(let let*))
410 ;; recursively enter the optimizer for the bindings and body
411 ;; of a let or let*. This for depth-firstness: forms that
412 ;; are more deeply nested are optimized first.
413 (cons fn
414 (cons
415 (mapcar (lambda (binding)
416 (if (symbolp binding)
417 binding
418 (if (cdr (cdr binding))
419 (byte-compile-warn "malformed let binding: `%s'"
420 (prin1-to-string binding)))
421 (list (car binding)
422 (byte-optimize-form (nth 1 binding) nil))))
423 (nth 1 form))
424 (byte-optimize-body (cdr (cdr form)) for-effect))))
425 ((eq fn 'cond)
426 (cons fn
427 (mapcar (lambda (clause)
428 (if (consp clause)
429 (cons
430 (byte-optimize-form (car clause) nil)
431 (byte-optimize-body (cdr clause) for-effect))
432 (byte-compile-warn "malformed cond form: `%s'"
433 (prin1-to-string clause))
434 clause))
435 (cdr form))))
436 ((eq fn 'progn)
437 ;; as an extra added bonus, this simplifies (progn <x>) --> <x>
438 (if (cdr (cdr form))
439 (progn
440 (setq tmp (byte-optimize-body (cdr form) for-effect))
441 (if (cdr tmp) (cons 'progn tmp) (car tmp)))
442 (byte-optimize-form (nth 1 form) for-effect)))
443 ((eq fn 'prog1)
444 (if (cdr (cdr form))
445 (cons 'prog1
446 (cons (byte-optimize-form (nth 1 form) for-effect)
447 (byte-optimize-body (cdr (cdr form)) t)))
448 (byte-optimize-form (nth 1 form) for-effect)))
449 ((eq fn 'prog2)
450 (cons 'prog2
451 (cons (byte-optimize-form (nth 1 form) t)
452 (cons (byte-optimize-form (nth 2 form) for-effect)
453 (byte-optimize-body (cdr (cdr (cdr form))) t)))))
454
455 ((memq fn '(save-excursion save-restriction save-current-buffer))
456 ;; those subrs which have an implicit progn; it's not quite good
457 ;; enough to treat these like normal function calls.
458 ;; This can turn (save-excursion ...) into (save-excursion) which
459 ;; will be optimized away in the lap-optimize pass.
460 (cons fn (byte-optimize-body (cdr form) for-effect)))
461
462 ((eq fn 'with-output-to-temp-buffer)
463 ;; this is just like the above, except for the first argument.
464 (cons fn
465 (cons
466 (byte-optimize-form (nth 1 form) nil)
467 (byte-optimize-body (cdr (cdr form)) for-effect))))
468
469 ((eq fn 'if)
470 (when (< (length form) 3)
471 (byte-compile-warn "too few arguments for `if'"))
472 (cons fn
473 (cons (byte-optimize-form (nth 1 form) nil)
474 (cons
475 (byte-optimize-form (nth 2 form) for-effect)
476 (byte-optimize-body (nthcdr 3 form) for-effect)))))
477
478 ((memq fn '(and or)) ; Remember, and/or are control structures.
479 ;; Take forms off the back until we can't any more.
480 ;; In the future it could conceivably be a problem that the
481 ;; subexpressions of these forms are optimized in the reverse
482 ;; order, but it's ok for now.
483 (if for-effect
484 (let ((backwards (reverse (cdr form))))
485 (while (and backwards
486 (null (setcar backwards
487 (byte-optimize-form (car backwards)
488 for-effect))))
489 (setq backwards (cdr backwards)))
490 (if (and (cdr form) (null backwards))
491 (byte-compile-log
492 " all subforms of %s called for effect; deleted" form))
493 (and backwards
494 (cons fn (nreverse (mapcar 'byte-optimize-form
495 backwards)))))
496 (cons fn (mapcar 'byte-optimize-form (cdr form)))))
497
498 ((eq fn 'interactive)
499 (byte-compile-warn "misplaced interactive spec: `%s'"
500 (prin1-to-string form))
501 nil)
502
503 ((memq fn '(defun defmacro function condition-case))
504 ;; These forms are compiled as constants or by breaking out
505 ;; all the subexpressions and compiling them separately.
506 form)
507
508 ((eq fn 'unwind-protect)
509 ;; the "protected" part of an unwind-protect is compiled (and thus
510 ;; optimized) as a top-level form, so don't do it here. But the
511 ;; non-protected part has the same for-effect status as the
512 ;; unwind-protect itself. (The protected part is always for effect,
513 ;; but that isn't handled properly yet.)
514 (cons fn
515 (cons (byte-optimize-form (nth 1 form) for-effect)
516 (cdr (cdr form)))))
517
518 ((eq fn 'catch)
519 ;; the body of a catch is compiled (and thus optimized) as a
520 ;; top-level form, so don't do it here. The tag is never
521 ;; for-effect. The body should have the same for-effect status
522 ;; as the catch form itself, but that isn't handled properly yet.
523 (cons fn
524 (cons (byte-optimize-form (nth 1 form) nil)
525 (cdr (cdr form)))))
526
527 ((eq fn 'ignore)
528 ;; Don't treat the args to `ignore' as being
529 ;; computed for effect. We want to avoid the warnings
530 ;; that might occur if they were treated that way.
531 ;; However, don't actually bother calling `ignore'.
532 `(prog1 nil . ,(mapcar 'byte-optimize-form (cdr form))))
533
534 ;; Needed as long as we run byte-optimize-form after cconv.
535 ((eq fn 'internal-make-closure) form)
536
537 ((byte-code-function-p fn)
538 (cons fn (mapcar #'byte-optimize-form (cdr form))))
539
540 ((not (symbolp fn))
541 (byte-compile-warn "`%s' is a malformed function"
542 (prin1-to-string fn))
543 form)
544
545 ((and for-effect (setq tmp (get fn 'side-effect-free))
546 (or byte-compile-delete-errors
547 (eq tmp 'error-free)
548 ;; Detect the expansion of (pop foo).
549 ;; There is no need to compile the call to `car' there.
550 (and (eq fn 'car)
551 (eq (car-safe (cadr form)) 'prog1)
552 (let ((var (cadr (cadr form)))
553 (last (nth 2 (cadr form))))
554 (and (symbolp var)
555 (null (nthcdr 3 (cadr form)))
556 (eq (car-safe last) 'setq)
557 (eq (cadr last) var)
558 (eq (car-safe (nth 2 last)) 'cdr)
559 (eq (cadr (nth 2 last)) var))))
560 (progn
561 (byte-compile-warn "value returned from %s is unused"
562 (prin1-to-string form))
563 nil)))
564 (byte-compile-log " %s called for effect; deleted" fn)
565 ;; appending a nil here might not be necessary, but it can't hurt.
566 (byte-optimize-form
567 (cons 'progn (append (cdr form) '(nil))) t))
568
569 (t
570 ;; Otherwise, no args can be considered to be for-effect,
571 ;; even if the called function is for-effect, because we
572 ;; don't know anything about that function.
573 (let ((args (mapcar #'byte-optimize-form (cdr form))))
574 (if (and (get fn 'pure)
575 (byte-optimize-all-constp args))
576 (list 'quote (apply fn (mapcar #'eval args)))
577 (cons fn args)))))))
578
579 (defun byte-optimize-all-constp (list)
580 "Non-nil if all elements of LIST satisfy `byte-compile-constp'."
581 (let ((constant t))
582 (while (and list constant)
583 (unless (byte-compile-constp (car list))
584 (setq constant nil))
585 (setq list (cdr list)))
586 constant))
587
588 (defun byte-optimize-form (form &optional for-effect)
589 "The source-level pass of the optimizer."
590 ;;
591 ;; First, optimize all sub-forms of this one.
592 (setq form (byte-optimize-form-code-walker form for-effect))
593 ;;
594 ;; after optimizing all subforms, optimize this form until it doesn't
595 ;; optimize any further. This means that some forms will be passed through
596 ;; the optimizer many times, but that's necessary to make the for-effect
597 ;; processing do as much as possible.
598 ;;
599 (let (opt new)
600 (if (and (consp form)
601 (symbolp (car form))
602 (or (and for-effect
603 ;; we don't have any of these yet, but we might.
604 (setq opt (get (car form) 'byte-for-effect-optimizer)))
605 (setq opt (get (car form) 'byte-optimizer)))
606 (not (eq form (setq new (funcall opt form)))))
607 (progn
608 ;; (if (equal form new) (error "bogus optimizer -- %s" opt))
609 (byte-compile-log " %s\t==>\t%s" form new)
610 (setq new (byte-optimize-form new for-effect))
611 new)
612 form)))
613
614
615 (defun byte-optimize-body (forms all-for-effect)
616 ;; Optimize the cdr of a progn or implicit progn; all forms is a list of
617 ;; forms, all but the last of which are optimized with the assumption that
618 ;; they are being called for effect. the last is for-effect as well if
619 ;; all-for-effect is true. returns a new list of forms.
620 (let ((rest forms)
621 (result nil)
622 fe new)
623 (while rest
624 (setq fe (or all-for-effect (cdr rest)))
625 (setq new (and (car rest) (byte-optimize-form (car rest) fe)))
626 (if (or new (not fe))
627 (setq result (cons new result)))
628 (setq rest (cdr rest)))
629 (nreverse result)))
630
631 \f
632 ;; some source-level optimizers
633 ;;
634 ;; when writing optimizers, be VERY careful that the optimizer returns
635 ;; something not EQ to its argument if and ONLY if it has made a change.
636 ;; This implies that you cannot simply destructively modify the list;
637 ;; you must return something not EQ to it if you make an optimization.
638 ;;
639 ;; It is now safe to optimize code such that it introduces new bindings.
640
641 (defsubst byte-compile-trueconstp (form)
642 "Return non-nil if FORM always evaluates to a non-nil value."
643 (while (eq (car-safe form) 'progn)
644 (setq form (car (last (cdr form)))))
645 (cond ((consp form)
646 (case (car form)
647 (quote (cadr form))
648 ;; Can't use recursion in a defsubst.
649 ;; (progn (byte-compile-trueconstp (car (last (cdr form)))))
650 ))
651 ((not (symbolp form)))
652 ((eq form t))
653 ((keywordp form))))
654
655 (defsubst byte-compile-nilconstp (form)
656 "Return non-nil if FORM always evaluates to a nil value."
657 (while (eq (car-safe form) 'progn)
658 (setq form (car (last (cdr form)))))
659 (cond ((consp form)
660 (case (car form)
661 (quote (null (cadr form)))
662 ;; Can't use recursion in a defsubst.
663 ;; (progn (byte-compile-nilconstp (car (last (cdr form)))))
664 ))
665 ((not (symbolp form)) nil)
666 ((null form))))
667
668 ;; If the function is being called with constant numeric args,
669 ;; evaluate as much as possible at compile-time. This optimizer
670 ;; assumes that the function is associative, like + or *.
671 (defun byte-optimize-associative-math (form)
672 (let ((args nil)
673 (constants nil)
674 (rest (cdr form)))
675 (while rest
676 (if (numberp (car rest))
677 (setq constants (cons (car rest) constants))
678 (setq args (cons (car rest) args)))
679 (setq rest (cdr rest)))
680 (if (cdr constants)
681 (if args
682 (list (car form)
683 (apply (car form) constants)
684 (if (cdr args)
685 (cons (car form) (nreverse args))
686 (car args)))
687 (apply (car form) constants))
688 form)))
689
690 ;; If the function is being called with constant numeric args,
691 ;; evaluate as much as possible at compile-time. This optimizer
692 ;; assumes that the function satisfies
693 ;; (op x1 x2 ... xn) == (op ...(op (op x1 x2) x3) ...xn)
694 ;; like - and /.
695 (defun byte-optimize-nonassociative-math (form)
696 (if (or (not (numberp (car (cdr form))))
697 (not (numberp (car (cdr (cdr form))))))
698 form
699 (let ((constant (car (cdr form)))
700 (rest (cdr (cdr form))))
701 (while (numberp (car rest))
702 (setq constant (funcall (car form) constant (car rest))
703 rest (cdr rest)))
704 (if rest
705 (cons (car form) (cons constant rest))
706 constant))))
707
708 ;;(defun byte-optimize-associative-two-args-math (form)
709 ;; (setq form (byte-optimize-associative-math form))
710 ;; (if (consp form)
711 ;; (byte-optimize-two-args-left form)
712 ;; form))
713
714 ;;(defun byte-optimize-nonassociative-two-args-math (form)
715 ;; (setq form (byte-optimize-nonassociative-math form))
716 ;; (if (consp form)
717 ;; (byte-optimize-two-args-right form)
718 ;; form))
719
720 (defun byte-optimize-approx-equal (x y)
721 (<= (* (abs (- x y)) 100) (abs (+ x y))))
722
723 ;; Collect all the constants from FORM, after the STARTth arg,
724 ;; and apply FUN to them to make one argument at the end.
725 ;; For functions that can handle floats, that optimization
726 ;; can be incorrect because reordering can cause an overflow
727 ;; that would otherwise be avoided by encountering an arg that is a float.
728 ;; We avoid this problem by (1) not moving float constants and
729 ;; (2) not moving anything if it would cause an overflow.
730 (defun byte-optimize-delay-constants-math (form start fun)
731 ;; Merge all FORM's constants from number START, call FUN on them
732 ;; and put the result at the end.
733 (let ((rest (nthcdr (1- start) form))
734 (orig form)
735 ;; t means we must check for overflow.
736 (overflow (memq fun '(+ *))))
737 (while (cdr (setq rest (cdr rest)))
738 (if (integerp (car rest))
739 (let (constants)
740 (setq form (copy-sequence form)
741 rest (nthcdr (1- start) form))
742 (while (setq rest (cdr rest))
743 (cond ((integerp (car rest))
744 (setq constants (cons (car rest) constants))
745 (setcar rest nil))))
746 ;; If necessary, check now for overflow
747 ;; that might be caused by reordering.
748 (if (and overflow
749 ;; We have overflow if the result of doing the arithmetic
750 ;; on floats is not even close to the result
751 ;; of doing it on integers.
752 (not (byte-optimize-approx-equal
753 (apply fun (mapcar 'float constants))
754 (float (apply fun constants)))))
755 (setq form orig)
756 (setq form (nconc (delq nil form)
757 (list (apply fun (nreverse constants)))))))))
758 form))
759
760 (defsubst byte-compile-butlast (form)
761 (nreverse (cdr (reverse form))))
762
763 (defun byte-optimize-plus (form)
764 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
765 ;;(setq form (byte-optimize-delay-constants-math form 1 '+))
766 (if (memq 0 form) (setq form (delq 0 (copy-sequence form))))
767 ;; For (+ constants...), byte-optimize-predicate does the work.
768 (when (memq nil (mapcar 'numberp (cdr form)))
769 (cond
770 ;; (+ x 1) --> (1+ x) and (+ x -1) --> (1- x).
771 ((and (= (length form) 3)
772 (or (memq (nth 1 form) '(1 -1))
773 (memq (nth 2 form) '(1 -1))))
774 (let (integer other)
775 (if (memq (nth 1 form) '(1 -1))
776 (setq integer (nth 1 form) other (nth 2 form))
777 (setq integer (nth 2 form) other (nth 1 form)))
778 (setq form
779 (list (if (eq integer 1) '1+ '1-) other))))
780 ;; Here, we could also do
781 ;; (+ x y ... 1) --> (1+ (+ x y ...))
782 ;; (+ x y ... -1) --> (1- (+ x y ...))
783 ;; The resulting bytecode is smaller, but is it faster? -- cyd
784 ))
785 (byte-optimize-predicate form))
786
787 (defun byte-optimize-minus (form)
788 ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
789 ;;(setq form (byte-optimize-delay-constants-math form 2 '+))
790 ;; Remove zeros.
791 (when (and (nthcdr 3 form)
792 (memq 0 (cddr form)))
793 (setq form (nconc (list (car form) (cadr form))
794 (delq 0 (copy-sequence (cddr form)))))
795 ;; After the above, we must turn (- x) back into (- x 0)
796 (or (cddr form)
797 (setq form (nconc form (list 0)))))
798 ;; For (- constants..), byte-optimize-predicate does the work.
799 (when (memq nil (mapcar 'numberp (cdr form)))
800 (cond
801 ;; (- x 1) --> (1- x)
802 ((equal (nthcdr 2 form) '(1))
803 (setq form (list '1- (nth 1 form))))
804 ;; (- x -1) --> (1+ x)
805 ((equal (nthcdr 2 form) '(-1))
806 (setq form (list '1+ (nth 1 form))))
807 ;; (- 0 x) --> (- x)
808 ((and (eq (nth 1 form) 0)
809 (= (length form) 3))
810 (setq form (list '- (nth 2 form))))
811 ;; Here, we could also do
812 ;; (- x y ... 1) --> (1- (- x y ...))
813 ;; (- x y ... -1) --> (1+ (- x y ...))
814 ;; The resulting bytecode is smaller, but is it faster? -- cyd
815 ))
816 (byte-optimize-predicate form))
817
818 (defun byte-optimize-multiply (form)
819 (setq form (byte-optimize-delay-constants-math form 1 '*))
820 ;; For (* constants..), byte-optimize-predicate does the work.
821 (when (memq nil (mapcar 'numberp (cdr form)))
822 ;; After `byte-optimize-predicate', if there is a INTEGER constant
823 ;; in FORM, it is in the last element.
824 (let ((last (car (reverse (cdr form)))))
825 (cond
826 ;; Would handling (* ... 0) here cause floating point errors?
827 ;; See bug#1334.
828 ((eq 1 last) (setq form (byte-compile-butlast form)))
829 ((eq -1 last)
830 (setq form (list '- (if (nthcdr 3 form)
831 (byte-compile-butlast form)
832 (nth 1 form))))))))
833 (byte-optimize-predicate form))
834
835 (defun byte-optimize-divide (form)
836 (setq form (byte-optimize-delay-constants-math form 2 '*))
837 ;; After `byte-optimize-predicate', if there is a INTEGER constant
838 ;; in FORM, it is in the last element.
839 (let ((last (car (reverse (cdr (cdr form))))))
840 (cond
841 ;; Runtime error (leave it intact).
842 ((or (null last)
843 (eq last 0)
844 (memql 0.0 (cddr form))))
845 ;; No constants in expression
846 ((not (numberp last)))
847 ;; For (* constants..), byte-optimize-predicate does the work.
848 ((null (memq nil (mapcar 'numberp (cdr form)))))
849 ;; (/ x y.. 1) --> (/ x y..)
850 ((and (eq last 1) (nthcdr 3 form))
851 (setq form (byte-compile-butlast form)))
852 ;; (/ x -1), (/ x .. -1) --> (- x), (- (/ x ..))
853 ((eq last -1)
854 (setq form (list '- (if (nthcdr 3 form)
855 (byte-compile-butlast form)
856 (nth 1 form)))))))
857 (byte-optimize-predicate form))
858
859 (defun byte-optimize-logmumble (form)
860 (setq form (byte-optimize-delay-constants-math form 1 (car form)))
861 (byte-optimize-predicate
862 (cond ((memq 0 form)
863 (setq form (if (eq (car form) 'logand)
864 (cons 'progn (cdr form))
865 (delq 0 (copy-sequence form)))))
866 ((and (eq (car-safe form) 'logior)
867 (memq -1 form))
868 (cons 'progn (cdr form)))
869 (form))))
870
871
872 (defun byte-optimize-binary-predicate (form)
873 (if (byte-compile-constp (nth 1 form))
874 (if (byte-compile-constp (nth 2 form))
875 (condition-case ()
876 (list 'quote (eval form))
877 (error form))
878 ;; This can enable some lapcode optimizations.
879 (list (car form) (nth 2 form) (nth 1 form)))
880 form))
881
882 (defun byte-optimize-predicate (form)
883 (let ((ok t)
884 (rest (cdr form)))
885 (while (and rest ok)
886 (setq ok (byte-compile-constp (car rest))
887 rest (cdr rest)))
888 (if ok
889 (condition-case ()
890 (list 'quote (eval form))
891 (error form))
892 form)))
893
894 (defun byte-optimize-identity (form)
895 (if (and (cdr form) (null (cdr (cdr form))))
896 (nth 1 form)
897 (byte-compile-warn "identity called with %d arg%s, but requires 1"
898 (length (cdr form))
899 (if (= 1 (length (cdr form))) "" "s"))
900 form))
901
902 (put 'identity 'byte-optimizer 'byte-optimize-identity)
903
904 (put '+ 'byte-optimizer 'byte-optimize-plus)
905 (put '* 'byte-optimizer 'byte-optimize-multiply)
906 (put '- 'byte-optimizer 'byte-optimize-minus)
907 (put '/ 'byte-optimizer 'byte-optimize-divide)
908 (put 'max 'byte-optimizer 'byte-optimize-associative-math)
909 (put 'min 'byte-optimizer 'byte-optimize-associative-math)
910
911 (put '= 'byte-optimizer 'byte-optimize-binary-predicate)
912 (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate)
913 (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate)
914 (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate)
915 (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate)
916
917 (put '< 'byte-optimizer 'byte-optimize-predicate)
918 (put '> 'byte-optimizer 'byte-optimize-predicate)
919 (put '<= 'byte-optimizer 'byte-optimize-predicate)
920 (put '>= 'byte-optimizer 'byte-optimize-predicate)
921 (put '1+ 'byte-optimizer 'byte-optimize-predicate)
922 (put '1- 'byte-optimizer 'byte-optimize-predicate)
923 (put 'not 'byte-optimizer 'byte-optimize-predicate)
924 (put 'null 'byte-optimizer 'byte-optimize-predicate)
925 (put 'memq 'byte-optimizer 'byte-optimize-predicate)
926 (put 'consp 'byte-optimizer 'byte-optimize-predicate)
927 (put 'listp 'byte-optimizer 'byte-optimize-predicate)
928 (put 'symbolp 'byte-optimizer 'byte-optimize-predicate)
929 (put 'stringp 'byte-optimizer 'byte-optimize-predicate)
930 (put 'string< 'byte-optimizer 'byte-optimize-predicate)
931 (put 'string-lessp 'byte-optimizer 'byte-optimize-predicate)
932
933 (put 'logand 'byte-optimizer 'byte-optimize-logmumble)
934 (put 'logior 'byte-optimizer 'byte-optimize-logmumble)
935 (put 'logxor 'byte-optimizer 'byte-optimize-logmumble)
936 (put 'lognot 'byte-optimizer 'byte-optimize-predicate)
937
938 (put 'car 'byte-optimizer 'byte-optimize-predicate)
939 (put 'cdr 'byte-optimizer 'byte-optimize-predicate)
940 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate)
941 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate)
942
943
944 ;; I'm not convinced that this is necessary. Doesn't the optimizer loop
945 ;; take care of this? - Jamie
946 ;; I think this may some times be necessary to reduce ie (quote 5) to 5,
947 ;; so arithmetic optimizers recognize the numeric constant. - Hallvard
948 (put 'quote 'byte-optimizer 'byte-optimize-quote)
949 (defun byte-optimize-quote (form)
950 (if (or (consp (nth 1 form))
951 (and (symbolp (nth 1 form))
952 (not (byte-compile-const-symbol-p form))))
953 form
954 (nth 1 form)))
955
956 (defun byte-optimize-zerop (form)
957 (cond ((numberp (nth 1 form))
958 (eval form))
959 (byte-compile-delete-errors
960 (list '= (nth 1 form) 0))
961 (form)))
962
963 (put 'zerop 'byte-optimizer 'byte-optimize-zerop)
964
965 (defun byte-optimize-and (form)
966 ;; Simplify if less than 2 args.
967 ;; if there is a literal nil in the args to `and', throw it and following
968 ;; forms away, and surround the `and' with (progn ... nil).
969 (cond ((null (cdr form)))
970 ((memq nil form)
971 (list 'progn
972 (byte-optimize-and
973 (prog1 (setq form (copy-sequence form))
974 (while (nth 1 form)
975 (setq form (cdr form)))
976 (setcdr form nil)))
977 nil))
978 ((null (cdr (cdr form)))
979 (nth 1 form))
980 ((byte-optimize-predicate form))))
981
982 (defun byte-optimize-or (form)
983 ;; Throw away nil's, and simplify if less than 2 args.
984 ;; If there is a literal non-nil constant in the args to `or', throw away all
985 ;; following forms.
986 (if (memq nil form)
987 (setq form (delq nil (copy-sequence form))))
988 (let ((rest form))
989 (while (cdr (setq rest (cdr rest)))
990 (if (byte-compile-trueconstp (car rest))
991 (setq form (copy-sequence form)
992 rest (setcdr (memq (car rest) form) nil))))
993 (if (cdr (cdr form))
994 (byte-optimize-predicate form)
995 (nth 1 form))))
996
997 (defun byte-optimize-cond (form)
998 ;; if any clauses have a literal nil as their test, throw them away.
999 ;; if any clause has a literal non-nil constant as its test, throw
1000 ;; away all following clauses.
1001 (let (rest)
1002 ;; This must be first, to reduce (cond (t ...) (nil)) to (progn t ...)
1003 (while (setq rest (assq nil (cdr form)))
1004 (setq form (delq rest (copy-sequence form))))
1005 (if (memq nil (cdr form))
1006 (setq form (delq nil (copy-sequence form))))
1007 (setq rest form)
1008 (while (setq rest (cdr rest))
1009 (cond ((byte-compile-trueconstp (car-safe (car rest)))
1010 ;; This branch will always be taken: kill the subsequent ones.
1011 (cond ((eq rest (cdr form)) ;First branch of `cond'.
1012 (setq form `(progn ,@(car rest))))
1013 ((cdr rest)
1014 (setq form (copy-sequence form))
1015 (setcdr (memq (car rest) form) nil)))
1016 (setq rest nil))
1017 ((and (consp (car rest))
1018 (byte-compile-nilconstp (caar rest)))
1019 ;; This branch will never be taken: kill its body.
1020 (setcdr (car rest) nil)))))
1021 ;;
1022 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... ))
1023 (if (eq 'cond (car-safe form))
1024 (let ((clauses (cdr form)))
1025 (if (and (consp (car clauses))
1026 (null (cdr (car clauses))))
1027 (list 'or (car (car clauses))
1028 (byte-optimize-cond
1029 (cons (car form) (cdr (cdr form)))))
1030 form))
1031 form))
1032
1033 (defun byte-optimize-if (form)
1034 ;; (if (progn <insts> <test>) <rest>) ==> (progn <insts> (if <test> <rest>))
1035 ;; (if <true-constant> <then> <else...>) ==> <then>
1036 ;; (if <false-constant> <then> <else...>) ==> (progn <else...>)
1037 ;; (if <test> nil <else...>) ==> (if (not <test>) (progn <else...>))
1038 ;; (if <test> <then> nil) ==> (if <test> <then>)
1039 (let ((clause (nth 1 form)))
1040 (cond ((and (eq (car-safe clause) 'progn)
1041 ;; `clause' is a proper list.
1042 (null (cdr (last clause))))
1043 (if (null (cddr clause))
1044 ;; A trivial `progn'.
1045 (byte-optimize-if `(if ,(cadr clause) ,@(nthcdr 2 form)))
1046 (nconc (butlast clause)
1047 (list
1048 (byte-optimize-if
1049 `(if ,(car (last clause)) ,@(nthcdr 2 form)))))))
1050 ((byte-compile-trueconstp clause)
1051 `(progn ,clause ,(nth 2 form)))
1052 ((byte-compile-nilconstp clause)
1053 `(progn ,clause ,@(nthcdr 3 form)))
1054 ((nth 2 form)
1055 (if (equal '(nil) (nthcdr 3 form))
1056 (list 'if clause (nth 2 form))
1057 form))
1058 ((or (nth 3 form) (nthcdr 4 form))
1059 (list 'if
1060 ;; Don't make a double negative;
1061 ;; instead, take away the one that is there.
1062 (if (and (consp clause) (memq (car clause) '(not null))
1063 (= (length clause) 2)) ; (not xxxx) or (not (xxxx))
1064 (nth 1 clause)
1065 (list 'not clause))
1066 (if (nthcdr 4 form)
1067 (cons 'progn (nthcdr 3 form))
1068 (nth 3 form))))
1069 (t
1070 (list 'progn clause nil)))))
1071
1072 (defun byte-optimize-while (form)
1073 (when (< (length form) 2)
1074 (byte-compile-warn "too few arguments for `while'"))
1075 (if (nth 1 form)
1076 form))
1077
1078 (put 'and 'byte-optimizer 'byte-optimize-and)
1079 (put 'or 'byte-optimizer 'byte-optimize-or)
1080 (put 'cond 'byte-optimizer 'byte-optimize-cond)
1081 (put 'if 'byte-optimizer 'byte-optimize-if)
1082 (put 'while 'byte-optimizer 'byte-optimize-while)
1083
1084 ;; byte-compile-negation-optimizer lives in bytecomp.el
1085 (put '/= 'byte-optimizer 'byte-compile-negation-optimizer)
1086 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer)
1087 (put 'nlistp 'byte-optimizer 'byte-compile-negation-optimizer)
1088
1089
1090 (defun byte-optimize-funcall (form)
1091 ;; (funcall (lambda ...) ...) ==> ((lambda ...) ...)
1092 ;; (funcall foo ...) ==> (foo ...)
1093 (let ((fn (nth 1 form)))
1094 (if (memq (car-safe fn) '(quote function))
1095 (cons (nth 1 fn) (cdr (cdr form)))
1096 form)))
1097
1098 (defun byte-optimize-apply (form)
1099 ;; If the last arg is a literal constant, turn this into a funcall.
1100 ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
1101 (let ((fn (nth 1 form))
1102 (last (nth (1- (length form)) form))) ; I think this really is fastest
1103 (or (if (or (null last)
1104 (eq (car-safe last) 'quote))
1105 (if (listp (nth 1 last))
1106 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form)))))))
1107 (nconc (list 'funcall fn) butlast
1108 (mapcar (lambda (x) (list 'quote x)) (nth 1 last))))
1109 (byte-compile-warn
1110 "last arg to apply can't be a literal atom: `%s'"
1111 (prin1-to-string last))
1112 nil))
1113 form)))
1114
1115 (put 'funcall 'byte-optimizer 'byte-optimize-funcall)
1116 (put 'apply 'byte-optimizer 'byte-optimize-apply)
1117
1118
1119 (put 'let 'byte-optimizer 'byte-optimize-letX)
1120 (put 'let* 'byte-optimizer 'byte-optimize-letX)
1121 (defun byte-optimize-letX (form)
1122 (cond ((null (nth 1 form))
1123 ;; No bindings
1124 (cons 'progn (cdr (cdr form))))
1125 ((or (nth 2 form) (nthcdr 3 form))
1126 form)
1127 ;; The body is nil
1128 ((eq (car form) 'let)
1129 (append '(progn) (mapcar 'car-safe (mapcar 'cdr-safe (nth 1 form)))
1130 '(nil)))
1131 (t
1132 (let ((binds (reverse (nth 1 form))))
1133 (list 'let* (reverse (cdr binds)) (nth 1 (car binds)) nil)))))
1134
1135
1136 (put 'nth 'byte-optimizer 'byte-optimize-nth)
1137 (defun byte-optimize-nth (form)
1138 (if (= (safe-length form) 3)
1139 (if (memq (nth 1 form) '(0 1))
1140 (list 'car (if (zerop (nth 1 form))
1141 (nth 2 form)
1142 (list 'cdr (nth 2 form))))
1143 (byte-optimize-predicate form))
1144 form))
1145
1146 (put 'nthcdr 'byte-optimizer 'byte-optimize-nthcdr)
1147 (defun byte-optimize-nthcdr (form)
1148 (if (= (safe-length form) 3)
1149 (if (memq (nth 1 form) '(0 1 2))
1150 (let ((count (nth 1 form)))
1151 (setq form (nth 2 form))
1152 (while (>= (setq count (1- count)) 0)
1153 (setq form (list 'cdr form)))
1154 form)
1155 (byte-optimize-predicate form))
1156 form))
1157
1158 ;; Fixme: delete-char -> delete-region (byte-coded)
1159 ;; optimize string-as-unibyte, string-as-multibyte, string-make-unibyte,
1160 ;; string-make-multibyte for constant args.
1161
1162 (put 'featurep 'byte-optimizer 'byte-optimize-featurep)
1163 (defun byte-optimize-featurep (form)
1164 ;; Emacs-21's byte-code doesn't run under XEmacs or SXEmacs anyway, so we
1165 ;; can safely optimize away this test.
1166 (if (member (cdr-safe form) '(((quote xemacs)) ((quote sxemacs))))
1167 nil
1168 (if (member (cdr-safe form) '(((quote emacs))))
1169 t
1170 form)))
1171
1172 (put 'set 'byte-optimizer 'byte-optimize-set)
1173 (defun byte-optimize-set (form)
1174 (let ((var (car-safe (cdr-safe form))))
1175 (cond
1176 ((and (eq (car-safe var) 'quote) (consp (cdr var)))
1177 `(setq ,(cadr var) ,@(cddr form)))
1178 ((and (eq (car-safe var) 'make-local-variable)
1179 (eq (car-safe (setq var (car-safe (cdr var)))) 'quote)
1180 (consp (cdr var)))
1181 `(progn ,(cadr form) (setq ,(cadr var) ,@(cddr form))))
1182 (t form))))
1183 \f
1184 ;; enumerating those functions which need not be called if the returned
1185 ;; value is not used. That is, something like
1186 ;; (progn (list (something-with-side-effects) (yow))
1187 ;; (foo))
1188 ;; may safely be turned into
1189 ;; (progn (progn (something-with-side-effects) (yow))
1190 ;; (foo))
1191 ;; Further optimizations will turn (progn (list 1 2 3) 'foo) into 'foo.
1192
1193 ;; Some of these functions have the side effect of allocating memory
1194 ;; and it would be incorrect to replace two calls with one.
1195 ;; But we don't try to do those kinds of optimizations,
1196 ;; so it is safe to list such functions here.
1197 ;; Some of these functions return values that depend on environment
1198 ;; state, so that constant folding them would be wrong,
1199 ;; but we don't do constant folding based on this list.
1200
1201 ;; However, at present the only optimization we normally do
1202 ;; is delete calls that need not occur, and we only do that
1203 ;; with the error-free functions.
1204
1205 ;; I wonder if I missed any :-\)
1206 (let ((side-effect-free-fns
1207 '(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan
1208 assoc assq
1209 boundp buffer-file-name buffer-local-variables buffer-modified-p
1210 buffer-substring byte-code-function-p
1211 capitalize car-less-than-car car cdr ceiling char-after char-before
1212 char-equal char-to-string char-width
1213 compare-strings concat coordinates-in-window-p
1214 copy-alist copy-sequence copy-marker cos count-lines
1215 decode-char
1216 decode-time default-boundp default-value documentation downcase
1217 elt encode-char exp expt encode-time error-message-string
1218 fboundp fceiling featurep ffloor
1219 file-directory-p file-exists-p file-locked-p file-name-absolute-p
1220 file-newer-than-file-p file-readable-p file-symlink-p file-writable-p
1221 float float-time floor format format-time-string frame-visible-p
1222 fround ftruncate
1223 get gethash get-buffer get-buffer-window getenv get-file-buffer
1224 hash-table-count
1225 int-to-string intern-soft
1226 keymap-parent
1227 length local-variable-if-set-p local-variable-p log log10 logand
1228 logb logior lognot logxor lsh langinfo
1229 make-list make-string make-symbol
1230 marker-buffer max member memq min mod multibyte-char-to-unibyte
1231 next-window nth nthcdr number-to-string
1232 parse-colon-path plist-get plist-member
1233 prefix-numeric-value previous-window prin1-to-string propertize
1234 degrees-to-radians
1235 radians-to-degrees rassq rassoc read-from-string regexp-quote
1236 region-beginning region-end reverse round
1237 sin sqrt string string< string= string-equal string-lessp string-to-char
1238 string-to-int string-to-number substring sxhash symbol-function
1239 symbol-name symbol-plist symbol-value string-make-unibyte
1240 string-make-multibyte string-as-multibyte string-as-unibyte
1241 string-to-multibyte
1242 tan truncate
1243 unibyte-char-to-multibyte upcase user-full-name
1244 user-login-name user-original-login-name custom-variable-p
1245 vconcat
1246 window-buffer window-dedicated-p window-edges window-height
1247 window-hscroll window-minibuffer-p window-width
1248 zerop))
1249 (side-effect-and-error-free-fns
1250 '(arrayp atom
1251 bobp bolp bool-vector-p
1252 buffer-end buffer-list buffer-size buffer-string bufferp
1253 car-safe case-table-p cdr-safe char-or-string-p characterp
1254 charsetp commandp cons consp
1255 current-buffer current-global-map current-indentation
1256 current-local-map current-minor-mode-maps current-time
1257 current-time-string current-time-zone
1258 eobp eolp eq equal eventp
1259 floatp following-char framep
1260 get-largest-window get-lru-window
1261 hash-table-p
1262 identity ignore integerp integer-or-marker-p interactive-p
1263 invocation-directory invocation-name
1264 keymapp
1265 line-beginning-position line-end-position list listp
1266 make-marker mark mark-marker markerp max-char
1267 memory-limit minibuffer-window
1268 mouse-movement-p
1269 natnump nlistp not null number-or-marker-p numberp
1270 one-window-p overlayp
1271 point point-marker point-min point-max preceding-char primary-charset
1272 processp
1273 recent-keys recursion-depth
1274 safe-length selected-frame selected-window sequencep
1275 standard-case-table standard-syntax-table stringp subrp symbolp
1276 syntax-table syntax-table-p
1277 this-command-keys this-command-keys-vector this-single-command-keys
1278 this-single-command-raw-keys
1279 user-real-login-name user-real-uid user-uid
1280 vector vectorp visible-frame-list
1281 wholenump window-configuration-p window-live-p windowp)))
1282 (while side-effect-free-fns
1283 (put (car side-effect-free-fns) 'side-effect-free t)
1284 (setq side-effect-free-fns (cdr side-effect-free-fns)))
1285 (while side-effect-and-error-free-fns
1286 (put (car side-effect-and-error-free-fns) 'side-effect-free 'error-free)
1287 (setq side-effect-and-error-free-fns (cdr side-effect-and-error-free-fns)))
1288 nil)
1289
1290 \f
1291 ;; pure functions are side-effect free functions whose values depend
1292 ;; only on their arguments. For these functions, calls with constant
1293 ;; arguments can be evaluated at compile time. This may shift run time
1294 ;; errors to compile time.
1295
1296 (let ((pure-fns
1297 '(concat symbol-name regexp-opt regexp-quote string-to-syntax)))
1298 (while pure-fns
1299 (put (car pure-fns) 'pure t)
1300 (setq pure-fns (cdr pure-fns)))
1301 nil)
1302 \f
1303 (defconst byte-constref-ops
1304 '(byte-constant byte-constant2 byte-varref byte-varset byte-varbind))
1305
1306 ;; Used and set dynamically in byte-decompile-bytecode-1.
1307 (defvar bytedecomp-op)
1308 (defvar bytedecomp-ptr)
1309
1310 ;; This function extracts the bitfields from variable-length opcodes.
1311 ;; Originally defined in disass.el (which no longer uses it.)
1312 (defun disassemble-offset (bytes)
1313 "Don't call this!"
1314 ;; Fetch and return the offset for the current opcode.
1315 ;; Return nil if this opcode has no offset.
1316 (cond ((< bytedecomp-op byte-nth)
1317 (let ((tem (logand bytedecomp-op 7)))
1318 (setq bytedecomp-op (logand bytedecomp-op 248))
1319 (cond ((eq tem 6)
1320 ;; Offset in next byte.
1321 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1322 (aref bytes bytedecomp-ptr))
1323 ((eq tem 7)
1324 ;; Offset in next 2 bytes.
1325 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1326 (+ (aref bytes bytedecomp-ptr)
1327 (progn (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1328 (lsh (aref bytes bytedecomp-ptr) 8))))
1329 (t tem)))) ;Offset was in opcode.
1330 ((>= bytedecomp-op byte-constant)
1331 (prog1 (- bytedecomp-op byte-constant) ;Offset in opcode.
1332 (setq bytedecomp-op byte-constant)))
1333 ((or (and (>= bytedecomp-op byte-constant2)
1334 (<= bytedecomp-op byte-goto-if-not-nil-else-pop))
1335 (= bytedecomp-op byte-stack-set2))
1336 ;; Offset in next 2 bytes.
1337 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1338 (+ (aref bytes bytedecomp-ptr)
1339 (progn (setq bytedecomp-ptr (1+ bytedecomp-ptr))
1340 (lsh (aref bytes bytedecomp-ptr) 8))))
1341 ((and (>= bytedecomp-op byte-listN)
1342 (<= bytedecomp-op byte-discardN))
1343 (setq bytedecomp-ptr (1+ bytedecomp-ptr)) ;Offset in next byte.
1344 (aref bytes bytedecomp-ptr))))
1345
1346 (defvar byte-compile-tag-number)
1347
1348 ;; This de-compiler is used for inline expansion of compiled functions,
1349 ;; and by the disassembler.
1350 ;;
1351 ;; This list contains numbers, which are pc values,
1352 ;; before each instruction.
1353 (defun byte-decompile-bytecode (bytes constvec)
1354 "Turn BYTECODE into lapcode, referring to CONSTVEC."
1355 (let ((byte-compile-constants nil)
1356 (byte-compile-variables nil)
1357 (byte-compile-tag-number 0))
1358 (byte-decompile-bytecode-1 bytes constvec)))
1359
1360 ;; As byte-decompile-bytecode, but updates
1361 ;; byte-compile-{constants, variables, tag-number}.
1362 ;; If MAKE-SPLICEABLE is true, then `return' opcodes are replaced
1363 ;; with `goto's destined for the end of the code.
1364 ;; That is for use by the compiler.
1365 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
1366 ;; In that case, we put a pc value into the list
1367 ;; before each insn (or its label).
1368 (defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable)
1369 (let ((length (length bytes))
1370 (bytedecomp-ptr 0) optr tags bytedecomp-op offset
1371 lap tmp)
1372 (while (not (= bytedecomp-ptr length))
1373 (or make-spliceable
1374 (push bytedecomp-ptr lap))
1375 (setq bytedecomp-op (aref bytes bytedecomp-ptr)
1376 optr bytedecomp-ptr
1377 ;; This uses dynamic-scope magic.
1378 offset (disassemble-offset bytes))
1379 (let ((opcode (aref byte-code-vector bytedecomp-op)))
1380 (assert opcode)
1381 (setq bytedecomp-op opcode))
1382 (cond ((memq bytedecomp-op byte-goto-ops)
1383 ;; It's a pc.
1384 (setq offset
1385 (cdr (or (assq offset tags)
1386 (let ((new (cons offset (byte-compile-make-tag))))
1387 (push new tags)
1388 new)))))
1389 ((cond ((eq bytedecomp-op 'byte-constant2)
1390 (setq bytedecomp-op 'byte-constant) t)
1391 ((memq bytedecomp-op byte-constref-ops)))
1392 (setq tmp (if (>= offset (length constvec))
1393 (list 'out-of-range offset)
1394 (aref constvec offset))
1395 offset (if (eq bytedecomp-op 'byte-constant)
1396 (byte-compile-get-constant tmp)
1397 (or (assq tmp byte-compile-variables)
1398 (let ((new (list tmp)))
1399 (push new byte-compile-variables)
1400 new)))))
1401 ((eq bytedecomp-op 'byte-stack-set2)
1402 (setq bytedecomp-op 'byte-stack-set))
1403 ((and (eq bytedecomp-op 'byte-discardN) (>= offset #x80))
1404 ;; The top bit of the operand for byte-discardN is a flag,
1405 ;; saying whether the top-of-stack is preserved. In
1406 ;; lapcode, we represent this by using a different opcode
1407 ;; (with the flag removed from the operand).
1408 (setq bytedecomp-op 'byte-discardN-preserve-tos)
1409 (setq offset (- offset #x80))))
1410 ;; lap = ( [ (pc . (op . arg)) ]* )
1411 (push (cons optr (cons bytedecomp-op (or offset 0)))
1412 lap)
1413 (setq bytedecomp-ptr (1+ bytedecomp-ptr)))
1414 (let ((rest lap))
1415 (while rest
1416 (cond ((numberp (car rest)))
1417 ((setq tmp (assq (car (car rest)) tags))
1418 ;; This addr is jumped to.
1419 (setcdr rest (cons (cons nil (cdr tmp))
1420 (cdr rest)))
1421 (setq tags (delq tmp tags))
1422 (setq rest (cdr rest))))
1423 (setq rest (cdr rest))))
1424 (if tags (error "optimizer error: missed tags %s" tags))
1425 ;; Remove addrs, lap = ( [ (op . arg) | (TAG tagno) ]* )
1426 (mapcar (function (lambda (elt)
1427 (if (numberp elt)
1428 elt
1429 (cdr elt))))
1430 (nreverse lap))))
1431
1432 \f
1433 ;;; peephole optimizer
1434
1435 (defconst byte-tagref-ops (cons 'TAG byte-goto-ops))
1436
1437 (defconst byte-conditional-ops
1438 '(byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop
1439 byte-goto-if-not-nil-else-pop))
1440
1441 (defconst byte-after-unbind-ops
1442 '(byte-constant byte-dup
1443 byte-symbolp byte-consp byte-stringp byte-listp byte-numberp byte-integerp
1444 byte-eq byte-not
1445 byte-cons byte-list1 byte-list2 ; byte-list3 byte-list4
1446 byte-interactive-p)
1447 ;; How about other side-effect-free-ops? Is it safe to move an
1448 ;; error invocation (such as from nth) out of an unwind-protect?
1449 ;; No, it is not, because the unwind-protect forms can alter
1450 ;; the inside of the object to which nth would apply.
1451 ;; For the same reason, byte-equal was deleted from this list.
1452 "Byte-codes that can be moved past an unbind.")
1453
1454 (defconst byte-compile-side-effect-and-error-free-ops
1455 '(byte-constant byte-dup byte-symbolp byte-consp byte-stringp byte-listp
1456 byte-integerp byte-numberp byte-eq byte-equal byte-not byte-car-safe
1457 byte-cdr-safe byte-cons byte-list1 byte-list2 byte-point byte-point-max
1458 byte-point-min byte-following-char byte-preceding-char
1459 byte-current-column byte-eolp byte-eobp byte-bolp byte-bobp
1460 byte-current-buffer byte-stack-ref))
1461
1462 (defconst byte-compile-side-effect-free-ops
1463 (nconc
1464 '(byte-varref byte-nth byte-memq byte-car byte-cdr byte-length byte-aref
1465 byte-symbol-value byte-get byte-concat2 byte-concat3 byte-sub1 byte-add1
1466 byte-eqlsign byte-gtr byte-lss byte-leq byte-geq byte-diff byte-negate
1467 byte-plus byte-max byte-min byte-mult byte-char-after byte-char-syntax
1468 byte-buffer-substring byte-string= byte-string< byte-nthcdr byte-elt
1469 byte-member byte-assq byte-quo byte-rem)
1470 byte-compile-side-effect-and-error-free-ops))
1471
1472 ;; This crock is because of the way DEFVAR_BOOL variables work.
1473 ;; Consider the code
1474 ;;
1475 ;; (defun foo (flag)
1476 ;; (let ((old-pop-ups pop-up-windows)
1477 ;; (pop-up-windows flag))
1478 ;; (cond ((not (eq pop-up-windows old-pop-ups))
1479 ;; (setq old-pop-ups pop-up-windows)
1480 ;; ...))))
1481 ;;
1482 ;; Uncompiled, old-pop-ups will always be set to nil or t, even if FLAG is
1483 ;; something else. But if we optimize
1484 ;;
1485 ;; varref flag
1486 ;; varbind pop-up-windows
1487 ;; varref pop-up-windows
1488 ;; not
1489 ;; to
1490 ;; varref flag
1491 ;; dup
1492 ;; varbind pop-up-windows
1493 ;; not
1494 ;;
1495 ;; we break the program, because it will appear that pop-up-windows and
1496 ;; old-pop-ups are not EQ when really they are. So we have to know what
1497 ;; the BOOL variables are, and not perform this optimization on them.
1498
1499 ;; The variable `byte-boolean-vars' is now primitive and updated
1500 ;; automatically by DEFVAR_BOOL.
1501
1502 (defun byte-optimize-lapcode (lap &optional _for-effect)
1503 "Simple peephole optimizer. LAP is both modified and returned.
1504 If FOR-EFFECT is non-nil, the return value is assumed to be of no importance."
1505 (let (lap0
1506 lap1
1507 lap2
1508 (keep-going 'first-time)
1509 (add-depth 0)
1510 rest tmp tmp2 tmp3
1511 (side-effect-free (if byte-compile-delete-errors
1512 byte-compile-side-effect-free-ops
1513 byte-compile-side-effect-and-error-free-ops)))
1514 (while keep-going
1515 (or (eq keep-going 'first-time)
1516 (byte-compile-log-lap " ---- next pass"))
1517 (setq rest lap
1518 keep-going nil)
1519 (while rest
1520 (setq lap0 (car rest)
1521 lap1 (nth 1 rest)
1522 lap2 (nth 2 rest))
1523
1524 ;; You may notice that sequences like "dup varset discard" are
1525 ;; optimized but sequences like "dup varset TAG1: discard" are not.
1526 ;; You may be tempted to change this; resist that temptation.
1527 (cond ;;
1528 ;; <side-effect-free> pop --> <deleted>
1529 ;; ...including:
1530 ;; const-X pop --> <deleted>
1531 ;; varref-X pop --> <deleted>
1532 ;; dup pop --> <deleted>
1533 ;;
1534 ((and (eq 'byte-discard (car lap1))
1535 (memq (car lap0) side-effect-free))
1536 (setq keep-going t)
1537 (setq tmp (aref byte-stack+-info (symbol-value (car lap0))))
1538 (setq rest (cdr rest))
1539 (cond ((= tmp 1)
1540 (byte-compile-log-lap
1541 " %s discard\t-->\t<deleted>" lap0)
1542 (setq lap (delq lap0 (delq lap1 lap))))
1543 ((= tmp 0)
1544 (byte-compile-log-lap
1545 " %s discard\t-->\t<deleted> discard" lap0)
1546 (setq lap (delq lap0 lap)))
1547 ((= tmp -1)
1548 (byte-compile-log-lap
1549 " %s discard\t-->\tdiscard discard" lap0)
1550 (setcar lap0 'byte-discard)
1551 (setcdr lap0 0))
1552 ((error "Optimizer error: too much on the stack"))))
1553 ;;
1554 ;; goto*-X X: --> X:
1555 ;;
1556 ((and (memq (car lap0) byte-goto-ops)
1557 (eq (cdr lap0) lap1))
1558 (cond ((eq (car lap0) 'byte-goto)
1559 (setq lap (delq lap0 lap))
1560 (setq tmp "<deleted>"))
1561 ((memq (car lap0) byte-goto-always-pop-ops)
1562 (setcar lap0 (setq tmp 'byte-discard))
1563 (setcdr lap0 0))
1564 ((error "Depth conflict at tag %d" (nth 2 lap0))))
1565 (and (memq byte-optimize-log '(t byte))
1566 (byte-compile-log " (goto %s) %s:\t-->\t%s %s:"
1567 (nth 1 lap1) (nth 1 lap1)
1568 tmp (nth 1 lap1)))
1569 (setq keep-going t))
1570 ;;
1571 ;; varset-X varref-X --> dup varset-X
1572 ;; varbind-X varref-X --> dup varbind-X
1573 ;; const/dup varset-X varref-X --> const/dup varset-X const/dup
1574 ;; const/dup varbind-X varref-X --> const/dup varbind-X const/dup
1575 ;; The latter two can enable other optimizations.
1576 ;;
1577 ;; For lexical variables, we could do the same
1578 ;; stack-set-X+1 stack-ref-X --> dup stack-set-X+2
1579 ;; but this is a very minor gain, since dup is stack-ref-0,
1580 ;; i.e. it's only better if X>5, and even then it comes
1581 ;; at the cost of an extra stack slot. Let's not bother.
1582 ((and (eq 'byte-varref (car lap2))
1583 (eq (cdr lap1) (cdr lap2))
1584 (memq (car lap1) '(byte-varset byte-varbind)))
1585 (if (and (setq tmp (memq (car (cdr lap2)) byte-boolean-vars))
1586 (not (eq (car lap0) 'byte-constant)))
1587 nil
1588 (setq keep-going t)
1589 (if (memq (car lap0) '(byte-constant byte-dup))
1590 (progn
1591 (setq tmp (if (or (not tmp)
1592 (byte-compile-const-symbol-p
1593 (car (cdr lap0))))
1594 (cdr lap0)
1595 (byte-compile-get-constant t)))
1596 (byte-compile-log-lap " %s %s %s\t-->\t%s %s %s"
1597 lap0 lap1 lap2 lap0 lap1
1598 (cons (car lap0) tmp))
1599 (setcar lap2 (car lap0))
1600 (setcdr lap2 tmp))
1601 (byte-compile-log-lap " %s %s\t-->\tdup %s" lap1 lap2 lap1)
1602 (setcar lap2 (car lap1))
1603 (setcar lap1 'byte-dup)
1604 (setcdr lap1 0)
1605 ;; The stack depth gets locally increased, so we will
1606 ;; increase maxdepth in case depth = maxdepth here.
1607 ;; This can cause the third argument to byte-code to
1608 ;; be larger than necessary.
1609 (setq add-depth 1))))
1610 ;;
1611 ;; dup varset-X discard --> varset-X
1612 ;; dup varbind-X discard --> varbind-X
1613 ;; dup stack-set-X discard --> stack-set-X-1
1614 ;; (the varbind variant can emerge from other optimizations)
1615 ;;
1616 ((and (eq 'byte-dup (car lap0))
1617 (eq 'byte-discard (car lap2))
1618 (memq (car lap1) '(byte-varset byte-varbind
1619 byte-stack-set)))
1620 (byte-compile-log-lap " dup %s discard\t-->\t%s" lap1 lap1)
1621 (setq keep-going t
1622 rest (cdr rest))
1623 (if (eq 'byte-stack-set (car lap1)) (decf (cdr lap1)))
1624 (setq lap (delq lap0 (delq lap2 lap))))
1625 ;;
1626 ;; not goto-X-if-nil --> goto-X-if-non-nil
1627 ;; not goto-X-if-non-nil --> goto-X-if-nil
1628 ;;
1629 ;; it is wrong to do the same thing for the -else-pop variants.
1630 ;;
1631 ((and (eq 'byte-not (car lap0))
1632 (memq (car lap1) '(byte-goto-if-nil byte-goto-if-not-nil)))
1633 (byte-compile-log-lap " not %s\t-->\t%s"
1634 lap1
1635 (cons
1636 (if (eq (car lap1) 'byte-goto-if-nil)
1637 'byte-goto-if-not-nil
1638 'byte-goto-if-nil)
1639 (cdr lap1)))
1640 (setcar lap1 (if (eq (car lap1) 'byte-goto-if-nil)
1641 'byte-goto-if-not-nil
1642 'byte-goto-if-nil))
1643 (setq lap (delq lap0 lap))
1644 (setq keep-going t))
1645 ;;
1646 ;; goto-X-if-nil goto-Y X: --> goto-Y-if-non-nil X:
1647 ;; goto-X-if-non-nil goto-Y X: --> goto-Y-if-nil X:
1648 ;;
1649 ;; it is wrong to do the same thing for the -else-pop variants.
1650 ;;
1651 ((and (memq (car lap0)
1652 '(byte-goto-if-nil byte-goto-if-not-nil)) ; gotoX
1653 (eq 'byte-goto (car lap1)) ; gotoY
1654 (eq (cdr lap0) lap2)) ; TAG X
1655 (let ((inverse (if (eq 'byte-goto-if-nil (car lap0))
1656 'byte-goto-if-not-nil 'byte-goto-if-nil)))
1657 (byte-compile-log-lap " %s %s %s:\t-->\t%s %s:"
1658 lap0 lap1 lap2
1659 (cons inverse (cdr lap1)) lap2)
1660 (setq lap (delq lap0 lap))
1661 (setcar lap1 inverse)
1662 (setq keep-going t)))
1663 ;;
1664 ;; const goto-if-* --> whatever
1665 ;;
1666 ((and (eq 'byte-constant (car lap0))
1667 (memq (car lap1) byte-conditional-ops)
1668 ;; If the `byte-constant's cdr is not a cons cell, it has
1669 ;; to be an index into the constant pool); even though
1670 ;; it'll be a constant, that constant is not known yet
1671 ;; (it's typically a free variable of a closure, so will
1672 ;; only be known when the closure will be built at
1673 ;; run-time).
1674 (consp (cdr lap0)))
1675 (cond ((if (memq (car lap1) '(byte-goto-if-nil
1676 byte-goto-if-nil-else-pop))
1677 (car (cdr lap0))
1678 (not (car (cdr lap0))))
1679 (byte-compile-log-lap " %s %s\t-->\t<deleted>"
1680 lap0 lap1)
1681 (setq rest (cdr rest)
1682 lap (delq lap0 (delq lap1 lap))))
1683 (t
1684 (byte-compile-log-lap " %s %s\t-->\t%s"
1685 lap0 lap1
1686 (cons 'byte-goto (cdr lap1)))
1687 (when (memq (car lap1) byte-goto-always-pop-ops)
1688 (setq lap (delq lap0 lap)))
1689 (setcar lap1 'byte-goto)))
1690 (setq keep-going t))
1691 ;;
1692 ;; varref-X varref-X --> varref-X dup
1693 ;; varref-X [dup ...] varref-X --> varref-X [dup ...] dup
1694 ;; stackref-X [dup ...] stackref-X+N --> stackref-X [dup ...] dup
1695 ;; We don't optimize the const-X variations on this here,
1696 ;; because that would inhibit some goto optimizations; we
1697 ;; optimize the const-X case after all other optimizations.
1698 ;;
1699 ((and (memq (car lap0) '(byte-varref byte-stack-ref))
1700 (progn
1701 (setq tmp (cdr rest))
1702 (setq tmp2 0)
1703 (while (eq (car (car tmp)) 'byte-dup)
1704 (setq tmp2 (1+ tmp2))
1705 (setq tmp (cdr tmp)))
1706 t)
1707 (eq (if (eq 'byte-stack-ref (car lap0))
1708 (+ tmp2 1 (cdr lap0))
1709 (cdr lap0))
1710 (cdr (car tmp)))
1711 (eq (car lap0) (car (car tmp))))
1712 (if (memq byte-optimize-log '(t byte))
1713 (let ((str ""))
1714 (setq tmp2 (cdr rest))
1715 (while (not (eq tmp tmp2))
1716 (setq tmp2 (cdr tmp2)
1717 str (concat str " dup")))
1718 (byte-compile-log-lap " %s%s %s\t-->\t%s%s dup"
1719 lap0 str lap0 lap0 str)))
1720 (setq keep-going t)
1721 (setcar (car tmp) 'byte-dup)
1722 (setcdr (car tmp) 0)
1723 (setq rest tmp))
1724 ;;
1725 ;; TAG1: TAG2: --> TAG1: <deleted>
1726 ;; (and other references to TAG2 are replaced with TAG1)
1727 ;;
1728 ((and (eq (car lap0) 'TAG)
1729 (eq (car lap1) 'TAG))
1730 (and (memq byte-optimize-log '(t byte))
1731 (byte-compile-log " adjacent tags %d and %d merged"
1732 (nth 1 lap1) (nth 1 lap0)))
1733 (setq tmp3 lap)
1734 (while (setq tmp2 (rassq lap0 tmp3))
1735 (setcdr tmp2 lap1)
1736 (setq tmp3 (cdr (memq tmp2 tmp3))))
1737 (setq lap (delq lap0 lap)
1738 keep-going t))
1739 ;;
1740 ;; unused-TAG: --> <deleted>
1741 ;;
1742 ((and (eq 'TAG (car lap0))
1743 (not (rassq lap0 lap)))
1744 (and (memq byte-optimize-log '(t byte))
1745 (byte-compile-log " unused tag %d removed" (nth 1 lap0)))
1746 (setq lap (delq lap0 lap)
1747 keep-going t))
1748 ;;
1749 ;; goto ... --> goto <delete until TAG or end>
1750 ;; return ... --> return <delete until TAG or end>
1751 ;;
1752 ((and (memq (car lap0) '(byte-goto byte-return))
1753 (not (memq (car lap1) '(TAG nil))))
1754 (setq tmp rest)
1755 (let ((i 0)
1756 (opt-p (memq byte-optimize-log '(t lap)))
1757 str deleted)
1758 (while (and (setq tmp (cdr tmp))
1759 (not (eq 'TAG (car (car tmp)))))
1760 (if opt-p (setq deleted (cons (car tmp) deleted)
1761 str (concat str " %s")
1762 i (1+ i))))
1763 (if opt-p
1764 (let ((tagstr
1765 (if (eq 'TAG (car (car tmp)))
1766 (format "%d:" (car (cdr (car tmp))))
1767 (or (car tmp) ""))))
1768 (if (< i 6)
1769 (apply 'byte-compile-log-lap-1
1770 (concat " %s" str
1771 " %s\t-->\t%s <deleted> %s")
1772 lap0
1773 (nconc (nreverse deleted)
1774 (list tagstr lap0 tagstr)))
1775 (byte-compile-log-lap
1776 " %s <%d unreachable op%s> %s\t-->\t%s <deleted> %s"
1777 lap0 i (if (= i 1) "" "s")
1778 tagstr lap0 tagstr))))
1779 (rplacd rest tmp))
1780 (setq keep-going t))
1781 ;;
1782 ;; <safe-op> unbind --> unbind <safe-op>
1783 ;; (this may enable other optimizations.)
1784 ;;
1785 ((and (eq 'byte-unbind (car lap1))
1786 (memq (car lap0) byte-after-unbind-ops))
1787 (byte-compile-log-lap " %s %s\t-->\t%s %s" lap0 lap1 lap1 lap0)
1788 (setcar rest lap1)
1789 (setcar (cdr rest) lap0)
1790 (setq keep-going t))
1791 ;;
1792 ;; varbind-X unbind-N --> discard unbind-(N-1)
1793 ;; save-excursion unbind-N --> unbind-(N-1)
1794 ;; save-restriction unbind-N --> unbind-(N-1)
1795 ;;
1796 ((and (eq 'byte-unbind (car lap1))
1797 (memq (car lap0) '(byte-varbind byte-save-excursion
1798 byte-save-restriction))
1799 (< 0 (cdr lap1)))
1800 (if (zerop (setcdr lap1 (1- (cdr lap1))))
1801 (delq lap1 rest))
1802 (if (eq (car lap0) 'byte-varbind)
1803 (setcar rest (cons 'byte-discard 0))
1804 (setq lap (delq lap0 lap)))
1805 (byte-compile-log-lap " %s %s\t-->\t%s %s"
1806 lap0 (cons (car lap1) (1+ (cdr lap1)))
1807 (if (eq (car lap0) 'byte-varbind)
1808 (car rest)
1809 (car (cdr rest)))
1810 (if (and (/= 0 (cdr lap1))
1811 (eq (car lap0) 'byte-varbind))
1812 (car (cdr rest))
1813 ""))
1814 (setq keep-going t))
1815 ;;
1816 ;; goto*-X ... X: goto-Y --> goto*-Y
1817 ;; goto-X ... X: return --> return
1818 ;;
1819 ((and (memq (car lap0) byte-goto-ops)
1820 (memq (car (setq tmp (nth 1 (memq (cdr lap0) lap))))
1821 '(byte-goto byte-return)))
1822 (cond ((and (not (eq tmp lap0))
1823 (or (eq (car lap0) 'byte-goto)
1824 (eq (car tmp) 'byte-goto)))
1825 (byte-compile-log-lap " %s [%s]\t-->\t%s"
1826 (car lap0) tmp tmp)
1827 (if (eq (car tmp) 'byte-return)
1828 (setcar lap0 'byte-return))
1829 (setcdr lap0 (cdr tmp))
1830 (setq keep-going t))))
1831 ;;
1832 ;; goto-*-else-pop X ... X: goto-if-* --> whatever
1833 ;; goto-*-else-pop X ... X: discard --> whatever
1834 ;;
1835 ((and (memq (car lap0) '(byte-goto-if-nil-else-pop
1836 byte-goto-if-not-nil-else-pop))
1837 (memq (car (car (setq tmp (cdr (memq (cdr lap0) lap)))))
1838 (eval-when-compile
1839 (cons 'byte-discard byte-conditional-ops)))
1840 (not (eq lap0 (car tmp))))
1841 (setq tmp2 (car tmp))
1842 (setq tmp3 (assq (car lap0) '((byte-goto-if-nil-else-pop
1843 byte-goto-if-nil)
1844 (byte-goto-if-not-nil-else-pop
1845 byte-goto-if-not-nil))))
1846 (if (memq (car tmp2) tmp3)
1847 (progn (setcar lap0 (car tmp2))
1848 (setcdr lap0 (cdr tmp2))
1849 (byte-compile-log-lap " %s-else-pop [%s]\t-->\t%s"
1850 (car lap0) tmp2 lap0))
1851 ;; Get rid of the -else-pop's and jump one step further.
1852 (or (eq 'TAG (car (nth 1 tmp)))
1853 (setcdr tmp (cons (byte-compile-make-tag)
1854 (cdr tmp))))
1855 (byte-compile-log-lap " %s [%s]\t-->\t%s <skip>"
1856 (car lap0) tmp2 (nth 1 tmp3))
1857 (setcar lap0 (nth 1 tmp3))
1858 (setcdr lap0 (nth 1 tmp)))
1859 (setq keep-going t))
1860 ;;
1861 ;; const goto-X ... X: goto-if-* --> whatever
1862 ;; const goto-X ... X: discard --> whatever
1863 ;;
1864 ((and (eq (car lap0) 'byte-constant)
1865 (eq (car lap1) 'byte-goto)
1866 (memq (car (car (setq tmp (cdr (memq (cdr lap1) lap)))))
1867 (eval-when-compile
1868 (cons 'byte-discard byte-conditional-ops)))
1869 (not (eq lap1 (car tmp))))
1870 (setq tmp2 (car tmp))
1871 (cond ((when (consp (cdr lap0))
1872 (memq (car tmp2)
1873 (if (null (car (cdr lap0)))
1874 '(byte-goto-if-nil byte-goto-if-nil-else-pop)
1875 '(byte-goto-if-not-nil
1876 byte-goto-if-not-nil-else-pop))))
1877 (byte-compile-log-lap " %s goto [%s]\t-->\t%s %s"
1878 lap0 tmp2 lap0 tmp2)
1879 (setcar lap1 (car tmp2))
1880 (setcdr lap1 (cdr tmp2))
1881 ;; Let next step fix the (const,goto-if*) sequence.
1882 (setq rest (cons nil rest))
1883 (setq keep-going t))
1884 ((or (consp (cdr lap0))
1885 (eq (car tmp2) 'byte-discard))
1886 ;; Jump one step further
1887 (byte-compile-log-lap
1888 " %s goto [%s]\t-->\t<deleted> goto <skip>"
1889 lap0 tmp2)
1890 (or (eq 'TAG (car (nth 1 tmp)))
1891 (setcdr tmp (cons (byte-compile-make-tag)
1892 (cdr tmp))))
1893 (setcdr lap1 (car (cdr tmp)))
1894 (setq lap (delq lap0 lap))
1895 (setq keep-going t))))
1896 ;;
1897 ;; X: varref-Y ... varset-Y goto-X -->
1898 ;; X: varref-Y Z: ... dup varset-Y goto-Z
1899 ;; (varset-X goto-BACK, BACK: varref-X --> copy the varref down.)
1900 ;; (This is so usual for while loops that it is worth handling).
1901 ;;
1902 ;; Here again, we could do it for stack-ref/stack-set, but
1903 ;; that's replacing a stack-ref-Y with a stack-ref-0, which
1904 ;; is a very minor improvement (if any), at the cost of
1905 ;; more stack use and more byte-code. Let's not do it.
1906 ;;
1907 ((and (eq (car lap1) 'byte-varset)
1908 (eq (car lap2) 'byte-goto)
1909 (not (memq (cdr lap2) rest)) ;Backwards jump
1910 (eq (car (car (setq tmp (cdr (memq (cdr lap2) lap)))))
1911 'byte-varref)
1912 (eq (cdr (car tmp)) (cdr lap1))
1913 (not (memq (car (cdr lap1)) byte-boolean-vars)))
1914 ;;(byte-compile-log-lap " Pulled %s to end of loop" (car tmp))
1915 (let ((newtag (byte-compile-make-tag)))
1916 (byte-compile-log-lap
1917 " %s: %s ... %s %s\t-->\t%s: %s %s: ... %s %s %s"
1918 (nth 1 (cdr lap2)) (car tmp)
1919 lap1 lap2
1920 (nth 1 (cdr lap2)) (car tmp)
1921 (nth 1 newtag) 'byte-dup lap1
1922 (cons 'byte-goto newtag)
1923 )
1924 (setcdr rest (cons (cons 'byte-dup 0) (cdr rest)))
1925 (setcdr tmp (cons (setcdr lap2 newtag) (cdr tmp))))
1926 (setq add-depth 1)
1927 (setq keep-going t))
1928 ;;
1929 ;; goto-X Y: ... X: goto-if*-Y --> goto-if-not-*-X+1 Y:
1930 ;; (This can pull the loop test to the end of the loop)
1931 ;;
1932 ((and (eq (car lap0) 'byte-goto)
1933 (eq (car lap1) 'TAG)
1934 (eq lap1
1935 (cdr (car (setq tmp (cdr (memq (cdr lap0) lap))))))
1936 (memq (car (car tmp))
1937 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
1938 byte-goto-if-nil-else-pop)))
1939 ;; (byte-compile-log-lap " %s %s, %s %s --> moved conditional"
1940 ;; lap0 lap1 (cdr lap0) (car tmp))
1941 (let ((newtag (byte-compile-make-tag)))
1942 (byte-compile-log-lap
1943 "%s %s: ... %s: %s\t-->\t%s ... %s:"
1944 lap0 (nth 1 lap1) (nth 1 (cdr lap0)) (car tmp)
1945 (cons (cdr (assq (car (car tmp))
1946 '((byte-goto-if-nil . byte-goto-if-not-nil)
1947 (byte-goto-if-not-nil . byte-goto-if-nil)
1948 (byte-goto-if-nil-else-pop .
1949 byte-goto-if-not-nil-else-pop)
1950 (byte-goto-if-not-nil-else-pop .
1951 byte-goto-if-nil-else-pop))))
1952 newtag)
1953
1954 (nth 1 newtag)
1955 )
1956 (setcdr tmp (cons (setcdr lap0 newtag) (cdr tmp)))
1957 (if (eq (car (car tmp)) 'byte-goto-if-nil-else-pop)
1958 ;; We can handle this case but not the -if-not-nil case,
1959 ;; because we won't know which non-nil constant to push.
1960 (setcdr rest (cons (cons 'byte-constant
1961 (byte-compile-get-constant nil))
1962 (cdr rest))))
1963 (setcar lap0 (nth 1 (memq (car (car tmp))
1964 '(byte-goto-if-nil-else-pop
1965 byte-goto-if-not-nil
1966 byte-goto-if-nil
1967 byte-goto-if-not-nil
1968 byte-goto byte-goto))))
1969 )
1970 (setq keep-going t))
1971 )
1972 (setq rest (cdr rest)))
1973 )
1974 ;; Cleanup stage:
1975 ;; Rebuild byte-compile-constants / byte-compile-variables.
1976 ;; Simple optimizations that would inhibit other optimizations if they
1977 ;; were done in the optimizing loop, and optimizations which there is no
1978 ;; need to do more than once.
1979 (setq byte-compile-constants nil
1980 byte-compile-variables nil)
1981 (setq rest lap)
1982 (byte-compile-log-lap " ---- final pass")
1983 (while rest
1984 (setq lap0 (car rest)
1985 lap1 (nth 1 rest))
1986 (if (memq (car lap0) byte-constref-ops)
1987 (if (memq (car lap0) '(byte-constant byte-constant2))
1988 (unless (memq (cdr lap0) byte-compile-constants)
1989 (setq byte-compile-constants (cons (cdr lap0)
1990 byte-compile-constants)))
1991 (unless (memq (cdr lap0) byte-compile-variables)
1992 (setq byte-compile-variables (cons (cdr lap0)
1993 byte-compile-variables)))))
1994 (cond (;;
1995 ;; const-C varset-X const-C --> const-C dup varset-X
1996 ;; const-C varbind-X const-C --> const-C dup varbind-X
1997 ;;
1998 (and (eq (car lap0) 'byte-constant)
1999 (eq (car (nth 2 rest)) 'byte-constant)
2000 (eq (cdr lap0) (cdr (nth 2 rest)))
2001 (memq (car lap1) '(byte-varbind byte-varset)))
2002 (byte-compile-log-lap " %s %s %s\t-->\t%s dup %s"
2003 lap0 lap1 lap0 lap0 lap1)
2004 (setcar (cdr (cdr rest)) (cons (car lap1) (cdr lap1)))
2005 (setcar (cdr rest) (cons 'byte-dup 0))
2006 (setq add-depth 1))
2007 ;;
2008 ;; const-X [dup/const-X ...] --> const-X [dup ...] dup
2009 ;; varref-X [dup/varref-X ...] --> varref-X [dup ...] dup
2010 ;;
2011 ((memq (car lap0) '(byte-constant byte-varref))
2012 (setq tmp rest
2013 tmp2 nil)
2014 (while (progn
2015 (while (eq 'byte-dup (car (car (setq tmp (cdr tmp))))))
2016 (and (eq (cdr lap0) (cdr (car tmp)))
2017 (eq (car lap0) (car (car tmp)))))
2018 (setcar tmp (cons 'byte-dup 0))
2019 (setq tmp2 t))
2020 (if tmp2
2021 (byte-compile-log-lap
2022 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0)))
2023 ;;
2024 ;; unbind-N unbind-M --> unbind-(N+M)
2025 ;;
2026 ((and (eq 'byte-unbind (car lap0))
2027 (eq 'byte-unbind (car lap1)))
2028 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1
2029 (cons 'byte-unbind
2030 (+ (cdr lap0) (cdr lap1))))
2031 (setq lap (delq lap0 lap))
2032 (setcdr lap1 (+ (cdr lap1) (cdr lap0))))
2033
2034 ;;
2035 ;; stack-set-M [discard/discardN ...] --> discardN-preserve-tos
2036 ;; stack-set-M [discard/discardN ...] --> discardN
2037 ;;
2038 ((and (eq (car lap0) 'byte-stack-set)
2039 (memq (car lap1) '(byte-discard byte-discardN))
2040 (progn
2041 ;; See if enough discard operations follow to expose or
2042 ;; destroy the value stored by the stack-set.
2043 (setq tmp (cdr rest))
2044 (setq tmp2 (1- (cdr lap0)))
2045 (setq tmp3 0)
2046 (while (memq (car (car tmp)) '(byte-discard byte-discardN))
2047 (setq tmp3
2048 (+ tmp3 (if (eq (car (car tmp)) 'byte-discard)
2049 1
2050 (cdr (car tmp)))))
2051 (setq tmp (cdr tmp)))
2052 (>= tmp3 tmp2)))
2053 ;; Do the optimization.
2054 (setq lap (delq lap0 lap))
2055 (setcar lap1
2056 (if (= tmp2 tmp3)
2057 ;; The value stored is the new TOS, so pop one more
2058 ;; value (to get rid of the old value) using the
2059 ;; TOS-preserving discard operator.
2060 'byte-discardN-preserve-tos
2061 ;; Otherwise, the value stored is lost, so just use a
2062 ;; normal discard.
2063 'byte-discardN))
2064 (setcdr lap1 (1+ tmp3))
2065 (setcdr (cdr rest) tmp)
2066 (byte-compile-log-lap " %s [discard/discardN]...\t-->\t%s"
2067 lap0 lap1))
2068
2069 ;;
2070 ;; discard/discardN/discardN-preserve-tos-X discard/discardN-Y -->
2071 ;; discardN-(X+Y)
2072 ;;
2073 ((and (memq (car lap0)
2074 '(byte-discard byte-discardN
2075 byte-discardN-preserve-tos))
2076 (memq (car lap1) '(byte-discard byte-discardN)))
2077 (setq lap (delq lap0 lap))
2078 (byte-compile-log-lap
2079 " %s %s\t-->\t(discardN %s)"
2080 lap0 lap1
2081 (+ (if (eq (car lap0) 'byte-discard) 1 (cdr lap0))
2082 (if (eq (car lap1) 'byte-discard) 1 (cdr lap1))))
2083 (setcdr lap1 (+ (if (eq (car lap0) 'byte-discard) 1 (cdr lap0))
2084 (if (eq (car lap1) 'byte-discard) 1 (cdr lap1))))
2085 (setcar lap1 'byte-discardN))
2086
2087 ;;
2088 ;; discardN-preserve-tos-X discardN-preserve-tos-Y -->
2089 ;; discardN-preserve-tos-(X+Y)
2090 ;;
2091 ((and (eq (car lap0) 'byte-discardN-preserve-tos)
2092 (eq (car lap1) 'byte-discardN-preserve-tos))
2093 (setq lap (delq lap0 lap))
2094 (setcdr lap1 (+ (cdr lap0) (cdr lap1)))
2095 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 (car rest)))
2096
2097 ;;
2098 ;; discardN-preserve-tos return --> return
2099 ;; dup return --> return
2100 ;; stack-set-N return --> return ; where N is TOS-1
2101 ;;
2102 ((and (eq (car lap1) 'byte-return)
2103 (or (memq (car lap0) '(byte-discardN-preserve-tos byte-dup))
2104 (and (eq (car lap0) 'byte-stack-set)
2105 (= (cdr lap0) 1))))
2106 ;; The byte-code interpreter will pop the stack for us, so
2107 ;; we can just leave stuff on it.
2108 (setq lap (delq lap0 lap))
2109 (byte-compile-log-lap " %s %s\t-->\t%s" lap0 lap1 lap1))
2110 )
2111 (setq rest (cdr rest)))
2112 (setq byte-compile-maxdepth (+ byte-compile-maxdepth add-depth)))
2113 lap)
2114
2115 (provide 'byte-opt)
2116
2117 \f
2118 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when this file compiles
2119 ;; itself, compile some of its most used recursive functions (at load time).
2120 ;;
2121 (eval-when-compile
2122 (or (byte-code-function-p (symbol-function 'byte-optimize-form))
2123 (assq 'byte-code (symbol-function 'byte-optimize-form))
2124 (let ((byte-optimize nil)
2125 (byte-compile-warnings nil))
2126 (mapc (lambda (x)
2127 (or noninteractive (message "compiling %s..." x))
2128 (byte-compile x)
2129 (or noninteractive (message "compiling %s...done" x)))
2130 '(byte-optimize-form
2131 byte-optimize-body
2132 byte-optimize-predicate
2133 byte-optimize-binary-predicate
2134 ;; Inserted some more than necessary, to speed it up.
2135 byte-optimize-form-code-walker
2136 byte-optimize-lapcode))))
2137 nil)
2138
2139 ;;; byte-opt.el ends here