| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import { |
| 9 | BlockId, |
| 10 | GeneratedSource, |
| 11 | HIRFunction, |
| 12 | computePostDominatorTree, |
| 13 | } from '.'; |
| 14 | import {CompilerError} from '..'; |
| 15 | |
| 16 | export function computeUnconditionalBlocks(fn: HIRFunction): Set<BlockId> { |
| 17 | // Construct the set of blocks that is always reachable from the entry block. |
| 18 | const unconditionalBlocks = new Set<BlockId>(); |
| 19 | const dominators = computePostDominatorTree(fn, { |
| 20 | /* |
| 21 | * Hooks must only be in a consistent order for executions that return normally, |
| 22 | * so we opt-in to viewing throw as a non-exit node. |
| 23 | */ |
| 24 | includeThrowsAsExitNode: false, |
| 25 | }); |
| 26 | const exit = dominators.exit; |
| 27 | let current: BlockId | null = fn.body.entry; |
| 28 | while (current !== null && current !== exit) { |
| 29 | CompilerError.invariant(!unconditionalBlocks.has(current), { |
| 30 | reason: |
| 31 | 'Internal error: non-terminating loop in ComputeUnconditionalBlocks', |
| 32 | loc: GeneratedSource, |
| 33 | }); |
| 34 | unconditionalBlocks.add(current); |
| 35 | current = dominators.get(current); |
| 36 | } |
| 37 | return unconditionalBlocks; |
| 38 | } |