From 7d3e7d02f89b0335e0661b00866a4dfc5547841c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 10 Mar 2021 08:09:08 -0800 Subject: compiler: eliminate unused closures. In this patch, we optimize away closures that are not used. Unused closures that have been hoisted to the top level are not affected. We look for close instructions which produce a dead treg, and rewrite these to jmp instructions to the same label. When this happend, we set a flag for a dead code elimination pass to be done again, to actually remove the now unreachable closure code. * share/txr/stdlib/optimize.tl (struct basic-blocks): New slot, reelim, indicating that dead code elimination is to be done again after peephole since the control flow graph has changed. (basic-blocks peephole-block): New pattern for eliminating a dead register, targeting the close instruction. (basic-blocks peephole): After peephole, check whether the reelim flag has been set and do elim-dead-code again. --- share/txr/stdlib/optimize.tl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/share/txr/stdlib/optimize.tl b/share/txr/stdlib/optimize.tl index 8f166a6f..109dc9b3 100644 --- a/share/txr/stdlib/optimize.tl +++ b/share/txr/stdlib/optimize.tl @@ -57,6 +57,7 @@ list rescan recalc + reelim (:static start (gensym "start-")) (:static jump-ops '(jmp if ifq ifql close swtch ret abscsr uwprot catch block jend)) @@ -359,6 +360,12 @@ (pushnew bl bb.rescan) (set bb.recalc t) (cdr insns)) + (@(require ((close (t @n) @nil @nil @jlabel . @nil) . @nil) + (dead-treg (car insns) n)) + (pushnew bl bb.rescan) + (set bb.recalc t + bb.reelim t) + ^((jmp ,jlabel) ,*(cdr insns))) (@(require ((@(or gcall gapply) (t @n) @idx . @nil) . @nil) (dead-treg (car insns) n) [%const-foldable% [bb.symvec idx]]) @@ -436,7 +443,9 @@ bb.(calc-liveness rescan) (set bb.recalc nil)) (each ((bl rescan)) - (set bl.insns bb.(peephole-block bl bl.insns))))) + (set bl.insns bb.(peephole-block bl bl.insns)))) + (when bb.reelim + bb.(elim-dead-code))) (defmeth basic-blocks thread-jumps (bb) (each ((bl bb.list)) -- cgit v1.2.3