1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +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. */ * noops. Input is assumed valid bytecode. */
void janet_bytecode_movopt(JanetFuncDef *def) { void janet_bytecode_movopt(JanetFuncDef *def) {
JanetcRegisterAllocator ra; JanetcRegisterAllocator ra;
int recur = 1;
/* Iterate this until no more instructions can be removed. */
while (recur) {
janetc_regalloc_init(&ra); janetc_regalloc_init(&ra);
/* Look for slots that have writes but no reads (and aren't in the closure bitset). */ /* 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. /* 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 * 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). */ * raise errors (outside of systemic errors like oom or stack overflow). */
recur = 0;
for (int32_t i = 0; i < def->bytecode_length; i++) { for (int32_t i = 0; i < def->bytecode_length; i++) {
uint32_t instr = def->bytecode[i]; uint32_t instr = def->bytecode[i];
switch (instr & 0x7F) { switch (instr & 0x7F) {
@ -345,6 +350,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
case JOP_MAKE_BRACKET_TUPLE: { case JOP_MAKE_BRACKET_TUPLE: {
if (!janetc_regalloc_check(&ra, DD)) { if (!janetc_regalloc_check(&ra, DD)) {
def->bytecode[i] = JOP_NOOP; def->bytecode[i] = JOP_NOOP;
recur = 1;
} }
} }
break; break;
@ -352,6 +358,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
case JOP_MOVE_FAR: { case JOP_MOVE_FAR: {
if (!janetc_regalloc_check(&ra, EE)) { if (!janetc_regalloc_check(&ra, EE)) {
def->bytecode[i] = JOP_NOOP; def->bytecode[i] = JOP_NOOP;
recur = 1;
} }
} }
break; break;
@ -366,6 +373,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
case JOP_CLOSURE: { case JOP_CLOSURE: {
if (!janetc_regalloc_check(&ra, AA)) { if (!janetc_regalloc_check(&ra, AA)) {
def->bytecode[i] = JOP_NOOP; def->bytecode[i] = JOP_NOOP;
recur = 1;
} }
} }
break; break;
@ -379,6 +387,7 @@ void janet_bytecode_movopt(JanetFuncDef *def) {
#undef DD #undef DD
#undef EE #undef EE
} }
}
/* Verify some bytecode */ /* Verify some bytecode */
int janet_verify(JanetFuncDef *def) { int janet_verify(JanetFuncDef *def) {