Skip to content

Commit 922e381

Browse files
committed
Compiler: simplify branch
1 parent 503abd9 commit 922e381

File tree

3 files changed

+119
-194
lines changed

3 files changed

+119
-194
lines changed

compiler/lib/eval.ml

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,30 @@ let the_cond_of info x =
710710
| _ -> Unknown)
711711
x
712712

713-
let eval_branch update_branch info l =
713+
module Simple_block = struct
714+
type t =
715+
| Return of Var.t
716+
| Expr of expr
717+
| Branch of cont
718+
719+
let equal a b =
720+
match a, b with
721+
| Return a, Return b -> Var.equal a b
722+
| Expr a, Expr b -> Poly.equal a b
723+
| Branch c1, Branch c2 -> Poly.equal c1 c2
724+
| Return _, (Expr _ | Branch _)
725+
| Expr _, (Return _ | Branch _)
726+
| Branch _, (Return _ | Expr _) -> false
727+
728+
let hash = function
729+
| Return x -> Var.idx x
730+
| Branch c1 -> Hashtbl.hash c1
731+
| Expr x -> Hashtbl.hash x
732+
end
733+
734+
module SBT = Hashtbl.Make (Simple_block)
735+
736+
let eval_branch blocks update_branch info l =
714737
match l with
715738
| Cond (x, ftrue, ffalse) as b -> (
716739
match the_cond_of info x with
@@ -721,13 +744,43 @@ let eval_branch update_branch info l =
721744
incr update_branch;
722745
Branch ftrue
723746
| Unknown -> b)
724-
| Switch (x, a) as b -> (
747+
| Switch (x, a) -> (
725748
match the_cont_of info x a with
726749
| Some cont ->
727750
incr update_branch;
728751
Branch cont
729-
| None -> b)
730-
| _ as b -> b
752+
| None ->
753+
let t = SBT.create 18 in
754+
Switch
755+
( x
756+
, Array.map a ~f:(function
757+
| pc, [] -> (
758+
let block = Code.Addr.Map.find pc blocks in
759+
let sb =
760+
match block with
761+
| { body = [] | [ Event _ ]; branch = Return x; params = [] } ->
762+
Some (Simple_block.Return x)
763+
| { body = [ Let (x, e) ] | [ Event _; Let (x, e) ]
764+
; branch = Return x'
765+
; params = []
766+
}
767+
when Var.equal x x' -> Some (Simple_block.Expr e)
768+
| { body = [] | [ Event _ ]; branch = Branch c1; params = [] } ->
769+
Some (Simple_block.Branch c1)
770+
| _ -> None
771+
in
772+
match sb with
773+
| None -> pc, []
774+
| Some sb -> (
775+
match SBT.find_opt t sb with
776+
| Some pc' when pc' <> pc ->
777+
incr update_branch;
778+
pc', []
779+
| Some _ | None ->
780+
SBT.add t sb pc;
781+
pc, []))
782+
| cont -> cont) ))
783+
| cont -> cont
731784

732785
exception May_raise
733786

@@ -808,7 +861,7 @@ let eval update_count update_branch inline_constant ~target info blocks =
808861
block.body
809862
~f:(eval_instr update_count inline_constant ~target info)
810863
in
811-
let branch = eval_branch update_branch info block.branch in
864+
let branch = eval_branch blocks update_branch info block.branch in
812865
{ block with Code.body; Code.branch })
813866
blocks
814867

compiler/tests-compiler/cond.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ let rip_relative_kind_beq x y =
104104
function rip_relative_kind_beq(x, y){
105105
switch(x){
106106
case 0:
107-
switch(y){case 0: return 1;case 1: return 0;default: return 0;}
107+
return 0 === y ? 1 : 0;
108108
case 1:
109-
switch(y){case 0: return 0;case 1: return 1;default: return 0;}
110-
default: switch(y){case 0: return 0;case 1: return 0;default: return 1;}
109+
return 1 === y ? 1 : 0;
110+
default: return 2 === y ? 1 : 0;
111111
}
112112
}
113113
//end

0 commit comments

Comments
 (0)