scdf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, Sparse Conditional Data Flow Propagation Framework |
  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: Nikita Popov <nikic@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "Optimizer/zend_optimizer_internal.h"
  19. #include "Optimizer/scdf.h"
  20. /* This defines a generic framework for sparse conditional dataflow propagation. The algorithm is
  21. * based on "Sparse conditional constant propagation" by Wegman and Zadeck. We're using a
  22. * generalized implementation as described in chapter 8.3 of the SSA book.
  23. *
  24. * Every SSA variable is associated with an element on a finite-height lattice, those value can only
  25. * ever be lowered during the operation of the algorithm. If a value is lowered all instructions and
  26. * phis using that value need to be reconsidered (this is done by adding the variable to a
  27. * worklist). For phi functions the result is computed by applying the meet operation to the
  28. * operands. This continues until a fixed point is reached.
  29. *
  30. * The algorithm is control-flow sensitive: All blocks except the start block are initially assumed
  31. * to be unreachable. When considering a branch instruction, we determine the feasible successors
  32. * based on the current state of the variable lattice. If a new edge becomes feasible we either have
  33. * to mark the successor block executable and consider all instructions in it, or, if the target is
  34. * already executable, we only have to reconsider the phi functions (as we only consider phi
  35. * operands which are associated with a feasible edge).
  36. *
  37. * The generic framework requires the definition of three functions:
  38. * * visit_instr() should recompute the lattice values of all SSA variables defined by an
  39. * instruction.
  40. * * visit_phi() should recompute the lattice value of the SSA variable defined by the phi. While
  41. * doing this it should only consider operands for which scfg_is_edge_feasible() returns true.
  42. * * get_feasible_successors() should determine the feasible successors for a branch instruction.
  43. * Note that this callback only needs to handle conditional branches (with two successors).
  44. */
  45. #if 0
  46. #define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
  47. #else
  48. #define DEBUG_PRINT(...)
  49. #endif
  50. void scdf_mark_edge_feasible(scdf_ctx *scdf, int from, int to) {
  51. uint32_t edge = scdf_edge(&scdf->ssa->cfg, from, to);
  52. if (zend_bitset_in(scdf->feasible_edges, edge)) {
  53. /* We already handled this edge */
  54. return;
  55. }
  56. DEBUG_PRINT("Marking edge %d->%d feasible\n", from, to);
  57. zend_bitset_incl(scdf->feasible_edges, edge);
  58. if (!zend_bitset_in(scdf->executable_blocks, to)) {
  59. if (!zend_bitset_in(scdf->block_worklist, to)) {
  60. DEBUG_PRINT("Adding block %d to worklist\n", to);
  61. }
  62. zend_bitset_incl(scdf->block_worklist, to);
  63. } else {
  64. /* Block is already executable, only a new edge became feasible.
  65. * Reevaluate phi nodes to account for changed source operands. */
  66. zend_ssa_block *ssa_block = &scdf->ssa->blocks[to];
  67. zend_ssa_phi *phi;
  68. for (phi = ssa_block->phis; phi; phi = phi->next) {
  69. zend_bitset_excl(scdf->phi_var_worklist, phi->ssa_var);
  70. scdf->handlers.visit_phi(scdf, phi);
  71. }
  72. }
  73. }
  74. void scdf_init(zend_optimizer_ctx *ctx, scdf_ctx *scdf, zend_op_array *op_array, zend_ssa *ssa) {
  75. scdf->op_array = op_array;
  76. scdf->ssa = ssa;
  77. scdf->instr_worklist_len = zend_bitset_len(op_array->last);
  78. scdf->phi_var_worklist_len = zend_bitset_len(ssa->vars_count);
  79. scdf->block_worklist_len = zend_bitset_len(ssa->cfg.blocks_count);
  80. scdf->instr_worklist = zend_arena_calloc(&ctx->arena,
  81. scdf->instr_worklist_len + scdf->phi_var_worklist_len + 2 * scdf->block_worklist_len + zend_bitset_len(ssa->cfg.edges_count),
  82. sizeof(zend_ulong));
  83. scdf->phi_var_worklist = scdf->instr_worklist + scdf->instr_worklist_len;
  84. scdf->block_worklist = scdf->phi_var_worklist + scdf->phi_var_worklist_len;
  85. scdf->executable_blocks = scdf->block_worklist + scdf->block_worklist_len;
  86. scdf->feasible_edges = scdf->executable_blocks + scdf->block_worklist_len;
  87. zend_bitset_incl(scdf->block_worklist, 0);
  88. zend_bitset_incl(scdf->executable_blocks, 0);
  89. }
  90. void scdf_solve(scdf_ctx *scdf, const char *name) {
  91. zend_ssa *ssa = scdf->ssa;
  92. DEBUG_PRINT("Start SCDF solve (%s)\n", name);
  93. while (!zend_bitset_empty(scdf->instr_worklist, scdf->instr_worklist_len)
  94. || !zend_bitset_empty(scdf->phi_var_worklist, scdf->phi_var_worklist_len)
  95. || !zend_bitset_empty(scdf->block_worklist, scdf->block_worklist_len)
  96. ) {
  97. int i;
  98. while ((i = zend_bitset_pop_first(scdf->phi_var_worklist, scdf->phi_var_worklist_len)) >= 0) {
  99. zend_ssa_phi *phi = ssa->vars[i].definition_phi;
  100. ZEND_ASSERT(phi);
  101. if (zend_bitset_in(scdf->executable_blocks, phi->block)) {
  102. scdf->handlers.visit_phi(scdf, phi);
  103. }
  104. }
  105. while ((i = zend_bitset_pop_first(scdf->instr_worklist, scdf->instr_worklist_len)) >= 0) {
  106. int block_num = ssa->cfg.map[i];
  107. if (zend_bitset_in(scdf->executable_blocks, block_num)) {
  108. zend_basic_block *block = &ssa->cfg.blocks[block_num];
  109. zend_op *opline = &scdf->op_array->opcodes[i];
  110. zend_ssa_op *ssa_op = &ssa->ops[i];
  111. if (opline->opcode == ZEND_OP_DATA) {
  112. opline--;
  113. ssa_op--;
  114. }
  115. scdf->handlers.visit_instr(scdf, opline, ssa_op);
  116. if (i == block->start + block->len - 1) {
  117. if (block->successors_count == 1) {
  118. scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
  119. } else if (block->successors_count > 1) {
  120. scdf->handlers.mark_feasible_successors(scdf, block_num, block, opline, ssa_op);
  121. }
  122. }
  123. }
  124. }
  125. while ((i = zend_bitset_pop_first(scdf->block_worklist, scdf->block_worklist_len)) >= 0) {
  126. /* This block is now live. Interpret phis and instructions in it. */
  127. zend_basic_block *block = &ssa->cfg.blocks[i];
  128. zend_ssa_block *ssa_block = &ssa->blocks[i];
  129. DEBUG_PRINT("Pop block %d from worklist\n", i);
  130. zend_bitset_incl(scdf->executable_blocks, i);
  131. {
  132. zend_ssa_phi *phi;
  133. for (phi = ssa_block->phis; phi; phi = phi->next) {
  134. zend_bitset_excl(scdf->phi_var_worklist, phi->ssa_var);
  135. scdf->handlers.visit_phi(scdf, phi);
  136. }
  137. }
  138. if (block->len == 0) {
  139. /* Zero length blocks don't have a last instruction that would normally do this */
  140. scdf_mark_edge_feasible(scdf, i, block->successors[0]);
  141. } else {
  142. zend_op *opline = NULL;
  143. int j, end = block->start + block->len;
  144. for (j = block->start; j < end; j++) {
  145. opline = &scdf->op_array->opcodes[j];
  146. zend_bitset_excl(scdf->instr_worklist, j);
  147. if (opline->opcode != ZEND_OP_DATA) {
  148. scdf->handlers.visit_instr(scdf, opline, &ssa->ops[j]);
  149. }
  150. }
  151. if (block->successors_count == 1) {
  152. scdf_mark_edge_feasible(scdf, i, block->successors[0]);
  153. } else if (block->successors_count > 1) {
  154. ZEND_ASSERT(opline && "Should have opline in non-empty block");
  155. if (opline->opcode == ZEND_OP_DATA) {
  156. opline--;
  157. j--;
  158. }
  159. scdf->handlers.mark_feasible_successors(scdf, i, block, opline, &ssa->ops[j-1]);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. /* If a live range starts in a reachable block and ends in an unreachable block, we should
  166. * not eliminate the latter. While it cannot be reached, the FREE opcode of the loop var
  167. * is necessary for the correctness of temporary compaction. */
  168. static bool is_live_loop_var_free(
  169. scdf_ctx *scdf, const zend_op *opline, const zend_ssa_op *ssa_op) {
  170. if (!zend_optimizer_is_loop_var_free(opline)) {
  171. return false;
  172. }
  173. int var = ssa_op->op1_use;
  174. if (var < 0) {
  175. return false;
  176. }
  177. zend_ssa_var *ssa_var = &scdf->ssa->vars[var];
  178. uint32_t def_block;
  179. if (ssa_var->definition >= 0) {
  180. def_block = scdf->ssa->cfg.map[ssa_var->definition];
  181. } else {
  182. def_block = ssa_var->definition_phi->block;
  183. }
  184. return zend_bitset_in(scdf->executable_blocks, def_block);
  185. }
  186. static bool kept_alive_by_loop_var_free(scdf_ctx *scdf, const zend_basic_block *block) {
  187. const zend_op_array *op_array = scdf->op_array;
  188. const zend_cfg *cfg = &scdf->ssa->cfg;
  189. if (!(cfg->flags & ZEND_FUNC_FREE_LOOP_VAR)) {
  190. return false;
  191. }
  192. for (uint32_t i = block->start; i < block->start + block->len; i++) {
  193. if (is_live_loop_var_free(scdf, &op_array->opcodes[i], &scdf->ssa->ops[i])) {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. static uint32_t cleanup_loop_var_free_block(scdf_ctx *scdf, zend_basic_block *block) {
  200. zend_ssa *ssa = scdf->ssa;
  201. const zend_op_array *op_array = scdf->op_array;
  202. const zend_cfg *cfg = &ssa->cfg;
  203. int block_num = block - cfg->blocks;
  204. uint32_t removed_ops = 0;
  205. /* Removes phi nodes */
  206. for (zend_ssa_phi *phi = ssa->blocks[block_num].phis; phi; phi = phi->next) {
  207. zend_ssa_remove_uses_of_var(ssa, phi->ssa_var);
  208. zend_ssa_remove_phi(ssa, phi);
  209. }
  210. for (uint32_t i = block->start; i < block->start + block->len; i++) {
  211. zend_op *opline = &op_array->opcodes[i];
  212. zend_ssa_op *ssa_op = &scdf->ssa->ops[i];
  213. if (opline->opcode == ZEND_NOP
  214. || is_live_loop_var_free(scdf, opline, ssa_op)) {
  215. continue;
  216. }
  217. /* While we have to preserve the loop var free, we can still remove other instructions
  218. * in the block. */
  219. zend_ssa_remove_defs_of_instr(ssa, ssa_op);
  220. zend_ssa_remove_instr(ssa, opline, ssa_op);
  221. removed_ops++;
  222. }
  223. zend_ssa_remove_block_from_cfg(ssa, block_num);
  224. return removed_ops;
  225. }
  226. /* Removes unreachable blocks. This will remove both the instructions (and phis) in the
  227. * blocks, as well as remove them from the successor / predecessor lists and mark them
  228. * unreachable. Blocks already marked unreachable are not removed. */
  229. int scdf_remove_unreachable_blocks(scdf_ctx *scdf) {
  230. zend_ssa *ssa = scdf->ssa;
  231. int i;
  232. int removed_ops = 0;
  233. for (i = 0; i < ssa->cfg.blocks_count; i++) {
  234. zend_basic_block *block = &ssa->cfg.blocks[i];
  235. if (!zend_bitset_in(scdf->executable_blocks, i) && (block->flags & ZEND_BB_REACHABLE)) {
  236. if (!kept_alive_by_loop_var_free(scdf, block)) {
  237. removed_ops += block->len;
  238. zend_ssa_remove_block(scdf->op_array, ssa, i);
  239. } else {
  240. removed_ops += cleanup_loop_var_free_block(scdf, block);
  241. }
  242. }
  243. }
  244. return removed_ops;
  245. }