-
-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into venom/pick
- Loading branch information
Showing
2 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,47 @@ | ||
from vyper.venom.analysis.dfg import DFGAnalysis | ||
from vyper.venom.analysis.liveness import LivenessAnalysis | ||
from vyper.venom.basicblock import IRBasicBlock | ||
from vyper.venom.passes.base_pass import IRPass | ||
|
||
|
||
class RemoveUnusedVariablesPass(IRPass): | ||
def run_pass(self): | ||
removeList = set() | ||
|
||
self.analyses_cache.request_analysis(LivenessAnalysis) | ||
|
||
for bb in self.function.get_basic_blocks(): | ||
for i, inst in enumerate(bb.instructions[:-1]): | ||
if inst.volatile: | ||
continue | ||
next_liveness = bb.instructions[i + 1].liveness | ||
if (inst.output and inst.output not in next_liveness) or inst.opcode == "nop": | ||
removeList.add(inst) | ||
|
||
bb.instructions = [inst for inst in bb.instructions if inst not in removeList] | ||
self._remove_unused_variables(bb) | ||
|
||
self.analyses_cache.invalidate_analysis(DFGAnalysis) | ||
|
||
def _remove_unused_variables(self, bb: IRBasicBlock): | ||
""" | ||
Remove the instructions of a basicblock that produce output that is never used. | ||
""" | ||
i = 0 | ||
while i < len(bb.instructions) - 1: | ||
inst = bb.instructions[i] | ||
i += 1 | ||
|
||
# Skip volatile instructions | ||
if inst.volatile: | ||
continue | ||
|
||
# Skip instructions without output | ||
if inst.output is None: | ||
continue | ||
|
||
# Skip instructions that produce output that is used | ||
next_liveness = bb.instructions[i].liveness | ||
if inst.output in next_liveness: | ||
continue | ||
|
||
# Remove the rest | ||
del bb.instructions[i - 1] | ||
|
||
# backtrack to the *previous* instruction, in case we removed | ||
# an instruction which had prevented the previous instruction | ||
# from being removed | ||
i -= 2 | ||
|
||
# don't go beyond 0 though | ||
i = max(0, i) |