Skip to content

Peephole optimisation of JuvixAsm #1561

@lukaszcz

Description

@lukaszcz

Simple peephole optimisation on the JuvixAsm level.

The C compiler might not catch some of these as it has no access to JuvixAsm semantics or the type information. This is particularly important for optimising away the single-branch case instructions. C compiler doesn't know that the constructor tag can have only one value, and C semantics mandates that if no branch matches then no branch code is executed (see the C99 standard). So a check is still needed for a single-branch switch and the C compiler cannot optimise it away.

Removing single-branch Case Nodes on the Core level is not good enough. In the Core, a single-branch Case Node cannot be removed if the branch references the constructor arguments.

Examples

  • Replace

    case ind {
      ctr: { <code> }
    };
    

    with

    <code>
    
  • Replace (where m points to the top of temp stack)

    pusht;
    push tmp[m];
    ret;
    

    with

    ret;
    

    Similarly with tcall or tccall instead of ret.

  • Remove

    pusht;
    push tmp[m];
    popt;
    
  • Possibly add a load instruction which writes to the top of the stack without pushing it. Replace

    pusht;
    push tmp[m].ctr[k];
    popt;
    

    with

    load $.ctr[k];
    

    and

    pusht;
    push tmp[m].ctr[k];
    ret;
    

    with

    load $.ctr[k];
    ret;
    

    Similarly with tcall or tccall instead of ret.

    Replace

    push V;
    load $.ctr[k];
    

    with

    push V.ctr[k];
    
  • Remove

    push V;
    pop;
    
  • Replace

    pusht;
    popt;
    

    with

    pop;
    
  • Replace

    <instr>;
    popt;
    

    with

    popt;
    <instr>;
    

    if <instr> doesn't reference tmp[m]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions