Commit graph

327 commits

Author SHA1 Message Date
Mattias Engdegård
09ae3f9f65 Remove obsolete variable
* lisp/emacs-lisp/byte-opt.el
(byte-optimize--vars-outside-condition): Remove.
(byte-optimize-form-code-walker): Remove bindings.
2021-09-11 17:17:33 +02:00
Mattias Engdegård
020a408eda Propagate aliased lexical variables in byte compiler
Replace uses of a variable aliasing another variable with that aliased
variable, to allow for variable removal when possible.  This also
enables opportunities for other optimisations.  Example:

 (let ((y x)) (f y)) => (f x)

The optimisation is only performed if both aliased and aliasing
variables are lexically bound.  Shadowing bindings are α-renamed when
necessary for correctness.  Example:

   (let* ((b a) (a EXPR)) (f a b))
=> (let* ((a{new} EXPR)) (f a{new} a))

* lisp/emacs-lisp/byte-opt.el (byte-optimize--aliased-vars): New.
(byte-optimize-form-code-walker): Cancel aliasing upon mutation.
(byte-optimize--rename-var-body, byte-optimize--rename-var): New.
(byte-optimize-let-form): Add the optimisation.
* test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases):
Add relevant test cases.
2021-09-11 17:17:33 +02:00
Mattias Engdegård
c4724add00 Normalise nested progn forms in byte-code optimiser
* lisp/emacs-lisp/byte-opt.el (byte-optimize-body): Flatten body.
This simplifies the source tree and reduces the number of different
cases that other optimisations need to take into account.
2021-09-06 16:47:13 +02:00
Mattias Engdegård
bba48d6ee5 More robust optimisation of ignore
Treat `ignore` as any other function during source-level optimisation,
to avoid having its warning-suppression effects cancelled by repeated
passes.  Instead, define a custom code generation function.

* lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker):
Don't treat `ignore' specially here.
(side-effect-free-fns): Don't mark `ignore` as side-effect-free
or error-free (although it is), since that would allow the optimiser
to elide calls.
* lisp/emacs-lisp/bytecomp.el (ignore, byte-compile-ignore):
Define and register a code-gen function.
2021-09-06 16:47:13 +02:00
Mattias Engdegård
fab1e220db Optimise member and assoc (etc) with constant empty list
* lisp/emacs-lisp/byte-opt.el
(byte-optimize-assq): New.
(byte-optimize-member, byte-optimize-assoc, byte-optimize-memq):
When the list argument is constant nil, the result is always nil.
* test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases):
Add test cases.
2021-09-06 16:47:13 +02:00
Mattias Engdegård
2a17925aab Cease attempts to const-propagate through setq
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.
2021-08-05 15:33:05 +02:00
Lars Ingebrigtsen
88577aed3a file-name-concat is not error free
* lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Declare
file-name-concat as side-effect free.
2021-08-03 19:41:57 +02:00
Lars Ingebrigtsen
301ce625cb Declare file-name-concat as side-effect free
* lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Declare
file-name-concat as side-effect (and error) free.
2021-08-03 19:08:43 +02:00
Mattias Engdegård
0809c9f6ef Declare match-beginning and match-end as side-effect-free
* lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Add functions.
2021-08-03 15:29:58 +02:00
Mattias Engdegård
f472dd8ea5 Simplify lexical let-optimisations
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
2021-07-30 14:37:38 +02:00
Mattias Engdegård
52a55e11de Optimise let and let* whose body is constant or the last variable
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.
2021-07-30 09:54:30 +02:00
Mattias Engdegård
ab9c06449d Move warnings about bad let-bindings from source optimiser to cconv
* 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).
2021-07-30 09:54:30 +02:00
Mattias Engdegård
dc9e2a1749 Optimise prog1 better
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.
2021-07-30 09:54:29 +02:00
Mattias Engdegård
9a63338114 Elide lexical variables in for-effect context in source optimiser
* 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.
2021-07-30 09:54:29 +02:00
Mattias Engdegård
566e29f78c Single source optimiser entry point
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.
2021-07-30 09:54:29 +02:00
Mattias Engdegård
109ca1bd00 Warn about arity errors in inlining calls (bug#12299)
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.
2021-07-23 15:19:01 +02:00
Mattias Engdegård
070c80ee06 Fix mistake in quote optimiser
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).
2021-07-21 11:17:18 +02:00
Mattias Engdegård
f9d7440814 ; * lisp/emacs-lisp/byte-opt.el (byte-optimize-eq): Fix last change. 2021-07-20 19:32:11 +02:00
Mattias Engdegård
46d7d44894 Strength-reduce (eq X nil) to (not X)
* lisp/emacs-lisp/byte-opt.el (byte-optimize-eq): New optimisation,
which results in better test and branch code generation where it
applies.
2021-07-20 19:21:00 +02:00
Mattias Engdegård
6b41d7da95 Constant-propagate (function SYMBOL)
* lisp/emacs-lisp/byte-opt.el (byte-optimize--substitutable-p):
Consider #'SYMBOL a constant for compile-time propagation purposes.
2021-06-03 21:28:10 +02:00
Mattias Engdegård
a517b77ffe Optimise (cons X nil) to (list X)
* lisp/emacs-lisp/byte-opt.el (byte-optimize-cons): New function.
2021-06-03 21:28:10 +02:00
Stefan Monnier
24c9657746 * lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Silence warnings
(byte-optimize--lexvars): Move before first use instead of using `dlet`
on that first use.
2021-05-27 17:31:57 -04:00
Mattias Engdegård
40d2970f43 Don't propagate lexical variables into inlined functions
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.
2021-05-27 14:16:17 +02:00
Stefan Monnier
354ecaf12b * lisp/emacs-lisp/byte-opt.el: Make the build more reproducible
(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.
2021-05-25 13:38:05 -04:00
Philipp Stephani
01bd4d1a82 Optimize calls to 'eql', 'memql' and similar for fixnums.
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.
2021-05-16 14:45:18 +02:00
Andrea Corallo
289000eee7 Merge branch 'feature/native-comp' into into trunk 2021-04-25 20:06:22 +02:00
Mattias Engdegård
a7cc19e5ff Don't erroneously declare mark as error-free
* 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.
2021-04-21 22:52:17 +02:00
Andrea Corallo
b064ddd3f6 Merge remote-tracking branch 'savannah/master' into native-comp 2021-04-13 12:06:23 +02:00
Mattias Engdegård
59342f689e Fix condition-case optimiser bug
* 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.
2021-04-09 19:20:55 +02:00
Eli Zaretskii
6810635bdd * lisp/emacs-lisp/byte-opt.el: Fix native re-compilation (bug#47161). 2021-03-15 19:24:20 +02:00
Mattias Engdegård
7add330903 Mark string predicates side-effect-free
* 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.
2021-03-10 14:08:41 +01:00
Pip Cet
77ec25122c Don't ignore lexically-bound variables in a defvar (bug#46912)
* lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Walk
the value form of a defvar.
2021-03-05 09:14:59 +00:00
Mattias Engdegård
f8ab343eb9 Declare more string predicates as pure
* 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.
2021-02-28 20:18:43 +01:00
Lars Ingebrigtsen
825aed11d2 Add the `always' function
* 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.
2021-02-20 13:44:19 +01:00
Mattias Engdegård
9518926220 Simplify expression in byte-code decompiler
* lisp/emacs-lisp/byte-opt.el (byte-decompile-bytecode-1):
Replace roundabout expression with what it essentially does.
2021-02-12 20:52:05 +01:00
Mattias Engdegård
5a11e9185c byte-opt.el: More concise expression
* lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker):
Refactor `setq` clause.
2021-02-12 20:52:05 +01:00
Mattias Engdegård
ea29908c18 Avoid traversing dead if branches in bytecode optimiser
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.
2021-02-12 20:52:05 +01:00
Mattias Engdegård
f3ae26cb2a Fix local defvar scoping error (bug#46387)
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.
2021-02-10 14:47:40 +01:00
Stefan Monnier
6fd8548b16 * lisp/emacs-lisp/byte-opt.el (byte-optimize--pcase): New macro
(byte-optimize-form-code-walker): Use it.
2021-02-09 12:10:07 -05:00
Stefan Monnier
04fb1664a8 * lisp/emacs-lisp/macroexp.el: Break cycle with bytecomp/byte-opt
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.
2021-02-09 12:02:25 -05:00
Mattias Engdegård
7e48430a43 ; * lisp/emacs-lisp/byte-opt.el: improved comment 2021-02-07 12:24:40 +01:00
Mattias Engdegård
765ffeb545 ; Improved commentary in the variable constprop mechanism
* 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.
2021-02-07 10:35:36 +01:00
Mattias Engdegård
83983b6b7a Constprop of lexical variables
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.
2021-02-06 20:22:24 +01:00
Stefan Monnier
b41b4add7b Fix spurious "Lexical argument shadows the dynamic variable" due to inlining
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.
2021-01-21 13:15:05 -05:00
Stefan Monnier
66439d31ad * lisp/emacs-lisp/byte-opt.el (byte-optimize-lapcode): Add 2 new opts
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.
2021-01-20 14:13:15 -05:00
Stefan Monnier
4dfebf25c7 * lisp/emacs-lisp/byte-opt.el (byte-optimize-lapcode): Move some opts.
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).
2021-01-20 14:13:15 -05:00
Stefan Monnier
09bfb12edc * lisp/emacs-lisp/byte-opt.el (byte-optimize-lapcode): Re-indent 2021-01-20 14:13:15 -05:00
Stefan Monnier
25e1b73294 * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Use pcase 2021-01-16 14:21:57 -05:00
Paul Eggert
ba05d005e5 Update copyright year to 2021
Run "TZ=UTC0 admin/update-copyright".
2021-01-01 01:13:56 -08:00
Lars Ingebrigtsen
0f790464d5 Add new predicates for sequence lengths
* 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.
2020-12-27 09:00:23 +01:00