Skip to content

Commit 02c4cef

Browse files
committed
Retain block-structured local regions until Mach.
Insertion of explicit endregion primitives now occurs in Selectgen, rather than in Lambda. Early exit from regions (via static exceptions) is now permitted, with endregions inserted where appropriate.
1 parent 86dbe1c commit 02c4cef

38 files changed

+221
-180
lines changed

asmcomp/CSEgen.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ method class_of_operation op =
236236
| Ifloatofint | Iintoffloat -> Op_pure
237237
| Ispecific _ -> Op_other
238238
| Iname_for_debugger _ -> Op_pure
239+
| Ibeginregion | Iendregion -> Op_other
239240

240241
(* Operations that are so cheap that it isn't worth factoring them. *)
241242

asmcomp/afl_instrument.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ and instrument = function
8888
in
8989
Ccatch (isrec, cases, instrument body)
9090
| Cexit (ex, args) -> Cexit (ex, List.map instrument args)
91+
| Cregion e -> Cregion (instrument e)
9192

9293
(* these are base cases and have no logging *)
9394
| Cconst_int _ | Cconst_natint _ | Cconst_float _

asmcomp/amd64/CSE.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ method! class_of_operation op =
2929
begin match spec with
3030
| Ilea _ | Isextend32 | Izextend32 -> Op_pure
3131
| Istore_int(_, _, is_asg) -> Op_store is_asg
32-
| Iregionbegin -> Op_load
33-
| Iregionend -> Op_store true
3432
| Ioffset_loc(_, _) -> Op_store true
3533
| Ifloatarithmem _ | Ifloatsqrtf _ -> Op_load
3634
| Ibswap _ | Isqrtf -> super#class_of_operation op

asmcomp/amd64/arch.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ type specific_operation =
4646
extension *)
4747
| Izextend32 (* 32 to 64 bit conversion with zero
4848
extension *)
49-
| Iregionbegin
50-
| Iregionend
5149

5250
and float_operation =
5351
Ifloatadd | Ifloatsub | Ifloatmul | Ifloatdiv
@@ -135,10 +133,6 @@ let print_specific_operation printreg op ppf arg =
135133
fprintf ppf "sextend32 %a" printreg arg.(0)
136134
| Izextend32 ->
137135
fprintf ppf "zextend32 %a" printreg arg.(0)
138-
| Iregionbegin ->
139-
fprintf ppf "iregionbegin"
140-
| Iregionend ->
141-
fprintf ppf "iregionend %a" printreg arg.(0)
142136

143137
let win64 =
144138
match Config.system with

asmcomp/amd64/emit.mlp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ let emit_instr fallthrough i =
754754
I.movsxd (arg32 i 0) (res i 0)
755755
| Lop(Ispecific(Izextend32)) ->
756756
I.mov (arg32 i 0) (res32 i 0)
757-
| Lop(Ispecific(Iregionbegin)) ->
757+
| Lop(Ibeginregion) ->
758758
I.mov (domain_field Domainstate.Domain_local_sp) (res i 0)
759-
| Lop(Ispecific(Iregionend)) ->
759+
| Lop(Iendregion) ->
760760
I.mov (arg i 0) (domain_field Domainstate.Domain_local_sp)
761761
| Lop (Iname_for_debugger _) -> ()
762762
| Lreloadretaddr ->

asmcomp/amd64/proc.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ let op_is_pure = function
357357
| Iintop(Icheckbound) | Iintop_imm(Icheckbound, _) -> false
358358
| Ispecific(Ilea _|Isextend32|Izextend32) -> true
359359
| Ispecific _ -> false
360+
| Ibeginregion | Iendregion -> false
360361
| _ -> true
361362

362363
(* Layout of the stack frame *)

asmcomp/amd64/selection.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ method! select_operation op args dbg =
245245
Ispecific Izextend32, [arg]
246246
| _ -> super#select_operation op args dbg
247247
end
248-
| Cbeginregion -> Ispecific Iregionbegin, args
249-
| Cendregion -> Ispecific Iregionend, args
250248
| _ -> super#select_operation op args dbg
251249

252250
(* Recognize float arithmetic with mem *)

asmcomp/cmm.ml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ and operation =
165165
| Ccmpf of float_comparison
166166
| Craise of Lambda.raise_kind
167167
| Ccheckbound
168-
| Cbeginregion
169-
| Cendregion
170168

171169
type expression =
172170
Cconst_int of int * Debuginfo.t
@@ -195,6 +193,7 @@ type expression =
195193
| Cexit of int * expression list
196194
| Ctrywith of expression * Backend_var.With_provenance.t * expression
197195
* Debuginfo.t
196+
| Cregion of expression
198197

199198
type codegen_option =
200199
| Reduce_code_size
@@ -254,6 +253,9 @@ let iter_shallow_tail f = function
254253
f e1;
255254
f e2;
256255
true
256+
| Cregion e ->
257+
f e;
258+
true
257259
| Cexit _ | Cop (Craise _, _, _) ->
258260
true
259261
| Cconst_int _
@@ -290,6 +292,8 @@ let rec map_tail f = function
290292
Ccatch(rec_flag, List.map map_h handlers, map_tail f body)
291293
| Ctrywith(e1, id, e2, dbg) ->
292294
Ctrywith(map_tail f e1, id, map_tail f e2, dbg)
295+
| Cregion e ->
296+
Cregion(map_tail f e)
293297
| Cexit _ | Cop (Craise _, _, _) as cmm ->
294298
cmm
295299
| Cconst_int _
@@ -302,6 +306,41 @@ let rec map_tail f = function
302306
| Cop _ as c ->
303307
f c
304308

309+
let iter_shallow f = function
310+
| Clet (_id, e1, e2) ->
311+
f e1; f e2
312+
| Clet_mut (_id, _kind, e1, e2) ->
313+
f e1; f e2
314+
| Cphantom_let (_id, _de, e) ->
315+
f e
316+
| Cassign (_id, e) ->
317+
f e
318+
| Ctuple el ->
319+
List.iter f el
320+
| Cop (_op, el, _dbg) ->
321+
List.iter f el
322+
| Csequence (e1, e2) ->
323+
f e1; f e2
324+
| Cifthenelse(cond, _ifso_dbg, ifso, _ifnot_dbg, ifnot, _dbg) ->
325+
f cond; f ifso; f ifnot
326+
| Cswitch (_e, _ia, ea, _dbg) ->
327+
Array.iter (fun (e, _) -> f e) ea
328+
| Ccatch (_rf, hl, body) ->
329+
let iter_h (_n, _ids, handler, _dbg) = f handler in
330+
List.iter iter_h hl; f body
331+
| Cexit (_n, el) ->
332+
List.iter f el
333+
| Ctrywith (e1, _id, e2, _dbg) ->
334+
f e1; f e2
335+
| Cregion e ->
336+
f e
337+
| Cconst_int _
338+
| Cconst_natint _
339+
| Cconst_float _
340+
| Cconst_symbol _
341+
| Cvar _ ->
342+
()
343+
305344
let map_shallow f = function
306345
| Clet (id, e1, e2) ->
307346
Clet (id, f e1, f e2)
@@ -328,6 +367,8 @@ let map_shallow f = function
328367
Cexit (n, List.map f el)
329368
| Ctrywith (e1, id, e2, dbg) ->
330369
Ctrywith (f e1, id, f e2, dbg)
370+
| Cregion e ->
371+
Cregion (f e)
331372
| Cconst_int _
332373
| Cconst_natint _
333374
| Cconst_float _

asmcomp/cmm.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ and operation =
162162
then the index.
163163
It results in a bounds error if the index is greater than
164164
or equal to the bound. *)
165-
| Cbeginregion
166-
| Cendregion
167165

168166
(** Every basic block should have a corresponding [Debuginfo.t] for its
169167
beginning. *)
@@ -195,6 +193,7 @@ and expression =
195193
| Cexit of int * expression list
196194
| Ctrywith of expression * Backend_var.With_provenance.t * expression
197195
* Debuginfo.t
196+
| Cregion of expression
198197

199198
type codegen_option =
200199
| Reduce_code_size
@@ -248,5 +247,8 @@ val map_tail: (expression -> expression) -> expression -> expression
248247
Same disclaimer as for [iter_shallow_tail] about the notion
249248
of "tail" sub-expression. *)
250249

250+
val iter_shallow: (expression -> unit) -> expression -> unit
251+
(** Apply the transformation to each immediate sub-expression. *)
252+
251253
val map_shallow: (expression -> expression) -> expression -> expression
252254
(** Apply the transformation to each immediate sub-expression. *)

asmcomp/cmm_helpers.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,24 @@ let cache_public_method meths tag cache dbg =
17721772
Csequence(Cop (Cstore (Word_int, Assignment), [cache; Cvar tagged], dbg),
17731773
Cvar tagged)))))
17741774

1775+
let region e =
1776+
(* [Cregion e] is equivalent to [e] if [e] contains no local allocs *)
1777+
let rec check_local_allocs = function
1778+
| Cregion _ ->
1779+
(* Assume any already-existing Cregion contains a local alloc.
1780+
This prevents O(n^2) behaviour with many nested regions *)
1781+
raise Exit
1782+
| Cop (Calloc Alloc_local, _, _)
1783+
| Cop ((Cextcall _ | Capply _), _, _) ->
1784+
raise Exit
1785+
| e ->
1786+
iter_shallow check_local_allocs e
1787+
in
1788+
match check_local_allocs e with
1789+
| () -> e
1790+
| exception Exit -> Cregion e
1791+
1792+
17751793
(* CR mshinwell: These will be filled in by later pull requests. *)
17761794
let placeholder_dbg () = Debuginfo.none
17771795
let placeholder_fun_dbg ~human_name:_ = Debuginfo.none

asmcomp/cmm_helpers.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ val send :
576576
Lambda.meth_kind -> expression -> expression -> expression list ->
577577
Debuginfo.t -> expression
578578

579+
(** Construct [Cregion e], eliding some useless regions *)
580+
val region : expression -> expression
581+
579582
(** Generic Cmm fragments *)
580583

581584
(** Generate generic functions *)

asmcomp/cmmgen.ml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ let rec transl env e =
556556
| Pandbint _ | Porbint _ | Pxorbint _ | Plslbint _ | Plsrbint _
557557
| Pasrbint _ | Pbintcomp (_, _) | Pstring_load _ | Pbytes_load _
558558
| Pbytes_set _ | Pbigstring_load _ | Pbigstring_set _
559-
| Pbbswap _ | Pendregion), _)
559+
| Pbbswap _), _)
560560
->
561561
fatal_error "Cmmgen.transl:prim"
562562
end
@@ -672,8 +672,8 @@ let rec transl env e =
672672
| Uunreachable ->
673673
let dbg = Debuginfo.none in
674674
Cop(Cload (Word_int, Mutable), [Cconst_int (0, dbg)], dbg)
675-
| Ubeginregion (r, e) ->
676-
Clet (r, Cop (Cbeginregion, [], Debuginfo.none), transl env e)
675+
| Uregion e ->
676+
region (transl env e)
677677

678678
and transl_catch env nfail ids body handler dbg =
679679
let ids = List.map (fun (id, kind) -> (id, kind, ref No_result)) ids in
@@ -846,8 +846,6 @@ and transl_prim_1 env p arg dbg =
846846
| Pbswap16 ->
847847
tag_int (bswap16 (ignore_high_bit_int (untag_int
848848
(transl env arg) dbg)) dbg) dbg
849-
| Pendregion ->
850-
Cop(Cendregion, [transl env arg], dbg)
851849
| (Pfield_computed | Psequand | Psequor
852850
| Paddint | Psubint | Pmulint | Pandint
853851
| Porint | Pxorint | Plslint | Plsrint | Pasrint
@@ -1041,7 +1039,7 @@ and transl_prim_2 env p arg1 arg2 dbg =
10411039
| Pmakearray (_, _) | Pduparray (_, _) | Parraylength _ | Parraysetu _
10421040
| Parraysets _ | Pbintofint _ | Pintofbint _ | Pcvtbint (_, _)
10431041
| Pnegbint _ | Pbigarrayref (_, _, _, _) | Pbigarrayset (_, _, _, _)
1044-
| Pbigarraydim _ | Pbytes_set _ | Pbigstring_set _ | Pbbswap _ | Pendregion
1042+
| Pbigarraydim _ | Pbytes_set _ | Pbigstring_set _ | Pbbswap _
10451043
->
10461044
fatal_errorf "Cmmgen.transl_prim_2: %a"
10471045
Printclambda_primitives.primitive p
@@ -1100,7 +1098,7 @@ and transl_prim_3 env p arg1 arg2 arg3 dbg =
11001098
| Psubbint _ | Pmulbint _ | Pdivbint _ | Pmodbint _ | Pandbint _ | Porbint _
11011099
| Pxorbint _ | Plslbint _ | Plsrbint _ | Pasrbint _ | Pbintcomp (_, _)
11021100
| Pbigarrayref (_, _, _, _) | Pbigarrayset (_, _, _, _) | Pbigarraydim _
1103-
| Pstring_load _ | Pbytes_load _ | Pbigstring_load _ | Pbbswap _ | Pendregion
1101+
| Pstring_load _ | Pbytes_load _ | Pbigstring_load _ | Pbbswap _
11041102
->
11051103
fatal_errorf "Cmmgen.transl_prim_3: %a"
11061104
Printclambda_primitives.primitive p

asmcomp/mach.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type operation =
6262
| Ispecific of Arch.specific_operation
6363
| Iname_for_debugger of { ident : Backend_var.t; which_parameter : int option;
6464
provenance : unit option; is_assignment : bool; }
65+
| Ibeginregion | Iendregion
6566

6667
type instruction =
6768
{ desc: instruction_desc;

asmcomp/mach.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type operation =
6969
(b) If [is_assignment] is [true], any information about other [Reg.t]s
7070
that have been previously deemed to hold the value of that
7171
identifier is forgotten. *)
72+
| Ibeginregion | Iendregion
7273

7374
type instruction =
7475
{ desc: instruction_desc;

asmcomp/printcmm.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ let operation d = function
157157
| Ccmpf c -> Printf.sprintf "%sf" (float_comparison c)
158158
| Craise k -> Lambda.raise_kind k ^ location d
159159
| Ccheckbound -> "checkbound" ^ location d
160-
| Cbeginregion -> "beginregion" ^ location d
161-
| Cendregion -> "endregion" ^ location d
162160

163161
let rec expr ppf = function
164162
| Cconst_int (n, _dbg) -> fprintf ppf "%i" n
@@ -268,6 +266,8 @@ let rec expr ppf = function
268266
| Ctrywith(e1, id, e2, _dbg) ->
269267
fprintf ppf "@[<2>(try@ %a@;<1 -2>with@ %a@ %a)@]"
270268
sequence e1 VP.print id sequence e2
269+
| Cregion e ->
270+
fprintf ppf "@[<2>(region@ %a)@]" sequence e
271271

272272
and sequence ppf = function
273273
| Csequence(e1, e2) -> fprintf ppf "%a@ %a" sequence e1 sequence e2

asmcomp/printmach.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ let operation op arg ppf res =
151151
| None -> ""
152152
| Some index -> sprintf "[P%d]" index)
153153
reg arg.(0)
154+
| Ibeginregion -> fprintf ppf "beginregion"
155+
| Iendregion -> fprintf ppf "endregion %a" reg arg.(0)
154156
| Ispecific op ->
155157
Arch.print_specific_operation reg op ppf arg
156158

0 commit comments

Comments
 (0)