]> gitweb.factorcode.org Git - factor.git/blob - core/optimizer/optimizer-tests.factor
Builtinn types now use new slot accessors; tuple slot type declaration work in progress
[factor.git] / core / optimizer / optimizer-tests.factor
1 USING: arrays compiler.units generic hashtables inference kernel
2 kernel.private math optimizer generator prettyprint sequences
3 sbufs strings tools.test vectors words sequences.private
4 quotations optimizer.backend classes classes.algebra
5 inference.dataflow classes.tuple.private continuations growable
6 optimizer.inlining namespaces hints ;
7 IN: optimizer.tests
8
9 [ H{ { 1 5 } { 3 4 } { 2 5 } } ] [
10     H{ { 1 2 } { 3 4 } } H{ { 2 5 } } union*
11 ] unit-test
12
13 [ H{ { 1 4 } { 2 4 } { 3 4 } } ] [
14     H{ { 1 2 } { 3 4 } } H{ { 2 3 } } union*
15 ] unit-test
16
17 GENERIC: xyz ( obj -- obj )
18 M: array xyz xyz ;
19
20 [ t ] [ \ xyz compiled>> ] unit-test
21
22 ! Test predicate inlining
23 : pred-test-1
24     dup fixnum? [
25         dup integer? [ "integer" ] [ "nope" ] if
26     ] [
27         "not a fixnum"
28     ] if ;
29
30 [ 1 "integer" ] [ 1 pred-test-1 ] unit-test
31
32 TUPLE: pred-test ;
33
34 : pred-test-2
35     dup tuple? [
36         dup pred-test? [ "pred-test" ] [ "nope" ] if
37     ] [
38         "not a tuple"
39     ] if ;
40
41 [ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-2 ] unit-test
42
43 : pred-test-3
44     dup pred-test? [
45         dup tuple? [ "pred-test" ] [ "nope" ] if
46     ] [
47         "not a tuple"
48     ] if ;
49
50 [ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-3 ] unit-test
51
52 : inline-test
53     "nom" = ;
54
55 [ t ] [ "nom" inline-test ] unit-test
56 [ f ] [ "shayin" inline-test ] unit-test
57 [ f ] [ 3 inline-test ] unit-test
58
59 : fixnum-declarations >fixnum 24 shift 1234 bitxor ;
60
61 [ ] [ 1000000 fixnum-declarations . ] unit-test
62
63 ! regression
64
65 : literal-not-branch 0 not [ ] [ ] if ;
66
67 [ ] [ literal-not-branch ] unit-test
68
69 ! regression
70
71 : bad-kill-1 ( a b -- c d e ) [ 3 f ] [ dup bad-kill-1 ] if ; inline
72 : bad-kill-2 bad-kill-1 drop ;
73
74 [ 3 ] [ t bad-kill-2 ] unit-test
75
76 ! regression
77 : (the-test) ( x -- y ) dup 0 > [ 1- (the-test) ] when ; inline
78 : the-test ( -- x y ) 2 dup (the-test) ;
79
80 [ 2 0 ] [ the-test ] unit-test
81
82 ! regression
83 : (double-recursion) ( start end -- )
84     < [
85         6 1 (double-recursion)
86         3 2 (double-recursion)
87     ] when ; inline
88
89 : double-recursion 0 2 (double-recursion) ;
90
91 [ ] [ double-recursion ] unit-test
92
93 ! regression
94 : double-label-1 ( a b c -- d )
95     [ f double-label-1 ] [ swap nth-unsafe ] if ; inline
96
97 : double-label-2 ( a -- b )
98     dup array? [ ] [ ] if 0 t double-label-1 ;
99
100 [ 0 ] [ 10 double-label-2 ] unit-test
101
102 ! regression
103 GENERIC: void-generic ( obj -- * )
104 : breakage ( -- * ) "hi" void-generic ;
105 [ t ] [ \ breakage compiled>> ] unit-test
106 [ breakage ] must-fail
107
108 ! regression
109 : branch-fold-regression-0 ( m -- n )
110     t [ ] [ 1+ branch-fold-regression-0 ] if ; inline
111
112 : branch-fold-regression-1 ( -- m )
113     10 branch-fold-regression-0 ;
114
115 [ 10 ] [ branch-fold-regression-1 ] unit-test
116
117 ! another regression
118 : constant-branch-fold-0 "hey" ; foldable
119 : constant-branch-fold-1 ( -- ? ) constant-branch-fold-0 "hey" = ; inline
120 [ 1 ] [ [ constant-branch-fold-1 [ 1 ] [ 2 ] if ] compile-call ] unit-test
121
122 ! another regression
123 : foo f ;
124 : bar ( -- ? ) foo 4 4 = and ;
125 [ f ] [ bar ] unit-test
126
127 ! ensure identities are working in some form
128 [ t ] [
129     [ { number } declare 0 + ] dataflow optimize
130     [ #push? ] node-exists? not
131 ] unit-test
132
133 ! compiling <tuple> with a non-literal class failed
134 : <tuple>-regression ( class -- tuple ) <tuple> ;
135
136 [ t ] [ \ <tuple>-regression compiled>> ] unit-test
137
138 GENERIC: foozul ( a -- b )
139 M: reversed foozul ;
140 M: integer foozul ;
141 M: slice foozul ;
142
143 [ reversed ] [ reversed \ foozul specific-method ] unit-test
144
145 ! regression
146 : constant-fold-2 f ; foldable
147 : constant-fold-3 4 ; foldable
148
149 [ f t ] [
150     [ constant-fold-2 constant-fold-3 4 = ] compile-call
151 ] unit-test
152
153 : constant-fold-4 f ; foldable
154 : constant-fold-5 f ; foldable
155
156 [ f ] [
157     [ constant-fold-4 constant-fold-5 or ] compile-call
158 ] unit-test
159
160 [ 5 ] [ 5 [ 0 + ] compile-call ] unit-test
161 [ 5 ] [ 5 [ 0 swap + ] compile-call ] unit-test
162
163 [ 5 ] [ 5 [ 0 - ] compile-call ] unit-test
164 [ -5 ] [ 5 [ 0 swap - ] compile-call ] unit-test
165 [ 0 ] [ 5 [ dup - ] compile-call ] unit-test
166
167 [ 5 ] [ 5 [ 1 * ] compile-call ] unit-test
168 [ 5 ] [ 5 [ 1 swap * ] compile-call ] unit-test
169 [ 0 ] [ 5 [ 0 * ] compile-call ] unit-test
170 [ 0 ] [ 5 [ 0 swap * ] compile-call ] unit-test
171 [ -5 ] [ 5 [ -1 * ] compile-call ] unit-test
172 [ -5 ] [ 5 [ -1 swap * ] compile-call ] unit-test
173
174 [ 0 ] [ 5 [ 1 mod ] compile-call ] unit-test
175 [ 0 ] [ 5 [ 1 rem ] compile-call ] unit-test
176
177 [ 5 ] [ 5 [ -1 bitand ] compile-call ] unit-test
178 [ 0 ] [ 5 [ 0 bitand ] compile-call ] unit-test
179 [ 5 ] [ 5 [ -1 swap bitand ] compile-call ] unit-test
180 [ 0 ] [ 5 [ 0 swap bitand ] compile-call ] unit-test
181 [ 5 ] [ 5 [ dup bitand ] compile-call ] unit-test
182
183 [ 5 ] [ 5 [ 0 bitor ] compile-call ] unit-test
184 [ -1 ] [ 5 [ -1 bitor ] compile-call ] unit-test
185 [ 5 ] [ 5 [ 0 swap bitor ] compile-call ] unit-test
186 [ -1 ] [ 5 [ -1 swap bitor ] compile-call ] unit-test
187 [ 5 ] [ 5 [ dup bitor ] compile-call ] unit-test
188
189 [ 5 ] [ 5 [ 0 bitxor ] compile-call ] unit-test
190 [ 5 ] [ 5 [ 0 swap bitxor ] compile-call ] unit-test
191 [ -6 ] [ 5 [ -1 bitxor ] compile-call ] unit-test
192 [ -6 ] [ 5 [ -1 swap bitxor ] compile-call ] unit-test
193 [ 0 ] [ 5 [ dup bitxor ] compile-call ] unit-test
194
195 [ 0 ] [ 5 [ 0 swap shift ] compile-call ] unit-test
196 [ 5 ] [ 5 [ 0 shift ] compile-call ] unit-test
197
198 [ f ] [ 5 [ dup < ] compile-call ] unit-test
199 [ t ] [ 5 [ dup <= ] compile-call ] unit-test
200 [ f ] [ 5 [ dup > ] compile-call ] unit-test
201 [ t ] [ 5 [ dup >= ] compile-call ] unit-test
202
203 [ t ] [ 5 [ dup eq? ] compile-call ] unit-test
204 [ t ] [ 5 [ dup = ] compile-call ] unit-test
205 [ t ] [ 5 [ dup number= ] compile-call ] unit-test
206 [ t ] [ \ vector [ \ vector = ] compile-call ] unit-test
207
208 GENERIC: detect-number ( obj -- obj )
209 M: number detect-number ;
210
211 [ 10 f [ <array> 0 + detect-number ] compile-call ] must-fail
212
213 ! Regression
214 [ 4 [ + ] ] [ 2 2 [ [ + ] [ call ] keep ] compile-call ] unit-test
215
216 ! Regression
217 USE: sorting
218 USE: sorting.private
219
220 : old-binsearch ( elt quot seq -- elt quot i )
221     dup length 1 <= [
222         slice-from
223     ] [
224         [ midpoint swap call ] 3keep roll dup zero?
225         [ drop dup slice-from swap midpoint@ + ]
226         [ partition old-binsearch ] if
227     ] if ; inline
228
229 [ 10 ] [
230     10 20 >vector <flat-slice>
231     [ [ - ] swap old-binsearch ] compile-call 2nip
232 ] unit-test
233
234 ! Regression
235 TUPLE: silly-tuple a b ;
236
237 [ 1 2 { silly-tuple-a silly-tuple-b } ] [
238     T{ silly-tuple f 1 2 }
239     [
240         { silly-tuple-a silly-tuple-b } [ get-slots ] keep
241     ] compile-call
242 ] unit-test
243
244 ! Regression
245 : empty-compound ;
246
247 : node-successor-f-bug ( x -- * )
248     [ 3 throw ] [ empty-compound ] compose [ 3 throw ] if ;
249
250 [ t ] [ \ node-successor-f-bug compiled>> ] unit-test
251
252 [ ] [ [ new ] dataflow optimize drop ] unit-test
253
254 [ ] [ [ <tuple> ] dataflow optimize drop ] unit-test
255
256 ! Make sure we have sane heuristics
257 : should-inline? ( generic class -- ? ) method flat-length 10 <= ;
258
259 [ t ] [ \ fixnum \ shift should-inline? ] unit-test
260 [ f ] [ \ array \ equal? should-inline? ] unit-test
261 [ f ] [ \ sequence \ hashcode* should-inline? ] unit-test
262 [ t ] [ \ array \ nth-unsafe should-inline? ] unit-test
263 [ t ] [ \ growable \ nth-unsafe should-inline? ] unit-test
264 [ t ] [ \ sbuf \ set-nth-unsafe should-inline? ] unit-test
265
266 ! Regression
267 : lift-throw-tail-regression ( obj -- obj str )
268     dup integer? [ "an integer" ] [
269         dup string? [ "a string" ] [
270             "error" throw
271         ] if
272     ] if ;
273
274 [ t ] [ \ lift-throw-tail-regression compiled>> ] unit-test
275 [ 3 "an integer" ] [ 3 lift-throw-tail-regression ] unit-test
276 [ "hi" "a string" ] [ "hi" lift-throw-tail-regression ] unit-test
277
278 : lift-loop-tail-test-1 ( a quot -- )
279     over even? [
280         [ >r 3 - r> call ] keep lift-loop-tail-test-1
281     ] [
282         over 0 < [
283             2drop
284         ] [
285             [ >r 2 - r> call ] keep lift-loop-tail-test-1
286         ] if
287     ] if ; inline
288
289 : lift-loop-tail-test-2
290     10 [ ] lift-loop-tail-test-1 1 2 3 ;
291
292 [ 1 2 3 ] [ lift-loop-tail-test-2 ] unit-test
293
294 GENERIC: generic-inline-test ( x -- y )
295 M: integer generic-inline-test ;
296
297 : generic-inline-test-1 ( -- x )
298     1
299     generic-inline-test
300     generic-inline-test
301     generic-inline-test
302     generic-inline-test
303     generic-inline-test
304     generic-inline-test
305     generic-inline-test
306     generic-inline-test
307     generic-inline-test
308     generic-inline-test ;
309
310 ! Inlining all of the above should only take two passes
311 [ { t f } ] [
312     \ generic-inline-test-1 def>> dataflow
313     [ optimize-1 , optimize-1 , drop ] { } make
314 ] unit-test
315
316 ! Forgot a recursive inline check
317 : recursive-inline-hang ( a -- a )
318     dup array? [ recursive-inline-hang ] when ;
319
320 HINTS: recursive-inline-hang array ;
321
322 : recursive-inline-hang-1 ( -- a )
323     { } recursive-inline-hang ;
324
325 [ t ] [ \ recursive-inline-hang-1 compiled>> ] unit-test
326
327 DEFER: recursive-inline-hang-3
328
329 : recursive-inline-hang-2 ( a -- a )
330     dup array? [ recursive-inline-hang-3 ] when ;
331
332 HINTS: recursive-inline-hang-2 array ;
333
334 : recursive-inline-hang-3 ( a -- a )
335     dup array? [ recursive-inline-hang-2 ] when ;
336
337 HINTS: recursive-inline-hang-3 array ;
338
339 ! Regression
340 USE: sequences.private
341
342 [ ] [ { (3append) } compile ] unit-test
343
344 ! Wow
345 : counter-example ( a b c d -- a' b' c' d' )
346     dup 0 > [ 1 - >r rot 2 * r> counter-example ] when ; inline
347
348 : counter-example' ( -- a' b' c' d' )
349     1 2 3.0 3 counter-example ;
350
351 [ 2 4 6.0 0 ] [ counter-example' ] unit-test
352
353 : member-test ( obj -- ? ) { + - * / /i } member? ;
354
355 \ member-test must-infer
356 [ ] [ \ member-test word-dataflow optimize 2drop ] unit-test
357 [ t ] [ \ + member-test ] unit-test
358 [ f ] [ \ append member-test ] unit-test