@@ -21,36 +21,39 @@ open! Stdlib
2121open Code
2222open Flow
2323
24- let rec function_cardinality info x acc =
25- get_approx
26- info
27- (fun x ->
28- match info.info_defs.(Var. idx x) with
29- | Expr (Closure (l , _ )) -> Some (List. length l)
30- | Expr (Prim (Extern "%closure" , [ Pc (String prim ) ])) -> (
31- try Some (Primitive. arity prim) with Not_found -> None )
32- | Expr (Apply { f; args; _ } ) -> (
33- if List. mem f ~set: acc
34- then None
35- else
36- match function_cardinality info f (f :: acc) with
37- | Some n ->
38- let diff = n - List. length args in
39- if diff > 0 then Some diff else None
40- | None -> None )
41- | _ -> None )
42- None
43- (fun u v ->
44- match u, v with
45- | Some n , Some m when n = m -> u
46- | _ -> None )
47- x
24+ let function_arity info x =
25+ let rec arity info x acc =
26+ get_approx
27+ info
28+ (fun x ->
29+ match info.info_defs.(Var. idx x) with
30+ | Expr (Closure (l , _ )) -> Some (List. length l)
31+ | Expr (Prim (Extern "%closure" , [ Pc (String prim ) ])) -> (
32+ try Some (Primitive. arity prim) with Not_found -> None )
33+ | Expr (Apply { f; args; _ } ) -> (
34+ if List. mem f ~set: acc
35+ then None
36+ else
37+ match arity info f (f :: acc) with
38+ | Some n ->
39+ let diff = n - List. length args in
40+ if diff > 0 then Some diff else None
41+ | None -> None )
42+ | _ -> None )
43+ None
44+ (fun u v ->
45+ match u, v with
46+ | Some n , Some m when n = m -> u
47+ | _ -> None )
48+ x
49+ in
50+ arity info x []
4851
49- let specialize_instr info (acc , free_pc , extra ) i =
52+ let specialize_instr function_arity (acc , free_pc , extra ) i =
5053 match i with
51- | Let (x , Apply { f; args; _ } ) when Config.Flag. optcall () -> (
54+ | Let (x , Apply { f; args; exact } ) when ( not exact) && Config.Flag. optcall () -> (
5255 let n' = List. length args in
53- match function_cardinality info f [] with
56+ match function_arity f with
5457 | None -> i :: acc, free_pc, extra
5558 | Some n when n = n' ->
5659 Let (x, Apply { f; args; exact = true }) :: acc, free_pc, extra
@@ -80,13 +83,13 @@ let specialize_instr info (acc, free_pc, extra) i =
8083 | _ -> i :: acc, free_pc, extra)
8184 | _ -> i :: acc, free_pc, extra
8285
83- let specialize_instrs info p =
86+ let specialize_instrs ~ function_arity p =
8487 let blocks, free_pc =
8588 Addr.Map. fold
8689 (fun pc block (blocks , free_pc ) ->
8790 let body, free_pc, extra =
8891 List. fold_right block.body ~init: ([] , free_pc, [] ) ~f: (fun i acc ->
89- specialize_instr info acc i)
92+ specialize_instr function_arity acc i)
9093 in
9194 let blocks =
9295 List. fold_left extra ~init: blocks ~f: (fun blocks (pc , b ) ->
@@ -98,4 +101,4 @@ let specialize_instrs info p =
98101 in
99102 { p with blocks; free_pc }
100103
101- let f info p = specialize_instrs info p
104+ let f = specialize_instrs
0 commit comments