| 1 |
(* |
| 2 |
* Simple module for building the IR etc. Doesn't do any real optimizations. |
| 3 |
* |
| 4 |
* -- Allen |
| 5 |
*) |
| 6 |
|
| 7 |
functor MLRISCGlue |
| 8 |
(structure Asm : INSTRUCTION_EMITTER |
| 9 |
structure Flowgraph : FLOWGRAPH |
| 10 |
structure InsnProps : INSN_PROPERTIES |
| 11 |
structure FreqProps : FREQUENCY_PROPERTIES |
| 12 |
sharing InsnProps.I = Asm.I = Flowgraph.I = FreqProps.I |
| 13 |
sharing Flowgraph.P = Asm.P |
| 14 |
) : MLRISC_GLUE = |
| 15 |
struct |
| 16 |
|
| 17 |
structure F = Flowgraph |
| 18 |
structure I = F.I |
| 19 |
|
| 20 |
val mlrisc = MLRiscControl.getFlag "mlrisc" |
| 21 |
val phases = MLRiscControl.getStringList "mlrisc-phases" |
| 22 |
val view_IR = MLRiscControl.getFlag "view-IR" |
| 23 |
val verbose = MLRiscControl.getFlag "verbose" |
| 24 |
|
| 25 |
fun error msg = MLRiscErrorMsg.error("MLRISCGlue",msg) |
| 26 |
|
| 27 |
structure GraphViewer = GraphViewer(AllDisplays) |
| 28 |
|
| 29 |
structure FormatInsn = FormatInstruction(Asm) |
| 30 |
|
| 31 |
structure CFG = ControlFlowGraph |
| 32 |
(structure I = I |
| 33 |
structure PseudoOps = F.P |
| 34 |
structure GraphImpl = DirectedGraph |
| 35 |
structure InsnProps = InsnProps |
| 36 |
structure Asm = Asm |
| 37 |
) |
| 38 |
|
| 39 |
structure Util = CFGUtil |
| 40 |
(structure CFG = CFG |
| 41 |
structure InsnProps = InsnProps |
| 42 |
) |
| 43 |
|
| 44 |
structure CFG2Cluster = CFG2Cluster |
| 45 |
(structure CFG = CFG |
| 46 |
structure Util = Util |
| 47 |
structure Flowgraph = Flowgraph |
| 48 |
) |
| 49 |
|
| 50 |
structure Cluster2CFG = Cluster2CFG |
| 51 |
(structure CFG = CFG |
| 52 |
structure Util = Util |
| 53 |
structure Flowgraph = Flowgraph |
| 54 |
structure InsnProps = InsnProps |
| 55 |
) |
| 56 |
|
| 57 |
structure Dom = DominatorTree(DirectedGraph) |
| 58 |
|
| 59 |
structure CDG = ControlDependenceGraph |
| 60 |
(structure Dom = Dom |
| 61 |
structure GraphImpl = DirectedGraph |
| 62 |
) |
| 63 |
|
| 64 |
structure Loop = LoopStructure |
| 65 |
(structure Dom = Dom |
| 66 |
structure GraphImpl = DirectedGraph |
| 67 |
) |
| 68 |
|
| 69 |
structure IR = MLRISC_IR |
| 70 |
(structure CFG = CFG |
| 71 |
structure CDG = CDG |
| 72 |
structure Loop = Loop |
| 73 |
structure GraphViewer = GraphViewer |
| 74 |
structure Util = Util |
| 75 |
) |
| 76 |
|
| 77 |
structure Guess = StaticBranchPrediction |
| 78 |
(structure IR = IR |
| 79 |
structure InsnProps = InsnProps |
| 80 |
structure FreqProps = FreqProps |
| 81 |
val loopMultiplier=10 |
| 82 |
) |
| 83 |
|
| 84 |
structure Liveness = LivenessAnalysis(CFG) |
| 85 |
|
| 86 |
structure Reshape = ReshapeBranches |
| 87 |
(structure IR = IR |
| 88 |
structure InsnProps = InsnProps |
| 89 |
) |
| 90 |
|
| 91 |
structure BranchChaining = BranchChaining |
| 92 |
(structure IR = IR |
| 93 |
structure InsnProps = InsnProps |
| 94 |
) |
| 95 |
|
| 96 |
structure CPR = CriticalPathReduction |
| 97 |
(structure IR = IR |
| 98 |
structure InsnProps = InsnProps |
| 99 |
) |
| 100 |
|
| 101 |
structure ClusterGraph = ClusterGraph(Flowgraph) |
| 102 |
|
| 103 |
structure ClusterViewer = ClusterViewer |
| 104 |
(structure GraphViewer = GraphViewer |
| 105 |
structure ClusterGraph = ClusterGraph |
| 106 |
structure Asm = Asm |
| 107 |
) |
| 108 |
|
| 109 |
fun view phase ir = if !view_IR then IR.view phase ir else () |
| 110 |
fun view' cluster = if !view_IR then |
| 111 |
ClusterViewer.view(ClusterGraph.clusterGraph cluster) else () |
| 112 |
|
| 113 |
fun optimize cluster = |
| 114 |
let datatype rep = IR of IR.IR |
| 115 |
| CLUSTER of F.cluster |
| 116 |
fun doPhase "cluster->cfg" (CLUSTER c) = IR(Cluster2CFG.cluster2cfg c) |
| 117 |
| doPhase "cfg->cluster" (IR cfg) = |
| 118 |
CLUSTER(CFG2Cluster.cfg2cluster{cfg=cfg,relayout=false}) |
| 119 |
| doPhase "guess" (r as IR ir) = (Guess.run ir; r) |
| 120 |
| doPhase "reshape" (r as IR ir) = (Reshape.run ir; r) |
| 121 |
| doPhase "branch-chaining" (r as IR ir) = (BranchChaining.run ir; r) |
| 122 |
| doPhase "cpr" (r as IR ir) = (CPR.run ir; r) |
| 123 |
| doPhase "view-cfg" (r as IR ir) = (view "cfg" ir; r) |
| 124 |
| doPhase "view-dom" (r as IR ir) = (view "dom" ir; r) |
| 125 |
| doPhase "view-pdom" (r as IR ir) = (view "pdom" ir; r) |
| 126 |
| doPhase "view-doms" (r as IR ir) = (view "doms" ir; r) |
| 127 |
| doPhase "view-cdg" (r as IR ir) = (view "cdg" ir; r) |
| 128 |
| doPhase "view-loop" (r as IR ir) = (view "loop" ir; r) |
| 129 |
| doPhase "view-cluster" (r as CLUSTER c) = (view' c; r) |
| 130 |
| doPhase phase _ = error(phase) |
| 131 |
fun doPhases [] (CLUSTER c) = c |
| 132 |
| doPhases [] _ = error "cluster needed" |
| 133 |
| doPhases (phase::phases) ir = |
| 134 |
(if !verbose then print("["^phase^"]\n") else (); |
| 135 |
doPhases phases (doPhase phase ir)) |
| 136 |
in doPhases (!phases) (CLUSTER cluster) |
| 137 |
end |
| 138 |
|
| 139 |
fun codegen cluster = if !mlrisc then optimize cluster else cluster |
| 140 |
|
| 141 |
end |