]> gitweb.factorcode.org Git - factor.git/blob - unfinished/compiler/cfg/vn/conditions/conditions.factor
ogg plays but 1) sound is broken and 2) it doesn't recognize EOF anymore, so it hangs...
[factor.git] / unfinished / compiler / cfg / vn / conditions / conditions.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences layouts accessors compiler.vops
4 compiler.cfg.vn.graph
5 compiler.cfg.vn.expressions
6 compiler.cfg.vn.liveness
7 compiler.cfg.vn ;
8 IN: compiler.cfg.vn.conditions
9
10 ! The CFG generator produces naive code for the following code
11 ! sequence:
12 !
13 ! fixnum< [ ... ] [ ... ] if
14 !
15 ! The fixnum< comparison generates a boolean, which is then
16 ! tested against f.
17 !
18 ! Using value numbering, we optimize the comparison of a boolean
19 ! against f where the boolean is the result of comparison.
20
21 : expr-f? ( expr -- ? )
22     dup op>> %iconst eq?
23     [ value>> \ f tag-number = ] [ drop f ] if ;
24
25 : comparison-with-f? ( insn -- expr/f ? )
26     #! The expr is a binary-op %icmp or %fcmp.
27     dup code>> cc/= eq? [
28         in>> vreg>vn vn>expr dup in2>> vn>expr expr-f?
29     ] [ drop f f ] if ;
30
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? ;
34
35 : original-comparison ( expr -- in/f code/f )
36     [ in>> vn>vreg ] [ code>> ] bi ;
37
38 : eliminate-boolean ( insn -- in/f code/f )
39     comparison-with-f? [
40         of-boolean? [
41             original-comparison
42         ] [ drop f f ] if
43     ] [ drop f f ] if ;
44
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 ;
50  
51 M: cond-branch eliminate
52     dup eliminate-boolean dup
53     [ [ >>in ] [ >>code ] bi* ] [ 2drop ] if ;