aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Nicolaescu2007-11-13 16:10:14 +0000
committerDan Nicolaescu2007-11-13 16:10:14 +0000
commitd8947b79fcc51b605fc25acc3ba5f0bd01188726 (patch)
tree63451defd539831878d4fb0d92e7fdca8d588705
parent30a308aa4b0a2136d6a9ff0427047f07dbfe9815 (diff)
downloademacs-d8947b79fcc51b605fc25acc3ba5f0bd01188726.tar.gz
emacs-d8947b79fcc51b605fc25acc3ba5f0bd01188726.zip
* emacs-lisp/byte-opt.el (byte-compile-trueconstp): Handle more
constant forms. (byte-compile-nilconstp): New function. (byte-optimize-cond): Kill subsequent branches when a branch is know to be taken or not taken. (byte-optimize-if): Use byte-compile-nilconstp instead of hand coding.
-rw-r--r--lisp/ChangeLog10
-rw-r--r--lisp/emacs-lisp/byte-opt.el49
2 files changed, 40 insertions, 19 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 36776cf818f..cf233cfadb8 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,13 @@
12007-11-13 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * emacs-lisp/byte-opt.el (byte-compile-trueconstp): Handle more
4 constant forms.
5 (byte-compile-nilconstp): New function.
6 (byte-optimize-cond): Kill subsequent branches when a branch is
7 know to be taken or not taken.
8 (byte-optimize-if): Use byte-compile-nilconstp instead of hand
9 coding.
10
12007-11-13 Dan Nicolaescu <dann@ics.uci.edu> 112007-11-13 Dan Nicolaescu <dann@ics.uci.edu>
2 12
3 * vc.el (vc-register): Allow registering a file passed as a 13 * vc.el (vc-register): Allow registering a file passed as a
diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el
index 4097ada0bda..a9bdc3df41d 100644
--- a/lisp/emacs-lisp/byte-opt.el
+++ b/lisp/emacs-lisp/byte-opt.el
@@ -185,6 +185,7 @@
185;;; Code: 185;;; Code:
186 186
187(require 'bytecomp) 187(require 'bytecomp)
188(eval-when-compile (require 'cl))
188 189
189(defun byte-compile-log-lap-1 (format &rest args) 190(defun byte-compile-log-lap-1 (format &rest args)
190 (if (aref byte-code-vector 0) 191 (if (aref byte-code-vector 0)
@@ -626,12 +627,24 @@
626;; It is now safe to optimize code such that it introduces new bindings. 627;; It is now safe to optimize code such that it introduces new bindings.
627 628
628;; I'd like this to be a defsubst, but let's not be self-referential... 629;; I'd like this to be a defsubst, but let's not be self-referential...
629(defmacro byte-compile-trueconstp (form) 630(defsubst byte-compile-trueconstp (form)
630 ;; Returns non-nil if FORM is a non-nil constant. 631 "Return non-nil if FORM always evaluates to a non-nil value."
631 `(cond ((consp ,form) (eq (car ,form) 'quote)) 632 (cond ((consp form)
632 ((not (symbolp ,form))) 633 (case (car form)
633 ((eq ,form t)) 634 (quote (cadr form))
634 ((keywordp ,form)))) 635 (progn (byte-compile-trueconstp (car (last (cdr form)))))))
636 ((not (symbolp form)))
637 ((eq form t))
638 ((keywordp form))))
639
640(defsubst byte-compile-nilconstp (form)
641 "Return non-nil if FORM always evaluates to a nil value."
642 (cond ((consp form)
643 (case (car form)
644 (quote (null (cadr form)))
645 (progn (byte-compile-nilconstp (car (last (cdr form)))))))
646 ((not (symbolp form)) nil)
647 ((null form))))
635 648
636;; If the function is being called with constant numeric args, 649;; If the function is being called with constant numeric args,
637;; evaluate as much as possible at compile-time. This optimizer 650;; evaluate as much as possible at compile-time. This optimizer
@@ -990,17 +1003,17 @@
990 (setq rest form) 1003 (setq rest form)
991 (while (setq rest (cdr rest)) 1004 (while (setq rest (cdr rest))
992 (cond ((byte-compile-trueconstp (car-safe (car rest))) 1005 (cond ((byte-compile-trueconstp (car-safe (car rest)))
993 (cond ((eq rest (cdr form)) 1006 ;; This branch will always be taken: kill the subsequent ones.
994 (setq form 1007 (cond ((eq rest (cdr form)) ;First branch of `cond'.
995 (if (cdr (car rest)) 1008 (setq form `(progn ,@(car rest))))
996 (if (cdr (cdr (car rest)))
997 (cons 'progn (cdr (car rest)))
998 (nth 1 (car rest)))
999 (car (car rest)))))
1000 ((cdr rest) 1009 ((cdr rest)
1001 (setq form (copy-sequence form)) 1010 (setq form (copy-sequence form))
1002 (setcdr (memq (car rest) form) nil))) 1011 (setcdr (memq (car rest) form) nil)))
1003 (setq rest nil))))) 1012 (setq rest nil))
1013 ((and (consp (car rest))
1014 (byte-compile-nilconstp (caar rest)))
1015 ;; This branch will never be taken: kill its body.
1016 (setcdr (car rest) nil)))))
1004 ;; 1017 ;;
1005 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... )) 1018 ;; Turn (cond (( <x> )) ... ) into (or <x> (cond ... ))
1006 (if (eq 'cond (car-safe form)) 1019 (if (eq 'cond (car-safe form))
@@ -1031,11 +1044,9 @@
1031 (byte-optimize-if 1044 (byte-optimize-if
1032 `(if ,(car (last clause)) ,@(nthcdr 2 form))))))) 1045 `(if ,(car (last clause)) ,@(nthcdr 2 form)))))))
1033 ((byte-compile-trueconstp clause) 1046 ((byte-compile-trueconstp clause)
1034 (nth 2 form)) 1047 `(progn ,clause ,(nth 2 form)))
1035 ((null clause) 1048 ((byte-compile-nilconstp clause)
1036 (if (nthcdr 4 form) 1049 `(progn ,clause ,@(nthcdr 3 form)))
1037 (cons 'progn (nthcdr 3 form))
1038 (nth 3 form)))
1039 ((nth 2 form) 1050 ((nth 2 form)
1040 (if (equal '(nil) (nthcdr 3 form)) 1051 (if (equal '(nil) (nthcdr 3 form))
1041 (list 'if clause (nth 2 form)) 1052 (list 'if clause (nth 2 form))