From 68e3c3ed6eb210ea175d5559e81e8596b6fec01c Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Thu, 20 May 2004 01:24:54 +0000 Subject: [PATCH] Convert constant conditional branches such as "if true goto L" into unconditional branches. --- ChangeLog | 6 +++++ include/jit/jit-value.h | 1 + jit/jit-insn.c | 28 +++++++++++++++++++ jit/jit-value.c | 60 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) diff --git a/ChangeLog b/ChangeLog index 80886d8..9d3e55d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,10 @@ +2004-05-20 Rhys Weatherley + + * include/jit/jit-value.h, jit/jit-insn.c, jit/jit-value.c: + convert constant conditional branches such as "if true goto L" into + unconditional branches. + 2004-05-15 Rhys Weatherley * tools/gen-apply.c: fix a macro generation bug for Win32 systems. diff --git a/include/jit/jit-value.h b/include/jit/jit-value.h index c72f0f2..81e7c56 100644 --- a/include/jit/jit-value.h +++ b/include/jit/jit-value.h @@ -90,6 +90,7 @@ jit_long jit_value_get_long_constant(jit_value_t value) JIT_NOTHROW; jit_float32 jit_value_get_float32_constant(jit_value_t value) JIT_NOTHROW; jit_float64 jit_value_get_float64_constant(jit_value_t value) JIT_NOTHROW; jit_nfloat jit_value_get_nfloat_constant(jit_value_t value) JIT_NOTHROW; +int jit_value_is_true(jit_value_t value) JIT_NOTHROW; int jit_constant_convert (jit_constant_t *result, const jit_constant_t *value, jit_type_t type, int overflow_check) JIT_NOTHROW; diff --git a/jit/jit-insn.c b/jit/jit-insn.c index 9d0b590..3093346 100644 --- a/jit/jit-insn.c +++ b/jit/jit-insn.c @@ -3547,6 +3547,20 @@ int jit_insn_branch_if *label = (func->builder->next_label)++; } + /* If the condition is constant, then convert it into either + an unconditional branch or a fall-through, as appropriate */ + if(jit_value_is_constant(value)) + { + if(jit_value_is_true(value)) + { + return jit_insn_branch(func, label); + } + else + { + return 1; + } + } + /* Determine if we can replace a previous comparison instruction */ block = func->builder->current_block; insn = _jit_block_get_last(block); @@ -3734,6 +3748,20 @@ int jit_insn_branch_if_not *label = (func->builder->next_label)++; } + /* If the condition is constant, then convert it into either + an unconditional branch or a fall-through, as appropriate */ + if(jit_value_is_constant(value)) + { + if(!jit_value_is_true(value)) + { + return jit_insn_branch(func, label); + } + else + { + return 1; + } + } + /* Determine if we can replace a previous comparison instruction */ block = func->builder->current_block; insn = _jit_block_get_last(block); diff --git a/jit/jit-value.c b/jit/jit-value.c index db018f1..7f0db7c 100644 --- a/jit/jit-value.c +++ b/jit/jit-value.c @@ -929,6 +929,66 @@ jit_nfloat jit_value_get_nfloat_constant(jit_value_t value) return (jit_nfloat)0.0; } +/*@ + * @deftypefun int jit_value_is_true (jit_value_t value) + * Determine if @code{value} is constant and non-zero. + * @end deftypefun +@*/ +int jit_value_is_true(jit_value_t value) +{ + if(!value || !(value->is_constant)) + { + return 0; + } + else if(value->is_nint_constant) + { + return (value->address != 0); + } + else + { + switch(jit_type_normalize(value->type)->kind) + { + case JIT_TYPE_LONG: + case JIT_TYPE_ULONG: + { + if(jit_value_get_long_constant(value) != 0) + { + return 1; + } + } + break; + + case JIT_TYPE_FLOAT32: + { + if(jit_value_get_float32_constant(value) != (jit_float32)0.0) + { + return 1; + } + } + break; + + case JIT_TYPE_FLOAT64: + { + if(jit_value_get_float64_constant(value) != (jit_float64)0.0) + { + return 1; + } + } + break; + + case JIT_TYPE_NFLOAT: + { + if(jit_value_get_nfloat_constant(value) != (jit_nfloat)0.0) + { + return 1; + } + } + break; + } + return 0; + } +} + /*@ * @deftypefun int jit_constant_convert ({jit_constant_t *} result, {const jit_constant_t *} value, jit_type_t type, int overflow_check) * Convert a the constant @code{value} into a new @code{type}, and -- 2.47.3