5 _log
= logging
.getLogger(__file__
)
8 def make_dead(insts
, idx
):
10 if org_inst
.op
== "DEAD":
12 if org_inst
.side_effect():
15 dead
= Inst(None, "DEAD", [])
16 dead
.addr
= org_inst
.addr
18 insts
[idx
].comments
["org_inst"] = org_inst
21 def dead_code_elimination_forward(bblock
):
22 """Try to perform eliminations using forward flow. This is reverse
23 to the natural direction, and requires multiple passing over
24 bblock to stabilize. Don't use it, here only for comparison."""
29 for i
, inst
in enumerate(bblock
.items
):
34 make_dead(bblock
.items
, last
)
36 node
= bblock
.cfg
[bblock
.addr
]
37 live_out
= node
.get("live_out")
38 if last
is not None and live_out
is not None:
40 make_dead(bblock
.items
, last
)
43 def dead_code_elimination_backward(bblock
):
44 node
= bblock
.cfg
[bblock
.addr
]
45 live
= node
.get("live_out")
47 _log
.warn("BBlock %s: No live_out set, conservatively assuming all defined vars are live", bblock
.addr
)
52 for i
in range(len(bblock
.items
) - 1, -1, -1):
53 inst
= bblock
.items
[i
]
54 if isinstance(inst
.dest
, REG
):
56 live
.remove(inst
.dest
)
58 make_dead(bblock
.items
, i
)
60 inst
= bblock
.items
[i
]
66 dead_code_elimination
= dead_code_elimination_backward