Expand description

A pass that duplicates switch-terminated blocks into a new copy for each predecessor, provided the predecessor sets the value being switched over to a constant.

The purpose of this pass is to help constant propagation passes to simplify the switch terminator of the copied blocks into gotos when some predecessors statically determine the output of switches.

    x = 12 ---              ---> something
              \            / 12
               --> switch x
              /            \ otherwise
    x = y  ---              ---> something else

becomes

    x = 12 ---> switch x ------> something
                         \ / 12
                          X
                         / \ otherwise
    x = y  ---> switch x ------> something else

so it can hopefully later be turned by another pass into

    x = 12 --------------------> something
                           / 12
                          /
                         /   otherwise
    x = y  ---- switch x ------> something else

This optimization is meant to cover simple cases like ? desugaring. For now, it thus focuses on simplicity rather than completeness (it notably sometimes duplicates abusively).

Structs

Functions

  • Finds a unique place that entirely determines the value of switch_place, if it exists. This is only a heuristic. Ideally we would like to track multiple determining places for some edge cases, but one is enough for a lot of situations.
  • This function describes a rough heuristic guessing whether a place is last set with a const within the block. Notably, it will be overly pessimistic in cases that are already not handled by separate_const_switch.
  • Returns the amount of blocks that were duplicated