Re: [Sbcl-commits] master: Perform local common subexpression elimination for some memory loads

Christophe Rhodes via Sbcl-devel <[email protected]> Tue, 04 Aug 2026 09:34:57 +0100
Newsgroups gmane.lisp.steel-bank.devel
Message-ID <[email protected]>
Interesting!  Lots of questions, some of which I'll just fire here with
the caveat that I haven't thought any of them through so they might not
make sense.

Rather than scanning backwards for matching subexpressions, could we
maintain a table of expressions as we compile that we could look up?

Could/should this be in some way linked to the loop analysis (so that
maybe we can prioritize eliminating expressions by nestedness of loop
position?)

snuglas via Sbcl-commits <[email protected]> writes:

> +;; Reuse of SYMBOL-VALUE of a special var would be nice,
> +;; but SYMBOL-VALUE it is not represented as a call in IR1
> +(define-load-time-global *elidable-memory-loads*
> +    '(car cdr %instance-ref %raw-instance-ref/word %raw-instance-ref/signed-word
> +      sap-ref-16 signed-sap-ref-16 sb-sys::%sap-ref-16-indexed sb-sys::%signed-sap-ref-16-indexed
> +      sap-ref-32 signed-sap-ref-32 sb-sys::%sap-ref-32-indexed sb-sys::%signed-sap-ref-32-indexed
> +      sap-ref-64 signed-sap-ref-64 sb-sys::%sap-ref-64-indexed sb-sys::%signed-sap-ref-64-indexed
> +      sb-alien:deref
> +      sb-alien:alien-sap))

Would it make sense for this to be an ir1-attribute?  And what are the
tradeoffs involved in it including simple-vector / (simple-array * 1)
reffers in there?

>                         (not (node-lvar node)))
>                    (return-from ir1-optimize-combination (flush-node node)))
>                   ((fold-call-derived-to-constant node)
> +                  (return-from ir1-optimize-combination))
> +                 ((and (combination-is node *elidable-memory-loads*)
> +                       (try-reuse-expr-value node))
>                    (return-from ir1-optimize-combination)))
>             (when (and (ir1-attributep (fun-info-attributes info) commutative)
>                        (= (length args) 2)

Should this optimization be gated on some kind of compilation-speed
and/or space vs speed test?

Christophe