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