| 1 |
"""Find basic blocks that are likely to be executed frequently. |
| 2 |
|
| 3 |
For example, this would not include blocks that have exception handlers. |
| 4 |
|
| 5 |
We can use different optimization heuristics for common and rare code. For |
| 6 |
example, we can make IR fast to compile instead of fast to execute for rare |
| 7 |
code. |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
from mypyc.ir.ops import BasicBlock, Branch, Goto |
| 13 |
|
| 14 |
|
| 15 |
def frequently_executed_blocks(entry_point: BasicBlock) -> set[BasicBlock]: |
| 16 |
result: set[BasicBlock] = set() |
| 17 |
worklist = [entry_point] |
| 18 |
while worklist: |
| 19 |
block = worklist.pop() |
| 20 |
if block in result: |
| 21 |
continue |
| 22 |
result.add(block) |
| 23 |
t = block.terminator |
| 24 |
if isinstance(t, Goto): |
| 25 |
worklist.append(t.label) |
| 26 |
elif isinstance(t, Branch): |
| 27 |
if t.rare or t.traceback_entry is not None: |
| 28 |
worklist.append(t.false) |
| 29 |
else: |
| 30 |
worklist.append(t.true) |
| 31 |
worklist.append(t.false) |
| 32 |
return result |