| 1 |
(* |
| 2 |
* Machine SSA representation. |
| 3 |
* |
| 4 |
* Some conventions |
| 5 |
* ---------------- |
| 6 |
* 1. Each machine instruction is mapped into an ssa_op. Some exceptions: |
| 7 |
* a. live-out and live-in for each entry and exit are represented as |
| 8 |
* SINK and SOURCE nodes. |
| 9 |
* b. PHI functions may be inserted |
| 10 |
* c. COPYs may be propagated during construction of the ssa graph. |
| 11 |
* 2. Each instruction set must provide the pseudo instructions SINK, SOURCE |
| 12 |
* and PHI. |
| 13 |
* 3. Each ssa_op is numbered with its own ssa_id, starting from 0. |
| 14 |
* 4. Each definition is given a unique (non-negative) value id |
| 15 |
* IMPORTANT: ssa_id <> value. Each instructions may define multiple |
| 16 |
* values, even zero. But each instruction is given its own unique id. |
| 17 |
* 5. Negative value ids are immediate constants. These are value numbered |
| 18 |
* so that the each distinct constant is given unique (negative) id. |
| 19 |
* These constants are entered into the operandTbl. |
| 20 |
* 6. In-edges of the graph are use-def chains. These are computed on the |
| 21 |
* fly. |
| 22 |
* Out-edges are def-use chains. Both type of edges have the following |
| 23 |
* form: |
| 24 |
* (source id, dst id, register) |
| 25 |
* Of course, immedidate operands have no edges associated with them. |
| 26 |
* 7. The graph interface has high overhead. So in order to allow faster |
| 27 |
* implementation, we allow direct access to the internal data structures. |
| 28 |
* These are: |
| 29 |
* Name Mapping Description |
| 30 |
* --------------------------------------------------------------- |
| 31 |
* defSiteTbl value -> ssa_id Definition site of a value |
| 32 |
* (i.e. use/def chains) |
| 33 |
* blockTbl ssa_id -> block Block id of an instruction |
| 34 |
* defsTbl ssa_id -> value list Definitions of an ssa_op |
| 35 |
* usesTbl ssa_id -> value list Uses of an ssa_op |
| 36 |
* rtlTbl ssa_id -> rtl RTL of an ssa_op |
| 37 |
* succTbl ssa_id -> outedges Out edges of an ssa_op |
| 38 |
* (i.e. def/use chains) |
| 39 |
* ssaOpTbl ssa_id -> ssa_op ssa_op table |
| 40 |
* cellKindTbl value -> ssa_id cellkind of a value |
| 41 |
* operandTbl value -> operand operand table |
| 42 |
* |
| 43 |
* But in general, you should use the graph interface for traversal |
| 44 |
* if are not sure how the internal tables work. |
| 45 |
* |
| 46 |
* -- Allen ([email protected]) |
| 47 |
* |
| 48 |
*) |
| 49 |
signature SSA = |
| 50 |
sig |
| 51 |
|
| 52 |
structure I : INSTRUCTIONS |
| 53 |
structure C : CELLS |
| 54 |
structure SP : SSA_PROPERTIES |
| 55 |
structure RTL : MLTREE_RTL |
| 56 |
structure CFG : SSA_FLOWGRAPH |
| 57 |
structure Dom : DOMINATOR_TREE |
| 58 |
structure DJ : DJ_GRAPH |
| 59 |
structure GCMap : GC_MAP |
| 60 |
structure MLTreeComp : MLTREECOMP |
| 61 |
structure W : FREQ |
| 62 |
sharing SP.I = CFG.I = MLTreeComp.I = I |
| 63 |
sharing SP.RTL = RTL |
| 64 |
sharing MLTreeComp.T = RTL.T |
| 65 |
sharing I.C = SP.C = C |
| 66 |
sharing Dom = DJ.Dom |
| 67 |
sharing CFG.W = W |
| 68 |
|
| 69 |
(*------------------------------------------------------------------------ |
| 70 |
* Basic type definitions used in the SSA form |
| 71 |
*------------------------------------------------------------------------*) |
| 72 |
type value = int (* value id *) |
| 73 |
type pos = int (* position within a block *) |
| 74 |
type block = Graph.node_id (* block id *) |
| 75 |
type ssa_id = Graph.node_id (* ssa id *) |
| 76 |
type rtl = RTL.rtl (* RTL *) |
| 77 |
type const = SP.OT.const (* constants *) |
| 78 |
type cfg = CFG.cfg (* control flow graph *) |
| 79 |
(* dominator tree *) |
| 80 |
type dom = (CFG.block,CFG.edge_info,CFG.info) Dom.dominator_tree |
| 81 |
type nameTbl = {oldName:C.cell, index:int} IntHashTable.hash_table |
| 82 |
|
| 83 |
(*------------------------------------------------------------------------ |
| 84 |
* An SSA op is an instruction |
| 85 |
*------------------------------------------------------------------------*) |
| 86 |
type ssa_op = I.instruction |
| 87 |
|
| 88 |
(*------------------------------------------------------------------------ |
| 89 |
* Information about the SSA graph |
| 90 |
*------------------------------------------------------------------------*) |
| 91 |
type ssa_info |
| 92 |
|
| 93 |
(*------------------------------------------------------------------------ |
| 94 |
* The graph structure |
| 95 |
*------------------------------------------------------------------------*) |
| 96 |
type ssa = (ssa_op,value,ssa_info) Graph.graph |
| 97 |
|
| 98 |
(*------------------------------------------------------------------------ |
| 99 |
* How to create a new SSA graph |
| 100 |
*------------------------------------------------------------------------*) |
| 101 |
(* create an empty SSA graph *) |
| 102 |
val newSSA : {cfg: cfg, |
| 103 |
dom: cfg -> dom, |
| 104 |
gcmap: GCMap.gcmap option, |
| 105 |
nameTbl: nameTbl option |
| 106 |
} -> ssa |
| 107 |
val newRenamedVar : ssa -> C.cell -> value (* generate renamed variable *) |
| 108 |
val newVar : ssa -> C.cell -> C.cell |
| 109 |
|
| 110 |
(* create a new op; but does not add edges *) |
| 111 |
val newOp : ssa -> {id : ssa_id, |
| 112 |
instr: I.instruction, |
| 113 |
rtl : rtl, |
| 114 |
defs : value list, |
| 115 |
uses : value list, |
| 116 |
block: block, |
| 117 |
pos : pos |
| 118 |
} -> unit |
| 119 |
val reserve : ssa -> int -> unit (* reserve n nodes *) |
| 120 |
val immed : ssa -> int -> value (* lookup immed value *) |
| 121 |
val operand : ssa -> I.operand -> value (* lookup perand *) |
| 122 |
val computeDefUseChains : ssa -> unit |
| 123 |
|
| 124 |
(*------------------------------------------------------------------------ |
| 125 |
* Extract info from the SSA graph |
| 126 |
*------------------------------------------------------------------------*) |
| 127 |
val dom : ssa -> dom (* extracts the dominator *) |
| 128 |
val cfg : ssa -> cfg (* extracts the CFG *) |
| 129 |
val maxVar : ssa -> int (* maximum number of ssa names *) |
| 130 |
val numberOfOperands : ssa -> int (* number of operands *) |
| 131 |
val const : ssa -> value -> const (* lookup const values *) |
| 132 |
|
| 133 |
(*------------------------------------------------------------------------ |
| 134 |
* Extract the raw tables. |
| 135 |
* These should only be used when the optimization guarantees that |
| 136 |
* no new ssa ops are added to the graph, since that may involve resizing |
| 137 |
* these tables, rendering them obsolete. |
| 138 |
*------------------------------------------------------------------------*) |
| 139 |
val defSiteTbl : ssa -> ssa_id Array.array |
| 140 |
val blockTbl : ssa -> block Array.array |
| 141 |
val posTbl : ssa -> pos Array.array |
| 142 |
val defsTbl : ssa -> value list Array.array |
| 143 |
val usesTbl : ssa -> value list Array.array |
| 144 |
val rtlTbl : ssa -> rtl Array.array |
| 145 |
val succTbl : ssa -> value Graph.edge list Array.array (* out edges *) |
| 146 |
val ssaOpTbl : ssa -> ssa_op Array.array (* node table *) |
| 147 |
val cellKindTbl: ssa -> C.cellkind IntHashTable.hash_table |
| 148 |
(* cellkind table *) |
| 149 |
val operandTbl : ssa -> SP.OT.operandTable |
| 150 |
val minPos : ssa -> int ref |
| 151 |
val maxPos : ssa -> int ref |
| 152 |
|
| 153 |
(*------------------------------------------------------------------------ |
| 154 |
* Zero registers, pinned registers |
| 155 |
*------------------------------------------------------------------------*) |
| 156 |
val zeroRegs : Word8Array.array |
| 157 |
val pinnedUseTbl : Word8Array.array |
| 158 |
val pinnedDefTbl : Word8Array.array |
| 159 |
|
| 160 |
(*------------------------------------------------------------------------ |
| 161 |
* Lookup information (the safe way) |
| 162 |
*------------------------------------------------------------------------*) |
| 163 |
val defSite : ssa -> value -> ssa_id (* lookup definition site *) |
| 164 |
val block : ssa -> ssa_id -> block (* lookup block id *) |
| 165 |
val rtl : ssa -> ssa_id -> rtl (* lookup rtl *) |
| 166 |
val uses : ssa -> ssa_id -> value list |
| 167 |
val defs : ssa -> ssa_id -> value list |
| 168 |
|
| 169 |
(* nodes linearized and indexed by block id *) |
| 170 |
val nodes : ssa -> {sources : ssa_id list Array.array, |
| 171 |
phis : ssa_id list Array.array, |
| 172 |
ops : ssa_id list Array.array, |
| 173 |
sinks : ssa_id list Array.array |
| 174 |
} |
| 175 |
val freqTbl : ssa -> W.freq Array.array (* frequency indexed by block *) |
| 176 |
val noResize : ssa -> ('a -> 'b) -> 'a -> 'b |
| 177 |
(*------------------------------------------------------------------------ |
| 178 |
* Iterators |
| 179 |
*------------------------------------------------------------------------*) |
| 180 |
val forallNodes : ssa -> (ssa_id -> unit) -> unit |
| 181 |
val foldNodes : ssa -> (ssa_id * 'a -> 'a) -> 'a -> 'a |
| 182 |
|
| 183 |
(*------------------------------------------------------------------------ |
| 184 |
* Remove all useless phi-functions from the graph. |
| 185 |
* Useless phi-functions are self-loops of the form |
| 186 |
* t <- phi(t, t, ..., t, s, t, ..., t) |
| 187 |
* This transformation removes this phi-function and replace all uses |
| 188 |
* of t by s. This process is worklist driven; removing a useless |
| 189 |
* phi-function can introduce other useless phi-functions. |
| 190 |
*------------------------------------------------------------------------*) |
| 191 |
val removeUselessPhiFunctions : ssa -> unit |
| 192 |
|
| 193 |
(*------------------------------------------------------------------------ |
| 194 |
* Remove all nodes from the graph. Note that no uses should be |
| 195 |
* present after this transformation. |
| 196 |
*------------------------------------------------------------------------*) |
| 197 |
val removeAllNodes : ssa -> ssa_id list -> unit |
| 198 |
|
| 199 |
(*------------------------------------------------------------------------ |
| 200 |
* Replace all use of one value with another. Return true iff |
| 201 |
* all uses of "from" has been replaced by "to". |
| 202 |
* Note: The definition of "from" must dominate all uses of "to", as |
| 203 |
* required by the SSA form. |
| 204 |
*------------------------------------------------------------------------*) |
| 205 |
val replaceAllUses : ssa -> {from:value, to:value, vn:value} -> bool |
| 206 |
|
| 207 |
(*------------------------------------------------------------------------ |
| 208 |
* Replace the definition of value by const. Return true iff |
| 209 |
* this operation is successful. |
| 210 |
*------------------------------------------------------------------------*) |
| 211 |
val foldConstant : ssa -> {value:value, const:value} -> bool |
| 212 |
|
| 213 |
(*------------------------------------------------------------------------ |
| 214 |
* Move an instruction from one block to another |
| 215 |
*------------------------------------------------------------------------*) |
| 216 |
val moveOp : ssa -> {id:ssa_id, block:block} -> unit |
| 217 |
|
| 218 |
(*------------------------------------------------------------------------ |
| 219 |
* Set the target of a conditional branch as true or false. |
| 220 |
* This removes the branch and eliminates all unreachable code. |
| 221 |
*------------------------------------------------------------------------*) |
| 222 |
val setBranch : ssa -> {id:ssa_id, cond:bool} -> unit |
| 223 |
|
| 224 |
(*------------------------------------------------------------------------ |
| 225 |
* Signal that an SSA has been changed. This uncaches all data structures. |
| 226 |
*------------------------------------------------------------------------*) |
| 227 |
val changed : ssa -> unit |
| 228 |
|
| 229 |
(*------------------------------------------------------------------------ |
| 230 |
* Pretty printing |
| 231 |
*------------------------------------------------------------------------*) |
| 232 |
val showOp : ssa -> ssa_id -> string |
| 233 |
val showVal : ssa -> value -> string |
| 234 |
val showRTL : ssa -> rtl -> string |
| 235 |
|
| 236 |
(*------------------------------------------------------------------------ |
| 237 |
* Graphical viewing |
| 238 |
*------------------------------------------------------------------------*) |
| 239 |
val viewAsCFG : ssa -> GraphLayout.layout |
| 240 |
val viewAsSSA : ssa -> GraphLayout.layout |
| 241 |
|
| 242 |
(*------------------------------------------------------------------------ |
| 243 |
* Check whether the ssa graph is consistent |
| 244 |
*------------------------------------------------------------------------*) |
| 245 |
val consistencyCheck : ssa -> unit |
| 246 |
end |
| 247 |
|