nop_removal.c 4.0 KB

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