| 1 |
(* compute-freqs-fn.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies. |
| 4 |
* |
| 5 |
* Compute block and edge weights (frequencies) from edge probabilities. |
| 6 |
* This algorithm uses symbolic simplification of the frequency equations. |
| 7 |
* It handles unstructured loops. |
| 8 |
*) |
| 9 |
|
| 10 |
functor ComputeFreqsFn ( |
| 11 |
structure CFG : CONTROL_FLOW_GRAPH |
| 12 |
) : COMPUTE_EXECUTION_FREQUENCIES = |
| 13 |
struct |
| 14 |
|
| 15 |
structure CFG = CFG |
| 16 |
structure Prob = Probability |
| 17 |
structure F = Format |
| 18 |
|
| 19 |
(* flags *) |
| 20 |
val dumpFreqs = MLRiscControl.mkFlag ( |
| 21 |
"dump-frequencies", |
| 22 |
"when true, block and edge frequencies are output") |
| 23 |
val dumpCFG = MLRiscControl.mkFlag ( |
| 24 |
"dump-cfg-after-frequencies", |
| 25 |
"when true, the CFG is output after frequency computation") |
| 26 |
fun pr s = TextIO.output(!MLRiscControl.debug_stream, s) |
| 27 |
fun prf (fmt, items) = pr(F.format fmt items) |
| 28 |
|
| 29 |
(* Complete edge probabilities; we use the edge weights to store this |
| 30 |
* information. |
| 31 |
*) |
| 32 |
structure CompleteProbs = CompleteProbsFn ( |
| 33 |
structure CFG = CFG |
| 34 |
fun recordProb (CFG.EDGE{w, ...}, p) = (w := p)) |
| 35 |
|
| 36 |
fun getProb (CFG.EDGE{w, ...}) = !w |
| 37 |
|
| 38 |
(* fudge factor for infinite loops. *) |
| 39 |
val epsilon = 1.0e~6 |
| 40 |
|
| 41 |
(***** Representation of equations *****) |
| 42 |
type var = Graph.node_id |
| 43 |
type term = (real * var) |
| 44 |
type sum = {terms : term list, c : real} |
| 45 |
datatype def = Unknown | Sum of sum |
| 46 |
|
| 47 |
val zero = {c = 0.0, terms = []} |
| 48 |
val one = {c = 1.0, terms = []} |
| 49 |
|
| 50 |
(* multiply a term by a scalar *) |
| 51 |
fun scale (coeff : real) (a, x) = (coeff*a, x) |
| 52 |
|
| 53 |
fun compute (cfg as Graph.GRAPH methods) = let |
| 54 |
val {in_edges, out_edges, node_info, capacity, ...} = methods |
| 55 |
val defs = Array.array(capacity(), Unknown) |
| 56 |
fun getVar id = Array.sub(defs, id) |
| 57 |
fun setVar (id, s) = Array.update(defs, id, s) |
| 58 |
(* if a node has been visited, then it has a definition *) |
| 59 |
fun visited id = ( |
| 60 |
case Array.sub(defs, id) of Unknown => false | _ => true) |
| 61 |
(** computations on sums **) |
| 62 |
(* if a variable is defined, compute the normal form of its definition |
| 63 |
* and return it. If the variable is unknown or its definition is |
| 64 |
* already in normal form, then return NONE. |
| 65 |
*) |
| 66 |
fun normalizeVar v = (case getVar v |
| 67 |
of Unknown => Unknown |
| 68 |
| Sum s => (case normalizeSum s |
| 69 |
of NONE => Sum s |
| 70 |
| SOME s' => let val sum = Sum s' |
| 71 |
in |
| 72 |
setVar(v, sum); sum |
| 73 |
end |
| 74 |
(* end case *)) |
| 75 |
(* end case *)) |
| 76 |
(* normalize a sum of scaled variables; if the sum is already normalized, |
| 77 |
* then return NONE. |
| 78 |
*) |
| 79 |
and normalizeSum ({terms, c} : sum) = let |
| 80 |
fun extract ((t as (b, y))::r, ts, todo : (real * sum) list) = ( |
| 81 |
case normalizeVar y |
| 82 |
of Unknown => extract(r, t::ts, todo) |
| 83 |
| Sum s => extract(r, ts, (b, s)::todo) |
| 84 |
(* end case *)) |
| 85 |
| extract ([], _, []) = NONE |
| 86 |
| extract ([], ts, todo) = |
| 87 |
SOME(addDefs ({terms=List.rev ts, c=c}, todo)) |
| 88 |
and addDefs (acc, []) = acc |
| 89 |
| addDefs (acc, (coeff, sum)::r) = |
| 90 |
addDefs (addScaled(acc, coeff, sum), r) |
| 91 |
in |
| 92 |
extract (terms, [], []) |
| 93 |
end |
| 94 |
(* compute r1 + coeff*r2, where r1 and r2 are normalized; the result |
| 95 |
* is normalized. |
| 96 |
*) |
| 97 |
and addScaled (r1 : sum, coeff : real, r2 : sum) = let |
| 98 |
fun combine ([], ts) = List.map (scale coeff) ts |
| 99 |
| combine (ts, []) = ts |
| 100 |
| combine (ts1 as (t1::r1), ts2 as (t2::r2)) = |
| 101 |
if (#2 t1 < #2 t2) |
| 102 |
then t1 :: combine(r1, ts2) |
| 103 |
else if (#2 t1 = #2 t2) |
| 104 |
then (#1 t1 + (coeff * #1 t2), #2 t1) :: combine (r1, r2) |
| 105 |
else (scale coeff t2) :: combine(ts1, r2) |
| 106 |
in |
| 107 |
{ c = #c r1 + coeff * #c r2, |
| 108 |
terms = combine(#terms r1, #terms r2) |
| 109 |
} |
| 110 |
end |
| 111 |
(* add the term (a*x) to a normalized term; we assume that x is Undefined. *) |
| 112 |
fun addScaledVar ({c, terms}, a : real, x) = let |
| 113 |
fun insert [] = [(a, x)] |
| 114 |
| insert ((t as (b, y))::r) = |
| 115 |
if (y < x) |
| 116 |
then t :: insert r |
| 117 |
else if (y = x) |
| 118 |
then (a+b, x) :: r |
| 119 |
else (a, x) :: t :: r |
| 120 |
in |
| 121 |
{c = c, terms = insert terms} |
| 122 |
end |
| 123 |
(* given a list of incoming edges, create the rhs sum. *) |
| 124 |
fun makeRHS preds = let |
| 125 |
fun f ((src, _, e), acc) = let |
| 126 |
val prob = getProb e |
| 127 |
in |
| 128 |
case normalizeVar src |
| 129 |
of Unknown => addScaledVar (acc, prob, src) |
| 130 |
| Sum sum => addScaled (acc, prob, sum) |
| 131 |
(* end case *) |
| 132 |
end |
| 133 |
in |
| 134 |
List.foldl f zero preds |
| 135 |
end |
| 136 |
(* Simplify the equation "x = rhs" by checking for x in rhs. We assume that |
| 137 |
* x is undefined and that the rhs is normaized. We return the simplified |
| 138 |
* rhs. |
| 139 |
*) |
| 140 |
fun simplify (x, rhs as {terms, c}) = let |
| 141 |
fun removeX ([], _) = rhs |
| 142 |
| removeX ((t as (a, y))::r, ts) = |
| 143 |
if (x < y) |
| 144 |
then rhs |
| 145 |
else if (x = y) |
| 146 |
then let |
| 147 |
val s = 1.0 / Real.max(1.0 - a, epsilon) |
| 148 |
val terms = List.revAppend(ts, r) |
| 149 |
in |
| 150 |
{c = s*c, terms = List.map (scale s) terms} |
| 151 |
end |
| 152 |
else removeX(r, t::ts) |
| 153 |
in |
| 154 |
removeX (terms, []) |
| 155 |
end |
| 156 |
(* INVARIANT: the variables corresponding to marked nodes are not Unknown |
| 157 |
* in the rhs of any equation. |
| 158 |
*) |
| 159 |
fun dfs id = if (visited id) |
| 160 |
then () |
| 161 |
else let |
| 162 |
val rhs = makeRHS (in_edges id) |
| 163 |
val rhs = simplify (id, rhs) |
| 164 |
in |
| 165 |
setVar (id, Sum rhs); |
| 166 |
followEdges (out_edges id) |
| 167 |
end |
| 168 |
and followEdges [] = () |
| 169 |
| followEdges ((_, dst, _)::r) = (dfs dst; followEdges r) |
| 170 |
val root = |
| 171 |
case #entries methods () of |
| 172 |
[root] => root |
| 173 |
| _ => raise Fail "ComputeFreqsFn: root" |
| 174 |
in |
| 175 |
(* initialize edge probabilities *) |
| 176 |
CompleteProbs.completeProbs cfg; |
| 177 |
(* initialize the root *) |
| 178 |
setVar (root, Sum one); |
| 179 |
(* traverse the successors of the root *) |
| 180 |
followEdges (out_edges root); |
| 181 |
(* record block and edge frequencies in CFG *) |
| 182 |
#forall_nodes methods (fn (id, CFG.BLOCK{freq, ...}) => ( |
| 183 |
case normalizeVar id |
| 184 |
of Unknown => freq := 0.0 |
| 185 |
| Sum{c, terms=[]} => freq := c |
| 186 |
| _ => raise Fail (concat[ |
| 187 |
"block ", Int.toString id, " unresolved" |
| 188 |
]) |
| 189 |
(* end case *))); |
| 190 |
#forall_edges methods (fn (src, _, CFG.EDGE{w, ...}) => let |
| 191 |
val CFG.BLOCK{freq, ...} = node_info src |
| 192 |
in |
| 193 |
w := !w * !freq |
| 194 |
end); |
| 195 |
if !dumpFreqs |
| 196 |
then let |
| 197 |
fun bfreq (id, CFG.BLOCK{kind, freq, ...}) = |
| 198 |
prf("\tbfreq(%s %d) = %f\n", [ |
| 199 |
F.STR(CFG.kindName kind), F.INT id, F.REAL(!freq) |
| 200 |
]) |
| 201 |
fun freq (src, dst, info as CFG.EDGE{w, ...}) = |
| 202 |
prf("\tfreq(%d->%d:%s) = %f\n", [ |
| 203 |
F.INT src, F.INT dst, F.STR(CFG.show_edge info), |
| 204 |
F.REAL(!w) |
| 205 |
]) |
| 206 |
in |
| 207 |
pr "[ computed frequencies ]\n"; |
| 208 |
#forall_nodes methods bfreq; |
| 209 |
#forall_edges methods freq |
| 210 |
end |
| 211 |
else (); |
| 212 |
if !dumpCFG |
| 213 |
then CFG.dump ( |
| 214 |
!MLRiscControl.debug_stream, |
| 215 |
"after frequency computation", cfg) |
| 216 |
else () |
| 217 |
end |
| 218 |
|
| 219 |
end |