Skip to content

Commit 6d8439a

Browse files
committed
add optimization of comparisons
1 parent 11a1719 commit 6d8439a

17 files changed

+118
-6
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
787787
float_equal ?comment a e1
788788
| Number (Float { f = f0; _ }), Number (Float { f = f1 }) when f0 = f1 ->
789789
true_
790+
| Number (Bigint { i = i0 }), Number (Bigint { i = i1 }) -> bool (i0 = i1)
790791
| _ -> { expression_desc = Bin (EqEqEq, e0, e1); comment }
791792

792793
let int_equal = float_equal
@@ -1269,6 +1270,16 @@ let bigint_div ?comment (e1: t) (e2: t) = bin ?comment Div e1 e2
12691270

12701271
let bigint_mod ?comment (e1: t) (e2: t) = bin ?comment Mod e1 e2
12711272

1273+
let bigint_comp (cmp : Lam_compat.comparison) ?comment (e0: t) (e1: t) =
1274+
match (cmp, e0.expression_desc, e1.expression_desc) with
1275+
| Ceq, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 = i1)
1276+
| Cneq, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 <> i1)
1277+
| Cge, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 >= i1)
1278+
| Cgt, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 > i1)
1279+
| Cle, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 <= i1)
1280+
| Clt, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 < i1)
1281+
| _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1282+
12721283
(* TODO -- alpha conversion
12731284
remember to add parens..
12741285
*)

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ val bigint_div : ?comment: string -> t -> t -> t
286286

287287
val bigint_mod : ?comment: string -> t -> t -> t
288288

289+
val bigint_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t
290+
289291
val js_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t
290292

291293
val not : t -> t

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
7676
(* Float operations *)
7777
| Pintoffloat | Pfloatofint | Pnegfloat
7878
(* | Pabsfloat *)
79-
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat | Pfloatcomp _ | Pjscomp _
79+
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat | Pfloatcomp _ | Pbigintcomp _ | Pjscomp _
8080
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint
8181
(* String operations *)
8282
| Pstringlength | Pstringrefu | Pstringrefs | Pbyteslength | Pbytesrefu

jscomp/core/lam_compile_primitive.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
251251
[Not_found] or [Invalid_argument] ?
252252
*)
253253
match args with [ e1; e2 ] -> E.int_comp cmp e1 e2 | _ -> assert false)
254+
| Pbigintcomp cmp -> (
255+
match args with [ e1; e2 ] -> E.bigint_comp cmp e1 e2 | _ -> assert false)
254256
(* List --> stamp = 0
255257
Assert_false --> stamp = 26
256258
*)

jscomp/core/lam_convert.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
260260
| Pmulbigint -> prim ~primitive:Pmulbigint ~args loc
261261
| Pdivbigint _is_safe (*FIXME*) -> prim ~primitive:Pdivbigint ~args loc
262262
| Pmodbigint _is_safe (*FIXME*) -> prim ~primitive:Pmodbigint ~args loc
263+
| Pbigintcomp x -> prim ~primitive:(Pbigintcomp x) ~args loc
263264
| Pintcomp x -> prim ~primitive:(Pintcomp x) ~args loc
264265
| Poffsetint x -> prim ~primitive:(Poffsetint x) ~args loc
265266
| Poffsetref x -> prim ~primitive:(Poffsetref x) ~args loc

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
123123
| "caml_float_equal_null" | "caml_float_equal_nullable"
124124
| "caml_float_equal_undefined" -> (
125125
match args with [ e0; e1 ] -> E.float_comp Ceq e0 e1 | _ -> assert false)
126+
| "caml_bigint_equal_null" | "caml_bigint_equal_nullable"
127+
| "caml_bigint_equal_undefined" -> (
128+
match args with [ e0; e1 ] -> E.bigint_comp Ceq e0 e1 | _ -> assert false)
126129
| "caml_string_equal_null" | "caml_string_equal_nullable"
127130
| "caml_string_equal_undefined" -> (
128131
match args with
@@ -137,8 +140,9 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
137140
| "caml_int_compare" ->
138141
E.runtime_call Js_runtime_modules.caml_primitive "int_compare" args
139142
| "caml_float_compare" -> call Js_runtime_modules.caml_primitive
143+
| "caml_bigint_compare" -> call Js_runtime_modules.caml_primitive
140144
| "caml_string_compare" -> call Js_runtime_modules.caml_primitive
141-
| "caml_bool_min" | "caml_int_min" | "caml_float_min" | "caml_string_min" -> (
145+
| "caml_bool_min" | "caml_int_min" | "caml_float_min" | "caml_bigint_min" | "caml_string_min" -> (
142146
match args with
143147
| [ a; b ] ->
144148
if
@@ -147,7 +151,7 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
147151
then E.econd (E.js_comp Clt a b) a b
148152
else call Js_runtime_modules.caml_primitive
149153
| _ -> assert false)
150-
| "caml_bool_max" | "caml_int_max" | "caml_float_max" | "caml_string_max" -> (
154+
| "caml_bool_max" | "caml_int_max" | "caml_float_max" | "caml_bigint_max" | "caml_string_max" -> (
151155
match args with
152156
| [ a; b ] ->
153157
if

jscomp/core/lam_primitive.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type t =
9090
| Pfloatcomp of Lam_compat.comparison
9191
| Pjscomp of Lam_compat.comparison
9292
| Pint64comp of Lam_compat.comparison
93+
| Pbigintcomp of Lam_compat.comparison
9394
| Pjs_apply (*[f;arg0;arg1; arg2; ... argN]*)
9495
| Pjs_runtime_apply (* [f; [...]] *)
9596
(* String operations *)
@@ -281,6 +282,11 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
281282
| Pfloatcomp comparison1 ->
282283
Lam_compat.eq_comparison comparison comparison1
283284
| _ -> false)
285+
| Pbigintcomp comparison -> (
286+
match rhs with
287+
| Pbigintcomp comparison1 ->
288+
Lam_compat.eq_comparison comparison comparison1
289+
| _ -> false)
284290
| Pjscomp comparison -> (
285291
match rhs with
286292
| Pjscomp comparison1 -> Lam_compat.eq_comparison comparison comparison1

jscomp/core/lam_primitive.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type t =
8080
| Pfloatcomp of Lam_compat.comparison
8181
| Pjscomp of Lam_compat.comparison
8282
| Pint64comp of Lam_compat.comparison
83+
| Pbigintcomp of Lam_compat.comparison
8384
| Pjs_apply (*[f;arg0;arg1; arg2; ... argN]*)
8485
| Pjs_runtime_apply (* [f; [...]] *)
8586
| Pstringlength

jscomp/core/lam_print.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ let primitive ppf (prim : Lam_primitive.t) =
140140
| Pmulbigint -> fprintf ppf "*n"
141141
| Pdivbigint -> fprintf ppf "/n"
142142
| Pmodbigint -> fprintf ppf "modn"
143+
| Pbigintcomp Ceq -> fprintf ppf "==,"
144+
| Pbigintcomp Cneq -> fprintf ppf "!=,"
145+
| Pbigintcomp Clt -> fprintf ppf "<,"
146+
| Pbigintcomp Cle -> fprintf ppf "<=,"
147+
| Pbigintcomp Cgt -> fprintf ppf ">,"
148+
| Pbigintcomp Cge -> fprintf ppf ">=,"
143149
| Pjscomp Ceq -> fprintf ppf "#=="
144150
| Pjscomp Cneq -> fprintf ppf "#!="
145151
| Pjscomp Clt -> fprintf ppf "#<"

jscomp/ml/lambda.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ type primitive =
230230
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
231231
| Pfloatcomp of comparison
232232
(* Bigint operations *)
233-
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint | Pdivbigint of is_safe | Pmodbigint of is_safe
233+
| Pnegbigint | Paddbigint | Psubbigint
234+
| Pmulbigint | Pdivbigint of is_safe | Pmodbigint of is_safe
235+
| Pbigintcomp of comparison
234236
(* String operations *)
235237
| Pstringlength | Pstringrefu | Pstringrefs
236238
| Pbyteslength | Pbytesrefu | Pbytessetu | Pbytesrefs | Pbytessets

0 commit comments

Comments
 (0)