-
Notifications
You must be signed in to change notification settings - Fork 64
Description
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
mpoints to the top of temp stack)pusht; push tmp[m]; ret;with
ret;Similarly with
tcallortccallinstead ofret. -
Remove
pusht; push tmp[m]; popt; -
Possibly add a
loadinstruction which writes to the top of the stack without pushing it. Replacepusht; 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
tcallortccallinstead ofret.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 referencetmp[m]