[be] Update docblock comment for ConstantPropagation
Joe Savona committed
Oct 10, 2023 at 13:15 UTC
495fec19e76fafcb7b57e0591ef1fa43da6cd7af
1 file changed
+19
-13
compiler/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts
+19
-13
@@ -34,21 +34,27 @@ import {
34
import { eliminateRedundantPhi } from "../SSA";
35
36
/**
37
- * Applies constant propagation and constant folding to the given function.
38
- * Note that because HIR operands are always a Place, constants cannot be directly
39
- * propagated into the HIR itself (the closest option would be to copy constants to
40
- * new temporaries just before each use, and update usage sites to reference those
41
- * new temporaries).
37
+ * Applies constant propagation/folding to the given function. The approach is
38
+ * [Sparse Conditional Constant Propagation](https://en.wikipedia.org/wiki/Sparse_conditional_constant_propagation):
39
+ * we use abstract interpretation to record known constant values for identifiers,
40
+ * with lack of a value indicating that the identifier does not have a
41
+ * known constant value.
42
*
43
- * Instead this pass implements constant folding, in which constant values are
44
- * propagated internally to the pass and subsequent operations are removed/folded where
45
- * possible.
43
+ * Instructions which can be compile-time evaluated *and* whose operands are known constants
44
+ * are replaced with the resulting constant value. For example a BinaryExpression
45
+ * where the left value is known to be `1` and the right value is known to be `2`
46
+ * can be replaced with a `Constant 3` instruction.
47
*
47
- * Note that this pass may prune control flow blocks that are unreachable, for example
48
- * a consequent or alternate branch if an `if` test is provably truthy or falsey.
49
- * If (and only if) terminals change, the pass re-runs various stages to ensure the
50
- * CFG is in minimal form. This means instruction ids *may* change as a result of this
51
- * pass.
48
+ * This pass also exploits the use of SSA form, tracking the constant values of
49
+ * local variables. For example, in `let x = 4; let y = x + 1` we know that
50
+ * `x = 4` in the binary expression and can replace the binary expression with
51
+ * `Constant 5`.
52
+ *
53
+ * This pass also visits conditionals (currently only IfTerminal) and can prune
54
+ * unreachable branches when the condition is a known truthy/falsey constant. The
55
+ * pass uses fixpoint iteration, looping until no additional updates can be
56
+ * performed. This allows the compiler to find cases where once one conditional is pruned,
57
+ * other values become constant, allowing subsequent conditionals to be pruned and so on.
58
*/
59
export function constantPropagation(fn: HIRFunction): void {
60
const constants: Constants = new Map();