zend_ssa.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, SSA - Static Single Assignment Form |
  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 ZEND_SSA_H
  19. #define ZEND_SSA_H
  20. #include "zend_optimizer.h"
  21. #include "zend_cfg.h"
  22. typedef struct _zend_ssa_range {
  23. zend_long min;
  24. zend_long max;
  25. bool underflow;
  26. bool overflow;
  27. } zend_ssa_range;
  28. typedef enum _zend_ssa_negative_lat {
  29. NEG_NONE = 0,
  30. NEG_INIT = 1,
  31. NEG_INVARIANT = 2,
  32. NEG_USE_LT = 3,
  33. NEG_USE_GT = 4,
  34. NEG_UNKNOWN = 5
  35. } zend_ssa_negative_lat;
  36. /* Special kind of SSA Phi function used in eSSA */
  37. typedef struct _zend_ssa_range_constraint {
  38. zend_ssa_range range; /* simple range constraint */
  39. int min_var;
  40. int max_var;
  41. int min_ssa_var; /* ((min_var>0) ? MIN(ssa_var) : 0) + range.min */
  42. int max_ssa_var; /* ((max_var>0) ? MAX(ssa_var) : 0) + range.max */
  43. zend_ssa_negative_lat negative;
  44. } zend_ssa_range_constraint;
  45. typedef struct _zend_ssa_type_constraint {
  46. uint32_t type_mask; /* Type mask to intersect with */
  47. zend_class_entry *ce; /* Class entry for instanceof constraints */
  48. } zend_ssa_type_constraint;
  49. typedef union _zend_ssa_pi_constraint {
  50. zend_ssa_range_constraint range;
  51. zend_ssa_type_constraint type;
  52. } zend_ssa_pi_constraint;
  53. /* SSA Phi - ssa_var = Phi(source0, source1, ...sourceN) */
  54. typedef struct _zend_ssa_phi zend_ssa_phi;
  55. struct _zend_ssa_phi {
  56. zend_ssa_phi *next; /* next Phi in the same BB */
  57. int pi; /* if >= 0 this is actually a e-SSA Pi */
  58. zend_ssa_pi_constraint constraint; /* e-SSA Pi constraint */
  59. int var; /* Original CV, VAR or TMP variable index */
  60. int ssa_var; /* SSA variable index */
  61. int block; /* current BB index */
  62. int visited : 1; /* flag to avoid recursive processing */
  63. int has_range_constraint : 1;
  64. zend_ssa_phi **use_chains;
  65. zend_ssa_phi *sym_use_chain;
  66. int *sources; /* Array of SSA IDs that produce this var.
  67. As many as this block has
  68. predecessors. */
  69. };
  70. typedef struct _zend_ssa_block {
  71. zend_ssa_phi *phis;
  72. } zend_ssa_block;
  73. typedef struct _zend_ssa_op {
  74. int op1_use;
  75. int op2_use;
  76. int result_use;
  77. int op1_def;
  78. int op2_def;
  79. int result_def;
  80. int op1_use_chain;
  81. int op2_use_chain;
  82. int res_use_chain;
  83. } zend_ssa_op;
  84. typedef enum _zend_ssa_alias_kind {
  85. NO_ALIAS,
  86. SYMTABLE_ALIAS,
  87. HTTP_RESPONSE_HEADER_ALIAS
  88. } zend_ssa_alias_kind;
  89. typedef enum _zend_ssa_escape_state {
  90. ESCAPE_STATE_UNKNOWN,
  91. ESCAPE_STATE_NO_ESCAPE,
  92. ESCAPE_STATE_FUNCTION_ESCAPE,
  93. ESCAPE_STATE_GLOBAL_ESCAPE
  94. } zend_ssa_escape_state;
  95. typedef struct _zend_ssa_var {
  96. int var; /* original var number; op.var for CVs and following numbers for VARs and TMP_VARs */
  97. int scc; /* strongly connected component */
  98. int definition; /* opcode that defines this value */
  99. zend_ssa_phi *definition_phi; /* phi that defines this value */
  100. int use_chain; /* uses of this value, linked through opN_use_chain */
  101. zend_ssa_phi *phi_use_chain; /* uses of this value in Phi, linked through use_chain */
  102. zend_ssa_phi *sym_use_chain; /* uses of this value in Pi constraints */
  103. unsigned int no_val : 1; /* value doesn't matter (used as op1 in ZEND_ASSIGN) */
  104. unsigned int scc_entry : 1;
  105. unsigned int alias : 2; /* value may be changed indirectly */
  106. unsigned int escape_state : 2;
  107. } zend_ssa_var;
  108. typedef struct _zend_ssa_var_info {
  109. uint32_t type; /* inferred type (see zend_inference.h) */
  110. zend_ssa_range range;
  111. zend_class_entry *ce;
  112. unsigned int has_range : 1;
  113. unsigned int is_instanceof : 1; /* 0 - class == "ce", 1 - may be child of "ce" */
  114. unsigned int recursive : 1;
  115. unsigned int use_as_double : 1;
  116. unsigned int delayed_fetch_this : 1;
  117. unsigned int avoid_refcounting : 1;
  118. unsigned int guarded_reference : 1;
  119. unsigned int indirect_reference : 1; /* IS_INDIRECT returned by FETCH_DIM_W/FETCH_OBJ_W */
  120. } zend_ssa_var_info;
  121. typedef struct _zend_ssa {
  122. zend_cfg cfg; /* control flow graph */
  123. int vars_count; /* number of SSA variables */
  124. int sccs; /* number of SCCs */
  125. zend_ssa_block *blocks; /* array of SSA blocks */
  126. zend_ssa_op *ops; /* array of SSA instructions */
  127. zend_ssa_var *vars; /* use/def chain of SSA variables */
  128. zend_ssa_var_info *var_info;
  129. } zend_ssa;
  130. BEGIN_EXTERN_C()
  131. ZEND_API int zend_build_ssa(zend_arena **arena, const zend_script *script, const zend_op_array *op_array, uint32_t build_flags, zend_ssa *ssa);
  132. ZEND_API int zend_ssa_compute_use_def_chains(zend_arena **arena, const zend_op_array *op_array, zend_ssa *ssa);
  133. ZEND_API int zend_ssa_rename_op(const zend_op_array *op_array, const zend_op *opline, uint32_t k, uint32_t build_flags, int ssa_vars_count, zend_ssa_op *ssa_ops, int *var);
  134. int zend_ssa_unlink_use_chain(zend_ssa *ssa, int op, int var);
  135. void zend_ssa_remove_predecessor(zend_ssa *ssa, int from, int to);
  136. void zend_ssa_remove_defs_of_instr(zend_ssa *ssa, zend_ssa_op *ssa_op);
  137. void zend_ssa_remove_instr(zend_ssa *ssa, zend_op *opline, zend_ssa_op *ssa_op);
  138. void zend_ssa_remove_phi(zend_ssa *ssa, zend_ssa_phi *phi);
  139. void zend_ssa_remove_uses_of_var(zend_ssa *ssa, int var_num);
  140. void zend_ssa_remove_block(zend_op_array *op_array, zend_ssa *ssa, int b);
  141. void zend_ssa_rename_var_uses(zend_ssa *ssa, int old_var, int new_var, bool update_types);
  142. void zend_ssa_remove_block_from_cfg(zend_ssa *ssa, int b);
  143. static zend_always_inline void _zend_ssa_remove_def(zend_ssa_var *var)
  144. {
  145. ZEND_ASSERT(var->definition >= 0);
  146. ZEND_ASSERT(var->use_chain < 0);
  147. ZEND_ASSERT(!var->phi_use_chain);
  148. var->definition = -1;
  149. }
  150. static zend_always_inline void zend_ssa_remove_result_def(zend_ssa *ssa, zend_ssa_op *ssa_op)
  151. {
  152. zend_ssa_var *var = &ssa->vars[ssa_op->result_def];
  153. _zend_ssa_remove_def(var);
  154. ssa_op->result_def = -1;
  155. }
  156. static zend_always_inline void zend_ssa_remove_op1_def(zend_ssa *ssa, zend_ssa_op *ssa_op)
  157. {
  158. zend_ssa_var *var = &ssa->vars[ssa_op->op1_def];
  159. _zend_ssa_remove_def(var);
  160. ssa_op->op1_def = -1;
  161. }
  162. static zend_always_inline void zend_ssa_remove_op2_def(zend_ssa *ssa, zend_ssa_op *ssa_op)
  163. {
  164. zend_ssa_var *var = &ssa->vars[ssa_op->op2_def];
  165. _zend_ssa_remove_def(var);
  166. ssa_op->op2_def = -1;
  167. }
  168. END_EXTERN_C()
  169. static zend_always_inline int zend_ssa_next_use(const zend_ssa_op *ssa_op, int var, int use)
  170. {
  171. ssa_op += use;
  172. if (ssa_op->op1_use == var) {
  173. return ssa_op->op1_use_chain;
  174. } else if (ssa_op->op2_use == var) {
  175. return ssa_op->op2_use_chain;
  176. } else {
  177. return ssa_op->res_use_chain;
  178. }
  179. }
  180. static zend_always_inline zend_ssa_phi* zend_ssa_next_use_phi(const zend_ssa *ssa, int var, const zend_ssa_phi *p)
  181. {
  182. if (p->pi >= 0) {
  183. return p->use_chains[0];
  184. } else {
  185. int j;
  186. for (j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
  187. if (p->sources[j] == var) {
  188. return p->use_chains[j];
  189. }
  190. }
  191. }
  192. return NULL;
  193. }
  194. static zend_always_inline bool zend_ssa_is_no_val_use(const zend_op *opline, const zend_ssa_op *ssa_op, int var)
  195. {
  196. if (opline->opcode == ZEND_ASSIGN
  197. || opline->opcode == ZEND_UNSET_CV
  198. || opline->opcode == ZEND_BIND_GLOBAL
  199. || opline->opcode == ZEND_BIND_STATIC) {
  200. return ssa_op->op1_use == var && ssa_op->op2_use != var;
  201. }
  202. if (opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW) {
  203. return ssa_op->op2_use == var && ssa_op->op1_use != var;
  204. }
  205. if (ssa_op->result_use == var
  206. && opline->opcode != ZEND_ADD_ARRAY_ELEMENT
  207. && opline->opcode != ZEND_ADD_ARRAY_UNPACK) {
  208. return ssa_op->op1_use != var && ssa_op->op2_use != var;
  209. }
  210. return 0;
  211. }
  212. static zend_always_inline void zend_ssa_rename_defs_of_instr(zend_ssa *ssa, zend_ssa_op *ssa_op) {
  213. /* Rename def to use if possible. Mark variable as not defined otherwise. */
  214. if (ssa_op->op1_def >= 0) {
  215. if (ssa_op->op1_use >= 0) {
  216. zend_ssa_rename_var_uses(ssa, ssa_op->op1_def, ssa_op->op1_use, 1);
  217. }
  218. ssa->vars[ssa_op->op1_def].definition = -1;
  219. ssa_op->op1_def = -1;
  220. }
  221. if (ssa_op->op2_def >= 0) {
  222. if (ssa_op->op2_use >= 0) {
  223. zend_ssa_rename_var_uses(ssa, ssa_op->op2_def, ssa_op->op2_use, 1);
  224. }
  225. ssa->vars[ssa_op->op2_def].definition = -1;
  226. ssa_op->op2_def = -1;
  227. }
  228. if (ssa_op->result_def >= 0) {
  229. if (ssa_op->result_use >= 0) {
  230. zend_ssa_rename_var_uses(ssa, ssa_op->result_def, ssa_op->result_use, 1);
  231. }
  232. ssa->vars[ssa_op->result_def].definition = -1;
  233. ssa_op->result_def = -1;
  234. }
  235. }
  236. #define NUM_PHI_SOURCES(phi) \
  237. ((phi)->pi >= 0 ? 1 : (ssa->cfg.blocks[(phi)->block].predecessors_count))
  238. /* FOREACH_USE and FOREACH_PHI_USE explicitly support "continue"
  239. * and changing the use chain of the current element */
  240. #define FOREACH_USE(var, use) do { \
  241. int _var_num = (var) - ssa->vars, next; \
  242. for (use = (var)->use_chain; use >= 0; use = next) { \
  243. next = zend_ssa_next_use(ssa->ops, _var_num, use);
  244. #define FOREACH_USE_END() \
  245. } \
  246. } while (0)
  247. #define FOREACH_PHI_USE(var, phi) do { \
  248. int _var_num = (var) - ssa->vars; \
  249. zend_ssa_phi *next_phi; \
  250. for (phi = (var)->phi_use_chain; phi; phi = next_phi) { \
  251. next_phi = zend_ssa_next_use_phi(ssa, _var_num, phi);
  252. #define FOREACH_PHI_USE_END() \
  253. } \
  254. } while (0)
  255. #define FOREACH_PHI_SOURCE(phi, source) do { \
  256. zend_ssa_phi *_phi = (phi); \
  257. int _i, _end = NUM_PHI_SOURCES(phi); \
  258. for (_i = 0; _i < _end; _i++) { \
  259. ZEND_ASSERT(_phi->sources[_i] >= 0); \
  260. source = _phi->sources[_i];
  261. #define FOREACH_PHI_SOURCE_END() \
  262. } \
  263. } while (0)
  264. #define FOREACH_PHI(phi) do { \
  265. int _i; \
  266. for (_i = 0; _i < ssa->cfg.blocks_count; _i++) { \
  267. phi = ssa->blocks[_i].phis; \
  268. for (; phi; phi = phi->next) {
  269. #define FOREACH_PHI_END() \
  270. } \
  271. } \
  272. } while (0)
  273. #define FOREACH_BLOCK(block) do { \
  274. int _i; \
  275. for (_i = 0; _i < ssa->cfg.blocks_count; _i++) { \
  276. (block) = &ssa->cfg.blocks[_i]; \
  277. if (!((block)->flags & ZEND_BB_REACHABLE)) { \
  278. continue; \
  279. }
  280. #define FOREACH_BLOCK_END() \
  281. } \
  282. } while (0)
  283. /* Does not support "break" */
  284. #define FOREACH_INSTR_NUM(i) do { \
  285. zend_basic_block *_block; \
  286. FOREACH_BLOCK(_block) { \
  287. uint32_t _end = _block->start + _block->len; \
  288. for ((i) = _block->start; (i) < _end; (i)++) {
  289. #define FOREACH_INSTR_NUM_END() \
  290. } \
  291. } FOREACH_BLOCK_END(); \
  292. } while (0)
  293. #endif /* ZEND_SSA_H */