Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/cfg-ir/ssa-fn.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/cfg-ir/ssa-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3508 - (view) (download)

1 : jhr 3470 (* ssa-fn.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 : jhr 3475 * The SSAFn functor is used to generate the High, Med, and Low IRs in the Diderot
9 :     * compiler. These IRs have the same program and control-flow structure, but differ
10 : jhr 3470 * in their types and operators.
11 :     *)
12 :    
13 :     functor SSAFn (
14 :    
15 : jhr 3508 val irName : string
16 : jhr 3470
17 :     structure Ty : SSA_TYPES
18 :     structure Op : OPERATORS where type ty = Ty.ty
19 :    
20 :     ) : SSA = struct
21 :    
22 :     structure Ty = Ty
23 :     structure Op = Op
24 :    
25 : jhr 3508 val irName = irName
26 : jhr 3470
27 :     (***** CFG *****)
28 :    
29 :     datatype cfg = CFG of {
30 :     entry : node, (* the entry node of a graph; not necessarily an ENTRY node *)
31 :     exit : node (* the exit node of a graph; not necessarily an EXIT node. *)
32 :     }
33 :    
34 :     and node = ND of {
35 :     id : Stamp.stamp,
36 :     props : PropList.holder,
37 :     kind : node_kind
38 :     }
39 :    
40 :     and node_kind
41 :     = NULL
42 :     | ENTRY of {
43 :     succ : node ref
44 :     }
45 :     | JOIN of {
46 :     preds : node list ref, (* includes fake control-flow edges *)
47 :     mask : bool list ref, (* true for incoming fake edges *)
48 :     phis : phi list ref, (* data-flow joins corresponding to the edges in preds *)
49 :     succ : node ref
50 :     }
51 :     | COND of {
52 :     pred : node ref,
53 :     cond : var,
54 :     trueBranch : node ref,
55 :     falseBranch : node ref
56 :     }
57 : jhr 3473 | FOREACH of { (* foreach-loop; this node combines aspects of the COND
58 :     * and JOIN nodes. *)
59 : jhr 3502 pred : node ref, (* the predecessor *)
60 : jhr 3473 phis : phi list ref, (* phi nodes (as in JOIN) *)
61 :     mask : bool list ref, (* true for incoming fake edges *)
62 :     var : var, (* the loop variable *)
63 :     src : var, (* the source of values being iterated over *)
64 : jhr 3502 bodyEntry : node ref, (* the loop body entry node *)
65 :     bodyExit : node ref, (* the loop body exit node *)
66 : jhr 3473 succ : node ref (* the loop-exit edge *)
67 :     }
68 : jhr 3470 | COM of { (* comment *)
69 :     pred : node ref,
70 :     text : string list,
71 :     succ : node ref
72 :     }
73 :     | ASSIGN of { (* local-variable assignment *)
74 :     pred : node ref,
75 :     stm : assign,
76 :     succ : node ref
77 :     }
78 :     | MASSIGN of { (* multi-assignment *)
79 :     pred : node ref,
80 :     stm : massign,
81 :     succ : node ref
82 :     }
83 :     | GASSIGN of { (* global variable assignment *)
84 :     pred: node ref,
85 :     lhs : global_var,
86 :     rhs : var,
87 :     succ : node ref
88 :     }
89 :     | NEW of { (* create new strand instance *)
90 :     pred : node ref,
91 :     strand : Atom.atom,
92 :     args : var list,
93 :     succ : node ref
94 :     }
95 :     | SAVE of { (* save state variable *)
96 :     pred: node ref,
97 :     lhs : state_var,
98 :     rhs : var,
99 :     succ : node ref
100 :     }
101 :     | EXIT of { (* includes die and stabilize *)
102 :     pred : node ref,
103 :     kind : ExitKind.kind, (* kind of exit node *)
104 :     succ : node option ref (* optional fake control-flow edge for when the EXIT is *)
105 :     (* not the CFG exit node *)
106 :     }
107 :    
108 :     and rhs
109 :     = GLOBAL of global_var (* read global variable *)
110 :     | STATE of state_var (* read strand state variable *)
111 :     | VAR of var
112 :     | LIT of Literal.t
113 :     | OP of Op.rator * var list
114 :     | CONS of var list * Ty.ty (* tensor-value construction *)
115 :     | SEQ of var list * Ty.ty (* sequence-value construction *)
116 :     | EINAPP of Ein.ein * var list
117 :    
118 :     and var = V of {
119 :     name : string, (* name *)
120 :     id : Stamp.stamp, (* unique ID *)
121 :     ty : Ty.ty, (* type *)
122 :     bind : var_bind ref, (* binding *)
123 :     useCnt : int ref, (* count of uses *)
124 :     props : PropList.holder
125 :     }
126 :    
127 :     and var_bind
128 :     = VB_NONE
129 :     | VB_RHS of rhs (* defined by an assignment (includes globals and state variables) *)
130 :     | VB_MULTIOP of int * Op.rator * var list
131 :     (* n'th result of operator in multi-assignment *)
132 :     | VB_PHI of var option list (* defined by a phi node *)
133 :     | VB_PARAM (* parameter to a strand *)
134 :    
135 :     (***** global variables *****)
136 :     and global_var = GV of {
137 :     id : Stamp.stamp, (* variable's unique ID *)
138 :     name : string, (* variable's name *)
139 :     ty : Ty.ty, (* variable's type *)
140 : jhr 3506 kind : global_kind, (* the variable kind *)
141 : jhr 3470 props : PropList.holder
142 :     }
143 :    
144 : jhr 3506 and global_kind = ConstVar | InputVar | GlobalVar
145 :    
146 : jhr 3470 (***** strand state variables *****)
147 :     and state_var = SV of {
148 :     id : Stamp.stamp, (* variable's unique ID *)
149 :     name : string, (* variable's name *)
150 :     ty : Ty.ty, (* variable's type *)
151 :     output : bool, (* true for output variables *)
152 :     props : PropList.holder
153 :     }
154 :    
155 :     withtype assign = (var * rhs)
156 :     and massign = (var list * Op.rator * var list)
157 :     and phi = (var * var option list) (* NONE for fake edges *)
158 :    
159 :     datatype assignment
160 :     = ASSGN of assign
161 :     | MASSGN of massign
162 :     | GASSGN of (global_var * var)
163 :     | SAV of (state_var * var)
164 :    
165 :     (***** Program representation *****)
166 :    
167 : jhr 3505 type input = global_var Inputs.input
168 :    
169 : jhr 3470 datatype program = Program of {
170 :     props : Properties.t list,
171 : jhr 3485 consts : global_var list, (* large constant variables *)
172 : jhr 3505 inputs : input list, (* global input variables *)
173 :     constInit : cfg, (* code that initializes constants and inputs *)
174 : jhr 3485 globals : global_var list, (* other global variables *)
175 : jhr 3470 globalInit : cfg, (* CFG to initialize other globals (if any) *)
176 : jhr 3485 strand : strand, (* the strand definition *)
177 :     create : create, (* initial strand creation *)
178 :     update : cfg option (* optional update code. *)
179 : jhr 3470 }
180 :    
181 :     and strand = Strand of {
182 :     name : Atom.atom,
183 :     params : var list,
184 :     state : state_var list,
185 :     stateInit : cfg,
186 : jhr 3485 initM : cfg option,
187 :     updateM : cfg,
188 :     stabilizeM : cfg option
189 : jhr 3470 }
190 :    
191 : jhr 3485 and create = Create of {
192 :     dim : int option, (* grid dimension; NONE for collections *)
193 :     code : cfg (* the loop nest for creating the strands *)
194 : jhr 3470 }
195 :    
196 :     structure GlobalVar =
197 :     struct
198 : jhr 3506 fun new (kind, name, ty) = GV{
199 : jhr 3470 id = Stamp.new(),
200 :     name = name,
201 :     ty = ty,
202 : jhr 3506 kind = kind,
203 : jhr 3470 props = PropList.newHolder()
204 :     }
205 :     fun name (GV{name, ...}) = name
206 :     fun uniqueName (GV{id, name, ...}) = name ^ Stamp.toString id
207 : jhr 3506 fun kind (GV{kind, ...}) = kind
208 : jhr 3470 fun ty (GV{ty, ...}) = ty
209 : jhr 3506 fun isInput (GV{kind=InputVar, ...}) = true
210 :     | isInput _ = false
211 : jhr 3470 fun same (GV{id=a, ...}, GV{id=b, ...}) = Stamp.same(a, b)
212 :     fun compare (GV{id=a, ...}, GV{id=b, ...}) = Stamp.compare(a, b)
213 :     fun hash (GV{id, ...}) = Stamp.hash id
214 :     fun toString (GV{id, name, ...}) = concat["globals.", name, Stamp.toString id]
215 :     (* properties *)
216 :     fun newProp initFn = PropList.newProp (fn (GV{props, ...}) => props, initFn)
217 :     fun newFlag () = PropList.newFlag (fn (GV{props, ...}) => props)
218 :     (* collections *)
219 :     local
220 :     structure V =
221 :     struct
222 :     type ord_key = global_var
223 :     val compare = compare
224 :     end
225 :     in
226 :     structure Map = RedBlackMapFn (V)
227 :     structure Set = RedBlackSetFn (V)
228 :     end
229 :     structure Tbl = HashTableFn (
230 :     struct
231 :     type hash_key = global_var
232 :     val hashVal = hash
233 :     val sameKey = same
234 :     end)
235 :     end
236 :    
237 :     structure StateVar =
238 :     struct
239 :     fun new (isOut, name, ty) = SV{
240 :     id = Stamp.new(),
241 :     name = name,
242 :     ty = ty,
243 :     output = isOut,
244 :     props = PropList.newHolder()
245 :     }
246 :     fun name (SV{name, ...}) = name
247 :     fun ty (SV{ty, ...}) = ty
248 :     fun isOutput (SV{output, ...}) = output
249 :     fun same (SV{id=a, ...}, SV{id=b, ...}) = Stamp.same(a, b)
250 :     fun compare (SV{id=a, ...}, SV{id=b, ...}) = Stamp.compare(a, b)
251 :     fun hash (SV{id, ...}) = Stamp.hash id
252 :     fun toString (SV{name, ...}) = "self." ^ name
253 :     (* properties *)
254 :     fun newProp initFn = PropList.newProp (fn (SV{props, ...}) => props, initFn)
255 :     fun newFlag () = PropList.newFlag (fn (SV{props, ...}) => props)
256 :     (* collections *)
257 :     local
258 :     structure V =
259 :     struct
260 :     type ord_key = state_var
261 :     val compare = compare
262 :     end
263 :     in
264 :     structure Map = RedBlackMapFn (V)
265 :     structure Set = RedBlackSetFn (V)
266 :     end
267 :     structure Tbl = HashTableFn (
268 :     struct
269 :     type hash_key = state_var
270 :     val hashVal = hash
271 :     val sameKey = same
272 :     end)
273 :     end
274 :    
275 :     structure Var =
276 :     struct
277 :     fun new (name, ty) = V{
278 :     name = name,
279 :     id = Stamp.new(),
280 :     ty = ty,
281 :     bind = ref VB_NONE,
282 :     useCnt = ref 0,
283 :     props = PropList.newHolder()
284 :     }
285 :     fun copy (V{name, ty, ...}) = new (name, ty)
286 :     fun name (V{name, ...}) = name
287 :     fun ty (V{ty, ...}) = ty
288 :     fun binding (V{bind, ...}) = !bind
289 :     fun setBinding (V{bind, ...}, vb) = bind := vb
290 :     fun useCount (V{useCnt, ...}) = !useCnt
291 :     fun same (V{id=a, ...}, V{id=b, ...}) = Stamp.same(a, b)
292 :     fun compare (V{id=a, ...}, V{id=b, ...}) = Stamp.compare(a, b)
293 :     fun hash (V{id, ...}) = Stamp.hash id
294 :     fun toString (V{name, id, ...}) = name ^ Stamp.toString id
295 :     (* properties *)
296 :     fun newProp initFn = PropList.newProp (fn (V{props, ...}) => props, initFn)
297 :     fun newFlag () = PropList.newFlag (fn (V{props, ...}) => props)
298 :     (* collections *)
299 :     local
300 :     structure V =
301 :     struct
302 :     type ord_key = var
303 :     val compare = compare
304 :     end
305 :     in
306 :     structure Map = RedBlackMapFn (V)
307 :     structure Set = RedBlackSetFn (V)
308 :     end
309 :     structure Tbl = HashTableFn (
310 :     struct
311 :     type hash_key = var
312 :     val hashVal = hash
313 :     val sameKey = same
314 :     end)
315 :     end
316 :    
317 :     structure Node =
318 :     struct
319 :     fun id (ND{id, ...}) = id
320 :     fun kind (ND{kind, ...}) = kind
321 :     fun same (ND{id=a, ...}, ND{id=b, ...}) = Stamp.same(a, b)
322 :     fun compare (ND{id=a, ...}, ND{id=b, ...}) = Stamp.compare(a, b)
323 :     fun hash (ND{id, ...}) = Stamp.hash id
324 :     fun toString (ND{id, kind, ...}) = let
325 :     val tag = (case kind
326 :     of NULL => "NULL"
327 :     | ENTRY _ => "ENTRY"
328 :     | JOIN _ => "JOIN"
329 :     | COND _ => "COND"
330 : jhr 3473 | FOREACH _ => "FOREACH"
331 : jhr 3470 | COM _ => "COM"
332 :     | ASSIGN _ => "ASSIGN"
333 :     | MASSIGN _ => "MASSIGN"
334 :     | GASSIGN _ => "GASSIGN"
335 :     | NEW _ => "NEW"
336 :     | SAVE _ => "SAVE"
337 :     | EXIT{kind, ...} => ExitKind.toString kind
338 :     (* end case *))
339 :     in
340 :     tag ^ Stamp.toString id
341 :     end
342 :     fun new kind = ND{id = Stamp.new(), props = PropList.newHolder(), kind = kind}
343 :     (* variable defs and uses *)
344 :     fun uses (ND{kind, ...}) = (case kind
345 :     of JOIN{phis, ...} => let
346 :     fun add ([], ys) = ys
347 :     | add (SOME x :: xs, ys) = add(xs, x::ys)
348 :     | add (NONE :: xs, ys) = add(xs, ys)
349 :     in
350 :     List.foldr (fn ((_, xs), ys) => add(xs, ys)) [] (!phis)
351 :     end
352 :     | COND{cond, ...} => [cond]
353 : jhr 3473 | FOREACH{src, phis, ...} => let
354 :     fun add ([], ys) = ys
355 :     | add (SOME x :: xs, ys) = add(xs, x::ys)
356 :     | add (NONE :: xs, ys) = add(xs, ys)
357 :     in
358 :     List.foldr (fn ((_, xs), ys) => add(xs, ys)) [src] (!phis)
359 :     end
360 : jhr 3470 | ASSIGN{stm=(y, rhs), ...} => (case rhs
361 :     of GLOBAL _ => []
362 :     | STATE _ => []
363 :     | VAR x => [x]
364 :     | LIT _ => []
365 :     | OP(_, args) => args
366 :     | CONS(args, _) => args
367 :     | SEQ(args, _) => args
368 :     | EINAPP(_, args) => args
369 :     (* end case *))
370 :     | MASSIGN{stm=(_, _, args), ...} => args
371 :     | GASSIGN{rhs, ...} => [rhs]
372 :     | NEW{args, ...} => args
373 :     | SAVE{rhs, ...} => [rhs]
374 :     | _ => []
375 :     (* end case *))
376 :     fun defs (ND{kind, ...}) = (case kind
377 :     of JOIN{phis, ...} => List.map #1 (!phis)
378 : jhr 3473 | FOREACH{var, phis, ...} => var :: List.map #1 (!phis)
379 : jhr 3470 | ASSIGN{stm=(y, _), ...} => [y]
380 :     | MASSIGN{stm=(ys, _, _), ...} => ys
381 :     | _ => []
382 :     (* end case *))
383 :     val dummy = new NULL
384 :     fun mkENTRY () = new (ENTRY{succ = ref dummy})
385 :     fun mkJOIN phis = new (JOIN{preds = ref [], mask = ref [], phis = ref phis, succ = ref dummy})
386 :     fun mkCOND cond = new (COND{
387 :     pred = ref dummy, cond = cond,
388 :     trueBranch = ref dummy, falseBranch = ref dummy
389 :     })
390 : jhr 3473 fun mkFOREACH (var, src) = new (FOREACH{
391 : jhr 3502 pred = ref dummy,
392 : jhr 3473 phis = ref [], mask = ref [],
393 :     var = var, src = src,
394 : jhr 3502 bodyEntry = ref dummy,
395 :     bodyExit = ref dummy,
396 :     succ = ref dummy
397 : jhr 3473 })
398 : jhr 3470 fun mkCOM text = new (COM{pred = ref dummy, text = text, succ = ref dummy})
399 :     fun mkASSIGN (lhs, rhs) = (
400 :     Var.setBinding (lhs, VB_RHS rhs);
401 :     new (ASSIGN{pred = ref dummy, stm = (lhs, rhs), succ = ref dummy}))
402 :     fun mkMASSIGN (lhs, rator, args) = let
403 :     fun setB (_, []) = ()
404 :     | setB (i, x::xs) = (
405 :     Var.setBinding (x, VB_MULTIOP(i, rator, args));
406 :     setB (i+1, xs))
407 :     in
408 :     setB (0, lhs);
409 :     new (MASSIGN{pred = ref dummy, stm = (lhs, rator, args), succ = ref dummy})
410 :     end
411 :     fun mkGASSIGN (lhs, rhs) = new (GASSIGN{
412 :     pred = ref dummy, lhs = lhs, rhs = rhs, succ = ref dummy
413 :     })
414 :     fun mkNEW {strand, args} = new (NEW{
415 :     pred = ref dummy, strand = strand, args = args, succ = ref dummy
416 :     })
417 :     fun mkSAVE (lhs, rhs) = new (SAVE{
418 :     pred = ref dummy, lhs = lhs, rhs = rhs, succ = ref dummy
419 :     })
420 : jhr 3505 fun mkEXIT kind = new (EXIT{kind = kind, pred = ref dummy, succ = ref NONE})
421 :     fun mkRETURN () = mkEXIT ExitKind.RETURN
422 :     fun mkACTIVE () = mkEXIT ExitKind.ACTIVE
423 :     fun mkSTABILIZE () = mkEXIT ExitKind.STABILIZE
424 :     fun mkDIE () = mkEXIT ExitKind.DIE
425 :     fun mkUNREACHABLE () = mkEXIT ExitKind.UNREACHABLE
426 : jhr 3470 fun isNULL (ND{kind=NULL, ...}) = true
427 :     | isNULL _ = false
428 :     (* is a node reachable from the CFG entry; UNREACHABLE exit nodes and JOINs with no real
429 :     * predecessors are unreachable.
430 :     *)
431 :     fun isReachable (ND{kind=EXIT{kind=ExitKind.UNREACHABLE, ...}, ...}) = false
432 :     | isReachable (ND{kind=JOIN{mask, ...}, ...}) = List.exists not (!mask)
433 :     | isReachable _ = true
434 :     (* editing node edges *)
435 :     fun hasPred (ND{kind, ...}) = (case kind
436 :     of NULL => false
437 :     | ENTRY _ => false
438 :     | _ => true
439 :     (* end case *))
440 :     fun setPred (nd0 as ND{kind, ...}, nd) = (case kind
441 :     of NULL => raise Fail("setPred on NULL node " ^ toString nd0)
442 :     | ENTRY _ => raise Fail("setPred on ENTRY node " ^ toString nd0)
443 :     | JOIN{preds, ...} => preds := !preds @ [nd] (* assume preds are added in order *)
444 : jhr 3502 | FOREACH{pred, ...} => pred := nd
445 : jhr 3470 | COND{pred, ...} => pred := nd
446 :     | COM{pred, ...} => pred := nd
447 :     | ASSIGN{pred, ...} => pred := nd
448 :     | MASSIGN{pred, ...} => pred := nd
449 :     | GASSIGN{pred, ...} => pred := nd
450 :     | NEW{pred, ...} => pred := nd
451 :     | SAVE{pred, ...} => pred := nd
452 :     | EXIT{pred, ...} => pred := nd
453 :     (* end case *))
454 :     fun preds (nd as ND{kind, ...}) = (case kind
455 :     of NULL => [] (*raise Fail("preds on NULL node "^toString nd)*)
456 :     | ENTRY _ => []
457 :     | JOIN{preds, ...} => !preds
458 :     | COND{pred, ...} => [!pred]
459 : jhr 3502 | FOREACH{pred, bodyExit, ...} => [!pred, !bodyExit]
460 : jhr 3470 | COM{pred, ...} => [!pred]
461 :     | ASSIGN{pred, ...} => [!pred]
462 :     | MASSIGN{pred, ...} => [!pred]
463 :     | GASSIGN{pred, ...} => [!pred]
464 :     | NEW{pred, ...} => [!pred]
465 :     | SAVE{pred, ...} => [!pred]
466 :     | EXIT{pred, ...} => [!pred]
467 :     (* end case *))
468 :     fun hasSucc (ND{kind, ...}) = (case kind
469 :     of NULL => false
470 :     | ENTRY _ => true
471 :     | JOIN _ => true
472 :     | COND _ => true
473 :     | COM _ => true
474 : jhr 3473 | FOREACH _ => true
475 : jhr 3470 | ASSIGN _ => true
476 :     | MASSIGN _ => true
477 :     | GASSIGN _ => true
478 :     | NEW _ => true
479 :     | SAVE _ => true
480 :     | EXIT{succ=ref(SOME _), ...} => true
481 :     | EXIT _ => false
482 :     (* end case *))
483 :     fun setSucc (nd0 as ND{kind, ...}, nd) = (case kind
484 :     of NULL => raise Fail("setSucc on NULL node "^toString nd0)
485 :     | ENTRY{succ} => succ := nd
486 :     | JOIN{succ, ...} => succ := nd
487 :     | COND _ => raise Fail("setSucc on COND node "^toString nd0)
488 : jhr 3501 | FOREACH{succ, ...} => succ := nd
489 : jhr 3470 | COM{succ, ...} => succ := nd
490 :     | ASSIGN{succ, ...} => succ := nd
491 :     | MASSIGN{succ, ...} => succ := nd
492 :     | GASSIGN{succ, ...} => succ := nd
493 :     | NEW{succ, ...} => succ := nd
494 :     | SAVE{succ, ...} => succ := nd
495 :     | EXIT{succ, ...} => succ := SOME nd
496 :     (* end case *))
497 :     fun succs (ND{kind, ...}) = (case kind
498 :     of NULL => [] (*raise Fail("succs on NULL node "^toString nd)*)
499 :     | ENTRY{succ} => [!succ]
500 :     | JOIN{succ, ...} => [!succ]
501 :     | COND{trueBranch, falseBranch, ...} => [!trueBranch, !falseBranch]
502 : jhr 3502 | FOREACH{bodyEntry, succ, ...} => [!bodyEntry, !succ]
503 : jhr 3470 | COM{succ, ...} => [!succ]
504 :     | ASSIGN{succ, ...} => [!succ]
505 :     | MASSIGN{succ, ...} => [!succ]
506 :     | GASSIGN{succ, ...} => [!succ]
507 :     | NEW{succ, ...} => [!succ]
508 :     | SAVE{succ, ...} => [!succ]
509 :     | EXIT{succ=ref(SOME nd), ...} => [nd]
510 :     | EXIT _ => []
511 :     (* end case *))
512 :     (* QUESTION: should these functions also do the setPred operation? *)
513 :     fun setTrueBranch (ND{kind=COND{trueBranch, ...}, ...}, nd) = trueBranch := nd
514 :     | setTrueBranch (nd, _) = raise Fail("setTrueBranch on " ^ toString nd)
515 :     fun setFalseBranch (ND{kind=COND{falseBranch, ...}, ...}, nd) = falseBranch := nd
516 :     | setFalseBranch (nd, _) = raise Fail("setFalseBranch on " ^ toString nd)
517 : jhr 3502 fun setBodyEntry (ND{kind=FOREACH{bodyEntry, ...}, ...}, nd) = bodyEntry := nd
518 :     | setBodyEntry (nd, _) = raise Fail("setBodyEntry on " ^ toString nd)
519 :     fun setBodyExit (ND{kind=FOREACH{bodyExit, ...}, ...}, nd) = bodyExit := nd
520 :     | setBodyExit (nd, _) = raise Fail("setBodyExit on " ^ toString nd)
521 : jhr 3470 fun setEdgeMask (ND{kind=JOIN{mask, ...}, ...}, mask') = mask := mask'
522 :     | setEdgeMask (nd, _) = raise Fail("setEdgeMask on " ^ toString nd)
523 :     fun addEdge (nd1, nd2) = (
524 :     if hasSucc nd1
525 :     then (
526 :     setSucc (nd1, nd2);
527 :     setPred (nd2, nd1))
528 :     else ())
529 :     (*DEBUG*)handle ex => (
530 :     print(concat["error in addEdge(", toString nd1, ",", toString nd2, ")\n"]);
531 :     raise ex)
532 : jhr 3502 (* replace the edge src-->oldDst by the edge src-->dst *)
533 : jhr 3470 fun replaceInEdge {src, oldDst, dst} = (
534 :     (* first set the successor of src *)
535 :     case kind src
536 :     of COND{trueBranch, falseBranch, ...} =>
537 :     if same(!trueBranch, oldDst)
538 :     then trueBranch := dst
539 :     else falseBranch := dst
540 : jhr 3502 | FOREACH{bodyEntry, succ, ...} =>
541 :     if same(!bodyEntry, oldDst)
542 :     then bodyEntry := dst
543 :     else succ := dst
544 : jhr 3470 | _ => setSucc (src, dst)
545 :     (* end case *);
546 :     (* then set the predecessor of dst *)
547 :     setPred (dst, src))
548 :     (*DEBUG*)handle ex => (
549 :     print(concat["error in replaceInEdge(", toString src, ",", toString oldDst, ",", toString dst, ")\n"]);
550 :     raise ex)
551 : jhr 3502 (* replace the edge oldSrc-->dst by the edge src-->dst *)
552 : jhr 3470 fun replaceOutEdge {oldSrc, src, dst} = (
553 :     (* first set the successor of src *)
554 :     case kind oldSrc
555 :     of COND{trueBranch, falseBranch, ...} =>
556 :     if same(!trueBranch, dst)
557 :     then setTrueBranch (src, dst)
558 :     else setFalseBranch (src, dst)
559 : jhr 3502 | FOREACH{bodyEntry, succ, ...} =>
560 :     if same(!bodyEntry, dst)
561 :     then setBodyEntry (src, dst)
562 :     else setSucc (src, dst)
563 : jhr 3470 | _ => setSucc (src, dst)
564 :     (* end case *);
565 :     (* then set the predecessor of dst *)
566 :     case kind dst
567 :     of JOIN{preds, ...} => let
568 :     fun edit [] = raise Fail "replaceOutEdge: cannot find predecessor"
569 :     | edit (nd::nds) = if same(nd, oldSrc) then src::nds else nd::edit nds
570 :     in
571 :     preds := edit (!preds)
572 :     end
573 : jhr 3502 | FOREACH{bodyExit, pred, ...} =>
574 :     if same(!bodyExit, oldSrc)
575 :     then bodyExit := src
576 :     else pred := src
577 : jhr 3470 | _ => setPred (dst, src)
578 :     (* end case *))
579 :     (*DEBUG*)handle ex => (
580 :     print(concat["error in replaceOutEdge(", toString oldSrc, ",", toString src, ",", toString dst, ")\n"]);
581 :     raise ex)
582 :     (* properties *)
583 :     fun newProp initFn =
584 :     PropList.newProp (fn (ND{props, ...}) => props, initFn)
585 :     fun newFlag () =
586 :     PropList.newFlag (fn (ND{props, ...}) => props)
587 :     end
588 :    
589 :     structure CFG =
590 :     struct
591 :     val empty = CFG{entry = Node.dummy, exit = Node.dummy}
592 :    
593 :     fun isEmpty (CFG{entry, exit}) =
594 :     Node.same(entry, exit) andalso Node.isNULL entry
595 :    
596 :     (* create a basic block from a list of assignments *)
597 :     fun mkBlock [] = empty
598 :     | mkBlock (stm::stms) = let
599 :     fun mkNode (ASSGN stm) = Node.mkASSIGN stm
600 :     | mkNode (MASSGN stm) = Node.mkMASSIGN stm
601 :     | mkNode (GASSGN stm) = Node.mkGASSIGN stm
602 :     | mkNode (SAV stm) = Node.mkSAVE stm
603 :     val entry = mkNode stm
604 :     fun f (stm, prev) = let
605 :     val nd = mkNode stm
606 :     in
607 :     Node.addEdge (prev, nd);
608 :     nd
609 :     end
610 :     val exit = List.foldl f entry stms
611 :     in
612 :     CFG{entry = entry, exit = exit}
613 :     end
614 :    
615 :     (* entry/exit nodes of a CFG *)
616 :     fun entry (CFG{entry = nd, ...}) = nd
617 :     fun exit (CFG{exit = nd, ...}) = nd
618 :    
619 :     (* DFS sorting of the graph rooted at the entry to a statement; the resulting list will
620 :     * be in preorder with parents before children.
621 :     *)
622 :     fun sort (CFG{entry, ...}) = let
623 :     val {getFn, setFn} = PropList.newFlag (fn (ND{props, ...}) => props)
624 :     fun dfs (nd, l) =
625 :     if getFn nd
626 :     then l
627 :     else (
628 :     setFn (nd, true);
629 :     nd :: List.foldl dfs l (Node.succs nd))
630 :     val nodes = dfs (entry, [])
631 :     in
632 :     List.app (fn nd => setFn(nd, false)) nodes;
633 :     nodes
634 :     end
635 :    
636 :     (* apply a function to all of the nodes in the graph rooted at the entry to the statement *)
637 :     fun apply (f : node -> unit) (CFG{entry, ...}) = let
638 :     val {getFn, setFn} = Node.newFlag()
639 :     fun dfs (nd, l) =
640 :     if getFn nd
641 :     then l
642 :     else (
643 :     f nd; (* visit *)
644 :     setFn (nd, true);
645 :     nd :: List.foldl dfs l (Node.succs nd))
646 :     val nodes = dfs (entry, [])
647 :     in
648 :     List.app (fn nd => setFn(nd, false)) nodes
649 :     end
650 :    
651 :     (* delete a simple node from a CFG *)
652 :     fun deleteNode (nd as ND{kind, ...}) = let
653 :     val (pred, succ) = (case kind
654 :     of COM{pred = ref pred, succ = ref succ, ...} => (pred, succ)
655 :     | ASSIGN{pred = ref pred, succ = ref succ, ...} => (pred, succ)
656 :     | MASSIGN{pred = ref pred, succ = ref succ, ...} => (pred, succ)
657 :     | GASSIGN{pred = ref pred, succ = ref succ, ...} => (pred, succ)
658 :     | NEW{pred = ref pred, succ = ref succ, ...} => (pred, succ)
659 :     | SAVE{pred = ref pred, succ = ref succ, ...} => (pred, succ)
660 :     | _ => raise Fail(concat["unsupported deleteNode(", Node.toString nd, ")\n"])
661 :     (* end case *))
662 :     in
663 :     (* replace the predecessor edge from succ to nd with an edge from succ to pred *)
664 :     case Node.kind succ
665 :     of JOIN{preds, ...} => let
666 :     fun edit [] = raise Fail "deleteNode: cannot find predecessor"
667 :     | edit (nd'::nds) = if Node.same(nd', nd) then pred::nds else nd'::edit nds
668 :     in
669 :     preds := edit (!preds)
670 :     end
671 :     | _ => Node.setPred (succ, pred)
672 :     (* end case *);
673 :     (* replace the successor edge from pred to nd with an edge from pred to succ *)
674 :     case Node.kind pred
675 :     of COND{trueBranch, falseBranch, ...} => (
676 :     (* note that we treat each branch independently, so that we handle the
677 :     * situation where both branches are the same node.
678 :     *)
679 :     if Node.same(!trueBranch, nd)
680 :     then Node.setTrueBranch(pred, succ)
681 :     else ();
682 :     if Node.same(!falseBranch, nd)
683 :     then Node.setFalseBranch(pred, succ)
684 :     else ())
685 :     | _ => Node.setSucc (pred, succ)
686 :     (* end case *)
687 :     end
688 :     (*DEBUG*)handle ex => (
689 :     print(concat["error in deleteNode(", Node.toString nd, ")\n"]);
690 :     raise ex)
691 :    
692 :     (* replace a simple node in a cfg with a subgraph *)
693 :     fun replaceNode (oldNd as ND{kind, ...}, node) = (case kind
694 :     of ASSIGN{pred, succ, ...} => (
695 :     Node.replaceInEdge {src = !pred, oldDst = oldNd, dst = node};
696 :     Node.replaceOutEdge {oldSrc = oldNd, src = node, dst = !succ})
697 :     | MASSIGN{pred, succ, ...} => (
698 :     Node.replaceInEdge {src = !pred, oldDst = oldNd, dst = node};
699 :     Node.replaceOutEdge {oldSrc = oldNd, src = node, dst = !succ})
700 :     | GASSIGN{pred, succ, ...} => (
701 :     Node.replaceInEdge {src = !pred, oldDst = oldNd, dst = node};
702 :     Node.replaceOutEdge {oldSrc = oldNd, src = node, dst = !succ})
703 :     | NEW{pred, succ, ...} => (
704 :     Node.replaceInEdge {src = !pred, oldDst = oldNd, dst = node};
705 :     Node.replaceOutEdge {oldSrc = oldNd, src = node, dst = !succ})
706 :     | SAVE{pred, succ, ...} => (
707 :     Node.replaceInEdge {src = !pred, oldDst = oldNd, dst = node};
708 :     Node.replaceOutEdge {oldSrc = oldNd, src = node, dst = !succ})
709 :     | EXIT{pred, succ=ref NONE, ...} =>
710 :     Node.replaceInEdge {src = !pred, oldDst = oldNd, dst = node}
711 :     | _ => raise Fail(concat[
712 :     "unsupported replaceNode(", Node.toString oldNd, ", ", Node.toString node, ")"
713 :     ])
714 :     (* end case *))
715 :    
716 :     (* replace a simple node in a cfg with a subgraph *)
717 :     fun replaceNodeWithCFG (nd as ND{kind, ...}, cfg as CFG{entry, exit}) =
718 :     if isEmpty cfg
719 :     then deleteNode nd
720 :     else (case kind
721 :     of ASSIGN{pred, succ, ...} => (
722 :     Node.replaceInEdge {src = !pred, oldDst = nd, dst = entry};
723 :     Node.replaceOutEdge {oldSrc = nd, src = exit, dst = !succ})
724 :     | MASSIGN{pred, succ, ...} => (
725 :     Node.replaceInEdge {src = !pred, oldDst = nd, dst = entry};
726 :     Node.replaceOutEdge {oldSrc = nd, src = exit, dst = !succ})
727 :     | _ => raise Fail "unsupported replaceNodeWithCFG"
728 :     (* end case *))
729 :    
730 :     (* concatenate two CFGs *)
731 :     fun concat (cfg1 as CFG{entry=e1, exit=x1}, cfg2 as CFG{entry=e2, exit=x2}) =
732 :     if isEmpty cfg1 then cfg2
733 :     else if isEmpty cfg2 then cfg1
734 :     else (
735 :     Node.setSucc (x1, e2);
736 :     Node.setPred (e2, x1);
737 :     CFG{entry = e1, exit = x2})
738 :     (*DEBUG*)handle ex => (
739 :     print(String.concat["error in concat({", Node.toString e1, ",", Node.toString x1,
740 :     "},{", Node.toString e2, ",", Node.toString x2, "})\n"]);
741 :     raise ex)
742 :    
743 :     (* prepend a node to a CFG *)
744 :     fun prependNode (nd, cfg as CFG{entry, exit}) =
745 :     if isEmpty cfg
746 :     then CFG{entry=nd, exit=nd}
747 :     else (
748 :     Node.setSucc (nd, entry);
749 :     Node.setPred (entry, nd);
750 :     CFG{entry=nd, exit=exit})
751 :    
752 :     (* append a node to a CFG *)
753 :     fun appendNode (cfg as CFG{entry, exit}, nd) =
754 :     if isEmpty cfg
755 :     then CFG{entry=nd, exit=nd}
756 :     else (
757 :     Node.setPred (nd, exit);
758 :     Node.setSucc (exit, nd);
759 :     CFG{entry=entry, exit=nd})
760 :    
761 :     (* insert a block of assignments at the beginning of the CFG. If the CFG has an ENTRY
762 :     * node, then the block is inserted immediatly following the entry.
763 :     *)
764 :     fun prependBlock ([], cfg) = cfg
765 :     | prependBlock (stms, cfg as CFG{entry, exit}) = (case entry
766 :     of ND{kind=ENTRY{succ}, ...} => let
767 :     fun mkNode (ASSGN stm) = Node.mkASSIGN stm
768 :     | mkNode (MASSGN stm) = Node.mkMASSIGN stm
769 :     | mkNode (GASSGN stm) = Node.mkGASSIGN stm
770 :     | mkNode (SAV stm) = Node.mkSAVE stm
771 :     fun f (stm, succ) = let
772 :     val nd = mkNode stm
773 :     in
774 :     Node.addEdge (nd, succ);
775 :     nd
776 :     end
777 :     val first = List.foldr f (!succ) stms
778 :     in
779 :     succ := first;
780 :     Node.setPred (first, entry);
781 :     cfg
782 :     end
783 :     | _ => concat(mkBlock stms, cfg)
784 :     (* end case *))
785 :    
786 :     (* insert a block of assignments at the end of the CFG argument If the CFG has an EXIT
787 :     * node, then the block is inserted immediatly before the exit.
788 :     *)
789 :     fun appendBlock (cfg, []) = cfg
790 :     | appendBlock (cfg as CFG{entry, exit}, stms) = (case exit
791 :     of ND{kind=EXIT{pred, ...}, ...} => let
792 :     fun mkNode (ASSGN stm) = Node.mkASSIGN stm
793 :     | mkNode (MASSGN stm) = Node.mkMASSIGN stm
794 :     | mkNode (GASSGN stm) = Node.mkGASSIGN stm
795 :     | mkNode (SAV stm) = Node.mkSAVE stm
796 :     fun f (stm, prev) = let
797 :     val nd = mkNode stm
798 :     in
799 :     Node.addEdge (prev, nd);
800 :     nd
801 :     end
802 :     val last = List.foldl f (!pred) stms
803 :     in
804 :     pred := last;
805 :     Node.setSucc(last, exit);
806 :     cfg
807 :     end
808 :     | _ => concat(cfg, mkBlock stms)
809 :     (* end case *))
810 :     end
811 :    
812 :     structure RHS =
813 :     struct
814 :     fun vars rhs = (case rhs
815 :     of GLOBAL _ => []
816 :     | STATE _ => []
817 :     | VAR x => [x]
818 :     | LIT _ => []
819 :     | OP(rator, xs) => xs
820 :     | CONS(xs, _) => xs
821 :     | SEQ(xs, _) => xs
822 :     | EINAPP(_, xs) => xs
823 :     (* end case *))
824 :    
825 :     fun map f = let
826 :     fun mapf rhs = (case rhs
827 :     of GLOBAL _ => rhs
828 :     | STATE _ => rhs
829 :     | VAR x => VAR(f x)
830 :     | LIT _ => rhs
831 :     | OP(rator, xs) => OP(rator, List.map f xs)
832 :     | CONS(xs, ty) => CONS(List.map f xs, ty)
833 :     | SEQ(xs, ty) => SEQ(List.map f xs, ty)
834 :     | EINAPP(ein, xs) => EINAPP(ein, List.map f xs)
835 :     (* end case *))
836 :     in
837 :     mapf
838 :     end
839 :    
840 :     fun app f = let
841 :     fun appf rhs = (case rhs
842 :     of GLOBAL _ => ()
843 :     | STATE _ => ()
844 :     | VAR x => f x
845 :     | LIT _ => ()
846 :     | OP(rator, xs) => List.app f xs
847 :     | CONS(xs, _) => List.app f xs
848 :     | SEQ(xs, _) => List.app f xs
849 :     | EINAPP(_, xs) => List.app f xs
850 :     (* end case *))
851 :     in
852 :     appf
853 :     end
854 :    
855 :     (* return a string representation of a rhs *)
856 :     fun toString rhs = (case rhs
857 :     of GLOBAL x => GlobalVar.toString x
858 :     | STATE x => StateVar.toString x
859 :     | VAR x => Var.toString x
860 :     | LIT lit => Literal.toString lit
861 :     | OP(rator, xs) => String.concat [
862 :     Op.toString rator,
863 :     "(", String.concatWithMap "," Var.toString xs, ")"
864 :     ]
865 :     | CONS(xs, ty) => String.concat [
866 :     "<", Ty.toString ty, ">[",
867 :     String.concatWithMap "," Var.toString xs, "]"
868 :     ]
869 :     | SEQ(xs, ty) => String.concat [
870 :     "<", Ty.toString ty, ">{",
871 :     String.concatWithMap "," Var.toString xs, "}"
872 :     ]
873 :     (* FIXME: proper printing of Ein applications *)
874 :     | EINAPP(ein, xs) => String.concat [
875 :     "EIN(", String.concatWithMap "," Var.toString xs, ")"
876 :     ]
877 :     (* end case *))
878 :     end
879 :    
880 :     (* return a string representation of a variable binding *)
881 :     fun vbToString VB_NONE = "NONE"
882 :     | vbToString (VB_RHS rhs) = concat["RHS(", RHS.toString rhs, ")"]
883 :     | vbToString (VB_MULTIOP(i, rator, xs)) = concat[
884 :     "MULTIOP(", Op.toString rator,
885 :     "[", String.concatWithMap "," Var.toString xs, "])"
886 :     ]
887 :     | vbToString (VB_PHI xs) = concat[
888 :     "PHI(",
889 :     String.concatWithMap "," (fn NONE => "_" | SOME x => Var.toString x) xs, ")"
890 :     ]
891 :     | vbToString VB_PARAM = "PARAM"
892 :    
893 :     (* return a string representation of a PHI node *)
894 :     fun phiToString (y, xs) = String.concat [
895 :     Ty.toString(Var.ty y), " ", Var.toString y, " = PHI(",
896 :     String.concatWithMap "," (fn NONE => "_" | SOME x => Var.toString x) xs, ")"
897 :     ]
898 :    
899 :     (* return a string representation of an assignment *)
900 :     fun assignToString (y, rhs) =
901 :     String.concat [Ty.toString(Var.ty y), " ", Var.toString y, " = ", RHS.toString rhs]
902 :     fun massignToString (ys, rator, xs) = String.concat [
903 :     "(", String.concatWithMap ","
904 :     (fn y => concat[Ty.toString(Var.ty y), " ", Var.toString y]) ys,
905 :     " = ", Op.toString rator,
906 :     "(", String.concatWithMap "," Var.toString xs, ")"
907 :     ]
908 :     fun assignmentToString (ASSGN asgn) = assignToString asgn
909 :     | assignmentToString (MASSGN masgn) = massignToString masgn
910 :     | assignmentToString (GASSGN(lhs, rhs)) = String.concat[
911 :     GlobalVar.toString lhs, " = ", Var.toString rhs
912 :     ]
913 :     | assignmentToString (SAV(lhs, rhs)) = String.concat[
914 :     StateVar.toString lhs, " = ", Var.toString rhs
915 :     ]
916 :    
917 :     end (* SSAFn *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0