summaryrefslogtreecommitdiff
path: root/compiler/PureMicroPasses.ml
diff options
context:
space:
mode:
authorSon Ho2023-12-21 20:00:35 +0100
committerSon Ho2023-12-21 20:00:35 +0100
commit6ee1063d98d82f6a3c0cf017834ec81cf012f0a1 (patch)
treefd95567234f60415da80954c02adb46a7aa06a86 /compiler/PureMicroPasses.ml
parent0fb89f21a302210aa284e54a10129c46dbe8b4b5 (diff)
Improve PureMicroPasses.filter_useless to simplify the matches
Diffstat (limited to 'compiler/PureMicroPasses.ml')
-rw-r--r--compiler/PureMicroPasses.ml15
1 files changed, 14 insertions, 1 deletions
diff --git a/compiler/PureMicroPasses.ml b/compiler/PureMicroPasses.ml
index 7babe95b..156fba29 100644
--- a/compiler/PureMicroPasses.ml
+++ b/compiler/PureMicroPasses.ml
@@ -1052,10 +1052,23 @@ let filter_useless (filter_monadic_calls : bool) (ctx : trans_ctx)
method! visit_expression env e =
match e with
| Var _ | CVar _ | Const _ | App _ | Qualif _
- | Switch (_, _)
| Meta (_, _)
| StructUpdate _ | Lambda _ ->
super#visit_expression env e
+ | Switch (scrut, switch) -> (
+ match switch with
+ | If (_, _) -> super#visit_expression env e
+ | Match branches ->
+ (* Simplify the branches *)
+ let simplify_branch (br : match_branch) =
+ (* Compute the set of values used inside the branch *)
+ let branch, used = self#visit_texpression env br.branch in
+ (* Simplify the pattern *)
+ let pat, _ = filter_typed_pattern (used ()) br.pat in
+ { pat; branch }
+ in
+ super#visit_expression env
+ (Switch (scrut, Match (List.map simplify_branch branches))))
| Let (monadic, lv, re, e) ->
(* Compute the set of values used in the next expression *)
let e, used = self#visit_texpression env e in