dce.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, DCE - Dead Code Elimination |
  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. | Dmitry Stogov <dmitry@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "Optimizer/zend_optimizer_internal.h"
  20. #include "Optimizer/zend_inference.h"
  21. #include "Optimizer/zend_ssa.h"
  22. #include "Optimizer/zend_func_info.h"
  23. #include "Optimizer/zend_call_graph.h"
  24. #include "zend_bitset.h"
  25. /* This pass implements a form of dead code elimination (DCE). The algorithm optimistically assumes
  26. * that all instructions and phis are dead. Instructions with immediate side-effects are then marked
  27. * as live. We then recursively (using a worklist) propagate liveness to the instructions that def
  28. * the used operands.
  29. *
  30. * Notes:
  31. * * This pass does not perform unreachable code elimination. This happens as part of the SCCP
  32. * pass.
  33. * * The DCE is performed without taking control-dependence into account, i.e. all conditional
  34. * branches are assumed to be live. It's possible to take control-dependence into account using
  35. * the DCE algorithm described by Cytron et al., however it requires the construction of a
  36. * postdominator tree and of postdominance frontiers, which does not seem worthwhile at this
  37. * point.
  38. * * We separate intrinsic side-effects from potential side-effects in the form of notices thrown
  39. * by the instruction (in case we want to make this configurable). See may_have_side_effects() and
  40. * zend_may_throw().
  41. * * We often cannot DCE assignments and unsets while guaranteeing that dtors run in the same
  42. * order. There is an optimization option to allow reordering of dtor effects.
  43. * * The algorithm is able to eliminate dead modifications of non-escaping arrays
  44. * and objects as well as dead arrays and objects allocations.
  45. */
  46. typedef struct {
  47. zend_ssa *ssa;
  48. zend_op_array *op_array;
  49. zend_bitset instr_dead;
  50. zend_bitset phi_dead;
  51. zend_bitset instr_worklist;
  52. zend_bitset phi_worklist;
  53. zend_bitset phi_worklist_no_val;
  54. uint32_t instr_worklist_len;
  55. uint32_t phi_worklist_len;
  56. unsigned reorder_dtor_effects : 1;
  57. } context;
  58. static inline bool is_bad_mod(const zend_ssa *ssa, int use, int def) {
  59. if (def < 0) {
  60. /* This modification is not tracked by SSA, assume the worst */
  61. return 1;
  62. }
  63. if (ssa->var_info[use].type & MAY_BE_REF) {
  64. /* Modification of reference may have side-effect */
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. static inline bool may_have_side_effects(
  70. zend_op_array *op_array, zend_ssa *ssa,
  71. const zend_op *opline, const zend_ssa_op *ssa_op,
  72. bool reorder_dtor_effects) {
  73. switch (opline->opcode) {
  74. case ZEND_NOP:
  75. case ZEND_IS_IDENTICAL:
  76. case ZEND_IS_NOT_IDENTICAL:
  77. case ZEND_QM_ASSIGN:
  78. case ZEND_FREE:
  79. case ZEND_FE_FREE:
  80. case ZEND_TYPE_CHECK:
  81. case ZEND_DEFINED:
  82. case ZEND_ADD:
  83. case ZEND_SUB:
  84. case ZEND_MUL:
  85. case ZEND_POW:
  86. case ZEND_BW_OR:
  87. case ZEND_BW_AND:
  88. case ZEND_BW_XOR:
  89. case ZEND_CONCAT:
  90. case ZEND_FAST_CONCAT:
  91. case ZEND_DIV:
  92. case ZEND_MOD:
  93. case ZEND_BOOL_XOR:
  94. case ZEND_BOOL:
  95. case ZEND_BOOL_NOT:
  96. case ZEND_BW_NOT:
  97. case ZEND_SL:
  98. case ZEND_SR:
  99. case ZEND_IS_EQUAL:
  100. case ZEND_IS_NOT_EQUAL:
  101. case ZEND_IS_SMALLER:
  102. case ZEND_IS_SMALLER_OR_EQUAL:
  103. case ZEND_CASE:
  104. case ZEND_CASE_STRICT:
  105. case ZEND_CAST:
  106. case ZEND_ROPE_INIT:
  107. case ZEND_ROPE_ADD:
  108. case ZEND_INIT_ARRAY:
  109. case ZEND_SPACESHIP:
  110. case ZEND_STRLEN:
  111. case ZEND_COUNT:
  112. case ZEND_GET_TYPE:
  113. case ZEND_ISSET_ISEMPTY_THIS:
  114. case ZEND_ISSET_ISEMPTY_DIM_OBJ:
  115. case ZEND_FETCH_DIM_IS:
  116. case ZEND_ISSET_ISEMPTY_CV:
  117. case ZEND_ISSET_ISEMPTY_VAR:
  118. case ZEND_FETCH_IS:
  119. case ZEND_IN_ARRAY:
  120. case ZEND_FUNC_NUM_ARGS:
  121. case ZEND_FUNC_GET_ARGS:
  122. case ZEND_ARRAY_KEY_EXISTS:
  123. /* No side effects */
  124. return 0;
  125. case ZEND_ADD_ARRAY_ELEMENT:
  126. /* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */
  127. if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
  128. return 1;
  129. }
  130. return 0;
  131. case ZEND_ROPE_END:
  132. /* TODO: Rope dce optimization, see #76446 */
  133. return 1;
  134. case ZEND_JMP:
  135. case ZEND_JMPZ:
  136. case ZEND_JMPNZ:
  137. case ZEND_JMPZNZ:
  138. case ZEND_JMPZ_EX:
  139. case ZEND_JMPNZ_EX:
  140. case ZEND_JMP_SET:
  141. case ZEND_COALESCE:
  142. case ZEND_ASSERT_CHECK:
  143. case ZEND_JMP_NULL:
  144. /* For our purposes a jumps and branches are side effects. */
  145. return 1;
  146. case ZEND_BEGIN_SILENCE:
  147. case ZEND_END_SILENCE:
  148. case ZEND_ECHO:
  149. case ZEND_INCLUDE_OR_EVAL:
  150. case ZEND_THROW:
  151. case ZEND_MATCH_ERROR:
  152. case ZEND_EXT_STMT:
  153. case ZEND_EXT_FCALL_BEGIN:
  154. case ZEND_EXT_FCALL_END:
  155. case ZEND_TICKS:
  156. case ZEND_YIELD:
  157. case ZEND_YIELD_FROM:
  158. case ZEND_VERIFY_NEVER_TYPE:
  159. /* Intrinsic side effects */
  160. return 1;
  161. case ZEND_DO_FCALL:
  162. case ZEND_DO_FCALL_BY_NAME:
  163. case ZEND_DO_ICALL:
  164. case ZEND_DO_UCALL:
  165. /* For now assume all calls have side effects */
  166. return 1;
  167. case ZEND_RECV:
  168. case ZEND_RECV_INIT:
  169. /* Even though RECV_INIT can be side-effect free, these cannot be simply dropped
  170. * due to the prologue skipping code. */
  171. return 1;
  172. case ZEND_ASSIGN_REF:
  173. return 1;
  174. case ZEND_ASSIGN:
  175. {
  176. if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)) {
  177. return 1;
  178. }
  179. if (!reorder_dtor_effects) {
  180. if (opline->op2_type != IS_CONST
  181. && (OP2_INFO() & MAY_HAVE_DTOR)
  182. && ssa->vars[ssa_op->op2_use].escape_state != ESCAPE_STATE_NO_ESCAPE) {
  183. /* DCE might shorten lifetime */
  184. return 1;
  185. }
  186. }
  187. return 0;
  188. }
  189. case ZEND_UNSET_VAR:
  190. return 1;
  191. case ZEND_UNSET_CV:
  192. {
  193. uint32_t t1 = OP1_INFO();
  194. if (t1 & MAY_BE_REF) {
  195. /* We don't consider uses as the LHS of an assignment as real uses during DCE, so
  196. * an unset may be considered dead even if there is a later assignment to the
  197. * variable. Removing the unset in this case would not be correct if the variable
  198. * is a reference, because unset breaks references. */
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. case ZEND_PRE_INC:
  204. case ZEND_POST_INC:
  205. case ZEND_PRE_DEC:
  206. case ZEND_POST_DEC:
  207. return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def);
  208. case ZEND_ASSIGN_OP:
  209. return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
  210. || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE;
  211. case ZEND_ASSIGN_DIM:
  212. case ZEND_ASSIGN_OBJ:
  213. if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
  214. || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
  215. return 1;
  216. }
  217. if (!reorder_dtor_effects) {
  218. opline++;
  219. ssa_op++;
  220. if (opline->op1_type != IS_CONST
  221. && (OP1_INFO() & MAY_HAVE_DTOR)) {
  222. /* DCE might shorten lifetime */
  223. return 1;
  224. }
  225. }
  226. return 0;
  227. case ZEND_PRE_INC_OBJ:
  228. case ZEND_PRE_DEC_OBJ:
  229. case ZEND_POST_INC_OBJ:
  230. case ZEND_POST_DEC_OBJ:
  231. if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
  232. || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
  233. return 1;
  234. }
  235. return 0;
  236. case ZEND_BIND_STATIC:
  237. if (op_array->static_variables) {
  238. /* Implicit and Explicit bind static is effectively prologue of closure so
  239. report it has side effects like RECV, RECV_INIT; This allows us to
  240. reflect on the closure and discover used variable at runtime */
  241. if ((opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) {
  242. return 1;
  243. }
  244. if ((opline->extended_value & ZEND_BIND_REF) != 0) {
  245. zval *value =
  246. (zval*)((char*)op_array->static_variables->arData +
  247. (opline->extended_value & ~ZEND_BIND_REF));
  248. if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
  249. /* AST may contain undefined constants */
  250. return 1;
  251. }
  252. }
  253. }
  254. return 0;
  255. case ZEND_CHECK_VAR:
  256. return (OP1_INFO() & MAY_BE_UNDEF) != 0;
  257. case ZEND_FE_RESET_R:
  258. case ZEND_FE_RESET_RW:
  259. /* Model as not having side-effects -- let the side-effect be introduced by
  260. * FE_FETCH if the array is not known to be non-empty. */
  261. return (OP1_INFO() & MAY_BE_ANY) != MAY_BE_ARRAY;
  262. default:
  263. /* For everything we didn't handle, assume a side-effect */
  264. return 1;
  265. }
  266. }
  267. static zend_always_inline void add_to_worklists(context *ctx, int var_num, int check) {
  268. zend_ssa_var *var = &ctx->ssa->vars[var_num];
  269. if (var->definition >= 0) {
  270. if (!check || zend_bitset_in(ctx->instr_dead, var->definition)) {
  271. zend_bitset_incl(ctx->instr_worklist, var->definition);
  272. }
  273. } else if (var->definition_phi) {
  274. if (!check || zend_bitset_in(ctx->phi_dead, var_num)) {
  275. zend_bitset_incl(ctx->phi_worklist, var_num);
  276. }
  277. }
  278. }
  279. static inline void add_to_phi_worklist_no_val(context *ctx, int var_num) {
  280. zend_ssa_var *var = &ctx->ssa->vars[var_num];
  281. if (var->definition_phi && zend_bitset_in(ctx->phi_dead, var_num)) {
  282. zend_bitset_incl(ctx->phi_worklist_no_val, var_num);
  283. }
  284. }
  285. static zend_always_inline void add_operands_to_worklists(context *ctx, zend_op *opline, zend_ssa_op *ssa_op, zend_ssa *ssa, int check) {
  286. if (ssa_op->result_use >= 0) {
  287. add_to_worklists(ctx, ssa_op->result_use, check);
  288. }
  289. if (ssa_op->op1_use >= 0) {
  290. if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use)
  291. || (opline->opcode == ZEND_ASSIGN
  292. && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF) != 0)) {
  293. add_to_worklists(ctx, ssa_op->op1_use, check);
  294. } else {
  295. add_to_phi_worklist_no_val(ctx, ssa_op->op1_use);
  296. }
  297. }
  298. if (ssa_op->op2_use >= 0) {
  299. if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op2_use)
  300. || (opline->opcode == ZEND_FE_FETCH_R
  301. && (ssa->var_info[ssa_op->op2_use].type & MAY_BE_REF) != 0)) {
  302. add_to_worklists(ctx, ssa_op->op2_use, check);
  303. } else {
  304. add_to_phi_worklist_no_val(ctx, ssa_op->op2_use);
  305. }
  306. }
  307. }
  308. static zend_always_inline void add_phi_sources_to_worklists(context *ctx, zend_ssa_phi *phi, int check) {
  309. zend_ssa *ssa = ctx->ssa;
  310. int source;
  311. FOREACH_PHI_SOURCE(phi, source) {
  312. add_to_worklists(ctx, source, check);
  313. } FOREACH_PHI_SOURCE_END();
  314. }
  315. static inline bool is_var_dead(context *ctx, int var_num) {
  316. zend_ssa_var *var = &ctx->ssa->vars[var_num];
  317. if (var->definition_phi) {
  318. return zend_bitset_in(ctx->phi_dead, var_num);
  319. } else if (var->definition >= 0) {
  320. return zend_bitset_in(ctx->instr_dead, var->definition);
  321. } else {
  322. /* Variable has no definition, so either the definition has already been removed (var is
  323. * dead) or this is one of the implicit variables at the start of the function (for our
  324. * purposes live) */
  325. return var_num >= ctx->op_array->last_var;
  326. }
  327. }
  328. // Sometimes we can mark the var as EXT_UNUSED
  329. static bool try_remove_var_def(context *ctx, int free_var, int use_chain, zend_op *opline) {
  330. if (use_chain >= 0) {
  331. return 0;
  332. }
  333. zend_ssa_var *var = &ctx->ssa->vars[free_var];
  334. int def = var->definition;
  335. if (def >= 0) {
  336. zend_ssa_op *def_op = &ctx->ssa->ops[def];
  337. if (def_op->result_def == free_var
  338. && var->phi_use_chain == NULL
  339. && var->use_chain == (opline - ctx->op_array->opcodes)) {
  340. zend_op *def_opline = &ctx->op_array->opcodes[def];
  341. switch (def_opline->opcode) {
  342. case ZEND_ASSIGN:
  343. case ZEND_ASSIGN_REF:
  344. case ZEND_ASSIGN_DIM:
  345. case ZEND_ASSIGN_OBJ:
  346. case ZEND_ASSIGN_OBJ_REF:
  347. case ZEND_ASSIGN_STATIC_PROP:
  348. case ZEND_ASSIGN_STATIC_PROP_REF:
  349. case ZEND_ASSIGN_OP:
  350. case ZEND_ASSIGN_DIM_OP:
  351. case ZEND_ASSIGN_OBJ_OP:
  352. case ZEND_ASSIGN_STATIC_PROP_OP:
  353. case ZEND_PRE_INC:
  354. case ZEND_PRE_DEC:
  355. case ZEND_PRE_INC_OBJ:
  356. case ZEND_PRE_DEC_OBJ:
  357. case ZEND_DO_ICALL:
  358. case ZEND_DO_UCALL:
  359. case ZEND_DO_FCALL_BY_NAME:
  360. case ZEND_DO_FCALL:
  361. case ZEND_INCLUDE_OR_EVAL:
  362. case ZEND_YIELD:
  363. case ZEND_YIELD_FROM:
  364. case ZEND_ASSERT_CHECK:
  365. def_opline->result_type = IS_UNUSED;
  366. def_opline->result.var = 0;
  367. def_op->result_def = -1;
  368. var->definition = -1;
  369. return 1;
  370. default:
  371. break;
  372. }
  373. }
  374. }
  375. return 0;
  376. }
  377. static zend_always_inline bool may_be_refcounted(uint32_t type) {
  378. return (type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) != 0;
  379. }
  380. static inline bool is_free_of_live_var(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
  381. switch (opline->opcode) {
  382. case ZEND_FREE:
  383. /* It is always safe to remove FREEs of non-refcounted values, even if they are live. */
  384. if ((ctx->ssa->var_info[ssa_op->op1_use].type & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) != 0
  385. && !may_be_refcounted(ctx->ssa->var_info[ssa_op->op1_use].type)) {
  386. return 0;
  387. }
  388. ZEND_FALLTHROUGH;
  389. case ZEND_FE_FREE:
  390. return !is_var_dead(ctx, ssa_op->op1_use);
  391. default:
  392. return 0;
  393. }
  394. }
  395. /* Returns whether the instruction has been DCEd */
  396. static bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
  397. zend_ssa *ssa = ctx->ssa;
  398. int free_var = -1;
  399. zend_uchar free_var_type;
  400. if (opline->opcode == ZEND_NOP) {
  401. return 0;
  402. }
  403. /* We mark FREEs as dead, but they're only really dead if the destroyed var is dead */
  404. if (is_free_of_live_var(ctx, opline, ssa_op)) {
  405. return 0;
  406. }
  407. if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))&& !is_var_dead(ctx, ssa_op->op1_use)) {
  408. if (!try_remove_var_def(ctx, ssa_op->op1_use, ssa_op->op1_use_chain, opline)) {
  409. if (may_be_refcounted(ssa->var_info[ssa_op->op1_use].type)
  410. && opline->opcode != ZEND_CASE && opline->opcode != ZEND_CASE_STRICT) {
  411. free_var = ssa_op->op1_use;
  412. free_var_type = opline->op1_type;
  413. }
  414. }
  415. }
  416. if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op2_use)) {
  417. if (!try_remove_var_def(ctx, ssa_op->op2_use, ssa_op->op2_use_chain, opline)) {
  418. if (may_be_refcounted(ssa->var_info[ssa_op->op2_use].type)) {
  419. if (free_var >= 0) {
  420. // TODO: We can't free two vars. Keep instruction alive.
  421. zend_bitset_excl(ctx->instr_dead, opline - ctx->op_array->opcodes);
  422. return 0;
  423. }
  424. free_var = ssa_op->op2_use;
  425. free_var_type = opline->op2_type;
  426. }
  427. }
  428. }
  429. zend_ssa_rename_defs_of_instr(ctx->ssa, ssa_op);
  430. zend_ssa_remove_instr(ctx->ssa, opline, ssa_op);
  431. if (free_var >= 0) {
  432. opline->opcode = ZEND_FREE;
  433. opline->op1.var = EX_NUM_TO_VAR(ssa->vars[free_var].var);
  434. opline->op1_type = free_var_type;
  435. ssa_op->op1_use = free_var;
  436. ssa_op->op1_use_chain = ssa->vars[free_var].use_chain;
  437. ssa->vars[free_var].use_chain = ssa_op - ssa->ops;
  438. return 0;
  439. }
  440. return 1;
  441. }
  442. static inline int get_common_phi_source(zend_ssa *ssa, zend_ssa_phi *phi) {
  443. int common_source = -1;
  444. int source;
  445. FOREACH_PHI_SOURCE(phi, source) {
  446. if (source == phi->ssa_var) {
  447. continue;
  448. }
  449. if (common_source == -1) {
  450. common_source = source;
  451. } else if (common_source != source) {
  452. return -1;
  453. }
  454. } FOREACH_PHI_SOURCE_END();
  455. /* If all sources are phi->ssa_var this phi must be in an unreachable cycle.
  456. * We can't easily drop the phi in that case, as we don't have something to replace it with.
  457. * Ideally SCCP would eliminate the whole cycle. */
  458. return common_source;
  459. }
  460. static void try_remove_trivial_phi(context *ctx, zend_ssa_phi *phi) {
  461. zend_ssa *ssa = ctx->ssa;
  462. if (phi->pi < 0) {
  463. /* Phi assignment with identical source operands */
  464. int common_source = get_common_phi_source(ssa, phi);
  465. if (common_source >= 0) {
  466. zend_ssa_rename_var_uses(ssa, phi->ssa_var, common_source, 1);
  467. zend_ssa_remove_phi(ssa, phi);
  468. }
  469. } else {
  470. /* Pi assignment that is only used in Phi/Pi assignments */
  471. // TODO What if we want to rerun type inference after DCE? Maybe separate this?
  472. /*ZEND_ASSERT(phi->sources[0] != -1);
  473. if (ssa->vars[phi->ssa_var].use_chain < 0) {
  474. zend_ssa_rename_var_uses_keep_types(ssa, phi->ssa_var, phi->sources[0], 1);
  475. zend_ssa_remove_phi(ssa, phi);
  476. }*/
  477. }
  478. }
  479. static inline bool may_break_varargs(const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) {
  480. if (ssa_op->op1_def >= 0
  481. && ssa->vars[ssa_op->op1_def].var < op_array->num_args) {
  482. return 1;
  483. }
  484. if (ssa_op->op2_def >= 0
  485. && ssa->vars[ssa_op->op2_def].var < op_array->num_args) {
  486. return 1;
  487. }
  488. if (ssa_op->result_def >= 0
  489. && ssa->vars[ssa_op->result_def].var < op_array->num_args) {
  490. return 1;
  491. }
  492. return 0;
  493. }
  494. static inline bool may_throw_dce_exception(const zend_op *opline) {
  495. return opline->opcode == ZEND_ADD_ARRAY_ELEMENT && opline->op2_type == IS_UNUSED;
  496. }
  497. int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, bool reorder_dtor_effects) {
  498. int i;
  499. zend_ssa_phi *phi;
  500. int removed_ops = 0;
  501. /* DCE of CV operations that changes arguments may affect vararg functions. */
  502. bool has_varargs = (ssa->cfg.flags & ZEND_FUNC_VARARG) != 0;
  503. context ctx;
  504. ctx.ssa = ssa;
  505. ctx.op_array = op_array;
  506. ctx.reorder_dtor_effects = reorder_dtor_effects;
  507. /* We have no dedicated phi vector, so we use the whole ssa var vector instead */
  508. ctx.instr_worklist_len = zend_bitset_len(op_array->last);
  509. ctx.instr_worklist = alloca(sizeof(zend_ulong) * ctx.instr_worklist_len);
  510. memset(ctx.instr_worklist, 0, sizeof(zend_ulong) * ctx.instr_worklist_len);
  511. ctx.phi_worklist_len = zend_bitset_len(ssa->vars_count);
  512. ctx.phi_worklist = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
  513. memset(ctx.phi_worklist, 0, sizeof(zend_ulong) * ctx.phi_worklist_len);
  514. ctx.phi_worklist_no_val = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
  515. memset(ctx.phi_worklist_no_val, 0, sizeof(zend_ulong) * ctx.phi_worklist_len);
  516. /* Optimistically assume all instructions and phis to be dead */
  517. ctx.instr_dead = alloca(sizeof(zend_ulong) * ctx.instr_worklist_len);
  518. memset(ctx.instr_dead, 0, sizeof(zend_ulong) * ctx.instr_worklist_len);
  519. ctx.phi_dead = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
  520. memset(ctx.phi_dead, 0xff, sizeof(zend_ulong) * ctx.phi_worklist_len);
  521. /* Mark non-CV phis as live. Even if the result is unused, we generally cannot remove one
  522. * of the producing instructions, as it combines producing the result with control flow.
  523. * This can be made more precise if there are any cases where this is not the case. */
  524. FOREACH_PHI(phi) {
  525. if (phi->var >= op_array->last_var
  526. && may_be_refcounted(ssa->var_info[phi->ssa_var].type)) {
  527. zend_bitset_excl(ctx.phi_dead, phi->ssa_var);
  528. add_phi_sources_to_worklists(&ctx, phi, 0);
  529. }
  530. } FOREACH_PHI_END();
  531. /* Mark reachable instruction without side effects as dead */
  532. int b = ssa->cfg.blocks_count;
  533. while (b > 0) {
  534. int op_data = -1;
  535. b--;
  536. zend_basic_block *block = &ssa->cfg.blocks[b];
  537. if (!(block->flags & ZEND_BB_REACHABLE)) {
  538. continue;
  539. }
  540. i = block->start + block->len;
  541. while (i > block->start) {
  542. i--;
  543. if (op_array->opcodes[i].opcode == ZEND_OP_DATA) {
  544. op_data = i;
  545. continue;
  546. }
  547. if (zend_bitset_in(ctx.instr_worklist, i)) {
  548. zend_bitset_excl(ctx.instr_worklist, i);
  549. add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0);
  550. if (op_data >= 0) {
  551. add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0);
  552. }
  553. } else if (may_have_side_effects(op_array, ssa, &op_array->opcodes[i], &ssa->ops[i], ctx.reorder_dtor_effects)
  554. || (zend_may_throw(&op_array->opcodes[i], &ssa->ops[i], op_array, ssa)
  555. && !may_throw_dce_exception(&op_array->opcodes[i]))
  556. || (has_varargs && may_break_varargs(op_array, ssa, &ssa->ops[i]))) {
  557. if (op_array->opcodes[i].opcode == ZEND_NEW
  558. && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL
  559. && ssa->ops[i].result_def >= 0
  560. && ssa->vars[ssa->ops[i].result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
  561. zend_bitset_incl(ctx.instr_dead, i);
  562. zend_bitset_incl(ctx.instr_dead, i+1);
  563. } else {
  564. add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0);
  565. if (op_data >= 0) {
  566. add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0);
  567. }
  568. }
  569. } else {
  570. zend_bitset_incl(ctx.instr_dead, i);
  571. if (op_data >= 0) {
  572. zend_bitset_incl(ctx.instr_dead, op_data);
  573. }
  574. }
  575. op_data = -1;
  576. }
  577. }
  578. /* Propagate liveness backwards to all definitions of used vars */
  579. while (!zend_bitset_empty(ctx.instr_worklist, ctx.instr_worklist_len)
  580. || !zend_bitset_empty(ctx.phi_worklist, ctx.phi_worklist_len)) {
  581. while ((i = zend_bitset_pop_first(ctx.instr_worklist, ctx.instr_worklist_len)) >= 0) {
  582. zend_bitset_excl(ctx.instr_dead, i);
  583. add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 1);
  584. if (i < op_array->last
  585. && (op_array->opcodes[i+1].opcode == ZEND_OP_DATA
  586. || (op_array->opcodes[i].opcode == ZEND_NEW
  587. && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL))) {
  588. zend_bitset_excl(ctx.instr_dead, i+1);
  589. add_operands_to_worklists(&ctx, &op_array->opcodes[i+1], &ssa->ops[i+1], ssa, 1);
  590. }
  591. }
  592. while ((i = zend_bitset_pop_first(ctx.phi_worklist, ctx.phi_worklist_len)) >= 0) {
  593. zend_bitset_excl(ctx.phi_dead, i);
  594. zend_bitset_excl(ctx.phi_worklist_no_val, i);
  595. add_phi_sources_to_worklists(&ctx, ssa->vars[i].definition_phi, 1);
  596. }
  597. }
  598. /* Eliminate dead instructions */
  599. ZEND_BITSET_FOREACH(ctx.instr_dead, ctx.instr_worklist_len, i) {
  600. removed_ops += dce_instr(&ctx, &op_array->opcodes[i], &ssa->ops[i]);
  601. } ZEND_BITSET_FOREACH_END();
  602. /* Improper uses don't count as "uses" for the purpose of instruction elimination,
  603. * but we have to retain phis defining them.
  604. * Propagate this information backwards, marking any phi with an improperly used
  605. * target as non-dead. */
  606. while ((i = zend_bitset_pop_first(ctx.phi_worklist_no_val, ctx.phi_worklist_len)) >= 0) {
  607. zend_ssa_phi *phi = ssa->vars[i].definition_phi;
  608. int source;
  609. zend_bitset_excl(ctx.phi_dead, i);
  610. FOREACH_PHI_SOURCE(phi, source) {
  611. add_to_phi_worklist_no_val(&ctx, source);
  612. } FOREACH_PHI_SOURCE_END();
  613. }
  614. /* Now collect the actually dead phis */
  615. FOREACH_PHI(phi) {
  616. if (zend_bitset_in(ctx.phi_dead, phi->ssa_var)) {
  617. zend_ssa_remove_uses_of_var(ssa, phi->ssa_var);
  618. zend_ssa_remove_phi(ssa, phi);
  619. } else {
  620. /* Remove trivial phis (phis with identical source operands) */
  621. try_remove_trivial_phi(&ctx, phi);
  622. }
  623. } FOREACH_PHI_END();
  624. return removed_ops;
  625. }