zend_inference.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, e-SSA based Type & Range Inference |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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. | http://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 ZEND_INFERENCE_H
  19. #define ZEND_INFERENCE_H
  20. #include "zend_optimizer.h"
  21. #include "zend_ssa.h"
  22. #include "zend_bitset.h"
  23. /* Bitmask for type inference (zend_ssa_var_info.type) */
  24. #include "zend_type_info.h"
  25. #define MAY_BE_IN_REG (1<<25) /* value allocated in CPU register */
  26. //TODO: remome MAY_BE_RC1, MAY_BE_RCN???
  27. #define MAY_BE_RC1 (1<<27) /* may be non-reference with refcount == 1 */
  28. #define MAY_BE_RCN (1<<28) /* may be non-reference with refcount > 1 */
  29. #define MAY_HAVE_DTOR \
  30. (MAY_BE_OBJECT|MAY_BE_RESOURCE \
  31. |MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE)
  32. #define DEFINE_SSA_OP_HAS_RANGE(opN) \
  33. static zend_always_inline zend_bool _ssa_##opN##_has_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  34. { \
  35. if (opline->opN##_type == IS_CONST) { \
  36. zval *zv = CRT_CONSTANT_EX(op_array, opline, opline->opN, ssa->rt_constants); \
  37. return (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_TRUE || Z_TYPE_P(zv) == IS_FALSE || Z_TYPE_P(zv) == IS_NULL); \
  38. } else { \
  39. return (opline->opN##_type != IS_UNUSED && \
  40. ssa->ops && \
  41. ssa->var_info && \
  42. ssa->ops[opline - op_array->opcodes].opN##_use >= 0 && \
  43. ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].has_range); \
  44. } \
  45. return 0; \
  46. }
  47. #define DEFINE_SSA_OP_MIN_RANGE(opN) \
  48. static zend_always_inline zend_long _ssa_##opN##_min_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  49. { \
  50. if (opline->opN##_type == IS_CONST) { \
  51. zval *zv = CRT_CONSTANT_EX(op_array, opline, opline->opN, ssa->rt_constants); \
  52. if (Z_TYPE_P(zv) == IS_LONG) { \
  53. return Z_LVAL_P(zv); \
  54. } else if (Z_TYPE_P(zv) == IS_TRUE) { \
  55. return 1; \
  56. } else if (Z_TYPE_P(zv) == IS_FALSE) { \
  57. return 0; \
  58. } else if (Z_TYPE_P(zv) == IS_NULL) { \
  59. return 0; \
  60. } \
  61. } else if (opline->opN##_type != IS_UNUSED && \
  62. ssa->ops && \
  63. ssa->var_info && \
  64. ssa->ops[opline - op_array->opcodes].opN##_use >= 0 && \
  65. ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].has_range) { \
  66. return ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].range.min; \
  67. } \
  68. return ZEND_LONG_MIN; \
  69. }
  70. #define DEFINE_SSA_OP_MAX_RANGE(opN) \
  71. static zend_always_inline zend_long _ssa_##opN##_max_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  72. { \
  73. if (opline->opN##_type == IS_CONST) { \
  74. zval *zv = CRT_CONSTANT_EX(op_array, opline, opline->opN, ssa->rt_constants); \
  75. if (Z_TYPE_P(zv) == IS_LONG) { \
  76. return Z_LVAL_P(zv); \
  77. } else if (Z_TYPE_P(zv) == IS_TRUE) { \
  78. return 1; \
  79. } else if (Z_TYPE_P(zv) == IS_FALSE) { \
  80. return 0; \
  81. } else if (Z_TYPE_P(zv) == IS_NULL) { \
  82. return 0; \
  83. } \
  84. } else if (opline->opN##_type != IS_UNUSED && \
  85. ssa->ops && \
  86. ssa->var_info && \
  87. ssa->ops[opline - op_array->opcodes].opN##_use >= 0 && \
  88. ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].has_range) { \
  89. return ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].range.max; \
  90. } \
  91. return ZEND_LONG_MAX; \
  92. }
  93. #define DEFINE_SSA_OP_RANGE_UNDERFLOW(opN) \
  94. static zend_always_inline char _ssa_##opN##_range_underflow(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  95. { \
  96. if (opline->opN##_type == IS_CONST) { \
  97. zval *zv = CRT_CONSTANT_EX(op_array, opline, opline->opN, ssa->rt_constants); \
  98. if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_TRUE || Z_TYPE_P(zv) == IS_FALSE || Z_TYPE_P(zv) == IS_NULL) { \
  99. return 0; \
  100. } \
  101. } else if (opline->opN##_type != IS_UNUSED && \
  102. ssa->ops && \
  103. ssa->var_info && \
  104. ssa->ops[opline - op_array->opcodes].opN##_use >= 0 && \
  105. ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].has_range) { \
  106. return ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].range.underflow; \
  107. } \
  108. return 1; \
  109. }
  110. #define DEFINE_SSA_OP_RANGE_OVERFLOW(opN) \
  111. static zend_always_inline char _ssa_##opN##_range_overflow(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  112. { \
  113. if (opline->opN##_type == IS_CONST) { \
  114. zval *zv = CRT_CONSTANT_EX(op_array, opline, opline->opN, ssa->rt_constants); \
  115. if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_TRUE || Z_TYPE_P(zv) == IS_FALSE || Z_TYPE_P(zv) == IS_NULL) { \
  116. return 0; \
  117. } \
  118. } else if (opline->opN##_type != IS_UNUSED && \
  119. ssa->ops && \
  120. ssa->var_info && \
  121. ssa->ops[opline - op_array->opcodes].opN##_use >= 0 && \
  122. ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].has_range) { \
  123. return ssa->var_info[ssa->ops[opline - op_array->opcodes].opN##_use].range.overflow; \
  124. } \
  125. return 1; \
  126. }
  127. DEFINE_SSA_OP_HAS_RANGE(op1)
  128. DEFINE_SSA_OP_MIN_RANGE(op1)
  129. DEFINE_SSA_OP_MAX_RANGE(op1)
  130. DEFINE_SSA_OP_RANGE_UNDERFLOW(op1)
  131. DEFINE_SSA_OP_RANGE_OVERFLOW(op1)
  132. DEFINE_SSA_OP_HAS_RANGE(op2)
  133. DEFINE_SSA_OP_MIN_RANGE(op2)
  134. DEFINE_SSA_OP_MAX_RANGE(op2)
  135. DEFINE_SSA_OP_RANGE_UNDERFLOW(op2)
  136. DEFINE_SSA_OP_RANGE_OVERFLOW(op2)
  137. #define OP1_HAS_RANGE() (_ssa_op1_has_range (op_array, ssa, opline))
  138. #define OP1_MIN_RANGE() (_ssa_op1_min_range (op_array, ssa, opline))
  139. #define OP1_MAX_RANGE() (_ssa_op1_max_range (op_array, ssa, opline))
  140. #define OP1_RANGE_UNDERFLOW() (_ssa_op1_range_underflow (op_array, ssa, opline))
  141. #define OP1_RANGE_OVERFLOW() (_ssa_op1_range_overflow (op_array, ssa, opline))
  142. #define OP2_HAS_RANGE() (_ssa_op2_has_range (op_array, ssa, opline))
  143. #define OP2_MIN_RANGE() (_ssa_op2_min_range (op_array, ssa, opline))
  144. #define OP2_MAX_RANGE() (_ssa_op2_max_range (op_array, ssa, opline))
  145. #define OP2_RANGE_UNDERFLOW() (_ssa_op2_range_underflow (op_array, ssa, opline))
  146. #define OP2_RANGE_OVERFLOW() (_ssa_op2_range_overflow (op_array, ssa, opline))
  147. static zend_always_inline uint32_t _const_op_type(const zval *zv) {
  148. if (Z_TYPE_P(zv) == IS_CONSTANT_AST) {
  149. return MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY;
  150. } else if (Z_TYPE_P(zv) == IS_ARRAY) {
  151. HashTable *ht = Z_ARRVAL_P(zv);
  152. uint32_t tmp = MAY_BE_ARRAY;
  153. zend_string *str;
  154. zval *val;
  155. if (Z_REFCOUNTED_P(zv)) {
  156. tmp |= MAY_BE_RC1 | MAY_BE_RCN;
  157. } else {
  158. tmp |= MAY_BE_RCN;
  159. }
  160. ZEND_HASH_FOREACH_STR_KEY_VAL(ht, str, val) {
  161. if (str) {
  162. tmp |= MAY_BE_ARRAY_KEY_STRING;
  163. } else {
  164. tmp |= MAY_BE_ARRAY_KEY_LONG;
  165. }
  166. tmp |= 1 << (Z_TYPE_P(val) + MAY_BE_ARRAY_SHIFT);
  167. } ZEND_HASH_FOREACH_END();
  168. return tmp;
  169. } else {
  170. uint32_t tmp = (1 << Z_TYPE_P(zv));
  171. if (Z_REFCOUNTED_P(zv)) {
  172. tmp |= MAY_BE_RC1 | MAY_BE_RCN;
  173. } else if (Z_TYPE_P(zv) == IS_STRING) {
  174. tmp |= MAY_BE_RCN;
  175. }
  176. return tmp;
  177. }
  178. }
  179. static zend_always_inline uint32_t get_ssa_var_info(const zend_ssa *ssa, int ssa_var_num)
  180. {
  181. if (ssa->var_info && ssa_var_num >= 0) {
  182. return ssa->var_info[ssa_var_num].type;
  183. } else {
  184. return MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ERROR;
  185. }
  186. }
  187. #define DEFINE_SSA_OP_INFO(opN) \
  188. static zend_always_inline uint32_t _ssa_##opN##_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  189. { \
  190. if (opline->opN##_type == IS_CONST) { \
  191. return _const_op_type(CRT_CONSTANT_EX(op_array, opline, opline->opN, ssa->rt_constants)); \
  192. } else { \
  193. return get_ssa_var_info(ssa, ssa->ops ? ssa->ops[opline - op_array->opcodes].opN##_use : -1); \
  194. } \
  195. }
  196. #define DEFINE_SSA_OP_DEF_INFO(opN) \
  197. static zend_always_inline uint32_t _ssa_##opN##_def_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline) \
  198. { \
  199. return get_ssa_var_info(ssa, ssa->ops ? ssa->ops[opline - op_array->opcodes].opN##_def : -1); \
  200. }
  201. DEFINE_SSA_OP_INFO(op1)
  202. DEFINE_SSA_OP_INFO(op2)
  203. DEFINE_SSA_OP_INFO(result)
  204. DEFINE_SSA_OP_DEF_INFO(op1)
  205. DEFINE_SSA_OP_DEF_INFO(op2)
  206. DEFINE_SSA_OP_DEF_INFO(result)
  207. #define OP1_INFO() (_ssa_op1_info(op_array, ssa, opline))
  208. #define OP2_INFO() (_ssa_op2_info(op_array, ssa, opline))
  209. #define OP1_DATA_INFO() (_ssa_op1_info(op_array, ssa, (opline+1)))
  210. #define OP2_DATA_INFO() (_ssa_op2_info(op_array, ssa, (opline+1)))
  211. #define RES_USE_INFO() (_ssa_result_info(op_array, ssa, opline))
  212. #define OP1_DEF_INFO() (_ssa_op1_def_info(op_array, ssa, opline))
  213. #define OP2_DEF_INFO() (_ssa_op2_def_info(op_array, ssa, opline))
  214. #define OP1_DATA_DEF_INFO() (_ssa_op1_def_info(op_array, ssa, (opline+1)))
  215. #define OP2_DATA_DEF_INFO() (_ssa_op2_def_info(op_array, ssa, (opline+1)))
  216. #define RES_INFO() (_ssa_result_def_info(op_array, ssa, opline))
  217. BEGIN_EXTERN_C()
  218. int zend_ssa_find_false_dependencies(const zend_op_array *op_array, zend_ssa *ssa);
  219. int zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa);
  220. int zend_ssa_inference(zend_arena **raena, const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level);
  221. uint32_t zend_array_element_type(uint32_t t1, int write, int insert);
  222. int zend_inference_calc_range(const zend_op_array *op_array, zend_ssa *ssa, int var, int widening, int narrowing, zend_ssa_range *tmp);
  223. void zend_inference_init_range(const zend_op_array *op_array, zend_ssa *ssa, int var, zend_bool underflow, zend_long min, zend_long max, zend_bool overflow);
  224. int zend_inference_narrowing_meet(zend_ssa_var_info *var_info, zend_ssa_range *r);
  225. int zend_inference_widening_meet(zend_ssa_var_info *var_info, zend_ssa_range *r);
  226. void zend_inference_check_recursive_dependencies(zend_op_array *op_array);
  227. int zend_infer_types_ex(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_bitset worklist, zend_long optimization_level);
  228. void zend_init_func_return_info(const zend_op_array *op_array,
  229. const zend_script *script,
  230. zend_ssa_var_info *ret);
  231. void zend_func_return_info(const zend_op_array *op_array,
  232. const zend_script *script,
  233. int recursive,
  234. int widening,
  235. zend_ssa_var_info *ret);
  236. int zend_may_throw(const zend_op *opline, zend_op_array *op_array, zend_ssa *ssa);
  237. END_EXTERN_C()
  238. #endif /* ZEND_INFERENCE_H */
  239. /*
  240. * Local variables:
  241. * tab-width: 4
  242. * c-basic-offset: 4
  243. * indent-tabs-mode: t
  244. * End:
  245. */