summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/byte-opt.el
Commit message (Collapse)AuthorAgeFilesLines
* Cease attempts to const-propagate through setqMattias Engdegård2021-08-051-9/+3
| | | | | | | | | | | | | | | The current method of propagating constants through setq was unsound because it relied on each setq form only being traversed at most once during optimisation, which isn't necessarily true in general; it could be made to miscompile code in rare cases. Since it was only used in limited circumstances, disabling this optimisation doesn't cost us much. * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Don't update the known value when traversing `setq`. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases): Add test case.
* file-name-concat is not error freeLars Ingebrigtsen2021-08-031-1/+1
| | | | | * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Declare file-name-concat as side-effect free.
* Declare file-name-concat as side-effect freeLars Ingebrigtsen2021-08-031-0/+1
| | | | | * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Declare file-name-concat as side-effect (and error) free.
* Declare `match-beginning` and `match-end` as side-effect-freeMattias Engdegård2021-08-031-0/+1
| | | | * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Add functions.
* Simplify lexical let-optimisationsMattias Engdegård2021-07-301-30/+16
| | | | | | | | | | | | | | | | | | Ensure in cconv that let-bindings have the normal form (VAR EXPR) where VAR is a valid variable name, so that we don't need to keep re-checking this all the time in the optimiser. * lisp/emacs-lisp/byte-opt.el (byte-optimize-enable-variable-constprop) (byte-optimize-warn-eliminated-variable): Remove; these were mainly used for debugging. * lisp/emacs-lisp/byte-opt.el (byte-optimize-let-form): Assume normalised let-bindings (with lexical-binding). Stop using the variables removed above. * lisp/emacs-lisp/cconv.el (cconv-convert): Ensure normalised let-bindings. Malformed bindings are dropped after warning. remove byte-optimize-warn-eliminated-variable
* Optimise let and let* whose body is constant or the last variableMattias Engdegård2021-07-301-12/+25
| | | | | | | | | | | | | | | | | | Simplify (let ((X1 E1) ... (Xn En)) Xn) => (progn E1 ... En) and (let* ((X1 E1) ... (Xn En)) Xn) => (let* ((X1 E1) ... (Xn-1 En-1)) En) and similarly the case where the body is a constant, extending a previous optimisation that only applied to the constant nil. This reduces the number of bound variables, shortens the code, and enables further optimisations. * lisp/emacs-lisp/byte-opt.el (byte-optimize-letX): Rewrite using `pcase` and add the aforementioned transformations. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases): Add test cases.
* Move warnings about bad let-bindings from source optimiser to cconvMattias Engdegård2021-07-301-10/+5
| | | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-let-form): Move warnings... * lisp/emacs-lisp/cconv.el (cconv-convert): ...here, which is an overall better place (closer to the front-end).
* Optimise prog1 betterMattias Engdegård2021-07-301-5/+8
| | | | | | | | | | Rewrite (prog1 CONST FORMS...) => (progn FORMS... CONST) where CONST is a compile-time constant, because putting the value last allows the lapcode peephole pass to do important improvements like branch elimination. Also use progn instead of prog1 for `ignore`. * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): New `prog1` and `ignore` transforms.
* Elide lexical variables in for-effect context in source optimiserMattias Engdegård2021-07-301-10/+15
| | | | | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Remove for-effect uses of lexical variables. We previously relied on this being done by the lapcode peephole optimiser but at source level it enables more optimisation opportunities. Keywords are elided for the same reason.
* Single source optimiser entry pointMattias Engdegård2021-07-301-1/+8
| | | | | | | | | | | | Make the optimiser aware of lexical arguments. Otherwise we cannot know for sure whether a variable is lexical or dynamic during traversal. * lisp/emacs-lisp/byte-opt.el (byte-optimize-one-form): New optimiser entry point, replacing the recursive byte-optimize-form. * lisp/emacs-lisp/bytecomp.el (byte-optimize-one-form): Autoload. (byte-compile-keep-pending, byte-compile-top-level): Use byte-optimize-one-form.
* Warn about arity errors in inlining calls (bug#12299)Mattias Engdegård2021-07-231-1/+4
| | | | | | | | | | | | | | | Wrong number of arguments in inlining function calls (to `defsubst` or explicitly using `inline`) did not result in warnings, or in very cryptic ones. * lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Add calls to `byte-compile--check-arity-bytecode`. * lisp/emacs-lisp/bytecomp.el (byte-compile-emit-callargs-warn) (byte-compile--check-arity-bytecode): New functions. (byte-compile-callargs-warn): Use factored-out function. * test/lisp/emacs-lisp/bytecomp-resources/warn-callargs-defsubst.el: * test/lisp/emacs-lisp/bytecomp-tests.el ("warn-callargs-defsubst.el"): New test case.
* Fix mistake in `quote` optimiserMattias Engdegård2021-07-211-4/+3
| | | | | | | | | | Found by Pip Cet. * lisp/emacs-lisp/byte-opt.el (byte-optimize-quote): Fix mistake that made this optimiser ineffective at removing quoting of nil, t, and keywords. The only obvious consequence is that we no longer need... (byte-optimize-form): ...a 'nil => nil normalising step here; remove. (byte-optimize-form-code-walker): Make the compiler warn about (quote).
* ; * lisp/emacs-lisp/byte-opt.el (byte-optimize-eq): Fix last change.Mattias Engdegård2021-07-201-4/+3
|
* Strength-reduce (eq X nil) to (not X)Mattias Engdegård2021-07-201-1/+7
| | | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-eq): New optimisation, which results in better test and branch code generation where it applies.
* Constant-propagate (function SYMBOL)Mattias Engdegård2021-06-031-1/+1
| | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize--substitutable-p): Consider #'SYMBOL a constant for compile-time propagation purposes.
* Optimise (cons X nil) to (list X)Mattias Engdegård2021-06-031-0/+8
| | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-cons): New function.
* * lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Silence warningsStefan Monnier2021-05-271-18/+28
| | | | | (byte-optimize--lexvars): Move before first use instead of using `dlet` on that first use.
* Don't propagate lexical variables into inlined functionsMattias Engdegård2021-05-271-1/+4
| | | | | | | | | | | | | | | | Functions compiled when inlined (thus from inside the optimiser) mustn't retain the lexical environment of the caller or there will be tears. See discussion at https://lists.gnu.org/archive/html/emacs-devel/2021-05/msg01227.html . Bug found by Stefan Monnier. * lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Bind byte-optimize--lexvars to nil when re-entering the compiler recursively. * test/lisp/emacs-lisp/bytecomp-resources/bc-test-alpha.el: * test/lisp/emacs-lisp/bytecomp-resources/bc-test-beta.el: New files. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-defsubst): New test.
* * lisp/emacs-lisp/byte-opt.el: Make the build more reproducibleStefan Monnier2021-05-251-27/+13
| | | | | | (byte-compile-inline-expand): When inlining code from another file, always inline the byte-code version of the function. (byte-optimize--pcase): Simplify edebug spec.
* Optimize calls to 'eql', 'memql' and similar for fixnums.Philipp Stephani2021-05-161-6/+20
| | | | | | | | | | | | | It's good practice to compare integers using 'eql' because two bignum objects representing the same integer might not be 'eq'. However, 'eql' is slower and doesn't have its own byte code. Therefore, replace it with 'eq' if one argument is guaranteed to be a fixnum on all platforms. * lisp/emacs-lisp/byte-opt.el (byte-optimize--fixnump): New helper function. (byte-optimize-equal, byte-optimize-member, byte-optimize-assoc): Use it to optimize 'eql' etc. to 'eq' if it will always compare fixnums.
* Merge branch 'feature/native-comp' into into trunkAndrea Corallo2021-04-251-0/+1
|\
| * Merge remote-tracking branch 'savannah/master' into native-compAndrea Corallo2021-04-131-2/+8
| |\
| * | * lisp/emacs-lisp/byte-opt.el: Fix native re-compilation (bug#47161).Eli Zaretskii2021-03-151-0/+1
| | |
* | | Don't erroneously declare `mark` as error-freeMattias Engdegård2021-04-211-2/+2
| |/ |/| | | | | | | | | * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns) (side-effect-and-error-free-fns): `mark` is side-effect-free but not error-free.
* | Fix condition-case optimiser bugMattias Engdegård2021-04-091-2/+8
|/ | | | | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Don't perform incorrect optimisations when a condition-case variable shadows another lexical variable. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases): New test case.
* Mark string predicates side-effect-freeMattias Engdegård2021-03-101-0/+2
| | | | | | * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Add string>, string-greaterp, string-empty-p, string-prefix-p, string-suffix-p and string-blank-p, all recently marked pure.
* Don't ignore lexically-bound variables in a defvar (bug#46912)Pip Cet2021-03-051-3/+6
| | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Walk the value form of a defvar.
* Declare more string predicates as pureMattias Engdegård2021-02-281-1/+2
| | | | | | * lisp/emacs-lisp/byte-opt.el (pure-fns): Treat string>, string-greaterp, string-empty-p, string-blank-p, string-prefix-p and string-suffix-p as pure functions in the compiler.
* Add the `always' functionLars Ingebrigtsen2021-02-201-1/+1
| | | | | | | | * doc/lispref/functions.texi (Calling Functions): Document it. * lisp/subr.el (always): New function. * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Mark it as side effect free.
* Simplify expression in byte-code decompilerMattias Engdegård2021-02-121-4/+1
| | | | | * lisp/emacs-lisp/byte-opt.el (byte-decompile-bytecode-1): Replace roundabout expression with what it essentially does.
* byte-opt.el: More concise expressionMattias Engdegård2021-02-121-10/+9
| | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Refactor `setq` clause.
* Avoid traversing dead `if` branches in bytecode optimiserMattias Engdegård2021-02-121-32/+32
| | | | | | | | | | | | | | | | | | | | There is no point in traversing conditional branches that are statically known never to be executed. This saves some optimisation effort, but more importantly prevents variable assignments and references in those branches from blocking effective constant propagation. Also attempt to traverse as much as possible in an unconditional context, which enables constant-propagation through (linear) assignments. * lisp/emacs-lisp/byte-opt.el (byte-optimize-form): Rewrite the (tail) recursion into an explicit loop. Normalise a return value of (quote nil) to nil, for easier subsequent optimisations. * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Don't traverse dead `if` branches. Use unconditional traversion context when possible.
* Fix local defvar scoping error (bug#46387)Mattias Engdegård2021-02-101-2/+2
| | | | | | | | | | | | This bug was introduced by the lexical variable constant propagation mechanism. It was discovered by Michael Heerdegen. * lisp/emacs-lisp/byte-opt.el (byte-optimize-let-form) (byte-optimize-body): Let the effects of a local defvar declaration be scoped by let and let*, not any arbitrary Lisp expression body (such as progn). * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--get-vars) (bytecomp-local-defvar): New test.
* * lisp/emacs-lisp/byte-opt.el (byte-optimize--pcase): New macroStefan Monnier2021-02-091-20/+50
| | | | (byte-optimize-form-code-walker): Use it.
* * lisp/emacs-lisp/macroexp.el: Break cycle with bytecomp/byte-optStefan Monnier2021-02-091-70/+2
| | | | | | | | | | | | | | | | | The recent change in macroexp triggered a cyclic dependency error during eager macroexpansion when neither `bytecomp` nor `byte-opt` had been byte-compiled yet. This fixes it by moving the offending function to macroexp.el. * lisp/emacs-lisp/macroexp.el (macroexp--unfold-lambda): Move from byte-opt.el and rename. (macroexp--expand-all): Use it. * lisp/emacs-lisp/byte-opt.el (byte-compile-unfold-lambda): Move to macroexp.el. (byte-compile-inline-expand, byte-optimize-form-code-walker): * lisp/emacs-lisp/bytecomp.el (byte-compile-form): Use `macroexp--unfold-lambda` instead.
* ; * lisp/emacs-lisp/byte-opt.el: improved commentMattias Engdegård2021-02-071-1/+1
|
* ; Improved commentary in the variable constprop mechanismMattias Engdegård2021-02-071-8/+13
| | | | | | | | * lisp/emacs-lisp/byte-opt.el (byte-optimize--lexvars) (byte-optimize--vars-outside-condition) (byte-optimize-form-code-walker, byte-optimize-let-form): Clarify various aspects in the variable constant-propagation code, as kindly pointed out by Stefan Monnier.
* Constprop of lexical variablesMattias Engdegård2021-02-061-70/+244
| | | | | | | | | | | | | | | | | | | | | | | | | | | Lexical variables bound to a constant value (symbol, number or string) are substituted at their point of use and the variable then eliminated if possible. Example: (let ((x (+ 2 3))) (f x)) => (f 5) This reduces code size, eliminates stack operations, and enables further optimisations. The implementation is conservative, and is strongly curtailed by the presence of variable mutation, conditions and loops. * lisp/emacs-lisp/byte-opt.el (byte-optimize-enable-variable-constprop) (byte-optimize-warn-eliminated-variable): New constants. (byte-optimize--lexvars, byte-optimize--vars-outside-condition) (byte-optimize--vars-outside-loop, byte-optimize--dynamic-vars): New dynamic variables. (byte-optimize--substitutable-p, byte-optimize-let-form): New functions. (byte-optimize-form-code-walker): Adapt clauses for variable constprop, and add clauses for 'setq' and 'defvar'. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-test-var) (bytecomp-test-get-var, bytecomp-test-identity) (byte-opt-testsuite-arith-data): Add test cases.
* Fix spurious "Lexical argument shadows the dynamic variable" due to inliningStefan Monnier2021-01-211-2/+4
| | | | | | | | | | | | | | | | | | | Before this patch doing: rm lisp/calendar/calendar.elc make lisp/calendar/cal-hebrew.elc would spew out lots of spurious such warnings about a `date` argument, pointing to code which has no `date` argument in sight. This was because that code had calls to inlinable functions (taking a `date` argument) defined in `calendar.el`, and while `date` is a normal lexical var at the site of those functions' definitions, it was declared as dynbound at the call site. * lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Don't impose our local context onto the inlined function. * test/lisp/emacs-lisp/bytecomp-tests.el: Add matching test.
* * lisp/emacs-lisp/byte-opt.el (byte-optimize-lapcode): Add 2 new optsStefan Monnier2021-01-201-0/+31
| | | | | | | | | | | | | | | | This introduces two new optimizations. They're designed for code like (while (let (...) (if ... (progn blabla t) (progn blabla nil))) ...) and they allow the elimination of the test internal to `while` since we can immediately know when we return `t` or `nil` what the result of the test will be. `cl-labels` tends to generate this kind of code when it applies the tail-call optimization.
* * lisp/emacs-lisp/byte-opt.el (byte-optimize-lapcode): Move some opts.Stefan Monnier2021-01-201-49/+50
| | | | | | | This moves two optimizations from the final pass to the main loop. Both may enable further optimizations (and the second can be applied repeatedly but "from the end", so the loop in the final pass only gets to apply it once).
* * lisp/emacs-lisp/byte-opt.el (byte-optimize-lapcode): Re-indentStefan Monnier2021-01-201-457/+457
|
* * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Use pcaseStefan Monnier2021-01-161-176/+175
|
* Update copyright year to 2021Paul Eggert2021-01-011-1/+1
| | | | Run "TZ=UTC0 admin/update-copyright".
* Add new predicates for sequence lengthsLars Ingebrigtsen2020-12-271-1/+3
| | | | | | | | | | | | | * doc/lispref/sequences.texi (Sequence Functions): Document them. * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Mark them as side-effect-free. * lisp/emacs-lisp/shortdoc.el (list): Mention them. * src/fns.c (Flength): Mention them in the doc string. (length_internal): New function. (Flength_less, Flength_greater, Flength_equal): New defuns. (syms_of_fns): Sym them.
* Don't quote lambdas in several placesStefan Kangas2020-11-121-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * lisp/allout-widgets.el (allout-widgets-adjusting-message) (allout-widgets-exposure-change-processor) (allout-widgets-count-buttons-in-region): * lisp/ansi-color.el (ansi-color-make-color-map): * lisp/case-table.el (describe-buffer-case-table): * lisp/emacs-lisp/byte-opt.el (byte-decompile-bytecode-1): * lisp/gnus/gnus-agent.el (gnus-agent-regenerate-group): * lisp/gnus/nnir.el (nnir-run-swish++, nnir-run-swish-e) (nnir-run-hyrex, nnir-run-namazu): * lisp/hippie-exp.el (make-hippie-expand-function) (try-complete-lisp-symbol, try-complete-lisp-symbol-partially) (try-expand-all-abbrevs): * lisp/international/mule-cmds.el (sort-coding-systems) (select-safe-coding-system, select-message-coding-system) (read-language-name, encoded-string-description): * lisp/international/quail.el (quail-keyseq-translate) (quail-get-translations, quail-build-decode-map) (quail-insert-decode-map): * lisp/jka-compr.el (jka-compr-uninstall): * lisp/locate.el (locate-in-alternate-database): * lisp/mail/mailabbrev.el (mail-resolve-all-aliases-1) (mail-abbrev-make-syntax-table): * lisp/mh-e/mh-seq.el (mh-read-folder-sequences): * lisp/net/eudcb-ldap.el (eudc-ldap-simple-query-internal): * lisp/progmodes/make-mode.el (makefile-query-targets) (makefile-prompt-for-gmake-funargs): * lisp/shadowfile.el (shadow-cancel, shadow-shadows-of): * lisp/sort.el (sort-pages, sort-fields, sort-regexp-fields): * lisp/subr.el (listify-key-sequence): * lisp/term/wyse50.el (terminal-init-wyse50): * lisp/textmodes/ispell.el (ispell-help) (ispell-begin-tex-skip-regexp): * lisp/textmodes/page-ext.el (pages-sort-region): * lisp/textmodes/refer.el (refer-find-entry-in-file): * lisp/url/url-expand.el (url-expand-file-name): Don't quote lambdas.
* Add missing side-effect-free and error-free propertiesMattias Engdegård2020-11-011-8/+10
| | | | | | | | | | | | Any function that is pure is also side-effect-free and some are also error-free. Right now these have to be declared separately. * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Add bool-vector-count-consecutive, bool-vector-count-population, bool-vector-subsetp, copysign, isnan, lax-plist-get, ldexp, memql, regexp-opt and string-to-syntax. (side-effect-and-error-free-fns): Add type-of. * lisp/subr.el (kbd, string-replace): Declare side-effect-free.
* * lisp/emacs-lisp/byte-opt.el (pure-fns): Fix typos.Mattias Engdegård2020-11-011-2/+2
|
* 'assoc' is not side-effect-free; constprop its pure subsetMattias Engdegård2020-10-311-6/+8
| | | | | | | | | | | Since a supplied test function can do anything, assoc is not side-effect-free (bug#44018). However, with only two arguments it is pure and should be optimised accordingly. * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Remove 'assoc'. (byte-optimize-assoc): Constant-propagate through 2-arg assoc calls. * test/lisp/emacs-lisp/bytecomp-tests.el (byte-opt-testsuite-arith-data): Add test cases.
* Handle single-argument `apply' consistently (bug#40968)Pip Cet2020-09-271-13/+16
| | | | | | | | | * src/eval.c (Fapply): Handle (apply nil) without crashing. Document single-argument form. * lisp/emacs-lisp/byte-opt.el (byte-optimize-apply): Don't attempt to optimize single-argument apply. * doc/lispref/functions.texi (Calling Functions): Document single-argument apply. Provide example (bug#40968).