From a4e89a91bff4de065b138a6fa4183cb4234d42c2 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 17 Apr 2023 16:46:10 -0700 Subject: compiler: rewrite t-regs through defining instruction. * stdlib/optimize.tl (subst-preserve): Rename list param to insn for clarity. (careful-subst-preserve): New function. This is like subst-preserve, but used only for instructions that have destination registers. It performs a rewrite such that those destination positions are avoided. (basic-blocks rename): When the instruction has src or dst as a target, don't just stop before that insn. Do the substitution in the source operands using careful-subst-preserve. --- stdlib/optimize.tl | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/stdlib/optimize.tl b/stdlib/optimize.tl index e78c9300..f3e2a6c6 100644 --- a/stdlib/optimize.tl +++ b/stdlib/optimize.tl @@ -354,10 +354,25 @@ ((nequal ninsn oinsn) (append (ldiff code tail) (list ninsn))) (t code)))) -(defun subst-preserve (x y bb li list) - (let ((sub (subst x y list))) +(defun subst-preserve (x y bb li insn) + (let ((sub (subst x y insn))) (cond - ((equal sub list) list) + ((equal sub insn) insn) + (t (set [bb.li-hash sub] li) sub)))) + +(defun careful-subst-preserve (x y bb li insn) + (let ((sub (match-case insn + ((@(or apply call) @def . @refs) + ^(,(car insn) ,def ,*(subst x y refs))) + ((@(or gapply gcall) @def @fn . @refs) + ^(,(car insn) ,def ,fn ,*(subst x y refs))) + ((mov @def @x) + ^(mov ,def ,y)) + ((catch @esreg @eareg . @refs) + ^(catch ,esreg ,eareg ,*(subst x y refs))) + (@else else)))) + (cond + ((equal sub insn) insn) (t (set [bb.li-hash sub] li) sub)))) (defmeth basic-blocks rename (bb insns dst src) @@ -369,12 +384,15 @@ (li [bb.li-hash insn])) (cond (close (add insn)) - ((or (and vreg end) - (mequal li.def0 dst src) - (mequal li.def1 dst src)) + ((and vreg end) (add insn) (pend insns) (set insns nil)) + ((or (mequal dst li.def0 li.def1) + (mequal src li.def0 li.def1)) + (add (careful-subst-preserve dst src bb li insn)) + (pend insns) + (set insns nil)) (t (add (subst-preserve dst src bb li insn))))))))) (defmeth basic-blocks peephole-block (bb bl) -- cgit v1.2.3