1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators fry kernel layouts locals
4 math make namespaces sequences cpu.architecture
10 compiler.cfg.comparisons
11 compiler.cfg.instructions
12 compiler.cfg.predecessors
14 compiler.cfg.liveness.ssa
15 compiler.cfg.stacks.uninitialized ;
16 IN: compiler.cfg.gc-checks
20 ! Garbage collection check insertion. This pass runs after
21 ! representation selection, since it needs to know which vregs
22 ! can contain tagged pointers.
24 : insert-gc-check? ( bb -- ? )
25 instructions>> [ ##allocation? ] any? ;
27 : blocks-with-gc ( cfg -- bbs )
28 post-order [ insert-gc-check? ] filter ;
30 ! A GC check for bb consists of two new basic blocks, gc-check
39 ! Any ##phi instructions at the start of bb are transplanted
40 ! into the gc-check block.
42 : <gc-check> ( phis size -- bb )
43 [ <basic-block> ] 2dip
47 cc<= int-rep next-vreg-rep int-rep next-vreg-rep
48 ##check-nursery-branch
50 ] V{ } make >>instructions ;
52 : wipe-locs ( uninitialized-locs -- )
56 [ '[ [ _ ] dip ##replace ] each ] bi
59 : <gc-call> ( uninitialized-locs gc-roots -- bb )
60 [ <basic-block> ] 2dip
61 [ [ wipe-locs ] [ ##call-gc ] bi* ##branch ] V{ } make
62 >>instructions t >>unlikely? ;
64 :: insert-guard ( body check bb -- )
65 bb predecessors>> check predecessors<<
66 V{ bb body } check successors<<
68 V{ check } body predecessors<<
69 V{ bb } body successors<<
71 V{ check body } bb predecessors<<
73 check predecessors>> [ bb check update-successors ] each ;
75 : (insert-gc-check) ( uninitialized-locs gc-roots phis size bb -- )
76 [ [ <gc-call> ] 2dip <gc-check> ] dip insert-guard ;
78 GENERIC: allocation-size* ( insn -- n )
80 M: ##allot allocation-size* size>> ;
82 M: ##box-alien allocation-size* drop 5 cells ;
84 M: ##box-displaced-alien allocation-size* drop 5 cells ;
86 : allocation-size ( bb -- n )
88 [ ##allocation? ] filter
89 [ allocation-size* data-alignment get align ] map-sum ;
91 : gc-live-in ( bb -- vregs )
92 [ live-in keys ] [ instructions>> [ ##phi? ] filter [ dst>> ] map ] bi
95 : live-tagged ( bb -- vregs )
96 gc-live-in [ rep-of tagged-rep? ] filter ;
98 : remove-phis ( bb -- phis )
99 [ [ ##phi? ] partition ] change-instructions drop ;
101 : insert-gc-check ( bb -- )
103 [ uninitialized-locs ]
113 : insert-gc-checks ( cfg -- cfg' )
117 dup compute-ssa-live-sets
118 dup compute-uninitialized-sets
120 [ insert-gc-check ] each