| 1 |
(* |
| 2 |
* This module computes frequencies when given branch probabilities. |
| 3 |
* Bug fix: |
| 4 |
* This module didn't work on irreducible flowgraphs! |
| 5 |
* The problem was caused |
| 6 |
* |
| 7 |
* -- Allen |
| 8 |
*) |
| 9 |
|
| 10 |
functor ComputeFrequencies |
| 11 |
(structure Loop : LOOP_STRUCTURE |
| 12 |
structure Freq : FREQ |
| 13 |
) : COMPUTE_FREQUENCIES = |
| 14 |
struct |
| 15 |
|
| 16 |
structure Loop = Loop |
| 17 |
structure Dom = Loop.Dom |
| 18 |
structure G = Graph |
| 19 |
structure S = BitSet |
| 20 |
structure W = Freq |
| 21 |
structure A = Array |
| 22 |
structure H = HashArray |
| 23 |
|
| 24 |
val op div = W.div |
| 25 |
|
| 26 |
fun compute_frequencies |
| 27 |
{cfg,loop,loopMultiplier,nodeFreq,edgeFreq,branchProb,isTakenBranch} = |
| 28 |
let val G.GRAPH cfg = cfg |
| 29 |
val Loop as G.GRAPH loop = loop |
| 30 |
val ENTRY = case #entries cfg () of |
| 31 |
[ENTRY] => ENTRY |
| 32 |
| _ => raise Graph.NotSingleEntry |
| 33 |
val N = #capacity cfg () |
| 34 |
val marked = S.create N |
| 35 |
val number_of_entries = length(#out_edges cfg ENTRY) |
| 36 |
val entry_weight = W.*(W.fromInt 100,number_of_entries) |
| 37 |
|
| 38 |
(* indexed by headers *) |
| 39 |
val likely_exits = H.array(N,[]) |
| 40 |
val exit_counts = H.array(N,0) |
| 41 |
|
| 42 |
(* indexed by nodes *) |
| 43 |
val entry_edges = A.tabulate(N,#in_edges cfg) |
| 44 |
val header_of = Loop.header Loop |
| 45 |
val nodeFreqs = A.array(N,0) |
| 46 |
val branchProbs = A.array(N,0) |
| 47 |
val TIMES = 20 |
| 48 |
|
| 49 |
val _ = #forall_nodes cfg (fn (b,b') => |
| 50 |
(A.update(nodeFreqs,b,!(nodeFreq b')); |
| 51 |
A.update(branchProbs,b,branchProb b') |
| 52 |
)) |
| 53 |
|
| 54 |
fun is_exit_edge (e as (i,j,_)) = |
| 55 |
List.exists (fn (i',j',_) => i = i' andalso j = j') |
| 56 |
(H.sub(likely_exits,A.sub(header_of,i))) |
| 57 |
|
| 58 |
val sum = List.foldr (fn ((_,_,e),m) => !(edgeFreq e) + m) 0 |
| 59 |
|
| 60 |
fun exit_weight_of i = |
| 61 |
let val h = A.sub(header_of,i) |
| 62 |
val w = A.sub(nodeFreqs,h) |
| 63 |
in w div (loopMultiplier * H.sub(exit_counts,h)) |
| 64 |
end |
| 65 |
|
| 66 |
val entryEdges = Loop.entryEdges Loop |
| 67 |
|
| 68 |
fun preprocess(header,Loop.LOOP{exits,...}) = |
| 69 |
let val real_exits = |
| 70 |
List.filter (fn (i,_,_) => A.sub(branchProbs,i) > 0) exits |
| 71 |
in H.update(likely_exits,header,real_exits); |
| 72 |
H.update(exit_counts,header,length real_exits); |
| 73 |
A.update(entry_edges,header,entryEdges header) |
| 74 |
end |
| 75 |
|
| 76 |
fun propagate(0,_) = (print "Out of time\n") |
| 77 |
| propagate(n,[]) = () |
| 78 |
| propagate(n,i::worklist) = |
| 79 |
let val _ = S.reset(marked,i) |
| 80 |
val old_weight = A.sub(nodeFreqs,i) |
| 81 |
val new_weight = sum(A.sub(entry_edges,i)) |
| 82 |
val new_weight = if i = ENTRY then entry_weight |
| 83 |
else (case H.sub(likely_exits,i) of |
| 84 |
[] => new_weight (* not a real loop! *) |
| 85 |
| _ => W.*(new_weight,loopMultiplier) |
| 86 |
) |
| 87 |
in if old_weight = new_weight then |
| 88 |
propagate(n,worklist) |
| 89 |
else (A.update(nodeFreqs,i,new_weight); |
| 90 |
propagate_edge_weight(#out_edges cfg i,new_weight,[]); |
| 91 |
propagate'(n,#out_edges cfg i,worklist) |
| 92 |
) |
| 93 |
end |
| 94 |
|
| 95 |
and propagate'(n,[],worklist) = propagate(n,worklist) |
| 96 |
| propagate'(n,(i,j,_)::es,worklist) = |
| 97 |
if S.markAndTest(marked,j) then |
| 98 |
propagate'(n,es,worklist) |
| 99 |
else propagate'(Int.-(n,1),es,j::worklist) |
| 100 |
|
| 101 |
and propagate_edge_weight([],W,es') = process_non_exits(W,es') |
| 102 |
| propagate_edge_weight((edge as (i,_,e))::es,W,es') = |
| 103 |
if is_exit_edge edge then |
| 104 |
let val exit_weight = exit_weight_of(A.sub(header_of,i)) |
| 105 |
val w = edgeFreq e |
| 106 |
in w := exit_weight; |
| 107 |
propagate_edge_weight(es,W-exit_weight,es') |
| 108 |
end |
| 109 |
else |
| 110 |
propagate_edge_weight(es,W,edge::es') |
| 111 |
|
| 112 |
and process_non_exits(W,[]) = () |
| 113 |
| process_non_exits(W,[(_,_,e)]) = edgeFreq e := W |
| 114 |
| process_non_exits(W,es as [edge1 as (i,_,e1),(_,_,e2)]) = |
| 115 |
if i = ENTRY then divide_evenly(W,es) else |
| 116 |
let val w = edgeFreq e1 |
| 117 |
val w' = edgeFreq e2 |
| 118 |
val (w_F,w_T) = if isTakenBranch edge1 then (w',w) else (w,w') |
| 119 |
val p = A.sub(branchProbs,i) |
| 120 |
in w_T := W.*(W,p) div 100; |
| 121 |
w_F := W - !w_T |
| 122 |
end |
| 123 |
| process_non_exits(W,es) = divide_evenly(W,es) |
| 124 |
|
| 125 |
and divide_evenly(W,es) = |
| 126 |
let val W' = W div (length es) |
| 127 |
in app (fn (_,_,e) => edgeFreq e := W') es |
| 128 |
end |
| 129 |
|
| 130 |
in |
| 131 |
#forall_nodes loop preprocess; |
| 132 |
propagate(TIMES * N, [ENTRY]); |
| 133 |
#forall_nodes cfg (fn (b,b') => nodeFreq b' := A.sub(nodeFreqs,b)) |
| 134 |
end handle Overflow => print "[Overflow]\n" |
| 135 |
|
| 136 |
end |