1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences layouts accessors compiler.vops
5 compiler.cfg.vn.expressions
6 compiler.cfg.vn.liveness
8 IN: compiler.cfg.vn.conditions
10 ! The CFG generator produces naive code for the following code
13 ! fixnum< [ ... ] [ ... ] if
15 ! The fixnum< comparison generates a boolean, which is then
18 ! Using value numbering, we optimize the comparison of a boolean
19 ! against f where the boolean is the result of comparison.
21 : expr-f? ( expr -- ? )
23 [ value>> \ f tag-number = ] [ drop f ] if ;
25 : comparison-with-f? ( insn -- expr/f ? )
26 #! The expr is a binary-op %icmp or %fcmp.
28 in>> vreg>vn vn>expr dup in2>> vn>expr expr-f?
31 : of-boolean? ( expr -- expr/f ? )
32 #! The expr is a binary-op %icmp or %fcmp.
33 in1>> vn>expr dup op>> { %%iboolean %%fboolean } memq? ;
35 : original-comparison ( expr -- in/f code/f )
36 [ in>> vn>vreg ] [ code>> ] bi ;
38 : eliminate-boolean ( insn -- in/f code/f )
45 M: cond-branch make-value-node
46 #! If the conditional branch is testing the result of an
47 #! earlier comparison against f, we only mark as live the
48 #! earlier comparison, so DCE will eliminate the boolean.
49 dup eliminate-boolean drop swap in>> or live-vreg ;
51 M: cond-branch eliminate
52 dup eliminate-boolean dup
53 [ [ >>in ] [ >>code ] bi* ] [ 2drop ] if ;