optimize_temp_vars_5.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  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: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Stanislav Malyshev <stas@zend.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "Optimizer/zend_optimizer.h"
  22. #include "Optimizer/zend_optimizer_internal.h"
  23. #include "zend_API.h"
  24. #include "zend_constants.h"
  25. #include "zend_execute.h"
  26. #include "zend_vm.h"
  27. #include "zend_bitset.h"
  28. #define GET_AVAILABLE_T() \
  29. for (i = 0; i < T; i++) { \
  30. if (!zend_bitset_in(taken_T, i)) { \
  31. break; \
  32. } \
  33. } \
  34. zend_bitset_incl(taken_T, i); \
  35. if (i > max) { \
  36. max = i; \
  37. }
  38. void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx)
  39. {
  40. int T = op_array->T;
  41. int offset = op_array->last_var;
  42. uint32_t bitset_len;
  43. zend_bitset taken_T; /* T index in use */
  44. zend_op **start_of_T; /* opline where T is first used */
  45. zend_bitset valid_T; /* Is the map_T valid */
  46. int *map_T; /* Map's the T to its new index */
  47. zend_op *opline, *end;
  48. int currT;
  49. int i;
  50. int max = -1;
  51. void *checkpoint = zend_arena_checkpoint(ctx->arena);
  52. bitset_len = zend_bitset_len(T);
  53. taken_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
  54. start_of_T = (zend_op **) zend_arena_alloc(&ctx->arena, T * sizeof(zend_op *));
  55. valid_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
  56. map_T = (int *) zend_arena_alloc(&ctx->arena, T * sizeof(int));
  57. end = op_array->opcodes;
  58. opline = &op_array->opcodes[op_array->last - 1];
  59. /* Find T definition points */
  60. while (opline >= end) {
  61. if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
  62. start_of_T[VAR_NUM(opline->result.var) - offset] = opline;
  63. }
  64. opline--;
  65. }
  66. zend_bitset_clear(valid_T, bitset_len);
  67. zend_bitset_clear(taken_T, bitset_len);
  68. end = op_array->opcodes;
  69. opline = &op_array->opcodes[op_array->last - 1];
  70. while (opline >= end) {
  71. if ((opline->op1_type & (IS_VAR | IS_TMP_VAR))) {
  72. currT = VAR_NUM(opline->op1.var) - offset;
  73. if (opline->opcode == ZEND_ROPE_END) {
  74. int num = (((opline->extended_value + 1) * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
  75. int var;
  76. var = max;
  77. while (var >= 0 && !zend_bitset_in(taken_T, var)) {
  78. var--;
  79. }
  80. max = MAX(max, var + num);
  81. var = var + 1;
  82. map_T[currT] = var;
  83. zend_bitset_incl(valid_T, currT);
  84. zend_bitset_incl(taken_T, var);
  85. opline->op1.var = NUM_VAR(var + offset);
  86. while (num > 1) {
  87. num--;
  88. zend_bitset_incl(taken_T, var + num);
  89. }
  90. } else {
  91. if (!zend_bitset_in(valid_T, currT)) {
  92. int use_new_var = 0;
  93. /* Code in "finally" blocks may modify temporary variables.
  94. * We allocate new temporaries for values that need to
  95. * relive FAST_CALLs.
  96. */
  97. if ((op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) &&
  98. (opline->opcode == ZEND_RETURN ||
  99. opline->opcode == ZEND_GENERATOR_RETURN ||
  100. opline->opcode == ZEND_RETURN_BY_REF ||
  101. opline->opcode == ZEND_FREE ||
  102. opline->opcode == ZEND_FE_FREE)) {
  103. zend_op *curr = opline;
  104. while (--curr >= end) {
  105. if (curr->opcode == ZEND_FAST_CALL) {
  106. use_new_var = 1;
  107. break;
  108. } else if (curr->opcode != ZEND_FREE &&
  109. curr->opcode != ZEND_FE_FREE &&
  110. curr->opcode != ZEND_VERIFY_RETURN_TYPE &&
  111. curr->opcode != ZEND_DISCARD_EXCEPTION) {
  112. break;
  113. }
  114. }
  115. }
  116. if (use_new_var) {
  117. i = ++max;
  118. zend_bitset_incl(taken_T, i);
  119. } else {
  120. GET_AVAILABLE_T();
  121. }
  122. map_T[currT] = i;
  123. zend_bitset_incl(valid_T, currT);
  124. }
  125. opline->op1.var = NUM_VAR(map_T[currT] + offset);
  126. }
  127. }
  128. if ((opline->op2_type & (IS_VAR | IS_TMP_VAR))) {
  129. currT = VAR_NUM(opline->op2.var) - offset;
  130. if (!zend_bitset_in(valid_T, currT)) {
  131. GET_AVAILABLE_T();
  132. map_T[currT] = i;
  133. zend_bitset_incl(valid_T, currT);
  134. }
  135. opline->op2.var = NUM_VAR(map_T[currT] + offset);
  136. }
  137. if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
  138. currT = VAR_NUM(opline->result.var) - offset;
  139. if (zend_bitset_in(valid_T, currT)) {
  140. if (start_of_T[currT] == opline) {
  141. /* ZEND_FAST_CALL can not share temporary var with others
  142. * since the fast_var could also be set by ZEND_HANDLE_EXCEPTION
  143. * which could be ahead of it */
  144. if (opline->opcode != ZEND_FAST_CALL) {
  145. zend_bitset_excl(taken_T, map_T[currT]);
  146. }
  147. }
  148. opline->result.var = NUM_VAR(map_T[currT] + offset);
  149. if (opline->opcode == ZEND_ROPE_INIT) {
  150. if (start_of_T[currT] == opline) {
  151. uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
  152. while (num > 1) {
  153. num--;
  154. zend_bitset_excl(taken_T, map_T[currT]+num);
  155. }
  156. }
  157. }
  158. } else {
  159. /* Code which gets here is using a wrongly built opcode such as RECV() */
  160. GET_AVAILABLE_T();
  161. map_T[currT] = i;
  162. zend_bitset_incl(valid_T, currT);
  163. opline->result.var = NUM_VAR(i + offset);
  164. }
  165. }
  166. opline--;
  167. }
  168. zend_arena_release(&ctx->arena, checkpoint);
  169. op_array->T = max + 1;
  170. }