| 1 |
(* |
| 2 |
* Convert the old cluster format into the new control flow graph format |
| 3 |
* |
| 4 |
* -- Allen |
| 5 |
*) |
| 6 |
|
| 7 |
signature CLUSTER2CFG = |
| 8 |
sig |
| 9 |
structure CFG : CONTROL_FLOW_GRAPH |
| 10 |
structure F : FLOWGRAPH |
| 11 |
sharing CFG.I = F.I |
| 12 |
sharing CFG.P = F.P |
| 13 |
|
| 14 |
val cluster2cfg : F.cluster -> CFG.cfg |
| 15 |
|
| 16 |
end |
| 17 |
|
| 18 |
functor Cluster2CFG |
| 19 |
(structure CFG : CONTROL_FLOW_GRAPH |
| 20 |
structure Util : CFG_UTIL |
| 21 |
structure Flowgraph : FLOWGRAPH |
| 22 |
structure InsnProps : INSN_PROPERTIES |
| 23 |
sharing Util.CFG = CFG |
| 24 |
sharing CFG.I = Flowgraph.I = InsnProps.I |
| 25 |
sharing CFG.P = Flowgraph.P |
| 26 |
) : CLUSTER2CFG = |
| 27 |
struct |
| 28 |
|
| 29 |
structure CFG = CFG |
| 30 |
structure I = CFG.I |
| 31 |
structure F = Flowgraph |
| 32 |
structure G = Graph |
| 33 |
structure W = CFG.W |
| 34 |
|
| 35 |
fun error msg = MLRiscErrorMsg.error("Cluster2CFG",msg) |
| 36 |
|
| 37 |
fun cluster2cfg |
| 38 |
(F.CLUSTER{blocks, entry, exit, blkCounter, annotations,...})= |
| 39 |
let fun id(F.BBLOCK{blknum, ...}) = blknum |
| 40 |
| id(F.ENTRY {blknum, ...}) = blknum |
| 41 |
| id(F.EXIT {blknum, ...}) = blknum |
| 42 |
| id _ = error "id" |
| 43 |
|
| 44 |
fun idfreq(blk,freq) = (id blk,freq) |
| 45 |
|
| 46 |
fun first_block (F.BBLOCK{blknum,...}::_) = blknum |
| 47 |
| first_block (_::bs) = first_block bs |
| 48 |
| first_block [] = error "first_block" |
| 49 |
|
| 50 |
val reorder = ref false |
| 51 |
val info = CFG.INFO { firstBlock = ref(first_block blocks), |
| 52 |
reorder = reorder, |
| 53 |
annotations = annotations |
| 54 |
} |
| 55 |
|
| 56 |
val CFG as G.GRAPH cfg = CFG.cfg(info) |
| 57 |
|
| 58 |
val F.ENTRY{ blknum = ENTRY, ... } = entry |
| 59 |
val F.EXIT{ blknum = EXIT, ... } = exit |
| 60 |
|
| 61 |
(* Add a list of blocks into the CFG *) |
| 62 |
fun add(F.ENTRY e::rest,Ps,Ls) = add_entry(e,Ps,Ls,rest) |
| 63 |
| add(F.EXIT e::rest,Ps,Ls) = add_exit(e,Ps,Ls,rest) |
| 64 |
| add(F.BBLOCK b::rest,Ps,Ls) = add_block(b,rev Ps,Ls,rest) |
| 65 |
| add((F.PSEUDO p)::rest,Ps,Ls) = |
| 66 |
add(rest,CFG.PSEUDO p::map CFG.LABEL Ls@Ps,[]) |
| 67 |
| add((F.LABEL l)::rest,Ps,Ls) = add(rest,Ps,l::Ls) |
| 68 |
| add([],Ps,Ls) = finish(Ps,Ls) |
| 69 |
|
| 70 |
(* Insert an entry node *) |
| 71 |
and add_entry({blknum, succ, freq}, [], [], rest) = |
| 72 |
( #add_node cfg (blknum,CFG.newStart(blknum,freq)); |
| 73 |
#set_entries cfg [blknum]; |
| 74 |
app (fn (blk,w) => add_edge(blknum, id blk, CFG.JUMP, w)) |
| 75 |
(!succ); |
| 76 |
add(rest, [], []) |
| 77 |
) |
| 78 |
| add_entry _ = error "add_entry" |
| 79 |
|
| 80 |
(* Insert an exit node *) |
| 81 |
and add_exit({blknum, pred, freq}, [], [], rest) = |
| 82 |
( #add_node cfg (blknum, CFG.newStop(blknum,freq)); |
| 83 |
#set_exits cfg [blknum]; |
| 84 |
add(rest, [], []) |
| 85 |
) |
| 86 |
|
| 87 |
(* Insert an normal basic block *) |
| 88 |
and add_block({blknum,annotations, |
| 89 |
freq,liveIn,liveOut,succ,pred,insns}, |
| 90 |
Ps,Ls,rest) = |
| 91 |
let val an = !annotations |
| 92 |
val an = |
| 93 |
case !succ of |
| 94 |
([(F.EXIT _,_)] | []) => #create CFG.LIVEOUT(!liveOut)::an |
| 95 |
| _ => an |
| 96 |
val bb = CFG.BLOCK{id = blknum, |
| 97 |
kind = CFG.NORMAL, |
| 98 |
labels= ref Ls, |
| 99 |
freq = freq, |
| 100 |
data = ref Ps, |
| 101 |
insns = insns, |
| 102 |
annotations=ref an |
| 103 |
} |
| 104 |
in #add_node cfg (blknum,bb); |
| 105 |
add_edges(blknum, !succ, !insns); |
| 106 |
(* |
| 107 |
add_call_edges(blknum, !callSucc); |
| 108 |
add_return_edge(blknum, !returnSucc); |
| 109 |
*) |
| 110 |
add(rest, [], []) |
| 111 |
end |
| 112 |
|
| 113 |
(* Finished insertion *) |
| 114 |
and finish([],[]) = () |
| 115 |
| finish(Ps,[]) = |
| 116 |
let val CFG.BLOCK{data,labels,...} = #node_info cfg EXIT |
| 117 |
in data := Ps @ !data |
| 118 |
end |
| 119 |
| finish _ = error "finish" |
| 120 |
|
| 121 |
(* Insert one edge into the flowgraph *) |
| 122 |
and add_edge(i,j,k,w) = |
| 123 |
let val k = if ENTRY = i then CFG.ENTRY |
| 124 |
else if EXIT = j then CFG.EXIT |
| 125 |
else k |
| 126 |
in #add_edge cfg (i,j,CFG.EDGE{k=k,w=w,a=ref []}) |
| 127 |
end |
| 128 |
|
| 129 |
(* Add edges into the flowgraph *) |
| 130 |
and add_edges (i, succs, insns) = |
| 131 |
let fun is_fallsthru (j,yes,no) = |
| 132 |
if j = i + 1 then |
| 133 |
(case insns of |
| 134 |
jmp::_ => (case InsnProps.instrKind jmp of |
| 135 |
InsnProps.IK_JUMP => no |
| 136 |
| _ => yes |
| 137 |
) |
| 138 |
| _ => yes) |
| 139 |
else no |
| 140 |
fun add_branch(i,(j,jw),(k,kw)) = |
| 141 |
let val j = id j and k = id k |
| 142 |
in if j = i + 1 then |
| 143 |
( add_edge(i,j,CFG.BRANCH false,jw); |
| 144 |
add_edge(i,k,CFG.BRANCH true,kw)) |
| 145 |
else if k = i + 1 then |
| 146 |
( add_edge(i,k,CFG.BRANCH false,kw); |
| 147 |
add_edge(i,j,CFG.BRANCH true,jw)) |
| 148 |
else error "add_branch" |
| 149 |
end |
| 150 |
fun add_switch(i,_,[]) = () |
| 151 |
| add_switch(i,k,(j,jw)::js) = |
| 152 |
(add_edge(i,id j,CFG.SWITCH k,jw); add_switch(i,k+1,js)) |
| 153 |
in case succs of |
| 154 |
[] => () |
| 155 |
| [(j,w)] => |
| 156 |
let val j = id j |
| 157 |
in add_edge(i,j,is_fallsthru(j,CFG.FALLSTHRU,CFG.JUMP),w) |
| 158 |
end |
| 159 |
| [j,k] => add_branch(i,j,k) |
| 160 |
| js => add_switch(i,0,js) |
| 161 |
end |
| 162 |
|
| 163 |
fun check_for_bad_entries() = |
| 164 |
app (fn (i,j,e) => |
| 165 |
if j = EXIT then () |
| 166 |
else |
| 167 |
case #in_edges cfg j of |
| 168 |
[_] => () (* only edge from ENTRY, okay *) |
| 169 |
| _ => |
| 170 |
error("entry "^Int.toString j^" has internal edges") |
| 171 |
) (#out_edges cfg ENTRY) |
| 172 |
|
| 173 |
(* add edge from entry to exit *) |
| 174 |
fun insert_entry_to_exit () = |
| 175 |
if #has_edge cfg (ENTRY,EXIT) then () |
| 176 |
else add_edge (ENTRY,EXIT,CFG.JUMP,ref 0) |
| 177 |
in |
| 178 |
add(entry::exit::blocks,[],[]); |
| 179 |
check_for_bad_entries(); |
| 180 |
insert_entry_to_exit(); |
| 181 |
CFG |
| 182 |
end |
| 183 |
|
| 184 |
end |
| 185 |
|