18
18
19
19
open ! Simplify_import
20
20
21
- let rebuild_switch ~simplify_let dacc ~arms ~scrutinee ~scrutinee_ty uacc
22
- ~after_rebuild =
21
+ let rebuild_switch ~simplify_let : _ _dacc ~arms ~scrutinee ~scrutinee_ty
22
+ ~tagged_scrutinee ~ not_scrutinee uacc ~ after_rebuild =
23
23
let new_let_conts, arms, identity_arms, not_arms =
24
24
Target_imm.Map. fold
25
25
(fun arm (action , use_id , arity )
@@ -141,38 +141,6 @@ let rebuild_switch ~simplify_let dacc ~arms ~scrutinee ~scrutinee_ty uacc
141
141
|> Continuation.Set. of_list
142
142
|> Continuation.Set. get_singleton
143
143
in
144
- let create_tagged_scrutinee uacc dest ~make_body =
145
- (* A problem with using [simplify_let] below is that the continuation
146
- [dest] might have [Apply_cont_rewrite]s in the environment, left over
147
- from the simplification of the existing uses. We must clear these to
148
- avoid a lookup failure for our new [Apply_cont] when
149
- [Simplify_apply_cont] tries to rewrite the use. There is no need for
150
- the rewrites anyway; they have already been applied.
151
- Likewise, we need to clear the continuation uses environment for
152
- [dest] in [dacc], since our new [Apply_cont] might not match the
153
- original uses (e.g. if a parameter has been removed). *)
154
- let uacc =
155
- UA. map_uenv uacc ~f: (fun uenv ->
156
- UE. delete_apply_cont_rewrite uenv dest)
157
- in
158
- let dacc = DA. delete_continuation_uses dacc dest in
159
- let bound_to = Variable. create " tagged_scrutinee" in
160
- let body = make_body ~tagged_scrutinee: (Simple. var bound_to) in
161
- let bound_to = Var_in_binding_pos. create bound_to NM. normal in
162
- let defining_expr =
163
- Named. create_prim (Unary (Box_number Untagged_immediate , scrutinee))
164
- Debuginfo. none
165
- in
166
- let let_expr =
167
- Let. create (Bindable_let_bound. singleton bound_to)
168
- defining_expr
169
- ~body
170
- ~free_names_of_body: Unknown
171
- in
172
- simplify_let dacc let_expr
173
- ~down_to_up: (fun _dacc ~rebuild ->
174
- rebuild uacc ~after_rebuild: (fun expr uacc -> expr, uacc))
175
- in
176
144
(* CR mshinwell: Here and elsewhere [UA.name_occurrences] should be empty
177
145
(maybe except for closure vars? -- check). We should add asserts. *)
178
146
let body, uacc =
@@ -185,37 +153,29 @@ let rebuild_switch ~simplify_let dacc ~arms ~scrutinee ~scrutinee_ty uacc
185
153
let dbg = Debuginfo. none in
186
154
match switch_is_identity with
187
155
| Some dest ->
156
+ let apply_cont = Apply_cont. create dest ~args: [tagged_scrutinee] ~dbg in
157
+ let uacc =
158
+ UA. map_uenv uacc ~f: (fun uenv ->
159
+ UE. delete_apply_cont_rewrite uenv dest)
160
+ in
188
161
let uacc =
189
- UA. notify_removed ~operation: Removed_operations. branch uacc
162
+ UA. add_free_names uacc (Apply_cont. free_names apply_cont) |>
163
+ UA. notify_removed ~operation: Removed_operations. branch
190
164
in
191
- create_tagged_scrutinee uacc dest ~make_body: (fun ~tagged_scrutinee ->
192
- (* No need to increment the cost_metrics inside [create_tagged_scrutinee] as it
193
- will call simplify over the result of [make_body]. *)
194
- Apply_cont. create dest ~args: [tagged_scrutinee] ~dbg
195
- |> Expr. create_apply_cont)
165
+ Rebuilt_expr. create_apply_cont apply_cont, uacc
196
166
| None ->
197
167
match switch_is_boolean_not with
198
168
| Some dest ->
169
+ let apply_cont = Apply_cont. create dest ~args: [not_scrutinee] ~dbg in
199
170
let uacc =
200
- UA. notify_removed ~operation: Removed_operations. branch uacc
171
+ UA. map_uenv uacc ~f: (fun uenv ->
172
+ UE. delete_apply_cont_rewrite uenv dest)
201
173
in
202
- create_tagged_scrutinee uacc dest ~make_body: (fun ~tagged_scrutinee ->
203
- let not_scrutinee = Variable. create " not_scrutinee" in
204
- let not_scrutinee' = Simple. var not_scrutinee in
205
- let do_tagging =
206
- Named. create_prim (P. Unary (Boolean_not , tagged_scrutinee))
207
- Debuginfo. none
208
- in
209
- let bound =
210
- VB. create not_scrutinee NM. normal
211
- |> Bindable_let_bound. singleton
212
- in
213
- let body =
214
- Apply_cont. create dest ~args: [not_scrutinee'] ~dbg
215
- |> Expr. create_apply_cont
216
- in
217
- Let. create bound do_tagging ~body ~free_names_of_body: Unknown
218
- |> Expr. create_let)
174
+ let uacc =
175
+ UA. add_free_names uacc (Apply_cont. free_names apply_cont) |>
176
+ UA. notify_removed ~operation: Removed_operations. branch
177
+ in
178
+ Rebuilt_expr. create_apply_cont apply_cont, uacc
219
179
| None ->
220
180
(* In that case, even though some branches were removed by simplify we
221
181
should not count them in the number of removed operations: these
@@ -260,7 +220,7 @@ let rebuild_switch ~simplify_let dacc ~arms ~scrutinee ~scrutinee_ty uacc
260
220
261
221
let simplify_switch_aux ~simplify_let
262
222
~scrutinee ~scrutinee_ty
263
- ~tagged_scrutinee : _ ~not_scrutinee : _
223
+ ~tagged_scrutinee ~not_scrutinee
264
224
dacc switch
265
225
~(down_to_up :
266
226
(Rebuilt_expr.t * Upwards_acc.t,
@@ -308,7 +268,7 @@ let simplify_switch_aux ~simplify_let
308
268
in
309
269
down_to_up dacc
310
270
~rebuild: (rebuild_switch ~simplify_let dacc ~arms ~scrutinee
311
- ~scrutinee_ty )
271
+ ~scrutinee_ty ~tagged_scrutinee ~not_scrutinee )
312
272
313
273
let simplify_switch
314
274
~(simplify_let :Flambda.Let.t Simplify_common.expr_simplifier )
0 commit comments