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
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 ( #call -- quot/f )
27 second value-info literal>> dup integer?
28 [ power-of-2? [ 1 - bitand ] f ? ] [ drop f ] if ;
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
43 in-d>> rem-custom-inlining
44 ] "custom-inlining" set-word-prop
46 : positive-fixnum? ( obj -- ? )
47 { [ fixnum? ] [ 0 >= ] } 1&& ;
49 : simplify-bitand? ( value1 value2 -- ? )
50 [ literal>> positive-fixnum? ]
51 [ class>> fixnum swap class<= ]
54 : all-ones? ( n -- ? ) dup 1 + bitand zero? ; inline
56 : redundant-bitand? ( value1 value2 -- ? )
57 [ interval>> ] [ literal>> ] bi* {
60 [ 0 swap [a,b] interval-subset? ]
63 : zero-bitand? ( value1 value2 -- ? )
64 [ interval>> ] [ literal>> ] bi* {
66 [ nip bitnot all-ones? ]
67 [ 0 swap bitnot [a,b] interval-subset? ]
71 bitand-integer-integer
77 in-d>> first2 [ value-info ] bi@ {
83 [ 2dup swap zero-bitand? ]
87 [ 2dup redundant-bitand? ]
91 [ 2dup swap redundant-bitand? ]
95 [ 2dup simplify-bitand? ]
96 [ 2drop [ >fixnum fixnum-bitand ] ]
99 [ 2dup swap simplify-bitand? ]
100 [ 2drop [ [ >fixnum ] dip fixnum-bitand ] ]
104 ] "custom-inlining" set-word-prop
109 in-d>> first value-info literal>> 1 eq? ;
113 cell-bits tag-bits get - 1 -
115 >fixnum dup 0 < [ 2drop 0 ] [
116 dup _ < [ fixnum-shift ] [
122 ] "custom-inlining" set-word-prop
124 { /i fixnum/i fixnum/i-fast bignum/i } [
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
135 ! Generate more efficient code for common idiom
137 in-d>> first value-info literal>> {
138 { V{ } [ [ drop { } 0 vector boa ] ] }
139 { H{ } [ [ drop 0 <hashtable> ] ] }
140 { HS{ } [ [ drop f fast-set ] ] }
143 ] "custom-inlining" set-word-prop
145 ERROR: bad-partial-eval quot word ;
147 : check-effect ( quot word -- )
148 2dup [ infer ] [ stack-effect ] bi* effect<=
149 [ 2drop ] [ bad-partial-eval ] if ;
151 :: define-partial-eval ( word quot n -- )
155 dup [ literal?>> ] all? [
160 dup word check-effect
163 ] "custom-inlining" set-word-prop ;
165 : inline-new ( class -- quot/f )
168 [ depends-on-tuple-layout ]
169 [ drop all-slots [ initial>> literalize ] [ ] map-as ]
175 \ new [ inline-new ] 1 define-partial-eval
179 [ "predicate" word-prop ] [ drop f ] if
180 ] 1 define-partial-eval
183 : nths-quot ( indices -- quot )
184 [ [ '[ _ swap nth ] ] map ] [ length ] bi
185 '[ _ cleave _ narray ] ;
188 shuffle-mapping nths-quot
189 ] 1 define-partial-eval
195 dup length iota zip >hashtable '[ _ at ]
198 ] 1 define-partial-eval
200 : member-eq-quot ( seq -- newquot )
201 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
202 [ drop f ] suffix [ cond ] curry ;
205 dup sequence? [ member-eq-quot ] [ drop f ] if
206 ] 1 define-partial-eval
209 : member-quot ( seq -- newquot )
212 [ literalize [ t ] ] { } map>assoc linear-case-quot
218 dup sequence? [ member-quot ] [ drop f ] if
219 ] 1 define-partial-eval
221 ! Fast at for integer maps
222 CONSTANT: lookup-table-at-max 256
224 : lookup-table-at? ( assoc -- ? )
225 #! Can we use a fast byte array test here?
229 [ keys [ integer? ] all? ]
230 [ keys [ 0 lookup-table-at-max between? ] all? ]
233 : lookup-table-seq ( assoc -- table )
234 [ keys supremum 1 + iota ] keep '[ _ at ] { } map-as ;
236 : lookup-table-quot ( seq -- newquot )
241 nth-unsafe dup >boolean
246 : fast-lookup-table-at? ( assoc -- ? )
248 [ [ integer? ] all? ]
249 [ [ 0 254 between? ] all? ]
252 : fast-lookup-table-seq ( assoc -- table )
253 lookup-table-seq [ 255 or ] B{ } map-as ;
255 : fast-lookup-table-quot ( seq -- newquot )
256 fast-lookup-table-seq
260 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
265 : at-quot ( assoc -- quot )
267 dup lookup-table-at? [
268 dup fast-lookup-table-at? [
269 fast-lookup-table-quot
276 \ at* [ at-quot ] 1 define-partial-eval
278 : diff-quot ( seq -- quot: ( seq' -- seq'' ) )
279 tester '[ [ [ @ not ] filter ] keep set-like ] ;
281 M\ set diff [ diff-quot ] 1 define-partial-eval
283 : intersect-quot ( seq -- quot: ( seq' -- seq'' ) )
284 tester '[ [ _ filter ] keep set-like ] ;
286 M\ set intersect [ intersect-quot ] 1 define-partial-eval
288 : fixnum-bits ( -- n )
289 cell-bits tag-bits get - ;
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 ? ;
295 \ bit? [ bit-quot ] "custom-inlining" set-word-prop
297 ! Speeds up sum-file, sort and reverse-complement benchmarks by
298 ! compiling decoder-readln better
300 in-d>> second value-info class>> growable class<=
301 [ \ push def>> ] [ f ] if
302 ] "custom-inlining" set-word-prop
304 ! Speeds up fasta benchmark
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
310 ! We want to constant-fold calls to heap-size, and recompile those
311 ! calls when a C type is redefined
314 [ depends-on-definition ] [ heap-size '[ _ ] ] bi
316 ] 1 define-partial-eval
318 ! Eliminates a few redundant checks here and there
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? ] ] }
327 ] "custom-inlining" set-word-prop