]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/compiler/cfg/ssa/destruction/destruction.factor
use reject instead of [ ... not ] filter.
[factor.git] / basis / compiler / cfg / ssa / destruction / destruction.factor
index ff174bc7130b861fc0a46e2d67233c24897dcd24..a67f6a5fa75d5a20c0709f42f16083794b641936 100644 (file)
@@ -1,42 +1,16 @@
-! Copyright (C) 2009, 2010 Slava Pestov.
+! Copyright (C) 2009, 2011 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays assocs fry kernel namespaces
-sequences sequences.deep
-sets vectors
-cpu.architecture
-compiler.cfg.rpo
-compiler.cfg.def-use
-compiler.cfg.registers
-compiler.cfg.dominance
-compiler.cfg.instructions
-compiler.cfg.liveness.ssa
-compiler.cfg.ssa.cssa
+USING: accessors arrays assocs combinators compiler.cfg.def-use
+compiler.cfg.dominance compiler.cfg.instructions
+compiler.cfg.liveness compiler.cfg.parallel-copy
+compiler.cfg.registers compiler.cfg.rpo compiler.cfg.ssa.cssa
+compiler.cfg.ssa.destruction.leaders
 compiler.cfg.ssa.interference
-compiler.cfg.ssa.interference.live-ranges
-compiler.cfg.utilities
-compiler.utilities ;
+compiler.cfg.ssa.interference.live-ranges compiler.cfg.utilities
+cpu.architecture kernel locals make namespaces sequences sets ;
 FROM: namespaces => set ;
 IN: compiler.cfg.ssa.destruction
 
-! Because of the design of the register allocator, this pass
-! has three peculiar properties.
-!
-! 1) Instead of renaming vreg usages in the CFG, a map from
-! vregs to canonical representatives is computed. This allows
-! the register allocator to use the original SSA names to get
-! reaching definitions.
-! 2) Useless ##copy instructions, and all ##phi instructions,
-! are eliminated, so the register allocator does not have to
-! remove any redundant operations.
-! 3) A side effect of running this pass is that SSA liveness
-! information is computed, so the register allocator does not
-! need to compute it again.
-
-SYMBOL: leader-map
-
-: leader ( vreg -- vreg' ) leader-map get compress-path ;
-
-! Maps leaders to equivalence class elements.
 SYMBOL: class-element-map
 
 : class-elements ( vreg -- elts ) class-element-map get at ;
@@ -46,98 +20,126 @@ SYMBOL: class-element-map
 ! Sequence of vreg pairs
 SYMBOL: copies
 
+: value-of ( vreg -- value )
+    dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
+
 : init-coalescing ( -- )
-    defs get keys
-    [ [ dup ] H{ } map>assoc leader-map set ]
-    [ [ dup 1vector ] H{ } map>assoc class-element-map set ] bi
+    defs get
+    [ keys unique leader-map set ]
+    [
+        [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map
+        class-element-map set
+    ] bi
     V{ } clone copies set ;
 
-: classes-interfere? ( vreg1 vreg2 -- ? )
-    [ leader ] bi@ 2dup eq? [ 2drop f ] [
-        [ class-elements flatten ] bi@ sets-interfere?
-    ] if ;
+: coalesce-elements ( merged vreg1 vreg2 -- )
+    ! delete leader1's class, and set leader2's class to merged.
+    class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
 
-: update-leaders ( vreg1 vreg2 -- )
-    swap leader-map get set-at ;
+: coalesce-vregs ( merged leader1 leader2 -- )
+    2dup swap leader-map get set-at coalesce-elements ;
 
-: merge-classes ( vreg1 vreg2 -- )
-    [ [ class-elements ] bi@ push ]
-    [ drop class-element-map get delete-at ] 2bi ;
+GENERIC: prepare-insn ( insn -- )
 
-: eliminate-copy ( vreg1 vreg2 -- )
-    [ leader ] bi@
-    2dup eq? [ 2drop ] [
-        [ update-leaders ]
-        [ merge-classes ]
-        2bi
-    ] if ;
+: maybe-eliminate-copy-later ( dst src -- )
+    2array copies get push ;
 
-GENERIC: prepare-insn ( insn -- )
+M: insn prepare-insn drop ;
 
-: try-to-coalesce ( dst src -- ) 2array copies get push ;
+M: alien-call-insn prepare-insn drop ;
 
-M: insn prepare-insn
+M: vreg-insn prepare-insn
     [ temp-vregs [ leader-map get conjoin ] each ]
     [
-        [ defs-vreg ] [ uses-vregs ] bi
-        2dup empty? not and [
-            first
+        [ defs-vregs ] [ uses-vregs ] bi
+        2dup [ empty? not ] both? [
+            [ first ] bi@
             2dup [ rep-of reg-class-of ] bi@ eq?
-            [ try-to-coalesce ] [ 2drop ] if
+            [ maybe-eliminate-copy-later ] [ 2drop ] if
         ] [ 2drop ] if
     ] bi ;
 
 M: ##copy prepare-insn
-    [ dst>> ] [ src>> ] bi try-to-coalesce ;
+    [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
+
+M: ##parallel-copy prepare-insn
+    values>> [ first2 maybe-eliminate-copy-later ] each ;
+
+: leaders ( vreg1 vreg2 -- vreg1' vreg2' )
+    [ leader ] bi@ ;
+
+: vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
+    [ class-elements ] bi@ sets-interfere? ;
+
+ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
+
+:: must-eliminate-copy ( vreg1 vreg2 -- )
+    ! Eliminate a copy.
+    vreg1 vreg2 = [
+        vreg1 vreg2 vregs-interfere?
+        [ vreg1 vreg2 vregs-shouldn't-interfere ]
+        [ vreg1 vreg2 coalesce-vregs ]
+        if
+    ] unless ;
 
 M: ##tagged>integer prepare-insn
-    [ dst>> ] [ src>> ] bi eliminate-copy ;
+    [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
 
 M: ##phi prepare-insn
     [ dst>> ] [ inputs>> values ] bi
-    [ eliminate-copy ] with each ;
-
-: prepare-block ( bb -- )
-    instructions>> [ prepare-insn ] each ;
+    [ leaders must-eliminate-copy ] with each ;
 
 : prepare-coalescing ( cfg -- )
     init-coalescing
-    [ prepare-block ] each-basic-block ;
+    [ [ prepare-insn ] each ] simple-analysis ;
 
-: process-copies ( -- )
-    copies get [
-        2dup classes-interfere?
-        [ 2drop ] [ eliminate-copy ] if
-    ] assoc-each ;
+:: maybe-eliminate-copy ( vreg1 vreg2 -- )
+    ! Eliminate a copy if possible.
+    vreg1 vreg2 = [
+        vreg1 vreg2 vregs-interfere?
+        [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
+    ] unless ;
 
-GENERIC: useful-insn? ( insn -- ? )
+: process-copies ( copies -- )
+    [ leaders maybe-eliminate-copy ] assoc-each ;
+
+: perform-coalescing ( cfg -- )
+    prepare-coalescing copies get process-copies ;
+
+GENERIC: cleanup-insn ( insn -- )
 
 : useful-copy? ( insn -- ? )
-    [ dst>> leader ] [ src>> leader ] bi eq? not ; inline
+    [ dst>> ] [ src>> ] bi leaders = not ; inline
 
-M: ##copy useful-insn? useful-copy? ;
+M: ##copy cleanup-insn
+    dup useful-copy? [ , ] [ drop ] if ;
 
-M: ##tagged>integer useful-insn? useful-copy? ;
+M: ##parallel-copy cleanup-insn
+    values>> [ leaders ] assoc-map [ first2 = ] reject
+    parallel-copy-rep % ;
 
-M: ##phi useful-insn? drop f ;
+M: ##tagged>integer cleanup-insn
+    dup useful-copy? [ , ] [ drop ] if ;
 
-M: insn useful-insn? drop t ;
+M: ##phi cleanup-insn drop ;
 
-: cleanup-block ( bb -- )
-    instructions>> [ useful-insn? ] filter! drop ;
+M: insn cleanup-insn , ;
 
 : cleanup-cfg ( cfg -- )
-    [ cleanup-block ] each-basic-block ;
+    [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
 
 PRIVATE>
 
-: destruct-ssa ( cfg -- cfg' )
-    needs-dominance
-
-    dup construct-cssa
-    dup compute-defs
-    dup compute-ssa-live-sets
-    dup compute-live-ranges
-    dup prepare-coalescing
-    process-copies
-    dup cleanup-cfg ;
+: destruct-ssa ( cfg -- )
+    f leader-map set
+    {
+        needs-dominance
+        construct-cssa
+        compute-defs
+        compute-insns
+        compute-live-sets
+        compute-live-ranges
+        perform-coalescing
+        cleanup-cfg
+        compute-live-sets
+    } apply-passes ;