| 1 |
(* |
| 2 |
* This is a hack module for removing dead code that are discovered by |
| 3 |
* the register allocator. This module acts as a wrapper |
| 4 |
* for the generic RA flowgraph module. |
| 5 |
* |
| 6 |
* -- Allen |
| 7 |
*) |
| 8 |
|
| 9 |
functor RADeadCodeElim ( |
| 10 |
structure Flowgraph : RA_FLOWGRAPH |
| 11 |
(* check for dead code on these cellkinds only *) |
| 12 |
val cellkind : CellsBasis.cellkind -> bool |
| 13 |
(* Dead registers are stored here. *) |
| 14 |
val deadRegs : bool IntHashTable.hash_table |
| 15 |
(* Affected blocks *) |
| 16 |
val affectedBlocks : bool IntHashTable.hash_table |
| 17 |
val spillInit : Flowgraph.G.interferenceGraph * CellsBasis.cellkind |
| 18 |
-> unit |
| 19 |
) : RA_FLOWGRAPH = |
| 20 |
struct |
| 21 |
structure F = Flowgraph |
| 22 |
|
| 23 |
open F |
| 24 |
|
| 25 |
(* We must save all the copy temporaries for this to work *) |
| 26 |
val mode = RACore.SAVE_COPY_TEMPS |
| 27 |
|
| 28 |
fun isOn(flag,mask) = Word.andb(flag,mask) <> 0w0 |
| 29 |
|
| 30 |
(* |
| 31 |
* New services that also removes dead code |
| 32 |
*) |
| 33 |
fun services f = |
| 34 |
let val {build, spill, blockNum, instrNum, programPoint} = F.services f |
| 35 |
(* |
| 36 |
* The following build method marks all pseudo registers |
| 37 |
* that are dead, and record their definition points. |
| 38 |
*) |
| 39 |
fun findDeadCode(G.GRAPH{nodes, copyTmps, mode, ...}) = |
| 40 |
let val dead = IntHashTable.insert deadRegs |
| 41 |
val affected = IntHashTable.insert affectedBlocks |
| 42 |
val affectedList = app (fn d => affected(blockNum d, true)) |
| 43 |
|
| 44 |
(* Mark all copy temporaries *) |
| 45 |
val marker = [{block=0,insn=0}] |
| 46 |
fun markCopyTmps [] = () |
| 47 |
| markCopyTmps(G.NODE{uses, ...}::tmps) = |
| 48 |
(uses := marker; markCopyTmps tmps) |
| 49 |
fun unmarkCopyTmps [] = () |
| 50 |
| unmarkCopyTmps(G.NODE{uses, ...}::tmps) = |
| 51 |
(uses := []; unmarkCopyTmps tmps) |
| 52 |
|
| 53 |
fun enter(_, G.NODE{uses=ref [], defs, number=reg, ...}) = |
| 54 |
(* This is dead, but make sure it is not a copy temporary. |
| 55 |
* Those cannot be eliminated. |
| 56 |
*) |
| 57 |
(affectedList (!defs); dead(reg, true)) |
| 58 |
| enter _ = () |
| 59 |
|
| 60 |
in markCopyTmps(!copyTmps); |
| 61 |
IntHashTable.appi enter nodes; |
| 62 |
unmarkCopyTmps(!copyTmps); |
| 63 |
if isOn(mode, RACore.HAS_PARALLEL_COPIES) then () |
| 64 |
else copyTmps := [] (* clean up now *) |
| 65 |
end |
| 66 |
|
| 67 |
(* |
| 68 |
* Build the graph, then remove dead code. |
| 69 |
*) |
| 70 |
fun buildIt(graph, kind) = |
| 71 |
let val moves = build(graph, kind) |
| 72 |
in if cellkind kind then findDeadCode(graph) else (); |
| 73 |
moves |
| 74 |
end |
| 75 |
|
| 76 |
fun spillIt(arg as {graph, cellkind, ...}) = |
| 77 |
(spillInit(graph, cellkind); spill arg) |
| 78 |
|
| 79 |
in {build=buildIt, spill=spillIt, programPoint=programPoint, |
| 80 |
blockNum=blockNum, instrNum=instrNum} |
| 81 |
end |
| 82 |
|
| 83 |
end |