1 ! Copyright (C) 2008, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: math math.intervals math.private math.partial-dispatch
4 namespaces sequences sets accessors assocs words kernel memoize fry
5 combinators combinators.short-circuit layouts alien.accessors
7 compiler.tree.combinators
8 compiler.tree.propagation.info
10 compiler.tree.def-use.simplified
11 compiler.tree.late-optimizations ;
12 FROM: namespaces => set ;
13 IN: compiler.tree.modular-arithmetic
15 ! This is a late-stage optimization.
16 ! See the comment in compiler.tree.late-optimizations.
18 ! Modular arithmetic optimization pass.
20 ! { integer integer } declare + >fixnum
22 ! [ >fixnum ] bi@ fixnum+fast
24 ! Words where the low-order bits of the output only depends on the
25 ! low-order bits of the input. If the output is only used for its
26 ! low-order bits, then the word can be converted into a form that is
28 { + - * bitand bitor bitxor } [
30 t "modular-arithmetic" set-word-prop
31 ] each-integer-derived-op
34 { bitand bitor bitxor bitnot >integer >bignum fixnum>bignum }
35 [ t "modular-arithmetic" set-word-prop ] each
37 ! Words that only use the low-order bits of their input. If the input
38 ! is a modular arithmetic word, then the input can be converted into
39 ! a form that is cheaper to compute.
41 >fixnum bignum>fixnum float>fixnum
42 set-alien-unsigned-1 set-alien-signed-1
43 set-alien-unsigned-2 set-alien-signed-2
46 { set-alien-unsigned-4 set-alien-signed-4 } append
48 [ t "low-order" set-word-prop ] each
50 ! Values which only have their low-order bits used. This set starts out
51 ! big and is gradually refined.
52 SYMBOL: modular-values
54 : modular-value? ( value -- ? )
55 modular-values get key? ;
57 : modular-value ( value -- )
58 modular-values get conjoin ;
60 ! Values which are known to be fixnums.
63 : fixnum-value? ( value -- ? )
64 fixnum-values get key? ;
66 : fixnum-value ( value -- )
67 fixnum-values get conjoin ;
69 GENERIC: compute-modular-candidates* ( node -- )
71 M: #push compute-modular-candidates*
72 [ out-d>> first ] [ literal>> ] bi
73 real? [ [ modular-value ] [ fixnum-value ] bi ] [ drop ] if ;
75 : small-shift? ( interval -- ? )
76 0 cell-bits tag-bits get - 1 - [a,b] interval-subset? ;
78 : modular-word? ( #call -- ? )
79 dup word>> { shift fixnum-shift bignum-shift } member-eq?
80 [ node-input-infos second interval>> small-shift? ]
81 [ word>> "modular-arithmetic" word-prop ]
84 : output-candidate ( #call -- )
85 out-d>> first [ modular-value ] [ fixnum-value ] bi ;
87 : low-order-word? ( #call -- ? )
88 word>> "low-order" word-prop ;
90 : input-candidiate ( #call -- )
91 in-d>> first modular-value ;
93 M: #call compute-modular-candidates*
95 { [ dup modular-word? ] [ output-candidate ] }
96 { [ dup low-order-word? ] [ input-candidiate ] }
100 M: node compute-modular-candidates*
103 : compute-modular-candidates ( nodes -- )
104 H{ } clone modular-values set
105 H{ } clone fixnum-values set
106 [ compute-modular-candidates* ] each-node ;
108 GENERIC: only-reads-low-order? ( node -- ? )
110 : output-modular? ( #call -- ? )
111 out-d>> first modular-values get key? ;
113 M: #call only-reads-low-order?
116 [ { [ modular-word? ] [ output-modular? ] } 1&& ]
119 M: node only-reads-low-order? drop f ;
123 : only-used-as-low-order? ( value -- ? )
124 actually-used-by [ node>> only-reads-low-order? ] all? ;
126 : (compute-modular-values) ( -- )
127 modular-values get keys [
128 dup only-used-as-low-order?
129 [ drop ] [ modular-values get delete-at changed? on ] if
132 : compute-modular-values ( -- )
133 [ changed? off (compute-modular-values) changed? get ] loop ;
135 GENERIC: optimize-modular-arithmetic* ( node -- nodes )
137 M: #push optimize-modular-arithmetic*
138 dup [ out-d>> first modular-value? ] [ literal>> real? ] bi and
139 [ [ >fixnum ] change-literal ] when ;
141 : redundant->fixnum? ( #call -- ? )
142 in-d>> first actually-defined-by
143 [ value>> { [ modular-value? ] [ fixnum-value? ] } 1&& ] all? ;
145 : optimize->fixnum ( #call -- nodes )
146 dup redundant->fixnum? [ drop f ] when ;
148 : should-be->fixnum? ( #call -- ? )
149 out-d>> first modular-value? ;
151 : optimize->integer ( #call -- nodes )
152 dup should-be->fixnum? [ \ >fixnum >>word ] when ;
154 MEMO: fixnum-coercion ( flags -- nodes )
155 ! flags indicate which input parameters are already known to be fixnums,
156 ! and don't need a coercion as a result.
157 [ [ ] [ >fixnum ] ? ] map '[ _ spread ] splice-quot ;
159 : modular-value-info ( #call -- alist )
160 [ in-d>> ] [ out-d>> ] bi append
161 fixnum <class-info> '[ _ ] { } map>assoc ;
163 : optimize-modular-op ( #call -- nodes )
164 dup out-d>> first modular-value? [
165 [ in-d>> ] [ word>> integer-op-input-classes ] [ ] tri
168 [ actually-defined-by [ value>> modular-value? ] all? ]
171 ] 2map fixnum-coercion
172 ] [ [ modular-variant ] change-word ] bi* suffix
175 : optimize-low-order-op ( #call -- nodes )
176 dup in-d>> first actually-defined-by [ value>> fixnum-value? ] all? [
177 [ ] [ in-d>> first ] [ info>> ] tri
178 [ drop fixnum <class-info> ] change-at
181 : like->fixnum? ( #call -- ? )
182 word>> { >fixnum bignum>fixnum float>fixnum } member-eq? ;
184 : like->integer? ( #call -- ? )
185 word>> { >integer >bignum fixnum>bignum } member-eq? ;
187 M: #call optimize-modular-arithmetic*
189 { [ dup like->fixnum? ] [ optimize->fixnum ] }
190 { [ dup like->integer? ] [ optimize->integer ] }
191 { [ dup modular-word? ] [ optimize-modular-op ] }
192 { [ dup low-order-word? ] [ optimize-low-order-op ] }
196 M: node optimize-modular-arithmetic* ;
198 : optimize-modular-arithmetic ( nodes -- nodes' )
199 dup compute-modular-candidates compute-modular-values
200 modular-values get assoc-empty? [
201 [ optimize-modular-arithmetic* ] map-nodes