| 1 |
(* |
| 2 |
* Module for adding preheaders and splitting critical edges |
| 3 |
* |
| 4 |
* -- Allen |
| 5 |
*) |
| 6 |
|
| 7 |
functor CFGStructuring(IR : MLRISC_IR) : CFG_STRUCTURING = |
| 8 |
struct |
| 9 |
|
| 10 |
structure IR = IR |
| 11 |
structure CFG = IR.CFG |
| 12 |
structure Loop = IR.Loop |
| 13 |
structure Util = IR.Util |
| 14 |
structure W = CFG.W |
| 15 |
structure G = Graph |
| 16 |
|
| 17 |
structure Reshape = ControlFlowGraphRestructureFn(structure Loop = Loop) |
| 18 |
|
| 19 |
fun reshape { add_preheader, split_critical_edges } IR = |
| 20 |
let val CFG as G.GRAPH cfg = IR |
| 21 |
val loop = IR.loop IR |
| 22 |
|
| 23 |
fun is_falls_thru(_,_,CFG.EDGE{k=CFG.BRANCH false,...}) = true |
| 24 |
| is_falls_thru(_,_,CFG.EDGE{k=CFG.FALLSTHRU,...}) = true |
| 25 |
| is_falls_thru(_,_,_) = false |
| 26 |
|
| 27 |
fun ins_preheader{header,entries=[]} = () |
| 28 |
| ins_preheader{header,entries=[_]} = () |
| 29 |
| ins_preheader{header=(h,h'),entries} = |
| 30 |
let fun sum_weights([],n) = n |
| 31 |
| sum_weights((_,_,CFG.EDGE{w,a,...})::es,n) = |
| 32 |
sum_weights(es,!w + n) |
| 33 |
val w = sum_weights(entries,W.zero) |
| 34 |
val CFG.BLOCK{annotations=old_an,...} = h' |
| 35 |
val p = #new_id cfg () |
| 36 |
val (preheader as CFG.BLOCK{freq,annotations,...}, new_edge) = |
| 37 |
if List.exists is_falls_thru entries then |
| 38 |
(CFG.empty_block(p,w), |
| 39 |
(p,h,CFG.EDGE{k=CFG.FALLSTHRU,w=ref w,a=a})) |
| 40 |
else |
| 41 |
(CFG.jump_block(p,CFG.define_label h',w), |
| 42 |
(p,h,CFG.EDGE{k=CFG.JUMP,w=ref w,a=a})) |
| 43 |
val new_entries = map (fn (i,j,e) => (i,p,e)) entries |
| 44 |
in annotations := !old_an; |
| 45 |
app (fn (i,j,_) => #remove_edge cfg (i,j)) entries; |
| 46 |
app (#add_edge cfg) new_entries; |
| 47 |
#add_node cfg (p,preheader); |
| 48 |
#add_edge cfg new_edge; |
| 49 |
app (fn (i,_,_) => Util.set_target(CFG,i)) new_entries |
| 50 |
end |
| 51 |
|
| 52 |
in Reshape.restructure (CFG,loop) |
| 53 |
{ add_preheader = if add_preheader then SOME ins_preheader |
| 54 |
else NONE, |
| 55 |
add_landing_pad = NONE |
| 56 |
}; |
| 57 |
CFG.changed CFG |
| 58 |
end |
| 59 |
|
| 60 |
end |
| 61 |
|