]> 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 dd799276995c61a38d50e5a6a0336d3abbd680f8..a67f6a5fa75d5a20c0709f42f16083794b641936 100644 (file)
-! Copyright (C) 2009 Slava Pestov.
+! Copyright (C) 2009, 2011 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors assocs fry kernel locals math math.order
-sequences namespaces sets
-compiler.cfg.rpo
-compiler.cfg.def-use
-compiler.cfg.utilities
-compiler.cfg.dominance
-compiler.cfg.instructions
-compiler.cfg.liveness.ssa
-compiler.cfg.critical-edges
-compiler.cfg.ssa.destruction.state
-compiler.cfg.ssa.destruction.forest
-compiler.cfg.ssa.destruction.copies
-compiler.cfg.ssa.destruction.renaming
-compiler.cfg.ssa.destruction.live-ranges
-compiler.cfg.ssa.destruction.process-blocks ;
+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
+cpu.architecture kernel locals make namespaces sequences sets ;
+FROM: namespaces => set ;
 IN: compiler.cfg.ssa.destruction
 
-! Based on "Fast Copy Coalescing and Live-Range Identification"
-! http://www.cs.ucsd.edu/classes/sp02/cse231/kenpldi.pdf
-
-! Dominance, liveness and def-use need to be computed
-
-: process-blocks ( cfg -- )
-    [ [ process-block ] if-has-phis ] each-basic-block ;
-
-SYMBOL: seen
-
-:: visit-renaming ( dst assoc src bb -- )
-    src seen get key? [
-        src dst bb add-waiting
-        src assoc delete-at
-    ] [ src seen get conjoin ] if ;
-
-:: break-interferences ( -- )
-    H{ } clone seen set
-    renaming-sets get [| dst assoc |
-        assoc [| src bb |
-            dst assoc src bb visit-renaming
-        ] assoc-each
-    ] assoc-each ;
-
-: remove-phis-from-block ( bb -- )
-    instructions>> [ ##phi? not ] filter-here ;
-
-: remove-phis ( cfg -- )
-    [ [ remove-phis-from-block ] if-has-phis ] each-basic-block ;
-
-: destruct-ssa ( cfg -- cfg' )
-    dup cfg-has-phis? [
-        dup split-critical-edges
-        compute-ssa-live-sets
-        init-coalescing
-        dup compute-def-use
-        dup compute-dominance
-        dup compute-live-ranges
-        dup process-blocks
-        break-interferences
-        dup perform-renaming
-        insert-copies
-        dup remove-phis
-    ] when ;
\ No newline at end of file
+SYMBOL: class-element-map
+
+: class-elements ( vreg -- elts ) class-element-map get at ;
+
+<PRIVATE
+
+! 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 unique leader-map set ]
+    [
+        [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map
+        class-element-map set
+    ] bi
+    V{ } clone copies set ;
+
+: 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* ;
+
+: coalesce-vregs ( merged leader1 leader2 -- )
+    2dup swap leader-map get set-at coalesce-elements ;
+
+GENERIC: prepare-insn ( insn -- )
+
+: maybe-eliminate-copy-later ( dst src -- )
+    2array copies get push ;
+
+M: insn prepare-insn drop ;
+
+M: alien-call-insn prepare-insn drop ;
+
+M: vreg-insn prepare-insn
+    [ temp-vregs [ leader-map get conjoin ] each ]
+    [
+        [ defs-vregs ] [ uses-vregs ] bi
+        2dup [ empty? not ] both? [
+            [ first ] bi@
+            2dup [ rep-of reg-class-of ] bi@ eq?
+            [ maybe-eliminate-copy-later ] [ 2drop ] if
+        ] [ 2drop ] if
+    ] bi ;
+
+M: ##copy prepare-insn
+    [ 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 leaders must-eliminate-copy ;
+
+M: ##phi prepare-insn
+    [ dst>> ] [ inputs>> values ] bi
+    [ leaders must-eliminate-copy ] with each ;
+
+: prepare-coalescing ( cfg -- )
+    init-coalescing
+    [ [ prepare-insn ] each ] simple-analysis ;
+
+:: maybe-eliminate-copy ( vreg1 vreg2 -- )
+    ! Eliminate a copy if possible.
+    vreg1 vreg2 = [
+        vreg1 vreg2 vregs-interfere?
+        [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
+    ] unless ;
+
+: 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>> ] [ src>> ] bi leaders = not ; inline
+
+M: ##copy cleanup-insn
+    dup useful-copy? [ , ] [ drop ] if ;
+
+M: ##parallel-copy cleanup-insn
+    values>> [ leaders ] assoc-map [ first2 = ] reject
+    parallel-copy-rep % ;
+
+M: ##tagged>integer cleanup-insn
+    dup useful-copy? [ , ] [ drop ] if ;
+
+M: ##phi cleanup-insn drop ;
+
+M: insn cleanup-insn , ;
+
+: cleanup-cfg ( cfg -- )
+    [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
+
+PRIVATE>
+
+: 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 ;