cairo_lang_lowering::optimizations::branch_inversion

Function branch_inversion

Source
pub fn branch_inversion(db: &dyn LoweringGroup, lowered: &mut FlatLowered)
Expand description

Performs branch inversion optimization on a lowered function.

The branch inversion optimization finds a match enum whose input is the output of a call to bool_not_impl. It swaps the arms of the match enum and changes its input to be the input before the negation.

This optimization is valid only if all paths leading to the match enum pass through the call to bool_not_impl. Therefore, the call to bool_not_impl should be in the same block as the match enum.

The call to bool_not_impl is not deleted as we don’t know if its output is used by other statements (or block ending).

Due to the limitations above, the reorder_statements function should be called before this optimization and between this optimization and the match optimization.

The first call to reorder_statements moves the call to bool_not_impl into the block whose match enum we want to optimize. The second call to reorder_statements removes the call to bool_not_impl if it is unused, allowing the match optimization to be applied to enum_init statements that appeared before the bool_not_impl.