zend_jit.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend JIT |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | https://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifndef HAVE_JIT_H
  19. #define HAVE_JIT_H
  20. #if defined(__x86_64__) || defined(i386) || defined(ZEND_WIN32)
  21. # define ZEND_JIT_TARGET_X86 1
  22. # define ZEND_JIT_TARGET_ARM64 0
  23. #elif defined (__aarch64__)
  24. # define ZEND_JIT_TARGET_X86 0
  25. # define ZEND_JIT_TARGET_ARM64 1
  26. #else
  27. # error "JIT not supported on this platform"
  28. #endif
  29. #define ZEND_JIT_LEVEL_NONE 0 /* no JIT */
  30. #define ZEND_JIT_LEVEL_MINIMAL 1 /* minimal JIT (subroutine threading) */
  31. #define ZEND_JIT_LEVEL_INLINE 2 /* selective inline threading */
  32. #define ZEND_JIT_LEVEL_OPT_FUNC 3 /* optimized JIT based on Type-Inference */
  33. #define ZEND_JIT_LEVEL_OPT_FUNCS 4 /* optimized JIT based on Type-Inference and call-tree */
  34. #define ZEND_JIT_LEVEL_OPT_SCRIPT 5 /* optimized JIT based on Type-Inference and inner-procedure analysis */
  35. #define ZEND_JIT_ON_SCRIPT_LOAD 0
  36. #define ZEND_JIT_ON_FIRST_EXEC 1
  37. #define ZEND_JIT_ON_PROF_REQUEST 2 /* compile the most frequently caled on first request functions */
  38. #define ZEND_JIT_ON_HOT_COUNTERS 3 /* compile functions after N calls or loop iterations */
  39. #define ZEND_JIT_ON_DOC_COMMENT 4 /* compile functions with "@jit" tag in doc-comments */
  40. #define ZEND_JIT_ON_HOT_TRACE 5 /* trace functions after N calls or loop iterations */
  41. #define ZEND_JIT_REG_ALLOC_LOCAL (1<<0) /* local linear scan register allocation */
  42. #define ZEND_JIT_REG_ALLOC_GLOBAL (1<<1) /* global linear scan register allocation */
  43. #define ZEND_JIT_CPU_AVX (1<<2) /* use AVX instructions, if available */
  44. #define ZEND_JIT_DEFAULT_BUFFER_SIZE "0"
  45. #define ZEND_JIT_COUNTER_INIT 32531
  46. #define ZEND_JIT_DEBUG_ASM (1<<0)
  47. #define ZEND_JIT_DEBUG_SSA (1<<1)
  48. #define ZEND_JIT_DEBUG_REG_ALLOC (1<<2)
  49. #define ZEND_JIT_DEBUG_ASM_STUBS (1<<3)
  50. #define ZEND_JIT_DEBUG_PERF (1<<4)
  51. #define ZEND_JIT_DEBUG_PERF_DUMP (1<<5)
  52. #define ZEND_JIT_DEBUG_OPROFILE (1<<6)
  53. #define ZEND_JIT_DEBUG_VTUNE (1<<7)
  54. #define ZEND_JIT_DEBUG_GDB (1<<8)
  55. #define ZEND_JIT_DEBUG_SIZE (1<<9)
  56. #define ZEND_JIT_DEBUG_ASM_ADDR (1<<10)
  57. #define ZEND_JIT_DEBUG_TRACE_START (1<<12)
  58. #define ZEND_JIT_DEBUG_TRACE_STOP (1<<13)
  59. #define ZEND_JIT_DEBUG_TRACE_COMPILED (1<<14)
  60. #define ZEND_JIT_DEBUG_TRACE_EXIT (1<<15)
  61. #define ZEND_JIT_DEBUG_TRACE_ABORT (1<<16)
  62. #define ZEND_JIT_DEBUG_TRACE_BLACKLIST (1<<17)
  63. #define ZEND_JIT_DEBUG_TRACE_BYTECODE (1<<18)
  64. #define ZEND_JIT_DEBUG_TRACE_TSSA (1<<19)
  65. #define ZEND_JIT_DEBUG_TRACE_EXIT_INFO (1<<20)
  66. #define ZEND_JIT_DEBUG_PERSISTENT 0x1f0 /* profile and debbuger flags can't be changed at run-time */
  67. #define ZEND_JIT_TRACE_MAX_LENGTH 1024 /* max length of single trace */
  68. #define ZEND_JIT_TRACE_MAX_EXITS 512 /* max number of side exits per trace */
  69. #define ZEND_JIT_TRACE_MAX_FUNCS 30 /* max number of different functions in a single trace */
  70. #define ZEND_JIT_TRACE_MAX_CALL_DEPTH 10 /* max depth of inlined calls */
  71. #define ZEND_JIT_TRACE_MAX_RET_DEPTH 4 /* max depth of inlined returns */
  72. #define ZEND_JIT_TRACE_MAX_LOOPS_UNROLL 10 /* max number of unrolled loops */
  73. #define ZEND_JIT_TRACE_BAD_ROOT_SLOTS 64 /* number of slots in bad root trace cache */
  74. typedef struct _zend_jit_trace_rec zend_jit_trace_rec;
  75. typedef struct _zend_jit_trace_stack_frame zend_jit_trace_stack_frame;
  76. typedef struct _sym_node zend_sym_node;
  77. typedef struct _zend_jit_globals {
  78. bool enabled;
  79. bool on;
  80. uint8_t trigger;
  81. uint8_t opt_level;
  82. uint32_t opt_flags;
  83. const char *options;
  84. zend_long buffer_size;
  85. zend_long debug;
  86. zend_long bisect_limit;
  87. double prof_threshold;
  88. zend_long max_root_traces; /* max number of root traces */
  89. zend_long max_side_traces; /* max number of side traces (per root trace) */
  90. zend_long max_exit_counters; /* max total number of side exits for all traces */
  91. zend_long hot_loop;
  92. zend_long hot_func;
  93. zend_long hot_return;
  94. zend_long hot_side_exit; /* number of exits before taking side trace */
  95. zend_long blacklist_root_trace; /* number of attempts to JIT a root trace before blacklist it */
  96. zend_long blacklist_side_trace; /* number of attempts to JIT a side trace before blacklist it */
  97. zend_long max_loop_unrolls; /* max number of unrolled loops */
  98. zend_long max_recursive_calls; /* max number of recursive inlined call unrolls */
  99. zend_long max_recursive_returns; /* max number of recursive inlined return unrolls */
  100. zend_long max_polymorphic_calls; /* max number of inlined polymorphic calls */
  101. zend_sym_node *symbols; /* symbols for disassembler */
  102. bool tracing;
  103. zend_jit_trace_rec *current_trace;
  104. zend_jit_trace_stack_frame *current_frame;
  105. const zend_op *bad_root_cache_opline[ZEND_JIT_TRACE_BAD_ROOT_SLOTS];
  106. uint8_t bad_root_cache_count[ZEND_JIT_TRACE_BAD_ROOT_SLOTS];
  107. uint8_t bad_root_cache_stop[ZEND_JIT_TRACE_BAD_ROOT_SLOTS];
  108. uint32_t bad_root_slot;
  109. uint8_t *exit_counters;
  110. } zend_jit_globals;
  111. #ifdef ZTS
  112. # define JIT_G(v) ZEND_TSRMG(jit_globals_id, zend_jit_globals *, v)
  113. extern int jit_globals_id;
  114. #else
  115. # define JIT_G(v) (jit_globals.v)
  116. extern zend_jit_globals jit_globals;
  117. #endif
  118. ZEND_EXT_API int zend_jit_op_array(zend_op_array *op_array, zend_script *script);
  119. ZEND_EXT_API int zend_jit_script(zend_script *script);
  120. ZEND_EXT_API void zend_jit_unprotect(void);
  121. ZEND_EXT_API void zend_jit_protect(void);
  122. ZEND_EXT_API void zend_jit_init(void);
  123. ZEND_EXT_API int zend_jit_config(zend_string *jit_options, int stage);
  124. ZEND_EXT_API int zend_jit_debug_config(zend_long old_val, zend_long new_val, int stage);
  125. ZEND_EXT_API int zend_jit_check_support(void);
  126. ZEND_EXT_API int zend_jit_startup(void *jit_buffer, size_t size, bool reattached);
  127. ZEND_EXT_API void zend_jit_shutdown(void);
  128. ZEND_EXT_API void zend_jit_activate(void);
  129. ZEND_EXT_API void zend_jit_deactivate(void);
  130. ZEND_EXT_API void zend_jit_status(zval *ret);
  131. ZEND_EXT_API void zend_jit_restart(void);
  132. typedef struct _zend_lifetime_interval zend_lifetime_interval;
  133. typedef struct _zend_life_range zend_life_range;
  134. struct _zend_life_range {
  135. uint32_t start;
  136. uint32_t end;
  137. zend_life_range *next;
  138. };
  139. #define ZREG_FLAGS_SHIFT 8
  140. #define ZREG_STORE (1<<0)
  141. #define ZREG_LOAD (1<<1)
  142. #define ZREG_LAST_USE (1<<2)
  143. #define ZREG_SPLIT (1<<3)
  144. struct _zend_lifetime_interval {
  145. int ssa_var;
  146. union {
  147. struct {
  148. ZEND_ENDIAN_LOHI_3(
  149. int8_t reg,
  150. uint8_t flags,
  151. uint16_t reserved
  152. )};
  153. uint32_t reg_flags;
  154. };
  155. zend_life_range range;
  156. zend_lifetime_interval *hint;
  157. zend_lifetime_interval *used_as_hint;
  158. zend_lifetime_interval *list_next;
  159. };
  160. #endif /* HAVE_JIT_H */