1 ! Copyright (C) 2008, 2011 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 hash-sets arrays hashtables.private ;
14 FROM: sets => set members ;
15 IN: compiler.tree.propagation.transforms
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
24 ] "custom-inlining" set-word-prop
26 : rem-custom-inlining ( inputs -- quot/f )
27 dup first value-info class>> integer class<= [
28 second value-info literal>> dup integer?
29 [ power-of-2? [ 1 - bitand ] f ? ] [ drop f ] if
39 in-d>> dup first value-info interval>> [0,inf] interval-subset?
40 [ rem-custom-inlining ] [ drop f ] if
41 ] "custom-inlining" set-word-prop
45 in-d>> rem-custom-inlining
46 ] "custom-inlining" set-word-prop
48 : non-negative-fixnum? ( obj -- ? )
49 { [ fixnum? ] [ 0 >= ] } 1&& ;
51 : simplify-bitand? ( value1 value2 -- ? )
52 [ literal>> non-negative-fixnum? ]
53 [ class>> fixnum swap class<= ]
56 : all-ones? ( n -- ? ) dup 1 + bitand zero? ; inline
58 : redundant-bitand? ( value1 value2 -- ? )
59 [ interval>> ] [ literal>> ] bi* {
62 [ 0 swap [a,b] interval-subset? ]
65 : zero-bitand? ( value1 value2 -- ? )
66 [ interval>> ] [ literal>> ] bi* {
68 [ nip bitnot all-ones? ]
69 [ 0 swap bitnot [a,b] interval-subset? ]
73 bitand-integer-integer
79 in-d>> first2 [ value-info ] bi@ {
85 [ 2dup swap zero-bitand? ]
89 [ 2dup redundant-bitand? ]
93 [ 2dup swap redundant-bitand? ]
97 [ 2dup simplify-bitand? ]
98 [ 2drop [ integer>fixnum fixnum-bitand ] ]
101 [ 2dup swap simplify-bitand? ]
102 [ 2drop [ [ integer>fixnum ] dip fixnum-bitand ] ]
106 ] "custom-inlining" set-word-prop
111 in-d>> first value-info literal>> 1 eq? ;
113 : shift-2^ ( -- quot )
114 cell-bits tag-bits get - 1 -
116 integer>fixnum-strict dup 0 < [ 2drop 0 ] [
117 dup _ < [ fixnum-shift ] [
125 in-d>> second value-info literal>> -1 eq? ;
127 : shift-2/ ( -- quot )
130 { [ over fixnum? ] [ fixnum-shift ] }
131 { [ over bignum? ] [ bignum-shift ] }
132 [ drop \ shift no-method ]
138 { [ dup 2^? ] [ drop shift-2^ ] }
139 { [ dup 2/? ] [ drop shift-2/ ] }
142 ] "custom-inlining" set-word-prop
144 { /i fixnum/i fixnum/i-fast bignum/i } [
146 in-d>> first2 [ value-info ] bi@ {
147 [ drop class>> integer class<= ]
148 [ drop interval>> 0 [a,a] interval>= ]
149 [ nip literal>> integer? ]
150 [ nip literal>> power-of-2? ]
151 } 2&& [ [ log2 neg shift ] ] [ f ] if
152 ] "custom-inlining" set-word-prop
155 ! Generate more efficient code for common idiom
157 in-d>> first value-info literal>> {
158 { V{ } [ [ drop { } 0 vector boa ] ] }
159 { H{ } [ [ drop 0 <hashtable> ] ] }
160 { HS{ } [ [ drop 0 <hash-set> ] ] }
163 ] "custom-inlining" set-word-prop
165 ERROR: bad-partial-eval quot word ;
167 : check-effect ( quot word -- )
168 2dup [ infer ] [ stack-effect ] bi* effect<=
169 [ 2drop ] [ bad-partial-eval ] if ;
171 :: define-partial-eval ( word quot n -- )
175 dup [ literal?>> ] all? [
180 dup word check-effect
183 ] "custom-inlining" set-word-prop ;
185 : inline-new ( class -- quot/f )
188 [ add-depends-on-tuple-layout ]
189 [ drop all-slots [ initial>> literalize ] [ ] map-as ]
195 \ new [ inline-new ] 1 define-partial-eval
201 ! union{ and intersection{ have useless expansions, and recurse infinitely
202 dup { [ length 2 >= ] [ second \ instance? = ] } 1&& [
206 ] 1 define-partial-eval
209 : nths-quot ( indices -- quot )
210 [ [ '[ _ swap nth ] ] map ] [ length ] bi
211 '[ _ cleave _ narray ] ;
214 shuffle-mapping nths-quot
215 ] 1 define-partial-eval
221 dup length iota zip >hashtable '[ _ at ]
224 ] 1 define-partial-eval
226 : member-eq-quot ( seq -- newquot )
227 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
228 [ drop f ] suffix [ cond ] curry ;
231 dup sequence? [ member-eq-quot ] [ drop f ] if
232 ] 1 define-partial-eval
235 : member-quot ( seq -- newquot )
238 [ literalize [ t ] ] { } map>assoc linear-case-quot
244 dup sequence? [ member-quot ] [ drop f ] if
245 ] 1 define-partial-eval
247 ! Fast at for integer maps
248 CONSTANT: lookup-table-at-max 256
250 : lookup-table-at? ( assoc -- ? )
251 #! Can we use a fast byte array test here?
255 [ keys [ integer? ] all? ]
256 [ keys [ 0 lookup-table-at-max between? ] all? ]
259 : lookup-table-seq ( assoc -- table )
260 [ keys supremum 1 + iota ] keep '[ _ at ] { } map-as ;
262 : lookup-table-quot ( seq -- newquot )
267 nth-unsafe dup >boolean
272 : fast-lookup-table-at? ( assoc -- ? )
274 [ [ integer? ] all? ]
275 [ [ 0 254 between? ] all? ]
278 : fast-lookup-table-seq ( assoc -- table )
279 lookup-table-seq [ 255 or ] B{ } map-as ;
281 : fast-lookup-table-quot ( seq -- newquot )
282 fast-lookup-table-seq
286 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
291 : at-quot ( assoc -- quot )
293 dup lookup-table-at? [
294 dup fast-lookup-table-at? [
295 fast-lookup-table-quot
302 \ at* [ at-quot ] 1 define-partial-eval
304 : diff-quot ( seq -- quot: ( seq' -- seq'' ) )
305 [ tester ] keep '[ members [ @ not ] filter _ set-like ] ;
307 M\ set diff [ diff-quot ] 1 define-partial-eval
309 : intersect-quot ( seq -- quot: ( seq' -- seq'' ) )
310 [ tester ] keep '[ members _ filter _ set-like ] ;
312 M\ set intersect [ intersect-quot ] 1 define-partial-eval
314 : intersects?-quot ( seq -- quot: ( seq' -- seq'' ) )
315 tester '[ members _ any? ] ;
317 M\ set intersects? [ intersects?-quot ] 1 define-partial-eval
319 : bit-quot ( #call -- quot/f )
320 in-d>> second value-info interval>> 0 fixnum-bits [a,b] interval-subset?
321 [ [ integer>fixnum ] dip fixnum-bit? ] f ? ;
323 \ bit? [ bit-quot ] "custom-inlining" set-word-prop
325 ! Speeds up sum-file, sort and reverse-complement benchmarks by
326 ! compiling decoder-readln better
328 in-d>> second value-info class>> growable class<=
329 [ \ push def>> ] [ f ] if
330 ] "custom-inlining" set-word-prop
332 : custom-inline-fixnum ( #call method -- y )
333 [ in-d>> first value-info class>> fixnum \ f class-or class<= ] dip
334 '[ [ dup [ _ no-method ] unless ] ] [ f ] if ;
336 ! Speeds up fasta benchmark
337 { >fixnum integer>fixnum integer>fixnum-strict } [
338 dup '[ _ custom-inline-fixnum ] "custom-inlining" set-word-prop
341 ! We want to constant-fold calls to heap-size, and recompile those
342 ! calls when a C type is redefined
344 [ add-depends-on-c-type ] [ heap-size '[ _ ] ] bi
345 ] 1 define-partial-eval
347 ! Eliminates a few redundant checks here and there
349 in-d>> first2 [ value-info class>> ] bi@ {
350 { [ 2dup [ fixnum classes-intersect? not ] either? ] [ [ 2drop f ] ] }
351 { [ 2dup [ fixnum class<= ] both? ] [ [ 2drop t ] ] }
352 { [ dup fixnum class<= ] [ [ drop fixnum? ] ] }
353 { [ over fixnum class<= ] [ [ nip fixnum? ] ] }
356 ] "custom-inlining" set-word-prop