]> gitweb.factorcode.org Git - factor.git/commitdiff
compiler.cfg.dce: new global dead code elimination pass
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Tue, 26 May 2009 07:58:57 +0000 (02:58 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Tue, 26 May 2009 07:58:57 +0000 (02:58 -0500)
basis/compiler/cfg/dce/authors.txt [new file with mode: 0644]
basis/compiler/cfg/dce/dce.factor [new file with mode: 0644]

diff --git a/basis/compiler/cfg/dce/authors.txt b/basis/compiler/cfg/dce/authors.txt
new file mode 100644 (file)
index 0000000..d4f5d6b
--- /dev/null
@@ -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 (file)
index 0000000..ed9b48f
--- /dev/null
@@ -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