Debian Codegraph @

lib/mlrisc-lib/MLRISC/frequencies/complete-probs-fn.sml

raw
1 (* complete-probs-fn.sml
2 *
3 * COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies
4 *
5 * Given a CFG that may have incomplete branch probability information,
6 * fill in the information.
7 *)
8
9
10 functor CompleteProbsFn (
11 structure CFG : CONTROL_FLOW_GRAPH
12 (* function to record edge probabilities *)
13 val recordProb : (CFG.edge_info * real) -> unit
14 ) : sig
15
16 structure CFG : CONTROL_FLOW_GRAPH
17
18 val completeProbs : CFG.cfg -> unit
19
20 end = struct
21
22 structure CFG = CFG
23 structure Prob = Probability
24
25 val dumpCFG = MLRiscControl.mkFlag
26 ("dump-cfg-after-complete-probs",
27 "when true, CFG is output after probability completion")
28
29 val {get=getProb, ...} = MLRiscAnnotations.BRANCH_PROB
30
31 (* Complete edge probabilities. *)
32 fun completeProbs (cfg as Graph.GRAPH{forall_nodes, out_edges, ...}) = let
33 fun doBlock (blkId, _) = let
34 fun computeProbs ((_, _, e as CFG.EDGE{a, ...})::r, remaining, n, es) = (
35 case getProb(!a)
36 of NONE => computeProbs (r, remaining, n+1, e::es)
37 | SOME p => (
38 recordProb (e, Prob.toReal p);
39 computeProbs (r, Prob.-(remaining, p), n, es))
40 (* end case *))
41 | computeProbs ([], _, 0, _) = ()
42 | computeProbs ([], remaining, n, es) = let
43 val p = Prob.toReal(Prob./(remaining, n))
44 in
45 List.app (fn e => recordProb (e, p)) es
46 end
47 in
48 computeProbs (out_edges blkId, Prob.always, 0, [])
49 end
50 in
51 forall_nodes doBlock
52 before (if !dumpCFG then CFG.dump (!MLRiscControl.debug_stream,
53 "after probability completion", cfg)
54 else ())
55 end
56
57 end