-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.ml
362 lines (296 loc) · 13.6 KB
/
cfg.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
open Ll
open Llutil
open Datastructures
(* control flow graphs ------------------------------------------------------ *)
(* This representation of control-flow graphs is more suited for dataflow
analysis than the abstract syntax defined in Ll.fdecl
- a cfg has:
blocks - a map of labels to Ll basic block, and
preds - a set of labels containing the blocks predecessors
ret_ty - the Ll return type of the function
args - a list of function parameters with their types
Representing cfgs as maps makes it simpler to lookup information
about the nodes in the graph. *)
type cfg = { blocks : Ll.block LblM.t
; preds : LblS.t LblM.t
; ret_ty : Ll.ty
; args : (Ll.uid * Ll.ty) list
}
let entry_lbl = "_entry"
(* compute a block's successors --------------------------------------------- *)
let block_succs (b:block) : LblS.t =
match b.term with
| _, Ret _ -> LblS.empty
| _, Br l -> LblS.singleton l
| _, Cbr (_,k,l) -> LblS.of_list [k; l]
(* compute a map from block labels to predecessors -------------------------- *)
let cfg_preds (ast:(lbl * block) list): LblS.t LblM.t =
let set_add l = LblM.update_or LblS.empty (LblS.add l) in
List.fold_left (fun m (l, b) ->
m |> LblM.update_or LblS.empty (fun s -> s) l
|> LblS.fold (set_add l) (block_succs b)) LblM.empty ast
(* lookup operations -------------------------------------------------------- *)
let preds (g:cfg) (l:lbl) : LblS.t = LblM.find l g.preds
let succs (g:cfg) (l:lbl) : LblS.t = block_succs @@ LblM.find l g.blocks
let block (g:cfg) (l:lbl) : Ll.block = LblM.find l g.blocks
let nodes (g:cfg) : LblS.t = LblM.bindings g.blocks |> List.map fst |> LblS.of_list
let exits (g:cfg) : LblS.t = LblM.bindings g.blocks
|> List.filter (fun (l, b) -> LblS.is_empty @@ block_succs b )
|> List.map fst
|> LblS.of_list
(* convert an Ll.fdecl to this representation ------------------------------- *)
let of_ast (fdecl:Ll.fdecl) : cfg =
let e,bs = fdecl.f_cfg in
let ast = (entry_lbl,e)::bs in
let preds = cfg_preds ast in
let blocks = List.fold_left
(fun g (l, block) -> LblM.add l block g)
LblM.empty ast
in
let paramtys, ret_ty = fdecl.f_ty in
let args = List.combine fdecl.f_param paramtys in
{ preds; blocks; ret_ty; args}
(* convert this representation back to Ll.cfg ------------------------------- *)
let to_ast (g:cfg) : Ll.fdecl =
let e = block g entry_lbl in
let f_cfg = e, g.blocks |> LblM.bindings |> List.remove_assoc entry_lbl in
let f_param, paramtys = List.split g.args in
{f_cfg; f_param; f_ty=paramtys,g.ret_ty; }
let add_block (l:lbl) (block:block) (g:cfg) : cfg =
{g with blocks=LblM.add l block g.blocks }
(* creating a flow graph froma control flow graph --------------------------- *)
(* Conceptually, this is a view of a cfg annotated with dataflow information
that should be usable by the generic solver and subsequent optimizations.
To create a flow graph module for a particular analysis, we need to supply
several parameters: *)
module type AS_GRAPH_PARAMS =
sig
(* The type of dataflow facts and the combine operator. This just implements
the FACT interface from cfg.ml *)
type t
val combine : t list -> t
val to_string : t -> string
(* We also need to specify the direction of the analysis *)
val forwards : bool
(* The flow functions defined on the CFG. *)
(* Flow functions across an intstruction or terminator
for forward analysis: should map in[n] to out[n]
for backward analysis: should map out[n] to in[n]
*)
val insn_flow : (uid * insn) -> t -> t
val terminator_flow : terminator -> t -> t
end
(* This Cfg.AsGraph can be used to create a flow graph for the solver from a
control flow graph with of_cfg *)
module AsGraph (D:AS_GRAPH_PARAMS) :
sig
(* Implement the DFA_GRAPH signature where facts are defined by the functor
argument type t and nodes are blocks of the cfg identified by labels *)
include Solver.DFA_GRAPH
with type fact := D.t
and module NodeS = LblS
(* To use the resulting flow graph in optimizations, we need expose a few
more operations: *)
(* Create a flow graph for this analysis an initial mapping of facts for
block labels, a constant flow-in value for the entry or exit labels depending
on the direction of the analysis, and a CFG *)
val of_cfg : (node -> D.t) -> D.t -> cfg -> t
(* We need to be able to look up the resulting solved dataflow analysis facts
per block and per instruction. We also need to be able to inspect the
input and output information for each.
The way we compute this information depends on the direction of the
analysis.
uid_in and uid_out work for both instructions and terminators the lbl
is of the containing block
*)
val block_in : t -> lbl -> D.t
val block_out : t -> lbl -> D.t
val uid_in : t -> lbl -> uid -> D.t
val uid_out : t -> lbl -> uid -> D.t
(* For testing purposes, we would like to be able to access the underlying
map of dataflow facts *)
val dfa : t -> D.t LblM.t
end =
struct
module NodeS = LblS
type t = { cfg:cfg
; dfa:D.t LblM.t }
(* We use _blocks_ of the control flow graph as the nodes of the dataflow
graph. Each block is identified by its lbl.
This choice means that we won't use the "exploded" control-flow graph, where
each instruction is considered a node. The reason for this decision is two-fold:
One: the edges of the cfg are defined in terms of block labesl.
Two: we can speed up the dataflow analysis by propagating information across and
entire block.
The cost of this decision is that we have to re-calculate the flow information
for individual instructions when we need it.
*)
type node = lbl
(* The label of the logical "boundary" node. This boundary node represents
a logical predecessor of the entry block (for forward analysis) or the
logical successor of the exits blocks (for backward analysis). It provides
an "edge" on which to put the entry/exit flow information. *)
let bound_lbl = "__bound"
(* The only way to create a flow graph is to provide an initial labeling *)
let of_cfg init flow_in cfg =
let dfa = cfg.blocks
|> LblM.mapi (fun l _ -> init l)
|> LblM.add bound_lbl flow_in
in
{ cfg; dfa }
(* Access to underlying cfg and facts map *)
let block g = block g.cfg
let nodes g = nodes g.cfg |> NodeS.add bound_lbl
let dfa g = LblM.remove bound_lbl g.dfa
(* Create the dfa successors and predecessors based on the direction of the
data flow analysis. This also adds the boundary node to the succs/preds.
This graph is build at the "block" level, so nodes are block labels.
*)
let extend k v f l = if LblS.mem l k then v else f l
let ns = NodeS.singleton
let dfa_preds =
if D.forwards
then fun g -> (preds g.cfg)
|> extend (ns entry_lbl) (ns bound_lbl)
|> extend (ns bound_lbl) NodeS.empty
else fun g -> (succs g.cfg)
|> extend (exits g.cfg) (ns bound_lbl)
|> extend (ns bound_lbl) NodeS.empty
let dfa_succs =
if D.forwards
then fun g -> extend (ns bound_lbl) (ns entry_lbl) (succs g.cfg)
else fun g -> extend (ns bound_lbl) (exits g.cfg) (preds g.cfg)
let preds = dfa_preds
let succs = dfa_succs
(* Block flow function *)
(* dataflow analysis helpers ------------------------------------------------ *)
(* Propagate dataflow information forward through a whole block.
- fi is the flow function for instructions
- ft is the flow function for terminators
- d_in is the dataflow fact on the in edge
Returns the resulting out / in fact. *)
let block_flow_forwards ({insns; term}:block) (d_in:'d) : 'd =
let d_tmn = List.fold_left (fun d (u,i) -> D.insn_flow (u,i) d) d_in insns in
D.terminator_flow (snd term) d_tmn
let block_flow_backwards ({insns; term}:block) (d_out:'d) : 'd =
let d_ins = D.terminator_flow (snd term) d_out in
List.fold_right (fun (u,i) d -> D.insn_flow (u,i) d) insns d_ins
let flow_block g l d =
(if D.forwards then block_flow_forwards else block_flow_backwards) (block g l) d
(* The supplied flow function, plus the boundary value. Used by the solver. *)
let flow g (l:lbl) =
if Lbl.compare l bound_lbl == 0
then fun _ -> LblM.find l g.dfa
else flow_block g l
(* Look up and modify facts when viewing the CFG as a dataflow graph, used by the solver. *)
let out g n = LblM.find n g.dfa
let add_fact n d g = { g with dfa=LblM.add n d g.dfa }
(* Because the cfg instance of the dataflow graph uses _basic blocks_ as nodes,
we need a way to recover the dataflow facts at individual instructions
within the block. Depending on the direction of the analysis, this amounts to
propagating information either forward or backwards through the block.
The following helper functions construct maps from each instruction or terminator
to the corresponding dataflow fact. *)
(* Compute IN[n] for each instruction in a block, given IN of the first instruction *)
let in_forwards_map
({insns; term}:block) (d_in:'d) : uid -> 'd =
let t_id, t = term in
let m, d_tmn = List.fold_left
(fun (m, d) (u,i) ->
let d' = D.insn_flow (u,i) d in
UidM.add u d m, d')
(UidM.empty, d_in)
insns
in
let m' = UidM.add t_id d_tmn m in
fun u -> UidM.find u m'
(* Compute OUT[n] for each instruction in a block, given IN of the first instruction *)
let out_forwards_map
({insns; term}:block) (d_in:'d) : uid -> 'd =
let t_id, t = term in
let m, d_tmn = List.fold_left
(fun (m, d_in) (u,i) ->
let d_out = D.insn_flow (u, i) d_in in
UidM.add u d_out m, d_out)
(UidM.empty, d_in)
insns
in
let d_out = D.terminator_flow t d_tmn in
let m' = UidM.add t_id d_out m in
fun u -> UidM.find u m'
(* Compute IN[n] for each instruction in a block, given OUT of the terminator*)
let in_backwards_map
({insns; term}:block) (d_out:'d) : uid -> 'd =
let t_id, t = term in
let d_ins = D.terminator_flow t d_out in
let m_init = UidM.add t_id d_ins UidM.empty in
let m, _ = List.fold_right (fun (u,i) (m, d_out) ->
let d_in = D.insn_flow (u,i) d_out in
UidM.add u d_in m, d_in)
insns (m_init, d_ins) in
fun u -> UidM.find u m
(* Compute OUT[n] for each instruction in a block, given OUT of the terminator*)
let out_backwards_map
({insns; term}:block) (d_out:'d) : uid -> 'd =
let t_id, t = term in
let m_init = UidM.add t_id d_out UidM.empty in
let d_ins = D.terminator_flow t d_out in
let m, _ = List.fold_right (fun (u,i) (m, d_out) ->
let d_in = D.insn_flow (u,i) d_out in
UidM.add u d_out m, d_in)
insns (m_init, d_ins) in
fun u -> UidM.find u m
(* Wrapper functions to account for directionality of the analysis *)
let block_in g n =
if D.forwards then
let preds = NodeS.elements @@ preds g n in
let d_outs = List.map (out g) preds in
D.combine d_outs
else
out g n
let block_out g n =
if D.forwards then
out g n
else
let preds = NodeS.elements @@ preds g n in
let d_outs = List.map (out g) preds in
D.combine d_outs
let uid_in g (l:lbl) =
if D.forwards then
in_forwards_map (block g l) (block_in g l)
else
in_backwards_map (block g l) (block_out g l)
let uid_out g (l:lbl) =
if D.forwards then
out_forwards_map (block g l) (block_in g l)
else
out_backwards_map (block g l) (block_out g l)
(* Printing functions *)
(* printing functions ------------------------------------------------------- *)
let annot_insn g l (u,i:uid*insn) =
Printf.sprintf " IN : %s\n %s\n OUT: %s"
(D.to_string (uid_in g l u))
(Llutil.string_of_named_insn (u,i))
(D.to_string (uid_out g l u))
let annot_terminator g l (u,t:uid*terminator) =
Printf.sprintf " IN : %s\n %s\n OUT: %s"
(D.to_string (uid_in g l u))
(Llutil.string_of_terminator t)
(D.to_string (uid_out g l u))
let to_string_annot (annot:lbl -> string) (g:t) : string =
LblM.to_string
(fun l block ->
Printf.sprintf "%s\n%s\n%s\n\n"
(annot l)
(Llutil.mapcat "\n" (annot_insn g l) (block.insns))
(annot_terminator g l (block.term))
) (g.cfg.blocks)
let printer_annot (annot:lbl -> string) (f:Format.formatter) (g:t) : unit =
Format.pp_print_string f (to_string_annot annot g)
let to_string g =
to_string_annot (fun l -> D.to_string (out g l)) g
let printer f g =
printer_annot (fun l -> D.to_string (out g l)) f g
end
(* exported type *)
type t = cfg