From 8b022f926c005a33077f9087848052caf7d56b32 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 26 May 2009 02:58:57 -0500 Subject: [PATCH] compiler.cfg.dce: new global dead code elimination pass --- basis/compiler/cfg/dce/authors.txt | 1 + basis/compiler/cfg/dce/dce.factor | 44 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 basis/compiler/cfg/dce/authors.txt create mode 100644 basis/compiler/cfg/dce/dce.factor diff --git a/basis/compiler/cfg/dce/authors.txt b/basis/compiler/cfg/dce/authors.txt new file mode 100644 index 0000000000..d4f5d6b3ae --- /dev/null +++ b/basis/compiler/cfg/dce/authors.txt @@ -0,0 +1 @@ +Slava Pestov \ No newline at end of file diff --git a/basis/compiler/cfg/dce/dce.factor b/basis/compiler/cfg/dce/dce.factor new file mode 100644 index 0000000000..ed9b48f7c6 --- /dev/null +++ b/basis/compiler/cfg/dce/dce.factor @@ -0,0 +1,44 @@ +! Copyright (C) 2008, 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors assocs sets kernel namespaces sequences +compiler.cfg.instructions compiler.cfg.def-use ; +IN: compiler.cfg.dce + +! Maps vregs to sequences of vregs +SYMBOL: liveness-graph + +! vregs which participate in side effects and thus are always live +SYMBOL: live-vregs + +: init-dead-code ( -- ) + H{ } clone liveness-graph set + H{ } clone live-vregs set ; + +GENERIC: compute-liveness ( insn -- ) + +M: ##flushable compute-liveness + [ uses-vregs ] [ dst>> ] bi liveness-graph get set-at ; + +: record-live ( vregs -- ) + [ + dup live-vregs get key? [ drop ] [ + [ live-vregs get conjoin ] + [ liveness-graph get at record-live ] + bi + ] if + ] each ; + +M: insn compute-liveness uses-vregs record-live ; + +GENERIC: live-insn? ( insn -- ? ) + +M: ##flushable live-insn? dst>> live-vregs get key? ; + +M: insn live-insn? drop t ; + +: eliminate-dead-code ( rpo -- rpo ) + init-dead-code + [ [ instructions>> [ compute-liveness ] each ] each ] + [ [ [ [ live-insn? ] filter ] change-instructions drop ] each ] + [ ] + tri ; \ No newline at end of file