@@ -13,9 +13,8 @@ let union_maps_left (m1 : ('a, 'b) Map.Poly.t) (m2 : ('a, 'b) Map.Poly.t) :
1313 | `Both (v1 , _ ) -> Some v1 in
1414 Map.Poly. merge m1 m2 ~f
1515
16- (* *
17- Merge two maps whose values are sets, and union the sets when there's a collision.
18- *)
16+ (* * Merge two maps whose values are sets, and union the sets when there's a
17+ collision. *)
1918let merge_set_maps m1 m2 =
2019 let merge_map_elems ~key :_ es =
2120 match es with
@@ -24,10 +23,8 @@ let merge_set_maps m1 m2 =
2423 | `Both (e1 , e2 ) -> Some (Set. union e1 e2) in
2524 Map.Poly. merge ~f: merge_map_elems m1 m2
2625
27- (* *
28- Like a forward traversal, but branches accumulate two different states that are
29- recombined with join.
30- *)
26+ (* * Like a forward traversal, but branches accumulate two different states that
27+ are recombined with join. *)
3128let branching_traverse_statement stmt ~join ~init ~f =
3229 Stmt.Pattern. (
3330 match stmt with
@@ -40,15 +37,12 @@ let branching_traverse_statement stmt ~join ~init ~f =
4037 (join s' s'', IfElse (pred, c, Some c')))
4138 | _ as s -> fwd_traverse_statement s ~init ~f )
4239
43- (* * Like a branching traversal, but doesn't return an updated statement.
44- *)
40+ (* * Like a branching traversal, but doesn't return an updated statement. *)
4541let branching_fold_statement stmt ~join ~init ~f =
4642 fst
4743 (branching_traverse_statement stmt ~join ~init ~f: (fun s a -> (f s a, () )))
4844
49- (* *
50- See interface file
51- *)
45+ (* * See interface file *)
5246let build_statement_map extract metadata stmt =
5347 let rec build_statement_map_rec next_label map stmt =
5448 let this_label = next_label in
@@ -66,38 +60,37 @@ let build_statement_map extract metadata stmt =
6660(* TODO: this currently does not seem to be labelling inside function bodies.
6761 Could we also do that? *)
6862
69- (* *
70- See interface file
71- *)
63+ (* * See interface file *)
7264let rec build_recursive_statement rebuild statement_map label =
7365 let stmt_ints, meta = Map.Poly. find_exn statement_map label in
7466 let build_stmt = build_recursive_statement rebuild statement_map in
7567 let stmt = Stmt.Pattern. map Fn. id build_stmt stmt_ints in
7668 rebuild stmt meta
7769
78- (* * Represents the state required to build control flow information during an MIR
79- traversal, where
80- * breaks is the set of Breaks seen since the beginning of their loop
81- * continues is the set of Continues seen since the beginning of their loop
82- * returns is the set of Returns seen since the beginning of their function definition
83- * exits is the set of nodes that could have been the last one to execute before this
84- node
85- *)
70+ (* * Represents the state required to build control flow information during an
71+ MIR traversal, where
72+ - breaks is the set of Breaks seen since the beginning of their loop
73+ - continues is the set of Continues seen since the beginning of their loop
74+ - returns is the set of Returns seen since the beginning of their function
75+ definition
76+ - exits is the set of nodes that could have been the last one to execute
77+ before this node *)
8678type cf_state =
8779 { breaks : label Set.Poly .t
8880 ; continues : label Set.Poly .t
8981 ; returns : label Set.Poly .t
9082 ; exits : label Set.Poly .t }
9183
92- (* * Represents the control flow information at each node in the control graph, where
93- * predecessors points to the nodes which could have executed before this node
94- * parents points to the adjacent nodes which directly influence the execution of this
95- node
96- *)
84+ (* * Represents the control flow information at each node in the control graph,
85+ where
86+ - predecessors points to the nodes which could have executed before this
87+ node
88+ - parents points to the adjacent nodes which directly influence the
89+ execution of this node *)
9790type cf_edges = {predecessors : label Set.Poly .t ; parents : label Set.Poly .t }
9891
99- (* * Join the state of a controlflow traversal across different branches of execution such
100- as over if/else branch. *)
92+ (* * Join the state of a controlflow traversal across different branches of
93+ execution such as over if/else branch. *)
10194let join_cf_states (state1 : cf_state ) (state2 : cf_state ) : cf_state =
10295 { breaks= Set. union state1.breaks state2.breaks
10396 ; continues= Set. union state1.continues state2.continues
@@ -112,11 +105,10 @@ let is_ctrl_flow pattern =
112105 | For _ -> true
113106 | _ -> false
114107
115- (* *
116- Simultaneously builds the controlflow parent graph, the predecessor graph and the exit
117- set of a statement. It's advantageous to build them together because they both rely on
118- some of the same Break, Continue and Return bookkeeping.
119- *)
108+ (* * Simultaneously builds the controlflow parent graph, the predecessor graph
109+ and the exit set of a statement. It's advantageous to build them together
110+ because they both rely on some of the same Break, Continue and Return
111+ bookkeeping. *)
120112let build_cf_graphs ?(flatten_loops = false ) ?(blocks_after_body = true )
121113 statement_map =
122114 let rec build_cf_graph_rec (cf_parent : label option )
@@ -138,28 +130,31 @@ let build_cf_graphs ?(flatten_loops = false) ?(blocks_after_body = true)
138130 branching_fold_statement stmt ~join
139131 ~init: ({in_state with exits= substmt_preds}, in_map)
140132 ~f: (build_cf_graph_rec child_cf) in
141- (* If the statement is a loop, we need to include the loop body exits as predecessors
142- of the loop *)
133+ (* If the statement is a loop, we need to include the loop body exits as
134+ predecessors of the loop *)
143135 let substmt_state, predecessors =
144136 match stmt with
145137 | For _ | While _ ->
146138 (* Loop statements are preceded by:
139+
147140 1. The statements that come before the loop
141+
148142 2. The natural exit points of the loop body
149- 3. Continue statements in the loop body
150- This comment mangling brought to you by the autoformatter
151- *)
143+
144+ 3. Continue statements in the loop body This comment mangling
145+ brought to you by the autoformatter *)
152146 let loop_predecessors =
153147 Set.Poly. union_list
154148 [ (* 1*) in_state.exits; (* 2*) substmt_state_unlooped.exits; (* 3*)
155149 Set. diff substmt_state_unlooped.continues in_state.continues ]
156150 in
157151 (* Loop exits are:
158- 1. The loop node itself, since the last action of a typical loop execution is
159- to check if there are any iterations remaining
160- 2. Break statements in the loop body, since broken loops don't execute the
161- loop statement
162- *)
152+
153+ 1. The loop node itself, since the last action of a typical loop
154+ execution is to check if there are any iterations remaining
155+
156+ 2. Break statements in the loop body, since broken loops don't
157+ execute the loop statement *)
163158 let loop_exits =
164159 if flatten_loops then substmt_state_unlooped.exits
165160 else
@@ -168,16 +163,16 @@ let build_cf_graphs ?(flatten_loops = false) ?(blocks_after_body = true)
168163 Set. diff substmt_state_unlooped.breaks in_state.breaks ] in
169164 ({substmt_state_unlooped with exits= loop_exits}, loop_predecessors)
170165 | (Block _ | Profile _ ) when blocks_after_body ->
171- (* Block statements are preceded by the natural exit points of the block
172- body *)
166+ (* Block statements are preceded by the natural exit points of the
167+ block body *)
173168 let block_predecessors = substmt_state_unlooped.exits in
174169 (* Block exits are just the block node *)
175170 let block_exits = Set.Poly. singleton label in
176171 ({substmt_state_unlooped with exits= block_exits}, block_predecessors)
177172 | _ -> (substmt_state_unlooped, in_state.exits) in
178- (* Some statements interact with the break/return/continue states
179- E.g., loops nullify breaks and continues in their body, but are still affected by
180- breaks and input continues*)
173+ (* Some statements interact with the break/return/continue states E.g.,
174+ loops nullify breaks and continues in their body, but are still affected
175+ by breaks and input continues *)
181176 let breaks_out, returns_out, continues_out, extra_cf_deps =
182177 match stmt with
183178 | Break ->
0 commit comments