pass3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 3: (Jump optimization)
  22. * - optimize series of JMPs
  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. /* we use "jmp_hitlist" to avoid infinity loops during jmp optimization */
  31. static zend_always_inline int in_hitlist(zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
  32. {
  33. int i;
  34. for (i = 0; i < jmp_hitlist_count; i++) {
  35. if (jmp_hitlist[i] == target) {
  36. return 1;
  37. }
  38. }
  39. return 0;
  40. }
  41. #define CHECK_LOOP(target) \
  42. if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
  43. jmp_hitlist[jmp_hitlist_count++] = target; \
  44. } else { \
  45. break; \
  46. }
  47. void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
  48. {
  49. zend_op *opline;
  50. zend_op *end;
  51. zend_op *target;
  52. zend_op **jmp_hitlist;
  53. int jmp_hitlist_count;
  54. ALLOCA_FLAG(use_heap);
  55. jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
  56. opline = op_array->opcodes;
  57. end = opline + op_array->last;
  58. while (opline < end) {
  59. switch (opline->opcode) {
  60. case ZEND_JMP:
  61. jmp_hitlist_count = 0;
  62. target = ZEND_OP1_JMP_ADDR(opline);
  63. while (1) {
  64. if (target->opcode == ZEND_JMP) {
  65. /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
  66. target = ZEND_OP1_JMP_ADDR(target);
  67. CHECK_LOOP(target);
  68. } else if (target->opcode == ZEND_NOP) {
  69. target = target + 1;
  70. } else {
  71. break;
  72. }
  73. ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
  74. }
  75. if (target == opline + 1) {
  76. /* convert L: JMP L+1 to NOP */
  77. MAKE_NOP(opline);
  78. } else if (target->opcode == ZEND_JMPZNZ) {
  79. /* JMP L, L: JMPZNZ L1,L2 -> JMPZNZ L1,L2 */
  80. *opline = *target;
  81. if (opline->op1_type == IS_CONST) {
  82. zval zv;
  83. ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
  84. opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
  85. }
  86. /* Jump addresses may be encoded as offsets, recompute them. */
  87. ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(target));
  88. opline->extended_value = ZEND_OPLINE_TO_OFFSET(opline,
  89. ZEND_OFFSET_TO_OPLINE(target, target->extended_value));
  90. goto optimize_jmpznz;
  91. } else if ((target->opcode == ZEND_RETURN ||
  92. target->opcode == ZEND_RETURN_BY_REF ||
  93. target->opcode == ZEND_GENERATOR_RETURN ||
  94. target->opcode == ZEND_EXIT) &&
  95. !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
  96. /* JMP L, L: RETURN to immediate RETURN */
  97. *opline = *target;
  98. if (opline->op1_type == IS_CONST) {
  99. zval zv;
  100. ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
  101. opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
  102. }
  103. } else if (opline > op_array->opcodes &&
  104. ((opline-1)->opcode == ZEND_JMPZ ||
  105. (opline-1)->opcode == ZEND_JMPNZ)) {
  106. if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
  107. /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
  108. if ((opline-1)->op1_type == IS_CV) {
  109. (opline-1)->opcode = ZEND_CHECK_VAR;
  110. (opline-1)->op2.num = 0;
  111. } else if ((opline-1)->op1_type & (IS_TMP_VAR|IS_VAR)) {
  112. (opline-1)->opcode = ZEND_FREE;
  113. (opline-1)->op2.num = 0;
  114. } else {
  115. MAKE_NOP(opline-1);
  116. }
  117. } else {
  118. /* JMPZ(X,L1), JMP(L2) -> JMPZNZ(X,L1,L2) */
  119. if ((opline-1)->opcode == ZEND_JMPZ) {
  120. (opline-1)->extended_value = ZEND_OPLINE_TO_OFFSET((opline-1), target);
  121. } else {
  122. (opline-1)->extended_value = ZEND_OPLINE_TO_OFFSET((opline-1), ZEND_OP2_JMP_ADDR(opline-1));
  123. ZEND_SET_OP_JMP_ADDR((opline-1), (opline-1)->op2, target);
  124. }
  125. (opline-1)->opcode = ZEND_JMPZNZ;
  126. }
  127. }
  128. break;
  129. case ZEND_JMP_SET:
  130. case ZEND_COALESCE:
  131. jmp_hitlist_count = 0;
  132. target = ZEND_OP2_JMP_ADDR(opline);
  133. while (1) {
  134. if (target->opcode == ZEND_JMP) {
  135. target = ZEND_OP1_JMP_ADDR(target);
  136. CHECK_LOOP(target);
  137. } else if (target->opcode == ZEND_NOP) {
  138. target = target + 1;
  139. } else {
  140. break;
  141. }
  142. ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
  143. }
  144. break;
  145. case ZEND_JMPZ:
  146. case ZEND_JMPNZ:
  147. jmp_hitlist_count = 0;
  148. target = ZEND_OP2_JMP_ADDR(opline);
  149. while (1) {
  150. if (target->opcode == ZEND_JMP) {
  151. /* plain JMP */
  152. /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
  153. target = ZEND_OP1_JMP_ADDR(target);
  154. CHECK_LOOP(target);
  155. } else if (target->opcode == opline->opcode &&
  156. SAME_VAR(opline->op1, target->op1)) {
  157. /* same opcode and same var as this opcode */
  158. /* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
  159. target = ZEND_OP2_JMP_ADDR(target);
  160. CHECK_LOOP(target);
  161. } else if (target->opcode == INV_COND(opline->opcode) &&
  162. SAME_VAR(opline->op1, target->op1)) {
  163. /* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
  164. JMPZ(X,L1+1) */
  165. target = target + 1;
  166. } else if (target->opcode == ZEND_JMPZNZ &&
  167. SAME_VAR(opline->op1, target->op1)) {
  168. target = (opline->opcode == ZEND_JMPZ) ?
  169. ZEND_OP2_JMP_ADDR(target) :
  170. ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
  171. CHECK_LOOP(target);
  172. } else if (target->opcode == ZEND_NOP) {
  173. target = target + 1;
  174. } else {
  175. break;
  176. }
  177. ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
  178. }
  179. /* convert L: JMPZ L+1 to NOP */
  180. if (target == opline + 1) {
  181. if (opline->op1_type == IS_CV) {
  182. opline->opcode = ZEND_CHECK_VAR;
  183. opline->op2.num = 0;
  184. } else if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
  185. opline->opcode = ZEND_FREE;
  186. opline->op2.num = 0;
  187. } else {
  188. MAKE_NOP(opline);
  189. }
  190. }
  191. break;
  192. case ZEND_JMPZ_EX:
  193. case ZEND_JMPNZ_EX:
  194. jmp_hitlist_count = 0;
  195. target = ZEND_OP2_JMP_ADDR(opline);
  196. while (1) {
  197. if (target->opcode == ZEND_JMP) {
  198. /* plain JMP */
  199. /* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
  200. target = ZEND_OP1_JMP_ADDR(target);
  201. CHECK_LOOP(target);
  202. } else if (target->opcode == opline->opcode-3 &&
  203. (SAME_VAR(target->op1, opline->result) ||
  204. SAME_VAR(target->op1, opline->op1))) {
  205. /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
  206. JMPZ_EX(X,L2) */
  207. target = ZEND_OP2_JMP_ADDR(target);
  208. CHECK_LOOP(target);
  209. } else if (target->opcode == opline->opcode &&
  210. target->result.var == opline->result.var &&
  211. (SAME_VAR(target->op1, opline->result) ||
  212. SAME_VAR(target->op1, opline->op1))) {
  213. /* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
  214. JMPZ_EX(X,L2) */
  215. target = ZEND_OP2_JMP_ADDR(target);
  216. CHECK_LOOP(target);
  217. } else if (target->opcode == ZEND_JMPZNZ &&
  218. (SAME_VAR(target->op1, opline->result) ||
  219. SAME_VAR(target->op1, opline->op1))) {
  220. /* Check for JMPZNZ with same cond variable */
  221. target = (opline->opcode == ZEND_JMPZ_EX) ?
  222. ZEND_OP2_JMP_ADDR(target) :
  223. ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
  224. CHECK_LOOP(target);
  225. } else if (target->opcode == INV_EX_COND(opline->opcode) &&
  226. (SAME_VAR(target->op1, opline->result) ||
  227. SAME_VAR(target->op1, opline->op1))) {
  228. /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
  229. JMPZ_EX(X,L1+1) */
  230. target = target + 1;
  231. } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
  232. target->result.var == opline->result.var &&
  233. (SAME_VAR(target->op1, opline->result) ||
  234. SAME_VAR(target->op1, opline->op1))) {
  235. /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
  236. JMPZ_EX(X,L1+1) */
  237. target = target + 1;
  238. } else if (target->opcode == ZEND_BOOL &&
  239. (SAME_VAR(target->op1, opline->result) ||
  240. SAME_VAR(target->op1, opline->op1))) {
  241. /* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
  242. Z = JMPZ_EX(X,L1+1) */
  243. /* NOTE: This optimization pattern is not safe, but works, */
  244. /* because result of JMPZ_EX instruction */
  245. /* is not used on the following path and */
  246. /* should be used once on the branch path. */
  247. /* */
  248. /* The pattern works well only if jumps processed in */
  249. /* direct order, otherwise it breaks JMPZ_EX */
  250. /* sequences too early. */
  251. opline->result.var = target->result.var;
  252. target = target + 1;
  253. CHECK_LOOP(target);
  254. } else if (target->opcode == ZEND_NOP) {
  255. target = target + 1;
  256. } else {
  257. break;
  258. }
  259. ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
  260. }
  261. /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
  262. if (target == opline + 1) {
  263. opline->opcode = ZEND_BOOL;
  264. opline->op2.num = 0;
  265. }
  266. break;
  267. case ZEND_JMPZNZ:
  268. optimize_jmpznz:
  269. jmp_hitlist_count = 0;
  270. target = ZEND_OP2_JMP_ADDR(opline);
  271. while (1) {
  272. if (target->opcode == ZEND_JMP) {
  273. /* JMPZNZ(X,L1,L2), L1: JMP(L3) => JMPZNZ(X,L3,L2), L1: JMP(L3) */
  274. target = ZEND_OP1_JMP_ADDR(target);
  275. CHECK_LOOP(target);
  276. } else if ((target->opcode == ZEND_JMPZ || target->opcode == ZEND_JMPZNZ) &&
  277. SAME_VAR(target->op1, opline->op1)) {
  278. /* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
  279. target = ZEND_OP2_JMP_ADDR(target);
  280. CHECK_LOOP(target);
  281. } else if (target->opcode == ZEND_JMPNZ &&
  282. SAME_VAR(target->op1, opline->op1)) {
  283. /* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
  284. target = target + 1;
  285. } else if (target->opcode == ZEND_NOP) {
  286. target = target + 1;
  287. } else {
  288. break;
  289. }
  290. ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
  291. }
  292. jmp_hitlist_count = 0;
  293. target = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
  294. while (1) {
  295. if (target->opcode == ZEND_JMP) {
  296. /* JMPZNZ(X,L1,L2), L2: JMP(L3) => JMPZNZ(X,L1,L3), L2: JMP(L3) */
  297. target = ZEND_OP1_JMP_ADDR(target);
  298. CHECK_LOOP(target);
  299. } else if (target->opcode == ZEND_JMPNZ &&
  300. SAME_VAR(target->op1, opline->op1)) {
  301. /* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
  302. target = ZEND_OP2_JMP_ADDR(target);
  303. CHECK_LOOP(target);
  304. } else if (target->opcode == ZEND_JMPZ &&
  305. SAME_VAR(target->op1, opline->op1)) {
  306. /* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
  307. target = target + 1;
  308. } else if (target->opcode == ZEND_JMPZNZ &&
  309. SAME_VAR(target->op1, opline->op1)) {
  310. /* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
  311. target = ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
  312. CHECK_LOOP(target);
  313. } else if (target->opcode == ZEND_NOP) {
  314. target = target + 1;
  315. } else {
  316. break;
  317. }
  318. opline->extended_value = ZEND_OPLINE_TO_OFFSET(opline, target);
  319. }
  320. if (ZEND_OP2_JMP_ADDR(opline) == target &&
  321. !(opline->op1_type & (IS_VAR|IS_TMP_VAR))) {
  322. /* JMPZNZ(?,L,L) -> JMP(L) */
  323. opline->opcode = ZEND_JMP;
  324. ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
  325. SET_UNUSED(opline->op1);
  326. SET_UNUSED(opline->op2);
  327. opline->extended_value = 0;
  328. }
  329. /* Don't convert JMPZNZ back to JMPZ/JMPNZ, because the
  330. following JMP is not removed yet. */
  331. break;
  332. }
  333. opline++;
  334. }
  335. free_alloca(jmp_hitlist, use_heap);
  336. }