Cleanup the control flow graph. This eliminates obviously dead basic blocks and eliminates blocks that contain nothing but a single jump. There is a lot more that could be done.
(blocks: list[BasicBlock])
| 134 | |
| 135 | |
| 136 | def cleanup_cfg(blocks: list[BasicBlock]) -> None: |
| 137 | """Cleanup the control flow graph. |
| 138 | |
| 139 | This eliminates obviously dead basic blocks and eliminates blocks that contain |
| 140 | nothing but a single jump. |
| 141 | |
| 142 | There is a lot more that could be done. |
| 143 | """ |
| 144 | changed = True |
| 145 | while changed: |
| 146 | # First collapse any jumps to basic block that only contain a goto |
| 147 | for block in blocks: |
| 148 | for i, tgt in enumerate(block.terminator.targets()): |
| 149 | block.terminator.set_target(i, get_real_target(tgt)) |
| 150 | |
| 151 | # Then delete any blocks that have no predecessors |
| 152 | changed = False |
| 153 | cfg = get_cfg(blocks) |
| 154 | orig_blocks = blocks.copy() |
| 155 | blocks.clear() |
| 156 | for i, block in enumerate(orig_blocks): |
| 157 | if i == 0 or cfg.pred[block]: |
| 158 | blocks.append(block) |
| 159 | else: |
| 160 | changed = True |
| 161 | |
| 162 | |
| 163 | T = TypeVar("T") |
no test coverage detected
searching dependent graphs…