Debian Codegraph @

lib/mlrisc-lib/MLRISC/IR/static-branch-prediction.sml

raw
1 (*
2 * This module performs static branch prediction using heuristics
3 * similar to Ball and Larus'
4 *
5 * -- Allen
6 *)
7
8 functor StaticBranchPrediction
9 (structure IR : MLRISC_IR
10 structure InsnProps : INSN_PROPERTIES
11 structure FreqProps : FREQUENCY_PROPERTIES
12 sharing IR.I = FreqProps.I = InsnProps.I
13 val loopMultiplier : int
14 ) : MLRISC_IR_OPTIMIZATION =
15 struct
16
17 structure IR = IR
18 structure CFG = IR.CFG
19 structure CompFreq = ComputeFrequencies(structure Loop = IR.Loop
20 structure Freq = CFG.W)
21
22 type flowgraph = IR.IR
23
24 val name = "StaticBranchPrediction"
25 fun run IR =
26 let fun branchProb(CFG.BLOCK{insns,...}) =
27 case !insns of
28 [] => 100 (* the fallsthru edge is always taken *)
29 | jmp::_ =>
30 (case InsnProps.instrKind jmp of
31 InsnProps.IK_JUMP => FreqProps.branchProb jmp
32 | _ => 100 (* the fallsthru edge is always taken *)
33 )
34
35
36 fun nodeFreq(CFG.BLOCK{freq,...}) = freq
37 fun edgeFreq(CFG.EDGE{w,...}) = w
38
39 (* is this a conditional branch and is it in the taken direction? *)
40 fun isTakenBranch(_,_,CFG.EDGE{k=CFG.BRANCH b,...}) = b
41 | isTakenBranch _ = false
42
43 in CompFreq.compute_frequencies
44 { cfg = IR,
45 loop = IR.loop IR,
46 loopMultiplier = loopMultiplier,
47 nodeFreq = nodeFreq,
48 edgeFreq = edgeFreq,
49 branchProb = branchProb,
50 isTakenBranch = isTakenBranch
51 };
52 IR
53 end
54
55 end