1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-17 18:59:56 +00:00

Add recursion to the pruning optimization.

This commit is contained in:
Calvin Rose 2023-05-29 18:05:14 -05:00
parent 4782a76bca
commit fcca9bbab3

View File

@ -177,6 +177,10 @@ void janet_bytecode_remove_noops(JanetFuncDef *def) {
* noops. Input is assumed valid bytecode. */
void janet_bytecode_movopt(JanetFuncDef *def) {
JanetcRegisterAllocator ra;
int recur = 1;
/* Iterate this until no more instructions can be removed. */
while (recur) {
janetc_regalloc_init(&ra);
/* Look for slots that have writes but no reads (and aren't in the closure bitset). */
@ -330,6 +334,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
/* Iterate and set noops on instructions that make writes that no one ever reads.
* Only set noops for instructions with no side effects - moves, loads, etc. that can't
* raise errors (outside of systemic errors like oom or stack overflow). */
recur = 0;
for (int32_t i = 0; i < def->bytecode_length; i++) {
uint32_t instr = def->bytecode[i];
switch (instr & 0x7F) {
@ -345,6 +350,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
case JOP_MAKE_BRACKET_TUPLE: {
if (!janetc_regalloc_check(&ra, DD)) {
def->bytecode[i] = JOP_NOOP;
recur = 1;
}
}
break;
@ -352,6 +358,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
case JOP_MOVE_FAR: {
if (!janetc_regalloc_check(&ra, EE)) {
def->bytecode[i] = JOP_NOOP;
recur = 1;
}
}
break;
@ -366,6 +373,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
case JOP_CLOSURE: {
if (!janetc_regalloc_check(&ra, AA)) {
def->bytecode[i] = JOP_NOOP;
recur = 1;
}
}
break;
@ -378,6 +386,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
#undef CC
#undef DD
#undef EE
}
}
/* Verify some bytecode */