1 USING: accessors arrays compiler.units generic hashtables
2 stack-checker kernel kernel.private math prettyprint sequences
3 sbufs strings tools.test vectors words sequences.private
4 quotations classes classes.algebra classes.tuple.private
5 continuations growable memory namespaces hints alien.accessors
6 compiler.tree.builder compiler.tree.optimizer sequences.deep
7 compiler.test definitions generic.single shuffle math.order
8 compiler.cfg.debugger classes.struct alien.syntax alien.data
9 alien.c-types splitting ;
10 IN: compiler.tests.optimizer
12 GENERIC: xyz ( obj -- obj )
15 [ t ] [ M\ array xyz word-optimized? ] unit-test
17 ! Test predicate inlining
18 : pred-test-1 ( a -- b c )
20 dup integer? [ "integer" ] [ "nope" ] if
25 [ 1 "integer" ] [ 1 pred-test-1 ] unit-test
29 : pred-test-2 ( a -- b c )
31 dup pred-test? [ "pred-test" ] [ "nope" ] if
36 [ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-2 ] unit-test
38 : pred-test-3 ( a -- b c )
40 dup tuple? [ "pred-test" ] [ "nope" ] if
45 [ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-3 ] unit-test
47 : inline-test ( a -- b )
50 [ t ] [ "nom" inline-test ] unit-test
51 [ f ] [ "shayin" inline-test ] unit-test
52 [ f ] [ 3 inline-test ] unit-test
54 : fixnum-declarations ( a -- b ) >fixnum 24 shift 1234 bitxor ;
56 [ ] [ 1000000 fixnum-declarations . ] unit-test
60 : literal-not-branch ( -- ) 0 not [ ] [ ] if ;
62 [ ] [ literal-not-branch ] unit-test
66 : bad-kill-1 ( a b -- c d e ) [ 3 f ] [ dup bad-kill-1 ] if ; inline recursive
67 : bad-kill-2 ( a b -- c d ) bad-kill-1 drop ;
69 [ 3 ] [ t bad-kill-2 ] unit-test
72 : (the-test) ( x -- y ) dup 0 > [ 1 - (the-test) ] when ; inline recursive
73 : the-test ( -- x y ) 2 dup (the-test) ;
75 [ 2 0 ] [ the-test ] unit-test
78 : (double-recursion) ( start end -- )
80 6 1 (double-recursion)
81 3 2 (double-recursion)
82 ] when ; inline recursive
84 : double-recursion ( -- ) 0 2 (double-recursion) ;
86 [ ] [ double-recursion ] unit-test
89 : double-label-1 ( a b c -- d )
90 [ f double-label-1 ] [ swap nth-unsafe ] if ; inline recursive
92 : double-label-2 ( a -- b )
93 dup array? [ ] [ ] if 0 t double-label-1 ;
95 [ 0 ] [ 10 iota double-label-2 ] unit-test
98 GENERIC: void-generic ( obj -- * )
99 : breakage ( -- * ) "hi" void-generic ;
100 [ t ] [ \ breakage word-optimized? ] unit-test
101 [ breakage ] must-fail
104 : branch-fold-regression-0 ( m -- n )
105 t [ ] [ 1 + branch-fold-regression-0 ] if ; inline recursive
107 : branch-fold-regression-1 ( -- m )
108 10 branch-fold-regression-0 ;
110 [ 10 ] [ branch-fold-regression-1 ] unit-test
113 : constant-branch-fold-0 ( -- value ) "hey" ; foldable
114 : constant-branch-fold-1 ( -- ? ) constant-branch-fold-0 "hey" = ; inline
115 [ 1 ] [ [ constant-branch-fold-1 [ 1 ] [ 2 ] if ] compile-call ] unit-test
118 : foo ( -- value ) f ;
119 : bar ( -- ? ) foo 4 4 = and ;
120 [ f ] [ bar ] unit-test
122 ! compiling <tuple> with a non-literal class failed
123 : <tuple>-regression ( class -- tuple ) <tuple> ;
125 [ t ] [ \ <tuple>-regression word-optimized? ] unit-test
128 : constant-fold-2 ( -- value ) f ; foldable
129 : constant-fold-3 ( -- value ) 4 ; foldable
132 [ constant-fold-2 constant-fold-3 4 = ] compile-call
135 : constant-fold-4 ( -- value ) f ; foldable
136 : constant-fold-5 ( -- value ) f ; foldable
139 [ constant-fold-4 constant-fold-5 or ] compile-call
142 [ 5 ] [ 5 [ 0 + ] compile-call ] unit-test
143 [ 5 ] [ 5 [ 0 swap + ] compile-call ] unit-test
145 [ 5 ] [ 5 [ 0 - ] compile-call ] unit-test
146 [ -5 ] [ 5 [ 0 swap - ] compile-call ] unit-test
147 [ 0 ] [ 5 [ dup - ] compile-call ] unit-test
149 [ 5 ] [ 5 [ 1 * ] compile-call ] unit-test
150 [ 5 ] [ 5 [ 1 swap * ] compile-call ] unit-test
151 [ 0 ] [ 5 [ 0 * ] compile-call ] unit-test
152 [ 0 ] [ 5 [ 0 swap * ] compile-call ] unit-test
153 [ -5 ] [ 5 [ -1 * ] compile-call ] unit-test
154 [ -5 ] [ 5 [ -1 swap * ] compile-call ] unit-test
156 [ 0 ] [ 5 [ 1 mod ] compile-call ] unit-test
157 [ 0 ] [ 5 [ 1 rem ] compile-call ] unit-test
158 [ 0.5 ] [ 5.5 [ 1 mod ] compile-call ] unit-test
159 [ 0.5 ] [ 5.5 [ 1 rem ] compile-call ] unit-test
161 [ 5 ] [ 5 [ -1 bitand ] compile-call ] unit-test
162 [ 0 ] [ 5 [ 0 bitand ] compile-call ] unit-test
163 [ 5 ] [ 5 [ -1 swap bitand ] compile-call ] unit-test
164 [ 0 ] [ 5 [ 0 swap bitand ] compile-call ] unit-test
165 [ 5 ] [ 5 [ dup bitand ] compile-call ] unit-test
167 [ 5 ] [ 5 [ 0 bitor ] compile-call ] unit-test
168 [ -1 ] [ 5 [ -1 bitor ] compile-call ] unit-test
169 [ 5 ] [ 5 [ 0 swap bitor ] compile-call ] unit-test
170 [ -1 ] [ 5 [ -1 swap bitor ] compile-call ] unit-test
171 [ 5 ] [ 5 [ dup bitor ] compile-call ] unit-test
173 [ 5 ] [ 5 [ 0 bitxor ] compile-call ] unit-test
174 [ 5 ] [ 5 [ 0 swap bitxor ] compile-call ] unit-test
175 [ -6 ] [ 5 [ -1 bitxor ] compile-call ] unit-test
176 [ -6 ] [ 5 [ -1 swap bitxor ] compile-call ] unit-test
177 [ 0 ] [ 5 [ dup bitxor ] compile-call ] unit-test
179 [ 0 ] [ 5 [ 0 swap shift ] compile-call ] unit-test
180 [ 5 ] [ 5 [ 0 shift ] compile-call ] unit-test
182 [ f ] [ 5 [ dup < ] compile-call ] unit-test
183 [ t ] [ 5 [ dup <= ] compile-call ] unit-test
184 [ f ] [ 5 [ dup > ] compile-call ] unit-test
185 [ t ] [ 5 [ dup >= ] compile-call ] unit-test
187 [ t ] [ 5 [ dup eq? ] compile-call ] unit-test
188 [ t ] [ 5 [ dup = ] compile-call ] unit-test
189 [ t ] [ 5 [ dup number= ] compile-call ] unit-test
190 [ t ] [ \ vector [ \ vector = ] compile-call ] unit-test
192 GENERIC: detect-number ( obj -- obj )
193 M: number detect-number ;
195 [ 10 f [ <array> 0 + detect-number ] compile-call ] must-fail
198 [ 4 [ + ] ] [ 2 2 [ [ + ] [ call ] keep ] compile-call ] unit-test
201 : empty-compound ( -- ) ;
203 : node-successor-f-bug ( x -- * )
204 [ 3 throw ] [ empty-compound ] compose [ 3 throw ] if ;
206 [ t ] [ \ node-successor-f-bug word-optimized? ] unit-test
208 [ ] [ [ new ] build-tree optimize-tree drop ] unit-test
210 [ ] [ [ <tuple> ] build-tree optimize-tree drop ] unit-test
213 : lift-throw-tail-regression ( obj -- obj str )
214 dup integer? [ "an integer" ] [
215 dup string? [ "a string" ] [
220 [ t ] [ \ lift-throw-tail-regression word-optimized? ] unit-test
221 [ 3 "an integer" ] [ 3 lift-throw-tail-regression ] unit-test
222 [ "hi" "a string" ] [ "hi" lift-throw-tail-regression ] unit-test
224 : lift-loop-tail-test-1 ( a quot: ( -- ) -- )
226 [ [ 3 - ] dip call ] keep lift-loop-tail-test-1
231 [ [ 2 - ] dip call ] keep lift-loop-tail-test-1
233 ] if ; inline recursive
235 : lift-loop-tail-test-2 ( -- a b c )
236 10 [ ] lift-loop-tail-test-1 1 2 3 ;
238 \ lift-loop-tail-test-2 def>> must-infer
240 [ 1 2 3 ] [ lift-loop-tail-test-2 ] unit-test
242 ! Forgot a recursive inline check
243 : recursive-inline-hang ( a -- a )
244 dup array? [ recursive-inline-hang ] when ;
246 HINTS: recursive-inline-hang array ;
248 : recursive-inline-hang-1 ( -- a )
249 { } recursive-inline-hang ;
251 [ t ] [ \ recursive-inline-hang-1 word-optimized? ] unit-test
253 DEFER: recursive-inline-hang-3
255 : recursive-inline-hang-2 ( a -- a )
256 dup array? [ recursive-inline-hang-3 ] when ;
258 HINTS: recursive-inline-hang-2 array ;
260 : recursive-inline-hang-3 ( a -- a )
261 dup array? [ recursive-inline-hang-2 ] when ;
263 HINTS: recursive-inline-hang-3 array ;
266 [ ] [ { 3append-as } compile ] unit-test
269 : counter-example ( a b c d -- a' b' c' d' )
270 dup 0 > [ 1 - [ rot 2 * ] dip counter-example ] when ; inline recursive
272 : counter-example' ( -- a' b' c' d' )
273 1 2 3.0 3 counter-example ;
275 [ 2 4 6.0 0 ] [ counter-example' ] unit-test
277 : member-test ( obj -- ? ) { + - * / /i } member? ;
279 \ member-test def>> must-infer
280 [ ] [ \ member-test build-tree optimize-tree drop ] unit-test
281 [ t ] [ \ + member-test ] unit-test
282 [ f ] [ \ append member-test ] unit-test
285 TUPLE: cons car cdr ;
287 UNION: improper-list cons POSTPONE: f ;
289 PREDICATE: list < improper-list
290 [ cdr>> list instance? ] [ t ] if* ;
293 T{ cons f 1 T{ cons f 2 T{ cons f 3 f } } }
294 [ list instance? ] compile-call
297 ! <tuple> type function bustage
298 [ T{ cons } 7 ] [ cons tuple-layout [ [ <tuple> ] [ length ] bi ] compile-call ] unit-test
301 : interval-inference-bug ( obj -- obj x )
302 dup "a" get { array-capacity } declare >=
303 [ dup "b" get { array-capacity } declare >= [ 3 ] [ 4 ] if ] [ 5 ] if ;
305 [ t ] [ \ interval-inference-bug word-optimized? ] unit-test
307 [ ] [ 1 "a" set 2 "b" set ] unit-test
308 [ 2 3 ] [ 2 interval-inference-bug ] unit-test
309 [ 1 4 ] [ 1 interval-inference-bug ] unit-test
310 [ 0 5 ] [ 0 interval-inference-bug ] unit-test
312 : aggressive-flush-regression ( a -- b )
313 f over [ <array> drop ] dip 1 + ;
315 [ 1.0 aggressive-flush-regression drop ] must-fail
317 [ 1 [ "hi" + drop ] compile-call ] must-fail
319 [ "hi" f [ <array> drop ] compile-call ] must-fail
321 TUPLE: some-tuple x ;
323 : allot-regression ( a -- b )
324 [ ] curry some-tuple boa ;
326 [ T{ some-tuple f [ 3 ] } ] [ 3 allot-regression ] unit-test
328 [ 1 ] [ B{ 0 0 0 0 } [ 0 alien-signed-4 1 + ] compile-call ] unit-test
329 [ 1 ] [ B{ 0 0 0 0 } [ 0 alien-unsigned-4 1 + ] compile-call ] unit-test
330 [ 1 ] [ B{ 0 0 0 0 0 0 0 0 } [ 0 alien-signed-8 1 + ] compile-call ] unit-test
331 [ 1 ] [ B{ 0 0 0 0 0 0 0 0 } [ 0 alien-unsigned-8 1 + ] compile-call ] unit-test
332 [ 1 ] [ B{ 0 0 0 0 0 0 0 0 } [ 0 alien-signed-cell 1 + ] compile-call ] unit-test
333 [ 1 ] [ B{ 0 0 0 0 0 0 0 0 } [ 0 alien-unsigned-cell 1 + ] compile-call ] unit-test
335 : deep-find-test ( seq -- ? ) [ 5 = ] deep-find ;
337 [ 5 ] [ { 1 2 { 3 { 4 5 } } } deep-find-test ] unit-test
338 [ f ] [ { 1 2 { 3 { 4 } } } deep-find-test ] unit-test
340 [ B{ 0 1 2 3 4 5 6 7 } ] [ [ 8 iota [ ] B{ } map-as ] compile-call ] unit-test
342 [ 0 ] [ 1234 [ { fixnum } declare -64 shift ] compile-call ] unit-test
344 ! Loop detection problem found by doublec
350 counter inc counter get 2 < [ loop-bbb ] when ; inline recursive
353 [ loop-aaa ] with-scope ; inline recursive
355 : loop-ccc ( -- ) loop-bbb ;
357 [ 0 ] [ 0 counter set loop-ccc counter get ] unit-test
359 ! Type inference issue
362 [ { bignum integer } declare [ shift ] keep 1 + ] compile-call
365 : broken-declaration ( -- ) \ + declare ;
367 [ f ] [ \ broken-declaration word-optimized? ] unit-test
369 [ ] [ [ \ broken-declaration forget ] with-compilation-unit ] unit-test
371 ! Interval inference issue
376 [ dup 700 <= [ swap 1024 rem rem ] [ 2drop 70 ] if ]
382 ! Modular arithmetic bug
383 : modular-arithmetic-bug ( a -- b ) >integer 256 mod ;
385 [ 1 ] [ 257 modular-arithmetic-bug ] unit-test
386 [ -10 ] [ -10 modular-arithmetic-bug ] unit-test
410 : dispatch-branch-problem ( a b c -- d )
411 dup 0 < [ "boo" throw ] when
412 1 + { [ + ] [ - ] [ * ] } dispatch ;
414 [ 3 4 -1 dispatch-branch-problem ] [ "boo" = ] must-fail-with
415 [ -1 ] [ 3 4 0 dispatch-branch-problem ] unit-test
416 [ 12 ] [ 3 4 1 dispatch-branch-problem ] unit-test
418 [ 1024 bignum ] [ 10 [ 1 >bignum swap >fixnum shift ] compile-call dup class-of ] unit-test
420 TUPLE: grid-mesh-tuple { length read-only } { step read-only } ;
422 : grid-mesh-test-case ( -- vertices )
423 1.0 1.0 { 2 } first /f [ /i 1 + ] keep grid-mesh-tuple boa
426 [ drop length>> >fixnum 2 min ] 2keep
429 0 swap set-nth-unsafe
433 [ { 0.5 } ] [ grid-mesh-test-case ] unit-test
435 [ { 1 } "bar" ] [ { 1 } [ [ [ [ "foo" throw ] [ "bar" throw ] recover ] attempt-all f ] [ ] recover ] compile-call ] unit-test
437 GENERIC: bad-push-test-case ( a -- b )
438 M: object bad-push-test-case "foo" throw ; inline
439 [ { 1 } "bar" ] [ { 1 } [ [ [ [ bad-push-test-case ] [ "bar" throw ] recover ] attempt-all f ] [ ] recover ] compile-call ] unit-test
441 STRUCT: BitmapData { Scan0 void* } ;
446 [ BitmapData memory>struct ALIEN: 123 >>Scan0 drop ]
447 with-out-parameters Scan0>>
453 10 [ [ minor-gc split-slice ] ignore-errors ] times