Skip to content

Commit e1ab2cf

Browse files
committed
Introduce ZEND_ASSUME() macro to help optimizing code.
Reuse ZEND_ASSERT() conditions for optimization.
1 parent fc28ae2 commit e1ab2cf

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

Zend/zend_portability.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,35 @@
7878
#include <intrin.h>
7979
#endif
8080

81+
/* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */
82+
#ifdef __GNUC__
83+
# define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
84+
#else
85+
# define ZEND_GCC_VERSION 0
86+
#endif
87+
88+
#if defined(ZEND_WIN32)
89+
# define ZEND_ASSUME(c) __assume(c)
90+
#elif defined(__GNUC__) && PHP_HAVE_BUILTIN_EXPECT && ZEND_GCC_VERSION >= 4005
91+
# define ZEND_ASSUME(c) do { \
92+
if (__builtin_expect(!(c), 0)) __builtin_unreachable(); \
93+
} while (0)
94+
#else
95+
# define ZEND_ASSUME(c)
96+
#endif
97+
98+
#if ZEND_DEBUG
99+
# define ZEND_ASSERT(c) assert(c)
100+
#else
101+
# define ZEND_ASSERT(c) ZEND_ASSUME(c)
102+
#endif
103+
81104
/* Only use this macro if you know for sure that all of the switches values
82105
are covered by its case statements */
83106
#if ZEND_DEBUG
84107
# define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSERT(0); break;
85-
#elif defined(ZEND_WIN32)
86-
# define EMPTY_SWITCH_DEFAULT_CASE() default: __assume(0); break;
87108
#else
88-
# define EMPTY_SWITCH_DEFAULT_CASE()
109+
# define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSUME(0); break;
89110
#endif
90111

91112
/* all HAVE_XXX test have to be after the include of zend_config above */
@@ -145,13 +166,6 @@ char *alloca();
145166
# define __has_attribute(x) 0
146167
#endif
147168

148-
/* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */
149-
#ifdef __GNUC__
150-
# define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
151-
#else
152-
# define ZEND_GCC_VERSION 0
153-
#endif
154-
155169
#if ZEND_GCC_VERSION >= 2096
156170
# define ZEND_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
157171
#else
@@ -337,7 +351,6 @@ char *alloca();
337351
# define ZEND_FILE_LINE_EMPTY_CC , ZEND_FILE_LINE_EMPTY_C
338352
# define ZEND_FILE_LINE_ORIG_RELAY_C __zend_orig_filename, __zend_orig_lineno
339353
# define ZEND_FILE_LINE_ORIG_RELAY_CC , ZEND_FILE_LINE_ORIG_RELAY_C
340-
# define ZEND_ASSERT(c) assert(c)
341354
#else
342355
# define ZEND_FILE_LINE_D
343356
# define ZEND_FILE_LINE_DC
@@ -351,7 +364,6 @@ char *alloca();
351364
# define ZEND_FILE_LINE_EMPTY_CC
352365
# define ZEND_FILE_LINE_ORIG_RELAY_C
353366
# define ZEND_FILE_LINE_ORIG_RELAY_CC
354-
# define ZEND_ASSERT(c)
355367
#endif /* ZEND_DEBUG */
356368

357369
#if ZEND_DEBUG

Zend/zend_vm_def.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,10 +3229,12 @@ ZEND_VM_HANDLER(129, ZEND_DO_ICALL, ANY, ANY)
32293229

32303230
fbc->internal_function.handler(call, ret);
32313231

3232+
#if ZEND_DEBUG
32323233
ZEND_ASSERT(
32333234
!call->func ||
32343235
!(call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
32353236
zend_verify_internal_return_type(call->func, EX_VAR(opline->result.var)));
3237+
#endif
32363238

32373239
EG(current_execute_data) = call->prev_execute_data;
32383240
zend_vm_stack_free_args(call);
@@ -3357,10 +3359,12 @@ ZEND_VM_HANDLER(131, ZEND_DO_FCALL_BY_NAME, ANY, ANY)
33573359

33583360
fbc->internal_function.handler(call, ret);
33593361

3362+
#if ZEND_DEBUG
33603363
ZEND_ASSERT(
33613364
!call->func ||
33623365
!(call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
33633366
zend_verify_internal_return_type(call->func, EX_VAR(opline->result.var)));
3367+
#endif
33643368

33653369
EG(current_execute_data) = call->prev_execute_data;
33663370
zend_vm_stack_free_args(call);
@@ -3494,10 +3498,12 @@ ZEND_VM_HANDLER(60, ZEND_DO_FCALL, ANY, ANY)
34943498
zend_execute_internal(call, ret);
34953499
}
34963500

3501+
#if ZEND_DEBUG
34973502
ZEND_ASSERT(
34983503
!call->func ||
34993504
!(call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
35003505
zend_verify_internal_return_type(call->func, EX_VAR(opline->result.var)));
3506+
#endif
35013507

35023508
EG(current_execute_data) = call->prev_execute_data;
35033509
zend_vm_stack_free_args(call);

Zend/zend_vm_execute.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,12 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DO_ICALL_SPEC_HANDLER(ZEND_OPC
560560

561561
fbc->internal_function.handler(call, ret);
562562

563+
#if ZEND_DEBUG
563564
ZEND_ASSERT(
564565
!call->func ||
565566
!(call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
566567
zend_verify_internal_return_type(call->func, EX_VAR(opline->result.var)));
568+
#endif
567569

568570
EG(current_execute_data) = call->prev_execute_data;
569571
zend_vm_stack_free_args(call);
@@ -688,10 +690,12 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER(
688690

689691
fbc->internal_function.handler(call, ret);
690692

693+
#if ZEND_DEBUG
691694
ZEND_ASSERT(
692695
!call->func ||
693696
!(call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
694697
zend_verify_internal_return_type(call->func, EX_VAR(opline->result.var)));
698+
#endif
695699

696700
EG(current_execute_data) = call->prev_execute_data;
697701
zend_vm_stack_free_args(call);
@@ -825,10 +829,12 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DO_FCALL_SPEC_HANDLER(ZEND_OPC
825829
zend_execute_internal(call, ret);
826830
}
827831

832+
#if ZEND_DEBUG
828833
ZEND_ASSERT(
829834
!call->func ||
830835
!(call->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
831836
zend_verify_internal_return_type(call->func, EX_VAR(opline->result.var)));
837+
#endif
832838

833839
EG(current_execute_data) = call->prev_execute_data;
834840
zend_vm_stack_free_args(call);

0 commit comments

Comments
 (0)