Skip to content

Boolean peephole: short-circuit fusion and push not through all ordering comparisons #238

Description

@Unisay

Problem

Two small conditional simplifications pslua does not do:

  • Short-circuit fusion: if p then B else false → p && B, if p then true else q → p || q for a cheap q, fusing nested one-armed conditionals into &&/||.
  • not through ordering: not (a < b) → a >= b for every ordering comparison. pslua pushes not through ==/~= (foldNotEqual) and cancels double negation (foldPrimNot), but leaves not (a < b) and the other three orderings wrapped.

Approach

Two peephole-local rewrites in IR/Optimizer.hs:

  • extend the not-pushing fold to the four ordering primops (<, <=, >, >=), each flipping to its complement;
  • add the short-circuit fusions for if p then _ else false and if p then true else _, gated so the fused branch is cheap by the isInlinableExpr test and evaluation order is preserved in the strict setting.

Prerequisites / Relations

Independent. Adjacent to the residual-conditional folds from #180: #220 (fold Eq through a literal conditional), #222 (fold ordering on ASCII Char literals), #223 (collapse Boolean matches). This adds the operator-level simplifications those folds do not reach. None block the others.

Verification / Measurement

not (a < b) reduces to a >= b, and if p then q else false to p && q, in focused optimizer tests; a fusion whose folded branch is non-trivial is left alone (the evaluation-order guard). Eval goldens unchanged; structural goldens shrink where these shapes occur.

Metadata

Metadata

Assignees

No one assigned

    Labels

    OptimisationA Compiler Optimisationarea: irIR / optimizer / DCE / inlinerenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions