Skip to content

Commit

Permalink
Optimize out push/pop pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
vtereshkov committed Nov 24, 2024
1 parent 72c08c0 commit 995799a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion playground/umka.js

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions src/umka_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ static bool optimizePop(CodeGen *gen)
}


static bool optimizePopReg(CodeGen *gen, RegisterIndex regIndex)
{
Instruction *prev = getPrevInstr(gen, 1);

// Optimization: PUSH_REG n + POP_REG n -> 0
if (prev && prev->opcode == OP_PUSH_REG && prev->operand.intVal == regIndex)
{
genRemoveInstr(gen);
return true;
}

return false;
}


static bool optimizeSwapAssign(CodeGen *gen, TypeKind typeKind, int structSize)
{
Instruction *prev = getPrevInstr(gen, 1);
Expand Down Expand Up @@ -526,8 +541,11 @@ void genPop(CodeGen *gen)

void genPopReg(CodeGen *gen, RegisterIndex regIndex)
{
const Instruction instr = {.opcode = OP_POP_REG, .tokKind = TOK_NONE, .typeKind = TYPE_NONE, .operand.intVal = regIndex};
genAddInstr(gen, &instr);
if (!optimizePopReg(gen, regIndex))
{
const Instruction instr = {.opcode = OP_POP_REG, .tokKind = TOK_NONE, .typeKind = TYPE_NONE, .operand.intVal = regIndex};
genAddInstr(gen, &instr);
}
}


Expand Down

0 comments on commit 995799a

Please sign in to comment.