1 USING: accessors compiler.cfg compiler.cfg.comparisons
2 compiler.cfg.instructions compiler.cfg.registers
3 compiler.cfg.representations.conversion
4 compiler.cfg.representations.preferred compiler.cfg.utilities
5 compiler.constants compiler.test cpu.architecture kernel layouts
6 literals make math namespaces sequences system tools.test ;
7 FROM: alien.c-types => char ;
8 IN: compiler.cfg.representations
10 { { double-rep double-rep } } [
27 H{ } clone representations set
29 3 vreg-counter set-global
33 T{ ##allot f 2 16 float 4 }
34 T{ ##store-memory-imm f 1 2 $[ float-offset ] double-rep f }
38 2 1 tagged-rep double-rep emit-conversion
44 T{ ##load-memory-imm f 2 1 $[ float-offset ] double-rep f }
48 2 1 double-rep tagged-rep emit-conversion
52 : test-representations ( -- )
53 0 get block>cfg dup cfg set select-representations ;
55 ! Make sure cost calculation isn't completely wrong
64 T{ ##add-float f 3 1 2 }
65 T{ ##replace f 3 D: 0 }
66 T{ ##replace f 3 D: 1 }
67 T{ ##replace f 3 D: 2 }
79 { } [ test-representations ] unit-test
81 { 1 } [ 1 get instructions>> [ ##allot? ] count ] unit-test
83 ! Don't dereference the result of a peek
95 T{ ##add-float f 2 1 1 }
96 T{ ##replace f 2 D: 0 }
102 T{ ##add-float f 3 1 1 }
103 T{ ##replace f 3 D: 0 }
111 { } [ test-representations ] unit-test
118 } [ 1 get instructions>> ] unit-test
120 ! We cannot untag-fixnum the result of a peek if there are usages
121 ! of it as a tagged-rep
133 T{ ##replace f 1 R: 0 }
140 T{ ##replace f 2 D: 0 }
154 { } [ test-representations ] unit-test
161 } [ 1 get instructions>> ] unit-test
163 ! But its ok to untag-fixnum the result of a peek if all usages use
183 T{ ##replace f 2 D: 0 }
184 T{ ##replace f 3 D: 1 }
198 3 vreg-counter set-global
200 { } [ test-representations ] unit-test
205 T{ ##sar-imm f 1 4 $[ tag-bits get ] }
208 } [ 1 get instructions>> ] unit-test
210 ! scalar-rep => int-rep conversion
219 T{ ##vector>scalar f 3 2 int-4-rep }
220 T{ ##replace f 3 D: 0 }
232 { } [ test-representations ] unit-test
234 { t } [ 1 get instructions>> 4 swap nth ##scalar>integer? ] unit-test
236 ! Test phi node behavior
243 T{ ##load-integer f 1 1 }
248 T{ ##load-integer f 2 2 }
253 T{ ##phi f 3 H{ { 1 1 } { 2 2 } } }
254 T{ ##replace f 3 D: 0 }
268 { } [ test-representations ] unit-test
270 { T{ ##load-tagged f 1 $[ 1 tag-fixnum ] } }
271 [ 1 get instructions>> first ]
274 { T{ ##load-tagged f 2 $[ 2 tag-fixnum ] } }
275 [ 2 get instructions>> first ]
278 ! ##load-reference corner case
292 T{ ##load-reference f 3 f }
297 T{ ##phi f 4 H{ { 1 2 } { 2 3 } } }
298 T{ ##replace f 4 D: 0 }
312 { } [ test-representations ] unit-test
315 { 2 } [ 2 get instructions>> length ] unit-test
319 ! Make sure load-constant is converted into load-double
327 T{ ##load-reference f 2 0.5 }
328 T{ ##add-float f 3 1 2 }
329 T{ ##replace f 3 D: 0 }
341 [ ] [ test-representations ] unit-test
343 [ t ] [ 1 get instructions>> second ##load-double? ] unit-test
345 ! Make sure phi nodes are handled in a sane way
353 T{ ##compare-imm-branch f 1 2 cc= }
357 T{ ##load-reference f 2 1.5 }
362 T{ ##load-reference f 3 2.5 }
367 T{ ##phi f 4 H{ { 2 2 } { 3 3 } } }
369 T{ ##add-float f 6 4 5 }
370 T{ ##replace f 6 D: 0 }
381 [ ] [ test-representations ] unit-test
383 [ t ] [ 2 get instructions>> first ##load-double? ] unit-test
385 [ t ] [ 3 get instructions>> first ##load-double? ] unit-test
387 [ t ] [ 4 get instructions>> first ##phi? ] unit-test
390 : test-peephole ( insns -- insns )
393 0 get instructions>> ;
395 ! Don't convert the def site into anything but tagged-rep since
396 ! we might lose precision
397 5 vreg-counter set-global
403 T{ ##tagged>integer f 2 1 }
404 T{ ##add-float f 3 0 0 }
405 T{ ##store-memory-imm f 3 2 0 float-rep f }
406 T{ ##store-memory-imm f 3 2 4 float-rep f }
407 T{ ##mul-float f 4 0 0 }
408 T{ ##replace f 4 D: 0 }
410 [ ##single>double-float? ] any?
413 ! Converting a ##load-integer into a ##load-tagged
416 T{ ##load-tagged f 1 $[ 100 tag-fixnum ] }
417 T{ ##replace f 1 D: 0 }
421 T{ ##load-integer f 1 100 }
422 T{ ##replace f 1 D: 0 }
426 ! Peephole optimization if input to ##shl-imm is tagged
427 3 vreg-counter set-global
432 T{ ##sar-imm f 2 1 1 }
434 T{ ##shl-imm f 3 4 $[ tag-bits get ] }
435 T{ ##replace f 3 D: 0 }
440 T{ ##shl-imm f 2 1 3 }
442 T{ ##replace f 3 D: 0 }
446 3 vreg-counter set-global
451 T{ ##shl-imm f 2 1 $[ 10 tag-bits get - ] }
453 T{ ##shl-imm f 3 4 $[ tag-bits get ] }
454 T{ ##replace f 3 D: 0 }
459 T{ ##shl-imm f 2 1 10 }
461 T{ ##replace f 3 D: 0 }
468 T{ ##copy f 2 1 int-rep }
470 T{ ##shl-imm f 3 5 $[ tag-bits get ] }
471 T{ ##replace f 3 D: 0 }
476 T{ ##shl-imm f 2 1 $[ tag-bits get ] }
478 T{ ##replace f 3 D: 0 }
482 ! Peephole optimization if output of ##shl-imm needs to be tagged
485 T{ ##load-integer f 1 100 }
486 T{ ##shl-imm f 2 1 $[ 3 tag-bits get + ] }
487 T{ ##replace f 2 D: 0 }
491 T{ ##load-integer f 1 100 }
492 T{ ##shl-imm f 2 1 3 }
493 T{ ##replace f 2 D: 0 }
497 ! Peephole optimization if both input and output of ##shl-imm
502 T{ ##shl-imm f 1 0 3 }
503 T{ ##replace f 1 D: 0 }
508 T{ ##shl-imm f 1 0 3 }
509 T{ ##replace f 1 D: 0 }
513 ! Peephole optimization if neither input nor output of ##shl-imm need to be tagged
516 T{ ##load-integer f 1 100 }
517 T{ ##shl-imm f 2 1 3 }
518 T{ ##load-integer f 3 100 }
519 T{ ##load-integer f 4 100 }
520 T{ ##store-memory f 2 3 4 0 0 int-rep char }
524 T{ ##load-integer f 1 100 }
525 T{ ##shl-imm f 2 1 3 }
526 T{ ##load-integer f 3 100 }
527 T{ ##load-integer f 4 100 }
528 T{ ##store-memory f 2 3 4 0 0 int-rep char }
532 6 vreg-counter set-global
534 ! Peephole optimization if input to ##sar-imm is tagged
538 T{ ##sar-imm f 7 1 $[ 3 tag-bits get + ] }
539 T{ ##shl-imm f 2 7 $[ tag-bits get ] }
540 T{ ##replace f 2 D: 0 }
545 T{ ##sar-imm f 2 1 3 }
546 T{ ##replace f 2 D: 0 }
550 6 vreg-counter set-global
552 ! (Lack of) peephole optimization if output of ##sar-imm needs to be tagged
555 T{ ##load-integer f 1 100 }
556 T{ ##sar-imm f 7 1 3 }
557 T{ ##shl-imm f 2 7 $[ tag-bits get ] }
558 T{ ##replace f 2 D: 0 }
562 T{ ##load-integer f 1 100 }
563 T{ ##sar-imm f 2 1 3 }
564 T{ ##replace f 2 D: 0 }
568 ! Peephole optimization if input of ##sar-imm is tagged but output is untagged
573 T{ ##sar-imm f 1 0 $[ 3 tag-bits get + ] }
574 T{ ##load-integer f 3 100 }
575 T{ ##load-integer f 4 100 }
576 T{ ##store-memory f 1 3 4 0 0 int-rep char }
581 T{ ##sar-imm f 1 0 3 }
582 T{ ##load-integer f 3 100 }
583 T{ ##load-integer f 4 100 }
584 T{ ##store-memory f 1 3 4 0 0 int-rep char }
588 ! Peephole optimization if neither input nor output of ##sar-imm need to be tagged
591 T{ ##load-integer f 1 100 }
592 T{ ##sar-imm f 2 1 3 }
593 T{ ##load-integer f 3 100 }
594 T{ ##load-integer f 4 100 }
595 T{ ##store-memory f 2 3 4 0 0 int-rep char }
599 T{ ##load-integer f 1 100 }
600 T{ ##sar-imm f 2 1 3 }
601 T{ ##load-integer f 3 100 }
602 T{ ##load-integer f 4 100 }
603 T{ ##store-memory f 2 3 4 0 0 int-rep char }
609 T{ ##load-vector f 0 B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } short-8-rep }
610 T{ ##select-vector f 1 0 0 short-8-rep }
611 T{ ##sar-imm f 2 1 3 }
612 T{ ##load-integer f 3 100 }
614 T{ ##load-integer f 5 100 }
615 T{ ##load-integer f 6 100 }
616 T{ ##store-memory f 4 5 6 0 0 int-rep char }
620 T{ ##load-vector f 0 B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } short-8-rep }
621 T{ ##select-vector f 1 0 0 short-8-rep }
622 T{ ##sar-imm f 2 1 3 }
623 T{ ##load-integer f 3 100 }
625 T{ ##load-integer f 5 100 }
626 T{ ##load-integer f 6 100 }
627 T{ ##store-memory f 4 5 6 0 0 int-rep char }
631 6 vreg-counter set-global
635 T{ ##load-vector f 0 B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } int-4-rep }
636 T{ ##select-vector f 1 0 0 int-4-rep }
637 T{ ##sar-imm f 2 1 3 }
638 T{ ##load-integer f 3 100 }
640 T{ ##shl-imm f 4 7 $[ tag-bits get ] }
641 T{ ##replace f 4 D: 0 }
645 T{ ##load-vector f 0 B{ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } int-4-rep }
646 T{ ##select-vector f 1 0 0 int-4-rep }
647 T{ ##sar-imm f 2 1 3 }
648 T{ ##load-integer f 3 100 }
650 T{ ##replace f 4 D: 0 }
654 ! Tag/untag elimination
658 T{ ##add-imm f 2 1 $[ 100 tag-fixnum ] }
659 T{ ##replace f 2 D: 0 }
664 T{ ##add-imm f 2 1 100 }
665 T{ ##replace f 2 D: 0 }
674 T{ ##replace f 2 D: 0 }
681 T{ ##replace f 2 D: 0 }
685 ! Make sure we don't exceed immediate bounds
687 4 vreg-counter set-global
692 T{ ##sar-imm f 5 0 $[ tag-bits get ] }
693 T{ ##add-imm f 6 5 $[ 30 2^ ] }
694 T{ ##shl-imm f 2 6 $[ tag-bits get ] }
695 T{ ##replace f 2 D: 0 }
700 T{ ##add-imm f 2 0 $[ 30 2^ ] }
701 T{ ##replace f 2 D: 0 }
707 T{ ##load-integer f 0 100 }
708 T{ ##mul-imm f 7 0 $[ 30 2^ ] }
709 T{ ##shl-imm f 1 7 $[ tag-bits get ] }
710 T{ ##replace f 1 D: 0 }
714 T{ ##load-integer f 0 100 }
715 T{ ##mul-imm f 1 0 $[ 30 2^ ] }
716 T{ ##replace f 1 D: 0 }
721 ! Tag/untag elimination for ##mul-imm
725 T{ ##mul-imm f 1 0 100 }
726 T{ ##replace f 1 D: 0 }
731 T{ ##mul-imm f 1 0 100 }
732 T{ ##replace f 1 D: 0 }
736 4 vreg-counter set-global
742 T{ ##sar-imm f 5 1 $[ tag-bits get ] }
743 T{ ##add-imm f 2 5 30 }
744 T{ ##mul-imm f 3 2 $[ 100 tag-fixnum ] }
745 T{ ##replace f 3 D: 0 }
751 T{ ##add-imm f 2 1 30 }
752 T{ ##mul-imm f 3 2 100 }
753 T{ ##replace f 3 D: 0 }
757 ! Tag/untag elimination for ##compare-integer and ##test
762 T{ ##test f 2 0 1 cc= }
763 T{ ##replace f 2 D: 0 }
769 T{ ##test f 2 0 1 cc= }
770 T{ ##replace f 2 D: 0 }
778 T{ ##compare-integer f 2 0 1 cc= }
779 T{ ##replace f 2 D: 0 }
785 T{ ##compare-integer f 2 0 1 cc= }
786 T{ ##replace f 2 D: 0 }
794 T{ ##compare-integer-branch f 0 1 cc= }
800 T{ ##compare-integer-branch f 0 1 cc= }
808 T{ ##test-branch f 0 1 cc= }
814 T{ ##test-branch f 0 1 cc= }
822 T{ ##compare-integer-imm-branch f 0 $[ 10 tag-fixnum ] cc= }
828 T{ ##compare-integer-imm-branch f 0 10 cc= }
836 T{ ##test-imm-branch f 0 $[ 10 tag-fixnum ] cc= }
842 T{ ##test-imm-branch f 0 10 cc= }
846 ! Tag/untag elimination for ##neg
851 T{ ##replace f 1 D: 0 }
857 T{ ##replace f 1 D: 0 }
861 4 vreg-counter set-global
865 T{ ##peek { dst 0 } { loc D: 0 } }
866 T{ ##peek { dst 1 } { loc D: 1 } }
867 T{ ##sar-imm { dst 5 } { src1 0 } { src2 4 } }
868 T{ ##sar-imm { dst 6 } { src1 1 } { src2 4 } }
869 T{ ##mul { dst 2 } { src1 5 } { src2 6 } }
870 T{ ##mul-imm { dst 3 } { src1 2 } { src2 -16 } }
871 T{ ##replace { src 3 } { loc D: 0 } }
879 T{ ##replace f 3 D: 0 }
883 ! Tag/untag elimination for ##not
884 2 vreg-counter set-global
890 T{ ##xor-imm f 1 3 $[ tag-mask get ] }
891 T{ ##replace f 1 D: 0 }
897 T{ ##replace f 1 D: 0 }
901 ! untag elimination for ##bit-count
902 2 vreg-counter set-global
907 T{ ##bit-count f 3 0 }
908 T{ ##shl-imm f 1 3 $[ tag-bits get ] }
909 T{ ##replace f 1 D: 0 }
914 T{ ##bit-count f 1 0 }
915 T{ ##replace f 1 D: 0 }