! Copyright (C) 2011 Alex Vondrak.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs compiler.cfg
-compiler.cfg.dataflow-analysis compiler.cfg.def-use hashtables
-kernel namespaces sequences ;
+compiler.cfg.dataflow-analysis compiler.cfg.def-use
+compiler.cfg.gvn.graph compiler.cfg.renaming.functor
+compiler.utilities hashtables kernel namespaces sequences ;
IN: compiler.cfg.gvn.avail
-! assoc mapping basic blocks to the set of value numbers that
-! are defined in the block
-SYMBOL: bbs>defns
-
-! : defined ( bb -- vns ) bbs>defns get at ;
-
: defined ( bb -- vregs )
- instructions>> [ defs-vregs ] map concat [ dup ] H{ } map>assoc ;
+ instructions>> [ defs-vregs ] map concat unique ;
+
+! This doesn't propagate across "kill blocks". Not sure if
+! that's right, though I may as well assume as much.
FORWARD-ANALYSIS: avail
-M: avail-analysis transfer-set drop defined assoc-union ;
+M: avail transfer-set drop defined assoc-union ;
+
+: available? ( vn -- ? ) basic-block get avail-in key? ;
+
+: best-vreg ( available-vregs -- vreg )
+ [ f ] [ infimum ] if-empty ;
+
+: >avail-vreg ( vreg -- vreg/f )
+ final-iteration? get [
+ congruence-class [ available? ] filter best-vreg
+ ] when ;
+
+: available-uses? ( insn -- ? )
+ uses-vregs [ >avail-vreg ] all? ;
+
+: with-available-uses? ( quot -- ? )
+ keep swap [ available-uses? ] [ drop f ] if ; inline
-: available? ( vn -- ? )
- basic-block get avail-ins get at key? ;
+: make-available ( vreg -- )
+ basic-block get avail-ins get [ dupd clone ?set-at ] assocs:change-at ;
-: make-available ( insn -- insn )
- dup dst>>
- basic-block get avail-ins get [ dupd ?set-at ] change-at ;
+RENAMING: >avail [ ] [ dup >avail-vreg swap or ] [ ]