| 1 |
(* |
| 2 |
* This is a generic module for performing (forward) dataflow |
| 3 |
* analysis on the SSA graph. It behavior is somewhat parameterizable. |
| 4 |
* |
| 5 |
* -- Allen ([email protected]) |
| 6 |
*) |
| 7 |
|
| 8 |
functor SSADataflow(SSA : SSA) : SSA_DATAFLOW_ANALYSIS = |
| 9 |
struct |
| 10 |
|
| 11 |
structure SSA = SSA |
| 12 |
structure RTL = SSA.RTL |
| 13 |
structure C = SSA.C |
| 14 |
structure T = RTL.T |
| 15 |
structure G = Graph |
| 16 |
structure A = Array |
| 17 |
structure W8A = Word8Array |
| 18 |
|
| 19 |
fun error msg = MLRiscErrorMsg.error("SSADataflowAnalysis",msg) |
| 20 |
|
| 21 |
fun dataflowAnalysis |
| 22 |
{ssa=SSA as G.GRAPH ssa, meet, bot, top, ==, transfer, const } = |
| 23 |
let val M = #capacity ssa () |
| 24 |
val values = A.array(M, bot) (* flow values *) |
| 25 |
val onWorkList = W8A.array(M, 0w1) (* initially everything in on WL *) |
| 26 |
val defsTbl = SSA.defsTbl SSA |
| 27 |
val usesTbl = SSA.usesTbl SSA |
| 28 |
val rtlTbl = SSA.rtlTbl SSA |
| 29 |
val succTbl = SSA.succTbl SSA |
| 30 |
val defSiteTbl = SSA.defSiteTbl SSA |
| 31 |
|
| 32 |
fun valueOf v = if v < 0 then const v else A.sub(values, v) |
| 33 |
|
| 34 |
val zeroR = case C.zeroReg C.GP of SOME r => r | NONE => ~1 |
| 35 |
|
| 36 |
fun iterate [] = () (* done *) |
| 37 |
| iterate(i::WL) = |
| 38 |
let val _ = W8A.update(onWorkList, i, 0w0) |
| 39 |
val uses = A.sub(usesTbl, i) |
| 40 |
val defs = A.sub(defsTbl, i) |
| 41 |
val rtl = A.sub(rtlTbl, i) |
| 42 |
val inputs = map valueOf uses |
| 43 |
in case rtl of |
| 44 |
T.PHI _ => phi(i, inputs, defs, WL) |
| 45 |
| T.SOURCE{liveIn, ...} => source(i, defs, liveIn, WL) |
| 46 |
| T.SINK _ => iterate WL |
| 47 |
| _ => |
| 48 |
let val outputs = transfer{rtl=rtl, inputs=inputs, defs=defs} |
| 49 |
in process(i, defs, outputs, WL, false) |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
and phi(i, inputs, [t], WL) = |
| 54 |
let val old = A.sub(values, t) |
| 55 |
val new = meet inputs |
| 56 |
in if ==(old,new) then iterate WL |
| 57 |
else (A.update(values, t, new); propagate(i, WL)) |
| 58 |
end |
| 59 |
| phi _ = error "phi" |
| 60 |
|
| 61 |
and source(i, [], [], WL) = propagate(i, WL) |
| 62 |
| source(i, v::vs, r::rs, WL) = |
| 63 |
(A.update(values, v, if r = zeroR then const ~1 else top); |
| 64 |
(* XXX *) |
| 65 |
source(i, vs, rs, WL) |
| 66 |
) |
| 67 |
| source _ = error "source" |
| 68 |
|
| 69 |
and process(i, [], [], WL, affected) = |
| 70 |
if affected then propagate(i, WL) else iterate WL |
| 71 |
| process(i, v::vs, d::ds, WL, affected) = |
| 72 |
let val old = A.sub(values, v) |
| 73 |
in if ==(old,d) then process(i, vs, ds, WL, affected) |
| 74 |
else (A.update(values, v, d); |
| 75 |
process(i, vs, ds, WL, true) |
| 76 |
) |
| 77 |
end |
| 78 |
| process _ = error "process" |
| 79 |
|
| 80 |
and propagate(i, WL) = insert(A.sub(succTbl, i), WL) |
| 81 |
|
| 82 |
and insert([], WL) = iterate WL |
| 83 |
| insert((_,j,_)::es, WL) = |
| 84 |
if W8A.sub(onWorkList, j) <> 0w0 then insert(es, WL) |
| 85 |
else (W8A.update(onWorkList, j, 0w1); insert(es, j::WL)) |
| 86 |
|
| 87 |
val ops = map #1 (#nodes ssa ()) |
| 88 |
|
| 89 |
in iterate ops; |
| 90 |
values |
| 91 |
end |
| 92 |
|
| 93 |
end |