| 1 |
"""Simple copy propagation optimization. |
| 2 |
|
| 3 |
Example input: |
| 4 |
|
| 5 |
x = f() |
| 6 |
y = x |
| 7 |
|
| 8 |
The register x is redundant and we can directly assign its value to y: |
| 9 |
|
| 10 |
y = f() |
| 11 |
|
| 12 |
This can optimize away registers that are assigned to once. |
| 13 |
""" |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
from mypyc.ir.func_ir import FuncIR |
| 18 |
from mypyc.ir.ops import Assign, AssignMulti, LoadAddress, LoadErrorValue, Register, Value |
| 19 |
from mypyc.irbuild.ll_builder import LowLevelIRBuilder |
| 20 |
from mypyc.options import CompilerOptions |
| 21 |
from mypyc.sametype import is_same_type |
| 22 |
from mypyc.transform.ir_transform import IRTransform |
| 23 |
|
| 24 |
|
| 25 |
def do_copy_propagation(fn: FuncIR, options: CompilerOptions) -> None: |
| 26 |
"""Perform copy propagation optimization for fn.""" |
| 27 |
|
| 28 |
# Anything with an assignment count >1 will not be optimized |
| 29 |
# here, as it would be require data flow analysis and we want to |
| 30 |
# keep this simple and fast, at least until we've made data flow |
| 31 |
# analysis much faster. |
| 32 |
counts: dict[Value, int] = {} |
| 33 |
replacements: dict[Value, Value] = {} |
| 34 |
for arg in fn.arg_regs: |
| 35 |
# Arguments are always assigned to initially |
| 36 |
counts[arg] = 1 |
| 37 |
|
| 38 |
for block in fn.blocks: |
| 39 |
for op in block.ops: |
| 40 |
if isinstance(op, Assign): |
| 41 |
c = counts.get(op.dest, 0) |
| 42 |
counts[op.dest] = c + 1 |
| 43 |
# Does this look like a supported assignment? |
| 44 |
# TODO: Something needs LoadErrorValue assignments to be preserved? |
| 45 |
if ( |
| 46 |
c == 0 |
| 47 |
and is_same_type(op.dest.type, op.src.type) |
| 48 |
and not isinstance(op.src, LoadErrorValue) |
| 49 |
): |
| 50 |
replacements[op.dest] = op.src |
| 51 |
elif c == 1: |
| 52 |
# Too many assignments -- don't replace this one |
| 53 |
replacements.pop(op.dest, 0) |
| 54 |
elif isinstance(op, AssignMulti): |
| 55 |
# Copy propagation not supported for AssignMulti destinations |
| 56 |
counts[op.dest] = 2 |
| 57 |
replacements.pop(op.dest, 0) |
| 58 |
elif isinstance(op, LoadAddress): |
| 59 |
# We don't support taking the address of an arbitrary Value, |
| 60 |
# so we'll need to preserve the operands of LoadAddress. |
| 61 |
if isinstance(op.src, Register): |
| 62 |
counts[op.src] = 2 |
| 63 |
replacements.pop(op.src, 0) |
| 64 |
|
| 65 |
# Follow chains of propagation with more than one assignment. |
| 66 |
for src, dst in list(replacements.items()): |
| 67 |
if counts.get(dst, 0) > 1: |
| 68 |
# Not supported |
| 69 |
del replacements[src] |
| 70 |
else: |
| 71 |
while dst in replacements: |
| 72 |
dst = replacements[dst] |
| 73 |
if counts.get(dst, 0) > 1: |
| 74 |
# Not supported |
| 75 |
del replacements[src] |
| 76 |
if src in replacements: |
| 77 |
replacements[src] = dst |
| 78 |
|
| 79 |
builder = LowLevelIRBuilder(None, options) |
| 80 |
transform = CopyPropagationTransform(builder, replacements) |
| 81 |
transform.transform_blocks(fn.blocks) |
| 82 |
fn.blocks = builder.blocks |
| 83 |
|
| 84 |
|
| 85 |
class CopyPropagationTransform(IRTransform): |
| 86 |
def __init__(self, builder: LowLevelIRBuilder, map: dict[Value, Value]) -> None: |
| 87 |
super().__init__(builder) |
| 88 |
self.op_map.update(map) |
| 89 |
self.removed = set(map) |
| 90 |
|
| 91 |
def visit_assign(self, op: Assign) -> Value | None: |
| 92 |
if op.dest in self.removed: |
| 93 |
return None |
| 94 |
return self.add(op) |