@@ -696,13 +696,13 @@ let rec push_negation (e : t) : t option =
696
696
| _ -> None
697
697
698
698
(* *
699
- [simplify_and e1 e2] attempts to simplify the boolean AND expression [e1 && e2].
699
+ [simplify_and_ e1 e2] attempts to simplify the boolean AND expression [e1 && e2].
700
700
Returns [Some simplified] if simplification is possible, [None] otherwise.
701
701
702
702
Basic simplification rules:
703
703
- [false && e] -> [false]
704
704
- [true && e] -> [e]
705
- - [e && false] -> [false] If [e] has no side effects
705
+ - [e && false] -> [false]
706
706
- [e && true] -> [e]
707
707
- [(a && b) && e] -> If either [a && e] or [b && e] can be simplified,
708
708
creates new AND expression with simplified parts: [(a' && b')]
@@ -735,42 +735,42 @@ let rec push_negation (e : t) : t option =
735
735
attempting to reduce it to a simpler form. If no simplification is possible,
736
736
returns [None].
737
737
*)
738
- let rec simplify_and ~n (e1 : t ) (e2 : t ) : t option =
738
+ let rec simplify_and_ ~n (e1 : t ) (e2 : t ) : t option =
739
739
if debug then
740
740
Printf. eprintf " %s simplify_and %s %s\n "
741
741
(String. make (n * 2 ) ' ' )
742
742
(! string_of_expression e1) (! string_of_expression e2);
743
743
let res =
744
744
match (e1.expression_desc, e2.expression_desc) with
745
745
| Bool false , _ -> Some false_
746
- | _ , Bool false when no_side_effect e1 -> Some false_
746
+ | _ , Bool false -> Some false_
747
747
| Bool true , _ -> Some e2
748
748
| _ , Bool true -> Some e1
749
749
| Bin (And, a , b ), _ -> (
750
- let ao = simplify_and ~n: (n + 1 ) a e2 in
751
- let bo = simplify_and ~n: (n + 1 ) b e2 in
750
+ let ao = simplify_and_ ~n: (n + 1 ) a e2 in
751
+ let bo = simplify_and_ ~n: (n + 1 ) b e2 in
752
752
match (ao, bo) with
753
753
| None , None -> (
754
- match simplify_and ~n: (n + 1 ) a b with
754
+ match simplify_and_ ~n: (n + 1 ) a b with
755
755
| None -> None
756
756
| Some e -> simplify_and_force ~n: (n + 1 ) e e2)
757
757
| Some a_ , None -> simplify_and_force ~n: (n + 1 ) a_ b
758
758
| None , Some b_ -> simplify_and_force ~n: (n + 1 ) a b_
759
759
| Some a_ , Some b_ -> simplify_and_force ~n: (n + 1 ) a_ b_)
760
760
| _ , Bin (And, a , b ) ->
761
- simplify_and ~n: (n + 1 )
761
+ simplify_and_ ~n: (n + 1 )
762
762
{expression_desc = Bin (And , e1, a); comment = None }
763
763
b
764
764
| Bin (Or, a , b ), _ -> (
765
- let ao = simplify_and ~n: (n + 1 ) a e2 in
766
- let bo = simplify_and ~n: (n + 1 ) b e2 in
765
+ let ao = simplify_and_ ~n: (n + 1 ) a e2 in
766
+ let bo = simplify_and_ ~n: (n + 1 ) b e2 in
767
767
match (ao, bo) with
768
768
| Some {expression_desc = Bool false } , None ->
769
769
Some {expression_desc = Bin (And , b, e2); comment = None }
770
770
| None , Some {expression_desc = Bool false } ->
771
771
Some {expression_desc = Bin (And , a, e2); comment = None }
772
772
| None , _ | _ , None -> (
773
- match simplify_or ~n: (n + 1 ) a b with
773
+ match simplify_or_ ~n: (n + 1 ) a b with
774
774
| None -> None
775
775
| Some e -> simplify_and_force ~n: (n + 1 ) e e2)
776
776
| Some a_ , Some b_ -> simplify_or_force ~n: (n + 1 ) a_ b_)
@@ -1037,12 +1037,12 @@ let rec simplify_and ~n (e1 : t) (e2 : t) : t option =
1037
1037
res
1038
1038
1039
1039
and simplify_and_force ~n (e1 : t ) (e2 : t ) : t option =
1040
- match simplify_and ~n e1 e2 with
1040
+ match simplify_and_ ~n e1 e2 with
1041
1041
| None -> Some {expression_desc = Bin (And , e1, e2); comment = None }
1042
1042
| x -> x
1043
1043
1044
1044
(* *
1045
- [simplify_or e1 e2] attempts to simplify the boolean OR expression [e1 || e2].
1045
+ [simplify_or_ e1 e2] attempts to simplify the boolean OR expression [e1 || e2].
1046
1046
Returns [Some simplified] if simplification is possible, [None] otherwise.
1047
1047
1048
1048
Basic simplification rules:
@@ -1061,7 +1061,7 @@ and simplify_and_force ~n (e1 : t) (e2 : t) : t option =
1061
1061
This transformation allows reuse of [simplify_and]'s more extensive optimizations
1062
1062
in the context of OR expressions.
1063
1063
*)
1064
- and simplify_or ~n (e1 : t ) (e2 : t ) : t option =
1064
+ and simplify_or_ ~n (e1 : t ) (e2 : t ) : t option =
1065
1065
if debug then
1066
1066
Printf. eprintf " %ssimplify_or %s %s\n "
1067
1067
(String. make (n * 2 ) ' ' )
@@ -1076,7 +1076,7 @@ and simplify_or ~n (e1 : t) (e2 : t) : t option =
1076
1076
| _ -> (
1077
1077
match (push_negation e1, push_negation e2) with
1078
1078
| Some e1_ , Some e2_ -> (
1079
- match simplify_and ~n: (n + 1 ) e1_ e2_ with
1079
+ match simplify_and_ ~n: (n + 1 ) e1_ e2_ with
1080
1080
| Some e -> push_negation e
1081
1081
| None -> None )
1082
1082
| _ -> None )
@@ -1091,10 +1091,18 @@ and simplify_or ~n (e1 : t) (e2 : t) : t option =
1091
1091
res
1092
1092
1093
1093
and simplify_or_force ~n (e1 : t ) (e2 : t ) : t option =
1094
- match simplify_or ~n e1 e2 with
1094
+ match simplify_or_ ~n e1 e2 with
1095
1095
| None -> Some {expression_desc = Bin (Or , e1, e2); comment = None }
1096
1096
| x -> x
1097
1097
1098
+ let simplify_and (e1 : t ) (e2 : t ) : t option =
1099
+ if no_side_effect e1 && no_side_effect e2 then simplify_and_ ~n: 0 e1 e2
1100
+ else None
1101
+
1102
+ let simplify_or (e1 : t ) (e2 : t ) : t option =
1103
+ if no_side_effect e1 && no_side_effect e2 then simplify_or_ ~n: 0 e1 e2
1104
+ else None
1105
+
1098
1106
let and_ ?comment (e1 : t ) (e2 : t ) : t =
1099
1107
match (e1.expression_desc, e2.expression_desc) with
1100
1108
| Var i , Var j when Js_op_util. same_vident i j -> e1
@@ -1111,7 +1119,7 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
1111
1119
when Js_op_util. same_vident i j ->
1112
1120
e2
1113
1121
| _ , _ -> (
1114
- match simplify_and ~n: 0 e1 e2 with
1122
+ match simplify_and e1 e2 with
1115
1123
| Some e -> e
1116
1124
| None -> {expression_desc = Bin (And , e1, e2); comment})
1117
1125
@@ -1125,7 +1133,7 @@ let or_ ?comment (e1 : t) (e2 : t) =
1125
1133
when Js_op_util. same_vident i j ->
1126
1134
{e2 with expression_desc = Bin (Or , r, l)}
1127
1135
| _ , _ -> (
1128
- match simplify_or ~n: 0 e1 e2 with
1136
+ match simplify_or e1 e2 with
1129
1137
| Some e -> e
1130
1138
| None -> {expression_desc = Bin (Or , e1, e2); comment})
1131
1139
@@ -1190,11 +1198,11 @@ let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
1190
1198
Seq (b, {expression_desc = Undefined _}) ) ->
1191
1199
seq (econd ?comment pred a b) undefined
1192
1200
| _ , _ , Bool false -> (
1193
- match simplify_and ~n: 0 pred ifso with
1201
+ match simplify_and pred ifso with
1194
1202
| Some e -> e
1195
1203
| None -> default () )
1196
1204
| _ , Bool false , _ -> (
1197
- match simplify_and ~n: 0 (not pred) ifnot with
1205
+ match simplify_and (not pred) ifnot with
1198
1206
| Some e -> e
1199
1207
| None -> default () )
1200
1208
| _ -> default ()
0 commit comments