1 ! Copyright (C) 2008, 2011 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types assocs classes classes.algebra
4 classes.tuple classes.tuple.private combinators
5 combinators.short-circuit compiler.tree.propagation.info effects
6 fry generalizations generic generic.single growable hash-sets
7 hashtables kernel layouts locals math math.integers.private
8 math.intervals math.order math.partial-dispatch math.private
9 namespaces quotations sequences sequences.generalizations
10 sequences.private sets sets.private stack-checker
11 stack-checker.dependencies vectors words ;
13 IN: compiler.tree.propagation.transforms
16 ! If first input has a known type and second input is an
17 ! object, we convert this to [ swap equal? ].
18 in-d>> first2 value-info class>> object class= [
19 value-info class>> \ equal? method-for-class
22 ] "custom-inlining" set-word-prop
24 : rem-custom-inlining ( inputs -- quot/f )
25 dup first value-info class>> integer class<= [
26 second value-info literal>> dup integer?
27 [ 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 : non-negative-fixnum? ( obj -- ? )
47 { [ fixnum? ] [ 0 >= ] } 1&& ;
49 : simplify-bitand? ( value1 value2 -- ? )
50 [ literal>> non-negative-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 [ integer>fixnum fixnum-bitand ] ]
99 [ 2dup swap simplify-bitand? ]
100 [ 2drop [ [ integer>fixnum ] dip fixnum-bitand ] ]
104 ] "custom-inlining" set-word-prop
109 in-d>> first value-info literal>> 1 eq? ;
111 : shift-2^ ( -- quot )
112 cell-bits tag-bits get - 1 -
114 integer>fixnum-strict dup 0 < [ 2drop 0 ] [
115 dup _ < [ fixnum-shift ] [
123 in-d>> second value-info literal>> -1 eq? ;
125 : shift-2/ ( -- quot )
128 { [ over fixnum? ] [ fixnum-shift ] }
129 { [ over bignum? ] [ bignum-shift ] }
130 [ drop \ shift no-method ]
136 { [ dup 2^? ] [ drop shift-2^ ] }
137 { [ dup 2/? ] [ drop shift-2/ ] }
140 ] "custom-inlining" set-word-prop
142 { /i fixnum/i fixnum/i-fast bignum/i } [
144 in-d>> first2 [ value-info ] bi@ {
145 [ drop class>> integer class<= ]
146 [ drop interval>> 0 [a,a] interval>= ]
147 [ nip literal>> integer? ]
148 [ nip literal>> power-of-2? ]
149 } 2&& [ [ log2 neg shift ] ] [ f ] if
150 ] "custom-inlining" set-word-prop
153 ! Generate more efficient code for common idiom
155 in-d>> first value-info literal>> {
156 { V{ } [ [ drop { } 0 vector boa ] ] }
157 { H{ } [ [ drop 0 <hashtable> ] ] }
158 { HS{ } [ [ drop 0 <hash-set> ] ] }
161 ] "custom-inlining" set-word-prop
163 ERROR: bad-partial-eval quot word ;
165 : check-effect ( quot word -- )
166 2dup [ infer ] [ stack-effect ] bi* effect<=
167 [ 2drop ] [ bad-partial-eval ] if ;
169 :: define-partial-eval ( word quot n -- )
173 dup [ literal?>> ] all? [
178 dup word check-effect
181 ] "custom-inlining" set-word-prop ;
183 : inline-new ( class -- quot/f )
186 [ add-depends-on-tuple-layout ]
187 [ drop all-slots [ initial>> literalize ] [ ] map-as ]
193 \ new [ inline-new ] 1 define-partial-eval
199 ! union{ and intersection{ have useless expansions, and recurse infinitely
200 dup { [ length 2 >= ] [ second \ instance? = ] } 1&& [
204 ] 1 define-partial-eval
207 : nths-quot ( indices -- quot )
208 [ [ '[ _ swap nth ] ] map ] [ length ] bi
209 '[ _ cleave _ narray ] ;
212 shuffle-mapping nths-quot
213 ] 1 define-partial-eval
219 H{ } zip-index-as '[ _ at ]
222 ] 1 define-partial-eval
224 : member-eq-quot ( seq -- newquot )
225 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
226 [ drop f ] suffix [ cond ] curry ;
229 dup sequence? [ member-eq-quot ] [ drop f ] if
230 ] 1 define-partial-eval
233 : member-quot ( seq -- newquot )
236 [ literalize [ t ] ] { } map>assoc linear-case-quot
242 dup sequence? [ member-quot ] [ drop f ] if
243 ] 1 define-partial-eval
245 ! Fast at for integer maps
246 CONSTANT: lookup-table-at-max 256
248 : lookup-table-at? ( assoc -- ? )
249 ! Can we use a fast byte array test here?
253 [ keys [ integer? ] all? ]
254 [ keys [ 0 lookup-table-at-max between? ] all? ]
257 : lookup-table-seq ( assoc -- table )
258 [ keys supremum 1 + iota ] keep '[ _ at ] { } map-as ;
260 : lookup-table-quot ( seq -- newquot )
265 nth-unsafe dup >boolean
270 : fast-lookup-table-at? ( assoc -- ? )
272 [ [ integer? ] all? ]
273 [ [ 0 254 between? ] all? ]
276 : fast-lookup-table-seq ( assoc -- table )
277 lookup-table-seq [ 255 or ] B{ } map-as ;
279 : fast-lookup-table-quot ( seq -- newquot )
280 fast-lookup-table-seq
284 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
289 : at-quot ( assoc -- quot )
291 dup lookup-table-at? [
292 dup fast-lookup-table-at? [
293 fast-lookup-table-quot
300 \ at* [ at-quot ] 1 define-partial-eval
302 : diff-quot ( seq -- quot: ( seq' -- seq'' ) )
303 [ tester ] keep '[ members [ @ ] reject _ set-like ] ;
305 M\ sets:set diff [ diff-quot ] 1 define-partial-eval
307 : intersect-quot ( seq -- quot: ( seq' -- seq'' ) )
308 [ tester ] keep '[ members _ filter _ set-like ] ;
310 M\ sets:set intersect [ intersect-quot ] 1 define-partial-eval
312 : intersects?-quot ( seq -- quot: ( seq' -- seq'' ) )
313 tester '[ members _ any? ] ;
315 M\ sets:set intersects? [ intersects?-quot ] 1 define-partial-eval
317 : bit-quot ( #call -- quot/f )
318 in-d>> second value-info interval>> 0 fixnum-bits [a,b] interval-subset?
319 [ [ integer>fixnum ] dip fixnum-bit? ] f ? ;
321 \ bit? [ bit-quot ] "custom-inlining" set-word-prop
323 ! Speeds up sum-file, sort and reverse-complement benchmarks by
324 ! compiling decoder-readln better
326 in-d>> second value-info class>> growable class<=
327 [ \ push def>> ] [ f ] if
328 ] "custom-inlining" set-word-prop
330 : custom-inline-fixnum ( #call method -- y )
331 [ in-d>> first value-info class>> fixnum \ f class-or class<= ] dip
332 '[ [ dup [ _ no-method ] unless ] ] [ f ] if ;
334 ! Speeds up fasta benchmark
335 { >fixnum integer>fixnum integer>fixnum-strict } [
336 dup '[ _ custom-inline-fixnum ] "custom-inlining" set-word-prop
339 ! We want to constant-fold calls to heap-size, and recompile those
340 ! calls when a C type is redefined
342 [ add-depends-on-c-type ] [ heap-size '[ _ ] ] bi
343 ] 1 define-partial-eval
345 ! Eliminates a few redundant checks here and there
347 in-d>> first2 [ value-info class>> ] bi@ {
348 { [ 2dup [ fixnum classes-intersect? not ] either? ] [ [ 2drop f ] ] }
349 { [ 2dup [ fixnum class<= ] both? ] [ [ 2drop t ] ] }
350 { [ dup fixnum class<= ] [ [ drop fixnum? ] ] }
351 { [ over fixnum class<= ] [ [ nip fixnum? ] ] }
354 ] "custom-inlining" set-word-prop