nop_removal.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /* pass 10:
  22. * - remove NOPs
  23. */
  24. #include "Optimizer/zend_optimizer.h"
  25. #include "Optimizer/zend_optimizer_internal.h"
  26. #include "zend_API.h"
  27. #include "zend_constants.h"
  28. #include "zend_execute.h"
  29. #include "zend_vm.h"
  30. void zend_optimizer_nop_removal(zend_op_array *op_array, zend_optimizer_ctx *ctx)
  31. {
  32. zend_op *end, *opline;
  33. uint32_t new_count, i, shift;
  34. int j;
  35. uint32_t *shiftlist;
  36. ALLOCA_FLAG(use_heap);
  37. shiftlist = (uint32_t *)do_alloca(sizeof(uint32_t) * op_array->last, use_heap);
  38. i = new_count = shift = 0;
  39. end = op_array->opcodes + op_array->last;
  40. for (opline = op_array->opcodes; opline < end; opline++) {
  41. /* Kill JMP-over-NOP-s */
  42. if (opline->opcode == ZEND_JMP && ZEND_OP1_JMP_ADDR(opline) > op_array->opcodes + i) {
  43. /* check if there are only NOPs under the branch */
  44. zend_op *target = ZEND_OP1_JMP_ADDR(opline) - 1;
  45. while (target->opcode == ZEND_NOP) {
  46. target--;
  47. }
  48. if (target == opline) {
  49. /* only NOPs */
  50. opline->opcode = ZEND_NOP;
  51. }
  52. }
  53. shiftlist[i++] = shift;
  54. if (opline->opcode == ZEND_NOP) {
  55. shift++;
  56. } else {
  57. if (shift) {
  58. zend_op *new_opline = op_array->opcodes + new_count;
  59. *new_opline = *opline;
  60. zend_optimizer_migrate_jump(op_array, new_opline, opline);
  61. }
  62. new_count++;
  63. }
  64. }
  65. if (shift) {
  66. op_array->last = new_count;
  67. end = op_array->opcodes + op_array->last;
  68. /* update JMPs */
  69. for (opline = op_array->opcodes; opline<end; opline++) {
  70. zend_optimizer_shift_jump(op_array, opline, shiftlist);
  71. }
  72. /* update try/catch array */
  73. for (j = 0; j < op_array->last_try_catch; j++) {
  74. op_array->try_catch_array[j].try_op -= shiftlist[op_array->try_catch_array[j].try_op];
  75. op_array->try_catch_array[j].catch_op -= shiftlist[op_array->try_catch_array[j].catch_op];
  76. if (op_array->try_catch_array[j].finally_op) {
  77. op_array->try_catch_array[j].finally_op -= shiftlist[op_array->try_catch_array[j].finally_op];
  78. op_array->try_catch_array[j].finally_end -= shiftlist[op_array->try_catch_array[j].finally_end];
  79. }
  80. }
  81. /* update early binding list */
  82. if (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) {
  83. uint32_t *opline_num = &ctx->script->first_early_binding_opline;
  84. ZEND_ASSERT(op_array == &ctx->script->main_op_array);
  85. do {
  86. *opline_num -= shiftlist[*opline_num];
  87. opline_num = &op_array->opcodes[*opline_num].result.opline_num;
  88. } while (*opline_num != (uint32_t)-1);
  89. }
  90. }
  91. free_alloca(shiftlist, use_heap);
  92. }