compact_vars.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, Removing unused variables |
  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 "zend_bitset.h"
  20. /* This pass removes all CVs and temporaries that are completely unused. It does *not* merge any CVs or TMPs.
  21. * This pass does not operate on SSA form anymore. */
  22. void zend_optimizer_compact_vars(zend_op_array *op_array) {
  23. int i;
  24. ALLOCA_FLAG(use_heap1);
  25. ALLOCA_FLAG(use_heap2);
  26. uint32_t used_vars_len = zend_bitset_len(op_array->last_var + op_array->T);
  27. zend_bitset used_vars = ZEND_BITSET_ALLOCA(used_vars_len, use_heap1);
  28. uint32_t *vars_map = do_alloca((op_array->last_var + op_array->T) * sizeof(uint32_t), use_heap2);
  29. uint32_t num_cvs, num_tmps;
  30. /* Determine which CVs are used */
  31. zend_bitset_clear(used_vars, used_vars_len);
  32. for (i = 0; i < op_array->last; i++) {
  33. zend_op *opline = &op_array->opcodes[i];
  34. if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
  35. zend_bitset_incl(used_vars, VAR_NUM(opline->op1.var));
  36. }
  37. if (opline->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
  38. zend_bitset_incl(used_vars, VAR_NUM(opline->op2.var));
  39. }
  40. if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
  41. zend_bitset_incl(used_vars, VAR_NUM(opline->result.var));
  42. if (opline->opcode == ZEND_ROPE_INIT) {
  43. uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
  44. while (num > 1) {
  45. num--;
  46. zend_bitset_incl(used_vars, VAR_NUM(opline->result.var) + num);
  47. }
  48. }
  49. }
  50. }
  51. num_cvs = 0;
  52. for (i = 0; i < op_array->last_var; i++) {
  53. if (zend_bitset_in(used_vars, i)) {
  54. vars_map[i] = num_cvs++;
  55. } else {
  56. vars_map[i] = (uint32_t) -1;
  57. }
  58. }
  59. num_tmps = 0;
  60. for (i = op_array->last_var; i < op_array->last_var + op_array->T; i++) {
  61. if (zend_bitset_in(used_vars, i)) {
  62. vars_map[i] = num_cvs + num_tmps++;
  63. } else {
  64. vars_map[i] = (uint32_t) -1;
  65. }
  66. }
  67. free_alloca(used_vars, use_heap1);
  68. if (num_cvs == op_array->last_var && num_tmps == op_array->T) {
  69. free_alloca(vars_map, use_heap2);
  70. return;
  71. }
  72. ZEND_ASSERT(num_cvs <= op_array->last_var);
  73. ZEND_ASSERT(num_tmps <= op_array->T);
  74. /* Update CV and TMP references in opcodes */
  75. for (i = 0; i < op_array->last; i++) {
  76. zend_op *opline = &op_array->opcodes[i];
  77. if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
  78. opline->op1.var = NUM_VAR(vars_map[VAR_NUM(opline->op1.var)]);
  79. }
  80. if (opline->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
  81. opline->op2.var = NUM_VAR(vars_map[VAR_NUM(opline->op2.var)]);
  82. }
  83. if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
  84. opline->result.var = NUM_VAR(vars_map[VAR_NUM(opline->result.var)]);
  85. }
  86. }
  87. /* Update CV name table */
  88. if (num_cvs != op_array->last_var) {
  89. if (num_cvs) {
  90. zend_string **names = safe_emalloc(sizeof(zend_string *), num_cvs, 0);
  91. for (i = 0; i < op_array->last_var; i++) {
  92. if (vars_map[i] != (uint32_t) -1) {
  93. names[vars_map[i]] = op_array->vars[i];
  94. } else {
  95. zend_string_release_ex(op_array->vars[i], 0);
  96. }
  97. }
  98. efree(op_array->vars);
  99. op_array->vars = names;
  100. } else {
  101. for (i = 0; i < op_array->last_var; i++) {
  102. zend_string_release_ex(op_array->vars[i], 0);
  103. }
  104. efree(op_array->vars);
  105. op_array->vars = NULL;
  106. }
  107. op_array->last_var = num_cvs;
  108. }
  109. op_array->T = num_tmps;
  110. free_alloca(vars_map, use_heap2);
  111. }