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
-
Replace (where m points to the top of temp stack)
with
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
and
pusht;
push tmp[m].ctr[k];
ret;
with
Similarly with tcall or tccall instead of ret.
Replace
with
-
Remove
-
Replace
with
-
Replace
with
if <instr> doesn't reference tmp[m]
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
with
Replace (where
mpoints to the top of temp stack)with
Similarly with
tcallortccallinstead ofret.Remove
Possibly add a
loadinstruction which writes to the top of the stack without pushing it. Replacewith
and
with
Similarly with
tcallortccallinstead ofret.Replace
with
Remove
Replace
with
Replace
with
if
<instr>doesn't referencetmp[m]