blockfreq.py 1004 B

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