Debian Codegraph @

mypyc/transform/flag_elimination.py

raw
1 """Bool register elimination optimization.
2
3 Example input:
4
5 L1:
6 r0 = f()
7 b = r0
8 goto L3
9 L2:
10 r1 = g()
11 b = r1
12 goto L3
13 L3:
14 if b goto L4 else goto L5
15
16 The register b is redundant and we replace the assignments with two copies of
17 the branch in L3:
18
19 L1:
20 r0 = f()
21 if r0 goto L4 else goto L5
22 L2:
23 r1 = g()
24 if r1 goto L4 else goto L5
25
26 This helps generate simpler IR for tagged integers comparisons, for example.
27 """
28
29 from __future__ import annotations
30
31 from mypyc.ir.func_ir import FuncIR
32 from mypyc.ir.ops import Assign, BasicBlock, Branch, Goto, Register, Unreachable
33 from mypyc.irbuild.ll_builder import LowLevelIRBuilder
34 from mypyc.options import CompilerOptions
35 from mypyc.transform.ir_transform import IRTransform
36
37
38 def do_flag_elimination(fn: FuncIR, options: CompilerOptions) -> None:
39 # Find registers that are used exactly once as source, and in a branch.
40 counts: dict[Register, int] = {}
41 branches: dict[Register, Branch] = {}
42 labels: dict[Register, BasicBlock] = {}
43 for block in fn.blocks:
44 for i, op in enumerate(block.ops):
45 for src in op.sources():
46 if isinstance(src, Register):
47 counts[src] = counts.get(src, 0) + 1
48 if i == 0 and isinstance(op, Branch) and isinstance(op.value, Register):
49 branches[op.value] = op
50 labels[op.value] = block
51
52 # Based on these we can find the candidate registers.
53 candidates: set[Register] = {
54 r for r in branches if counts.get(r, 0) == 1 and r not in fn.arg_regs
55 }
56
57 # Remove candidates with invalid assignments.
58 for block in fn.blocks:
59 for i, op in enumerate(block.ops):
60 if isinstance(op, Assign) and op.dest in candidates:
61 next_op = block.ops[i + 1]
62 if not (isinstance(next_op, Goto) and next_op.label is labels[op.dest]):
63 # Not right
64 candidates.remove(op.dest)
65
66 builder = LowLevelIRBuilder(None, options)
67 transform = FlagEliminationTransform(
68 builder, {x: y for x, y in branches.items() if x in candidates}
69 )
70 transform.transform_blocks(fn.blocks)
71 fn.blocks = builder.blocks
72
73
74 class FlagEliminationTransform(IRTransform):
75 def __init__(self, builder: LowLevelIRBuilder, branch_map: dict[Register, Branch]) -> None:
76 super().__init__(builder)
77 self.branch_map = branch_map
78 self.branches = set(branch_map.values())
79
80 def visit_assign(self, op: Assign) -> None:
81 if old_branch := self.branch_map.get(op.dest):
82 # Replace assignment with a copy of the old branch, which is in a
83 # separate basic block. The old branch will be deleted in visit_branch.
84 new_branch = Branch(
85 op.src,
86 old_branch.true,
87 old_branch.false,
88 old_branch.op,
89 old_branch.line,
90 rare=old_branch.rare,
91 )
92 new_branch.negated = old_branch.negated
93 new_branch.traceback_entry = old_branch.traceback_entry
94 self.add(new_branch)
95 else:
96 self.add(op)
97
98 def visit_goto(self, op: Goto) -> None:
99 # This is a no-op if basic block already terminated
100 self.builder.goto(op.label)
101
102 def visit_branch(self, op: Branch) -> None:
103 if op in self.branches:
104 # This branch is optimized away
105 self.add(Unreachable())
106 else:
107 self.add(op)