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 IN: compiler.tree.modular-arithmetic
14 ! This is a late-stage optimization.
15 ! See the comment in compiler.tree.late-optimizations.
17 ! Modular arithmetic optimization pass.
19 ! { integer integer } declare + >fixnum
21 ! [ >fixnum ] bi@ fixnum+fast
23 ! Words where the low-order bits of the output only depends on the
24 ! low-order bits of the input. If the output is only used for its
25 ! low-order bits, then the word can be converted into a form that is
27 { + - * bitand bitor bitxor } [
29 t "modular-arithmetic" set-word-prop
30 ] each-integer-derived-op
33 { bitand bitor bitxor bitnot >integer >bignum fixnum>bignum }
34 [ t "modular-arithmetic" set-word-prop ] each
36 ! Words that only use the low-order bits of their input. If the input
37 ! is a modular arithmetic word, then the input can be converted into
38 ! a form that is cheaper to compute.
40 >fixnum bignum>fixnum float>fixnum
41 set-alien-unsigned-1 set-alien-signed-1
42 set-alien-unsigned-2 set-alien-signed-2
45 { set-alien-unsigned-4 set-alien-signed-4 } append
47 [ t "low-order" set-word-prop ] each
49 ! Values which only have their low-order bits used. This set starts out
50 ! big and is gradually refined.
51 SYMBOL: modular-values
53 : modular-value? ( value -- ? )
54 modular-values get key? ;
56 : modular-value ( value -- )
57 modular-values get conjoin ;
59 ! Values which are known to be fixnums.
62 : fixnum-value? ( value -- ? )
63 fixnum-values get key? ;
65 : fixnum-value ( value -- )
66 fixnum-values get conjoin ;
68 GENERIC: compute-modular-candidates* ( node -- )
70 M: #push compute-modular-candidates*
71 [ out-d>> first ] [ literal>> ] bi
72 real? [ [ modular-value ] [ fixnum-value ] bi ] [ drop ] if ;
74 : small-shift? ( interval -- ? )
75 0 cell-bits tag-bits get - 1 - [a,b] interval-subset? ;
77 : modular-word? ( #call -- ? )
78 dup word>> { shift fixnum-shift bignum-shift } member-eq?
79 [ node-input-infos second interval>> small-shift? ]
80 [ word>> "modular-arithmetic" word-prop ]
83 : output-candidate ( #call -- )
84 out-d>> first [ modular-value ] [ fixnum-value ] bi ;
86 : low-order-word? ( #call -- ? )
87 word>> "low-order" word-prop ;
89 : input-candidiate ( #call -- )
90 in-d>> first modular-value ;
92 M: #call compute-modular-candidates*
94 { [ dup modular-word? ] [ output-candidate ] }
95 { [ dup low-order-word? ] [ input-candidiate ] }
99 M: node compute-modular-candidates*
102 : compute-modular-candidates ( nodes -- )
103 H{ } clone modular-values set
104 H{ } clone fixnum-values set
105 [ compute-modular-candidates* ] each-node ;
107 GENERIC: only-reads-low-order? ( node -- ? )
109 : output-modular? ( #call -- ? )
110 out-d>> first modular-values get key? ;
112 M: #call only-reads-low-order?
115 [ { [ modular-word? ] [ output-modular? ] } 1&& ]
118 M: node only-reads-low-order? drop f ;
122 : only-used-as-low-order? ( value -- ? )
123 actually-used-by [ node>> only-reads-low-order? ] all? ;
125 : (compute-modular-values) ( -- )
126 modular-values get keys [
127 dup only-used-as-low-order?
128 [ drop ] [ modular-values get delete-at changed? on ] if
131 : compute-modular-values ( -- )
132 [ changed? off (compute-modular-values) changed? get ] loop ;
134 GENERIC: optimize-modular-arithmetic* ( node -- nodes )
136 M: #push optimize-modular-arithmetic*
137 dup [ out-d>> first modular-value? ] [ literal>> real? ] bi and
138 [ [ >fixnum ] change-literal ] when ;
140 : redundant->fixnum? ( #call -- ? )
141 in-d>> first actually-defined-by
142 [ value>> { [ modular-value? ] [ fixnum-value? ] } 1&& ] all? ;
144 : optimize->fixnum ( #call -- nodes )
145 dup redundant->fixnum? [ drop f ] when ;
147 : should-be->fixnum? ( #call -- ? )
148 out-d>> first modular-value? ;
150 : optimize->integer ( #call -- nodes )
151 dup should-be->fixnum? [ \ >fixnum >>word ] when ;
153 MEMO: fixnum-coercion ( flags -- nodes )
154 ! flags indicate which input parameters are already known to be fixnums,
155 ! and don't need a coercion as a result.
156 [ [ ] [ >fixnum ] ? ] map '[ _ spread ] splice-quot ;
158 : modular-value-info ( #call -- alist )
159 [ in-d>> ] [ out-d>> ] bi append
160 fixnum <class-info> '[ _ ] { } map>assoc ;
162 : optimize-modular-op ( #call -- nodes )
163 dup out-d>> first modular-value? [
164 [ in-d>> ] [ word>> integer-op-input-classes ] [ ] tri
167 [ actually-defined-by [ value>> modular-value? ] all? ]
170 ] 2map fixnum-coercion
171 ] [ [ modular-variant ] change-word ] bi* suffix
174 : optimize-low-order-op ( #call -- nodes )
175 dup in-d>> first actually-defined-by [ value>> fixnum-value? ] all? [
176 [ ] [ in-d>> first ] [ info>> ] tri
177 [ drop fixnum <class-info> ] change-at
180 : like->fixnum? ( #call -- ? )
181 word>> { >fixnum bignum>fixnum float>fixnum } member-eq? ;
183 : like->integer? ( #call -- ? )
184 word>> { >integer >bignum fixnum>bignum } member-eq? ;
186 M: #call optimize-modular-arithmetic*
188 { [ dup like->fixnum? ] [ optimize->fixnum ] }
189 { [ dup like->integer? ] [ optimize->integer ] }
190 { [ dup modular-word? ] [ optimize-modular-op ] }
191 { [ dup low-order-word? ] [ optimize-low-order-op ] }
195 M: node optimize-modular-arithmetic* ;
197 : optimize-modular-arithmetic ( nodes -- nodes' )
198 dup compute-modular-candidates compute-modular-values
199 modular-values get assoc-empty? [
200 [ optimize-modular-arithmetic* ] map-nodes