Skip to content

Commit 4e6889f

Browse files
committed
peephole if then else
1 parent e5d9444 commit 4e6889f

20 files changed

+107
-79
lines changed

jscomp/all.depend

+3-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ core/js_closure.cmi : ext/ident_set.cmi
349349
core/js_number.cmi :
350350
core/js_cmj_datasets.cmi : ext/string_map.cmi core/js_cmj_format.cmi
351351
core/lam_exit_code.cmi : core/lam.cmi
352-
core/lam_compile_util.cmi : core/js_op.cmx
352+
core/lam_compile_util.cmi : core/lam.cmi core/js_op.cmx
353353
core/lam_id_kind.cmi : core/lam_arity.cmi core/lam.cmi
354354
core/lam_stats.cmi : core/lam_id_kind.cmi ext/ident_set.cmi \
355355
ext/ident_hashtbl.cmi
@@ -481,7 +481,8 @@ core/js_cmj_datasets.cmx : ext/string_map.cmx core/js_cmj_format.cmx \
481481
core/lam_exit_code.cmx : core/lam.cmx core/lam_exit_code.cmi
482482
core/j.cmx : core/js_op.cmx core/js_fun_env.cmx core/js_closure.cmx \
483483
core/js_call_info.cmx ext/ident_set.cmx
484-
core/lam_compile_util.cmx : core/js_op.cmx core/lam_compile_util.cmi
484+
core/lam_compile_util.cmx : core/lam.cmx core/js_op.cmx \
485+
core/lam_compile_util.cmi
485486
core/lam_id_kind.cmx : core/lam_arity.cmx core/lam.cmx core/lam_id_kind.cmi
486487
core/lam_stats.cmx : core/lam_id_kind.cmx ext/ident_set.cmx \
487488
ext/ident_hashtbl.cmx core/lam_stats.cmi

jscomp/core/lam.ml

+25-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ type meth_kind = Lambda.meth_kind
5353
| Public of string option
5454
| Cached
5555

56+
type pointer_info =
57+
| Pt_constructor of string
58+
| Pt_variant of string
59+
| Pt_module_alias
60+
| Pt_na
61+
5662
type constant =
5763
| Const_js_null
5864
| Const_js_undefined
@@ -66,7 +72,7 @@ type constant =
6672
| Const_int32 of int32
6773
| Const_int64 of int64
6874
| Const_nativeint of nativeint
69-
| Const_pointer of int * Lambda.pointer_info
75+
| Const_pointer of int * pointer_info
7076
| Const_block of int * Lambda.tag_info * constant list
7177
| Const_float_array of string list
7278
| Const_immstring of string
@@ -1141,7 +1147,14 @@ let if_ (a : t) (b : t) c =
11411147
| Const_float_array _
11421148
| Const_immstring _ -> b
11431149
end
1144-
| _ -> Lifthenelse (a,b,c)
1150+
| _ ->
1151+
begin match a, b, c with
1152+
| Lprim {primitive = Pintcomp _;}, Lconst(Const_js_true), Lconst(Const_js_false)
1153+
-> a
1154+
| _ ->
1155+
1156+
Lifthenelse (a,b,c)
1157+
end
11451158

11461159

11471160
let abs_int x = if x < 0 then - x else x
@@ -1195,10 +1208,10 @@ let stringswitch (lam : t) cases default : t =
11951208

11961209

11971210
let true_ : t =
1198-
Lconst (Const_pointer ( 1, Pt_builtin_boolean))
1211+
Lconst (Const_js_true)
11991212

12001213
let false_ : t =
1201-
Lconst (Const_pointer( 0, Pt_builtin_boolean))
1214+
Lconst (Const_js_false)
12021215

12031216
let unit : t =
12041217
Lconst (Const_pointer( 0, Pt_constructor "()"))
@@ -1909,7 +1922,14 @@ let convert exports lam : _ * _ =
19091922
| Const_base (Const_int32 i) -> (Const_int32 i)
19101923
| Const_base (Const_int64 i) -> (Const_int64 i)
19111924
| Const_base (Const_nativeint i) -> (Const_nativeint i)
1912-
| Const_pointer(i,p) -> Const_pointer (i,p)
1925+
| Const_pointer(i,p) ->
1926+
begin match p with
1927+
| Pt_constructor p -> Const_pointer(i, Pt_constructor p)
1928+
| Pt_variant p -> Const_pointer(i,Pt_variant p)
1929+
| Pt_module_alias -> Const_pointer(i, Pt_module_alias)
1930+
| Pt_builtin_boolean -> if i = 0 then Const_js_false else Const_js_true
1931+
| Pt_na -> Const_pointer(i, Pt_na)
1932+
end
19131933
| Const_float_array (s) -> Const_float_array(s)
19141934
| Const_immstring s -> Const_immstring s
19151935
| Const_block (i,t,xs) ->

jscomp/core/lam.mli

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type function_kind
5151
= Curried
5252
(* | Tupled *)
5353

54+
type pointer_info =
55+
| Pt_constructor of string
56+
| Pt_variant of string
57+
| Pt_module_alias
58+
| Pt_na
59+
5460
type constant =
5561
| Const_js_null
5662
| Const_js_undefined
@@ -64,7 +70,7 @@ type constant =
6470
| Const_int32 of int32
6571
| Const_int64 of int64
6672
| Const_nativeint of nativeint
67-
| Const_pointer of int * Lambda.pointer_info
73+
| Const_pointer of int * pointer_info
6874
| Const_block of int * Lambda.tag_info * constant list
6975
| Const_float_array of string list
7076
| Const_immstring of string

jscomp/core/lam_analysis.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,5 +621,5 @@ and eq_primitive ( lhs : Lam.primitive) (rhs : Lam.primitive) =
621621
let safe_to_inline (lam : Lam.t) =
622622
match lam with
623623
| Lfunction _ -> true
624-
| Lconst (Const_pointer _ | Const_immstring _ ) -> true
624+
| Lconst (Const_pointer _ | Const_immstring _ | Const_js_true | Const_js_false) -> true
625625
| _ -> false

jscomp/core/lam_compile_const.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,10 @@ let rec translate (x : Lam.constant ) : J.expression =
6868
E.unicode i
6969
(* E.str i ~delimiter:Literals.escaped_j_delimiter *)
7070

71-
| Const_pointer (c,pointer_info) ->
72-
begin match pointer_info with
73-
| Pt_builtin_boolean ->
74-
E.bool (c <> 0)
75-
| _ ->
71+
| Const_pointer (c,pointer_info) ->
7672
E.int ?comment:(Lam_compile_util.comment_of_pointer_info pointer_info)
7773
(Int32.of_int c )
78-
end
74+
7975
| Const_block(tag, tag_info, xs ) ->
8076
Js_of_lam_block.make_block NA tag_info
8177
(E.small_int tag) (Ext_list.map translate xs)

jscomp/core/lam_compile_util.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ let comment_of_tag_info (x : Lambda.tag_info) =
5454
| Blk_exception -> Some "exception"
5555
| Blk_extension -> Some "extension"
5656
| Blk_na -> None
57-
let comment_of_pointer_info (x : Lambda.pointer_info)=
57+
let comment_of_pointer_info (x : Lam.pointer_info)=
5858
match x with
5959
| Pt_constructor x -> Some x
6060
| Pt_variant x -> Some x
61-
| Pt_builtin_boolean -> Some "boolean"
62-
| Lambda.Pt_module_alias -> None (* FIXME *)
61+
| Pt_module_alias -> None (* FIXME *)
6362
| Pt_na -> None

jscomp/core/lam_compile_util.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ val jsop_of_comp : Lambda.comparison -> Js_op.binop
3636

3737
val comment_of_tag_info : Lambda.tag_info -> string option
3838

39-
val comment_of_pointer_info : Lambda.pointer_info -> string option
39+
val comment_of_pointer_info : Lam.pointer_info -> string option

jscomp/core/lam_eta_conversion.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let transform_under_supply n loc status fn args =
4646
| Const_char _ | Const_string _
4747
| Const_float _ | Const_int32 _
4848
| Const_int64 _ | Const_nativeint _
49-
| Const_pointer _ | Const_immstring _ )
49+
| Const_pointer _ | Const_immstring _ | Const_js_true | Const_js_false)
5050
| Lprim {primitive = Pfield _;
5151
args = [ Lglobal_module _ ]; _ }
5252
| Lfunction _

jscomp/core/lam_pass_lets_dce.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam =
5353
((
5454
Const_int _ | Const_char _ | Const_float _ | Const_int32 _
5555
| Const_nativeint _ )
56-
| Const_pointer _ ) (* could be poly-variant [`A] -> [65a]*)
56+
| Const_pointer _ |Const_js_true | Const_js_false) (* could be poly-variant [`A] -> [65a]*)
5757
| Lprim {primitive = Pfield (_);
5858
args = [
5959
Lglobal_module _

jscomp/test/compare_test.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ function compare2(x, y) {
2828
return true;
2929
}
3030
case 2 :
31-
if (y >= 2) {
32-
return true;
33-
} else {
34-
return false;
35-
}
31+
return y >= 2;
3632

3733
}
3834
}

jscomp/test/ext_string_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ function is_valid_npm_package_name(s) {
537537
} else {
538538
return true;
539539
}
540-
} else if (x !== 45 && x < 48) {
541-
return false;
540+
} else if (x !== 45) {
541+
return x >= 48;
542542
} else {
543543
return true;
544544
}

jscomp/test/mario_game.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ function col_bypass(c1, c2) {
12381238
var ctypes;
12391239
switch (c1.tag | 0) {
12401240
case 0 :
1241-
ctypes = c2.tag === 1 && c1[2][/* invuln */7] > 0 ? true : false;
1241+
ctypes = c2.tag === 1 ? c1[2][/* invuln */7] > 0 : false;
12421242
break;
12431243
case 1 :
12441244
ctypes = c2.tag === 2 ? true : false;

jscomp/test/ocaml_re_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1953,8 +1953,8 @@ function equal$2(_x1, _x2) {
19531953
return false;
19541954
}
19551955
case 9 :
1956-
if (typeof x2 === "number" && x2 >= 9) {
1957-
return true;
1956+
if (typeof x2 === "number") {
1957+
return x2 >= 9;
19581958
} else {
19591959
return false;
19601960
}

jscomp/test/ocaml_typedtree_test.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -24264,8 +24264,8 @@ function in_pervasives(p) {
2426424264

2426524265
function is_datatype(decl) {
2426624266
var match = decl[/* type_kind */2];
24267-
if (typeof match === "number" && match === 0) {
24268-
return false;
24267+
if (typeof match === "number") {
24268+
return match !== 0;
2426924269
} else {
2427024270
return true;
2427124271
}
@@ -35150,17 +35150,11 @@ function parenthesized_ident(name) {
3515035150
} else {
3515135151
return true;
3515235152
}
35153-
} else if (match >= 123) {
35154-
return true;
3515535153
} else {
35156-
return false;
35154+
return match >= 123;
3515735155
}
3515835156
} else if (match >= 91) {
35159-
if (match !== 95) {
35160-
return true;
35161-
} else {
35162-
return false;
35163-
}
35157+
return match !== 95;
3516435158
} else if (match >= 65) {
3516535159
return false;
3516635160
} else {
@@ -41505,10 +41499,10 @@ function same_path(t, t$prime) {
4150541499
var exit = 0;
4150641500
if (typeof s1 === "number" || s1.tag) {
4150741501
exit = 1;
41508-
} else if (typeof s2 === "number" || !(!s2.tag && s1[0] === s2[0])) {
41502+
} else if (typeof s2 === "number" || s2.tag) {
4150941503
return false;
4151041504
} else {
41511-
return true;
41505+
return s1[0] === s2[0];
4151241506
}
4151341507
if (exit === 1) {
4151441508
var exit$1 = 0;
@@ -58357,11 +58351,7 @@ function is_nonexpansive(_exp) {
5835758351
_exp = match[1];
5835858352
continue ;
5835958353
case 19 :
58360-
if (class_type_arity(match[2][/* cty_type */1]) > 0) {
58361-
return true;
58362-
} else {
58363-
return false;
58364-
}
58354+
return class_type_arity(match[2][/* cty_type */1]) > 0;
5836558355
case 23 :
5836658356
if (is_nonexpansive_mod(match[2])) {
5836758357
_exp = match[3];

jscomp/test/pipe_syntax.js

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function f7(a) {
7474
];
7575
}
7676

77+
function f8(a) {
78+
return /* Some */[/* Some */[a]];
79+
}
80+
7781
exports.t0 = t0;
7882
exports.t1 = t1;
7983
exports.t2 = t2;
@@ -86,4 +90,5 @@ exports.f4 = f4;
8690
exports.f5 = f5;
8791
exports.f6 = f6;
8892
exports.f7 = f7;
93+
exports.f8 = f8;
8994
/* No side effect */

jscomp/test/pipe_syntax.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ let f5 a b c d =
5050

5151
let f6 a = a |. Some
5252

53-
let f7 a = a |. (Some, Some, Some)
53+
let f7 a = a |. (Some, Some, Some)
54+
55+
let f8 a = a |. Some |. Some

jscomp/test/testing.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ function failure_test(f, x, s) {
145145

146146
function scan_failure_test(f, x) {
147147
return test_raises_exc_p((function (param) {
148-
if (param[0] === Scanf.Scan_failure) {
149-
return true;
150-
} else {
151-
return false;
152-
}
148+
return param[0] === Scanf.Scan_failure;
153149
}), f, x);
154150
}
155151

lib/js/bytes.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ function is_space(param) {
166166
} else {
167167
return true;
168168
}
169-
} else if (switcher !== 2) {
170-
return true;
171169
} else {
172-
return false;
170+
return switcher !== 2;
173171
}
174172
}
175173

lib/js/string.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ function is_space(param) {
7272
} else {
7373
return true;
7474
}
75-
} else if (switcher !== 2) {
76-
return true;
7775
} else {
78-
return false;
76+
return switcher !== 2;
7977
}
8078
}
8179

0 commit comments

Comments
 (0)