From ed556939dfc61dc5f20346905155f36b14a2e3a1 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 26 Nov 2024 21:22:12 +0100 Subject: [PATCH] Extract call_level conditions out to separate functions (#16949) These are repeated a couple of times, so centralise it in 2 functions to reduce repetition and make updating this less error-prone. --- ext/opcache/jit/zend_jit.c | 77 +++++++++++++++++--------------- ext/opcache/jit/zend_jit_trace.c | 51 +++++---------------- 2 files changed, 51 insertions(+), 77 deletions(-) diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c index cc22e7375a8c1..558faae22d0a0 100644 --- a/ext/opcache/jit/zend_jit.c +++ b/ext/opcache/jit/zend_jit.c @@ -727,6 +727,38 @@ ZEND_EXT_API void zend_jit_status(zval *ret) add_assoc_zval(ret, "jit", &stats); } +static bool zend_jit_inc_call_level(uint8_t opcode) +{ + switch (opcode) { + case ZEND_INIT_FCALL: + case ZEND_INIT_FCALL_BY_NAME: + case ZEND_INIT_NS_FCALL_BY_NAME: + case ZEND_INIT_METHOD_CALL: + case ZEND_INIT_DYNAMIC_CALL: + case ZEND_INIT_STATIC_METHOD_CALL: + case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL: + case ZEND_INIT_USER_CALL: + case ZEND_NEW: + return true; + default: + return false; + } +} + +static bool zend_jit_dec_call_level(uint8_t opcode) +{ + switch (opcode) { + case ZEND_DO_FCALL: + case ZEND_DO_ICALL: + case ZEND_DO_UCALL: + case ZEND_DO_FCALL_BY_NAME: + case ZEND_CALLABLE_CONVERT: + return true; + default: + return false; + } +} + static zend_string *zend_jit_func_name(const zend_op_array *op_array) { smart_str buf = {0}; @@ -1463,17 +1495,8 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op for (i = ssa->cfg.blocks[b].start; i <= end; i++) { zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[i] : NULL; opline = op_array->opcodes + i; - switch (opline->opcode) { - case ZEND_INIT_FCALL: - case ZEND_INIT_FCALL_BY_NAME: - case ZEND_INIT_NS_FCALL_BY_NAME: - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_DYNAMIC_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL: - case ZEND_INIT_USER_CALL: - case ZEND_NEW: - call_level++; + if (zend_jit_inc_call_level(opline->opcode)) { + call_level++; } if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) { @@ -2582,25 +2605,10 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op i++; for (; i < end; i++) { opline = op_array->opcodes + i; - switch (opline->opcode) { - case ZEND_INIT_FCALL: - case ZEND_INIT_FCALL_BY_NAME: - case ZEND_INIT_NS_FCALL_BY_NAME: - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_DYNAMIC_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL: - case ZEND_INIT_USER_CALL: - case ZEND_NEW: - call_level++; - break; - case ZEND_DO_FCALL: - case ZEND_DO_ICALL: - case ZEND_DO_UCALL: - case ZEND_DO_FCALL_BY_NAME: - case ZEND_CALLABLE_CONVERT: - call_level--; - break; + if (zend_jit_inc_call_level(opline->opcode)) { + call_level++; + } else if (zend_jit_dec_call_level(opline->opcode)) { + call_level--; } } opline = op_array->opcodes + i; @@ -2715,13 +2723,8 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op } } done: - switch (opline->opcode) { - case ZEND_DO_FCALL: - case ZEND_DO_ICALL: - case ZEND_DO_UCALL: - case ZEND_DO_FCALL_BY_NAME: - case ZEND_CALLABLE_CONVERT: - call_level--; + if (zend_jit_dec_call_level(opline->opcode)) { + call_level--; } } zend_jit_bb_end(&ctx, b); diff --git a/ext/opcache/jit/zend_jit_trace.c b/ext/opcache/jit/zend_jit_trace.c index a8ea08cd1733c..c7c470330f060 100644 --- a/ext/opcache/jit/zend_jit_trace.c +++ b/ext/opcache/jit/zend_jit_trace.c @@ -1164,28 +1164,13 @@ static const zend_op *zend_jit_trace_find_init_fcall_op(zend_jit_trace_rec *p, c if (opline) { while (opline > op_array->opcodes) { opline--; - switch (opline->opcode) { - case ZEND_INIT_FCALL: - case ZEND_INIT_FCALL_BY_NAME: - case ZEND_INIT_NS_FCALL_BY_NAME: - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_DYNAMIC_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL: - case ZEND_INIT_USER_CALL: - case ZEND_NEW: - if (call_level == 0) { - return opline; - } - call_level--; - break; - case ZEND_DO_FCALL: - case ZEND_DO_ICALL: - case ZEND_DO_UCALL: - case ZEND_DO_FCALL_BY_NAME: - case ZEND_CALLABLE_CONVERT: - call_level++; - break; + if (zend_jit_inc_call_level(opline->opcode)) { + if (call_level == 0) { + return opline; + } + call_level--; + } else if (zend_jit_dec_call_level(opline->opcode)) { + call_level++; } } } @@ -4394,17 +4379,8 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par frame_flags = 0; - switch (opline->opcode) { - case ZEND_INIT_FCALL: - case ZEND_INIT_FCALL_BY_NAME: - case ZEND_INIT_NS_FCALL_BY_NAME: - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_DYNAMIC_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL: - case ZEND_INIT_USER_CALL: - case ZEND_NEW: - frame->call_level++; + if (zend_jit_inc_call_level(opline->opcode)) { + frame->call_level++; } if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) { @@ -6426,13 +6402,8 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par done: polymorphic_side_trace = 0; - switch (opline->opcode) { - case ZEND_DO_FCALL: - case ZEND_DO_ICALL: - case ZEND_DO_UCALL: - case ZEND_DO_FCALL_BY_NAME: - case ZEND_CALLABLE_CONVERT: - frame->call_level--; + if (zend_jit_dec_call_level(opline->opcode)) { + frame->call_level--; } if (ra) {