Skip to content

Commit 40fc40b

Browse files
committed
Remove back-end switch optimization that relies on numeric values.
The only one that could still make sense is for the identity function, but that would be a strange optimization. Better have it earlier on, and apply to partial cases too, where it's mostly the identity.
1 parent 50f4d60 commit 40fc40b

File tree

5 files changed

+693
-20
lines changed

5 files changed

+693
-20
lines changed

jscomp/core/lam_convert.ml

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,24 @@ let lam_is_var (x : Lam.t) (y : Ident.t) =
119119
(** Make sure no int range overflow happens
120120
also we only check [int]
121121
*)
122-
let happens_to_be_diff (sw_consts : (int * Lambda.lambda) list) : int option =
122+
let happens_to_be_diff (sw_consts : (int * Lambda.lambda) list) sw_names : int option =
123123
match sw_consts with
124124
| ( a,
125-
Lconst (Const_pointer (a0, Pt_constructor _) | Const_base (Const_int a0))
125+
Lconst (Const_base (Const_int a0))
126126
)
127127
:: ( b,
128128
Lconst
129-
(Const_pointer (b0, Pt_constructor _) | Const_base (Const_int b0)) )
129+
(Const_base (Const_int b0)) )
130130
:: rest
131-
when no_over_flow a && no_over_flow a0 && no_over_flow b && no_over_flow b0
131+
when sw_names = None && no_over_flow a && no_over_flow a0 && no_over_flow b && no_over_flow b0
132132
->
133133
let diff = a0 - a in
134134
if b0 - b = diff then
135135
if
136136
Ext_list.for_all rest (fun (x, lam) ->
137137
match lam with
138138
| Lconst
139-
( Const_pointer (x0, Pt_constructor _)
140-
| Const_base (Const_int x0) )
139+
( Const_base (Const_int x0) )
141140
when no_over_flow x0 && no_over_flow x ->
142141
x0 - x = diff
143142
| _ -> false)
@@ -701,8 +700,9 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
701700
sw_numblocks = 0;
702701
sw_consts;
703702
sw_numconsts;
703+
sw_names;
704704
} -> (
705-
match happens_to_be_diff sw_consts with
705+
match happens_to_be_diff sw_consts sw_names with
706706
| Some 0 -> e
707707
| Some i ->
708708
prim ~primitive:Paddint
@@ -712,7 +712,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
712712
Lam.const (Const_int { i = Int32.of_int i; comment = None });
713713
]
714714
Location.none
715-
| None ->
715+
| _ ->
716716
Lam.switch e
717717
{
718718
sw_failaction = None;

jscomp/test/adt_optimize_test.js

+40-5
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,58 @@
22

33

44
function f(x) {
5-
return x + 1 | 0;
5+
switch (x) {
6+
case /* A */0 :
7+
return 1;
8+
case /* B */1 :
9+
return 2;
10+
case /* C */2 :
11+
return 3;
12+
13+
}
614
}
715

816
function f_0(x) {
9-
return x - 1 | 0;
17+
switch (x) {
18+
case /* A */0 :
19+
return -1;
20+
case /* B */1 :
21+
return 0;
22+
case /* C */2 :
23+
return 1;
24+
25+
}
1026
}
1127

1228
function f2(param) {
1329
if (param >= 3) {
1430
return /* T003 */3;
15-
} else {
16-
return param;
31+
}
32+
switch (param) {
33+
case 0 :
34+
return /* T000 */0;
35+
case 1 :
36+
return /* T001 */1;
37+
case 2 :
38+
return /* T002 */2;
39+
1740
}
1841
}
1942

2043
function f3(param) {
21-
return param;
44+
switch (param) {
45+
case /* X0 */0 :
46+
return /* Y0 */0;
47+
case /* X1 */1 :
48+
return /* Y1 */1;
49+
case /* X2 */2 :
50+
return /* Y2 */2;
51+
case /* X3 */3 :
52+
return /* Y3 */3;
53+
case /* X4 */4 :
54+
return /* Y4 */4;
55+
56+
}
2257
}
2358

2459
function f4(param) {

0 commit comments

Comments
 (0)