Skip to content

Commit 6c8577c

Browse files
committed
Compiler: optmize cond more
1 parent c6b3095 commit 6c8577c

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

compiler/lib/eval.ml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,39 @@ let the_case_of info x =
299299
| Pc (Tuple (j, _, _)) -> CTag j
300300
| _ -> Unknown
301301

302+
type cond_of =
303+
| Zero
304+
| Non_zero
305+
| Unknown
306+
307+
let the_cond_of info x =
308+
get_approx
309+
info
310+
(fun x ->
311+
match info.info_defs.(Var.idx x) with
312+
| Expr (Constant (Int 0l | Float 0.)) -> Zero
313+
| Expr
314+
(Constant
315+
(Int _ | Float _ | Tuple _ | String _ | IString _ | Float_array _ | Int64 _))
316+
->
317+
Non_zero
318+
| Expr (Block (_, _, _)) -> Non_zero
319+
| Expr (Field _ | Closure _ | Prim _ | Apply _) -> Unknown
320+
| Param | Phi _ -> Unknown)
321+
Unknown
322+
(fun u v ->
323+
match u, v with
324+
| Zero, Zero -> Zero
325+
| Non_zero, Non_zero -> Non_zero
326+
| _ -> Unknown)
327+
x
328+
302329
let eval_branch info = function
303330
| Cond (x, ftrue, ffalse) as b -> (
304-
match the_int info (Pv x) with
305-
| Some 0l -> Branch ffalse
306-
| Some _ -> Branch ftrue
307-
| _ -> b)
331+
match the_cond_of info x with
332+
| Zero -> Branch ffalse
333+
| Non_zero -> Branch ftrue
334+
| Unknown -> b)
308335
| Switch (x, const, tags) as b -> (
309336
(* [the_case_of info (Pv x)] might be meaningless when we're inside a dead code.
310337
The proper fix would be to remove the deadcode entirely.

0 commit comments

Comments
 (0)