*/
#include "jit-internal.h"
+#include <jit/jit-dump.h>
#define USE_FORWARD_PROPAGATION 1
#define USE_BACKWARD_PROPAGATION 1
/* There is no next use of this value and it is not
live on exit from the block. So we can discard
the entire instruction as it will have no effect */
+#ifdef _JIT_COMPILE_DEBUG
+ printf("liveness analysis: optimize away instruction '");
+ jit_dump_insn(stdout, block->func, insn);
+ printf("'\n");
+#endif
insn->opcode = (short)JIT_OP_NOP;
continue;
}
}
}
-#if USE_FORWARD_PROPAGATION || USE_BACKWARD_PROPAGATION
+#if defined(USE_FORWARD_PROPAGATION) || defined(USE_BACKWARD_PROPAGATION)
static int
is_copy_opcode(int opcode)
{
continue;
}
- /* Copy to itself could be safely discarded */
+ /* Discard copy to itself */
if(dest == value)
{
+#ifdef _JIT_COMPILE_DEBUG
+ printf("forward copy propagation: optimize away copy to itself in '");
+ jit_dump_insn(stdout, block->func, insn);
+ printf("'\n");
+#endif
insn->opcode = (short)JIT_OP_NOP;
optimized = 1;
continue;
}
/* Not smart enough to tell when it is safe to optimize copying
- to a value used in other basic block. So just give up. */
+ to a value that is used in other basic blocks or may be
+ aliased. */
if(!dest->is_temporary)
{
continue;
}
+ if(dest->is_addressable || dest->is_volatile)
+ {
+ continue;
+ }
iter2 = iter;
while((insn2 = jit_insn_iter_next(&iter2)) != 0)
{
- flags2 = insn2->flags;
+ /* Skip NOP instructions, which may have arguments left
+ over from when the instruction was replaced, but which
+ are not relevant to our analysis */
+ if(insn->opcode == JIT_OP_NOP)
+ {
+ continue;
+ }
+ flags2 = insn2->flags;
if((flags2 & JIT_INSN_DEST_OTHER_FLAGS) == 0)
{
if((flags2 & JIT_INSN_DEST_IS_VALUE) == 0)
}
else if(insn2->dest == dest)
{
+#ifdef _JIT_COMPILE_DEBUG
+ printf("forward copy propagation: in '");
+ jit_dump_insn(stdout, block->func, insn2);
+ printf("' replace ");
+ jit_dump_value(stdout, block->func, insn2->dest, 0);
+ printf(" with ");
+ jit_dump_value(stdout, block->func, value, 0);
+ printf("'\n");
+#endif
insn2->dest = value;
optimized = 1;
}
{
if(insn2->value1 == dest)
{
+#ifdef _JIT_COMPILE_DEBUG
+ printf("forward copy propagation: in '");
+ jit_dump_insn(stdout, block->func, insn2);
+ printf("' replace ");
+ jit_dump_value(stdout, block->func, insn2->value1, 0);
+ printf(" with ");
+ jit_dump_value(stdout, block->func, value, 0);
+ printf("'\n");
+#endif
insn2->value1 = value;
optimized = 1;
}
{
if(insn2->value2 == dest)
{
+#ifdef _JIT_COMPILE_DEBUG
+ printf("forward copy propagation: in '");
+ jit_dump_insn(stdout, block->func, insn2);
+ printf("' replace ");
+ jit_dump_value(stdout, block->func, insn2->value2, 0);
+ printf(" with ");
+ jit_dump_value(stdout, block->func, value, 0);
+ printf("'\n");
+#endif
insn2->value2 = value;
optimized = 1;
}
}
#endif
-#if USE_BACKWARD_PROPAGATION
+#ifdef USE_BACKWARD_PROPAGATION
/*
* Perform simple copy propagation within basic block for the case when a
* temporary value is stored to another value. This replaces instructions
{
continue;
}
+ if(dest->is_addressable || dest->is_volatile)
+ {
+ continue;
+ }
value = insn->value1;
if(value == 0)
{
continue;
}
+ if(value->is_addressable || value->is_volatile)
+ {
+ continue;
+ }
- /* Copy to itself could be safely discarded */
+ /* Discard copy to itself */
if(dest == value)
{
+#ifdef _JIT_COMPILE_DEBUG
+ printf("backward copy propagation: optimize away copy to itself in '");
+ jit_dump_insn(stdout, block->func, insn);
+ printf("'\n");
+#endif
insn->opcode = (short)JIT_OP_NOP;
optimized = 1;
continue;
iter2 = iter;
while((insn2 = jit_insn_iter_previous(&iter2)) != 0)
{
- flags2 = insn2->flags;
+ /* Skip NOP instructions, which may have arguments left
+ over from when the instruction was replaced, but which
+ are not relevant to our analysis */
+ if(insn->opcode == JIT_OP_NOP)
+ {
+ continue;
+ }
+ flags2 = insn2->flags;
if((flags2 & JIT_INSN_DEST_OTHER_FLAGS) == 0)
{
if(insn2->dest == dest)
{
if((flags2 & JIT_INSN_DEST_IS_VALUE) == 0)
{
+#ifdef _JIT_COMPILE_DEBUG
+ printf("backward copy propagation: in '");
+ jit_dump_insn(stdout, block->func, insn2);
+ printf("' replace ");
+ jit_dump_value(stdout, block->func, insn2->dest, 0);
+ printf(" with ");
+ jit_dump_value(stdout, block->func, dest, 0);
+ printf(" and optimize away '");
+ jit_dump_insn(stdout, block->func, insn);
+ printf("'\n");
+#endif
insn->opcode = (short)JIT_OP_NOP;
insn2->dest = dest;
optimized = 1;
/* Perform peephole optimization on branches to branches */
_jit_block_peephole_branch(block);
-#if USE_FORWARD_PROPAGATION
+#ifdef USE_FORWARD_PROPAGATION
/* Perform forward copy propagation for the block */
forward_propagation(block);
#endif
/* Compute the liveness flags for the block */
compute_liveness_for_block(block);
-#if USE_BACKWARD_PROPAGATION
+#ifdef USE_BACKWARD_PROPAGATION
/* Perform backward copy propagation for the block */
if(backward_propagation(block))
{