[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Libjit] [PATCH] implement constant folding for arithmetic, shift and co
From: |
Jakob Löw |
Subject: |
[Libjit] [PATCH] implement constant folding for arithmetic, shift and compare instructions & Re: [PATCH 2/2] Add constant folding optimization pass |
Date: |
Sat, 29 Jul 2017 14:29:43 +0200 |
On Wed, 2017-05-24 at 14:01 +1000, George Barrett wrote:
> This commit adds an additional optimization pass that statically
> computes constant temporary variables and propagates the result to
> the
> operands of following instructions.
>
> Since this involves iterating through all subsequent instructions
> looking for references to the old temporary, this can be quite
> expensive
> for long functions. The math.pas test is the best example, showing
> _jit_block_fold_constants() to be the second-longest running function
> in
> libjit.
While I agree, that constant folding is a very important missing
feature in libjit I think making it an optimization pass with heavy
performance penalties is the wrong approach. In the following I present
a patch that folds constant expressions directly when calling a
jit_insn_ function. This means that when e.g. calling jit_insn_add with
two constant values you get a constant value back. Unfortunately this
approach still has two small disadvantages:
1. the optimization cannot be turned off via the
jit_function_set_optimization_level function
2. there is no optimal argument reordering so x * 3 * 3 becomes
(x * 3) * 3 while 3 * 3 * x becomes 9 * x
implement constant folding in a way that arithmetic, shift and compare
jit_insn_ functions return a jit_value_t that is a constant itself.
---
jit/jit-insn.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/jit/jit-insn.c b/jit/jit-insn.c
index 65d9038..798feed 100644
--- a/jit/jit-insn.c
+++ b/jit/jit-insn.c
@@ -657,7 +657,11 @@ static jit_value_t apply_unary_arith
func->builder->may_throw = 1;
}
value1 = jit_insn_convert(func, value1, result_type,
overflow_check);
- if(_jit_opcode_is_supported(oper))
+ if(jit_value_is_constant(value1))
+ {
+ return _jit_opcode_apply(func, oper, result_type,
value1, 0);
+ }
+ else if(_jit_opcode_is_supported(oper))
{
return apply_unary(func, oper, value1, result_type);
}
@@ -726,7 +730,11 @@ static jit_value_t apply_arith
}
value1 = jit_insn_convert(func, value1, result_type,
overflow_check);
value2 = jit_insn_convert(func, value2, result_type,
overflow_check);
- if(_jit_opcode_is_supported(oper))
+ if(jit_value_is_constant(value1) &&
jit_value_is_constant(value2))
+ {
+ return _jit_opcode_apply(func, oper, result_type,
value1, value2);
+ }
+ else if(_jit_opcode_is_supported(oper))
{
return apply_binary(func, oper, value1, value2,
result_type);
}
@@ -780,7 +788,11 @@ static jit_value_t apply_shift
}
value1 = jit_insn_convert(func, value1, result_type, 0);
value2 = jit_insn_convert(func, value2, count_type, 0);
- if(_jit_opcode_is_supported(oper))
+ if(jit_value_is_constant(value1) &&
jit_value_is_constant(value2))
+ {
+ return _jit_opcode_apply(func, oper, result_type,
value1, value2);
+ }
+ else if(_jit_opcode_is_supported(oper))
{
return apply_binary(func, oper, value1, value2,
result_type);
}
@@ -835,7 +847,11 @@ static jit_value_t apply_compare
}
value1 = jit_insn_convert(func, value1, result_type, 0);
value2 = jit_insn_convert(func, value2, result_type, 0);
- if(_jit_opcode_is_supported(oper))
+ if(jit_value_is_constant(value1) &&
jit_value_is_constant(value2))
+ {
+ return _jit_opcode_apply(func, oper, result_type,
value1, value2);
+ }
+ else if(_jit_opcode_is_supported(oper))
{
return apply_binary(func, oper, value1, value2,
jit_type_int);
}
--
2.11.0
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Libjit] [PATCH] implement constant folding for arithmetic, shift and compare instructions & Re: [PATCH 2/2] Add constant folding optimization pass,
Jakob Löw <=