]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/transforms/transforms.factor
Move remaining sequence operations from generalizations to sequences.generalizations...
[factor.git] / basis / compiler / tree / propagation / transforms / transforms.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types kernel sequences words fry generic
4 generic.single accessors classes.tuple classes classes.algebra
5 definitions stack-checker.dependencies quotations
6 classes.tuple.private math math.partial-dispatch math.private
7 math.intervals sets.private math.floats.private
8 math.integers.private layouts math.order vectors hashtables
9 combinators effects generalizations sequences.generalizations
10 assocs sets combinators.short-circuit sequences.private locals
11 growable stack-checker namespaces compiler.tree.propagation.info
12 ;
13 FROM: math => float ;
14 FROM: sets => set ;
15 IN: compiler.tree.propagation.transforms
16
17 \ equal? [
18     ! If first input has a known type and second input is an
19     ! object, we convert this to [ swap equal? ].
20     in-d>> first2 value-info class>> object class= [
21         value-info class>> \ equal? method-for-class
22         [ swap equal? ] f ?
23     ] [ drop f ] if
24 ] "custom-inlining" set-word-prop
25
26 : rem-custom-inlining ( #call -- quot/f )
27     second value-info literal>> dup integer?
28     [ power-of-2? [ 1 - bitand ] f ? ] [ drop f ] if ;
29
30 {
31     mod-integer-integer
32     mod-integer-fixnum
33     mod-fixnum-integer
34     fixnum-mod
35 } [
36     [
37         in-d>> dup first value-info interval>> [0,inf] interval-subset?
38         [ rem-custom-inlining ] [ drop f ] if
39     ] "custom-inlining" set-word-prop
40 ] each
41
42 \ rem [
43     in-d>> rem-custom-inlining
44 ] "custom-inlining" set-word-prop
45
46 : positive-fixnum? ( obj -- ? )
47     { [ fixnum? ] [ 0 >= ] } 1&& ;
48
49 : simplify-bitand? ( value1 value2 -- ? )
50     [ literal>> positive-fixnum? ]
51     [ class>> fixnum swap class<= ]
52     bi* and ;
53
54 : all-ones? ( n -- ? ) dup 1 + bitand zero? ; inline
55
56 : redundant-bitand? ( value1 value2 -- ? )
57     [ interval>> ] [ literal>> ] bi* {
58         [ nip integer? ]
59         [ nip all-ones? ]
60         [ 0 swap [a,b] interval-subset? ]
61     } 2&& ;
62
63 : zero-bitand? ( value1 value2 -- ? )
64     [ interval>> ] [ literal>> ] bi* {
65         [ nip integer? ]
66         [ nip bitnot all-ones? ]
67         [ 0 swap bitnot [a,b] interval-subset? ]
68     } 2&& ;
69
70 {
71     bitand-integer-integer
72     bitand-integer-fixnum
73     bitand-fixnum-integer
74     bitand
75 } [
76     [
77         in-d>> first2 [ value-info ] bi@ {
78             {
79                 [ 2dup zero-bitand? ]
80                 [ 2drop [ 2drop 0 ] ]
81             }
82             {
83                 [ 2dup swap zero-bitand? ]
84                 [ 2drop [ 2drop 0 ] ]
85             }
86             {
87                 [ 2dup redundant-bitand? ]
88                 [ 2drop [ drop ] ]
89             }
90             {
91                 [ 2dup swap redundant-bitand? ]
92                 [ 2drop [ nip ] ]
93             }
94             {
95                 [ 2dup simplify-bitand? ]
96                 [ 2drop [ >fixnum fixnum-bitand ] ]
97             }
98             {
99                 [ 2dup swap simplify-bitand? ]
100                 [ 2drop [ [ >fixnum ] dip fixnum-bitand ] ]
101             }
102             [ 2drop f ]
103         } cond
104     ] "custom-inlining" set-word-prop
105 ] each
106
107 ! Speeds up 2^
108 : 2^? ( #call -- ? )
109     in-d>> first value-info literal>> 1 eq? ;
110
111 \ shift [
112     2^? [
113         cell-bits tag-bits get - 1 -
114         '[
115             >fixnum dup 0 < [ 2drop 0 ] [
116                 dup _ < [ fixnum-shift ] [
117                     fixnum-shift
118                 ] if
119             ] if
120         ]
121     ] [ f ] if
122 ] "custom-inlining" set-word-prop
123
124 { /i fixnum/i fixnum/i-fast bignum/i } [
125     [
126         in-d>> first2 [ value-info ] bi@ {
127             [ drop class>> integer class<= ]
128             [ drop interval>> 0 [a,a] interval>= ]
129             [ nip literal>> integer? ]
130             [ nip literal>> power-of-2? ]
131         } 2&& [ [ log2 neg shift ] ] [ f ] if
132     ] "custom-inlining" set-word-prop
133 ] each
134
135 ! Generate more efficient code for common idiom
136 \ clone [
137     in-d>> first value-info literal>> {
138         { V{ } [ [ drop { } 0 vector boa ] ] }
139         { H{ } [ [ drop 0 <hashtable> ] ] }
140         { HS{ } [ [ drop f fast-set ] ] }
141         [ drop f ]
142     } case
143 ] "custom-inlining" set-word-prop
144
145 ERROR: bad-partial-eval quot word ;
146
147 : check-effect ( quot word -- )
148     2dup [ infer ] [ stack-effect ] bi* effect<=
149     [ 2drop ] [ bad-partial-eval ] if ;
150
151 :: define-partial-eval ( word quot n -- )
152     word [
153         in-d>> n tail*
154         [ value-info ] map
155         dup [ literal?>> ] all? [
156             [ literal>> ] map
157             n firstn
158             quot call dup [
159                 [ n ndrop ] prepose
160                 dup word check-effect
161             ] when
162         ] [ drop f ] if
163     ] "custom-inlining" set-word-prop ;
164
165 : inline-new ( class -- quot/f )
166     dup tuple-class? [
167         dup tuple-layout
168         [ depends-on-tuple-layout ]
169         [ drop all-slots [ initial>> literalize ] [ ] map-as ]
170         [ nip ]
171         2tri
172         '[ @ _ <tuple-boa> ]
173     ] [ drop f ] if ;
174
175 \ new [ inline-new ] 1 define-partial-eval
176
177 \ instance? [
178     dup class?
179     [ "predicate" word-prop ] [ drop f ] if
180 ] 1 define-partial-eval
181
182 ! Shuffling
183 : nths-quot ( indices -- quot )
184     [ [ '[ _ swap nth ] ] map ] [ length ] bi
185     '[ _ cleave _ narray ] ;
186
187 \ shuffle [
188     shuffle-mapping nths-quot
189 ] 1 define-partial-eval
190
191 ! Index search
192 \ index [
193     dup sequence? [
194         dup length 4 >= [
195             dup length iota zip >hashtable '[ _ at ]
196         ] [ drop f ] if
197     ] [ drop f ] if
198 ] 1 define-partial-eval
199
200 : member-eq-quot ( seq -- newquot )
201     [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
202     [ drop f ] suffix [ cond ] curry ;
203
204 \ member-eq? [
205     dup sequence? [ member-eq-quot ] [ drop f ] if
206 ] 1 define-partial-eval
207
208 ! Membership testing
209 : member-quot ( seq -- newquot )
210     dup length 4 <= [
211         [ drop f ] swap
212         [ literalize [ t ] ] { } map>assoc linear-case-quot
213     ] [
214         tester
215     ] if ;
216
217 \ member? [
218     dup sequence? [ member-quot ] [ drop f ] if
219 ] 1 define-partial-eval
220
221 ! Fast at for integer maps
222 CONSTANT: lookup-table-at-max 256
223
224 : lookup-table-at? ( assoc -- ? )
225     #! Can we use a fast byte array test here?
226     {
227         [ assoc-size 4 > ]
228         [ values [ ] all? ]
229         [ keys [ integer? ] all? ]
230         [ keys [ 0 lookup-table-at-max between? ] all? ]
231     } 1&& ;
232
233 : lookup-table-seq ( assoc -- table )
234     [ keys supremum 1 + iota ] keep '[ _ at ] { } map-as ;
235
236 : lookup-table-quot ( seq -- newquot )
237     lookup-table-seq
238     '[
239         _ over integer? [
240             2dup bounds-check? [
241                 nth-unsafe dup >boolean
242             ] [ 2drop f f ] if
243         ] [ 2drop f f ] if
244     ] ;
245
246 : fast-lookup-table-at? ( assoc -- ? )
247     values {
248         [ [ integer? ] all? ]
249         [ [ 0 254 between? ] all? ]
250     } 1&& ;
251
252 : fast-lookup-table-seq ( assoc -- table )
253     lookup-table-seq [ 255 or ] B{ } map-as ;
254
255 : fast-lookup-table-quot ( seq -- newquot )
256     fast-lookup-table-seq
257     '[
258         _ over integer? [
259             2dup bounds-check? [
260                 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
261             ] [ 2drop f f ] if
262         ] [ 2drop f f ] if
263     ] ;
264
265 : at-quot ( assoc -- quot )
266     dup assoc? [
267         dup lookup-table-at? [
268             dup fast-lookup-table-at? [
269                 fast-lookup-table-quot
270             ] [
271                 lookup-table-quot
272             ] if
273         ] [ drop f ] if
274     ] [ drop f ] if ;
275
276 \ at* [ at-quot ] 1 define-partial-eval
277
278 : diff-quot ( seq -- quot: ( seq' -- seq'' ) )
279     tester '[ [ [ @ not ] filter ] keep set-like ] ;
280
281 M\ set diff [ diff-quot ] 1 define-partial-eval
282
283 : intersect-quot ( seq -- quot: ( seq' -- seq'' ) )
284     tester '[ [ _ filter ] keep set-like ] ;
285
286 M\ set intersect [ intersect-quot ] 1 define-partial-eval
287
288 : fixnum-bits ( -- n )
289     cell-bits tag-bits get - ;
290
291 : bit-quot ( #call -- quot/f )
292     in-d>> second value-info interval>> 0 fixnum-bits [a,b] interval-subset?
293     [ [ >fixnum ] dip fixnum-bit? ] f ? ;
294
295 \ bit? [ bit-quot ] "custom-inlining" set-word-prop
296
297 ! Speeds up sum-file, sort and reverse-complement benchmarks by
298 ! compiling decoder-readln better
299 \ push [
300     in-d>> second value-info class>> growable class<=
301     [ \ push def>> ] [ f ] if
302 ] "custom-inlining" set-word-prop
303
304 ! Speeds up fasta benchmark
305 \ >fixnum [
306     in-d>> first value-info class>> fixnum \ f class-or class<=
307     [ [ dup [ \ >fixnum no-method ] unless ] ] [ f ] if
308 ] "custom-inlining" set-word-prop
309
310 ! We want to constant-fold calls to heap-size, and recompile those
311 ! calls when a C type is redefined
312 \ heap-size [
313     dup word? [
314         [ depends-on-definition ] [ heap-size '[ _ ] ] bi
315     ] [ drop f ] if
316 ] 1 define-partial-eval
317
318 ! Eliminates a few redundant checks here and there
319 \ both-fixnums? [
320     in-d>> first2 [ value-info class>> ] bi@ {
321         { [ 2dup [ fixnum classes-intersect? not ] either? ] [ [ 2drop f ] ] }
322         { [ 2dup [ fixnum class<= ] both? ] [ [ 2drop t ] ] }
323         { [ dup fixnum class<= ] [ [ drop fixnum? ] ] }
324         { [ over fixnum class<= ] [ [ nip fixnum? ] ] }
325         [ f ]
326     } cond 2nip
327 ] "custom-inlining" set-word-prop