Home / Function/ applyConstantPropagation() — react Function Reference

applyConstantPropagation() — react Function Reference

Architecture documentation for the applyConstantPropagation() function in ConstantPropagation.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  4419ef20_4b5f_b18c_6237_57b76d298666["applyConstantPropagation()"]
  ee83ff7b_e532_0fd6_0eae_7fe885ffe129["ConstantPropagation.ts"]
  4419ef20_4b5f_b18c_6237_57b76d298666 -->|defined in| ee83ff7b_e532_0fd6_0eae_7fe885ffe129
  fbdc3953_3621_331c_a5b1_d3199bea5254["constantPropagationImpl()"]
  fbdc3953_3621_331c_a5b1_d3199bea5254 -->|calls| 4419ef20_4b5f_b18c_6237_57b76d298666
  9356843d_f70e_f922_5710_d8bd36a38797["evaluatePhi()"]
  4419ef20_4b5f_b18c_6237_57b76d298666 -->|calls| 9356843d_f70e_f922_5710_d8bd36a38797
  7da3db63_9dcc_8c1f_be1f_66f4e99b59ea["evaluateInstruction()"]
  4419ef20_4b5f_b18c_6237_57b76d298666 -->|calls| 7da3db63_9dcc_8c1f_be1f_66f4e99b59ea
  3a755420_8f69_0883_ccd1_2cfb88e3627e["read()"]
  4419ef20_4b5f_b18c_6237_57b76d298666 -->|calls| 3a755420_8f69_0883_ccd1_2cfb88e3627e
  style 4419ef20_4b5f_b18c_6237_57b76d298666 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts lines 107–166

function applyConstantPropagation(
  fn: HIRFunction,
  constants: Constants,
): boolean {
  let hasChanges = false;
  for (const [, block] of fn.body.blocks) {
    /*
     * Initialize phi values if all operands have the same known constant value.
     * Note that this analysis uses a single-pass only, so it will never fill in
     * phi values for blocks that have a back-edge.
     */
    for (const phi of block.phis) {
      let value = evaluatePhi(phi, constants);
      if (value !== null) {
        constants.set(phi.place.identifier.id, value);
      }
    }

    for (let i = 0; i < block.instructions.length; i++) {
      if (block.kind === 'sequence' && i === block.instructions.length - 1) {
        /*
         * evaluating the last value of a value block can break order of evaluation,
         * skip these instructions
         */
        continue;
      }
      const instr = block.instructions[i]!;
      const value = evaluateInstruction(constants, instr);
      if (value !== null) {
        constants.set(instr.lvalue.identifier.id, value);
      }
    }

    const terminal = block.terminal;
    switch (terminal.kind) {
      case 'if': {
        const testValue = read(constants, terminal.test);
        if (testValue !== null && testValue.kind === 'Primitive') {
          hasChanges = true;
          const targetBlockId = testValue.value
            ? terminal.consequent
            : terminal.alternate;
          block.terminal = {
            kind: 'goto',
            variant: GotoVariant.Break,
            block: targetBlockId,
            id: terminal.id,
            loc: terminal.loc,
          };
        }
        break;
      }
      default: {
        // no-op
      }
    }
  }

  return hasChanges;
}

Domain

Subdomains

Frequently Asked Questions

What does applyConstantPropagation() do?
applyConstantPropagation() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts.
Where is applyConstantPropagation() defined?
applyConstantPropagation() is defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts at line 107.
What does applyConstantPropagation() call?
applyConstantPropagation() calls 3 function(s): evaluateInstruction, evaluatePhi, read.
What calls applyConstantPropagation()?
applyConstantPropagation() is called by 1 function(s): constantPropagationImpl.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free