1 ! Copyright (C) 2008, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.accessors assocs combinators
4 combinators.short-circuit compiler.tree
5 compiler.tree.combinators compiler.tree.def-use.simplified
6 compiler.tree.late-optimizations compiler.tree.propagation.info
7 fry kernel layouts math math.intervals math.partial-dispatch
8 math.private memoize namespaces sequences sets words ;
9 IN: compiler.tree.modular-arithmetic
11 ! This is a late-stage optimization.
12 ! See the comment in compiler.tree.late-optimizations.
14 ! Modular arithmetic optimization pass.
16 ! { integer integer } declare + >fixnum
18 ! [ >fixnum ] bi@ fixnum+fast
20 ! Words where the low-order bits of the output only depends on the
21 ! low-order bits of the input. If the output is only used for its
22 ! low-order bits, then the word can be converted into a form that is
24 { + - * bitand bitor bitxor } [
26 t "modular-arithmetic" set-word-prop
27 ] each-integer-derived-op
30 { bitand bitor bitxor bitnot >integer >bignum fixnum>bignum }
31 [ t "modular-arithmetic" set-word-prop ] each
33 ! Words that only use the low-order bits of their input. If the input
34 ! is a modular arithmetic word, then the input can be converted into
35 ! a form that is cheaper to compute.
37 >fixnum bignum>fixnum integer>fixnum
39 set-alien-unsigned-1 set-alien-signed-1
40 set-alien-unsigned-2 set-alien-signed-2
43 { set-alien-unsigned-4 set-alien-signed-4 } append
45 [ t "low-order" set-word-prop ] each
47 ! Values which only have their low-order bits used. This set starts out
48 ! big and is gradually refined.
49 SYMBOL: modular-values
51 : modular-value? ( value -- ? )
52 modular-values get in? ;
54 : modular-value ( value -- )
55 modular-values get adjoin ;
57 ! Values which are known to be fixnums.
60 : fixnum-value? ( value -- ? )
61 fixnum-values get in? ;
63 : fixnum-value ( value -- )
64 fixnum-values get adjoin ;
66 GENERIC: compute-modular-candidates* ( node -- )
68 M: #push compute-modular-candidates*
69 [ out-d>> first ] [ literal>> ] bi
70 real? [ [ modular-value ] [ fixnum-value ] bi ] [ drop ] if ;
72 : small-shift? ( interval -- ? )
73 0 cell-bits tag-bits get - 1 - [a,b] interval-subset? ;
75 : modular-word? ( #call -- ? )
76 dup word>> { shift fixnum-shift bignum-shift } member-eq?
77 [ node-input-infos second interval>> small-shift? ]
78 [ word>> "modular-arithmetic" word-prop ]
81 : output-candidate ( #call -- )
82 out-d>> first [ modular-value ] [ fixnum-value ] bi ;
84 : low-order-word? ( #call -- ? )
85 word>> "low-order" word-prop ;
87 : input-candidiate ( #call -- )
88 in-d>> first modular-value ;
90 M: #call compute-modular-candidates*
92 { [ dup modular-word? ] [ output-candidate ] }
93 { [ dup low-order-word? ] [ input-candidiate ] }
97 M: node compute-modular-candidates*
100 : compute-modular-candidates ( nodes -- )
101 HS{ } clone modular-values namespaces:set
102 HS{ } clone fixnum-values namespaces:set
103 [ compute-modular-candidates* ] each-node ;
105 GENERIC: only-reads-low-order? ( node -- ? )
107 : output-modular? ( #call -- ? )
108 out-d>> first modular-value? ;
110 M: #call only-reads-low-order?
113 [ { [ modular-word? ] [ output-modular? ] } 1&& ]
116 M: node only-reads-low-order? drop f ;
120 : only-used-as-low-order? ( value -- ? )
121 actually-used-by [ node>> only-reads-low-order? ] all? ;
123 : (compute-modular-values) ( -- )
124 modular-values get members [
125 dup only-used-as-low-order?
126 [ drop ] [ modular-values get delete changed? on ] if
129 : compute-modular-values ( -- )
130 [ changed? off (compute-modular-values) changed? get ] loop ;
132 GENERIC: optimize-modular-arithmetic* ( node -- nodes )
134 M: #push optimize-modular-arithmetic*
135 dup [ out-d>> first modular-value? ] [ literal>> real? ] bi and
136 [ [ >fixnum ] change-literal ] when ;
138 : redundant->fixnum? ( #call -- ? )
139 in-d>> first actually-defined-by
140 [ value>> { [ modular-value? ] [ fixnum-value? ] } 1&& ] all? ;
142 : optimize->fixnum ( #call -- nodes )
143 dup redundant->fixnum? [ drop f ] when ;
145 : should-be->fixnum? ( #call -- ? )
146 out-d>> first modular-value? ;
148 : optimize->integer ( #call -- nodes )
149 dup should-be->fixnum? [ \ >fixnum >>word ] when ;
151 MEMO: fixnum-coercion ( flags -- nodes )
152 ! flags indicate which input parameters are already known to be fixnums,
153 ! and don't need a coercion as a result.
154 [ [ ] [ >fixnum ] ? ] map shallow-spread>quot
155 '[ _ call ] splice-quot ;
157 : modular-value-info ( #call -- alist )
158 [ in-d>> ] [ out-d>> ] bi append
159 fixnum <class-info> '[ _ ] { } map>assoc ;
161 : optimize-modular-op ( #call -- nodes )
162 dup out-d>> first modular-value? [
163 [ in-d>> ] [ word>> integer-op-input-classes ] [ ] tri
166 [ actually-defined-by [ value>> modular-value? ] all? ]
169 ] 2map fixnum-coercion
170 ] [ [ modular-variant ] change-word ] bi* suffix
173 : optimize-low-order-op ( #call -- nodes )
174 dup in-d>> first actually-defined-by [ value>> fixnum-value? ] all? [
175 [ ] [ in-d>> first ] [ info>> ] tri
176 [ drop fixnum <class-info> ] change-at
179 : like->fixnum? ( #call -- ? )
182 bignum>fixnum bignum>fixnum-strict
183 integer>fixnum integer>fixnum-strict
186 : like->integer? ( #call -- ? )
187 word>> { >integer >bignum fixnum>bignum } member-eq? ;
189 M: #call optimize-modular-arithmetic*
191 { [ dup like->fixnum? ] [ optimize->fixnum ] }
192 { [ dup like->integer? ] [ optimize->integer ] }
193 { [ dup modular-word? ] [ optimize-modular-op ] }
194 { [ dup low-order-word? ] [ optimize-low-order-op ] }
198 M: node optimize-modular-arithmetic* ;
200 : optimize-modular-arithmetic ( nodes -- nodes' )
201 dup compute-modular-candidates compute-modular-values
202 modular-values get null? [
203 [ optimize-modular-arithmetic* ] map-nodes