Coverage Report

Created: 2025-09-27 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/jit/zend_jit_trace.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend JIT                                                             |
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: Dmitry Stogov <dmitry@php.net>                              |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "zend_jit.h"
20
#include "zend_jit_internal.h"
21
#include "zend_shared_alloc.h"
22
#include "ir/ir.h"
23
#include "zend_vm_opcodes.h"
24
25
static zend_jit_trace_info *zend_jit_traces = NULL;
26
static const void **zend_jit_exit_groups = NULL;
27
28
0
#define ZEND_JIT_COUNTER_NUM   zend_jit_traces[0].root
29
0
#define ZEND_JIT_TRACE_NUM     zend_jit_traces[0].id
30
0
#define ZEND_JIT_EXIT_NUM      zend_jit_traces[0].exit_count
31
0
#define ZEND_JIT_EXIT_COUNTERS zend_jit_traces[0].exit_counters
32
33
#define ZEND_JIT_TRACE_STOP_DESCRIPTION(name, description) \
34
  description,
35
36
static const char * zend_jit_trace_stop_description[] = {
37
  ZEND_JIT_TRACE_STOP(ZEND_JIT_TRACE_STOP_DESCRIPTION)
38
};
39
40
static zend_always_inline const char *zend_jit_trace_star_desc(uint8_t trace_flags)
41
0
{
42
0
  if (trace_flags & ZEND_JIT_TRACE_START_LOOP) {
43
0
    return "loop";
44
0
  } else if (trace_flags & ZEND_JIT_TRACE_START_ENTER) {
45
0
    return "enter";
46
0
  } else if (trace_flags & ZEND_JIT_TRACE_START_RETURN) {
47
0
    return "return";
48
0
  } else {
49
0
    ZEND_UNREACHABLE();
50
0
    return "???";
51
0
  }
52
0
}
53
54
static void zend_jit_trace_startup(bool reattached)
55
0
{
56
0
  if (!reattached) {
57
0
    zend_jit_traces = (zend_jit_trace_info*)zend_shared_alloc(sizeof(zend_jit_trace_info) * JIT_G(max_root_traces));
58
0
    if (!zend_jit_traces) {
59
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT root traces buffer!");
60
0
    }
61
0
    zend_jit_exit_groups = (const void**)zend_shared_alloc(sizeof(void*) * (ZEND_JIT_TRACE_MAX_EXITS/ZEND_JIT_EXIT_POINTS_PER_GROUP));
62
0
    if (!zend_jit_exit_groups) {
63
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT exit groups buffer!");
64
0
    }
65
0
    ZEND_JIT_TRACE_NUM = 1;
66
0
    ZEND_JIT_COUNTER_NUM = 0;
67
0
    ZEND_JIT_EXIT_NUM = 0;
68
0
    ZEND_JIT_EXIT_COUNTERS = 0;
69
0
    ZCSG(jit_traces) = zend_jit_traces;
70
0
    ZCSG(jit_exit_groups) = zend_jit_exit_groups;
71
0
    ZCSG(jit_counters_stopped) = false;
72
0
  } else {
73
0
    zend_jit_traces = ZCSG(jit_traces);
74
0
    if (!zend_jit_traces) {
75
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not obtain JIT traces buffer!");
76
0
    }
77
0
    zend_jit_exit_groups = ZCSG(jit_exit_groups);
78
0
    if (!zend_jit_exit_groups) {
79
0
      zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not obtain JIT exit groups buffer!");
80
0
    }
81
0
  }
82
83
0
  JIT_G(exit_counters) = calloc(JIT_G(max_exit_counters), 1);
84
0
  if (JIT_G(exit_counters) == NULL) {
85
0
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT exit counters buffer!");
86
0
  }
87
0
}
88
89
static const void *zend_jit_trace_allocate_exit_point(uint32_t n)
90
0
{
91
0
  const void *group = NULL;
92
93
0
  if (UNEXPECTED(n >= ZEND_JIT_TRACE_MAX_EXITS)) {
94
0
    return NULL;
95
0
  }
96
0
  do {
97
0
    group = zend_jit_trace_allocate_exit_group(ZEND_JIT_EXIT_NUM);
98
0
    if (!group) {
99
0
      return NULL;
100
0
    }
101
0
    zend_jit_exit_groups[ZEND_JIT_EXIT_NUM / ZEND_JIT_EXIT_POINTS_PER_GROUP] =
102
0
      group;
103
0
    ZEND_JIT_EXIT_NUM += ZEND_JIT_EXIT_POINTS_PER_GROUP;
104
0
  } while (n >= ZEND_JIT_EXIT_NUM);
105
0
  return (const void*)
106
0
    ((const char*)group +
107
0
    ((n % ZEND_JIT_EXIT_POINTS_PER_GROUP) * ZEND_JIT_EXIT_POINTS_SPACING));
108
0
}
109
110
static const void *zend_jit_trace_get_exit_addr(uint32_t n)
111
0
{
112
0
  if (UNEXPECTED(n >= ZEND_JIT_EXIT_NUM)) {
113
0
    return zend_jit_trace_allocate_exit_point(n);
114
0
  }
115
0
  return (const void*)
116
0
    ((const char*)zend_jit_exit_groups[n / ZEND_JIT_EXIT_POINTS_PER_GROUP] +
117
0
    ((n % ZEND_JIT_EXIT_POINTS_PER_GROUP) * ZEND_JIT_EXIT_POINTS_SPACING));
118
0
}
119
120
static uint32_t zend_jit_exit_point_by_addr(const void *addr)
121
0
{
122
0
  uint32_t n = (ZEND_JIT_EXIT_NUM + (ZEND_JIT_EXIT_POINTS_PER_GROUP - 1)) / ZEND_JIT_EXIT_POINTS_PER_GROUP;
123
0
  uint32_t i;
124
125
0
  for (i = 0; i < n; i++) {
126
0
    if ((char*)addr >= (char*)zend_jit_exit_groups[i]
127
0
     && (char*)addr <= (char*)zend_jit_exit_groups[i] + ((ZEND_JIT_EXIT_POINTS_PER_GROUP - 1) * ZEND_JIT_EXIT_POINTS_SPACING)) {
128
0
      return (i * ZEND_JIT_EXIT_POINTS_PER_GROUP) +
129
0
        (((char*)addr - (char*)zend_jit_exit_groups[i]) / ZEND_JIT_EXIT_POINTS_SPACING);
130
0
    }
131
0
  }
132
0
  return (uint32_t)-1;
133
0
}
134
135
static uint32_t _zend_jit_trace_get_exit_point(const zend_op *to_opline, uint32_t flags ZEND_FILE_LINE_DC)
136
0
{
137
0
  zend_jit_trace_info *t = &zend_jit_traces[ZEND_JIT_TRACE_NUM];
138
0
  uint32_t exit_point;
139
0
  const zend_op_array *op_array;
140
0
  uint32_t stack_offset = (uint32_t)-1;
141
0
  uint32_t stack_size;
142
0
  zend_jit_trace_stack *stack = NULL;
143
144
0
  if (delayed_call_chain) {
145
0
    assert(to_opline != NULL); /* CALL and IP share the same register */
146
0
    flags |= ZEND_JIT_EXIT_RESTORE_CALL;
147
0
  }
148
0
  if (JIT_G(current_frame)) {
149
0
    op_array = &JIT_G(current_frame)->func->op_array;
150
0
    stack_size = op_array->last_var + op_array->T;
151
0
    if (stack_size) {
152
0
      stack = JIT_G(current_frame)->stack;
153
0
      do {
154
0
        if (STACK_TYPE(stack, stack_size-1) != IS_UNKNOWN
155
0
         || STACK_MEM_TYPE(stack, stack_size-1) != IS_UNKNOWN
156
0
         || STACK_REF(stack, stack_size-1) != IR_UNUSED
157
0
        ) {
158
0
          break;
159
0
        }
160
0
        stack_size--;
161
0
      } while (stack_size);
162
0
    }
163
0
  } else {
164
0
    op_array = NULL;
165
0
    stack_size = 0;
166
0
  }
167
168
  /* Try to reuse exit points */
169
0
  if (to_opline != NULL
170
0
   && !(flags & ZEND_JIT_EXIT_METHOD_CALL)
171
0
   && t->exit_count > 0) {
172
0
    uint32_t i = t->exit_count;
173
174
0
    do {
175
0
      i--;
176
0
      if (stack_size == 0
177
0
       || (t->exit_info[i].stack_size >= stack_size
178
0
        && memcmp(t->stack_map + t->exit_info[i].stack_offset, stack, stack_size * sizeof(zend_jit_trace_stack)) == 0)) {
179
0
        if (t->exit_info[i].opline == to_opline
180
0
         && t->exit_info[i].flags == flags
181
0
         && t->exit_info[i].stack_size == stack_size
182
0
#if ZEND_DEBUG
183
0
         && (((JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_EXIT_INFO_SRC) == 0)
184
0
           || (strcmp(t->exit_info[i].filename, __zend_filename) == 0
185
0
             && t->exit_info[i].lineno == __zend_lineno))
186
0
#endif
187
0
        ) {
188
0
          return i;
189
0
        }
190
0
      }
191
0
    } while (i > 0);
192
0
  }
193
194
0
  exit_point = t->exit_count;
195
0
  if (exit_point < ZEND_JIT_TRACE_MAX_EXITS) {
196
0
    if (stack_size != 0 && stack_offset == (uint32_t)-1) {
197
0
      stack_offset = t->stack_map_size;
198
0
      t->stack_map_size += stack_size;
199
      // TODO: reduce number of reallocations ???
200
0
      t->stack_map = erealloc(t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
201
0
      memcpy(t->stack_map + stack_offset, stack, stack_size * sizeof(zend_jit_trace_stack));
202
0
    }
203
0
    t->exit_count++;
204
0
    t->exit_info[exit_point].opline = to_opline;
205
0
    t->exit_info[exit_point].op_array = op_array;
206
0
    t->exit_info[exit_point].flags = flags;
207
0
    t->exit_info[exit_point].stack_size = stack_size;
208
0
    t->exit_info[exit_point].stack_offset = stack_offset;
209
0
    t->exit_info[exit_point].poly_func = (zend_jit_ref_snapshot){.reg = ZREG_NONE};
210
0
    t->exit_info[exit_point].poly_this = (zend_jit_ref_snapshot){.reg = ZREG_NONE};
211
0
#if ZEND_DEBUG
212
0
    if ((JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_EXIT_INFO_SRC) != 0) {
213
0
      t->exit_info[exit_point].filename = __zend_filename;
214
0
      t->exit_info[exit_point].lineno = __zend_lineno;
215
0
    } else {
216
0
      t->exit_info[exit_point].filename = NULL;
217
0
      t->exit_info[exit_point].lineno = 0;
218
0
    }
219
0
#endif
220
0
  }
221
222
0
  return exit_point;
223
0
}
224
225
static void zend_jit_trace_add_code(const void *start, uint32_t size)
226
0
{
227
0
  zend_jit_trace_info *t = &zend_jit_traces[ZEND_JIT_TRACE_NUM];
228
229
0
  t->code_start = start;
230
0
  t->code_size  = size;
231
0
}
232
233
/**
234
 * Locate a trace in the #zend_jit_traces array with the specified
235
 * #code_start address.
236
 *
237
 * @return the #zend_jit_traces index or 0 if no such #code_start
238
 * address was found
239
 */
240
static uint32_t zend_jit_find_trace(const void *addr)
241
0
{
242
0
  uint32_t i;
243
244
0
  for (i = 1; i < ZEND_JIT_TRACE_NUM; i++) {
245
0
    if (zend_jit_traces[i].code_start == addr) {
246
0
      return i;
247
0
    }
248
0
  }
249
0
  return 0;
250
0
}
251
252
static zend_string *zend_jit_trace_name(const zend_op_array *op_array, uint32_t lineno)
253
0
{
254
0
  smart_str buf = {0};
255
256
0
  smart_str_appends(&buf, TRACE_PREFIX);
257
0
  smart_str_append_long(&buf, (zend_long)ZEND_JIT_TRACE_NUM);
258
0
  smart_str_appendc(&buf, '$');
259
0
  if (op_array->function_name) {
260
0
    if (op_array->scope) {
261
0
      smart_str_appendl(&buf, ZSTR_VAL(op_array->scope->name), ZSTR_LEN(op_array->scope->name));
262
0
      smart_str_appends(&buf, "::");
263
0
      smart_str_appendl(&buf, ZSTR_VAL(op_array->function_name), ZSTR_LEN(op_array->function_name));
264
0
    } else {
265
0
      smart_str_appendl(&buf, ZSTR_VAL(op_array->function_name), ZSTR_LEN(op_array->function_name));
266
0
    }
267
0
  } else if (op_array->filename) {
268
0
    smart_str_appendl(&buf, ZSTR_VAL(op_array->filename), ZSTR_LEN(op_array->filename));
269
0
  }
270
0
  smart_str_appendc(&buf, '$');
271
0
  smart_str_append_long(&buf, (zend_long)lineno);
272
0
  smart_str_0(&buf);
273
0
  return buf.s;
274
0
}
275
276
static int zend_jit_trace_may_exit(const zend_op_array *op_array, const zend_op *opline)
277
0
{
278
0
  switch (opline->opcode) {
279
0
    case ZEND_IS_IDENTICAL:
280
0
    case ZEND_IS_NOT_IDENTICAL:
281
0
    case ZEND_IS_EQUAL:
282
0
    case ZEND_IS_NOT_EQUAL:
283
0
    case ZEND_IS_SMALLER:
284
0
    case ZEND_IS_SMALLER_OR_EQUAL:
285
0
    case ZEND_CASE:
286
0
    case ZEND_CASE_STRICT:
287
0
    case ZEND_ISSET_ISEMPTY_CV:
288
0
    case ZEND_ISSET_ISEMPTY_VAR:
289
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
290
0
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
291
0
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
292
0
    case ZEND_INSTANCEOF:
293
0
    case ZEND_TYPE_CHECK:
294
0
    case ZEND_DEFINED:
295
0
    case ZEND_IN_ARRAY:
296
0
    case ZEND_ARRAY_KEY_EXISTS:
297
0
      if (opline->result_type & (IS_SMART_BRANCH_JMPNZ | IS_SMART_BRANCH_JMPZ)) {
298
        /* smart branch */
299
0
        return 1;
300
0
      }
301
0
      break;
302
0
    case ZEND_JMPZ:
303
0
    case ZEND_JMPNZ:
304
0
    case ZEND_JMPZ_EX:
305
0
    case ZEND_JMPNZ_EX:
306
0
    case ZEND_JMP_SET:
307
0
    case ZEND_COALESCE:
308
0
    case ZEND_JMP_NULL:
309
0
    case ZEND_FE_RESET_R:
310
0
    case ZEND_FE_RESET_RW:
311
0
    case ZEND_ASSERT_CHECK:
312
0
    case ZEND_FE_FETCH_R:
313
0
    case ZEND_FE_FETCH_RW:
314
0
    case ZEND_SWITCH_LONG:
315
0
    case ZEND_SWITCH_STRING:
316
0
    case ZEND_MATCH:
317
0
    case ZEND_BIND_INIT_STATIC_OR_JMP:
318
0
    case ZEND_JMP_FRAMELESS:
319
      /* branch opcodes */
320
0
      return 1;
321
0
    case ZEND_NEW:
322
0
      if (opline->extended_value == 0 && (opline+1)->opcode == ZEND_DO_FCALL) {
323
        /* NEW may skip constructor without arguments */
324
0
        return 1;
325
0
      }
326
0
      break;
327
0
    case ZEND_CATCH:
328
0
    case ZEND_FAST_CALL:
329
0
    case ZEND_FAST_RET:
330
0
    case ZEND_GENERATOR_CREATE:
331
0
    case ZEND_GENERATOR_RETURN:
332
0
    case ZEND_YIELD:
333
0
    case ZEND_YIELD_FROM:
334
0
    case ZEND_INCLUDE_OR_EVAL:
335
0
    case ZEND_MATCH_ERROR:
336
      /* unsupported */
337
0
      return 1;
338
0
    case ZEND_DO_FCALL:
339
      /* potentially polymorphic call */
340
0
      return 1;
341
#if 0
342
    case ZEND_DO_UCALL:
343
    case ZEND_DO_FCALL_BY_NAME:
344
      /* monomorphic call */
345
      // TODO: recompilation may change target ???
346
      return 0;
347
#endif
348
0
    case ZEND_RETURN_BY_REF:
349
0
    case ZEND_RETURN:
350
      /* return */
351
0
      return !JIT_G(current_frame) || TRACE_FRAME_IS_UNKNOWN_RETURN(JIT_G(current_frame));
352
0
    default:
353
0
      break;
354
0
  }
355
0
  return 0;
356
0
}
357
358
static zend_always_inline uint32_t zend_jit_trace_type_to_info_ex(uint8_t type, uint32_t info)
359
0
{
360
0
  if (type == IS_UNKNOWN) {
361
0
    return info;
362
0
  }
363
0
  ZEND_ASSERT(info & (1 << type));
364
0
  if (type < IS_STRING) {
365
0
    return (1 << type);
366
0
  } else if (type != IS_ARRAY) {
367
0
    return (1 << type) | (info & (MAY_BE_RC1|MAY_BE_RCN));
368
0
  } else {
369
0
    return MAY_BE_ARRAY | (info & (MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_ARRAY_KEY_ANY|MAY_BE_RC1|MAY_BE_RCN));
370
0
  }
371
0
}
372
373
static zend_always_inline uint32_t zend_jit_trace_type_to_info(uint8_t type)
374
0
{
375
0
  return zend_jit_trace_type_to_info_ex(type, -1);
376
0
}
377
378
static zend_always_inline zend_ssa_alias_kind zend_jit_var_may_alias(const zend_op_array *op_array, const zend_ssa *ssa, uint32_t var)
379
0
{
380
0
  if (var >= op_array->last_var) {
381
0
    return NO_ALIAS;
382
0
  } else if ((!op_array->function_name || (ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS))) {
383
0
    return SYMTABLE_ALIAS;
384
0
  } else if (ssa->vars) {
385
0
    return ssa->vars[var].alias;
386
0
  } else if (zend_string_equals_literal(op_array->vars[var], "http_response_header")) {
387
0
    return HTTP_RESPONSE_HEADER_ALIAS;
388
0
  }
389
0
  return NO_ALIAS;
390
0
}
391
392
static zend_always_inline void zend_jit_trace_add_op_guard(zend_ssa             *tssa,
393
                                                           int                   ssa_var,
394
                                                           uint8_t               op_type)
395
0
{
396
0
  zend_ssa_var_info *info = &tssa->var_info[ssa_var];
397
398
0
  if ((info->type & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1 << op_type)) {
399
0
    if (UNEXPECTED(tssa->vars[ssa_var].alias != NO_ALIAS)) {
400
0
      info->type |= MAY_BE_GUARD;
401
0
    } else {
402
0
      info->type = MAY_BE_GUARD | zend_jit_trace_type_to_info_ex(op_type, info->type);
403
0
    }
404
0
  }
405
0
}
406
407
0
#define ADD_OP_GUARD(_ssa_var, _op_type) do { \
408
0
    if (_ssa_var >= 0 && _op_type != IS_UNKNOWN) { \
409
0
      zend_jit_trace_add_op_guard(tssa, _ssa_var, _op_type); \
410
0
    } \
411
0
  } while (0)
412
413
0
#define CHECK_OP_TRACE_TYPE(_var, _ssa_var, op_info, op_type) do { \
414
0
    if (op_type != IS_UNKNOWN) { \
415
0
      if ((op_info & MAY_BE_GUARD) != 0) { \
416
0
        if (!zend_jit_type_guard(&ctx, opline, _var, op_type)) { \
417
0
          goto jit_failure; \
418
0
        } \
419
0
        if (ssa->vars[_ssa_var].alias != NO_ALIAS) { \
420
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(_var), IS_UNKNOWN, 1); \
421
0
          op_info = zend_jit_trace_type_to_info(op_type); \
422
0
        } else { \
423
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(_var), op_type, 1); \
424
0
          op_info &= ~MAY_BE_GUARD; \
425
0
          ssa->var_info[_ssa_var].type &= op_info; \
426
0
        } \
427
0
      } \
428
0
    } \
429
0
  } while (0)
430
431
#define ADD_OP1_TRACE_GUARD() \
432
0
  ADD_OP_GUARD(tssa->ops[idx].op1_use, op1_type)
433
#define ADD_OP2_TRACE_GUARD() \
434
0
  ADD_OP_GUARD(tssa->ops[idx].op2_use, op2_type)
435
#define ADD_OP1_DATA_TRACE_GUARD() \
436
0
  ADD_OP_GUARD(tssa->ops[idx+1].op1_use, op3_type)
437
438
#define CHECK_OP1_TRACE_TYPE() \
439
0
  CHECK_OP_TRACE_TYPE(opline->op1.var, ssa_op->op1_use, op1_info, op1_type)
440
#define CHECK_OP2_TRACE_TYPE() \
441
0
  CHECK_OP_TRACE_TYPE(opline->op2.var, ssa_op->op2_use, op2_info, op2_type)
442
#define CHECK_OP1_DATA_TRACE_TYPE() \
443
0
  CHECK_OP_TRACE_TYPE((opline+1)->op1.var, (ssa_op+1)->op1_use, op1_data_info, op3_type)
444
445
static zend_always_inline size_t zend_jit_trace_frame_size(const zend_op_array *op_array, uint32_t num_args)
446
0
{
447
0
  if (op_array && op_array->type == ZEND_USER_FUNCTION) {
448
0
    return ZEND_MM_ALIGNED_SIZE(offsetof(zend_jit_trace_stack_frame, stack) + ZEND_MM_ALIGNED_SIZE((op_array->last_var + op_array->T) * sizeof(zend_jit_trace_stack)));
449
0
  } else if (op_array) {
450
0
    return ZEND_MM_ALIGNED_SIZE(offsetof(zend_jit_trace_stack_frame, stack) + ZEND_MM_ALIGNED_SIZE(op_array->num_args * sizeof(zend_jit_trace_stack)));
451
0
  } else {
452
0
    return ZEND_MM_ALIGNED_SIZE(offsetof(zend_jit_trace_stack_frame, stack) + ZEND_MM_ALIGNED_SIZE(num_args * sizeof(zend_jit_trace_stack)));
453
0
  }
454
0
}
455
456
static zend_jit_trace_stack_frame* zend_jit_trace_call_frame(zend_jit_trace_stack_frame *frame, const zend_op_array *op_array, uint32_t num_args)
457
0
{
458
0
  return (zend_jit_trace_stack_frame*)((char*)frame + zend_jit_trace_frame_size(op_array, num_args));
459
0
}
460
461
static zend_jit_trace_stack_frame* zend_jit_trace_ret_frame(zend_jit_trace_stack_frame *frame, const zend_op_array *op_array)
462
0
{
463
0
  return (zend_jit_trace_stack_frame*)((char*)frame - zend_jit_trace_frame_size(op_array, 0));
464
0
}
465
466
static void zend_jit_trace_send_type(const zend_op *opline, zend_jit_trace_stack_frame *call, uint8_t type)
467
0
{
468
0
  zend_jit_trace_stack *stack = call->stack;
469
0
  const zend_op_array *op_array = &call->func->op_array;
470
0
  uint32_t arg_num = opline->op2.num;
471
472
0
  if (arg_num > op_array->num_args) {
473
0
    return;
474
0
  }
475
0
  if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
476
0
    zend_arg_info *arg_info;
477
478
0
    ZEND_ASSERT(arg_num <= op_array->num_args);
479
0
    arg_info = &op_array->arg_info[arg_num-1];
480
481
0
    if (ZEND_TYPE_IS_SET(arg_info->type)) {
482
0
      if (!(ZEND_TYPE_FULL_MASK(arg_info->type) & (1u << type))) {
483
0
        return;
484
0
      }
485
0
    }
486
0
  }
487
0
  SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), type, 1);
488
0
}
489
490
static bool zend_jit_needs_arg_dtor(const zend_function *func, uint32_t arg_num, zend_call_info *call_info)
491
0
{
492
0
  if (func
493
0
   && func->type == ZEND_INTERNAL_FUNCTION
494
0
   && (func->internal_function.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0
495
0
   && arg_num < func->internal_function.num_args) {
496
0
    const zend_internal_arg_info *arg_info = &func->internal_function.arg_info[arg_num];
497
498
0
    if (ZEND_ARG_SEND_MODE(arg_info) == ZEND_SEND_BY_VAL
499
0
     && ZEND_TYPE_IS_SET(arg_info->type)
500
0
     && (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_ANY) != MAY_BE_ANY) {
501
0
      if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE
502
0
       && JIT_G(current_frame)
503
0
       && JIT_G(current_frame)->call
504
0
       && JIT_G(current_frame)->call->func) {
505
0
        uint32_t type = STACK_TYPE(JIT_G(current_frame)->call->stack, arg_num);
506
507
0
        if (type != IS_UNKNOWN
508
0
         && type < IS_STRING
509
0
         && ZEND_TYPE_FULL_MASK(arg_info->type) & (1u << type)) {
510
0
          return false;
511
0
        }
512
0
      }
513
0
      if (call_info && arg_num < call_info->num_args && call_info->arg_info[arg_num].opline) {
514
0
        const zend_op *opline = call_info->arg_info[arg_num].opline;
515
516
0
        if (opline->opcode == ZEND_SEND_VAL && opline->op1_type == IS_CONST) {
517
0
          zval *zv = RT_CONSTANT(opline, opline->op1);
518
519
0
          if (!Z_REFCOUNTED_P(zv)) {
520
0
            uint32_t type = Z_TYPE_P(zv);
521
522
            // TODO: few functions (e.g. pcntl_exec) modify arrays in-place ???
523
0
            if (type != IS_ARRAY
524
0
             && (ZEND_TYPE_FULL_MASK(arg_info->type) & (1u << type))) {
525
0
              return false;
526
0
            }
527
0
          }
528
0
        }
529
0
      }
530
0
    }
531
0
  }
532
533
0
  return true;
534
0
}
535
536
static zend_ssa *zend_jit_trace_build_ssa(const zend_op_array *op_array, zend_script *script)
537
0
{
538
0
  zend_jit_op_array_trace_extension *jit_extension;
539
0
  zend_ssa *ssa;
540
541
0
  jit_extension =
542
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
543
0
  jit_extension->func_info.num = 0;
544
0
  jit_extension->func_info.flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC
545
0
    | ZEND_FUNC_JIT_ON_PROF_REQUEST
546
0
    | ZEND_FUNC_JIT_ON_HOT_COUNTERS
547
0
    | ZEND_FUNC_JIT_ON_HOT_TRACE;
548
0
  memset(&jit_extension->func_info.ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa));
549
0
  ssa = &jit_extension->func_info.ssa;
550
551
0
  if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNC) {
552
0
    do {
553
0
      if (zend_jit_op_array_analyze1(op_array, script, ssa) != SUCCESS) {
554
0
        break;
555
0
      }
556
557
0
      if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNCS) {
558
0
        zend_analyze_calls(&CG(arena), script, ZEND_CALL_TREE, (zend_op_array*)op_array, &jit_extension->func_info);
559
0
        jit_extension->func_info.call_map = zend_build_call_map(&CG(arena), &jit_extension->func_info, op_array);
560
0
        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
561
0
          zend_init_func_return_info(op_array, script, &jit_extension->func_info.return_info);
562
0
        }
563
0
      }
564
565
0
      if (zend_jit_op_array_analyze2(op_array, script, ssa, 0) != SUCCESS) {
566
0
        break;
567
0
      }
568
569
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_SSA) {
570
0
        zend_dump_op_array(op_array, ZEND_DUMP_HIDE_UNREACHABLE|ZEND_DUMP_RC_INFERENCE|ZEND_DUMP_SSA, "JIT", ssa);
571
0
      }
572
0
      return ssa;
573
0
    } while (0);
574
0
  }
575
576
0
  memset(ssa, 0, sizeof(zend_ssa));
577
0
  ssa->cfg.blocks_count = 1;
578
579
0
  if (JIT_G(opt_level) == ZEND_JIT_LEVEL_INLINE) {
580
0
    zend_cfg cfg;
581
0
    void *checkpoint = zend_arena_checkpoint(CG(arena));
582
583
0
    if (zend_jit_build_cfg(op_array, &cfg) == SUCCESS) {
584
0
      ssa->cfg.flags = cfg.flags;
585
0
    } else{
586
0
      ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
587
0
    }
588
589
    /* TODO: move this to zend_cfg.c ? */
590
0
    if (!op_array->function_name) {
591
0
      ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
592
0
    }
593
594
0
    zend_arena_release(&CG(arena), checkpoint);
595
0
  }
596
597
0
  return ssa;
598
0
}
599
600
static void zend_jit_dump_trace(zend_jit_trace_rec *trace_buffer, zend_ssa *tssa);
601
static void zend_jit_dump_exit_info(zend_jit_trace_info *t);
602
603
static zend_always_inline int zend_jit_trace_op_len(const zend_op *opline)
604
0
{
605
0
  int len;
606
607
0
  switch (opline->opcode) {
608
0
    case ZEND_ASSIGN_DIM:
609
0
    case ZEND_ASSIGN_OBJ:
610
0
    case ZEND_ASSIGN_STATIC_PROP:
611
0
    case ZEND_ASSIGN_DIM_OP:
612
0
    case ZEND_ASSIGN_OBJ_OP:
613
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
614
0
    case ZEND_ASSIGN_OBJ_REF:
615
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
616
0
    case ZEND_FRAMELESS_ICALL_3:
617
0
    case ZEND_DECLARE_ATTRIBUTED_CONST:
618
0
      return 2; /* OP_DATA */
619
0
    case ZEND_RECV_INIT:
620
0
      len = 1;
621
0
      opline++;
622
0
      while (opline->opcode == ZEND_RECV_INIT) {
623
0
        len++;
624
0
        opline++;
625
0
      }
626
0
      return len;
627
0
    case ZEND_BIND_GLOBAL:
628
0
      len = 1;
629
0
      opline++;
630
0
      while (opline->opcode == ZEND_BIND_GLOBAL) {
631
0
        len++;
632
0
        opline++;
633
0
      }
634
0
      return len;
635
//    case ZEND_IS_IDENTICAL:
636
//    case ZEND_IS_NOT_IDENTICAL:
637
//    case ZEND_IS_EQUAL:
638
//    case ZEND_IS_NOT_EQUAL:
639
//    case ZEND_IS_SMALLER:
640
//    case ZEND_IS_SMALLER_OR_EQUAL:
641
//    case ZEND_CASE:
642
//    case ZEND_ISSET_ISEMPTY_CV:
643
//    case ZEND_ISSET_ISEMPTY_VAR:
644
//    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
645
//    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
646
//    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
647
//    case ZEND_INSTANCEOF:
648
//    case ZEND_TYPE_CHECK:
649
//    case ZEND_DEFINED:
650
//    case ZEND_IN_ARRAY:
651
//    case ZEND_ARRAY_KEY_EXISTS:
652
0
    default:
653
0
      if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
654
0
        return 2; /* JMPZ/JMPNZ */
655
0
      }
656
0
      return 1;
657
0
  }
658
0
}
659
660
static int zend_jit_trace_add_phis(zend_jit_trace_rec *trace_buffer, uint32_t ssa_vars_count, zend_ssa *tssa, zend_jit_trace_stack *stack)
661
0
{
662
0
  const zend_op_array *op_array;
663
0
  zend_jit_trace_rec *p;
664
0
  int k, vars_count;
665
0
  zend_bitset use, def;
666
0
  uint32_t build_flags = ZEND_SSA_RC_INFERENCE | ZEND_SSA_USE_CV_RESULTS;
667
0
  uint32_t set_size;
668
0
  zend_ssa_phi *prev = NULL;
669
0
  int level = 0;
670
0
  ALLOCA_FLAG(use_heap);
671
672
0
  op_array = trace_buffer->op_array;
673
0
  set_size = zend_bitset_len(op_array->last_var + op_array->T);
674
0
  use = ZEND_BITSET_ALLOCA(set_size * 2, use_heap);
675
0
  memset(use, 0, set_size * 2 * ZEND_BITSET_ELM_SIZE);
676
0
  def = use + set_size;
677
0
  p = trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE;
678
0
  for (;;p++) {
679
0
    if (p->op == ZEND_JIT_TRACE_VM && level == 0) {
680
0
      const zend_op *opline = p->opline;
681
0
      int len;
682
683
0
      zend_dfg_add_use_def_op(op_array, opline, build_flags, use, def);
684
0
      len = zend_jit_trace_op_len(opline);
685
0
      while (len > 1) {
686
0
        opline++;
687
0
        if (opline->opcode != ZEND_OP_DATA) {
688
0
          zend_dfg_add_use_def_op(op_array, opline, build_flags, use, def);
689
0
        }
690
0
        len--;
691
0
      }
692
0
    } else if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
693
0
    } else if (p->op == ZEND_JIT_TRACE_DO_ICALL) {
694
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
695
0
      level++;
696
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
697
0
      if (level == 0) {
698
        // Phi for recursive calls and returns are not supported yet ???
699
0
        assert(0);
700
0
      } else {
701
0
        level--;
702
0
      }
703
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
704
0
      break;
705
0
    }
706
0
  }
707
708
0
  zend_bitset_intersection(use, def, set_size);
709
710
0
  if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER) {
711
0
    vars_count = op_array->last_var;
712
0
  } else {
713
0
    vars_count = op_array->last_var + op_array->T;
714
0
  }
715
0
  for (k = 0; k < vars_count; k++) {
716
0
    if (zend_bitset_in(use, k)) {
717
0
      zend_ssa_phi *phi = zend_arena_calloc(&CG(arena), 1,
718
0
        ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)) +
719
0
        ZEND_MM_ALIGNED_SIZE(sizeof(int) * 2) +
720
0
        sizeof(void*) * 2);
721
0
      phi->sources = (int*)(((char*)phi) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)));
722
0
      phi->sources[0] = STACK_VAR(stack, k);
723
0
      phi->sources[1] = -1;
724
0
      phi->use_chains = (zend_ssa_phi**)(((char*)phi->sources) + ZEND_MM_ALIGNED_SIZE(sizeof(int) * 2));
725
0
      phi->pi = -1;
726
0
      phi->var = k;
727
0
      phi->ssa_var = ssa_vars_count;
728
0
      SET_STACK_VAR(stack, k, ssa_vars_count);
729
0
      ssa_vars_count++;
730
0
      phi->block = 1;
731
0
      if (prev) {
732
0
        prev->next = phi;
733
0
      } else {
734
0
        tssa->blocks[1].phis = phi;
735
0
      }
736
0
      prev = phi;
737
0
    }
738
0
  }
739
740
0
  free_alloca(use, use_heap);
741
742
0
  return ssa_vars_count;
743
0
}
744
745
static int zend_jit_trace_add_call_phis(zend_jit_trace_rec *trace_buffer, uint32_t ssa_vars_count, zend_ssa *tssa, zend_jit_trace_stack *stack)
746
0
{
747
0
  zend_ssa_phi *prev = NULL;
748
0
  const zend_op_array *op_array = trace_buffer->op_array;
749
0
  const zend_op *opline = trace_buffer[1].opline;
750
0
  int count = opline - op_array->opcodes;
751
0
  int i;
752
753
0
  for(i = 0; i < count; i++) {
754
0
    zend_ssa_phi *phi = zend_arena_calloc(&CG(arena), 1,
755
0
      ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)) +
756
0
      ZEND_MM_ALIGNED_SIZE(sizeof(int) * 2) +
757
0
      sizeof(void*) * 2);
758
0
    phi->sources = (int*)(((char*)phi) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)));
759
0
    phi->sources[0] = STACK_VAR(stack, i);
760
0
    phi->sources[1] = -1;
761
0
    phi->use_chains = (zend_ssa_phi**)(((char*)phi->sources) + ZEND_MM_ALIGNED_SIZE(sizeof(int) * 2));
762
0
    phi->pi = -1;
763
0
    phi->var = i;
764
0
    phi->ssa_var = ssa_vars_count;
765
0
    SET_STACK_VAR(stack, i, ssa_vars_count);
766
0
    ssa_vars_count++;
767
0
    phi->block = 1;
768
0
    if (prev) {
769
0
      prev->next = phi;
770
0
    } else {
771
0
      tssa->blocks[1].phis = phi;
772
0
    }
773
0
    prev = phi;
774
0
  }
775
0
  return ssa_vars_count;
776
0
}
777
778
static int zend_jit_trace_add_ret_phis(zend_jit_trace_rec *trace_buffer, uint32_t ssa_vars_count, zend_ssa *tssa, zend_jit_trace_stack *stack)
779
0
{
780
0
  const zend_op *opline = trace_buffer[1].opline - 1;
781
0
  int i;
782
783
0
  if (RETURN_VALUE_USED(opline)) {
784
0
    zend_ssa_phi *phi = zend_arena_calloc(&CG(arena), 1,
785
0
      ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)) +
786
0
      ZEND_MM_ALIGNED_SIZE(sizeof(int) * 2) +
787
0
      sizeof(void*) * 2);
788
789
0
    i = EX_VAR_TO_NUM(opline->result.var);
790
0
    phi->sources = (int*)(((char*)phi) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)));
791
0
    phi->sources[0] = STACK_VAR(stack, i);
792
0
    phi->sources[1] = -1;
793
0
    phi->use_chains = (zend_ssa_phi**)(((char*)phi->sources) + ZEND_MM_ALIGNED_SIZE(sizeof(int) * 2));
794
0
    phi->pi = -1;
795
0
    phi->var = i;
796
0
    phi->ssa_var = ssa_vars_count;
797
0
    SET_STACK_VAR(stack, i, ssa_vars_count);
798
0
    ssa_vars_count++;
799
0
    phi->block = 1;
800
0
    tssa->blocks[1].phis = phi;
801
0
  }
802
0
  return ssa_vars_count;
803
0
}
804
805
static bool zend_jit_trace_is_false_loop(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op **tssa_opcodes, zend_ssa *tssa)
806
0
{
807
0
  const zend_op *opline;
808
0
  uint32_t b;
809
0
  zend_basic_block *bb;
810
811
0
  ZEND_ASSERT(tssa->cfg.blocks_count == 2);
812
0
  ZEND_ASSERT(tssa->cfg.blocks[1].len > 0);
813
814
0
  b = ssa->cfg.map[tssa_opcodes[0] - op_array->opcodes];
815
0
  opline = tssa_opcodes[tssa->cfg.blocks[1].len - 1];
816
0
  if (opline >= op_array->opcodes && opline < op_array->opcodes + op_array->last) {
817
0
    bb = ssa->cfg.blocks + ssa->cfg.map[opline - op_array->opcodes];
818
0
    return bb->loop_header != b;
819
0
  } else {
820
0
    return false;
821
0
  }
822
0
}
823
824
static int zend_jit_trace_copy_ssa_var_info(const zend_op_array  *op_array,
825
                                            const zend_ssa       *ssa,
826
                                            const zend_op       **tssa_opcodes,
827
                                            zend_ssa             *tssa,
828
                                            int                   ssa_var,
829
                                            const zend_op        *opline)
830
0
{
831
0
  int var, use, def, src;
832
0
  zend_ssa_op *op;
833
834
0
  if (tssa->vars[ssa_var].definition_phi) {
835
0
    uint32_t b = ssa->cfg.map[tssa_opcodes[0] - op_array->opcodes];
836
0
    zend_basic_block *bb = ssa->cfg.blocks + b;
837
838
0
    if ((bb->flags & ZEND_BB_LOOP_HEADER)
839
0
     && !zend_jit_trace_is_false_loop(op_array, ssa, tssa_opcodes, tssa)) {
840
0
      zend_ssa_phi *phi = ssa->blocks[b].phis;
841
0
      zend_ssa_phi *pi = NULL;
842
843
0
      var = tssa->vars[ssa_var].var;
844
0
      while (phi) {
845
0
        if (ssa->vars[phi->ssa_var].var == var) {
846
0
          if (phi->pi >= 0) {
847
0
            pi = phi;
848
0
          } else {
849
0
            src = phi->ssa_var;
850
0
            goto copy_info;
851
0
          }
852
0
        }
853
0
        phi = phi->next;
854
0
      }
855
0
      if (pi) {
856
0
        src = pi->ssa_var;
857
0
        goto copy_info;
858
0
      }
859
#if 0
860
      while (bb->idom >= 0) {
861
        uint32_t n;
862
863
        b = bb->idom;
864
        bb = ssa->cfg.blocks + b;
865
866
        for (n = bb->len, op = ssa->ops + bb->start + n; n > 0; n--) {
867
          op--;
868
          if (op->result_def >= 0 && ssa->vars[op->result_def].var == var) {
869
            src = op->result_def;
870
            goto copy_info;
871
          } else if (op->op2_def >= 0 && ssa->vars[op->op2_def].var == var) {
872
            src = op->op2_def;
873
            goto copy_info;
874
          } else if (op->op1_def >= 0 && ssa->vars[op->op1_def].var == var) {
875
            src = op->op1_def;
876
            goto copy_info;
877
          }
878
        }
879
880
        phi = ssa->blocks[b].phis;
881
        zend_ssa_phi *pi = NULL;
882
        while (phi) {
883
          if (ssa->vars[phi->ssa_var].var == var) {
884
            if (phi->pi >= 0) {
885
              pi = phi;
886
            } else {
887
              src = phi->ssa_var;
888
              goto copy_info;
889
            }
890
          }
891
          phi = phi->next;
892
        }
893
        if (pi) {
894
          src = pi->ssa_var;
895
          goto copy_info;
896
        }
897
      }
898
#endif
899
0
    }
900
0
  } else if (tssa->vars[ssa_var].definition >= 0) {
901
0
    def = tssa->vars[ssa_var].definition;
902
0
    ZEND_ASSERT((tssa_opcodes[def] - op_array->opcodes) < op_array->last);
903
0
    op = ssa->ops + (tssa_opcodes[def] - op_array->opcodes);
904
0
    if (tssa->ops[def].op1_def == ssa_var) {
905
0
      src = op->op1_def;
906
0
    } else if (tssa->ops[def].op2_def == ssa_var) {
907
0
      src = op->op2_def;
908
0
    } else if (tssa->ops[def].result_def == ssa_var) {
909
0
      src = op->result_def;
910
0
    } else {
911
0
      assert(0);
912
0
      return 0;
913
0
    }
914
0
    goto copy_info;
915
0
  }
916
917
0
  if (tssa->vars[ssa_var].phi_use_chain) {
918
    // TODO: this may be incorrect ???
919
0
    var = tssa->vars[ssa_var].phi_use_chain->ssa_var;
920
0
  } else {
921
0
    var = ssa_var;
922
0
  }
923
0
  use = tssa->vars[var].use_chain;
924
0
  if (use >= 0) {
925
0
    ZEND_ASSERT((tssa_opcodes[use] - op_array->opcodes) < op_array->last);
926
0
    op = ssa->ops + (tssa_opcodes[use] - op_array->opcodes);
927
0
    if (tssa->ops[use].op1_use == var) {
928
0
      src = op->op1_use;
929
0
    } else if (tssa->ops[use].op2_use == var) {
930
0
      src = op->op2_use;
931
0
    } else if (tssa->ops[use].result_use == var) {
932
0
      src = op->result_use;
933
0
    } else {
934
0
      assert(0);
935
0
      return 0;
936
0
    }
937
0
    if (opline) {
938
      /* Try to find a difinition in SSA dominators tree */
939
0
      var = tssa->vars[ssa_var].var;
940
0
      uint32_t op_num = opline - op_array->opcodes;
941
0
      uint32_t b = ssa->cfg.map[op_num];
942
0
      zend_basic_block *bb = ssa->cfg.blocks + b;
943
0
      zend_ssa_phi *pi, *phi;
944
945
0
      while (1) {
946
0
        while (op_num > bb->start) {
947
0
          op_num--;
948
0
          op = ssa->ops + op_num;
949
0
          if (op->result_def >= 0 && ssa->vars[op->result_def].var == var) {
950
0
            src = op->result_def;
951
0
            goto copy_info;
952
0
          } else if (op->op2_def >= 0 && ssa->vars[op->op2_def].var == var) {
953
0
            src = op->op2_def;
954
0
            goto copy_info;
955
0
          } else if (op->op1_def >= 0 && ssa->vars[op->op1_def].var == var) {
956
0
            src = op->op1_def;
957
0
            goto copy_info;
958
0
          }
959
0
        }
960
0
        phi = ssa->blocks[b].phis;
961
0
        pi = NULL;
962
0
        while (phi) {
963
0
          if (ssa->vars[phi->ssa_var].var == var) {
964
0
            if (phi->pi >= 0) {
965
0
              pi = phi;
966
0
            } else {
967
0
              src = phi->ssa_var;
968
0
              goto copy_info;
969
0
            }
970
0
          }
971
0
          phi = phi->next;
972
0
        }
973
0
        if (pi) {
974
0
          src = pi->ssa_var;
975
0
          goto copy_info;
976
0
        }
977
0
        if (bb->idom < 0) {
978
0
          break;
979
0
        }
980
0
        b = bb->idom;
981
0
        bb = ssa->cfg.blocks + b;
982
0
        op_num = bb->start + bb->len;
983
0
      }
984
0
    }
985
0
    goto copy_info;
986
0
  }
987
0
  return 0;
988
989
0
copy_info:
990
0
  tssa->vars[ssa_var].no_val = ssa->vars[src].no_val;
991
0
  tssa->vars[ssa_var].alias = ssa->vars[src].alias;
992
0
  memcpy(&tssa->var_info[ssa_var], &ssa->var_info[src], sizeof(zend_ssa_var_info));
993
0
  return 1;
994
0
}
995
996
static void zend_jit_trace_propagate_range(const zend_op_array *op_array, const zend_op **tssa_opcodes, zend_ssa *tssa, int ssa_var)
997
0
{
998
0
  zend_ssa_range tmp;
999
0
  int def = tssa->vars[ssa_var].definition;
1000
1001
0
  if (tssa->vars[ssa_var].alias == NO_ALIAS
1002
0
   && zend_inference_propagate_range(op_array, tssa, tssa_opcodes[def], &tssa->ops[def], ssa_var, &tmp)) {
1003
0
    tssa->var_info[ssa_var].range.min = tmp.min;
1004
0
    tssa->var_info[ssa_var].range.max = tmp.max;
1005
0
    tssa->var_info[ssa_var].range.underflow = tmp.underflow;
1006
0
    tssa->var_info[ssa_var].range.overflow = tmp.overflow;
1007
0
    tssa->var_info[ssa_var].has_range = 1;
1008
0
  }
1009
0
}
1010
1011
static void zend_jit_trace_copy_ssa_var_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op **tssa_opcodes, zend_ssa *tssa, int ssa_var)
1012
0
{
1013
0
  int def;
1014
0
  zend_ssa_op *op;
1015
0
  zend_ssa_var_info *info;
1016
0
  unsigned int no_val;
1017
0
  zend_ssa_alias_kind alias;
1018
1019
0
  def = tssa->vars[ssa_var].definition;
1020
0
  if (def >= 0) {
1021
0
    ZEND_ASSERT((tssa_opcodes[def] - op_array->opcodes) < op_array->last);
1022
0
    op = ssa->ops + (tssa_opcodes[def] - op_array->opcodes);
1023
0
    if (tssa->ops[def].op1_def == ssa_var) {
1024
0
      no_val = ssa->vars[op->op1_def].no_val;
1025
0
      alias = ssa->vars[op->op1_def].alias;
1026
0
      info = ssa->var_info + op->op1_def;
1027
0
    } else if (tssa->ops[def].op2_def == ssa_var) {
1028
0
      no_val = ssa->vars[op->op2_def].no_val;
1029
0
      alias = ssa->vars[op->op2_def].alias;
1030
0
      info = ssa->var_info + op->op2_def;
1031
0
    } else if (tssa->ops[def].result_def == ssa_var) {
1032
0
      no_val = ssa->vars[op->result_def].no_val;
1033
0
      alias = ssa->vars[op->result_def].alias;
1034
0
      info = ssa->var_info + op->result_def;
1035
0
    } else {
1036
0
      assert(0);
1037
0
      return;
1038
0
    }
1039
1040
0
    tssa->vars[ssa_var].no_val = no_val;
1041
0
    tssa->vars[ssa_var].alias = alias;
1042
1043
0
    if (!(info->type & MAY_BE_REF)) {
1044
0
      zend_jit_trace_propagate_range(op_array, tssa_opcodes, tssa, ssa_var);
1045
0
    }
1046
1047
0
    if (info->has_range) {
1048
0
      if (tssa->var_info[ssa_var].has_range) {
1049
0
        tssa->var_info[ssa_var].range.min = MAX(tssa->var_info[ssa_var].range.min, info->range.min);
1050
0
        tssa->var_info[ssa_var].range.max = MIN(tssa->var_info[ssa_var].range.max, info->range.max);
1051
0
        tssa->var_info[ssa_var].range.underflow = tssa->var_info[ssa_var].range.underflow && info->range.underflow;
1052
0
        tssa->var_info[ssa_var].range.overflow = tssa->var_info[ssa_var].range.overflow && info->range.overflow;
1053
0
      } else {
1054
0
        tssa->var_info[ssa_var].has_range = 1;
1055
0
        tssa->var_info[ssa_var].range = info->range;
1056
0
      }
1057
0
    }
1058
0
  }
1059
0
}
1060
1061
static int zend_jit_trace_restrict_ssa_var_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op **tssa_opcodes, zend_ssa *tssa, int ssa_var)
1062
0
{
1063
0
  int def;
1064
0
  zend_ssa_op *op;
1065
0
  zend_ssa_var_info *info;
1066
1067
0
  def = tssa->vars[ssa_var].definition;
1068
0
  if (def >= 0) {
1069
0
    ZEND_ASSERT((tssa_opcodes[def] - op_array->opcodes) < op_array->last);
1070
0
    op = ssa->ops + (tssa_opcodes[def] - op_array->opcodes);
1071
0
    if (tssa->ops[def].op1_def == ssa_var) {
1072
0
      info = ssa->var_info + op->op1_def;
1073
0
    } else if (tssa->ops[def].op2_def == ssa_var) {
1074
0
      info = ssa->var_info + op->op2_def;
1075
0
    } else if (tssa->ops[def].result_def == ssa_var) {
1076
0
      info = ssa->var_info + op->result_def;
1077
0
    } else {
1078
0
      assert(0);
1079
0
      return 0;
1080
0
    }
1081
0
    tssa->var_info[ssa_var].type &= info->type;
1082
0
    if (info->ce) {
1083
0
      if (tssa->var_info[ssa_var].ce) {
1084
0
        if (tssa->var_info[ssa_var].ce != info->ce) {
1085
0
          if (instanceof_function(tssa->var_info[ssa_var].ce, info->ce)) {
1086
            /* everything fine */
1087
0
          } else if (instanceof_function(info->ce, tssa->var_info[ssa_var].ce)) {
1088
            // TODO: TSSA may miss Pi() functions and corresponding instanceof() constraints ???
1089
0
          } else {
1090
            // TODO: classes may implement the same interface ???
1091
            //ZEND_UNREACHABLE();
1092
0
          }
1093
0
        }
1094
0
        tssa->var_info[ssa_var].is_instanceof =
1095
0
          tssa->var_info[ssa_var].is_instanceof && info->is_instanceof;
1096
0
      } else {
1097
0
        tssa->var_info[ssa_var].ce = info->ce;
1098
0
        tssa->var_info[ssa_var].is_instanceof = info->is_instanceof;
1099
0
      }
1100
0
    }
1101
0
    if (info->has_range) {
1102
0
      if (tssa->var_info[ssa_var].has_range) {
1103
0
        tssa->var_info[ssa_var].range.min = MAX(tssa->var_info[ssa_var].range.min, info->range.min);
1104
0
        tssa->var_info[ssa_var].range.max = MIN(tssa->var_info[ssa_var].range.max, info->range.max);
1105
0
        tssa->var_info[ssa_var].range.underflow = tssa->var_info[ssa_var].range.underflow && info->range.underflow;
1106
0
        tssa->var_info[ssa_var].range.overflow = tssa->var_info[ssa_var].range.overflow && info->range.overflow;
1107
0
      } else {
1108
0
        tssa->var_info[ssa_var].has_range = 1;
1109
0
        tssa->var_info[ssa_var].range = info->range;
1110
0
      }
1111
0
    }
1112
0
    return 1;
1113
0
  }
1114
0
  return 0;
1115
0
}
1116
1117
static int find_return_ssa_var(zend_jit_trace_rec *p, zend_ssa_op *ssa_op)
1118
0
{
1119
0
  while (1) {
1120
0
    if (p->op == ZEND_JIT_TRACE_VM) {
1121
0
      if (p->opline->opcode == ZEND_DO_UCALL
1122
0
       || p->opline->opcode == ZEND_DO_FCALL_BY_NAME
1123
0
       || p->opline->opcode == ZEND_DO_FCALL) {
1124
0
        if (p->opline->result_type != IS_UNUSED) {
1125
0
          return ssa_op->result_def;
1126
0
        }
1127
0
      }
1128
0
      return -1;
1129
0
    } else if (p->op >= ZEND_JIT_TRACE_OP1_TYPE && p->op <= ZEND_JIT_TRACE_VAL_INFO) {
1130
      /*skip */
1131
0
    } else {
1132
0
      return -1;
1133
0
    }
1134
0
    p--;
1135
0
  }
1136
0
}
1137
1138
static const zend_op *zend_jit_trace_find_init_fcall_op(zend_jit_trace_rec *p, const zend_op_array *op_array)
1139
0
{
1140
0
  if (!(p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL)) {
1141
0
    p--;
1142
0
    while (1) {
1143
0
      if (p->op == ZEND_JIT_TRACE_VM) {
1144
0
        if (p->opline->opcode == ZEND_INIT_FCALL
1145
0
         || p->opline->opcode == ZEND_INIT_FCALL_BY_NAME
1146
0
         || p->opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME
1147
0
         || p->opline->opcode == ZEND_INIT_DYNAMIC_CALL
1148
0
         || p->opline->opcode == ZEND_INIT_USER_CALL
1149
0
         || p->opline->opcode == ZEND_NEW
1150
0
         || p->opline->opcode == ZEND_INIT_METHOD_CALL
1151
0
         || p->opline->opcode == ZEND_INIT_STATIC_METHOD_CALL
1152
0
         || p->opline->opcode == ZEND_INIT_PARENT_PROPERTY_HOOK_CALL) {
1153
0
          return p->opline;
1154
0
        }
1155
0
        return NULL;
1156
0
      } else if (p->op >= ZEND_JIT_TRACE_OP1_TYPE && p->op <= ZEND_JIT_TRACE_VAL_INFO) {
1157
        /*skip */
1158
0
      } else {
1159
0
        return NULL;
1160
0
      }
1161
0
      p--;
1162
0
    }
1163
0
  } else {
1164
0
    const zend_op *opline = NULL;
1165
0
    int call_level = 0;
1166
1167
0
    p++;
1168
0
    while (1) {
1169
0
      if (p->op == ZEND_JIT_TRACE_VM) {
1170
0
        opline = p->opline;
1171
0
        break;
1172
0
      } else if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
1173
0
        call_level++;
1174
        /*skip */
1175
0
      } else {
1176
0
        return NULL;
1177
0
      }
1178
0
      p--;
1179
0
    }
1180
0
    if (opline) {
1181
0
      while (opline > op_array->opcodes) {
1182
0
        opline--;
1183
0
        if (zend_jit_inc_call_level(opline->opcode)) {
1184
0
          if (call_level == 0) {
1185
0
            return opline;
1186
0
          }
1187
0
          call_level--;
1188
0
        } else if (zend_jit_dec_call_level(opline->opcode)) {
1189
0
          call_level++;
1190
0
        }
1191
0
      }
1192
0
    }
1193
0
  }
1194
0
  return NULL;
1195
0
}
1196
1197
static int is_checked_guard(const zend_ssa *tssa, const zend_op **ssa_opcodes, uint32_t var, uint32_t phi_var)
1198
0
{
1199
0
  if ((tssa->var_info[phi_var].type & MAY_BE_ANY) == MAY_BE_LONG
1200
0
   && !(tssa->var_info[var].type & MAY_BE_REF)) {
1201
0
    int idx = tssa->vars[var].definition;
1202
1203
0
    if (idx >= 0) {
1204
0
      if (tssa->ops[idx].op1_def == var) {
1205
0
        const zend_op *opline = ssa_opcodes[idx];
1206
0
        if (opline->opcode == ZEND_PRE_DEC
1207
0
         || opline->opcode == ZEND_PRE_INC
1208
0
         || opline->opcode == ZEND_POST_DEC
1209
0
         || opline->opcode == ZEND_POST_INC) {
1210
0
          if (tssa->ops[idx].op1_use >= 0
1211
0
           && (tssa->var_info[tssa->ops[idx].op1_use].type & MAY_BE_STRING)) {
1212
0
            return 0;
1213
0
          }
1214
0
          if (!(tssa->var_info[tssa->ops[idx].op1_use].type & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1215
0
            return 0;
1216
0
          }
1217
0
          return 1;
1218
0
        } else if (opline->opcode == ZEND_ASSIGN_OP
1219
0
         && (opline->extended_value == ZEND_ADD
1220
0
          || opline->extended_value == ZEND_SUB
1221
0
          || opline->extended_value == ZEND_MUL)) {
1222
0
          if ((opline->op2_type & (IS_VAR|IS_CV))
1223
0
            && tssa->ops[idx].op2_use >= 0
1224
0
            && (tssa->var_info[tssa->ops[idx].op2_use].type & MAY_BE_REF)) {
1225
0
            return 0;
1226
0
          }
1227
0
          if (!(tssa->var_info[tssa->ops[idx].op1_use].type & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1228
0
            return 0;
1229
0
          }
1230
0
          if (opline->op2_type == IS_CONST) {
1231
0
            zval *zv = RT_CONSTANT(opline, opline->op2);
1232
0
            if (Z_TYPE_P(zv) != IS_LONG && Z_TYPE_P(zv) != IS_DOUBLE) {
1233
0
              return 0;
1234
0
            }
1235
0
          } else if (!(tssa->var_info[tssa->ops[idx].op2_use].type & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1236
0
            return 0;
1237
0
          }
1238
0
          return 1;
1239
0
        }
1240
0
      }
1241
0
      if (tssa->ops[idx].result_def == var) {
1242
0
        const zend_op *opline = ssa_opcodes[idx];
1243
0
        if (opline->opcode == ZEND_ADD
1244
0
         || opline->opcode == ZEND_SUB
1245
0
         || opline->opcode == ZEND_MUL) {
1246
0
          if ((opline->op1_type & (IS_VAR|IS_CV))
1247
0
            && tssa->ops[idx].op1_use >= 0
1248
0
            && (tssa->var_info[tssa->ops[idx].op1_use].type & MAY_BE_REF)) {
1249
0
            return 0;
1250
0
          }
1251
0
          if ((opline->op2_type & (IS_VAR|IS_CV))
1252
0
            && tssa->ops[idx].op2_use >= 0
1253
0
            && (tssa->var_info[tssa->ops[idx].op2_use].type & MAY_BE_REF)) {
1254
0
            return 0;
1255
0
          }
1256
0
          if (opline->op1_type == IS_CONST) {
1257
0
            zval *zv = RT_CONSTANT(opline, opline->op1);
1258
0
            if (Z_TYPE_P(zv) != IS_LONG && Z_TYPE_P(zv) != IS_DOUBLE) {
1259
0
              return 0;
1260
0
            }
1261
0
          } else if (!(tssa->var_info[tssa->ops[idx].op1_use].type & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1262
0
            return 0;
1263
0
          }
1264
0
          if (opline->op2_type == IS_CONST) {
1265
0
            zval *zv = RT_CONSTANT(opline, opline->op2);
1266
0
            if (Z_TYPE_P(zv) != IS_LONG && Z_TYPE_P(zv) != IS_DOUBLE) {
1267
0
              return 0;
1268
0
            }
1269
0
          } else if (!(tssa->var_info[tssa->ops[idx].op2_use].type & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1270
0
            return 0;
1271
0
          }
1272
0
          return 1;
1273
0
        } else if (opline->opcode == ZEND_PRE_DEC
1274
0
         || opline->opcode == ZEND_PRE_INC
1275
0
         || opline->opcode == ZEND_POST_DEC
1276
0
         || opline->opcode == ZEND_POST_INC) {
1277
0
          if ((opline->op1_type & (IS_VAR|IS_CV))
1278
0
            && tssa->ops[idx].op1_use >= 0
1279
0
            && (tssa->var_info[tssa->ops[idx].op1_use].type & MAY_BE_REF)) {
1280
0
            return 0;
1281
0
          }
1282
0
          if (!(tssa->var_info[tssa->ops[idx].op1_use].type & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1283
0
            return 0;
1284
0
          }
1285
0
          return 1;
1286
0
        }
1287
0
      }
1288
0
    }
1289
0
  }
1290
0
  return 0;
1291
0
}
1292
1293
typedef struct _zend_tssa {
1294
  zend_ssa        ssa;
1295
  const zend_op **tssa_opcodes;
1296
  int             used_stack;
1297
} zend_tssa;
1298
1299
static const zend_op _nop_opcode = {0};
1300
1301
static uint32_t find_trampoline_num_args(zend_jit_trace_rec *start, zend_jit_trace_rec *p)
1302
0
{
1303
0
  int inline_level = 0, call_level = 0;
1304
1305
0
  p--;
1306
0
  while (p != start) {
1307
0
    if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
1308
0
      if (inline_level == 0) {
1309
0
        if (call_level == 0) {
1310
0
          ZEND_ASSERT(!p->op_array);
1311
0
          return ZEND_JIT_TRACE_NUM_ARGS(p->info);
1312
0
        } else {
1313
0
          call_level--;
1314
0
        }
1315
0
      }
1316
0
    } else if (p->op == ZEND_JIT_TRACE_DO_ICALL) {
1317
0
      if (inline_level == 0) {
1318
0
        call_level++;
1319
0
      }
1320
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
1321
0
      if (inline_level) {
1322
0
        inline_level--;
1323
0
      } else {
1324
0
        return 0;
1325
0
      }
1326
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
1327
0
      inline_level++;
1328
0
    }
1329
0
    p--;
1330
0
  }
1331
0
  return 0;
1332
0
}
1333
1334
static zend_ssa *zend_jit_trace_build_tssa(zend_jit_trace_rec *trace_buffer, uint32_t parent_trace, uint32_t exit_num, zend_script *script, const zend_op_array **op_arrays, int *num_op_arrays_ptr)
1335
0
{
1336
0
  zend_ssa *tssa;
1337
0
  zend_ssa_op *ssa_ops, *op;
1338
0
  zend_ssa_var *ssa_vars;
1339
0
  zend_ssa_var_info *ssa_var_info;
1340
0
  const zend_op_array *op_array;
1341
0
  const zend_op *opline;
1342
0
  const zend_op **ssa_opcodes;
1343
0
  zend_jit_trace_rec *p;
1344
0
  int i, v, idx, len, ssa_ops_count, vars_count, ssa_vars_count;
1345
0
  zend_jit_trace_stack *stack;
1346
0
  uint32_t build_flags = ZEND_SSA_RC_INFERENCE | ZEND_SSA_USE_CV_RESULTS;
1347
0
  uint32_t optimization_level = 0;
1348
0
  int call_level, level, num_op_arrays, used_stack, max_used_stack;
1349
0
  size_t frame_size, stack_top, stack_size, stack_bottom;
1350
0
  zend_jit_op_array_trace_extension *jit_extension;
1351
0
  zend_ssa *ssa;
1352
0
  zend_jit_trace_stack_frame *frame, *top, *call;
1353
0
  zend_ssa_var_info return_value_info;
1354
1355
  /* 1. Count number of TSSA opcodes;
1356
   *    Count number of activation frames;
1357
   *    Calculate size of abstract stack;
1358
   *    Construct regular SSA for involved op_array */
1359
0
  op_array = trace_buffer->op_array;
1360
0
  stack_top = stack_size = zend_jit_trace_frame_size(op_array, 0);
1361
0
  stack_bottom = 0;
1362
0
  p = trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE;
1363
0
  ssa_ops_count = 0;
1364
0
  call_level = 0;
1365
0
  level = 0;
1366
0
  num_op_arrays = 0;
1367
  /* Remember op_array to cleanup */
1368
0
  op_arrays[num_op_arrays++] = op_array;
1369
  /* Build SSA */
1370
0
  ssa = zend_jit_trace_build_ssa(op_array, script);
1371
0
  for (;;p++) {
1372
0
    if (p->op == ZEND_JIT_TRACE_VM) {
1373
0
      if (JIT_G(opt_level) < ZEND_JIT_LEVEL_OPT_FUNC) {
1374
0
        const zend_op *opline = p->opline;
1375
1376
0
        switch (opline->opcode) {
1377
0
          case ZEND_INCLUDE_OR_EVAL:
1378
0
            ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
1379
0
            break;
1380
0
          case ZEND_FETCH_R:
1381
0
          case ZEND_FETCH_W:
1382
0
          case ZEND_FETCH_RW:
1383
0
          case ZEND_FETCH_FUNC_ARG:
1384
0
          case ZEND_FETCH_IS:
1385
0
          case ZEND_FETCH_UNSET:
1386
0
          case ZEND_UNSET_VAR:
1387
0
          case ZEND_ISSET_ISEMPTY_VAR:
1388
0
            if (opline->extended_value & ZEND_FETCH_LOCAL) {
1389
0
              ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
1390
0
            } else if ((opline->extended_value & (ZEND_FETCH_GLOBAL | ZEND_FETCH_GLOBAL_LOCK)) &&
1391
0
                       !op_array->function_name) {
1392
0
              ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
1393
0
            }
1394
0
            break;
1395
0
        }
1396
0
      }
1397
0
      ssa_ops_count += zend_jit_trace_op_len(p->opline);
1398
0
    } else if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
1399
0
      call_level++;
1400
0
      stack_top += zend_jit_trace_frame_size(p->op_array, ZEND_JIT_TRACE_NUM_ARGS(p->info));
1401
0
      if (stack_top > stack_size) {
1402
0
        stack_size = stack_top;
1403
0
      }
1404
0
    } else if (p->op == ZEND_JIT_TRACE_DO_ICALL) {
1405
0
      uint32_t num_args = 0;
1406
0
      if (JIT_G(opt_level) < ZEND_JIT_LEVEL_OPT_FUNC) {
1407
0
        if (p->func
1408
0
         && p->func != (zend_function*)&zend_pass_function
1409
0
         && (zend_string_equals_literal(p->func->common.function_name, "extract")
1410
0
          || zend_string_equals_literal(p->func->common.function_name, "compact")
1411
0
          || zend_string_equals_literal(p->func->common.function_name, "get_defined_vars"))) {
1412
0
          ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
1413
0
        }
1414
0
      }
1415
0
      if (!p->func) {
1416
        /* Find num_args in the corresponding ZEND_JIT_TRACE_INIT_CALL record */
1417
0
        num_args = find_trampoline_num_args(trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE, p);
1418
0
      }
1419
0
      frame_size = zend_jit_trace_frame_size(p->op_array, num_args);
1420
0
      if (call_level == 0) {
1421
0
        if (stack_top + frame_size > stack_size) {
1422
0
          stack_size = stack_top + frame_size;
1423
0
        }
1424
0
      } else {
1425
0
        call_level--;
1426
0
        stack_top -= frame_size;
1427
0
      }
1428
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
1429
0
      op_array = p->op_array;
1430
0
      if (call_level == 0) {
1431
0
        stack_top += zend_jit_trace_frame_size(op_array, 0);
1432
0
        if (stack_top > stack_size) {
1433
0
          stack_size = stack_top;
1434
0
        }
1435
0
      } else {
1436
0
        call_level--;
1437
0
      }
1438
0
      level++;
1439
0
      jit_extension =
1440
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
1441
0
      ssa = &jit_extension->func_info.ssa;
1442
0
      if (ssa->cfg.blocks_count) {
1443
        /* pass */
1444
0
      } else if (num_op_arrays == ZEND_JIT_TRACE_MAX_FUNCS) {
1445
        /* Too many functions in single trace */
1446
0
        *num_op_arrays_ptr = num_op_arrays;
1447
0
        return NULL;
1448
0
      } else {
1449
        /* Remember op_array to cleanup */
1450
0
        op_arrays[num_op_arrays++] = op_array;
1451
        /* Build SSA */
1452
0
        ssa = zend_jit_trace_build_ssa(op_array, script);
1453
0
      }
1454
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
1455
0
      if (level == 0) {
1456
0
        stack_bottom += zend_jit_trace_frame_size(p->op_array, 0);
1457
0
        jit_extension =
1458
0
          (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
1459
0
        ssa = &jit_extension->func_info.ssa;
1460
0
        if (ssa->cfg.blocks_count) {
1461
          /* pass */
1462
0
        } else if (num_op_arrays == ZEND_JIT_TRACE_MAX_FUNCS) {
1463
          /* Too many functions in single trace */
1464
0
          *num_op_arrays_ptr = num_op_arrays;
1465
0
          return NULL;
1466
0
        } else {
1467
          /* Remember op_array to cleanup */
1468
0
          op_arrays[num_op_arrays++] = op_array;
1469
          /* Build SSA */
1470
0
          ssa = zend_jit_trace_build_ssa(op_array, script);
1471
0
        }
1472
0
      } else {
1473
0
        stack_top -= zend_jit_trace_frame_size(op_array, 0);
1474
0
        level--;
1475
0
      }
1476
0
      op_array = p->op_array;
1477
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
1478
0
      break;
1479
0
    }
1480
0
  }
1481
0
  *num_op_arrays_ptr = num_op_arrays;
1482
1483
  /* Allocate space for abstract stack */
1484
0
  JIT_G(current_frame) = frame = (zend_jit_trace_stack_frame*)((char*)zend_arena_alloc(&CG(arena), stack_bottom + stack_size) + stack_bottom);
1485
1486
  /* 2. Construct TSSA */
1487
0
  tssa = zend_arena_calloc(&CG(arena), 1, sizeof(zend_tssa));
1488
0
  tssa->cfg.flags = ZEND_SSA_TSSA;
1489
0
  tssa->cfg.blocks = zend_arena_calloc(&CG(arena), 2, sizeof(zend_basic_block));
1490
0
  tssa->blocks = zend_arena_calloc(&CG(arena), 2, sizeof(zend_ssa_block));
1491
0
  tssa->cfg.predecessors = zend_arena_calloc(&CG(arena), 2, sizeof(int));
1492
1493
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
1494
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
1495
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
1496
0
    tssa->cfg.blocks_count = 2;
1497
0
    tssa->cfg.edges_count = 2;
1498
1499
0
    tssa->cfg.predecessors[0] = 0;
1500
0
    tssa->cfg.predecessors[1] = 1;
1501
1502
0
    tssa->cfg.blocks[0].flags = ZEND_BB_START|ZEND_BB_REACHABLE;
1503
0
    tssa->cfg.blocks[0].successors_count = 1;
1504
0
    tssa->cfg.blocks[0].predecessors_count = 0;
1505
0
    tssa->cfg.blocks[0].successors = tssa->cfg.blocks[0].successors_storage;
1506
0
    tssa->cfg.blocks[0].successors[0] = 1;
1507
1508
0
    tssa->cfg.blocks[1].flags = ZEND_BB_FOLLOW|ZEND_BB_TARGET|ZEND_BB_LOOP_HEADER|ZEND_BB_REACHABLE;
1509
0
    tssa->cfg.blocks[1].len = ssa_ops_count;
1510
0
    tssa->cfg.blocks[1].successors_count = 1;
1511
0
    tssa->cfg.blocks[1].predecessors_count = 2;
1512
0
    tssa->cfg.blocks[1].successors = tssa->cfg.blocks[1].successors_storage;
1513
0
    tssa->cfg.blocks[1].successors[1] = 1;
1514
0
  } else {
1515
0
    tssa->cfg.blocks_count = 1;
1516
0
    tssa->cfg.edges_count = 0;
1517
1518
0
    tssa->cfg.blocks[0].flags = ZEND_BB_START|ZEND_BB_EXIT|ZEND_BB_REACHABLE;
1519
0
    tssa->cfg.blocks[0].len = ssa_ops_count;
1520
0
    tssa->cfg.blocks[0].successors_count = 0;
1521
0
    tssa->cfg.blocks[0].predecessors_count = 0;
1522
0
  }
1523
0
  ((zend_tssa*)tssa)->used_stack = -1;
1524
1525
0
  if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
1526
0
    return tssa;
1527
0
  }
1528
1529
0
  tssa->ops = ssa_ops = zend_arena_alloc(&CG(arena), ssa_ops_count * sizeof(zend_ssa_op));
1530
0
  memset(ssa_ops, -1, ssa_ops_count * sizeof(zend_ssa_op));
1531
0
  ssa_opcodes = zend_arena_calloc(&CG(arena), ssa_ops_count + 1, sizeof(zend_op*));
1532
0
  ((zend_tssa*)tssa)->tssa_opcodes = ssa_opcodes;
1533
0
  ssa_opcodes[ssa_ops_count] = &_nop_opcode;
1534
1535
0
  op_array = trace_buffer->op_array;
1536
0
  if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER) {
1537
0
    ssa_vars_count = op_array->last_var;
1538
0
  } else {
1539
0
    ssa_vars_count = op_array->last_var + op_array->T;
1540
0
  }
1541
0
  stack = frame->stack;
1542
0
  for (i = 0; i < ssa_vars_count; i++) {
1543
0
    SET_STACK_VAR(stack, i, i);
1544
0
  }
1545
1546
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP) {
1547
    // TODO: For tracing, it's possible, to create pseudo Phi functions
1548
    //       at the end of loop, without this additional pass (like LuaJIT) ???
1549
0
    ssa_vars_count = zend_jit_trace_add_phis(trace_buffer, ssa_vars_count, tssa, stack);
1550
0
  } else if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL) {
1551
0
    ssa_vars_count = zend_jit_trace_add_call_phis(trace_buffer, ssa_vars_count, tssa, stack);
1552
0
  } else if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
1553
0
    ssa_vars_count = zend_jit_trace_add_ret_phis(trace_buffer, ssa_vars_count, tssa, stack);
1554
0
  }
1555
1556
0
  p = trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE;
1557
0
  idx = 0;
1558
0
  level = 0;
1559
0
  for (;;p++) {
1560
0
    if (p->op == ZEND_JIT_TRACE_VM) {
1561
0
      opline = p->opline;
1562
0
      ssa_opcodes[idx] = opline;
1563
0
      ssa_vars_count = zend_ssa_rename_op(op_array, opline, idx, build_flags, ssa_vars_count, ssa_ops, (int*)stack);
1564
0
      idx++;
1565
0
      len = zend_jit_trace_op_len(p->opline);
1566
0
      while (len > 1) {
1567
0
        opline++;
1568
0
        ssa_opcodes[idx] = opline;
1569
0
        if (opline->opcode != ZEND_OP_DATA) {
1570
0
          ssa_vars_count = zend_ssa_rename_op(op_array, opline, idx, build_flags, ssa_vars_count, ssa_ops, (int*)stack);
1571
0
        }
1572
0
        idx++;
1573
0
        len--;
1574
0
      }
1575
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
1576
0
      frame = zend_jit_trace_call_frame(frame, op_array, 0);
1577
0
      stack = frame->stack;
1578
0
      op_array = p->op_array;
1579
0
      level++;
1580
0
      if (ssa_vars_count >= ZEND_JIT_TRACE_MAX_SSA_VAR) {
1581
0
        return NULL;
1582
0
      }
1583
0
      ZEND_JIT_TRACE_SET_FIRST_SSA_VAR(p->info, ssa_vars_count);
1584
0
      for (i = 0; i < op_array->last_var; i++) {
1585
0
        SET_STACK_VAR(stack, i, ssa_vars_count++);
1586
0
      }
1587
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
1588
0
      op_array = p->op_array;
1589
0
      frame = zend_jit_trace_ret_frame(frame, op_array);
1590
0
      stack = frame->stack;
1591
0
      if (level == 0) {
1592
0
        if (ssa_vars_count >= ZEND_JIT_TRACE_MAX_SSA_VAR) {
1593
0
          return NULL;
1594
0
        }
1595
0
        ZEND_JIT_TRACE_SET_FIRST_SSA_VAR(p->info, ssa_vars_count);
1596
0
        for (i = 0; i < op_array->last_var + op_array->T; i++) {
1597
0
          SET_STACK_VAR(stack, i, ssa_vars_count++);
1598
0
        }
1599
0
      } else {
1600
0
        level--;
1601
0
      }
1602
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
1603
0
      break;
1604
0
    }
1605
0
  }
1606
1607
0
  op_array = trace_buffer->op_array;
1608
0
  tssa->vars_count = ssa_vars_count;
1609
0
  tssa->vars = ssa_vars = zend_arena_calloc(&CG(arena), tssa->vars_count, sizeof(zend_ssa_var));
1610
0
  if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER) {
1611
0
    vars_count = op_array->last_var;
1612
0
  } else {
1613
0
    vars_count = op_array->last_var + op_array->T;
1614
0
  }
1615
0
  i = 0;
1616
0
  while (i < vars_count) {
1617
0
    ssa_vars[i].var = i;
1618
0
    ssa_vars[i].scc = -1;
1619
0
    ssa_vars[i].definition = -1;
1620
0
    ssa_vars[i].use_chain = -1;
1621
0
    i++;
1622
0
  }
1623
0
  while (i < tssa->vars_count) {
1624
0
    ssa_vars[i].var = -1;
1625
0
    ssa_vars[i].scc = -1;
1626
0
    ssa_vars[i].definition = -1;
1627
0
    ssa_vars[i].use_chain = -1;
1628
0
    i++;
1629
0
  }
1630
1631
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
1632
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
1633
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
1634
    /* Update Phi sources */
1635
0
    zend_ssa_phi *phi = tssa->blocks[1].phis;
1636
1637
0
    while (phi) {
1638
0
      phi->sources[1] = STACK_VAR(stack, phi->var);
1639
0
      ssa_vars[phi->ssa_var].var = phi->var;
1640
0
      ssa_vars[phi->ssa_var].definition_phi = phi;
1641
0
      ssa_vars[phi->sources[0]].phi_use_chain = phi;
1642
0
      ssa_vars[phi->sources[1]].phi_use_chain = phi;
1643
0
      phi = phi->next;
1644
0
    }
1645
0
  }
1646
1647
  /* 3. Compute use-def chains */
1648
0
  idx = (ssa_ops_count - 1);
1649
0
  op = ssa_ops + idx;
1650
0
  while (idx >= 0) {
1651
0
    opline = ssa_opcodes[idx];
1652
0
    if (op->op1_use >= 0) {
1653
0
      op->op1_use_chain = ssa_vars[op->op1_use].use_chain;
1654
0
      ssa_vars[op->op1_use].use_chain = idx;
1655
0
    }
1656
0
    if (op->op2_use >= 0 && op->op2_use != op->op1_use) {
1657
0
      op->op2_use_chain = ssa_vars[op->op2_use].use_chain;
1658
0
      ssa_vars[op->op2_use].use_chain = idx;
1659
0
    }
1660
0
    if (op->result_use >= 0 && op->result_use != op->op1_use && op->result_use != op->op2_use) {
1661
0
      op->res_use_chain = ssa_vars[op->result_use].use_chain;
1662
0
      ssa_vars[op->result_use].use_chain = idx;
1663
0
    }
1664
0
    if (op->op1_def >= 0) {
1665
0
      ssa_vars[op->op1_def].var = EX_VAR_TO_NUM(opline->op1.var);
1666
0
      ssa_vars[op->op1_def].definition = idx;
1667
0
    }
1668
0
    if (op->op2_def >= 0) {
1669
0
      ssa_vars[op->op2_def].var = EX_VAR_TO_NUM(opline->op2.var);
1670
0
      ssa_vars[op->op2_def].definition = idx;
1671
0
    }
1672
0
    if (op->result_def >= 0) {
1673
0
      ssa_vars[op->result_def].var = EX_VAR_TO_NUM(opline->result.var);
1674
0
      ssa_vars[op->result_def].definition = idx;
1675
0
    }
1676
0
    op--;
1677
0
    idx--;
1678
0
  }
1679
1680
  /* 4. Type inference */
1681
0
  op_array = trace_buffer->op_array;
1682
0
  opline = trace_buffer[1].opline;
1683
0
  jit_extension =
1684
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
1685
0
  ssa = &jit_extension->func_info.ssa;
1686
1687
0
  tssa->var_info = ssa_var_info = zend_arena_calloc(&CG(arena), tssa->vars_count, sizeof(zend_ssa_var_info));
1688
1689
0
  if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER) {
1690
0
    i = 0;
1691
0
    while (i < op_array->last_var) {
1692
0
      if (i < op_array->num_args) {
1693
0
        if (ssa->var_info
1694
0
         && zend_jit_trace_copy_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, i, NULL)) {
1695
          /* pass */
1696
0
        } else {
1697
0
          if (ssa->vars) {
1698
0
            ssa_vars[i].no_val = ssa->vars[i].no_val;
1699
0
            ssa_vars[i].alias = ssa->vars[i].alias;
1700
0
          } else {
1701
0
            ssa_vars[i].alias = zend_jit_var_may_alias(op_array, ssa, i);
1702
0
          }
1703
0
          if (op_array->arg_info && i < trace_buffer[1].opline - op_array->opcodes) {
1704
0
            zend_arg_info *arg_info = &op_array->arg_info[i];
1705
0
            zend_class_entry *ce;
1706
0
            uint32_t tmp = zend_fetch_arg_info_type(script, arg_info, &ce);
1707
1708
0
            if (ZEND_ARG_SEND_MODE(arg_info)) {
1709
0
              tmp |= MAY_BE_REF;
1710
0
            }
1711
0
            ssa_var_info[i].type = tmp;
1712
0
            ssa_var_info[i].ce = ce;
1713
0
            ssa_var_info[i].is_instanceof = 1;
1714
0
          } else {
1715
0
            ssa_var_info[i].type = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
1716
0
          }
1717
0
        }
1718
0
      } else {
1719
0
        if (ssa->vars) {
1720
0
          ssa_vars[i].no_val = ssa->vars[i].no_val;
1721
0
          ssa_vars[i].alias = ssa->vars[i].alias;
1722
0
        } else {
1723
0
          ssa_vars[i].alias = zend_jit_var_may_alias(op_array, ssa, i);
1724
0
        }
1725
0
        if (ssa_vars[i].alias == NO_ALIAS) {
1726
0
          ssa_var_info[i].type = MAY_BE_UNDEF;
1727
0
        } else {
1728
0
          ssa_var_info[i].type = MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
1729
0
        }
1730
0
      }
1731
0
      i++;
1732
0
    }
1733
0
  } else {
1734
0
    int parent_vars_count = 0;
1735
0
    zend_jit_trace_stack *parent_stack = NULL;
1736
1737
0
    i = 0;
1738
0
    if (parent_trace) {
1739
0
      parent_vars_count = MIN(zend_jit_traces[parent_trace].exit_info[exit_num].stack_size,
1740
0
        op_array->last_var + op_array->T);
1741
0
      if (parent_vars_count) {
1742
0
        parent_stack =
1743
0
          zend_jit_traces[parent_trace].stack_map +
1744
0
          zend_jit_traces[parent_trace].exit_info[exit_num].stack_offset;
1745
0
      }
1746
0
    }
1747
0
    while (i < op_array->last_var + op_array->T) {
1748
0
      if (!ssa->var_info
1749
0
       || !zend_jit_trace_copy_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, i, opline)) {
1750
0
        if (ssa->vars && i < ssa->vars_count) {
1751
0
          ssa_vars[i].alias = ssa->vars[i].alias;
1752
0
        } else {
1753
0
          ssa_vars[i].alias = zend_jit_var_may_alias(op_array, ssa, i);
1754
0
        }
1755
0
        if (i < op_array->last_var) {
1756
0
          ssa_var_info[i].type = MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
1757
0
        } else {
1758
0
          ssa_var_info[i].type = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
1759
0
        }
1760
0
      }
1761
0
      if (i < parent_vars_count) {
1762
        /* Initialize TSSA variable from parent trace */
1763
0
        uint8_t op_type = STACK_TYPE(parent_stack, i);
1764
1765
0
        if (op_type != IS_UNKNOWN) {
1766
0
          ssa_var_info[i].type &= zend_jit_trace_type_to_info(op_type);
1767
0
          if (!ssa_var_info[i].type
1768
0
           && op_type == IS_UNDEF
1769
0
           && i >= op_array->last_var) {
1770
            // TODO: It's better to use NULL instead of UNDEF for temporary variables
1771
0
            ssa_var_info[i].type |= MAY_BE_UNDEF;
1772
0
          }
1773
0
        }
1774
0
      }
1775
0
      i++;
1776
0
    }
1777
0
  }
1778
1779
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
1780
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
1781
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
1782
    /* Propagate initial value through Phi functions */
1783
0
    zend_ssa_phi *phi = tssa->blocks[1].phis;
1784
1785
0
    while (phi) {
1786
0
      if (!ssa->var_info
1787
0
       || !zend_jit_trace_copy_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, phi->ssa_var, NULL)) {
1788
0
        ssa_vars[phi->ssa_var].alias = ssa_vars[phi->sources[0]].alias;
1789
0
        ssa_var_info[phi->ssa_var].type = ssa_var_info[phi->sources[0]].type;
1790
0
      }
1791
0
      phi = phi->next;
1792
0
    }
1793
0
  }
1794
1795
0
  frame = JIT_G(current_frame);
1796
0
  top = zend_jit_trace_call_frame(frame, op_array, 0);
1797
0
  TRACE_FRAME_INIT(frame, op_array, 0, 0);
1798
0
  TRACE_FRAME_SET_RETURN_SSA_VAR(frame, -1);
1799
0
  frame->used_stack = 0;
1800
0
  memset(&return_value_info, 0, sizeof(return_value_info));
1801
1802
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP) {
1803
0
    max_used_stack = used_stack = 0;
1804
0
  } else {
1805
0
    max_used_stack = used_stack = -1;
1806
0
  }
1807
1808
0
  p = trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE;
1809
0
  idx = 0;
1810
0
  level = 0;
1811
0
  opline = NULL;
1812
0
  for (;;p++) {
1813
0
    if (p->op == ZEND_JIT_TRACE_VM) {
1814
0
      uint8_t orig_op1_type, orig_op2_type, op1_type, op2_type, op3_type;
1815
0
      uint8_t val_type = IS_UNKNOWN;
1816
//      zend_class_entry *op1_ce = NULL;
1817
0
      zend_class_entry *op2_ce = NULL;
1818
1819
0
      opline = p->opline;
1820
1821
0
      op1_type = orig_op1_type = p->op1_type;
1822
0
      op2_type = orig_op2_type = p->op2_type;
1823
0
      op3_type = p->op3_type;
1824
0
      if (op1_type & (IS_TRACE_REFERENCE|IS_TRACE_INDIRECT)) {
1825
0
        op1_type = IS_UNKNOWN;
1826
0
      }
1827
0
      if (op1_type != IS_UNKNOWN) {
1828
0
        op1_type &= ~IS_TRACE_PACKED;
1829
0
      }
1830
0
      if (op2_type & (IS_TRACE_REFERENCE|IS_TRACE_INDIRECT)) {
1831
0
        op2_type = IS_UNKNOWN;
1832
0
      }
1833
0
      if (op3_type & (IS_TRACE_REFERENCE|IS_TRACE_INDIRECT)) {
1834
0
        op3_type = IS_UNKNOWN;
1835
0
      }
1836
1837
0
      if ((p+1)->op == ZEND_JIT_TRACE_OP1_TYPE) {
1838
//        op1_ce = (zend_class_entry*)(p+1)->ce;
1839
0
        p++;
1840
0
      }
1841
0
      if ((p+1)->op == ZEND_JIT_TRACE_OP2_TYPE) {
1842
0
        op2_ce = (zend_class_entry*)(p+1)->ce;
1843
0
        p++;
1844
0
      }
1845
0
      if ((p+1)->op == ZEND_JIT_TRACE_VAL_INFO) {
1846
0
        val_type = (p+1)->op1_type;
1847
0
        p++;
1848
0
      }
1849
1850
0
      switch (opline->opcode) {
1851
0
        case ZEND_ASSIGN_OP:
1852
0
          if (opline->extended_value == ZEND_POW
1853
0
           || opline->extended_value == ZEND_DIV) {
1854
            // TODO: check for division by zero ???
1855
0
            break;
1856
0
          }
1857
0
          if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
1858
0
            break;
1859
0
          }
1860
0
          ADD_OP1_TRACE_GUARD();
1861
0
          ADD_OP2_TRACE_GUARD();
1862
0
          break;
1863
0
        case ZEND_ASSIGN_DIM_OP:
1864
0
          if (opline->result_type != IS_UNUSED) {
1865
0
            break;
1866
0
          }
1867
0
          if (op3_type != IS_UNKNOWN
1868
0
           && !zend_jit_supported_binary_op(
1869
0
              opline->extended_value, MAY_BE_ANY, (1<<op3_type))) {
1870
0
            break;
1871
0
          }
1872
0
          ZEND_FALLTHROUGH;
1873
0
        case ZEND_ASSIGN_DIM:
1874
0
          if (opline->opcode == ZEND_ASSIGN_DIM
1875
0
           && opline->op1_type == IS_CV
1876
0
           && (opline+1)->op1_type == IS_CV
1877
0
           && (opline+1)->op1.var == opline->op1.var) {
1878
            /* skip $a[x] = $a; */
1879
0
            break;
1880
0
          }
1881
0
          if (opline->op1_type == IS_CV || opline->opcode == ZEND_ASSIGN_DIM_OP) {
1882
0
            ADD_OP1_DATA_TRACE_GUARD();
1883
0
          }
1884
0
          ADD_OP2_TRACE_GUARD();
1885
0
          ADD_OP1_TRACE_GUARD();
1886
0
          if (op1_type == IS_ARRAY
1887
0
           && ((opline->op2_type == IS_CONST
1888
0
             && Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_LONG)
1889
0
            || (opline->op2_type != IS_CONST
1890
0
             && op2_type == IS_LONG))) {
1891
1892
0
            if (!(orig_op1_type & IS_TRACE_PACKED)) {
1893
0
              zend_ssa_var_info *info = &tssa->var_info[tssa->ops[idx].op1_use];
1894
1895
0
              if (MAY_BE_PACKED(info->type) && MAY_BE_HASH(info->type)
1896
0
               && (info->type & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
1897
0
                info->type |= MAY_BE_PACKED_GUARD;
1898
0
                info->type &= ~MAY_BE_ARRAY_PACKED;
1899
0
              }
1900
0
            } else if (opline->opcode == ZEND_ASSIGN_DIM_OP
1901
0
                && val_type != IS_UNKNOWN
1902
0
                && val_type != IS_UNDEF) {
1903
0
              zend_ssa_var_info *info = &tssa->var_info[tssa->ops[idx].op1_use];
1904
1905
0
              if (MAY_BE_PACKED(info->type) && MAY_BE_HASH(info->type)
1906
0
               && (info->type & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
1907
0
                info->type |= MAY_BE_PACKED_GUARD;
1908
0
                info->type &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
1909
0
              }
1910
0
            }
1911
0
          }
1912
0
          break;
1913
0
        case ZEND_ASSIGN_OBJ_OP:
1914
0
          if (opline->extended_value == ZEND_POW
1915
0
           || opline->extended_value == ZEND_DIV) {
1916
            // TODO: check for division by zero ???
1917
0
            break;
1918
0
          }
1919
0
          if (opline->result_type != IS_UNUSED) {
1920
0
            break;
1921
0
          }
1922
0
          ZEND_FALLTHROUGH;
1923
0
        case ZEND_ASSIGN_OBJ:
1924
0
        case ZEND_PRE_INC_OBJ:
1925
0
        case ZEND_PRE_DEC_OBJ:
1926
0
        case ZEND_POST_INC_OBJ:
1927
0
        case ZEND_POST_DEC_OBJ:
1928
0
          if (opline->op2_type != IS_CONST
1929
0
           || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
1930
0
           || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
1931
0
            break;
1932
0
          }
1933
0
          if (opline->opcode == ZEND_ASSIGN_OBJ
1934
0
           || opline->opcode == ZEND_ASSIGN_OBJ_OP) {
1935
0
            if (opline->op1_type == IS_CV
1936
0
             && (opline+1)->op1_type == IS_CV
1937
0
             && (opline+1)->op1.var == opline->op1.var) {
1938
              /* skip $a->prop += $a; */
1939
0
              break;
1940
0
            }
1941
0
            ADD_OP1_DATA_TRACE_GUARD();
1942
0
          }
1943
0
          ADD_OP1_TRACE_GUARD();
1944
0
          break;
1945
0
        case ZEND_CONCAT:
1946
0
        case ZEND_FAST_CONCAT:
1947
0
          if ((opline->op1_type == IS_CONST || orig_op1_type == IS_STRING)
1948
0
           && (opline->op2_type == IS_CONST || orig_op2_type == IS_STRING)) {
1949
0
            ADD_OP2_TRACE_GUARD();
1950
0
            ADD_OP1_TRACE_GUARD();
1951
0
          }
1952
0
          break;
1953
0
        case ZEND_ADD:
1954
0
        case ZEND_SUB:
1955
0
        case ZEND_MUL:
1956
//        case ZEND_DIV: // TODO: check for division by zero ???
1957
0
          if (orig_op1_type == IS_UNDEF || orig_op2_type == IS_UNDEF) {
1958
0
            break;
1959
0
          }
1960
0
          ZEND_FALLTHROUGH;
1961
0
        case ZEND_IS_EQUAL:
1962
0
        case ZEND_IS_NOT_EQUAL:
1963
0
        case ZEND_IS_SMALLER:
1964
0
        case ZEND_IS_SMALLER_OR_EQUAL:
1965
0
        case ZEND_CASE:
1966
0
        case ZEND_IS_IDENTICAL:
1967
0
        case ZEND_IS_NOT_IDENTICAL:
1968
0
        case ZEND_CASE_STRICT:
1969
0
        case ZEND_BW_OR:
1970
0
        case ZEND_BW_AND:
1971
0
        case ZEND_BW_XOR:
1972
0
        case ZEND_SL:
1973
0
        case ZEND_SR:
1974
0
        case ZEND_MOD:
1975
0
          ADD_OP2_TRACE_GUARD();
1976
0
          ZEND_FALLTHROUGH;
1977
0
        case ZEND_ECHO:
1978
0
        case ZEND_STRLEN:
1979
0
        case ZEND_COUNT:
1980
0
        case ZEND_QM_ASSIGN:
1981
0
        case ZEND_FE_RESET_R:
1982
0
          ADD_OP1_TRACE_GUARD();
1983
0
          break;
1984
0
        case ZEND_FE_FETCH_R:
1985
0
          ADD_OP1_TRACE_GUARD();
1986
0
          if (op1_type == IS_ARRAY && (orig_op1_type & ~IS_TRACE_PACKED) == IS_ARRAY) {
1987
1988
0
            zend_ssa_var_info *info = &tssa->var_info[tssa->ops[idx].op1_use];
1989
1990
0
            if (MAY_BE_PACKED(info->type) && MAY_BE_HASH(info->type)
1991
0
             && (info->type & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
1992
0
              info->type |= MAY_BE_PACKED_GUARD;
1993
0
              if (orig_op1_type & IS_TRACE_PACKED) {
1994
0
                info->type &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
1995
0
              } else {
1996
0
                info->type &= ~MAY_BE_ARRAY_PACKED;
1997
0
              }
1998
0
            }
1999
0
          }
2000
0
          break;
2001
0
        case ZEND_VERIFY_RETURN_TYPE:
2002
0
          if (opline->op1_type == IS_UNUSED) {
2003
            /* Always throws */
2004
0
            break;
2005
0
          }
2006
0
          if (opline->op1_type == IS_CONST) {
2007
            /* TODO Different instruction format, has return value */
2008
0
            break;
2009
0
          }
2010
0
          if (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
2011
            /* Not worth bothering with */
2012
0
            break;
2013
0
          }
2014
0
          ADD_OP1_TRACE_GUARD();
2015
0
          break;
2016
0
        case ZEND_FETCH_DIM_FUNC_ARG:
2017
0
          if (!frame
2018
0
           || !frame->call
2019
0
           || !frame->call->func
2020
0
           || !TRACE_FRAME_IS_LAST_SEND_BY_VAL(frame->call)) {
2021
0
            break;
2022
0
          }
2023
0
          ADD_OP2_TRACE_GUARD();
2024
0
          ADD_OP1_TRACE_GUARD();
2025
0
          break;
2026
0
        case ZEND_PRE_INC:
2027
0
        case ZEND_PRE_DEC:
2028
0
        case ZEND_POST_INC:
2029
0
        case ZEND_POST_DEC:
2030
0
          if (opline->op1_type != IS_CV) {
2031
0
            break;
2032
0
          }
2033
0
          ADD_OP1_TRACE_GUARD();
2034
0
          break;
2035
0
        case ZEND_ASSIGN:
2036
0
          if (opline->op1_type != IS_CV) {
2037
0
            break;
2038
0
          }
2039
0
          ADD_OP2_TRACE_GUARD();
2040
0
          if (op1_type != IS_UNKNOWN
2041
0
           && (tssa->var_info[tssa->ops[idx].op1_use].type & MAY_BE_REF)) {
2042
0
            ADD_OP1_TRACE_GUARD();
2043
0
          }
2044
0
          break;
2045
0
        case ZEND_CAST:
2046
0
          if (opline->extended_value != op1_type) {
2047
0
            break;
2048
0
          }
2049
0
          ADD_OP1_TRACE_GUARD();
2050
0
          break;
2051
0
        case ZEND_JMPZ:
2052
0
        case ZEND_JMPNZ:
2053
0
        case ZEND_JMPZ_EX:
2054
0
        case ZEND_JMPNZ_EX:
2055
0
        case ZEND_BOOL:
2056
0
        case ZEND_BOOL_NOT:
2057
0
          ADD_OP1_TRACE_GUARD();
2058
0
          break;
2059
0
        case ZEND_ISSET_ISEMPTY_CV:
2060
0
          if ((opline->extended_value & ZEND_ISEMPTY)) {
2061
            // TODO: support for empty() ???
2062
0
            break;
2063
0
          }
2064
0
          ADD_OP1_TRACE_GUARD();
2065
0
          break;
2066
0
        case ZEND_IN_ARRAY:
2067
0
          if (opline->op1_type == IS_VAR || opline->op1_type == IS_TMP_VAR) {
2068
0
            break;
2069
0
          }
2070
0
          ADD_OP1_TRACE_GUARD();
2071
0
          break;
2072
0
        case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2073
0
          if ((opline->extended_value & ZEND_ISEMPTY)) {
2074
            // TODO: support for empty() ???
2075
0
            break;
2076
0
          }
2077
0
          ZEND_FALLTHROUGH;
2078
0
        case ZEND_FETCH_DIM_R:
2079
0
        case ZEND_FETCH_DIM_IS:
2080
0
        case ZEND_FETCH_LIST_R:
2081
0
          ADD_OP1_TRACE_GUARD();
2082
0
          ADD_OP2_TRACE_GUARD();
2083
2084
0
          if (op1_type == IS_ARRAY
2085
0
           && opline->op1_type != IS_CONST
2086
0
           && ((opline->op2_type == IS_CONST
2087
0
             && Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_LONG)
2088
0
            || (opline->op2_type != IS_CONST
2089
0
             && op2_type == IS_LONG))) {
2090
2091
0
            zend_ssa_var_info *info = &tssa->var_info[tssa->ops[idx].op1_use];
2092
2093
0
            if (MAY_BE_PACKED(info->type) && MAY_BE_HASH(info->type)
2094
0
             && (info->type & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
2095
0
              info->type |= MAY_BE_PACKED_GUARD;
2096
0
              if (orig_op1_type & IS_TRACE_PACKED) {
2097
0
                info->type &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
2098
0
              } else {
2099
0
                info->type &= ~MAY_BE_ARRAY_PACKED;
2100
0
              }
2101
0
            }
2102
0
          }
2103
0
          break;
2104
0
        case ZEND_FETCH_DIM_W:
2105
0
        case ZEND_FETCH_DIM_RW:
2106
//        case ZEND_FETCH_DIM_UNSET:
2107
0
        case ZEND_FETCH_LIST_W:
2108
0
          if (opline->op1_type != IS_CV
2109
0
           && (orig_op1_type == IS_UNKNOWN
2110
0
            || !(orig_op1_type & IS_TRACE_INDIRECT))) {
2111
0
            break;
2112
0
          }
2113
0
          ADD_OP1_TRACE_GUARD();
2114
0
          ADD_OP2_TRACE_GUARD();
2115
0
          if (op1_type == IS_ARRAY
2116
0
           && !(orig_op1_type & IS_TRACE_PACKED)
2117
0
           && ((opline->op2_type == IS_CONST
2118
0
             && Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_LONG)
2119
0
            || (opline->op2_type != IS_CONST
2120
0
             && op2_type == IS_LONG))) {
2121
2122
0
            zend_ssa_var_info *info = &tssa->var_info[tssa->ops[idx].op1_use];
2123
2124
0
            if (MAY_BE_PACKED(info->type) && MAY_BE_HASH(info->type)
2125
0
             && (info->type & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
2126
0
              info->type |= MAY_BE_PACKED_GUARD;
2127
0
              info->type &= ~MAY_BE_ARRAY_PACKED;
2128
0
            }
2129
0
          }
2130
0
          break;
2131
0
        case ZEND_SEND_VAL_EX:
2132
0
        case ZEND_SEND_VAR_EX:
2133
0
        case ZEND_SEND_VAR_NO_REF_EX:
2134
0
          if (opline->op2_type == IS_CONST) {
2135
            /* Named parameters not supported in JIT */
2136
0
            break;
2137
0
          }
2138
0
          if (opline->op2.num > MAX_ARG_FLAG_NUM) {
2139
0
            goto propagate_arg;
2140
0
          }
2141
0
          ZEND_FALLTHROUGH;
2142
0
        case ZEND_SEND_VAL:
2143
0
        case ZEND_SEND_VAR:
2144
0
        case ZEND_SEND_VAR_NO_REF:
2145
0
        case ZEND_SEND_FUNC_ARG:
2146
0
          if (opline->op2_type == IS_CONST) {
2147
            /* Named parameters not supported in JIT */
2148
0
            break;
2149
0
          }
2150
0
          ADD_OP1_TRACE_GUARD();
2151
0
propagate_arg:
2152
          /* Propagate argument type */
2153
0
          if (frame->call
2154
0
           && frame->call->func
2155
0
           && frame->call->func->type == ZEND_USER_FUNCTION
2156
0
           && opline->op2.num <= frame->call->func->op_array.num_args) {
2157
0
            uint32_t info;
2158
2159
0
            if (opline->op1_type == IS_CONST) {
2160
0
              info = _const_op_type(RT_CONSTANT(opline, opline->op1));
2161
0
            } else {
2162
0
              ZEND_ASSERT(ssa_ops[idx].op1_use >= 0);
2163
0
              info = ssa_var_info[ssa_ops[idx].op1_use].type & ~MAY_BE_GUARD;
2164
0
            }
2165
0
            if (frame->call->func->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
2166
0
              zend_arg_info *arg_info;
2167
2168
0
              ZEND_ASSERT(frame->call->func->op_array.arg_info);
2169
0
              arg_info = &frame->call->func->op_array.arg_info[opline->op2.num - 1];
2170
0
              if (ZEND_TYPE_IS_SET(arg_info->type)) {
2171
0
                zend_class_entry *ce;
2172
0
                uint32_t tmp = zend_fetch_arg_info_type(script, arg_info, &ce);
2173
0
                info &= tmp;
2174
0
                if (!info) {
2175
0
                  break;
2176
0
                }
2177
0
              }
2178
0
            }
2179
0
            if (opline->op1_type == IS_CV && (info & MAY_BE_RC1)) {
2180
0
              info |= MAY_BE_RCN;
2181
0
            }
2182
0
            if (info & MAY_BE_UNDEF) {
2183
0
              info |= MAY_BE_NULL;
2184
0
              info &= ~MAY_BE_UNDEF;
2185
0
            }
2186
0
            if (ARG_SHOULD_BE_SENT_BY_REF(frame->call->func, opline->op2.num)) {
2187
0
              info |= MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_KEY_ANY;
2188
0
            }
2189
0
            SET_STACK_INFO(frame->call->stack, opline->op2.num - 1, info);
2190
0
          }
2191
0
          break;
2192
0
        case ZEND_RETURN:
2193
0
          ADD_OP1_TRACE_GUARD();
2194
          /* Propagate return value types */
2195
0
          if (opline->op1_type == IS_UNUSED) {
2196
0
            return_value_info.type = MAY_BE_NULL;
2197
0
          } else if (opline->op1_type == IS_CONST) {
2198
0
            return_value_info.type = _const_op_type(RT_CONSTANT(opline, opline->op1));
2199
0
          } else {
2200
0
            ZEND_ASSERT(ssa_ops[idx].op1_use >= 0);
2201
0
            return_value_info = ssa_var_info[ssa_ops[idx].op1_use];
2202
0
            if (return_value_info.type & MAY_BE_UNDEF) {
2203
0
              return_value_info.type &= ~MAY_BE_UNDEF;
2204
0
              return_value_info.type |= MAY_BE_NULL;
2205
0
            }
2206
0
            if (return_value_info.type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2207
              /* CVs are going to be destructed and the reference-counter
2208
                 of return value may be decremented to 1 */
2209
0
              return_value_info.type |= MAY_BE_RC1;
2210
0
            }
2211
0
            return_value_info.type &= ~MAY_BE_GUARD;
2212
0
          }
2213
0
          break;
2214
0
        case ZEND_CHECK_FUNC_ARG:
2215
0
          if (!frame
2216
0
           || !frame->call
2217
0
           || !frame->call->func) {
2218
0
            break;
2219
0
          }
2220
0
          if (opline->op2_type == IS_CONST
2221
0
           || opline->op2.num > MAX_ARG_FLAG_NUM) {
2222
            /* Named parameters not supported in JIT */
2223
0
            TRACE_FRAME_SET_LAST_SEND_UNKNOWN(frame->call);
2224
0
            break;
2225
0
          }
2226
0
          if (ARG_SHOULD_BE_SENT_BY_REF(frame->call->func, opline->op2.num)) {
2227
0
            TRACE_FRAME_SET_LAST_SEND_BY_REF(frame->call);
2228
0
          } else {
2229
0
            TRACE_FRAME_SET_LAST_SEND_BY_VAL(frame->call);
2230
0
          }
2231
0
          break;
2232
0
        case ZEND_FETCH_OBJ_FUNC_ARG:
2233
0
          if (!frame
2234
0
           || !frame->call
2235
0
           || !frame->call->func
2236
0
           || !TRACE_FRAME_IS_LAST_SEND_BY_VAL(frame->call)) {
2237
0
            break;
2238
0
          }
2239
0
          ZEND_FALLTHROUGH;
2240
0
        case ZEND_FETCH_OBJ_R:
2241
0
        case ZEND_FETCH_OBJ_IS:
2242
0
        case ZEND_FETCH_OBJ_W:
2243
0
          if (opline->op2_type != IS_CONST
2244
0
           || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
2245
0
           || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
2246
0
            break;
2247
0
          }
2248
0
          if (opline->op1_type != IS_UNUSED && op1_type == IS_OBJECT) {
2249
0
            ADD_OP1_TRACE_GUARD();
2250
0
          }
2251
0
          break;
2252
0
        case ZEND_INIT_METHOD_CALL:
2253
0
          if (opline->op2_type != IS_CONST
2254
0
           || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING) {
2255
0
            break;
2256
0
          }
2257
0
          ADD_OP1_TRACE_GUARD();
2258
0
          break;
2259
0
        case ZEND_INIT_DYNAMIC_CALL:
2260
0
          if (orig_op2_type == IS_OBJECT && op2_ce == zend_ce_closure) {
2261
0
            ADD_OP2_TRACE_GUARD();
2262
0
          }
2263
0
          break;
2264
0
        case ZEND_SEND_ARRAY:
2265
0
        case ZEND_SEND_UNPACK:
2266
0
        case ZEND_CHECK_UNDEF_ARGS:
2267
0
        case ZEND_INCLUDE_OR_EVAL:
2268
0
          max_used_stack = used_stack = -1;
2269
0
          break;
2270
0
        case ZEND_TYPE_CHECK:
2271
0
          if (opline->extended_value == MAY_BE_RESOURCE) {
2272
            // TODO: support for is_resource() ???
2273
0
            break;
2274
0
          }
2275
0
          if (op1_type != IS_UNKNOWN
2276
0
           && (opline->extended_value == (1 << op1_type)
2277
0
            || opline->extended_value == MAY_BE_ANY - (1 << op1_type))) {
2278
            /* add guards only for exact checks, to avoid code duplication */
2279
0
            ADD_OP1_TRACE_GUARD();
2280
0
          }
2281
0
          break;
2282
0
        case ZEND_ROPE_INIT:
2283
0
        case ZEND_ROPE_ADD:
2284
0
        case ZEND_ROPE_END:
2285
0
          if (op2_type == IS_STRING) {
2286
0
            ADD_OP2_TRACE_GUARD();
2287
0
          }
2288
0
          break;
2289
0
        default:
2290
0
          break;
2291
0
      }
2292
0
      len = zend_jit_trace_op_len(opline);
2293
0
      if (ssa->var_info) {
2294
        /* Add statically inferred ranges */
2295
0
        if (ssa_ops[idx].op1_def >= 0) {
2296
0
          zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op1_def);
2297
0
        }
2298
0
        if (ssa_ops[idx].op2_def >= 0) {
2299
0
          zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op2_def);
2300
0
        }
2301
0
        if (ssa_ops[idx].result_def >= 0) {
2302
0
          zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].result_def);
2303
0
        }
2304
0
        if (len == 2 && (opline+1)->opcode == ZEND_OP_DATA) {
2305
0
          if (ssa_ops[idx+1].op1_def >= 0) {
2306
0
            zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx+1].op1_def);
2307
0
          }
2308
0
          if (ssa_ops[idx+1].op2_def >= 0) {
2309
0
            zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx+1].op2_def);
2310
0
          }
2311
0
          if (ssa_ops[idx+1].result_def >= 0) {
2312
0
            zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx+1].result_def);
2313
0
          }
2314
0
        }
2315
0
      } else {
2316
0
        if (ssa_ops[idx].op1_def >= 0) {
2317
0
          ssa_vars[ssa_ops[idx].op1_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM(opline->op1.var));
2318
0
          if (ssa_ops[idx].op1_use < 0 || !(ssa_var_info[ssa_ops[idx].op1_use].type & MAY_BE_REF)) {
2319
0
            zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx].op1_def);
2320
0
          }
2321
0
        }
2322
0
        if (ssa_ops[idx].op2_def >= 0) {
2323
0
          ssa_vars[ssa_ops[idx].op2_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM(opline->op2.var));
2324
0
          if (ssa_ops[idx].op2_use < 0 || !(ssa_var_info[ssa_ops[idx].op2_use].type & MAY_BE_REF)) {
2325
0
            zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx].op2_def);
2326
0
          }
2327
0
        }
2328
0
        if (ssa_ops[idx].result_def >= 0) {
2329
0
          ssa_vars[ssa_ops[idx].result_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM(opline->result.var));
2330
0
          if (ssa_ops[idx].result_use < 0 || !(ssa_var_info[ssa_ops[idx].result_use].type & MAY_BE_REF)) {
2331
0
            zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx].result_def);
2332
0
          }
2333
0
        }
2334
0
        if (len == 2 && (opline+1)->opcode == ZEND_OP_DATA) {
2335
0
          if (ssa_ops[idx+1].op1_def >= 0) {
2336
0
            ssa_vars[ssa_ops[idx+1].op1_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM((opline+1)->op1.var));
2337
0
            if (ssa_ops[idx+1].op1_use < 0 || !(ssa_var_info[ssa_ops[idx+1].op1_use].type & MAY_BE_REF)) {
2338
0
              zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx+1].op1_def);
2339
0
            }
2340
0
          }
2341
0
          if (ssa_ops[idx+1].op2_def >= 0) {
2342
0
            ssa_vars[ssa_ops[idx+1].op2_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM((opline+1)->op2.var));
2343
0
            if (ssa_ops[idx+1].op2_use < 0 || !(ssa_var_info[ssa_ops[idx+1].op2_use].type & MAY_BE_REF)) {
2344
0
              zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx+1].op2_def);
2345
0
            }
2346
0
          }
2347
0
          if (ssa_ops[idx+1].result_def >= 0) {
2348
0
            ssa_vars[ssa_ops[idx+1].result_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM((opline+1)->result.var));
2349
0
            if (ssa_ops[idx+1].result_use < 0 || !(ssa_var_info[ssa_ops[idx+1].result_use].type & MAY_BE_REF)) {
2350
0
              zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx+1].result_def);
2351
0
            }
2352
0
          }
2353
0
        }
2354
0
      }
2355
0
      if (opline->opcode == ZEND_RECV_INIT
2356
0
       && !(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
2357
        /* RECV_INIT always copy the constant */
2358
0
        ssa_var_info[ssa_ops[idx].result_def].type = _const_op_type(RT_CONSTANT(opline, opline->op2));
2359
0
      } else if ((opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW)
2360
0
       && ssa_opcodes[idx + 1] == ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value)) {
2361
0
        if (ssa_ops[idx].op2_use >= 0 && ssa_ops[idx].op2_def >= 0) {
2362
0
          ssa_var_info[ssa_ops[idx].op2_def] = ssa_var_info[ssa_ops[idx].op2_use];
2363
0
        }
2364
0
      } else {
2365
0
        if (zend_update_type_info(op_array, tssa, script, (zend_op*)opline, ssa_ops + idx, ssa_opcodes, optimization_level) == FAILURE) {
2366
          // TODO:
2367
0
          assert(0);
2368
0
        }
2369
0
        if (opline->opcode == ZEND_ASSIGN_DIM_OP
2370
0
         && ssa_ops[idx].op1_def >= 0
2371
0
         && op1_type == IS_ARRAY
2372
0
         && (orig_op1_type & IS_TRACE_PACKED)
2373
0
         && val_type != IS_UNKNOWN
2374
0
         && val_type != IS_UNDEF
2375
0
         && ((opline->op2_type == IS_CONST
2376
0
           && Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_LONG)
2377
0
          || (opline->op2_type != IS_CONST
2378
0
           && op2_type == IS_LONG))) {
2379
0
          zend_ssa_var_info *info = &ssa_var_info[ssa_ops[idx].op1_def];
2380
2381
0
          info->type &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
2382
0
        }
2383
0
      }
2384
0
      if (ssa->var_info) {
2385
        /* Add statically inferred restrictions */
2386
0
        if (ssa_ops[idx].op1_def >= 0) {
2387
0
          if (opline->opcode == ZEND_SEND_VAR_EX
2388
0
           && frame
2389
0
           && frame->call
2390
0
           && frame->call->func
2391
0
           && !ARG_SHOULD_BE_SENT_BY_REF(frame->call->func, opline->op2.num)) {
2392
0
            ssa_var_info[ssa_ops[idx].op1_def] = ssa_var_info[ssa_ops[idx].op1_use];
2393
0
            ssa_var_info[ssa_ops[idx].op1_def].type &= ~MAY_BE_GUARD;
2394
0
            if (ssa_var_info[ssa_ops[idx].op1_def].type & MAY_BE_RC1) {
2395
0
              ssa_var_info[ssa_ops[idx].op1_def].type |= MAY_BE_RCN;
2396
0
            }
2397
0
          } else {
2398
0
            zend_jit_trace_restrict_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op1_def);
2399
0
          }
2400
0
        }
2401
0
        if (ssa_ops[idx].op2_def >= 0) {
2402
0
          if ((opline->opcode != ZEND_FE_FETCH_R && opline->opcode != ZEND_FE_FETCH_RW)
2403
0
           || ssa_opcodes[idx + 1] != ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value)) {
2404
0
            zend_jit_trace_restrict_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op2_def);
2405
0
          }
2406
0
        }
2407
0
        if (ssa_ops[idx].result_def >= 0) {
2408
0
          zend_jit_trace_restrict_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].result_def);
2409
0
        }
2410
0
      }
2411
0
      idx++;
2412
0
      while (len > 1) {
2413
0
        opline++;
2414
0
        if (opline->opcode != ZEND_OP_DATA) {
2415
0
          if (ssa->var_info) {
2416
            /* Add statically inferred ranges */
2417
0
            if (ssa_ops[idx].op1_def >= 0) {
2418
0
              zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op1_def);
2419
0
            }
2420
0
            if (ssa_ops[idx].op2_def >= 0) {
2421
0
              zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op2_def);
2422
0
            }
2423
0
            if (ssa_ops[idx].result_def >= 0) {
2424
0
              zend_jit_trace_copy_ssa_var_range(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].result_def);
2425
0
            }
2426
0
          } else {
2427
0
            if (ssa_ops[idx].op1_def >= 0) {
2428
0
              ssa_vars[ssa_ops[idx].op1_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM(opline->op1.var));
2429
0
              if (ssa_ops[idx].op1_use < 0 || !(ssa_var_info[ssa_ops[idx].op1_use].type & MAY_BE_REF)) {
2430
0
                zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx].op1_def);
2431
0
              }
2432
0
            }
2433
0
            if (ssa_ops[idx].op2_def >= 0) {
2434
0
              ssa_vars[ssa_ops[idx].op2_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM(opline->op2.var));
2435
0
              if (ssa_ops[idx].op2_use < 0 || !(ssa_var_info[ssa_ops[idx].op2_use].type & MAY_BE_REF)) {
2436
0
                zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx].op2_def);
2437
0
              }
2438
0
            }
2439
0
            if (ssa_ops[idx].result_def >= 0) {
2440
0
              ssa_vars[ssa_ops[idx].result_def].alias = zend_jit_var_may_alias(op_array, ssa, EX_VAR_TO_NUM(opline->result.var));
2441
0
              if (ssa_ops[idx].result_use < 0 || !(ssa_var_info[ssa_ops[idx].result_use].type & MAY_BE_REF)) {
2442
0
                zend_jit_trace_propagate_range(op_array, ssa_opcodes, tssa, ssa_ops[idx].result_def);
2443
0
              }
2444
0
            }
2445
0
          }
2446
0
          if (opline->opcode == ZEND_RECV_INIT
2447
0
           && !(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
2448
            /* RECV_INIT always copy the constant */
2449
0
            ssa_var_info[ssa_ops[idx].result_def].type = _const_op_type(RT_CONSTANT(opline, opline->op2));
2450
0
          } else {
2451
0
            if (zend_update_type_info(op_array, tssa, script, (zend_op*)opline, ssa_ops + idx, ssa_opcodes, optimization_level) == FAILURE) {
2452
              // TODO:
2453
0
              assert(0);
2454
0
            }
2455
0
          }
2456
0
        }
2457
0
        if (ssa->var_info) {
2458
          /* Add statically inferred restrictions */
2459
0
          if (ssa_ops[idx].op1_def >= 0) {
2460
0
            zend_jit_trace_restrict_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op1_def);
2461
0
          }
2462
0
          if (ssa_ops[idx].op2_def >= 0) {
2463
0
            zend_jit_trace_restrict_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].op2_def);
2464
0
          }
2465
0
          if (ssa_ops[idx].result_def >= 0) {
2466
0
            zend_jit_trace_restrict_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, ssa_ops[idx].result_def);
2467
0
          }
2468
0
        }
2469
0
        idx++;
2470
0
        len--;
2471
0
      }
2472
2473
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
2474
0
      op_array = p->op_array;
2475
0
      jit_extension =
2476
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
2477
0
      ssa = &jit_extension->func_info.ssa;
2478
2479
0
      call = frame->call;
2480
0
      if (!call) {
2481
        /* Trace missed INIT_FCALL opcode */
2482
0
        call = top;
2483
0
        TRACE_FRAME_INIT(call, op_array, 0, 0);
2484
0
        call->used_stack = 0;
2485
0
        top = zend_jit_trace_call_frame(top, op_array, 0);
2486
0
      } else {
2487
0
        ZEND_ASSERT(&call->func->op_array == op_array);
2488
0
      }
2489
0
      frame->call = call->prev;
2490
0
      call->prev = frame;
2491
0
      TRACE_FRAME_SET_RETURN_SSA_VAR(call, find_return_ssa_var(p - 1, ssa_ops + (idx - 1)));
2492
0
      frame = call;
2493
2494
0
      level++;
2495
0
      i = 0;
2496
0
      v = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
2497
0
      while (i < op_array->last_var) {
2498
0
        ssa_vars[v].var = i;
2499
0
        if (i < op_array->num_args) {
2500
0
          if (ssa->var_info
2501
0
           && zend_jit_trace_copy_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, v, NULL)) {
2502
            /* pass */
2503
0
          } else {
2504
0
            ssa_vars[v].alias = zend_jit_var_may_alias(op_array, ssa, i);
2505
0
            if (op_array->arg_info) {
2506
0
              zend_arg_info *arg_info = &op_array->arg_info[i];
2507
0
              zend_class_entry *ce;
2508
0
              uint32_t tmp = zend_fetch_arg_info_type(script, arg_info, &ce);
2509
2510
0
              if (ZEND_ARG_SEND_MODE(arg_info)) {
2511
0
                tmp |= MAY_BE_REF;
2512
0
              }
2513
0
              ssa_var_info[v].type = tmp;
2514
0
              ssa_var_info[v].ce = ce;
2515
0
              ssa_var_info[v].is_instanceof = 1;
2516
0
            } else {
2517
0
              ssa_var_info[v].type = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2518
0
            }
2519
0
          }
2520
0
        } else {
2521
0
          if (ssa->vars) {
2522
0
            ssa_vars[v].no_val = ssa->vars[i].no_val;
2523
0
            ssa_vars[v].alias = ssa->vars[i].alias;
2524
0
          } else {
2525
0
            ssa_vars[v].alias = zend_jit_var_may_alias(op_array, ssa, i);
2526
0
          }
2527
0
          if (ssa_vars[v].alias == NO_ALIAS) {
2528
0
            ssa_var_info[v].type = MAY_BE_UNDEF;
2529
0
          } else {
2530
0
            ssa_var_info[v].type = MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2531
0
          }
2532
0
        }
2533
0
        if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)
2534
0
         && i < op_array->num_args) {
2535
          /* Propagate argument type */
2536
0
          ssa_var_info[v].type &= STACK_INFO(frame->stack, i);
2537
0
        }
2538
0
        i++;
2539
0
        v++;
2540
0
      }
2541
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
2542
0
      op_array = p->op_array;
2543
0
      jit_extension =
2544
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
2545
0
      ssa = &jit_extension->func_info.ssa;
2546
0
      if (level == 0) {
2547
0
        i = 0;
2548
0
        v = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
2549
0
        while (i < op_array->last_var) {
2550
0
          ssa_vars[v].var = i;
2551
0
          if (!ssa->var_info
2552
0
           || !zend_jit_trace_copy_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, v, NULL)) {
2553
0
            ssa_var_info[v].type = MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2554
0
          }
2555
0
          i++;
2556
0
          v++;
2557
0
        }
2558
0
        while (i < op_array->last_var + op_array->T) {
2559
0
          ssa_vars[v].var = i;
2560
0
          if (!ssa->var_info
2561
0
           || !zend_jit_trace_copy_ssa_var_info(op_array, ssa, ssa_opcodes, tssa, v, NULL)) {
2562
0
            ssa_var_info[v].type = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2563
0
          }
2564
0
          i++;
2565
0
          v++;
2566
0
        }
2567
0
        if (return_value_info.type != 0) {
2568
0
          zend_jit_trace_rec *q = p + 1;
2569
0
          while (q->op == ZEND_JIT_TRACE_INIT_CALL) {
2570
0
            q++;
2571
0
          }
2572
0
          if (q->op == ZEND_JIT_TRACE_VM
2573
0
           || (q->op == ZEND_JIT_TRACE_END
2574
0
            && q->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET)) {
2575
0
            const zend_op *opline = q->opline - 1;
2576
0
            if (opline->result_type != IS_UNUSED) {
2577
0
              ssa_var_info[
2578
0
                ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info) +
2579
0
                EX_VAR_TO_NUM(opline->result.var)] = return_value_info;
2580
0
            }
2581
0
          }
2582
0
          memset(&return_value_info, 0, sizeof(return_value_info));
2583
0
        }
2584
0
      } else {
2585
0
        level--;
2586
0
        if (return_value_info.type != 0) {
2587
0
          if ((p+1)->op == ZEND_JIT_TRACE_VM) {
2588
0
            const zend_op *opline = (p+1)->opline - 1;
2589
0
            if (opline->result_type != IS_UNUSED) {
2590
0
              if (TRACE_FRAME_RETURN_SSA_VAR(frame) >= 0) {
2591
0
                ssa_var_info[TRACE_FRAME_RETURN_SSA_VAR(frame)] = return_value_info;
2592
0
              }
2593
0
            }
2594
0
          }
2595
0
          memset(&return_value_info, 0, sizeof(return_value_info));
2596
0
        }
2597
0
      }
2598
2599
0
      top = frame;
2600
0
      if (frame->prev) {
2601
0
        if (used_stack > 0) {
2602
0
          used_stack -= frame->used_stack;
2603
0
        }
2604
0
        frame = frame->prev;
2605
0
        ZEND_ASSERT(&frame->func->op_array == op_array);
2606
0
      } else {
2607
0
        max_used_stack = used_stack = -1;
2608
0
        frame = zend_jit_trace_ret_frame(frame, op_array);
2609
0
        TRACE_FRAME_INIT(frame, op_array, 0, 0);
2610
0
        TRACE_FRAME_SET_RETURN_SSA_VAR(frame, -1);
2611
0
        frame->used_stack = 0;
2612
0
      }
2613
2614
0
    } else if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
2615
0
      call = top;
2616
0
      TRACE_FRAME_INIT(call, p->func, 0, 0);
2617
0
      call->prev = frame->call;
2618
0
      call->used_stack = 0;
2619
0
      frame->call = call;
2620
0
      top = zend_jit_trace_call_frame(top, p->op_array, ZEND_JIT_TRACE_NUM_ARGS(p->info));
2621
0
      if (p->func && p->func->type == ZEND_USER_FUNCTION) {
2622
0
        for (i = 0; i < p->op_array->last_var + p->op_array->T; i++) {
2623
0
          SET_STACK_INFO(call->stack, i, -1);
2624
0
        }
2625
0
      }
2626
0
      if (used_stack >= 0
2627
0
       && !(p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL)) {
2628
0
        if (p->func == NULL || (p-1)->op != ZEND_JIT_TRACE_VM) {
2629
0
          max_used_stack = used_stack = -1;
2630
0
        } else {
2631
0
          const zend_op *opline = (p-1)->opline;
2632
2633
0
          switch (opline->opcode) {
2634
0
            case ZEND_INIT_FCALL:
2635
0
            case ZEND_INIT_FCALL_BY_NAME:
2636
0
            case ZEND_INIT_NS_FCALL_BY_NAME:
2637
0
            case ZEND_INIT_METHOD_CALL:
2638
0
            case ZEND_INIT_DYNAMIC_CALL:
2639
0
            case ZEND_INIT_STATIC_METHOD_CALL:
2640
            //case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
2641
            //case ZEND_INIT_USER_CALL:
2642
            //case ZEND_NEW:
2643
0
              frame->used_stack = zend_vm_calc_used_stack(opline->extended_value, (zend_function*)p->func);
2644
0
              used_stack += frame->used_stack;
2645
0
              if (used_stack > max_used_stack) {
2646
0
                max_used_stack = used_stack;
2647
0
              }
2648
0
              break;
2649
0
            default:
2650
0
              max_used_stack = used_stack = -1;
2651
0
          }
2652
0
        }
2653
0
      }
2654
0
    } else if (p->op == ZEND_JIT_TRACE_DO_ICALL) {
2655
0
      call = frame->call;
2656
0
      if (call) {
2657
0
        top = call;
2658
0
        frame->call = call->prev;
2659
0
      }
2660
2661
0
      if (idx > 0
2662
0
       && ssa_ops[idx-1].result_def >= 0
2663
0
       && p->func
2664
0
       && (p->func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2665
0
       && !(p->func->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
2666
0
        ZEND_ASSERT(ssa_opcodes[idx-1] == opline);
2667
0
        ZEND_ASSERT(opline->opcode == ZEND_DO_ICALL ||
2668
0
          opline->opcode == ZEND_DO_FCALL ||
2669
0
          opline->opcode == ZEND_DO_FCALL_BY_NAME);
2670
2671
0
        if (opline->result_type != IS_UNDEF) {
2672
0
          zend_class_entry *ce;
2673
0
          const zend_function *func = p->func;
2674
0
          zend_arg_info *ret_info = func->common.arg_info - 1;
2675
0
          uint32_t ret_type = zend_fetch_arg_info_type(NULL, ret_info, &ce);
2676
2677
0
          ssa_var_info[ssa_ops[idx-1].result_def].type &= ret_type;
2678
0
        }
2679
0
      }
2680
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
2681
0
      break;
2682
0
    }
2683
0
  }
2684
2685
0
  ((zend_tssa*)tssa)->used_stack = max_used_stack;
2686
2687
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
2688
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
2689
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
2690
    /* Propagate guards through Phi sources */
2691
0
    zend_ssa_phi *phi = tssa->blocks[1].phis;
2692
2693
0
    op_array = trace_buffer->op_array;
2694
0
    jit_extension =
2695
0
      (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
2696
0
    ssa = &jit_extension->func_info.ssa;
2697
2698
0
    while (phi) {
2699
0
      uint32_t t = ssa_var_info[phi->ssa_var].type;
2700
2701
0
      if ((t & MAY_BE_GUARD) && tssa->vars[phi->ssa_var].alias == NO_ALIAS) {
2702
0
        uint32_t t0 = ssa_var_info[phi->sources[0]].type;
2703
0
        uint32_t t1 = ssa_var_info[phi->sources[1]].type;
2704
2705
0
        if (((t0 | t1) & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == (t & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF))) {
2706
0
          if (!((t0 | t1) & MAY_BE_GUARD)) {
2707
0
            ssa_var_info[phi->ssa_var].type = t & ~MAY_BE_GUARD;
2708
0
          }
2709
0
        } else if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == (t & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF))) {
2710
0
          if (!(t1 & MAY_BE_GUARD)
2711
0
           || is_checked_guard(tssa, ssa_opcodes, phi->sources[1], phi->ssa_var)) {
2712
0
            ssa_var_info[phi->ssa_var].type = t & ~MAY_BE_GUARD;
2713
0
            t0 = (t & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) |
2714
0
              (t0 & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) |
2715
0
              MAY_BE_GUARD;
2716
0
            if (!(t0 & MAY_BE_ARRAY)) {
2717
0
              t0 &= ~(MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_ARRAY_KEY_ANY);
2718
0
            }
2719
0
            if (!(t0 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
2720
0
              t0 &= ~(MAY_BE_RC1|MAY_BE_RCN);
2721
0
            }
2722
0
            ssa_var_info[phi->sources[0]].type = t0;
2723
0
            ssa_var_info[phi->sources[0]].type = t0;
2724
0
          }
2725
0
        } else {
2726
0
          if ((t0 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != (t & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF))) {
2727
0
            t0 = (t & t0 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) |
2728
0
              (t0 & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) |
2729
0
              MAY_BE_GUARD;
2730
0
            if (!(t0 & MAY_BE_ARRAY)) {
2731
0
              t0 &= ~(MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_ARRAY_KEY_ANY);
2732
0
            }
2733
0
            if (!(t0 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
2734
0
              t0 &= ~(MAY_BE_RC1|MAY_BE_RCN);
2735
0
            }
2736
0
            ssa_var_info[phi->sources[0]].type = t0;
2737
0
          }
2738
0
          if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != (t & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF))) {
2739
0
            if (((t & t1) & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != 0
2740
0
             && is_checked_guard(tssa, ssa_opcodes, phi->sources[1], phi->ssa_var)) {
2741
0
              t1 = (t & t1 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) |
2742
0
                (t1 & ~(MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) |
2743
0
                MAY_BE_GUARD;
2744
0
              if (!(t1 & MAY_BE_ARRAY)) {
2745
0
                t1 &= ~(MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_ARRAY_KEY_ANY);
2746
0
              }
2747
0
              if (!(t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
2748
0
                t1 &= ~(MAY_BE_RC1|MAY_BE_RCN);
2749
0
              }
2750
0
              ssa_var_info[phi->sources[1]].type = t1;
2751
0
              ssa_var_info[phi->ssa_var].type = t & ~MAY_BE_GUARD;
2752
0
            }
2753
0
          }
2754
0
        }
2755
0
        t = ssa_var_info[phi->ssa_var].type;
2756
0
      }
2757
2758
0
      if ((t & MAY_BE_PACKED_GUARD) && tssa->vars[phi->ssa_var].alias == NO_ALIAS) {
2759
0
        uint32_t t0 = ssa_var_info[phi->sources[0]].type;
2760
0
        uint32_t t1 = ssa_var_info[phi->sources[1]].type;
2761
2762
0
        if (((t0 | t1) & MAY_BE_ARRAY_KEY_ANY) == (t & MAY_BE_ARRAY_KEY_ANY)) {
2763
0
          if (!((t0 | t1) & MAY_BE_PACKED_GUARD)) {
2764
0
            ssa_var_info[phi->ssa_var].type = t & ~MAY_BE_PACKED_GUARD;
2765
0
          }
2766
0
        } else if ((t1 & MAY_BE_ARRAY_KEY_ANY) == (t & MAY_BE_ARRAY_KEY_ANY)) {
2767
0
          if (!(t1 & MAY_BE_PACKED_GUARD)) {
2768
0
            ssa_var_info[phi->ssa_var].type = t & ~MAY_BE_PACKED_GUARD;
2769
0
            ssa_var_info[phi->sources[0]].type =
2770
0
              (t0 & ~MAY_BE_ARRAY_KEY_ANY) | (t & MAY_BE_ARRAY_KEY_ANY) | MAY_BE_PACKED_GUARD;
2771
0
          }
2772
0
        }
2773
0
      }
2774
0
      phi = phi->next;
2775
0
    }
2776
0
  }
2777
2778
0
  if (UNEXPECTED(JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_TSSA)) {
2779
0
    if (parent_trace) {
2780
0
      fprintf(stderr, "---- TRACE %d TSSA start (side trace %d/%d) %s%s%s() %s:%d\n",
2781
0
        ZEND_JIT_TRACE_NUM,
2782
0
        parent_trace,
2783
0
        exit_num,
2784
0
        trace_buffer->op_array->scope ? ZSTR_VAL(trace_buffer->op_array->scope->name) : "",
2785
0
        trace_buffer->op_array->scope ? "::" : "",
2786
0
        trace_buffer->op_array->function_name ?
2787
0
          ZSTR_VAL(trace_buffer->op_array->function_name) : "$main",
2788
0
        ZSTR_VAL(trace_buffer->op_array->filename),
2789
0
        trace_buffer[1].opline->lineno);
2790
0
    } else {
2791
0
      fprintf(stderr, "---- TRACE %d TSSA start (%s) %s%s%s() %s:%d\n",
2792
0
        ZEND_JIT_TRACE_NUM,
2793
0
        zend_jit_trace_star_desc(trace_buffer->start),
2794
0
        trace_buffer->op_array->scope ? ZSTR_VAL(trace_buffer->op_array->scope->name) : "",
2795
0
        trace_buffer->op_array->scope ? "::" : "",
2796
0
        trace_buffer->op_array->function_name ?
2797
0
          ZSTR_VAL(trace_buffer->op_array->function_name) : "$main",
2798
0
        ZSTR_VAL(trace_buffer->op_array->filename),
2799
0
        trace_buffer[1].opline->lineno);
2800
0
    }
2801
0
    zend_jit_dump_trace(trace_buffer, tssa);
2802
0
    if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LINK) {
2803
0
      uint32_t idx = trace_buffer[1].last;
2804
0
      uint32_t link_to = zend_jit_find_trace(trace_buffer[idx].opline->handler);
2805
0
      fprintf(stderr, "---- TRACE %d TSSA stop (link to %d)\n",
2806
0
        ZEND_JIT_TRACE_NUM,
2807
0
        link_to);
2808
0
    } else {
2809
0
      fprintf(stderr, "---- TRACE %d TSSA stop (%s)\n",
2810
0
        ZEND_JIT_TRACE_NUM,
2811
0
        zend_jit_trace_stop_description[trace_buffer->stop]);
2812
0
    }
2813
0
  }
2814
2815
0
  return tssa;
2816
0
}
2817
2818
0
#define RA_HAS_IVAL(var)          (ra[var].ref != 0)
2819
0
#define RA_IVAL_FLAGS(var)        ra[var].flags
2820
0
#define RA_IVAL_START(var, line)  do {ra[var].ref = IR_NULL;} while (0)
2821
#define RA_IVAL_END(var, line)
2822
#define RA_IVAL_CLOSE(var, line)
2823
0
#define RA_IVAL_DEL(var)          do {ra[var].ref = IR_UNUSED;} while (0)
2824
0
#define RA_HAS_REG(var)           (ra[var].ref != 0)
2825
0
#define RA_REG_FLAGS(var)         ra[var].flags
2826
0
#define RA_REG_START(var, line)   do {ra[var].ref = IR_NULL;} while (0)
2827
0
#define RA_REG_DEL(var)           do {ra[var].ref = IR_UNUSED;} while (0)
2828
2829
static void zend_jit_trace_use_var(int line, int var, int def, int use_chain, zend_jit_reg_var *ra, const zend_ssa *ssa, const zend_op **ssa_opcodes, const zend_op_array *op_array, const zend_ssa *op_array_ssa)
2830
0
{
2831
0
  ZEND_ASSERT(RA_HAS_IVAL(var));
2832
0
  ZEND_ASSERT(!(RA_IVAL_FLAGS(var) & ZREG_LAST_USE));
2833
0
  RA_IVAL_END(var, line);
2834
0
  if (def >= 0) {
2835
0
    RA_IVAL_FLAGS(var) |= ZREG_LAST_USE;
2836
0
  } else if (use_chain < 0 && (RA_IVAL_FLAGS(var) & (ZREG_LOAD|ZREG_STORE))) {
2837
0
    RA_IVAL_FLAGS(var) |= ZREG_LAST_USE;
2838
0
  } else if (use_chain >= 0 && !zend_ssa_is_no_val_use(ssa_opcodes[use_chain], ssa->ops + use_chain, var)) {
2839
    /* pass */
2840
0
  } else if (op_array_ssa->vars) {
2841
0
    uint32_t use = ssa_opcodes[line] - op_array->opcodes;
2842
2843
0
    if (ssa->ops[line].op1_use == var) {
2844
0
      if (zend_ssa_is_last_use(op_array, op_array_ssa, op_array_ssa->ops[use].op1_use, use)) {
2845
0
        RA_IVAL_FLAGS(var) |= ZREG_LAST_USE;
2846
0
      }
2847
0
    } else if (ssa->ops[line].op2_use == var) {
2848
0
      if (zend_ssa_is_last_use(op_array, op_array_ssa, op_array_ssa->ops[use].op2_use, use)) {
2849
0
        RA_IVAL_FLAGS(var) |= ZREG_LAST_USE;
2850
0
      }
2851
0
    } else if (ssa->ops[line].result_use == var) {
2852
0
      if (zend_ssa_is_last_use(op_array, op_array_ssa, op_array_ssa->ops[use].result_use, use)) {
2853
0
        RA_IVAL_FLAGS(var) |= ZREG_LAST_USE;
2854
0
      }
2855
0
    }
2856
0
  }
2857
0
}
2858
2859
static zend_jit_reg_var* zend_jit_trace_allocate_registers(zend_jit_trace_rec *trace_buffer, zend_ssa *ssa, uint32_t parent_trace, uint32_t exit_num)
2860
0
{
2861
0
  const zend_op **ssa_opcodes = ((zend_tssa*)ssa)->tssa_opcodes;
2862
0
  zend_jit_trace_rec *p;
2863
0
  const zend_op_array *op_array;
2864
0
  zend_jit_op_array_trace_extension *jit_extension;
2865
0
  const zend_ssa *op_array_ssa;
2866
0
  const zend_ssa_op *ssa_op;
2867
0
  int i, j, idx, count, level;
2868
0
  zend_jit_reg_var *ra;
2869
0
  const zend_op_array **vars_op_array;
2870
0
  void *checkpoint;
2871
0
  zend_jit_trace_stack_frame *frame;
2872
0
  zend_jit_trace_stack *stack;
2873
0
  uint32_t parent_vars_count = parent_trace ?
2874
0
    zend_jit_traces[parent_trace].exit_info[exit_num].stack_size : 0;
2875
0
  zend_jit_trace_stack *parent_stack = parent_vars_count ?
2876
0
    zend_jit_traces[parent_trace].stack_map +
2877
0
    zend_jit_traces[parent_trace].exit_info[exit_num].stack_offset : NULL;
2878
0
  checkpoint = zend_arena_checkpoint(CG(arena));
2879
0
  ra = zend_arena_calloc(&CG(arena), ssa->vars_count, sizeof(zend_jit_reg_var));
2880
0
  vars_op_array = zend_arena_calloc(&CG(arena), ssa->vars_count, sizeof(zend_op_array*));
2881
0
  memset(ZEND_VOIDP(vars_op_array), 0, sizeof(zend_op_array*) * ssa->vars_count);
2882
2883
0
  op_array = trace_buffer->op_array;
2884
0
  jit_extension =
2885
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
2886
0
  op_array_ssa = &jit_extension->func_info.ssa;
2887
0
  frame = JIT_G(current_frame);
2888
0
  frame->prev = NULL;
2889
0
  frame->func = (const zend_function*)op_array;
2890
0
  stack = frame->stack;
2891
2892
0
  count = 0;
2893
2894
0
  i = 0;
2895
0
  j = op_array->last_var;
2896
0
  if (trace_buffer->start != ZEND_JIT_TRACE_START_ENTER) {
2897
0
    j += op_array->T;
2898
0
  }
2899
0
  while (i < j) {
2900
0
    SET_STACK_VAR(stack, i, i);
2901
0
    vars_op_array[i] = op_array;
2902
    /* We don't start intervals for variables used in Phi */
2903
0
    if ((ssa->vars[i].use_chain >= 0 /*|| ssa->vars[i].phi_use_chain*/)
2904
0
     && !zend_ssa_is_no_val_use(ssa_opcodes[ssa->vars[i].use_chain], ssa->ops + ssa->vars[i].use_chain, i)
2905
0
     && ssa->vars[i].alias == NO_ALIAS
2906
0
     && zend_jit_var_supports_reg(ssa, i)) {
2907
0
      RA_IVAL_START(i, 0);
2908
0
      if (i < parent_vars_count
2909
0
       && STACK_REG(parent_stack, i) != ZREG_NONE
2910
0
       && STACK_FLAGS(parent_stack, i) != ZREG_ZVAL_COPY
2911
0
       ) {
2912
        /* We will try to reuse register from parent trace */
2913
0
        RA_IVAL_FLAGS(i) = STACK_FLAGS(parent_stack, i);
2914
0
        count += 2;
2915
0
      } else {
2916
0
        RA_IVAL_FLAGS(i) = ZREG_LOAD;
2917
0
        count++;
2918
0
      }
2919
0
    }
2920
0
    i++;
2921
0
  }
2922
2923
0
  if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER) {
2924
0
    j = op_array->last_var + op_array->T;
2925
0
    while (i < j) {
2926
0
      SET_STACK_VAR(stack, i, -1);
2927
0
      i++;
2928
0
    }
2929
0
  }
2930
2931
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
2932
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
2933
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
2934
0
    zend_ssa_phi *phi = ssa->blocks[1].phis;
2935
2936
0
    while (phi) {
2937
0
      SET_STACK_VAR(stack, phi->var, phi->ssa_var);
2938
0
      vars_op_array[phi->ssa_var] = op_array;
2939
0
      if (ssa->vars[phi->ssa_var].use_chain >= 0
2940
0
       && ssa->vars[phi->ssa_var].alias == NO_ALIAS
2941
0
       && zend_jit_var_supports_reg(ssa, phi->ssa_var)) {
2942
0
        RA_IVAL_START(phi->ssa_var, 0);
2943
0
        count++;
2944
0
      }
2945
0
      phi = phi->next;
2946
0
    }
2947
0
  }
2948
2949
0
  p = trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE;
2950
0
  level = 0;
2951
0
  ssa_op = ssa->ops;
2952
0
  idx = 0;
2953
0
  for (;;p++) {
2954
0
    if (p->op == ZEND_JIT_TRACE_VM) {
2955
0
      const zend_op *opline = p->opline;
2956
0
      int len;
2957
0
      bool support_opline;
2958
2959
0
      support_opline =
2960
0
        zend_jit_opline_supports_reg(op_array, ssa, opline, ssa_op, p);
2961
2962
0
      if (support_opline
2963
0
       && opline->opcode == ZEND_ASSIGN
2964
0
       && opline->op1_type == IS_CV
2965
0
       && ssa_op->op1_def >= 0
2966
0
       && ssa->vars[ssa_op->op1_def].alias != NO_ALIAS) {
2967
        /* avoid register allocation in case of possibility of indirect modification*/
2968
0
        support_opline = false;
2969
0
      }
2970
2971
0
      if (ssa_op->op1_use >= 0
2972
0
       && RA_HAS_IVAL(ssa_op->op1_use)) {
2973
0
        if (!support_opline) {
2974
0
          RA_IVAL_DEL(ssa_op->op1_use);
2975
0
          count--;
2976
0
        } else if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use)) {
2977
0
          zend_jit_trace_use_var(idx, ssa_op->op1_use, ssa_op->op1_def, ssa_op->op1_use_chain,
2978
0
            ra,
2979
0
            ssa, ssa_opcodes, op_array, op_array_ssa);
2980
0
          if (opline->op1_type != IS_CV) {
2981
0
            if (opline->opcode == ZEND_CASE
2982
0
             || opline->opcode == ZEND_CASE_STRICT
2983
0
             || opline->opcode == ZEND_SWITCH_LONG
2984
0
             || opline->opcode == ZEND_MATCH
2985
0
             || opline->opcode == ZEND_FETCH_LIST_R
2986
0
             || opline->opcode == ZEND_COPY_TMP
2987
0
             || opline->opcode == ZEND_SWITCH_STRING
2988
0
             || opline->opcode == ZEND_FE_FETCH_R
2989
0
             || opline->opcode == ZEND_FE_FETCH_RW
2990
0
             || opline->opcode == ZEND_FETCH_LIST_W
2991
0
             || opline->opcode == ZEND_VERIFY_RETURN_TYPE
2992
0
             || opline->opcode == ZEND_BIND_LEXICAL
2993
0
             || opline->opcode == ZEND_ROPE_ADD) {
2994
              /* The value is kept alive and may be used outside of the trace */
2995
0
              RA_IVAL_FLAGS(ssa_op->op1_use) |= ZREG_STORE;
2996
0
            } else {
2997
0
              RA_IVAL_FLAGS(ssa_op->op1_use) |= ZREG_LAST_USE;
2998
0
            }
2999
0
          }
3000
0
        }
3001
0
      }
3002
0
      if (ssa_op->op2_use >= 0
3003
0
       && ssa_op->op2_use != ssa_op->op1_use
3004
0
       && RA_HAS_IVAL(ssa_op->op2_use)) {
3005
0
        if (!support_opline) {
3006
0
          RA_IVAL_DEL(ssa_op->op2_use);
3007
0
          count--;
3008
0
        } else if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op2_use)) {
3009
0
          zend_jit_trace_use_var(idx, ssa_op->op2_use, ssa_op->op2_def, ssa_op->op2_use_chain,
3010
0
            ra,
3011
0
            ssa, ssa_opcodes, op_array, op_array_ssa);
3012
0
          if (opline->op2_type != IS_CV) {
3013
0
            RA_IVAL_FLAGS(ssa_op->op2_use) |= ZREG_LAST_USE;
3014
0
          }
3015
0
        }
3016
0
      }
3017
0
      if (ssa_op->result_use >= 0
3018
0
       && ssa_op->result_use != ssa_op->op1_use
3019
0
       && ssa_op->result_use != ssa_op->op2_use
3020
0
       && RA_HAS_IVAL(ssa_op->result_use)) {
3021
0
        if (!support_opline) {
3022
0
          RA_IVAL_DEL(ssa_op->result_use);
3023
0
          count--;
3024
0
        } else if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->result_use)) {
3025
0
          zend_jit_trace_use_var(idx, ssa_op->result_use, ssa_op->result_def, ssa_op->res_use_chain,
3026
0
            ra,
3027
0
            ssa, ssa_opcodes, op_array, op_array_ssa);
3028
0
        }
3029
0
      }
3030
3031
0
      if (ssa_op->op1_def >= 0) {
3032
0
        RA_IVAL_CLOSE(EX_VAR_TO_NUM(opline->op1.var), idx);
3033
0
        SET_STACK_VAR(stack, EX_VAR_TO_NUM(opline->op1.var), ssa_op->op1_def);
3034
0
      }
3035
0
      if (ssa_op->op2_def >= 0) {
3036
0
        RA_IVAL_CLOSE(EX_VAR_TO_NUM(opline->op2.var), idx);
3037
0
        SET_STACK_VAR(stack, EX_VAR_TO_NUM(opline->op2.var), ssa_op->op2_def);
3038
0
      }
3039
0
      if (ssa_op->result_def >= 0) {
3040
0
        RA_IVAL_CLOSE(EX_VAR_TO_NUM(opline->result.var), idx);
3041
0
        SET_STACK_VAR(stack, EX_VAR_TO_NUM(opline->result.var), ssa_op->result_def);
3042
0
      }
3043
3044
0
      if (support_opline) {
3045
0
        if (ssa_op->result_def >= 0
3046
0
         && (ssa->vars[ssa_op->result_def].use_chain >= 0
3047
0
            || ssa->vars[ssa_op->result_def].phi_use_chain)
3048
0
         && ssa->vars[ssa_op->result_def].alias == NO_ALIAS
3049
0
         && zend_jit_var_supports_reg(ssa, ssa_op->result_def)) {
3050
0
          if (!(ssa->var_info[ssa_op->result_def].type & MAY_BE_GUARD)
3051
0
           || opline->opcode == ZEND_PRE_INC
3052
0
           || opline->opcode == ZEND_PRE_DEC
3053
0
           || opline->opcode == ZEND_POST_INC
3054
0
           || opline->opcode == ZEND_POST_DEC
3055
0
           || opline->opcode == ZEND_ADD
3056
0
           || opline->opcode == ZEND_SUB
3057
0
           || opline->opcode == ZEND_MUL
3058
0
           || opline->opcode == ZEND_FETCH_DIM_R
3059
0
           || opline->opcode == ZEND_FETCH_OBJ_R
3060
0
           || opline->opcode == ZEND_FETCH_CONSTANT) {
3061
0
            if (!(ssa->var_info[ssa_op->result_def].type & MAY_BE_DOUBLE)
3062
0
             || (opline->opcode != ZEND_PRE_INC && opline->opcode != ZEND_PRE_DEC)) {
3063
0
              vars_op_array[ssa_op->result_def] = op_array;
3064
0
              RA_IVAL_START(ssa_op->result_def, idx);
3065
0
              count++;
3066
0
            }
3067
0
          }
3068
0
        }
3069
0
        if (ssa_op->op1_def >= 0
3070
0
         && (ssa->vars[ssa_op->op1_def].use_chain >= 0
3071
0
            || ssa->vars[ssa_op->op1_def].phi_use_chain)
3072
0
         && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS
3073
0
         && zend_jit_var_supports_reg(ssa, ssa_op->op1_def)
3074
0
         && (!(ssa->var_info[ssa_op->op1_def].type & MAY_BE_GUARD)
3075
0
          || opline->opcode == ZEND_PRE_INC
3076
0
          || opline->opcode == ZEND_PRE_DEC
3077
0
          || opline->opcode == ZEND_POST_INC
3078
0
          || opline->opcode == ZEND_POST_DEC)) {
3079
0
          vars_op_array[ssa_op->op1_def] = op_array;
3080
0
          RA_IVAL_START(ssa_op->op1_def, idx);
3081
0
          count++;
3082
0
        }
3083
0
        if (ssa_op->op2_def >= 0
3084
0
         && (ssa->vars[ssa_op->op2_def].use_chain >= 0
3085
0
            || ssa->vars[ssa_op->op2_def].phi_use_chain)
3086
0
         && ssa->vars[ssa_op->op2_def].alias == NO_ALIAS
3087
0
         && zend_jit_var_supports_reg(ssa, ssa_op->op2_def)
3088
0
         && !(ssa->var_info[ssa_op->op2_def].type & MAY_BE_GUARD)) {
3089
0
          vars_op_array[ssa_op->op2_def] = op_array;
3090
0
          RA_IVAL_START(ssa_op->op2_def, idx);
3091
0
          count++;
3092
0
        }
3093
0
      }
3094
3095
0
      len = zend_jit_trace_op_len(opline);
3096
0
      switch (opline->opcode) {
3097
0
        case ZEND_ASSIGN_DIM:
3098
0
        case ZEND_ASSIGN_OBJ:
3099
0
        case ZEND_ASSIGN_STATIC_PROP:
3100
0
        case ZEND_ASSIGN_DIM_OP:
3101
0
        case ZEND_ASSIGN_OBJ_OP:
3102
0
        case ZEND_ASSIGN_STATIC_PROP_OP:
3103
0
        case ZEND_ASSIGN_OBJ_REF:
3104
0
        case ZEND_ASSIGN_STATIC_PROP_REF:
3105
0
        case ZEND_FRAMELESS_ICALL_3:
3106
          /* OP_DATA */
3107
0
          ssa_op++;
3108
0
          opline++;
3109
0
          if (ssa_op->op1_use >= 0
3110
0
           && RA_HAS_IVAL(ssa_op->op1_use)
3111
0
           && !zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use)) {
3112
0
            if (support_opline) {
3113
0
              zend_jit_trace_use_var(idx, ssa_op->op1_use, ssa_op->op1_def, ssa_op->op1_use_chain,
3114
0
                ra,
3115
0
                ssa, ssa_opcodes, op_array, op_array_ssa);
3116
0
              if (opline->op1_type != IS_CV) {
3117
0
                RA_IVAL_FLAGS(ssa_op->op1_use) |= ZREG_LAST_USE;
3118
0
              }
3119
0
            } else {
3120
0
              RA_IVAL_DEL(ssa_op->op1_use);
3121
0
              count--;
3122
0
            }
3123
0
          }
3124
0
          if (ssa_op->op1_def >= 0) {
3125
0
            RA_IVAL_CLOSE(EX_VAR_TO_NUM(opline->op1.var), idx);
3126
0
            SET_STACK_VAR(stack, EX_VAR_TO_NUM(opline->op1.var), ssa_op->op1_def);
3127
0
            if (support_opline
3128
0
             && (ssa->vars[ssa_op->op1_def].use_chain >= 0
3129
0
                || ssa->vars[ssa_op->op1_def].phi_use_chain)
3130
0
             && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS
3131
0
             && zend_jit_var_supports_reg(ssa, ssa_op->op1_def)
3132
0
             && !(ssa->var_info[ssa_op->op1_def].type & MAY_BE_GUARD)) {
3133
0
              vars_op_array[ssa_op->op1_def] = op_array;
3134
0
              RA_IVAL_START(ssa_op->op1_def, idx);
3135
0
              count++;
3136
0
            }
3137
0
          }
3138
0
          ssa_op++;
3139
0
          opline++;
3140
0
          idx+=2;
3141
0
          break;
3142
0
        case ZEND_RECV_INIT:
3143
0
            ssa_op++;
3144
0
          opline++;
3145
0
          idx++;
3146
0
          while (opline->opcode == ZEND_RECV_INIT) {
3147
            /* RECV_INIT doesn't support registers */
3148
0
            if (ssa_op->result_use >= 0 && RA_HAS_IVAL(ssa_op->result_use)) {
3149
0
              RA_IVAL_DEL(ssa_op->result_use);
3150
0
              count--;
3151
0
            }
3152
0
            if (ssa_op->result_def >= 0) {
3153
0
              RA_IVAL_CLOSE(EX_VAR_TO_NUM(opline->result.var), idx);
3154
0
              SET_STACK_VAR(stack, EX_VAR_TO_NUM(opline->result.var), ssa_op->result_def);
3155
0
            }
3156
0
            ssa_op++;
3157
0
            opline++;
3158
0
            idx++;
3159
0
          }
3160
0
          break;
3161
0
        case ZEND_BIND_GLOBAL:
3162
0
          ssa_op++;
3163
0
          opline++;
3164
0
          idx++;
3165
0
          while (opline->opcode == ZEND_BIND_GLOBAL) {
3166
            /* BIND_GLOBAL doesn't support registers */
3167
0
            if (ssa_op->op1_def >= 0) {
3168
0
              RA_IVAL_CLOSE(EX_VAR_TO_NUM(opline->op1.var), idx);
3169
0
              SET_STACK_VAR(stack, EX_VAR_TO_NUM(opline->op1.var), ssa_op->op1_def);
3170
0
            }
3171
0
            ssa_op++;
3172
0
            opline++;
3173
0
            idx++;
3174
0
          }
3175
0
          break;
3176
0
        default:
3177
0
          ssa_op += len;
3178
0
          idx += len;
3179
0
          break;
3180
0
      }
3181
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
3182
      /* New call frames */
3183
0
      zend_jit_trace_stack_frame *prev_frame = frame;
3184
3185
      /* Clear allocated registers */
3186
0
      for (i = 0; i < op_array->last_var + op_array->T; i++) {
3187
0
        j = STACK_VAR(stack, i);
3188
0
        if (j >= 0 && RA_HAS_IVAL(j) && !(RA_IVAL_FLAGS(j) & ZREG_LAST_USE)) {
3189
0
          RA_IVAL_DEL(j);
3190
0
          count--;
3191
0
        }
3192
0
      }
3193
3194
0
      frame = zend_jit_trace_call_frame(frame, op_array, 0);
3195
0
      frame->prev = prev_frame;
3196
0
      frame->func = (const zend_function*)p->op_array;
3197
0
      stack = frame->stack;
3198
0
      op_array = p->op_array;
3199
0
      jit_extension =
3200
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
3201
0
      op_array_ssa = &jit_extension->func_info.ssa;
3202
0
      j = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
3203
0
      for (i = 0; i < op_array->last_var; i++) {
3204
0
        SET_STACK_VAR(stack, i, j);
3205
0
        vars_op_array[j] = op_array;
3206
0
        if (ssa->vars[j].use_chain >= 0
3207
0
         && ssa->vars[j].alias == NO_ALIAS
3208
0
         && zend_jit_var_supports_reg(ssa, j)) {
3209
0
          RA_IVAL_START(j, idx);
3210
0
          RA_IVAL_FLAGS(j) = ZREG_LOAD;
3211
0
          count++;
3212
0
        }
3213
0
        j++;
3214
0
      }
3215
0
      for (i = op_array->last_var; i < op_array->last_var + op_array->T; i++) {
3216
0
        SET_STACK_VAR(stack, i, -1);
3217
0
      }
3218
0
      level++;
3219
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
3220
      /* Close exiting call frames */
3221
0
      for (i = 0; i < op_array->last_var; i++) {
3222
0
        RA_IVAL_CLOSE(i, idx-1);
3223
0
      }
3224
0
      op_array = p->op_array;
3225
0
      jit_extension =
3226
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
3227
0
      op_array_ssa = &jit_extension->func_info.ssa;
3228
0
      frame = zend_jit_trace_ret_frame(frame, op_array);
3229
0
      stack = frame->stack;
3230
0
      if (level == 0) {
3231
        /* New return frames */
3232
0
        frame->prev = NULL;
3233
0
        frame->func = (const zend_function*)op_array;
3234
0
        j = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
3235
0
        for (i = 0; i < op_array->last_var + op_array->T; i++) {
3236
0
          SET_STACK_VAR(stack, i, j);
3237
0
          vars_op_array[j] = op_array;
3238
0
          if (ssa->vars[j].use_chain >= 0
3239
0
           && ssa->vars[j].alias == NO_ALIAS
3240
0
           && zend_jit_var_supports_reg(ssa, j)
3241
0
           && !(ssa->var_info[j].type & MAY_BE_GUARD)) {
3242
0
            RA_IVAL_START(j, idx);
3243
0
            RA_IVAL_FLAGS(j) = ZREG_LOAD;
3244
0
            count++;
3245
0
          }
3246
0
          j++;
3247
0
        }
3248
0
      } else {
3249
0
        level--;
3250
0
      }
3251
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
3252
0
      break;
3253
0
    }
3254
0
  }
3255
3256
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
3257
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
3258
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
3259
0
    zend_ssa_phi *phi = ssa->blocks[1].phis;
3260
3261
0
    while (phi) {
3262
0
      i = phi->sources[1];
3263
0
      if (RA_HAS_IVAL(i) && !ssa->vars[phi->ssa_var].no_val) {
3264
0
        RA_IVAL_END(i, idx);
3265
0
        RA_IVAL_FLAGS(i) &= ~ZREG_LAST_USE;
3266
0
      }
3267
0
      phi = phi->next;
3268
0
    }
3269
3270
0
    if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP) {
3271
0
      for (i = 0; i < op_array->last_var; i++) {
3272
0
        if (RA_HAS_IVAL(i) && !ssa->vars[i].phi_use_chain) {
3273
0
          RA_IVAL_END(i, idx);
3274
0
          RA_IVAL_FLAGS(i) &= ~ZREG_LAST_USE;
3275
0
        } else {
3276
0
          RA_IVAL_CLOSE(i, idx);
3277
0
        }
3278
0
      }
3279
0
    }
3280
0
  }
3281
3282
0
  if (count) {
3283
0
    for (i = 0; i < ssa->vars_count; i++) {
3284
0
      if (RA_HAS_REG(i)) {
3285
0
        if ((RA_REG_FLAGS(i) & ZREG_LOAD) &&
3286
0
            (RA_REG_FLAGS(i) & ZREG_LAST_USE) &&
3287
0
            (i >= parent_vars_count || STACK_REG(parent_stack, i) == ZREG_NONE) &&
3288
0
            zend_ssa_next_use(ssa->ops, i, ssa->vars[i].use_chain) < 0) {
3289
          /* skip life range with single use */
3290
0
          RA_REG_DEL(i);
3291
0
          count--;
3292
0
        }
3293
0
      }
3294
0
    }
3295
0
  }
3296
3297
0
  if (count) {
3298
    /* SSA resolution */
3299
0
    if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
3300
0
     || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
3301
0
     || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
3302
0
      zend_ssa_phi *phi = ssa->blocks[1].phis;
3303
3304
0
      while (phi) {
3305
0
        int def = phi->ssa_var;
3306
0
        int use = phi->sources[1];
3307
3308
0
        if (RA_HAS_REG(def)) {
3309
0
          if (!RA_HAS_REG(use)) {
3310
0
            RA_REG_FLAGS(def) |= ZREG_LOAD;
3311
0
            if ((RA_REG_FLAGS(def) & ZREG_LAST_USE)
3312
0
             && ssa->vars[def].use_chain >= 0
3313
0
             && !ssa->vars[def].phi_use_chain
3314
0
             && zend_ssa_next_use(ssa->ops, def, ssa->vars[def].use_chain) < 0
3315
0
            ) {
3316
              /* remove interval used once */
3317
0
              RA_REG_DEL(def);
3318
0
              count--;
3319
0
            }
3320
0
          } else if ((ssa->var_info[def].type & MAY_BE_ANY) != (ssa->var_info[use].type & MAY_BE_ANY)) {
3321
0
            RA_REG_FLAGS(def) |= ZREG_LOAD;
3322
0
            RA_REG_FLAGS(use) |= ZREG_STORE;
3323
0
          } else {
3324
0
            use = phi->sources[0];
3325
0
            if (zend_jit_var_supports_reg(ssa, use)) {
3326
0
              ZEND_ASSERT(!RA_HAS_REG(use));
3327
0
              RA_REG_START(use, 0);
3328
0
              RA_REG_FLAGS(use) = ZREG_LOAD;
3329
0
              count++;
3330
0
            } else {
3331
0
              RA_REG_FLAGS(def) |= ZREG_LOAD;
3332
0
            }
3333
0
          }
3334
0
        } else if (RA_HAS_REG(use)) {
3335
0
          if (ssa->vars[use].use_chain >= 0) {
3336
0
            RA_REG_FLAGS(use) |= ZREG_STORE; // TODO: ext/opcache/tests/jit/reg_alloc_00[67].phpt ???
3337
0
          } else {
3338
0
            RA_REG_DEL(use);
3339
0
            count--;
3340
0
          }
3341
0
        }
3342
0
        phi = phi->next;
3343
0
      }
3344
0
    } else if (p->stop >= ZEND_JIT_TRACE_STOP_LINK) {
3345
0
      for (i = 0; i < op_array->last_var + op_array->T; i++) {
3346
0
        int var = STACK_VAR(stack, i);
3347
0
        if (var >= 0 && RA_HAS_REG(var)
3348
0
         && !(RA_REG_FLAGS(var) & (ZREG_LOAD|ZREG_STORE|ZREG_LAST_USE))) {
3349
0
          RA_REG_FLAGS(var) |= ZREG_STORE;
3350
0
        }
3351
0
      }
3352
0
    }
3353
3354
0
    if (!count) {
3355
0
      zend_arena_release(&CG(arena), checkpoint);
3356
0
      return NULL;
3357
0
    }
3358
3359
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_REG_ALLOC) {
3360
0
      fprintf(stderr, "---- TRACE %d Live Ranges \"%s\"\n", ZEND_JIT_TRACE_NUM, op_array->function_name ? ZSTR_VAL(op_array->function_name) : "[main]");
3361
0
      for (i = 0; i < ssa->vars_count; i++) {
3362
0
        if (RA_HAS_REG(i)) {
3363
0
          fprintf(stderr, "#%d.", i);
3364
0
          uint32_t var_num = ssa->vars[i].var;
3365
0
          zend_dump_var(vars_op_array[i], (var_num < vars_op_array[i]->last_var ? IS_CV : 0), var_num);
3366
0
          if (RA_REG_FLAGS(i) & ZREG_LAST_USE) {
3367
0
            fprintf(stderr, " last_use");
3368
0
          }
3369
0
          if (RA_REG_FLAGS(i) & ZREG_LOAD) {
3370
0
            fprintf(stderr, " load");
3371
0
          }
3372
0
          if (RA_REG_FLAGS(i) & ZREG_STORE) {
3373
0
            fprintf(stderr, " store");
3374
0
          }
3375
0
          fprintf(stderr, "\n");
3376
0
        }
3377
0
      }
3378
0
      fprintf(stderr, "\n");
3379
0
    }
3380
3381
0
    return ra;
3382
0
  }
3383
3384
0
  zend_arena_release(&CG(arena), checkpoint);
3385
0
  return NULL;
3386
0
}
3387
3388
static void zend_jit_trace_cleanup_stack(zend_jit_ctx *jit, zend_jit_trace_stack *stack, const zend_op *opline, const zend_ssa_op *ssa_op, const zend_ssa *ssa, const zend_op **ssa_opcodes)
3389
0
{
3390
0
  if (ssa_op->op1_use >= 0
3391
0
   && jit->ra[ssa_op->op1_use].ref
3392
0
   && (jit->ra[ssa_op->op1_use].flags & ZREG_LAST_USE)
3393
0
   && (ssa_op->op1_use_chain == -1
3394
0
    || zend_ssa_is_no_val_use(ssa_opcodes[ssa_op->op1_use_chain], ssa->ops + ssa_op->op1_use_chain, ssa_op->op1_use))) {
3395
0
    CLEAR_STACK_REF(stack, EX_VAR_TO_NUM(opline->op1.var));
3396
0
  }
3397
0
  if (ssa_op->op2_use >= 0
3398
0
   && ssa_op->op2_use != ssa_op->op1_use
3399
0
   && jit->ra[ssa_op->op2_use].ref
3400
0
   && (jit->ra[ssa_op->op2_use].flags & ZREG_LAST_USE)
3401
0
   && (ssa_op->op2_use_chain == -1
3402
0
    || zend_ssa_is_no_val_use(ssa_opcodes[ssa_op->op2_use_chain], ssa->ops + ssa_op->op2_use_chain, ssa_op->op2_use))) {
3403
0
    CLEAR_STACK_REF(stack, EX_VAR_TO_NUM(opline->op2.var));
3404
0
  }
3405
0
  if (ssa_op->result_use >= 0
3406
0
   && ssa_op->result_use != ssa_op->op1_use
3407
0
   && ssa_op->result_use != ssa_op->op2_use
3408
0
   && jit->ra[ssa_op->result_use].ref
3409
0
   && (jit->ra[ssa_op->result_use].flags & ZREG_LAST_USE)
3410
0
   && (ssa_op->res_use_chain == -1
3411
0
    || zend_ssa_is_no_val_use(ssa_opcodes[ssa_op->res_use_chain], ssa->ops + ssa_op->res_use_chain, ssa_op->result_use))) {
3412
0
    CLEAR_STACK_REF(stack, EX_VAR_TO_NUM(opline->result.var));
3413
0
  }
3414
0
}
3415
3416
static void zend_jit_trace_setup_ret_counter(const zend_op *opline, size_t offset)
3417
0
{
3418
0
  zend_op *next_opline = (zend_op*)(opline + 1);
3419
3420
0
  if (JIT_G(hot_return) && !ZEND_OP_TRACE_INFO(next_opline, offset)->trace_flags) {
3421
0
    ZEND_ASSERT(zend_jit_ret_trace_counter_handler != NULL);
3422
0
    if (!ZEND_OP_TRACE_INFO(next_opline, offset)->counter) {
3423
0
      ZEND_OP_TRACE_INFO(next_opline, offset)->counter =
3424
0
        &zend_jit_hot_counters[ZEND_JIT_COUNTER_NUM];
3425
0
      ZEND_JIT_COUNTER_NUM = (ZEND_JIT_COUNTER_NUM + 1) % ZEND_HOT_COUNTERS_COUNT;
3426
0
    }
3427
0
    ZEND_OP_TRACE_INFO(next_opline, offset)->trace_flags = ZEND_JIT_TRACE_START_RETURN;
3428
0
    next_opline->handler = zend_jit_ret_trace_counter_handler;
3429
0
  }
3430
0
}
3431
3432
static bool zend_jit_may_delay_fetch_this(const zend_op_array *op_array, zend_ssa *ssa, const zend_op **ssa_opcodes, const zend_ssa_op *ssa_op)
3433
0
{
3434
0
  int var = ssa_op->result_def;
3435
0
  int i;
3436
0
  int use = ssa->vars[var].use_chain;
3437
0
  const zend_op *opline;
3438
3439
0
  if (use < 0
3440
0
   || ssa->vars[var].phi_use_chain
3441
0
   || ssa->ops[use].op1_use != var
3442
0
   || ssa->ops[use].op1_use_chain != -1) {
3443
0
    return false;
3444
0
  }
3445
3446
0
  opline = ssa_opcodes[use];
3447
0
  if (opline->opcode == ZEND_INIT_METHOD_CALL) {
3448
0
    return (opline->op2_type == IS_CONST &&
3449
0
      Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_STRING);
3450
0
  } else if (opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG) {
3451
0
    if (!JIT_G(current_frame)
3452
0
     || !JIT_G(current_frame)->call
3453
0
     || !JIT_G(current_frame)->call->func
3454
0
     || !TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
3455
0
      return false;
3456
0
    }
3457
0
  } else if (opline->opcode != ZEND_FETCH_OBJ_R
3458
0
      && opline->opcode != ZEND_FETCH_OBJ_IS
3459
0
      && opline->opcode != ZEND_FETCH_OBJ_W
3460
0
      && opline->opcode != ZEND_ASSIGN_OBJ
3461
0
      && opline->opcode != ZEND_ASSIGN_OBJ_OP
3462
0
      && opline->opcode != ZEND_PRE_INC_OBJ
3463
0
      && opline->opcode != ZEND_PRE_DEC_OBJ
3464
0
      && opline->opcode != ZEND_POST_INC_OBJ
3465
0
      && opline->opcode != ZEND_POST_DEC_OBJ) {
3466
0
    return false;
3467
0
  }
3468
3469
0
  if (opline->op2_type != IS_CONST
3470
0
   || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
3471
0
   || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
3472
0
    return false;
3473
0
  }
3474
3475
0
  if (opline->opcode == ZEND_ASSIGN_OBJ_OP) {
3476
0
    if (opline->op1_type == IS_CV
3477
0
     && (opline+1)->op1_type == IS_CV
3478
0
     && (opline+1)->op1.var == opline->op1.var) {
3479
      /* skip $a->prop += $a; */
3480
0
      return false;
3481
0
    }
3482
0
    if (!zend_jit_supported_binary_op(
3483
0
        opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
3484
0
      return false;
3485
0
    }
3486
0
  }
3487
3488
0
  for (i = ssa->vars[var].definition; i < use; i++) {
3489
0
    if (ssa_opcodes[i]->opcode == ZEND_DO_UCALL
3490
0
     || ssa_opcodes[i]->opcode == ZEND_DO_FCALL_BY_NAME
3491
0
     || ssa_opcodes[i]->opcode == ZEND_DO_FCALL
3492
0
     || ssa_opcodes[i]->opcode == ZEND_INCLUDE_OR_EVAL) {
3493
0
      return false;
3494
0
    }
3495
0
  }
3496
3497
0
  return true;
3498
0
}
3499
3500
static int zend_jit_trace_stack_needs_deoptimization(zend_jit_trace_stack *stack, uint32_t stack_size)
3501
0
{
3502
0
  uint32_t i;
3503
3504
0
  for (i = 0; i < stack_size; i++) {
3505
0
    if (STACK_FLAGS(stack, i) & ~(ZREG_LOAD|ZREG_STORE|ZREG_LAST_USE)) {
3506
0
      return 1;
3507
0
    } else if (STACK_REG(stack, i) != ZREG_NONE) {
3508
0
      return 1;
3509
0
    }
3510
0
  }
3511
0
  return 0;
3512
0
}
3513
3514
static int zend_jit_trace_exit_needs_deoptimization(uint32_t trace_num, uint32_t exit_num)
3515
0
{
3516
0
  const zend_op *opline = zend_jit_traces[trace_num].exit_info[exit_num].opline;
3517
0
  uint32_t flags = zend_jit_traces[trace_num].exit_info[exit_num].flags;
3518
0
  uint32_t stack_size;
3519
0
  zend_jit_trace_stack *stack;
3520
3521
0
  if (opline || (flags & (ZEND_JIT_EXIT_RESTORE_CALL|ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2|ZEND_JIT_EXIT_CHECK_EXCEPTION))) {
3522
0
    return 1;
3523
0
  }
3524
3525
0
  stack_size = zend_jit_traces[trace_num].exit_info[exit_num].stack_size;
3526
0
  stack = zend_jit_traces[trace_num].stack_map + zend_jit_traces[trace_num].exit_info[exit_num].stack_offset;
3527
0
  return zend_jit_trace_stack_needs_deoptimization(stack, stack_size);
3528
0
}
3529
3530
static int zend_jit_trace_deoptimization(
3531
                                         zend_jit_ctx                   *jit,
3532
                                         const zend_jit_trace_exit_info *exit_info,
3533
                                         zend_jit_trace_stack           *parent_stack,
3534
                                         int                             parent_vars_count,
3535
                                         zend_ssa                       *ssa,
3536
                                         zend_jit_trace_stack           *stack,
3537
                                         zend_jit_exit_const            *constants,
3538
                                         bool                            polymorphic_side_trace)
3539
0
{
3540
0
  uint32_t flags = exit_info->flags;
3541
0
  const zend_op *opline = exit_info->opline;
3542
3543
0
  int i;
3544
0
  int check2 = -1;
3545
3546
  // TODO: Merge this loop with the following register LOAD loop to implement parallel move ???
3547
0
  for (i = 0; i < parent_vars_count; i++) {
3548
0
    int8_t reg = STACK_REG(parent_stack, i);
3549
3550
0
    if (STACK_FLAGS(parent_stack, i) == ZREG_CONST) {
3551
0
      uint8_t type = STACK_TYPE(parent_stack, i);
3552
3553
0
      if (type == IS_LONG) {
3554
0
        if (!zend_jit_store_const_long(jit, i,
3555
0
            (zend_long)constants[STACK_REF(parent_stack, i)].i)) {
3556
0
          return 0;
3557
0
        }
3558
0
      } else if (type == IS_DOUBLE) {
3559
0
        if (!zend_jit_store_const_double(jit, i,
3560
0
            constants[STACK_REF(parent_stack, i)].d)) {
3561
0
          return 0;
3562
0
        }
3563
0
      } else {
3564
0
        ZEND_UNREACHABLE();
3565
0
      }
3566
0
      if (stack) {
3567
0
        SET_STACK_TYPE(stack, i, type, 1);
3568
0
        if (jit->ra && jit->ra[i].ref) {
3569
0
          SET_STACK_REF(stack, i, jit->ra[i].ref);
3570
0
        }
3571
0
      }
3572
0
    } else if (STACK_FLAGS(parent_stack, i) == ZREG_TYPE_ONLY) {
3573
0
      uint8_t type = STACK_TYPE(parent_stack, i);
3574
3575
0
      if (!zend_jit_store_type(jit, i, type)) {
3576
0
        return 0;
3577
0
      }
3578
0
      if (stack) {
3579
0
        SET_STACK_TYPE(stack, i, type, 1);
3580
0
      }
3581
0
    } else if (STACK_FLAGS(parent_stack, i) == ZREG_THIS) {
3582
0
      if (polymorphic_side_trace) {
3583
0
        ssa->var_info[i].delayed_fetch_this = 1;
3584
0
        if (stack) {
3585
0
          SET_STACK_REG_EX(stack, i, ZREG_NONE, ZREG_THIS);
3586
0
        }
3587
0
      } else if (!zend_jit_load_this(jit, EX_NUM_TO_VAR(i))) {
3588
0
        return 0;
3589
0
      }
3590
0
    } else if (STACK_FLAGS(parent_stack, i) == ZREG_ZVAL_ADDREF) {
3591
0
      zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(i));
3592
0
      zend_jit_zval_try_addref(jit, dst);
3593
0
    } else if (STACK_FLAGS(parent_stack, i) == ZREG_ZVAL_COPY) {
3594
0
      ZEND_ASSERT(reg != ZREG_NONE);
3595
0
      ZEND_ASSERT(check2 == -1);
3596
0
      check2 = i;
3597
0
    } else if (STACK_FLAGS(parent_stack, i) & ZREG_SPILL_SLOT) {
3598
0
      if (ssa && ssa->vars[i].no_val) {
3599
        /* pass */
3600
0
      } else {
3601
0
        uint8_t type = STACK_TYPE(parent_stack, i);
3602
3603
0
        if (!zend_jit_store_spill_slot(jit, 1 << type, i, reg, STACK_REF(parent_stack, i),
3604
0
            STACK_MEM_TYPE(parent_stack, i) != type)) {
3605
0
          return 0;
3606
0
        }
3607
0
        if (stack) {
3608
0
          if (jit->ra && jit->ra[i].ref) {
3609
0
            SET_STACK_TYPE(stack, i, type, 0);
3610
0
            if ((STACK_FLAGS(parent_stack, i) & (ZREG_LOAD|ZREG_STORE)) != 0) {
3611
0
              SET_STACK_REF_EX(stack, i, jit->ra[i].ref, ZREG_LOAD);
3612
0
            } else {
3613
0
              SET_STACK_REF(stack, i, jit->ra[i].ref);
3614
0
            }
3615
0
          } else {
3616
0
            SET_STACK_TYPE(stack, i, type, 1);
3617
0
          }
3618
0
        }
3619
0
      }
3620
0
    } else if (reg != ZREG_NONE) {
3621
0
      if (ssa && ssa->vars[i].no_val) {
3622
        /* pass */
3623
0
      } else {
3624
0
        uint8_t type = STACK_TYPE(parent_stack, i);
3625
3626
0
        if (!zend_jit_store_reg(jit, 1 << type, i, reg,
3627
0
            (STACK_FLAGS(parent_stack, i) & (ZREG_LOAD|ZREG_STORE)) != 0,
3628
0
            STACK_MEM_TYPE(parent_stack, i) != type)) {
3629
0
          return 0;
3630
0
        }
3631
0
        if (stack) {
3632
0
          if (jit->ra && jit->ra[i].ref) {
3633
0
            SET_STACK_TYPE(stack, i, type, 0);
3634
0
            if ((STACK_FLAGS(parent_stack, i) & (ZREG_LOAD|ZREG_STORE)) != 0) {
3635
0
              SET_STACK_REF_EX(stack, i, jit->ra[i].ref, ZREG_LOAD);
3636
0
            } else {
3637
0
              SET_STACK_REF(stack, i, jit->ra[i].ref);
3638
0
            }
3639
0
          } else {
3640
0
            SET_STACK_TYPE(stack, i, type, 1);
3641
0
          }
3642
0
        }
3643
0
      }
3644
0
    }
3645
0
  }
3646
3647
0
  if (check2 != -1) {
3648
0
    int8_t reg = STACK_REG(parent_stack, check2);
3649
3650
0
    ZEND_ASSERT(STACK_FLAGS(parent_stack, check2) == ZREG_ZVAL_COPY);
3651
0
    ZEND_ASSERT(reg != ZREG_NONE);
3652
0
    if (!zend_jit_escape_if_undef(jit, check2, flags, opline, reg)) {
3653
0
      return 0;
3654
0
    }
3655
0
    if (!zend_jit_restore_zval(jit, EX_NUM_TO_VAR(check2), reg)) {
3656
0
      return 0;
3657
0
    }
3658
0
  }
3659
3660
0
  if (flags & ZEND_JIT_EXIT_RESTORE_CALL) {
3661
0
    if (!zend_jit_save_call_chain(jit, -1)) {
3662
0
      return 0;
3663
0
    }
3664
0
  }
3665
3666
0
  if (flags & ZEND_JIT_EXIT_FREE_OP2) {
3667
0
    const zend_op *op = opline - 1;
3668
3669
0
    if (!zend_jit_free_op(jit, op, -1, op->op2.var)) {
3670
0
      return 0;
3671
0
    }
3672
0
  }
3673
3674
0
  if (flags & ZEND_JIT_EXIT_FREE_OP1) {
3675
0
    const zend_op *op = opline - 1;
3676
3677
0
    if (!zend_jit_free_op(jit, op, -1, op->op1.var)) {
3678
0
      return 0;
3679
0
    }
3680
0
  }
3681
3682
0
  if (flags & (ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2|ZEND_JIT_EXIT_CHECK_EXCEPTION)) {
3683
0
    zend_jit_check_exception(jit);
3684
0
  }
3685
3686
0
  if (flags & ZEND_JIT_EXIT_METHOD_CALL) {
3687
0
    jit->poly_func_ref = zend_jit_deopt_rload_spilled(jit, IR_ADDR,
3688
0
        exit_info->poly_func.reg, exit_info->poly_func.offset);
3689
0
    jit->poly_this_ref = zend_jit_deopt_rload_spilled(jit, IR_ADDR,
3690
0
        exit_info->poly_this.reg, exit_info->poly_this.offset);
3691
3692
0
    if (!polymorphic_side_trace) {
3693
0
      if (!zend_jit_free_trampoline(jit, jit->poly_func_ref)) {
3694
0
        return 0;
3695
0
      }
3696
0
    }
3697
0
  }
3698
3699
0
  return 1;
3700
0
}
3701
3702
static void zend_jit_trace_set_var_range(zend_ssa_var_info *info, zend_long min, zend_long max)
3703
0
{
3704
0
  info->has_range = 1;
3705
0
  info->range.min = min;
3706
0
  info->range.max = max;
3707
0
  info->range.underflow = 0;
3708
0
  info->range.overflow = 0;
3709
0
}
3710
3711
static void zend_jit_trace_update_condition_ranges(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, zend_ssa *ssa, bool exit_if_true)
3712
0
{
3713
0
  zend_long op1_min, op1_max, op2_min, op2_max;
3714
3715
0
  if ((OP1_INFO() & MAY_BE_ANY) != MAY_BE_LONG
3716
0
   || (OP1_INFO() & MAY_BE_ANY) != MAY_BE_LONG) {
3717
0
    return;
3718
0
  }
3719
3720
0
  op1_min = OP1_MIN_RANGE();
3721
0
  op1_max = OP1_MAX_RANGE();
3722
0
  op2_min = OP2_MIN_RANGE();
3723
0
  op2_max = OP2_MAX_RANGE();
3724
3725
0
  switch (opline->opcode) {
3726
0
    case ZEND_IS_EQUAL:
3727
0
    case ZEND_CASE:
3728
0
    case ZEND_IS_IDENTICAL:
3729
0
    case ZEND_CASE_STRICT:
3730
0
    case ZEND_IS_NOT_IDENTICAL:
3731
0
      if (!exit_if_true) {
3732
        /* op1 == op2 */
3733
0
        if (ssa_op->op1_use >= 0) {
3734
0
          zend_jit_trace_set_var_range(
3735
0
            &ssa->var_info[ssa_op->op1_use],
3736
0
            MAX(op1_min, op2_min),
3737
0
            MIN(op1_max, op2_max));
3738
0
        }
3739
0
        if (ssa_op->op2_use >= 0) {
3740
0
          zend_jit_trace_set_var_range(
3741
0
            &ssa->var_info[ssa_op->op2_use],
3742
0
            MAX(op2_min, op1_min),
3743
0
            MIN(op2_max, op1_max));
3744
0
        }
3745
0
      }
3746
0
      break;
3747
0
    case ZEND_IS_NOT_EQUAL:
3748
0
      if (exit_if_true) {
3749
        /* op1 == op2 */
3750
0
        if (ssa_op->op1_use >= 0) {
3751
0
          zend_jit_trace_set_var_range(
3752
0
            &ssa->var_info[ssa_op->op1_use],
3753
0
            MAX(op1_min, op2_min),
3754
0
            MIN(op1_max, op2_max));
3755
0
        }
3756
0
        if (ssa_op->op2_use >= 0) {
3757
0
          zend_jit_trace_set_var_range(
3758
0
            &ssa->var_info[ssa_op->op2_use],
3759
0
            MAX(op2_min, op1_min),
3760
0
            MIN(op2_max, op1_max));
3761
0
        }
3762
0
      }
3763
0
      break;
3764
0
    case ZEND_IS_SMALLER_OR_EQUAL:
3765
0
      if (!exit_if_true) {
3766
        /* op1 <= op2 */
3767
0
        if (ssa_op->op1_use >= 0) {
3768
0
          zend_jit_trace_set_var_range(
3769
0
            &ssa->var_info[ssa_op->op1_use],
3770
0
            op1_min,
3771
0
            MIN(op1_max, op2_max));
3772
0
        }
3773
0
        if (ssa_op->op2_use >= 0) {
3774
0
          zend_jit_trace_set_var_range(
3775
0
            &ssa->var_info[ssa_op->op2_use],
3776
0
            MAX(op2_min, op1_min),
3777
0
            op2_max);
3778
0
        }
3779
0
      } else {
3780
        /* op1 > op2 */
3781
0
        if (ssa_op->op1_use >= 0) {
3782
0
          zend_jit_trace_set_var_range(
3783
0
            &ssa->var_info[ssa_op->op1_use],
3784
0
            op2_min != ZEND_LONG_MAX ? MAX(op1_min, op2_min + 1) : op1_min,
3785
0
            op1_max);
3786
0
        }
3787
0
        if (ssa_op->op2_use >= 0) {
3788
0
          zend_jit_trace_set_var_range(
3789
0
            &ssa->var_info[ssa_op->op2_use],
3790
0
            op2_min,
3791
0
            op2_max != ZEND_LONG_MIN ?MIN(op2_max, op1_max - 1) : op1_max);
3792
0
        }
3793
0
      }
3794
0
      break;
3795
0
    case ZEND_IS_SMALLER:
3796
0
      if (!exit_if_true) {
3797
        /* op1 < op2 */
3798
0
        if (ssa_op->op1_use >= 0) {
3799
0
          zend_jit_trace_set_var_range(
3800
0
            &ssa->var_info[ssa_op->op1_use],
3801
0
            op1_min,
3802
0
            op2_max != ZEND_LONG_MIN ? MIN(op1_max, op2_max - 1) : op1_max);
3803
0
        }
3804
0
        if (ssa_op->op2_use >= 0) {
3805
0
          zend_jit_trace_set_var_range(
3806
0
            &ssa->var_info[ssa_op->op2_use],
3807
0
            op1_min != ZEND_LONG_MAX ? MAX(op2_min, op1_min + 1) : op2_min,
3808
0
            op2_max);
3809
0
        }
3810
0
      } else {
3811
        /* op1 >= op2 */
3812
0
        if (ssa_op->op1_use >= 0) {
3813
0
          zend_jit_trace_set_var_range(
3814
0
            &ssa->var_info[ssa_op->op1_use],
3815
0
            MAX(op1_min, op2_min),
3816
0
            op1_max);
3817
0
        }
3818
0
        if (ssa_op->op2_use >= 0) {
3819
0
          zend_jit_trace_set_var_range(
3820
0
            &ssa->var_info[ssa_op->op2_use],
3821
0
            op2_min,
3822
0
            MIN(op2_max, op1_max));
3823
0
        }
3824
0
      }
3825
0
      break;
3826
0
  }
3827
0
}
3828
3829
static bool zend_jit_may_skip_comparison(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_ssa *ssa, const zend_op **ssa_opcodes, const zend_op_array *op_array)
3830
0
{
3831
0
  uint8_t prev_opcode;
3832
3833
0
  if (opline->op1_type == IS_CONST
3834
0
   && Z_TYPE_P(RT_CONSTANT(opline, opline->op1)) == IS_LONG
3835
0
   && Z_LVAL_P(RT_CONSTANT(opline, opline->op1)) == 0) {
3836
0
    if (ssa_op->op2_use >= 0) {
3837
0
      if ((ssa_op-1)->op1_def == ssa_op->op2_use) {
3838
0
        ssa_op--;
3839
0
        opline = ssa_opcodes[ssa_op - ssa->ops];
3840
0
        prev_opcode = opline->opcode;
3841
0
        if (prev_opcode == ZEND_PRE_INC
3842
0
         || prev_opcode == ZEND_PRE_DEC
3843
0
         || prev_opcode == ZEND_POST_INC
3844
0
         || prev_opcode == ZEND_POST_DEC) {
3845
0
          return (OP1_INFO() & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-MAY_BE_LONG)) == 0;
3846
0
        }
3847
0
      } else if ((ssa_op-1)->result_def == ssa_op->op2_use) {
3848
0
        ssa_op--;
3849
0
        opline = ssa_opcodes[ssa_op - ssa->ops];
3850
0
        prev_opcode = opline->opcode;
3851
0
        if (prev_opcode == ZEND_ADD
3852
0
         || prev_opcode == ZEND_SUB) {
3853
0
          return (OP1_INFO() & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-MAY_BE_LONG)) == 0 &&
3854
0
            (OP2_INFO() & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-MAY_BE_LONG)) == 0;
3855
0
        }
3856
0
      }
3857
0
    }
3858
0
  } else if (opline->op2_type == IS_CONST
3859
0
   && Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_LONG
3860
0
   && Z_LVAL_P(RT_CONSTANT(opline, opline->op2)) == 0) {
3861
0
    if (ssa_op->op1_use >= 0) {
3862
0
      if ((ssa_op-1)->op1_def == ssa_op->op1_use) {
3863
0
        ssa_op--;
3864
0
        opline = ssa_opcodes[ssa_op - ssa->ops];
3865
0
        prev_opcode = opline->opcode;
3866
0
        if (prev_opcode == ZEND_PRE_INC
3867
0
         || prev_opcode == ZEND_PRE_DEC
3868
0
         || prev_opcode == ZEND_POST_INC
3869
0
         || prev_opcode == ZEND_POST_DEC) {
3870
0
          return (OP1_INFO() & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-MAY_BE_LONG)) == 0;
3871
0
        }
3872
0
      } else if ((ssa_op-1)->result_def == ssa_op->op1_use) {
3873
0
        ssa_op--;
3874
0
        opline = ssa_opcodes[ssa_op - ssa->ops];
3875
0
        prev_opcode = opline->opcode;
3876
0
        if (prev_opcode == ZEND_ADD
3877
0
         || prev_opcode == ZEND_SUB) {
3878
0
          return (OP1_INFO() & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-MAY_BE_LONG)) == 0 &&
3879
0
            (OP2_INFO() & ((MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)-MAY_BE_LONG)) == 0;
3880
0
        }
3881
0
      }
3882
0
    }
3883
0
  } else {
3884
0
    const zend_ssa_op *prev_ssa_op = ssa_op - 1;
3885
0
    prev_opcode = ssa_opcodes[prev_ssa_op - ssa->ops]->opcode;
3886
3887
0
    if ((prev_opcode == ZEND_JMPZ || prev_opcode == ZEND_JMPNZ)
3888
0
     && prev_ssa_op != ssa->ops
3889
0
     && prev_ssa_op->op1_use >= 0
3890
0
     && prev_ssa_op->op1_use == (prev_ssa_op-1)->result_def) {
3891
0
      prev_ssa_op--;
3892
0
      prev_opcode = ssa_opcodes[prev_ssa_op - ssa->ops]->opcode;
3893
0
    }
3894
3895
0
    if (ssa_op->op1_use == prev_ssa_op->op1_use
3896
0
     && ssa_op->op2_use == prev_ssa_op->op2_use) {
3897
0
      if (prev_opcode == ZEND_IS_EQUAL
3898
0
       || prev_opcode == ZEND_IS_NOT_EQUAL
3899
0
       || prev_opcode == ZEND_IS_SMALLER
3900
0
       || prev_opcode == ZEND_IS_SMALLER_OR_EQUAL
3901
0
       || prev_opcode == ZEND_CASE
3902
0
       || prev_opcode == ZEND_IS_IDENTICAL
3903
0
       || prev_opcode == ZEND_IS_NOT_IDENTICAL
3904
0
       || prev_opcode == ZEND_CASE_STRICT) {
3905
0
        if (ssa_op->op1_use < 0) {
3906
0
          if (RT_CONSTANT(opline, opline->op1) != RT_CONSTANT(&ssa_opcodes[prev_ssa_op - ssa->ops], ssa_opcodes[prev_ssa_op - ssa->ops]->op1)) {
3907
0
            return false;
3908
0
          }
3909
0
        }
3910
0
        if (ssa_op->op2_use < 0) {
3911
0
          if (RT_CONSTANT(opline, opline->op2) != RT_CONSTANT(&ssa_opcodes[prev_ssa_op - ssa->ops], ssa_opcodes[prev_ssa_op - ssa->ops]->op2)) {
3912
0
            return false;
3913
0
          }
3914
0
        }
3915
0
        return true;
3916
0
      }
3917
0
    }
3918
0
  }
3919
0
  return false;
3920
0
}
3921
3922
static bool zend_jit_trace_next_is_send_result(const zend_op              *opline,
3923
                                               zend_jit_trace_rec         *p,
3924
                                               zend_jit_trace_stack_frame *frame)
3925
0
{
3926
0
  if (opline->result_type == IS_TMP_VAR
3927
0
   && (p+1)->op == ZEND_JIT_TRACE_VM
3928
0
   && (p+1)->opline == opline + 1
3929
0
   && ((opline+1)->opcode == ZEND_SEND_VAL
3930
0
    || ((opline+1)->opcode == ZEND_SEND_VAL_EX
3931
0
     && frame
3932
0
     && frame->call
3933
0
     && frame->call->func
3934
0
     && !ARG_MUST_BE_SENT_BY_REF(frame->call->func, (opline+1)->op2.num)))
3935
0
   && (opline+1)->op1_type == IS_TMP_VAR
3936
0
   && (opline+1)->op2_type != IS_CONST /* Named parameters not supported in JIT */
3937
0
   && (opline+1)->op1.var == opline->result.var) {
3938
3939
0
    if (frame->call && frame->call->func) {
3940
0
      uint8_t res_type = (p+1)->op1_type;
3941
3942
0
      if (res_type != IS_UNKNOWN && !(res_type & IS_TRACE_REFERENCE) ) {
3943
0
        zend_jit_trace_send_type(opline+1, frame->call, res_type);
3944
0
      }
3945
0
    }
3946
0
    return true;
3947
0
  }
3948
0
  return false;
3949
0
}
3950
3951
static int zend_jit_find_ssa_var(const zend_op_array *op_array,
3952
                                 const zend_ssa      *ssa,
3953
                                 uint32_t             opline_num,
3954
                                 uint32_t             var_num)
3955
0
{
3956
0
  int ssa_var, j, b = ssa->cfg.map[opline_num];
3957
0
  const zend_basic_block *bb = ssa->cfg.blocks + b;
3958
0
  const zend_ssa_phi *phi;
3959
0
  const zend_ssa_op *ssa_op;
3960
0
  zend_worklist worklist;
3961
0
  ALLOCA_FLAG(use_heap)
3962
3963
0
  while (1) {
3964
0
    ssa_op = ssa->ops + opline_num;
3965
0
    ssa_var = ssa_op->result_def;
3966
0
    if (ssa_var >= 0 && ssa->vars[ssa_var].var == var_num) {
3967
0
      return ssa_var;
3968
0
    }
3969
0
    ssa_var = ssa_op->op2_def;
3970
0
    if (ssa_var >= 0 && ssa->vars[ssa_var].var == var_num) {
3971
0
      return ssa_var;
3972
0
    }
3973
0
    ssa_var = ssa_op->op1_def;
3974
0
    if (ssa_var >= 0 && ssa->vars[ssa_var].var == var_num) {
3975
0
      return ssa_var;
3976
0
    }
3977
0
    if (opline_num == bb->start) {
3978
0
      break;
3979
0
    }
3980
0
    opline_num--;
3981
0
  }
3982
0
  phi = ssa->blocks[b].phis;
3983
0
  ssa_var = -1;
3984
0
  while (phi) {
3985
0
    if (phi->var == var_num) {
3986
0
      ssa_var = phi->ssa_var;
3987
0
    }
3988
0
    phi = phi->next;
3989
0
  }
3990
0
  if (ssa_var >= 0) {
3991
0
    return ssa_var;
3992
0
  }
3993
3994
0
  if (!bb->predecessors_count) {
3995
0
    return -1;
3996
0
  }
3997
3998
0
  ZEND_WORKLIST_ALLOCA(&worklist, ssa->cfg.blocks_count, use_heap);
3999
4000
0
  for (j = 0; j < bb->predecessors_count; j++) {
4001
0
    b = ssa->cfg.predecessors[bb->predecessor_offset + j];
4002
0
    zend_worklist_push(&worklist, b);
4003
0
  }
4004
4005
0
  while (zend_worklist_len(&worklist) != 0) {
4006
0
    b = zend_worklist_pop(&worklist);
4007
0
    bb = &ssa->cfg.blocks[b];
4008
0
    if (bb->len) {
4009
0
      opline_num = bb->start + bb->len - 1;
4010
0
      while (1) {
4011
0
        ssa_op = ssa->ops + opline_num;
4012
0
        ssa_var = ssa_op->result_def;
4013
0
        if (ssa_var >= 0 && ssa->vars[ssa_var].var == var_num) {
4014
0
          goto found;
4015
0
        }
4016
0
        ssa_var = ssa_op->op2_def;
4017
0
        if (ssa_var >= 0 && ssa->vars[ssa_var].var == var_num) {
4018
0
          goto found;
4019
0
        }
4020
0
        ssa_var = ssa_op->op1_def;
4021
0
        if (ssa_var >= 0 && ssa->vars[ssa_var].var == var_num) {
4022
0
          goto found;
4023
0
        }
4024
0
        if (opline_num == bb->start) {
4025
0
          break;
4026
0
        }
4027
0
        opline_num--;
4028
0
      }
4029
0
    }
4030
0
    phi = ssa->blocks[b].phis;
4031
0
    ssa_var = -1;
4032
0
    while (phi) {
4033
0
      if (phi->var == var_num) {
4034
0
        ssa_var = phi->ssa_var;
4035
0
      }
4036
0
      phi = phi->next;
4037
0
    }
4038
0
    if (ssa_var >= 0) {
4039
0
      goto found;
4040
0
    }
4041
0
    for (j = 0; j < bb->predecessors_count; j++) {
4042
0
      b = ssa->cfg.predecessors[bb->predecessor_offset + j];
4043
0
      zend_worklist_push(&worklist, b);
4044
0
    }
4045
0
  }
4046
0
  ssa_var = -1;
4047
4048
0
found:
4049
0
  ZEND_WORKLIST_FREE_ALLOCA(&worklist, use_heap);
4050
0
  return ssa_var;
4051
0
}
4052
4053
static bool zend_jit_trace_must_store_type(const zend_op_array *op_array,
4054
                                           const zend_ssa      *ssa,
4055
                                           uint32_t             opline_num,
4056
                                           uint32_t             var_num,
4057
                                           uint8_t              type)
4058
0
{
4059
0
  if (ssa->var_info) {
4060
0
    int ssa_var = zend_jit_find_ssa_var(op_array, ssa, opline_num, var_num);
4061
4062
0
    if (ssa_var >= 0) {
4063
0
      if ((ssa->var_info[ssa_var].type & (MAY_BE_ANY|MAY_BE_UNDEF)) != (1U << type)) {
4064
0
        return false;
4065
0
      }
4066
0
    }
4067
0
  }
4068
0
  return true;
4069
0
}
4070
4071
static bool zend_jit_trace_may_throw(const zend_op       *opline,
4072
                                     const zend_ssa_op   *ssa_op,
4073
                                     const zend_op_array *op_array,
4074
                                     const zend_ssa      *ssa,
4075
                                     uint32_t             t1,
4076
                                     uint32_t             t2,
4077
                                     uint32_t             t3,
4078
                                     uint32_t             val_type)
4079
0
{
4080
0
    switch (opline->opcode) {
4081
0
    case ZEND_ASSIGN_DIM_OP:
4082
0
      if (opline->extended_value != ZEND_CONCAT
4083
0
       && val_type == IS_LONG
4084
0
       && (t1 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == MAY_BE_ARRAY
4085
0
       && MAY_BE_PACKED_ONLY(t1)
4086
0
       && !(t1 & MAY_BE_ARRAY_OF_REF)
4087
0
       && (t2 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == MAY_BE_LONG
4088
0
       && (t3 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) == MAY_BE_LONG) {
4089
0
        return false;
4090
0
      }
4091
0
      break;
4092
0
    default:
4093
0
      break;
4094
0
  }
4095
0
  return zend_may_throw_ex(opline, ssa_op, op_array, ssa, t1, t2);
4096
0
}
4097
4098
static zend_vm_opcode_handler_t zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t parent_trace, uint32_t exit_num)
4099
0
{
4100
0
  zend_vm_opcode_handler_t handler = NULL;
4101
0
  zend_jit_ctx ctx;
4102
0
  zend_jit_ctx *jit = &ctx;
4103
0
  zend_jit_reg_var *ra = NULL;
4104
0
  zend_script *script = NULL;
4105
0
  zend_string *name = NULL;
4106
0
  void *checkpoint;
4107
0
  const zend_op_array *op_array;
4108
0
  zend_ssa *ssa, *op_array_ssa;
4109
0
  const zend_op **ssa_opcodes;
4110
0
  zend_jit_trace_rec *p;
4111
0
  zend_jit_op_array_trace_extension *jit_extension;
4112
0
  int num_op_arrays = 0;
4113
0
  bool do_bailout = 0;
4114
0
  zend_jit_trace_info *t;
4115
0
  const zend_op_array *op_arrays[ZEND_JIT_TRACE_MAX_FUNCS];
4116
0
  uint8_t smart_branch_opcode;
4117
0
  const void *exit_addr;
4118
0
  uint32_t op1_info, op1_def_info, op2_info, res_info, res_use_info, op1_data_info, op1_mem_info;
4119
0
  bool send_result = 0;
4120
0
  bool skip_comparison;
4121
0
  zend_jit_addr op1_addr, op1_def_addr, op2_addr, op2_def_addr, res_addr;
4122
0
  zend_class_entry *ce;
4123
0
  bool ce_is_instanceof;
4124
0
  bool on_this = 0;
4125
0
  bool delayed_fetch_this = 0;
4126
0
  bool avoid_refcounting = 0;
4127
0
  bool polymorphic_side_trace =
4128
0
    parent_trace &&
4129
0
    (zend_jit_traces[parent_trace].exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL);
4130
0
  uint32_t i;
4131
0
  zend_jit_trace_stack_frame *frame, *top, *call;
4132
0
  zend_jit_trace_stack *stack;
4133
0
  uint8_t res_type = IS_UNKNOWN;
4134
0
  const zend_op *opline, *orig_opline;
4135
0
  const zend_ssa_op *ssa_op, *orig_ssa_op;
4136
0
  int checked_stack;
4137
0
  int peek_checked_stack;
4138
0
  uint32_t frame_flags = 0;
4139
4140
0
  JIT_G(current_trace) = trace_buffer;
4141
4142
0
  checkpoint = zend_arena_checkpoint(CG(arena));
4143
4144
0
  zend_try {
4145
4146
0
  ssa = zend_jit_trace_build_tssa(trace_buffer, parent_trace, exit_num, script, op_arrays, &num_op_arrays);
4147
4148
0
  if (!ssa) {
4149
0
    goto jit_cleanup;
4150
0
  }
4151
4152
0
  ssa_opcodes = ((zend_tssa*)ssa)->tssa_opcodes;
4153
4154
0
  op_array = trace_buffer->op_array;
4155
0
  opline = trace_buffer[1].opline;
4156
0
  name = zend_jit_trace_name(op_array, opline->lineno);
4157
0
  zend_jit_trace_start(&ctx, op_array, ssa, name, ZEND_JIT_TRACE_NUM,
4158
0
    parent_trace ? &zend_jit_traces[parent_trace] : NULL, exit_num);
4159
0
  ctx.trace = &zend_jit_traces[ZEND_JIT_TRACE_NUM];
4160
4161
  /* Register allocation */
4162
0
  if ((JIT_G(opt_flags) & (ZEND_JIT_REG_ALLOC_LOCAL|ZEND_JIT_REG_ALLOC_GLOBAL))
4163
0
   && JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) {
4164
0
    ctx.ra = ra = zend_jit_trace_allocate_registers(trace_buffer, ssa, parent_trace, exit_num);
4165
0
  }
4166
4167
0
  p = trace_buffer;
4168
0
  ZEND_ASSERT(p->op == ZEND_JIT_TRACE_START);
4169
0
  op_array = p->op_array;
4170
0
  frame = JIT_G(current_frame);
4171
0
  top = zend_jit_trace_call_frame(frame, op_array, 0);
4172
0
  TRACE_FRAME_INIT(frame, op_array, TRACE_FRAME_MASK_UNKNOWN_RETURN, -1);
4173
0
  frame->used_stack = checked_stack = peek_checked_stack = 0;
4174
0
  stack = frame->stack;
4175
0
  for (i = 0; i < op_array->last_var + op_array->T; i++) {
4176
0
    SET_STACK_TYPE(stack, i, IS_UNKNOWN, 1);
4177
0
  }
4178
4179
0
  opline = p[1].opline;
4180
0
  p += ZEND_JIT_TRACE_START_REC_SIZE;
4181
4182
0
  jit_extension =
4183
0
    (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
4184
0
  op_array_ssa = &jit_extension->func_info.ssa;
4185
4186
0
  if (!parent_trace) {
4187
0
    zend_jit_set_last_valid_opline(&ctx, opline);
4188
0
    zend_jit_track_last_valid_opline(&ctx);
4189
0
  } else {
4190
0
    if (zend_jit_traces[parent_trace].exit_info[exit_num].opline == NULL) {
4191
0
      zend_jit_trace_opline_guard(&ctx, opline);
4192
0
    } else {
4193
0
      zend_jit_reset_last_valid_opline(&ctx);
4194
0
    }
4195
0
  }
4196
4197
0
  if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) {
4198
0
    int last_var;
4199
0
    int parent_vars_count = 0;
4200
0
    zend_jit_trace_stack *parent_stack = NULL;
4201
0
    int used_stack = ((zend_tssa*)ssa)->used_stack;
4202
4203
0
    if (used_stack > 0) {
4204
0
      peek_checked_stack = used_stack;
4205
0
      if (!zend_jit_stack_check(&ctx, opline, used_stack)) {
4206
0
        goto jit_failure;
4207
0
      }
4208
0
    }
4209
4210
0
    if (parent_trace) {
4211
0
      parent_vars_count = MIN(zend_jit_traces[parent_trace].exit_info[exit_num].stack_size,
4212
0
        op_array->last_var + op_array->T);
4213
0
      if (parent_vars_count) {
4214
0
        parent_stack =
4215
0
          zend_jit_traces[parent_trace].stack_map +
4216
0
          zend_jit_traces[parent_trace].exit_info[exit_num].stack_offset;
4217
0
      }
4218
0
    }
4219
4220
0
    last_var = op_array->last_var;
4221
0
    if (trace_buffer->start != ZEND_JIT_TRACE_START_ENTER) {
4222
0
      last_var += op_array->T;
4223
0
    }
4224
4225
0
    for (i = 0; i < last_var; i++) {
4226
0
      uint32_t info = ssa->var_info[i].type;
4227
4228
0
      if (!(info & MAY_BE_GUARD) && has_concrete_type(info)) {
4229
0
        uint8_t type, mem_type;
4230
4231
0
        type = concrete_type(info);
4232
0
        if (i < parent_vars_count
4233
0
         && STACK_TYPE(parent_stack, i) == type) {
4234
0
          mem_type = STACK_MEM_TYPE(parent_stack, i);
4235
0
          if (mem_type != IS_UNKNOWN) {
4236
0
            SET_STACK_TYPE(stack, i, mem_type, 1);
4237
0
          }
4238
0
          SET_STACK_TYPE(stack, i, type, 0);
4239
0
        } else {
4240
0
          SET_STACK_TYPE(stack, i, type, 1);
4241
0
        }
4242
0
      } else if (ssa->vars[i].alias != NO_ALIAS) {
4243
0
        SET_STACK_TYPE(stack, i, IS_UNKNOWN, 1);
4244
0
      } else if (i < parent_vars_count
4245
0
       && STACK_TYPE(parent_stack, i) != IS_UNKNOWN) {
4246
        /* This must be already handled by trace type inference */
4247
0
        ZEND_ASSERT(ssa->vars[i].use_chain < 0 && !ssa->vars[i].phi_use_chain);
4248
0
        SET_STACK_TYPE(stack, i, STACK_TYPE(parent_stack, i), 1);
4249
0
      } else if ((info & MAY_BE_GUARD) != 0
4250
0
       && (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
4251
0
        || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
4252
0
        || (trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET
4253
0
         && EX_VAR_TO_NUM((opline-1)->result.var) == i))
4254
0
       && (ssa->vars[i].use_chain != -1
4255
0
        || (ssa->vars[i].phi_use_chain
4256
0
         && !(ssa->var_info[ssa->vars[i].phi_use_chain->ssa_var].type & MAY_BE_GUARD)))) {
4257
        /* Check loop-invariant variable type */
4258
0
        if (!zend_jit_type_guard(&ctx, opline, EX_NUM_TO_VAR(i), concrete_type(info))) {
4259
0
          goto jit_failure;
4260
0
        }
4261
0
        info &= ~MAY_BE_GUARD;
4262
0
        ssa->var_info[i].type = info;
4263
0
        SET_STACK_TYPE(stack, i, concrete_type(info), 1);
4264
0
      } else if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER
4265
0
       && op_array->function_name
4266
0
       && i >= op_array->num_args) {
4267
        /* This must be already handled by trace type inference */
4268
0
        ZEND_UNREACHABLE();
4269
        // SET_STACK_TYPE(stack, i, IS_UNDEF, 1);
4270
0
      }
4271
4272
0
      if ((info & MAY_BE_PACKED_GUARD) != 0
4273
0
       && (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
4274
0
        || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
4275
0
        || (trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET
4276
0
         && EX_VAR_TO_NUM((opline-1)->result.var) == i))
4277
0
       && (ssa->vars[i].use_chain != -1
4278
0
        || (ssa->vars[i].phi_use_chain
4279
0
         && !(ssa->var_info[ssa->vars[i].phi_use_chain->ssa_var].type & MAY_BE_PACKED_GUARD)))) {
4280
0
        ZEND_ASSERT(STACK_TYPE(stack, i) == IS_ARRAY);
4281
4282
0
        if (!zend_jit_packed_guard(&ctx, opline, EX_NUM_TO_VAR(i), info)) {
4283
0
          goto jit_failure;
4284
0
        }
4285
0
        info &= ~MAY_BE_PACKED_GUARD;
4286
0
        ssa->var_info[i].type = info;
4287
0
      }
4288
0
    }
4289
4290
0
    if (parent_trace) {
4291
      /* Deoptimization */
4292
0
      if (!zend_jit_trace_deoptimization(&ctx,
4293
0
          &zend_jit_traces[parent_trace].exit_info[exit_num],
4294
0
          parent_stack, parent_vars_count, ssa, stack,
4295
0
          zend_jit_traces[parent_trace].constants,
4296
0
          polymorphic_side_trace)) {
4297
0
        goto jit_failure;
4298
0
      }
4299
0
    }
4300
4301
0
    if (ra
4302
0
     && trace_buffer->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
4303
0
     && trace_buffer->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
4304
0
      for (i = 0; i < last_var; i++) {
4305
0
        if (RA_HAS_REG(i)
4306
0
         && (RA_REG_FLAGS(i) & ZREG_LOAD) != 0
4307
0
         && ra[i].ref != STACK_REF(stack, i)
4308
0
        ) {
4309
4310
0
          if ((ssa->var_info[i].type & MAY_BE_GUARD) != 0) {
4311
0
            uint8_t op_type;
4312
4313
0
            ssa->var_info[i].type &= ~MAY_BE_GUARD;
4314
0
            op_type = concrete_type(ssa->var_info[i].type);
4315
0
            if (!zend_jit_type_guard(&ctx, opline, EX_NUM_TO_VAR(i), op_type)) {
4316
0
              goto jit_failure;
4317
0
            }
4318
0
            SET_STACK_TYPE(stack, i, op_type, 1);
4319
0
          }
4320
4321
0
          if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP) {
4322
0
            if (!zend_jit_load_var(&ctx, ssa->var_info[i].type, i, i)) {
4323
0
              goto jit_failure;
4324
0
            }
4325
0
            SET_STACK_REF_EX(stack, i, ra[i].ref, ZREG_LOAD);
4326
0
          } else {
4327
0
            SET_STACK_REF_EX(stack, i, IR_NULL, ZREG_LOAD);
4328
0
          }
4329
0
        }
4330
0
      }
4331
0
    }
4332
0
  }
4333
4334
0
  if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
4335
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
4336
0
   || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
4337
4338
0
    jit->trace_loop_ref = zend_jit_trace_begin_loop(&ctx); /* start of of trace loop */
4339
4340
0
    if (ra) {
4341
0
      zend_ssa_phi *phi = ssa->blocks[1].phis;
4342
4343
      /* First try to insert IR Phi */
4344
0
      while (phi) {
4345
0
        if (RA_HAS_REG(phi->ssa_var)
4346
0
         && !(RA_REG_FLAGS(phi->ssa_var) & ZREG_LOAD)) {
4347
0
          zend_jit_trace_gen_phi(&ctx, phi);
4348
0
          SET_STACK_REF(stack, phi->var, ra[phi->ssa_var].ref);
4349
0
        }
4350
0
        phi = phi->next;
4351
0
      }
4352
4353
0
      phi = ssa->blocks[1].phis;
4354
0
      while (phi) {
4355
0
        if (RA_HAS_REG(phi->ssa_var)) {
4356
0
          if (RA_REG_FLAGS(phi->ssa_var) & ZREG_LOAD) {
4357
0
            uint32_t info = ssa->var_info[phi->ssa_var].type;
4358
4359
0
            if (info & MAY_BE_GUARD) {
4360
0
              if (!zend_jit_type_guard(&ctx, opline, EX_NUM_TO_VAR(phi->var), concrete_type(info))) {
4361
0
                goto jit_failure;
4362
0
              }
4363
0
              info &= ~MAY_BE_GUARD;
4364
0
              ssa->var_info[phi->ssa_var].type = info;
4365
0
              SET_STACK_TYPE(stack, phi->var, concrete_type(info), 1);
4366
0
            }
4367
0
            if (!zend_jit_load_var(&ctx, ssa->var_info[phi->ssa_var].type, ssa->vars[phi->ssa_var].var, phi->ssa_var)) {
4368
0
              goto jit_failure;
4369
0
            }
4370
0
            SET_STACK_REF_EX(stack, phi->var, ra[phi->ssa_var].ref, ZREG_LOAD);
4371
0
          } else if (RA_REG_FLAGS(phi->ssa_var) & ZREG_STORE) {
4372
4373
0
            if (!zend_jit_store_var(&ctx, ssa->var_info[phi->ssa_var].type, ssa->vars[phi->ssa_var].var, phi->ssa_var,
4374
0
                STACK_MEM_TYPE(stack, phi->var) != ssa->var_info[phi->ssa_var].type)) {
4375
0
              goto jit_failure;
4376
0
            }
4377
0
            SET_STACK_REF_EX(stack, phi->var, ra[phi->ssa_var].ref, ZREG_STORE);
4378
0
          } else {
4379
            /* Register has to be written back on side exit */
4380
0
            SET_STACK_REF(stack, phi->var, ra[phi->ssa_var].ref);
4381
0
          }
4382
0
        }
4383
0
        phi = phi->next;
4384
0
      }
4385
0
    }
4386
4387
//    if (trace_buffer->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
4388
//      if (ra && dzend_jit_trace_stack_needs_deoptimization(stack, op_array->last_var + op_array->T)) {
4389
//        uint32_t exit_point = zend_jit_trace_get_exit_point(opline, ZEND_JIT_EXIT_TO_VM);
4390
//
4391
//        timeout_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
4392
//        if (!timeout_exit_addr) {
4393
//          goto jit_failure;
4394
//        }
4395
//      }
4396
//    }
4397
4398
0
  }
4399
4400
0
  ssa_op = (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) ? ssa->ops : NULL;
4401
0
  for (;;p++) {
4402
0
    if (p->op == ZEND_JIT_TRACE_VM) {
4403
0
      uint8_t op1_type = p->op1_type;
4404
0
      uint8_t op2_type = p->op2_type;
4405
0
      uint8_t op3_type = p->op3_type;
4406
0
      uint8_t orig_op1_type = op1_type;
4407
0
      uint8_t orig_op2_type = op2_type;
4408
0
      uint8_t val_type = IS_UNKNOWN;
4409
0
      bool op1_indirect;
4410
0
      zend_class_entry *op1_ce = NULL;
4411
0
      zend_class_entry *op2_ce = NULL;
4412
0
      bool gen_handler = false;
4413
4414
0
      opline = p->opline;
4415
0
      if (op1_type & (IS_TRACE_REFERENCE|IS_TRACE_INDIRECT)) {
4416
0
        op1_type = IS_UNKNOWN;
4417
0
      }
4418
0
      if (op1_type != IS_UNKNOWN) {
4419
0
        op1_type &= ~IS_TRACE_PACKED;
4420
0
      }
4421
0
      if (op2_type & (IS_TRACE_REFERENCE|IS_TRACE_INDIRECT)) {
4422
0
        op2_type = IS_UNKNOWN;
4423
0
      }
4424
0
      if (op3_type & (IS_TRACE_REFERENCE|IS_TRACE_INDIRECT)) {
4425
0
        op3_type = IS_UNKNOWN;
4426
0
      }
4427
4428
0
      if ((p+1)->op == ZEND_JIT_TRACE_OP1_TYPE) {
4429
0
        op1_ce = (zend_class_entry*)(p+1)->ce;
4430
0
        p++;
4431
0
      }
4432
0
      if ((p+1)->op == ZEND_JIT_TRACE_OP2_TYPE) {
4433
0
        op2_ce = (zend_class_entry*)(p+1)->ce;
4434
0
        p++;
4435
0
      }
4436
0
      if ((p+1)->op == ZEND_JIT_TRACE_VAL_INFO) {
4437
0
        val_type = (p+1)->op1_type;
4438
0
        p++;
4439
0
      }
4440
4441
0
      frame_flags = 0;
4442
4443
0
      if (zend_jit_inc_call_level(opline->opcode)) {
4444
0
        frame->call_level++;
4445
0
      }
4446
4447
0
      if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) {
4448
0
        switch (opline->opcode) {
4449
0
          case ZEND_PRE_INC:
4450
0
          case ZEND_PRE_DEC:
4451
0
          case ZEND_POST_INC:
4452
0
          case ZEND_POST_DEC:
4453
0
            if (opline->op1_type != IS_CV) {
4454
0
              break;
4455
0
            }
4456
0
            op1_info = OP1_INFO();
4457
0
            CHECK_OP1_TRACE_TYPE();
4458
0
            if (!(op1_info & MAY_BE_LONG)) {
4459
0
              break;
4460
0
            }
4461
0
            if (opline->result_type != IS_UNUSED) {
4462
0
              res_use_info = zend_jit_trace_type_to_info(
4463
0
                STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var)));
4464
0
              if (opline->result_type == IS_CV) {
4465
0
                res_use_info &= (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE);
4466
0
              }
4467
0
              res_info = RES_INFO();
4468
0
              res_addr = RES_REG_ADDR();
4469
0
            } else {
4470
0
              res_use_info = -1;
4471
0
              res_info = -1;
4472
0
              res_addr = 0;
4473
0
            }
4474
0
            op1_def_info = OP1_DEF_INFO();
4475
0
            if (op1_def_info & MAY_BE_GUARD
4476
0
             && !has_concrete_type(op1_def_info)) {
4477
0
              op1_def_info &= ~MAY_BE_GUARD;
4478
0
            }
4479
0
            if (!zend_jit_inc_dec(&ctx, opline,
4480
0
                op1_info, OP1_REG_ADDR(),
4481
0
                op1_def_info, OP1_DEF_REG_ADDR(),
4482
0
                res_use_info, res_info,
4483
0
                res_addr,
4484
0
                (op1_def_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) && zend_may_overflow(opline, ssa_op, op_array, ssa),
4485
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
4486
0
              goto jit_failure;
4487
0
            }
4488
0
            if ((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)
4489
0
             && !(op1_info & MAY_BE_STRING)) {
4490
0
              ssa->var_info[ssa_op->op1_def].type &= ~MAY_BE_GUARD;
4491
0
              if (opline->result_type != IS_UNUSED) {
4492
0
                ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
4493
0
              }
4494
0
            } else if ((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_DOUBLE|MAY_BE_GUARD)
4495
0
             && !(op1_info & MAY_BE_STRING)) {
4496
0
              ssa->var_info[ssa_op->op1_def].type &= ~MAY_BE_GUARD;
4497
0
              if (opline->result_type != IS_UNUSED) {
4498
0
                ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
4499
0
              }
4500
0
            }
4501
0
            if (opline->result_type != IS_UNUSED
4502
0
             && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)
4503
0
             && !(op1_info & MAY_BE_STRING)) {
4504
0
              ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
4505
0
            } else if (opline->result_type != IS_UNUSED
4506
0
             && (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_DOUBLE|MAY_BE_GUARD)
4507
0
             && !(res_info & MAY_BE_STRING)) {
4508
0
              ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
4509
0
            }
4510
0
            goto done;
4511
0
          case ZEND_BW_OR:
4512
0
          case ZEND_BW_AND:
4513
0
          case ZEND_BW_XOR:
4514
0
          case ZEND_SL:
4515
0
          case ZEND_SR:
4516
0
          case ZEND_MOD:
4517
0
            op1_info = OP1_INFO();
4518
0
            CHECK_OP1_TRACE_TYPE();
4519
0
            op2_info = OP2_INFO();
4520
0
            CHECK_OP2_TRACE_TYPE();
4521
0
            if (!(op1_info & MAY_BE_LONG)
4522
0
             || !(op2_info & MAY_BE_LONG)) {
4523
0
              break;
4524
0
            }
4525
0
            res_addr = RES_REG_ADDR();
4526
0
            if (Z_MODE(res_addr) != IS_REG
4527
0
             && zend_jit_trace_next_is_send_result(opline, p, frame)) {
4528
0
              send_result = 1;
4529
0
              res_use_info = -1;
4530
0
              res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
4531
0
              if (!zend_jit_reuse_ip(&ctx)) {
4532
0
                goto jit_failure;
4533
0
              }
4534
0
            } else {
4535
0
              res_use_info = zend_jit_trace_type_to_info(
4536
0
                STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var)));
4537
0
              if (opline->result_type == IS_CV) {
4538
0
                res_use_info &= (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE);
4539
0
              }
4540
0
            }
4541
0
            res_info = RES_INFO();
4542
0
            if (!zend_jit_long_math(&ctx, opline,
4543
0
                op1_info, OP1_RANGE(), OP1_REG_ADDR(),
4544
0
                op2_info, OP2_RANGE(), OP2_REG_ADDR(),
4545
0
                res_use_info, res_info, res_addr,
4546
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
4547
0
              goto jit_failure;
4548
0
            }
4549
0
            goto done;
4550
0
          case ZEND_ADD:
4551
0
          case ZEND_SUB:
4552
0
          case ZEND_MUL:
4553
//          case ZEND_DIV: // TODO: check for division by zero ???
4554
0
            op1_info = OP1_INFO();
4555
0
            op1_addr = OP1_REG_ADDR();
4556
0
            op2_info = OP2_INFO();
4557
0
            op2_addr = OP2_REG_ADDR();
4558
0
            if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
4559
0
              break;
4560
0
            }
4561
0
            if (opline->opcode == ZEND_ADD &&
4562
0
                (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY &&
4563
0
                (op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
4564
              /* pass */
4565
0
            } else if (!(op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) ||
4566
0
                !(op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
4567
0
              break;
4568
0
            }
4569
0
            if (orig_op1_type != IS_UNKNOWN
4570
0
             && (orig_op1_type & IS_TRACE_REFERENCE)
4571
0
             && opline->op1_type == IS_CV
4572
0
             && (orig_op2_type == IS_UNKNOWN || !(orig_op2_type & IS_TRACE_REFERENCE))) {
4573
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
4574
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
4575
0
                goto jit_failure;
4576
0
              }
4577
0
              if (ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
4578
0
                ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
4579
0
              }
4580
0
            } else {
4581
0
              CHECK_OP1_TRACE_TYPE();
4582
0
            }
4583
0
            if (orig_op2_type != IS_UNKNOWN
4584
0
             && (orig_op2_type & IS_TRACE_REFERENCE)
4585
0
             && opline->op2_type == IS_CV
4586
0
             && (orig_op1_type == IS_UNKNOWN || !(orig_op1_type & IS_TRACE_REFERENCE))) {
4587
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op2_type, &op2_info, &op2_addr,
4588
0
                  !ssa->var_info[ssa_op->op2_use].guarded_reference, 1)) {
4589
0
                goto jit_failure;
4590
0
              }
4591
0
              if (ssa->vars[ssa_op->op2_use].alias == NO_ALIAS) {
4592
0
                ssa->var_info[ssa_op->op2_use].guarded_reference = 1;
4593
0
              }
4594
0
            } else {
4595
0
              CHECK_OP2_TRACE_TYPE();
4596
0
            }
4597
0
            res_addr = RES_REG_ADDR();
4598
0
            if (Z_MODE(res_addr) != IS_REG
4599
0
             && zend_jit_trace_next_is_send_result(opline, p, frame)) {
4600
0
              send_result = 1;
4601
0
              res_use_info = -1;
4602
0
              res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
4603
0
              if (!zend_jit_reuse_ip(&ctx)) {
4604
0
                goto jit_failure;
4605
0
              }
4606
0
            } else {
4607
0
              res_use_info = zend_jit_trace_type_to_info(
4608
0
                STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var)));
4609
0
              if (opline->result_type == IS_CV) {
4610
0
                res_use_info &= (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE);
4611
0
              }
4612
0
            }
4613
0
            res_info = RES_INFO();
4614
0
            if (opline->opcode == ZEND_ADD &&
4615
0
                (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY &&
4616
0
                (op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
4617
0
              if (!zend_jit_add_arrays(&ctx, opline, op1_info, op1_addr, op2_info, op2_addr, res_addr)) {
4618
0
                goto jit_failure;
4619
0
              }
4620
0
            } else {
4621
0
              bool may_overflow = (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (res_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) && zend_may_overflow(opline, ssa_op, op_array, ssa);
4622
4623
0
              if (ra
4624
0
               && may_overflow
4625
0
               && ((res_info & MAY_BE_GUARD)
4626
0
               && (res_info & MAY_BE_ANY) == MAY_BE_LONG)
4627
0
               && ((opline->opcode == ZEND_ADD
4628
0
                 && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1)
4629
0
                || (opline->opcode == ZEND_SUB
4630
0
                 && Z_MODE(op2_addr) == IS_CONST_ZVAL && Z_LVAL_P(Z_ZV(op2_addr)) == 1))) {
4631
0
                zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
4632
0
              }
4633
0
              if (!zend_jit_math(&ctx, opline,
4634
0
                  op1_info, op1_addr,
4635
0
                  op2_info, op2_addr,
4636
0
                  res_use_info, res_info, res_addr,
4637
0
                  may_overflow,
4638
0
                  zend_may_throw(opline, ssa_op, op_array, ssa))) {
4639
0
                goto jit_failure;
4640
0
              }
4641
0
              if (((res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)
4642
0
                || (res_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_DOUBLE|MAY_BE_GUARD))
4643
0
               && has_concrete_type(op1_info)
4644
0
               && (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))
4645
0
               && has_concrete_type(op2_info)
4646
0
               && (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
4647
0
                ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
4648
0
              }
4649
0
            }
4650
0
            goto done;
4651
0
          case ZEND_CONCAT:
4652
0
          case ZEND_FAST_CONCAT:
4653
0
            op1_info = OP1_INFO();
4654
0
            CHECK_OP1_TRACE_TYPE();
4655
0
            op2_info = OP2_INFO();
4656
0
            CHECK_OP2_TRACE_TYPE();
4657
0
            if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
4658
0
              break;
4659
0
            }
4660
0
            if (!(op1_info & MAY_BE_STRING) ||
4661
0
                !(op2_info & MAY_BE_STRING)) {
4662
0
              break;
4663
0
            }
4664
0
            res_addr = RES_REG_ADDR();
4665
0
            if (zend_jit_trace_next_is_send_result(opline, p, frame)) {
4666
0
              send_result = 1;
4667
0
              res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
4668
0
              if (!zend_jit_reuse_ip(&ctx)) {
4669
0
                goto jit_failure;
4670
0
              }
4671
0
            }
4672
0
            if (!zend_jit_concat(&ctx, opline,
4673
0
                op1_info, op2_info, res_addr,
4674
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
4675
0
              goto jit_failure;
4676
0
            }
4677
0
            goto done;
4678
0
          case ZEND_ASSIGN_OP:
4679
0
            if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
4680
0
              break;
4681
0
            }
4682
0
            op1_info = OP1_INFO();
4683
0
            CHECK_OP1_TRACE_TYPE();
4684
0
            op2_info = OP2_INFO();
4685
0
            CHECK_OP2_TRACE_TYPE();
4686
0
            if (!zend_jit_supported_binary_op(
4687
0
                opline->extended_value, op1_info, op2_info)) {
4688
0
              break;
4689
0
            }
4690
0
            op1_addr = OP1_REG_ADDR();
4691
0
            if (Z_MODE(op1_addr) != IS_REG
4692
0
             || Z_LOAD(op1_addr)
4693
0
             || Z_STORE(op1_addr)) {
4694
0
              op1_mem_info = op1_info;
4695
0
            } else {
4696
0
              op1_mem_info = zend_jit_trace_type_to_info(
4697
0
                STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var)));
4698
0
            }
4699
0
            op1_def_info = OP1_DEF_INFO();
4700
0
            if (op1_def_info & MAY_BE_GUARD
4701
0
             && !has_concrete_type(op1_def_info)) {
4702
0
              op1_def_info &= ~MAY_BE_GUARD;
4703
0
            }
4704
0
            if (!zend_jit_assign_op(&ctx, opline,
4705
0
                op1_info, op1_addr, OP1_RANGE(),
4706
0
                op1_def_info, OP1_DEF_REG_ADDR(), op1_mem_info,
4707
0
                op2_info, OP2_REG_ADDR(), OP2_RANGE(),
4708
0
                (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (op1_def_info & (MAY_BE_DOUBLE|MAY_BE_GUARD)) && zend_may_overflow(opline, ssa_op, op_array, ssa),
4709
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
4710
0
              goto jit_failure;
4711
0
            }
4712
0
            if ((op1_def_info & (MAY_BE_ANY|MAY_BE_GUARD)) == (MAY_BE_LONG|MAY_BE_GUARD)
4713
0
             && has_concrete_type(op1_info)
4714
0
             && has_concrete_type(op2_info)) {
4715
0
              ssa->var_info[ssa_op->op1_def].type &= ~MAY_BE_GUARD;
4716
0
              if (opline->result_type != IS_UNUSED) {
4717
0
                ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
4718
0
              }
4719
0
            }
4720
0
            goto done;
4721
0
          case ZEND_ASSIGN_DIM_OP:
4722
0
            if (opline->result_type != IS_UNUSED) {
4723
0
              break;
4724
0
            }
4725
0
            if (!zend_jit_supported_binary_op(
4726
0
                opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
4727
0
              break;
4728
0
            }
4729
0
            if (opline->op1_type == IS_CV
4730
0
             && (opline+1)->op1_type == IS_CV
4731
0
             && (opline+1)->op1.var == opline->op1.var) {
4732
              /* skip $a[x] += $a; */
4733
0
              break;
4734
0
            }
4735
0
            op1_info = OP1_INFO();
4736
0
            op1_addr = OP1_REG_ADDR();
4737
0
            op1_indirect = 0;
4738
0
            if (opline->op1_type == IS_VAR) {
4739
0
              if (orig_op1_type != IS_UNKNOWN
4740
0
               && (orig_op1_type & IS_TRACE_INDIRECT)) {
4741
0
                op1_indirect = 1;
4742
0
                if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
4743
0
                    &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
4744
0
                  goto jit_failure;
4745
0
                }
4746
0
              }
4747
0
            }
4748
0
            if (orig_op1_type != IS_UNKNOWN
4749
0
             && (orig_op1_type & IS_TRACE_REFERENCE)) {
4750
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
4751
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
4752
0
                goto jit_failure;
4753
0
              }
4754
0
              if (opline->op1_type == IS_CV
4755
0
               && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
4756
0
                ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
4757
0
              }
4758
0
            } else {
4759
0
              CHECK_OP1_TRACE_TYPE();
4760
0
            }
4761
0
            op2_info = OP2_INFO();
4762
0
            CHECK_OP2_TRACE_TYPE();
4763
0
            op1_data_info = OP1_DATA_INFO();
4764
0
            CHECK_OP1_DATA_TRACE_TYPE();
4765
0
            op1_def_info = OP1_DEF_INFO();
4766
0
            if (!zend_jit_assign_dim_op(&ctx, opline,
4767
0
                op1_info, op1_def_info, op1_addr, op1_indirect,
4768
0
                op2_info, (opline->op2_type != IS_UNUSED) ? OP2_REG_ADDR() : 0,
4769
0
                (opline->op2_type != IS_UNUSED) ? OP2_RANGE() : NULL,
4770
0
                op1_data_info, OP1_DATA_REG_ADDR(), OP1_DATA_RANGE(), val_type,
4771
0
                zend_jit_trace_may_throw(opline, ssa_op, op_array, ssa,
4772
0
                  op1_info, op2_info, op1_data_info, val_type))) {
4773
0
              goto jit_failure;
4774
0
            }
4775
0
            if (opline->op1_type == IS_VAR && !(op1_info & (MAY_BE_ANY-MAY_BE_NULL))) {
4776
0
              SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_ARRAY, 1);
4777
0
            }
4778
0
            goto done;
4779
0
          case ZEND_PRE_INC_OBJ:
4780
0
          case ZEND_PRE_DEC_OBJ:
4781
0
          case ZEND_POST_INC_OBJ:
4782
0
          case ZEND_POST_DEC_OBJ:
4783
0
            if (opline->op2_type != IS_CONST
4784
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
4785
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
4786
0
              break;
4787
0
            }
4788
0
            ce = NULL;
4789
0
            ce_is_instanceof = 0;
4790
0
            on_this = delayed_fetch_this = 0;
4791
0
            op1_indirect = 0;
4792
0
            if (opline->op1_type == IS_UNUSED) {
4793
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
4794
0
              ce = op_array->scope;
4795
              /* scope is NULL for closures. */
4796
0
              if (ce) {
4797
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
4798
0
              }
4799
0
              op1_addr = 0;
4800
0
              on_this = 1;
4801
0
            } else {
4802
0
              if (ssa_op->op1_use >= 0) {
4803
0
                delayed_fetch_this = ssa->var_info[ssa_op->op1_use].delayed_fetch_this;
4804
0
              }
4805
0
              op1_info = OP1_INFO();
4806
0
              if (!(op1_info & MAY_BE_OBJECT)) {
4807
0
                break;
4808
0
              }
4809
0
              op1_addr = OP1_REG_ADDR();
4810
0
              if (opline->op1_type == IS_VAR) {
4811
0
                if (orig_op1_type != IS_UNKNOWN
4812
0
                 && (orig_op1_type & IS_TRACE_INDIRECT)) {
4813
0
                  op1_indirect = 1;
4814
0
                  if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
4815
0
                      &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
4816
0
                    goto jit_failure;
4817
0
                  }
4818
0
                }
4819
0
              }
4820
0
              if (orig_op1_type != IS_UNKNOWN
4821
0
               && (orig_op1_type & IS_TRACE_REFERENCE)) {
4822
0
                if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
4823
0
                    !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
4824
0
                  goto jit_failure;
4825
0
                }
4826
0
                if (opline->op1_type == IS_CV
4827
0
                 && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
4828
0
                  ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
4829
0
                }
4830
0
              } else {
4831
0
                CHECK_OP1_TRACE_TYPE();
4832
0
              }
4833
0
              if (!(op1_info & MAY_BE_OBJECT)) {
4834
0
                break;
4835
0
              }
4836
0
              if (ssa->var_info && ssa->ops) {
4837
0
                if (ssa_op->op1_use >= 0) {
4838
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
4839
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
4840
0
                    ce = op1_ssa->ce;
4841
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
4842
0
                  }
4843
0
                }
4844
0
              }
4845
0
              if (delayed_fetch_this) {
4846
0
                on_this = 1;
4847
0
              } else if (ssa_op->op1_use >= 0 && ssa->vars[ssa_op->op1_use].definition >= 0) {
4848
0
                on_this = ssa_opcodes[ssa->vars[ssa_op->op1_use].definition]->opcode == ZEND_FETCH_THIS;
4849
0
              } else if (op_array_ssa->ops
4850
0
                      && op_array_ssa->vars
4851
0
                  && op_array_ssa->ops[opline-op_array->opcodes].op1_use >= 0
4852
0
                  && op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition >= 0) {
4853
0
                on_this = op_array->opcodes[op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition].opcode == ZEND_FETCH_THIS;
4854
0
              }
4855
0
            }
4856
0
            if (!zend_jit_incdec_obj(&ctx, opline, op_array, ssa, ssa_op,
4857
0
                op1_info, op1_addr,
4858
0
                op1_indirect, ce, ce_is_instanceof, on_this, delayed_fetch_this, op1_ce,
4859
0
                val_type)) {
4860
0
              goto jit_failure;
4861
0
            }
4862
0
            goto done;
4863
0
          case ZEND_ASSIGN_OBJ_OP:
4864
0
            if (opline->result_type != IS_UNUSED) {
4865
0
              break;
4866
0
            }
4867
0
            if (opline->op2_type != IS_CONST
4868
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
4869
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
4870
0
              break;
4871
0
            }
4872
0
            if (opline->op1_type == IS_CV
4873
0
             && (opline+1)->op1_type == IS_CV
4874
0
             && (opline+1)->op1.var == opline->op1.var) {
4875
              /* skip $a->prop += $a; */
4876
0
              break;
4877
0
            }
4878
0
            if (!zend_jit_supported_binary_op(
4879
0
                opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
4880
0
              break;
4881
0
            }
4882
0
            ce = NULL;
4883
0
            ce_is_instanceof = 0;
4884
0
            on_this = delayed_fetch_this = 0;
4885
0
            op1_indirect = 0;
4886
0
            if (opline->op1_type == IS_UNUSED) {
4887
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
4888
0
              ce = op_array->scope;
4889
              /* scope is NULL for closures. */
4890
0
              if (ce) {
4891
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
4892
0
              }
4893
0
              op1_addr = 0;
4894
0
              on_this = 1;
4895
0
            } else {
4896
0
              if (ssa_op->op1_use >= 0) {
4897
0
                delayed_fetch_this = ssa->var_info[ssa_op->op1_use].delayed_fetch_this;
4898
0
              }
4899
0
              op1_info = OP1_INFO();
4900
0
              if (!(op1_info & MAY_BE_OBJECT)) {
4901
0
                break;
4902
0
              }
4903
0
              op1_addr = OP1_REG_ADDR();
4904
0
              if (opline->op1_type == IS_VAR) {
4905
0
                if (orig_op1_type != IS_UNKNOWN
4906
0
                 && (orig_op1_type & IS_TRACE_INDIRECT)) {
4907
0
                  op1_indirect = 1;
4908
0
                  if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
4909
0
                      &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
4910
0
                    goto jit_failure;
4911
0
                  }
4912
0
                }
4913
0
              }
4914
0
              if (orig_op1_type != IS_UNKNOWN
4915
0
               && (orig_op1_type & IS_TRACE_REFERENCE)) {
4916
0
                if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
4917
0
                    !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
4918
0
                  goto jit_failure;
4919
0
                }
4920
0
                if (opline->op1_type == IS_CV
4921
0
                 && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
4922
0
                  ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
4923
0
                }
4924
0
              } else {
4925
0
                CHECK_OP1_TRACE_TYPE();
4926
0
              }
4927
0
              if (!(op1_info & MAY_BE_OBJECT)) {
4928
0
                break;
4929
0
              }
4930
0
              if (ssa->var_info && ssa->ops) {
4931
0
                if (ssa_op->op1_use >= 0) {
4932
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
4933
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
4934
0
                    ce = op1_ssa->ce;
4935
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
4936
0
                  }
4937
0
                }
4938
0
              }
4939
0
              if (delayed_fetch_this) {
4940
0
                on_this = 1;
4941
0
              } else if (ssa_op->op1_use >= 0 && ssa->vars[ssa_op->op1_use].definition >= 0) {
4942
0
                on_this = ssa_opcodes[ssa->vars[ssa_op->op1_use].definition]->opcode == ZEND_FETCH_THIS;
4943
0
              } else if (op_array_ssa->ops
4944
0
                      && op_array_ssa->vars
4945
0
                  && op_array_ssa->ops[opline-op_array->opcodes].op1_use >= 0
4946
0
                  && op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition >= 0) {
4947
0
                on_this = op_array->opcodes[op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition].opcode == ZEND_FETCH_THIS;
4948
0
              }
4949
0
            }
4950
0
            op1_data_info = OP1_DATA_INFO();
4951
0
            CHECK_OP1_DATA_TRACE_TYPE();
4952
0
            if (!zend_jit_assign_obj_op(&ctx, opline, op_array, ssa, ssa_op,
4953
0
                op1_info, op1_addr, op1_data_info, OP1_DATA_REG_ADDR(), OP1_DATA_RANGE(),
4954
0
                op1_indirect, ce, ce_is_instanceof, on_this, delayed_fetch_this, op1_ce,
4955
0
                val_type)) {
4956
0
              goto jit_failure;
4957
0
            }
4958
0
            goto done;
4959
0
          case ZEND_ASSIGN_OBJ:
4960
0
            if (opline->op2_type != IS_CONST
4961
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
4962
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
4963
0
              break;
4964
0
            }
4965
0
            ce = NULL;
4966
0
            ce_is_instanceof = 0;
4967
0
            on_this = delayed_fetch_this = 0;
4968
0
            op1_indirect = 0;
4969
0
            if (opline->op1_type == IS_UNUSED) {
4970
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
4971
0
              ce = op_array->scope;
4972
              /* scope is NULL for closures. */
4973
0
              if (ce) {
4974
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
4975
0
              }
4976
0
              op1_addr = 0;
4977
0
              on_this = 1;
4978
0
            } else {
4979
0
              if (ssa_op->op1_use >= 0) {
4980
0
                delayed_fetch_this = ssa->var_info[ssa_op->op1_use].delayed_fetch_this;
4981
0
              }
4982
0
              op1_info = OP1_INFO();
4983
0
              if (!(op1_info & MAY_BE_OBJECT)) {
4984
0
                break;
4985
0
              }
4986
0
              op1_addr = OP1_REG_ADDR();
4987
0
              if (opline->op1_type == IS_VAR) {
4988
0
                if (orig_op1_type != IS_UNKNOWN
4989
0
                 && (orig_op1_type & IS_TRACE_INDIRECT)) {
4990
0
                  op1_indirect = 1;
4991
0
                  if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
4992
0
                      &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
4993
0
                    goto jit_failure;
4994
0
                  }
4995
0
                }
4996
0
              }
4997
0
              if (orig_op1_type != IS_UNKNOWN
4998
0
               && (orig_op1_type & IS_TRACE_REFERENCE)) {
4999
0
                if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
5000
0
                    !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5001
0
                  goto jit_failure;
5002
0
                }
5003
0
                if (opline->op1_type == IS_CV
5004
0
                 && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5005
0
                  ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
5006
0
                }
5007
0
              } else {
5008
0
                CHECK_OP1_TRACE_TYPE();
5009
0
              }
5010
0
              if (!(op1_info & MAY_BE_OBJECT)) {
5011
0
                break;
5012
0
              }
5013
0
              if (ssa->var_info && ssa->ops) {
5014
0
                if (ssa_op->op1_use >= 0) {
5015
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
5016
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
5017
0
                    ce = op1_ssa->ce;
5018
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
5019
0
                  }
5020
0
                }
5021
0
              }
5022
0
              if (delayed_fetch_this) {
5023
0
                on_this = 1;
5024
0
              } else if (ssa_op->op1_use >= 0 && ssa->vars[ssa_op->op1_use].definition >= 0) {
5025
0
                on_this = ssa_opcodes[ssa->vars[ssa_op->op1_use].definition]->opcode == ZEND_FETCH_THIS;
5026
0
              } else if (op_array_ssa->ops
5027
0
                      && op_array_ssa->vars
5028
0
                  && op_array_ssa->ops[opline-op_array->opcodes].op1_use >= 0
5029
0
                  && op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition >= 0) {
5030
0
                on_this = op_array->opcodes[op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition].opcode == ZEND_FETCH_THIS;
5031
0
              }
5032
0
            }
5033
0
            op1_data_info = OP1_DATA_INFO();
5034
0
            CHECK_OP1_DATA_TRACE_TYPE();
5035
0
            if (!zend_jit_assign_obj(&ctx, opline, op_array, ssa, ssa_op,
5036
0
                op1_info, op1_addr, op1_data_info, OP1_DATA_REG_ADDR(), OP1_DATA_DEF_REG_ADDR(),
5037
0
                (opline->result_type != IS_UNUSED) ? RES_REG_ADDR() : 0,
5038
0
                op1_indirect, ce, ce_is_instanceof, on_this, delayed_fetch_this, op1_ce,
5039
0
                val_type,
5040
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
5041
0
              goto jit_failure;
5042
0
            }
5043
0
            if ((opline+1)->op1_type == IS_CV
5044
0
             && (ssa_op+1)->op1_def >= 0
5045
0
             && ssa->vars[(ssa_op+1)->op1_def].alias == NO_ALIAS) {
5046
0
              ssa->var_info[(ssa_op+1)->op1_def].guarded_reference = ssa->var_info[(ssa_op+1)->op1_use].guarded_reference;
5047
0
            }
5048
0
            goto done;
5049
0
          case ZEND_ASSIGN_DIM:
5050
0
            op1_info = OP1_INFO();
5051
0
            op1_addr = OP1_REG_ADDR();
5052
0
            op1_indirect = 0;
5053
0
            if (opline->op1_type == IS_CV
5054
0
             && (opline+1)->op1_type == IS_CV
5055
0
             && (opline+1)->op1.var == opline->op1.var) {
5056
              /* skip $a[x] = $a; */
5057
0
              break;
5058
0
            }
5059
0
            if (opline->op1_type == IS_VAR) {
5060
0
              if (orig_op1_type != IS_UNKNOWN
5061
0
               && (orig_op1_type & IS_TRACE_INDIRECT)) {
5062
0
                op1_indirect = 1;
5063
0
                if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
5064
0
                    &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
5065
0
                  goto jit_failure;
5066
0
                }
5067
0
              }
5068
0
            }
5069
0
            if (orig_op1_type != IS_UNKNOWN
5070
0
             && (orig_op1_type & IS_TRACE_REFERENCE)) {
5071
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
5072
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5073
0
                goto jit_failure;
5074
0
              }
5075
0
              if (opline->op1_type == IS_CV
5076
0
               && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5077
0
                ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
5078
0
              }
5079
0
            } else {
5080
0
              CHECK_OP1_TRACE_TYPE();
5081
0
            }
5082
0
            op2_info = OP2_INFO();
5083
0
            CHECK_OP2_TRACE_TYPE();
5084
0
            op1_data_info = OP1_DATA_INFO();
5085
0
            CHECK_OP1_DATA_TRACE_TYPE();
5086
0
            if (!zend_jit_assign_dim(&ctx, opline,
5087
0
                op1_info, op1_addr, op1_indirect,
5088
0
                op2_info, (opline->op2_type != IS_UNUSED) ? OP2_REG_ADDR() : 0,
5089
0
                (opline->op2_type != IS_UNUSED) ? OP2_RANGE() : NULL,
5090
0
                op1_data_info, OP1_DATA_REG_ADDR(),
5091
0
                (ctx.ra && (ssa_op+1)->op1_def >= 0) ? OP1_DATA_DEF_REG_ADDR() : 0,
5092
0
                (opline->result_type != IS_UNUSED) ? RES_REG_ADDR() : 0,
5093
0
                val_type,
5094
0
                zend_may_throw_ex(opline, ssa_op, op_array, ssa, op1_info, op2_info))) {
5095
0
              goto jit_failure;
5096
0
            }
5097
0
            if ((opline+1)->op1_type == IS_CV
5098
0
             && (ssa_op+1)->op1_def >= 0
5099
0
             && ssa->vars[(ssa_op+1)->op1_def].alias == NO_ALIAS) {
5100
0
              ssa->var_info[(ssa_op+1)->op1_def].guarded_reference = ssa->var_info[(ssa_op+1)->op1_use].guarded_reference;
5101
0
            }
5102
0
            if (opline->op1_type == IS_VAR && !(op1_info & (MAY_BE_ANY-MAY_BE_NULL))) {
5103
0
              SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), IS_ARRAY, 1);
5104
0
            }
5105
0
            goto done;
5106
0
          case ZEND_ASSIGN:
5107
0
            if (opline->op1_type != IS_CV) {
5108
0
              break;
5109
0
            }
5110
0
            op2_addr = OP2_REG_ADDR();
5111
0
            op2_info = OP2_INFO();
5112
0
            zend_jit_addr ref_addr = 0;
5113
5114
0
            if (ssa_op->op2_def < 0 || (Z_MODE(op2_addr) == IS_REG && ssa->vars[ssa_op->op2_def].no_val)) {
5115
0
              op2_def_addr = op2_addr;
5116
0
            } else {
5117
0
              op2_def_addr = OP2_DEF_REG_ADDR();
5118
0
            }
5119
0
            CHECK_OP2_TRACE_TYPE();
5120
0
            op1_info = OP1_INFO();
5121
0
            op1_def_info = OP1_DEF_INFO();
5122
0
            if (op1_type != IS_UNKNOWN && (op1_info & MAY_BE_GUARD)) {
5123
0
              if (op1_type < IS_STRING
5124
0
               && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) != (op1_def_info & (MAY_BE_ANY|MAY_BE_UNDEF))) {
5125
0
                if (!zend_jit_scalar_type_guard(&ctx, opline, opline->op1.var)) {
5126
0
                  goto jit_failure;
5127
0
                }
5128
0
                op1_info &= ~(MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF|MAY_BE_GUARD);
5129
0
              } else {
5130
0
                CHECK_OP1_TRACE_TYPE();
5131
0
              }
5132
0
            }
5133
0
            op1_addr = OP1_REG_ADDR();
5134
0
            op1_def_addr = OP1_DEF_REG_ADDR();
5135
0
            if (Z_MODE(op1_def_addr) != IS_REG &&
5136
0
                STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var)) !=
5137
0
                STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var))) {
5138
              /* type may be not set */
5139
0
              op1_info |= MAY_BE_NULL;
5140
0
            }
5141
0
            if (orig_op1_type != IS_UNKNOWN) {
5142
0
              if (orig_op1_type & IS_TRACE_REFERENCE) {
5143
0
                if (!zend_jit_guard_reference(&ctx, opline, &op1_addr, &ref_addr,
5144
0
                    !ssa->var_info[ssa_op->op1_use].guarded_reference)) {
5145
0
                  goto jit_failure;
5146
0
                }
5147
0
                op1_info &= ~MAY_BE_REF;
5148
0
                if (opline->op1_type == IS_CV
5149
0
                 && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5150
0
                  ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
5151
0
                }
5152
0
                if (opline->result_type == IS_UNUSED) {
5153
0
                  res_addr = 0;
5154
0
                } else {
5155
0
                  res_addr = RES_REG_ADDR();
5156
0
                  if (Z_MODE(res_addr) != IS_REG
5157
0
                   && zend_jit_trace_next_is_send_result(opline, p, frame)) {
5158
0
                    send_result = 1;
5159
0
                    res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
5160
0
                    if (!zend_jit_reuse_ip(&ctx)) {
5161
0
                      goto jit_failure;
5162
0
                    }
5163
0
                  }
5164
0
                }
5165
0
                op1_def_addr = op1_addr;
5166
0
                op1_def_info &= ~MAY_BE_REF;
5167
0
              } else if (op1_info & MAY_BE_REF) {
5168
0
                if (!zend_jit_noref_guard(&ctx, opline, op1_addr)) {
5169
0
                  goto jit_failure;
5170
0
                }
5171
0
                op1_info &= ~MAY_BE_REF;
5172
0
                op1_def_info &= ~MAY_BE_REF;
5173
0
              }
5174
0
            }
5175
0
            if (opline->result_type == IS_UNUSED) {
5176
0
              res_addr = 0;
5177
0
              res_info = -1;
5178
0
            } else {
5179
0
              res_addr = RES_REG_ADDR();
5180
0
              res_info = RES_INFO();
5181
0
              if (Z_MODE(res_addr) != IS_REG
5182
0
               && zend_jit_trace_next_is_send_result(opline, p, frame)) {
5183
0
                send_result = 1;
5184
0
                res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
5185
0
                if (!zend_jit_reuse_ip(&ctx)) {
5186
0
                  goto jit_failure;
5187
0
                }
5188
0
              }
5189
0
            }
5190
0
            if (!zend_jit_assign(&ctx, opline,
5191
0
                op1_info, op1_addr,
5192
0
                op1_def_info, op1_def_addr,
5193
0
                op2_info, op2_addr, op2_def_addr,
5194
0
                res_info, res_addr,
5195
0
                ref_addr,
5196
0
                zend_may_throw_ex(opline, ssa_op, op_array, ssa, op1_info, op2_info))) {
5197
0
              goto jit_failure;
5198
0
            }
5199
0
            if (ssa_op->op2_def >= 0
5200
0
             && Z_MODE(op2_addr) == IS_REG
5201
0
             && ssa->vars[ssa_op->op2_def].no_val) {
5202
0
              uint8_t type = (op2_info & MAY_BE_LONG) ? IS_LONG : IS_DOUBLE;
5203
0
              uint32_t var_num = EX_VAR_TO_NUM(opline->op2.var);
5204
5205
0
              if (STACK_MEM_TYPE(stack, var_num) != type
5206
0
               && ssa->vars[ssa_op->op2_def].use_chain < 0
5207
0
               && !ssa->vars[ssa_op->op2_def].phi_use_chain) {
5208
0
                if (!zend_jit_store_type(&ctx, var_num, type)) {
5209
0
                  return 0;
5210
0
                }
5211
0
                SET_STACK_TYPE(stack, var_num, type, 1);
5212
0
              }
5213
0
            }
5214
0
            if (opline->op2_type == IS_CV
5215
0
             && ssa_op->op2_def >= 0
5216
0
             && ssa->vars[ssa_op->op2_def].alias == NO_ALIAS) {
5217
0
              ssa->var_info[ssa_op->op2_def].guarded_reference = ssa->var_info[ssa_op->op2_use].guarded_reference;
5218
0
            }
5219
0
            goto done;
5220
0
          case ZEND_CAST:
5221
0
            if (opline->extended_value != op1_type) {
5222
0
              break;
5223
0
            }
5224
0
            ZEND_FALLTHROUGH;
5225
0
          case ZEND_QM_ASSIGN:
5226
0
            op1_addr = OP1_REG_ADDR();
5227
0
            if (ssa_op->op1_def < 0 || (Z_MODE(op1_addr) == IS_REG && ssa->vars[ssa_op->op1_def].no_val)) {
5228
0
              op1_def_addr = op1_addr;
5229
0
            } else {
5230
0
              op1_def_addr = OP1_DEF_REG_ADDR();
5231
0
            }
5232
0
            op1_info = OP1_INFO();
5233
0
            CHECK_OP1_TRACE_TYPE();
5234
0
            res_info = RES_INFO();
5235
0
            res_use_info = zend_jit_trace_type_to_info(
5236
0
              STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var)));
5237
0
            if (opline->result_type == IS_CV) {
5238
0
              res_use_info &= (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE);
5239
0
            }
5240
0
            res_addr = RES_REG_ADDR();
5241
0
            if (Z_MODE(res_addr) != IS_REG &&
5242
0
                STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var)) !=
5243
0
                STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var))) {
5244
              /* type may be not set */
5245
0
              res_use_info |= MAY_BE_NULL;
5246
0
            }
5247
0
            if (!zend_jit_qm_assign(&ctx, opline,
5248
0
                op1_info, op1_addr, op1_def_addr,
5249
0
                res_use_info, res_info, res_addr)) {
5250
0
              goto jit_failure;
5251
0
            }
5252
0
            if (ssa_op->op1_def >= 0
5253
0
             && Z_MODE(op1_addr) == IS_REG
5254
0
             && ssa->vars[ssa_op->op1_def].no_val) {
5255
0
              uint8_t type = (op1_info & MAY_BE_LONG) ? IS_LONG : IS_DOUBLE;
5256
0
              uint32_t var_num = EX_VAR_TO_NUM(opline->op1.var);
5257
5258
0
              if (STACK_MEM_TYPE(stack, var_num) != type
5259
0
               && ssa->vars[ssa_op->op1_def].use_chain < 0
5260
0
               && !ssa->vars[ssa_op->op1_def].phi_use_chain) {
5261
0
                if (!zend_jit_store_type(&ctx, var_num, type)) {
5262
0
                  return 0;
5263
0
                }
5264
0
                SET_STACK_TYPE(stack, var_num, type, 1);
5265
0
              }
5266
0
            }
5267
0
            if (opline->op1_type == IS_CV
5268
0
             && ssa_op->op1_def >= 0
5269
0
             && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5270
0
              ssa->var_info[ssa_op->op1_def].guarded_reference = ssa->var_info[ssa_op->op1_use].guarded_reference;
5271
0
            }
5272
0
            goto done;
5273
0
          case ZEND_INIT_FCALL:
5274
0
          case ZEND_INIT_FCALL_BY_NAME:
5275
0
          case ZEND_INIT_NS_FCALL_BY_NAME:
5276
0
            frame_flags = TRACE_FRAME_MASK_NESTED;
5277
0
            if (!zend_jit_init_fcall(&ctx, opline, op_array_ssa->cfg.map ? op_array_ssa->cfg.map[opline - op_array->opcodes] : -1, op_array, ssa, ssa_op, frame->call_level, p + 1, peek_checked_stack - checked_stack)) {
5278
0
              goto jit_failure;
5279
0
            }
5280
0
            goto done;
5281
0
          case ZEND_SEND_VAL:
5282
0
          case ZEND_SEND_VAL_EX:
5283
0
            if (opline->op2_type == IS_CONST) {
5284
              /* Named parameters not supported in JIT */
5285
0
              break;
5286
0
            }
5287
0
            if (opline->opcode == ZEND_SEND_VAL_EX
5288
0
             && opline->op2.num > MAX_ARG_FLAG_NUM) {
5289
0
              break;
5290
0
            }
5291
0
            op1_info = OP1_INFO();
5292
0
            CHECK_OP1_TRACE_TYPE();
5293
0
            if (!zend_jit_send_val(&ctx, opline,
5294
0
                op1_info, OP1_REG_ADDR())) {
5295
0
              goto jit_failure;
5296
0
            }
5297
0
            if (frame->call && frame->call->func) {
5298
0
              if (opline->op1_type == IS_CONST) {
5299
0
                zend_jit_trace_send_type(opline, frame->call, Z_TYPE_P(RT_CONSTANT(opline, opline->op1)));
5300
0
              } else if (op1_type != IS_UNKNOWN) {
5301
0
                if (op1_type == IS_UNDEF) {
5302
0
                  op1_type = IS_NULL;
5303
0
                }
5304
0
                zend_jit_trace_send_type(opline, frame->call, op1_type);
5305
0
              }
5306
0
            }
5307
0
            goto done;
5308
0
          case ZEND_SEND_REF:
5309
0
            if (opline->op2_type == IS_CONST) {
5310
              /* Named parameters not supported in JIT */
5311
0
              break;
5312
0
            }
5313
0
            op1_info = OP1_INFO();
5314
0
            if (!zend_jit_send_ref(&ctx, opline, op_array,
5315
0
                op1_info, 0)) {
5316
0
              goto jit_failure;
5317
0
            }
5318
0
            if (opline->op1_type == IS_CV
5319
0
             && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5320
0
              ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
5321
0
            }
5322
0
            goto done;
5323
0
          case ZEND_SEND_VAR:
5324
0
          case ZEND_SEND_VAR_EX:
5325
0
          case ZEND_SEND_VAR_NO_REF:
5326
0
          case ZEND_SEND_VAR_NO_REF_EX:
5327
0
          case ZEND_SEND_FUNC_ARG:
5328
0
            if (opline->op2_type == IS_CONST) {
5329
              /* Named parameters not supported in JIT */
5330
0
              break;
5331
0
            }
5332
0
            if ((opline->opcode == ZEND_SEND_VAR_EX
5333
0
              || opline->opcode == ZEND_SEND_VAR_NO_REF_EX)
5334
0
             && opline->op2.num > MAX_ARG_FLAG_NUM) {
5335
0
              break;
5336
0
            }
5337
0
            op1_addr = OP1_REG_ADDR();
5338
0
            if (ssa_op->op1_def < 0 || (Z_MODE(op1_addr) == IS_REG && ssa->vars[ssa_op->op1_def].no_val)) {
5339
0
              op1_def_addr = op1_addr;
5340
0
            } else {
5341
0
              op1_def_addr = OP1_DEF_REG_ADDR();
5342
0
            }
5343
0
            op1_info = OP1_INFO();
5344
0
            CHECK_OP1_TRACE_TYPE();
5345
0
            if (!zend_jit_send_var(&ctx, opline, op_array,
5346
0
                op1_info, op1_addr, op1_def_addr)) {
5347
0
              goto jit_failure;
5348
0
            }
5349
0
            if (ssa_op->op1_def >= 0
5350
0
             && Z_MODE(op1_addr) == IS_REG
5351
0
             && ssa->vars[ssa_op->op1_def].no_val) {
5352
0
              uint8_t type = (op1_info & MAY_BE_LONG) ? IS_LONG : IS_DOUBLE;
5353
0
              uint32_t var_num = EX_VAR_TO_NUM(opline->op1.var);
5354
5355
0
              if (STACK_MEM_TYPE(stack, var_num) != type
5356
0
               && ssa->vars[ssa_op->op1_def].use_chain < 0
5357
0
               && !ssa->vars[ssa_op->op1_def].phi_use_chain) {
5358
0
                if (!zend_jit_store_type(&ctx, var_num, type)) {
5359
0
                  return 0;
5360
0
                }
5361
0
                SET_STACK_TYPE(stack, var_num, type, 1);
5362
0
              }
5363
0
            }
5364
0
            if (opline->op1_type == IS_CV
5365
0
             && ssa_op->op1_def >= 0
5366
0
             && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5367
0
              ssa->var_info[ssa_op->op1_def].guarded_reference = ssa->var_info[ssa_op->op1_use].guarded_reference;
5368
0
            }
5369
0
            if (frame->call && frame->call->func) {
5370
0
              if ((opline->opcode == ZEND_SEND_VAR_EX
5371
0
                || opline->opcode == ZEND_SEND_FUNC_ARG)
5372
0
               && ARG_SHOULD_BE_SENT_BY_REF(frame->call->func, opline->op2.num)) {
5373
0
                goto done;
5374
0
              }
5375
0
              if (op1_type != IS_UNKNOWN) {
5376
0
                if (op1_type == IS_UNDEF) {
5377
0
                  op1_type = IS_NULL;
5378
0
                }
5379
0
                zend_jit_trace_send_type(opline, frame->call, op1_type);
5380
0
              }
5381
0
            }
5382
0
            goto done;
5383
0
          case ZEND_CHECK_FUNC_ARG:
5384
0
            if (!JIT_G(current_frame)
5385
0
             || !JIT_G(current_frame)->call
5386
0
             || !JIT_G(current_frame)->call->func) {
5387
0
              break;
5388
0
            }
5389
0
            if (opline->op2_type == IS_CONST
5390
0
             || opline->op2.num > MAX_ARG_FLAG_NUM) {
5391
              /* Named parameters not supported in JIT */
5392
0
              TRACE_FRAME_SET_LAST_SEND_UNKNOWN(JIT_G(current_frame)->call);
5393
0
              break;
5394
0
            }
5395
0
            if (!zend_jit_check_func_arg(&ctx, opline)) {
5396
0
              goto jit_failure;
5397
0
            }
5398
0
            goto done;
5399
0
          case ZEND_CHECK_UNDEF_ARGS:
5400
0
            if (JIT_G(current_frame)
5401
0
             && JIT_G(current_frame)->call) {
5402
0
              TRACE_FRAME_SET_UNKNOWN_NUM_ARGS(JIT_G(current_frame)->call);
5403
0
            }
5404
0
            if (!zend_jit_check_undef_args(&ctx, opline)) {
5405
0
              goto jit_failure;
5406
0
            }
5407
0
            goto done;
5408
0
          case ZEND_DO_UCALL:
5409
0
          case ZEND_DO_ICALL:
5410
0
          case ZEND_DO_FCALL_BY_NAME:
5411
0
          case ZEND_DO_FCALL:
5412
0
            if (!zend_jit_do_fcall(&ctx, opline, op_array, op_array_ssa, frame->call_level, -1, p + 1)) {
5413
0
              goto jit_failure;
5414
0
            }
5415
0
            goto done;
5416
0
          case ZEND_IS_EQUAL:
5417
0
          case ZEND_IS_NOT_EQUAL:
5418
0
          case ZEND_IS_SMALLER:
5419
0
          case ZEND_IS_SMALLER_OR_EQUAL:
5420
0
          case ZEND_CASE:
5421
0
            op1_info = OP1_INFO();
5422
0
            op2_info = OP2_INFO();
5423
0
            skip_comparison =
5424
0
              ssa_op != ssa->ops &&
5425
0
              (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG &&
5426
0
              (op2_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG &&
5427
0
              zend_jit_may_skip_comparison(opline, ssa_op, ssa, ssa_opcodes, op_array);
5428
0
            CHECK_OP1_TRACE_TYPE();
5429
0
            CHECK_OP2_TRACE_TYPE();
5430
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5431
0
              bool exit_if_true = 0;
5432
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5433
0
              uint32_t exit_point;
5434
5435
0
              if (ra) {
5436
0
                zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
5437
0
              }
5438
0
              exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5439
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5440
0
              if (!exit_addr) {
5441
0
                goto jit_failure;
5442
0
              }
5443
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5444
0
              if (!zend_jit_cmp(&ctx, opline,
5445
0
                  op1_info, OP1_RANGE(), OP1_REG_ADDR(),
5446
0
                  op2_info, OP2_RANGE(), OP2_REG_ADDR(),
5447
0
                  RES_REG_ADDR(),
5448
0
                  zend_may_throw(opline, ssa_op, op_array, ssa),
5449
0
                  smart_branch_opcode, -1, -1, exit_addr, skip_comparison)) {
5450
0
                goto jit_failure;
5451
0
              }
5452
0
              zend_jit_trace_update_condition_ranges(opline, ssa_op, op_array, ssa, exit_if_true);
5453
0
            } else {
5454
0
              smart_branch_opcode = 0;
5455
0
              exit_addr = NULL;
5456
0
              if (!zend_jit_cmp(&ctx, opline,
5457
0
                  op1_info, OP1_RANGE(), OP1_REG_ADDR(),
5458
0
                  op2_info, OP2_RANGE(), OP2_REG_ADDR(),
5459
0
                  RES_REG_ADDR(),
5460
0
                  zend_may_throw(opline, ssa_op, op_array, ssa),
5461
0
                  smart_branch_opcode, -1, -1, exit_addr, skip_comparison)) {
5462
0
                goto jit_failure;
5463
0
              }
5464
0
            }
5465
0
            goto done;
5466
0
          case ZEND_IS_IDENTICAL:
5467
0
          case ZEND_IS_NOT_IDENTICAL:
5468
0
          case ZEND_CASE_STRICT:
5469
0
            op1_info = OP1_INFO();
5470
0
            op2_info = OP2_INFO();
5471
0
            skip_comparison =
5472
0
              ssa_op != ssa->ops &&
5473
0
              (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG &&
5474
0
              (op2_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_GUARD)) == MAY_BE_LONG &&
5475
0
              zend_jit_may_skip_comparison(opline, ssa_op, ssa, ssa_opcodes, op_array);
5476
0
            CHECK_OP1_TRACE_TYPE();
5477
0
            CHECK_OP2_TRACE_TYPE();
5478
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5479
0
              bool exit_if_true = 0;
5480
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5481
0
              uint32_t exit_point;
5482
5483
0
              if (ra) {
5484
0
                zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
5485
0
              }
5486
0
              exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5487
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5488
0
              if (!exit_addr) {
5489
0
                goto jit_failure;
5490
0
              }
5491
0
              if (opline->opcode == ZEND_IS_NOT_IDENTICAL) {
5492
0
                exit_if_true = !exit_if_true;
5493
0
              }
5494
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5495
0
              if (!zend_jit_identical(&ctx, opline,
5496
0
                  op1_info, OP1_RANGE(), OP1_REG_ADDR(),
5497
0
                  op2_info, OP2_RANGE(), OP2_REG_ADDR(),
5498
0
                  RES_REG_ADDR(),
5499
0
                  zend_may_throw(opline, ssa_op, op_array, ssa),
5500
0
                  smart_branch_opcode, -1, -1, exit_addr, skip_comparison)) {
5501
0
                goto jit_failure;
5502
0
              }
5503
0
              zend_jit_trace_update_condition_ranges(opline, ssa_op, op_array, ssa, exit_if_true);
5504
0
            } else {
5505
0
              smart_branch_opcode = 0;
5506
0
              exit_addr = NULL;
5507
0
              if (!zend_jit_identical(&ctx, opline,
5508
0
                  op1_info, OP1_RANGE(), OP1_REG_ADDR(),
5509
0
                  op2_info, OP2_RANGE(), OP2_REG_ADDR(),
5510
0
                  RES_REG_ADDR(),
5511
0
                  zend_may_throw(opline, ssa_op, op_array, ssa),
5512
0
                  smart_branch_opcode, -1, -1, exit_addr, skip_comparison)) {
5513
0
                goto jit_failure;
5514
0
              }
5515
0
            }
5516
0
            goto done;
5517
0
          case ZEND_DEFINED:
5518
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5519
0
              bool exit_if_true = 0;
5520
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5521
0
              uint32_t exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5522
5523
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5524
0
              if (!exit_addr) {
5525
0
                goto jit_failure;
5526
0
              }
5527
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5528
0
            } else {
5529
0
              smart_branch_opcode = 0;
5530
0
              exit_addr = NULL;
5531
0
            }
5532
0
            if (!zend_jit_defined(&ctx, opline, smart_branch_opcode, -1, -1, exit_addr)) {
5533
0
              goto jit_failure;
5534
0
            }
5535
0
            goto done;
5536
0
          case ZEND_TYPE_CHECK:
5537
0
            if (opline->extended_value == MAY_BE_RESOURCE) {
5538
              // TODO: support for is_resource() ???
5539
0
              break;
5540
0
            }
5541
0
            op1_info = OP1_INFO();
5542
0
            CHECK_OP1_TRACE_TYPE();
5543
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5544
0
              bool exit_if_true = 0;
5545
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5546
0
              uint32_t exit_point;
5547
5548
0
              if (ra) {
5549
0
                zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
5550
0
              }
5551
0
              exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5552
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5553
0
              if (!exit_addr) {
5554
0
                goto jit_failure;
5555
0
              }
5556
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5557
0
            } else {
5558
0
              smart_branch_opcode = 0;
5559
0
              exit_addr = NULL;
5560
0
            }
5561
0
            if (!zend_jit_type_check(&ctx, opline, op1_info, smart_branch_opcode, -1, -1, exit_addr)) {
5562
0
              goto jit_failure;
5563
0
            }
5564
0
            goto done;
5565
0
          case ZEND_RETURN:
5566
0
            op1_info = OP1_INFO();
5567
0
            CHECK_OP1_TRACE_TYPE();
5568
0
            if (opline->op1_type == IS_CONST) {
5569
0
              res_type = Z_TYPE_P(RT_CONSTANT(opline, opline->op1));
5570
0
            } else if (op1_type != IS_UNKNOWN) {
5571
0
              res_type = op1_type;
5572
0
              if (res_type == IS_UNDEF) {
5573
0
                res_type = IS_NULL;
5574
0
              }
5575
0
            }
5576
0
            if (op_array->type == ZEND_EVAL_CODE
5577
             // TODO: support for top-level code
5578
0
             || !op_array->function_name
5579
             // TODO: support for IS_UNDEF ???
5580
0
             || (op1_info & MAY_BE_UNDEF)) {
5581
0
              if (!zend_jit_trace_handler(&ctx, op_array, opline, zend_may_throw(opline, ssa_op, op_array, ssa), p + 1)) {
5582
0
                goto jit_failure;
5583
0
              }
5584
0
            } else {
5585
0
              int j;
5586
0
              int may_throw = 0;
5587
0
              bool left_frame = 0;
5588
5589
0
              if (!zend_jit_return(&ctx, opline, op_array,
5590
0
                  op1_info, OP1_REG_ADDR())) {
5591
0
                goto jit_failure;
5592
0
              }
5593
0
              if (op_array->last_var > 100) {
5594
                /* To many CVs to unroll */
5595
0
                if (!zend_jit_free_cvs(&ctx)) {
5596
0
                  goto jit_failure;
5597
0
                }
5598
0
                left_frame = 1;
5599
0
              }
5600
0
              if (!left_frame) {
5601
0
                for (j = 0 ; j < op_array->last_var; j++) {
5602
0
                  uint32_t info;
5603
0
                  uint8_t type;
5604
5605
0
                  info = zend_ssa_cv_info(op_array, op_array_ssa, j);
5606
0
                  type = STACK_TYPE(stack, j);
5607
0
                  info = zend_jit_trace_type_to_info_ex(type, info);
5608
0
                  if (opline->op1_type == IS_CV
5609
0
                   && EX_VAR_TO_NUM(opline->op1.var) == j
5610
0
                   && !(op1_info & (MAY_BE_REF|MAY_BE_OBJECT))) {
5611
0
                    if (JIT_G(current_frame)
5612
0
                     && TRACE_FRAME_IS_RETURN_VALUE_USED(JIT_G(current_frame))) {
5613
0
                      continue;
5614
0
                    } else {
5615
0
                      info |= MAY_BE_NULL;
5616
0
                    }
5617
0
                  }
5618
0
                  if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
5619
0
                    if (!left_frame) {
5620
0
                      left_frame = 1;
5621
0
                        if (!zend_jit_leave_frame(&ctx)) {
5622
0
                        goto jit_failure;
5623
0
                        }
5624
0
                    }
5625
0
                    if (!zend_jit_free_cv(&ctx, info, j)) {
5626
0
                      goto jit_failure;
5627
0
                    }
5628
0
                    if (info & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_RESOURCE)) {
5629
0
                      if (info & MAY_BE_RC1) {
5630
0
                        may_throw = 1;
5631
0
                      }
5632
0
                    }
5633
0
                  }
5634
0
                }
5635
0
              }
5636
0
              if (!zend_jit_leave_func(&ctx, op_array, opline, op1_info, left_frame,
5637
0
                  p + 1, &zend_jit_traces[ZEND_JIT_TRACE_NUM],
5638
0
                  (op_array_ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS) != 0, may_throw)) {
5639
0
                goto jit_failure;
5640
0
              }
5641
0
            }
5642
0
            goto done;
5643
0
          case ZEND_BOOL:
5644
0
          case ZEND_BOOL_NOT:
5645
0
            op1_info = OP1_INFO();
5646
0
            CHECK_OP1_TRACE_TYPE();
5647
0
            if (!zend_jit_bool_jmpznz(&ctx, opline,
5648
0
                op1_info, OP1_REG_ADDR(), RES_REG_ADDR(),
5649
0
                -1, -1,
5650
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
5651
0
                opline->opcode, NULL)) {
5652
0
              goto jit_failure;
5653
0
            }
5654
0
            goto done;
5655
0
          case ZEND_JMPZ:
5656
0
          case ZEND_JMPNZ:
5657
0
          case ZEND_JMPZ_EX:
5658
0
          case ZEND_JMPNZ_EX:
5659
0
            op1_info = OP1_INFO();
5660
0
            CHECK_OP1_TRACE_TYPE();
5661
0
            if ((p+1)->op == ZEND_JIT_TRACE_VM || (p+1)->op == ZEND_JIT_TRACE_END) {
5662
0
              const zend_op *exit_opline = NULL;
5663
0
              uint32_t exit_point;
5664
5665
0
              if ((p+1)->opline == OP_JMP_ADDR(opline, opline->op2)) {
5666
                /* taken branch */
5667
0
                if (opline->opcode == ZEND_JMPNZ_EX) {
5668
0
                  smart_branch_opcode = ZEND_JMPZ_EX;
5669
0
                } else if (opline->opcode == ZEND_JMPZ_EX) {
5670
0
                  smart_branch_opcode = ZEND_JMPNZ_EX;
5671
0
                } else if (opline->opcode == ZEND_JMPNZ) {
5672
0
                  smart_branch_opcode = ZEND_JMPZ;
5673
0
                } else {
5674
0
                  smart_branch_opcode = ZEND_JMPNZ;
5675
0
                }
5676
0
                exit_opline = opline + 1;
5677
0
              } else if ((p+1)->opline == opline + 1) {
5678
                /* not taken branch */
5679
0
                smart_branch_opcode = opline->opcode;
5680
0
                exit_opline = OP_JMP_ADDR(opline, opline->op2);
5681
0
              } else {
5682
0
                ZEND_UNREACHABLE();
5683
0
              }
5684
0
              if (ra) {
5685
0
                zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
5686
0
              }
5687
0
              if (!(op1_info & MAY_BE_GUARD)
5688
0
               && has_concrete_type(op1_info)
5689
0
               && concrete_type(op1_info) <= IS_TRUE) {
5690
                /* unconditional branch */
5691
0
                exit_addr = NULL;
5692
0
              } else if (opline->result_type == IS_TMP_VAR) {
5693
0
                uint32_t old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var));
5694
5695
0
                SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
5696
0
                exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5697
0
                SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->result.var), old_info);
5698
0
                exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5699
0
                if (!exit_addr) {
5700
0
                  goto jit_failure;
5701
0
                }
5702
0
              } else {
5703
0
                exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5704
0
                exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5705
0
                if (!exit_addr) {
5706
0
                  goto jit_failure;
5707
0
                }
5708
0
              }
5709
0
            } else  {
5710
0
              ZEND_UNREACHABLE();
5711
0
            }
5712
0
            if (opline->result_type == IS_UNDEF) {
5713
0
              res_addr = 0;
5714
0
            } else {
5715
0
              res_addr = RES_REG_ADDR();
5716
0
            }
5717
0
            if (!zend_jit_bool_jmpznz(&ctx, opline,
5718
0
                op1_info, OP1_REG_ADDR(), res_addr,
5719
0
                -1, -1,
5720
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
5721
0
                smart_branch_opcode, exit_addr)) {
5722
0
              goto jit_failure;
5723
0
            }
5724
0
            goto done;
5725
0
          case ZEND_JMP_FRAMELESS:
5726
0
            op1_info = OP1_INFO();
5727
0
            ZEND_ASSERT((p+1)->op == ZEND_JIT_TRACE_VM);
5728
0
            const zend_op *exit_opline = NULL;
5729
0
            uint32_t exit_point;
5730
0
            zend_jmp_fl_result guard;
5731
5732
0
            if ((p+1)->opline == OP_JMP_ADDR(opline, opline->op2)) {
5733
              /* taken branch */
5734
0
              guard = ZEND_JMP_FL_HIT;
5735
0
              exit_opline = opline + 1;
5736
0
            } else if ((p+1)->opline == opline + 1) {
5737
              /* not taken branch */
5738
0
              guard = ZEND_JMP_FL_MISS;
5739
0
              exit_opline = OP_JMP_ADDR(opline, opline->op2);
5740
0
            } else {
5741
0
              ZEND_UNREACHABLE();
5742
0
            }
5743
0
            exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5744
0
            exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5745
0
            if (!exit_addr) {
5746
0
              goto jit_failure;
5747
0
            }
5748
0
            if (!zend_jit_jmp_frameless(&ctx, opline, exit_addr, guard)) {
5749
0
              goto jit_failure;
5750
0
            }
5751
0
            goto done;
5752
0
          case ZEND_ISSET_ISEMPTY_CV:
5753
0
            if ((opline->extended_value & ZEND_ISEMPTY)) {
5754
              // TODO: support for empty() ???
5755
0
              break;
5756
0
            }
5757
0
            op1_info = OP1_INFO();
5758
0
            op1_addr = OP1_REG_ADDR();
5759
0
            if (orig_op1_type != IS_UNKNOWN
5760
0
             && (orig_op1_type & IS_TRACE_REFERENCE)) {
5761
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
5762
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5763
0
                goto jit_failure;
5764
0
              }
5765
0
              if (opline->op1_type == IS_CV
5766
0
               && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
5767
0
                ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
5768
0
              }
5769
0
            } else {
5770
0
              CHECK_OP1_TRACE_TYPE();
5771
0
            }
5772
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5773
0
              bool exit_if_true = 0;
5774
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5775
0
              uint32_t exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5776
5777
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5778
0
              if (!exit_addr) {
5779
0
                goto jit_failure;
5780
0
              }
5781
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5782
0
            } else {
5783
0
              smart_branch_opcode = 0;
5784
0
              exit_addr = NULL;
5785
0
            }
5786
0
            if (!zend_jit_isset_isempty_cv(&ctx, opline,
5787
0
                op1_info, op1_addr,
5788
0
                smart_branch_opcode, -1, -1, exit_addr)) {
5789
0
              goto jit_failure;
5790
0
            }
5791
0
            goto done;
5792
0
          case ZEND_IN_ARRAY:
5793
0
            if (opline->op1_type == IS_VAR || opline->op1_type == IS_TMP_VAR) {
5794
0
              break;
5795
0
            }
5796
0
            op1_info = OP1_INFO();
5797
0
            op1_addr = OP1_REG_ADDR();
5798
0
            CHECK_OP1_TRACE_TYPE();
5799
0
            if ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_STRING) {
5800
0
              break;
5801
0
            }
5802
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5803
0
              bool exit_if_true = 0;
5804
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5805
0
              uint32_t exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5806
5807
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5808
0
              if (!exit_addr) {
5809
0
                goto jit_failure;
5810
0
              }
5811
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5812
0
            } else {
5813
0
              smart_branch_opcode = 0;
5814
0
              exit_addr = NULL;
5815
0
            }
5816
0
            if (!zend_jit_in_array(&ctx, opline,
5817
0
                op1_info, op1_addr,
5818
0
                smart_branch_opcode, -1, -1, exit_addr)) {
5819
0
              goto jit_failure;
5820
0
            }
5821
0
            goto done;
5822
0
          case ZEND_FETCH_DIM_FUNC_ARG:
5823
0
            if (!JIT_G(current_frame)
5824
0
             || !JIT_G(current_frame)->call
5825
0
             || !JIT_G(current_frame)->call->func
5826
0
             || !TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
5827
0
              break;
5828
0
            }
5829
0
            ZEND_FALLTHROUGH;
5830
0
          case ZEND_FETCH_DIM_R:
5831
0
          case ZEND_FETCH_DIM_IS:
5832
0
          case ZEND_FETCH_LIST_R:
5833
0
            op1_info = OP1_INFO();
5834
0
            op1_addr = OP1_REG_ADDR();
5835
0
            if (orig_op1_type != IS_UNKNOWN
5836
0
             && (orig_op1_type & IS_TRACE_REFERENCE)) {
5837
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
5838
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5839
0
                goto jit_failure;
5840
0
              }
5841
0
              if (opline->op1_type == IS_CV
5842
0
               && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
5843
0
                ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
5844
0
                if (ssa_op->op1_def >= 0) {
5845
0
                  ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
5846
0
                }
5847
0
              }
5848
0
            } else {
5849
0
              CHECK_OP1_TRACE_TYPE();
5850
0
            }
5851
0
            op2_info = OP2_INFO();
5852
0
            CHECK_OP2_TRACE_TYPE();
5853
0
            res_info = RES_INFO();
5854
0
            avoid_refcounting =
5855
0
              ssa_op->op1_use >= 0 &&
5856
0
              ssa->var_info[ssa_op->op1_use].avoid_refcounting;
5857
0
            if (op1_info & MAY_BE_PACKED_GUARD) {
5858
0
              ssa->var_info[ssa_op->op1_use].type &= ~MAY_BE_PACKED_GUARD;
5859
0
            } else if ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG
5860
0
                && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY
5861
0
                && MAY_BE_PACKED(op1_info)
5862
0
                && MAY_BE_HASH(op1_info)
5863
0
                && orig_op1_type != IS_UNKNOWN) {
5864
0
              op1_info |= MAY_BE_PACKED_GUARD;
5865
0
              if (orig_op1_type & IS_TRACE_PACKED) {
5866
0
                op1_info &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
5867
0
                if (op1_type != IS_UNKNOWN) {
5868
0
                  ssa->var_info[ssa_op->op1_use].type &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
5869
0
                }
5870
0
              } else {
5871
0
                op1_info &= ~MAY_BE_ARRAY_PACKED;
5872
0
                if (op1_type != IS_UNKNOWN) {
5873
0
                  ssa->var_info[ssa_op->op1_use].type &= ~MAY_BE_ARRAY_PACKED;
5874
0
                }
5875
0
              }
5876
0
            }
5877
0
            if (!zend_jit_fetch_dim_read(&ctx, opline, ssa, ssa_op,
5878
0
                op1_info, op1_addr, avoid_refcounting,
5879
0
                op2_info, OP2_REG_ADDR(), OP2_RANGE(),
5880
0
                res_info, RES_REG_ADDR(), val_type)) {
5881
0
              goto jit_failure;
5882
0
            }
5883
0
            if (ssa_op->op1_def >= 0 && op1_type != IS_UNKNOWN) {
5884
0
              ssa->var_info[ssa_op->op1_def].type = ssa->var_info[ssa_op->op1_use].type;
5885
0
            }
5886
0
            goto done;
5887
0
          case ZEND_FETCH_DIM_W:
5888
0
          case ZEND_FETCH_DIM_RW:
5889
//          case ZEND_FETCH_DIM_UNSET:
5890
0
          case ZEND_FETCH_LIST_W:
5891
0
            if (opline->op1_type != IS_CV
5892
0
             && (orig_op1_type == IS_UNKNOWN
5893
0
              || !(orig_op1_type & IS_TRACE_INDIRECT))) {
5894
0
              break;
5895
0
            }
5896
0
            op1_info = OP1_INFO();
5897
0
            op1_addr = OP1_REG_ADDR();
5898
0
            if (opline->op1_type == IS_VAR) {
5899
0
              if (orig_op1_type != IS_UNKNOWN
5900
0
               && (orig_op1_type & IS_TRACE_INDIRECT)) {
5901
0
                if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
5902
0
                    &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
5903
0
                  goto jit_failure;
5904
0
                }
5905
0
              } else {
5906
0
                break;
5907
0
              }
5908
0
            }
5909
0
            if (orig_op1_type != IS_UNKNOWN
5910
0
             && (orig_op1_type & IS_TRACE_REFERENCE)) {
5911
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
5912
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5913
0
                goto jit_failure;
5914
0
              }
5915
0
              if (opline->op1_type == IS_CV
5916
0
               && ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
5917
0
                ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
5918
0
              }
5919
0
            } else {
5920
0
              CHECK_OP1_TRACE_TYPE();
5921
0
            }
5922
0
            op2_info = OP2_INFO();
5923
0
            CHECK_OP2_TRACE_TYPE();
5924
0
            op1_def_info = OP1_DEF_INFO();
5925
0
            if (!zend_jit_fetch_dim(&ctx, opline,
5926
0
                op1_info, op1_addr,
5927
0
                op2_info, (opline->op2_type != IS_UNUSED) ? OP2_REG_ADDR() : 0,
5928
0
                (opline->op2_type != IS_UNUSED) ? OP2_RANGE() : NULL,
5929
0
                RES_REG_ADDR(), val_type)) {
5930
0
              goto jit_failure;
5931
0
            }
5932
0
            if (ssa_op->result_def >= 0
5933
0
             && (opline->opcode == ZEND_FETCH_DIM_W || opline->opcode == ZEND_FETCH_LIST_W)
5934
0
             && !(op1_info & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))
5935
0
             && !(op2_info & (MAY_BE_UNDEF|MAY_BE_RESOURCE|MAY_BE_ARRAY|MAY_BE_OBJECT))) {
5936
0
              ssa->var_info[ssa_op->result_def].indirect_reference = 1;
5937
0
            }
5938
0
            goto done;
5939
0
          case ZEND_ISSET_ISEMPTY_DIM_OBJ:
5940
0
            if ((opline->extended_value & ZEND_ISEMPTY)) {
5941
              // TODO: support for empty() ???
5942
0
              break;
5943
0
            }
5944
0
            op1_info = OP1_INFO();
5945
0
            op1_addr = OP1_REG_ADDR();
5946
0
            if (orig_op1_type != IS_UNKNOWN
5947
0
             && (orig_op1_type & IS_TRACE_REFERENCE)) {
5948
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
5949
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5950
0
                goto jit_failure;
5951
0
              }
5952
0
              if (opline->op1_type == IS_CV
5953
0
               && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
5954
0
                ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
5955
0
              }
5956
0
            } else {
5957
0
              CHECK_OP1_TRACE_TYPE();
5958
0
            }
5959
0
            op2_info = OP2_INFO();
5960
0
            CHECK_OP2_TRACE_TYPE();
5961
0
            if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
5962
0
              bool exit_if_true = 0;
5963
0
              const zend_op *exit_opline = zend_jit_trace_get_exit_opline(p + 1, opline + 1, &exit_if_true);
5964
0
              uint32_t exit_point;
5965
0
              int32_t old_ref = 0;
5966
0
              uint8_t old_flags = 0;
5967
5968
0
              if (ra) {
5969
0
                if (opline->op2_type != IS_CONST) {
5970
0
                  old_ref = STACK_REF(stack, EX_VAR_TO_NUM(opline->op2.var));
5971
0
                  old_flags = STACK_FLAGS(stack, EX_VAR_TO_NUM(opline->op2.var));
5972
0
                }
5973
0
                zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
5974
0
              }
5975
0
              if (ssa_op->op1_use >= 0
5976
0
               && ssa->var_info[ssa_op->op1_use].avoid_refcounting) {
5977
                /* Temporary reset ZREG_ZVAL_TRY_ADDREF */
5978
0
                uint32_t old_info = STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var));
5979
5980
0
                SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
5981
0
                exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5982
0
                SET_STACK_INFO(stack, EX_VAR_TO_NUM(opline->op1.var), old_info);
5983
0
              } else {
5984
0
                exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
5985
0
              }
5986
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
5987
0
              if (!exit_addr) {
5988
0
                goto jit_failure;
5989
0
              }
5990
0
              if (old_ref) {
5991
0
                SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->op2.var), old_ref, old_flags);
5992
0
              }
5993
0
              smart_branch_opcode = exit_if_true ? ZEND_JMPNZ : ZEND_JMPZ;
5994
0
            } else {
5995
0
              smart_branch_opcode = 0;
5996
0
              exit_addr = NULL;
5997
0
            }
5998
0
            avoid_refcounting =
5999
0
              ssa_op->op1_use >= 0 &&
6000
0
              ssa->var_info[ssa_op->op1_use].avoid_refcounting;
6001
0
            if (op1_info & MAY_BE_PACKED_GUARD) {
6002
0
              ssa->var_info[ssa_op->op1_use].type &= ~MAY_BE_PACKED_GUARD;
6003
0
            } else if ((op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG
6004
0
                && (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY
6005
0
                && MAY_BE_PACKED(op1_info)
6006
0
                && MAY_BE_HASH(op1_info)
6007
0
                && orig_op1_type != IS_UNKNOWN) {
6008
0
              op1_info |= MAY_BE_PACKED_GUARD;
6009
0
              if (orig_op1_type & IS_TRACE_PACKED) {
6010
0
                op1_info &= ~(MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
6011
0
              } else {
6012
0
                op1_info &= ~MAY_BE_ARRAY_PACKED;
6013
0
              }
6014
0
            }
6015
0
            if (!zend_jit_isset_isempty_dim(&ctx, opline,
6016
0
                op1_info, op1_addr, avoid_refcounting,
6017
0
                op2_info, OP2_REG_ADDR(), OP2_RANGE(), val_type,
6018
0
                zend_may_throw_ex(opline, ssa_op, op_array, ssa, op1_info, op2_info),
6019
0
                smart_branch_opcode, -1, -1,
6020
0
                exit_addr)) {
6021
0
              goto jit_failure;
6022
0
            }
6023
0
            goto done;
6024
0
          case ZEND_FETCH_OBJ_FUNC_ARG:
6025
0
            if (!JIT_G(current_frame)
6026
0
             || !JIT_G(current_frame)->call
6027
0
             || !JIT_G(current_frame)->call->func
6028
0
             || !TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
6029
0
              break;
6030
0
            }
6031
0
            ZEND_FALLTHROUGH;
6032
0
          case ZEND_FETCH_OBJ_R:
6033
0
          case ZEND_FETCH_OBJ_IS:
6034
0
          case ZEND_FETCH_OBJ_W:
6035
0
            on_this = delayed_fetch_this = 0;
6036
0
            avoid_refcounting = 0;
6037
0
            if (opline->op2_type != IS_CONST
6038
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
6039
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
6040
0
              break;
6041
0
            }
6042
0
            ce = NULL;
6043
0
            ce_is_instanceof = 0;
6044
0
            op1_indirect = 0;
6045
0
            if (opline->op1_type == IS_UNUSED) {
6046
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
6047
0
              ce = op_array->scope;
6048
              /* scope is NULL for closures. */
6049
0
              if (ce) {
6050
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
6051
0
              }
6052
0
              op1_addr = 0;
6053
0
              on_this = 1;
6054
0
            } else {
6055
0
              op1_info = OP1_INFO();
6056
0
              if (!(op1_info & MAY_BE_OBJECT)) {
6057
0
                break;
6058
0
              }
6059
0
              op1_addr = OP1_REG_ADDR();
6060
0
              if (opline->op1_type == IS_VAR
6061
0
               && opline->opcode == ZEND_FETCH_OBJ_W) {
6062
0
                if (orig_op1_type != IS_UNKNOWN
6063
0
                 && (orig_op1_type & IS_TRACE_INDIRECT)) {
6064
0
                  op1_indirect = 1;
6065
0
                  if (!zend_jit_fetch_indirect_var(&ctx, opline, orig_op1_type,
6066
0
                      &op1_info, &op1_addr, !ssa->var_info[ssa_op->op1_use].indirect_reference)) {
6067
0
                    goto jit_failure;
6068
0
                  }
6069
0
                }
6070
0
              }
6071
0
              if (orig_op1_type != IS_UNKNOWN
6072
0
               && (orig_op1_type & IS_TRACE_REFERENCE)) {
6073
0
                if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
6074
0
                    !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
6075
0
                  goto jit_failure;
6076
0
                }
6077
0
                if (opline->op1_type == IS_CV
6078
0
                 && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
6079
0
                  ssa->var_info[ssa_op->op1_def >= 0 ? ssa_op->op1_def : ssa_op->op1_use].guarded_reference = 1;
6080
0
                }
6081
0
              } else {
6082
0
                CHECK_OP1_TRACE_TYPE();
6083
0
              }
6084
0
              if (!(op1_info & MAY_BE_OBJECT)) {
6085
0
                break;
6086
0
              }
6087
0
              if (ssa->var_info && ssa->ops) {
6088
0
                if (ssa_op->op1_use >= 0) {
6089
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
6090
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
6091
0
                    ce = op1_ssa->ce;
6092
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
6093
0
                  }
6094
0
                }
6095
0
              }
6096
0
              if (ssa_op->op1_use >= 0) {
6097
0
                delayed_fetch_this = ssa->var_info[ssa_op->op1_use].delayed_fetch_this;
6098
0
                avoid_refcounting = ssa->var_info[ssa_op->op1_use].avoid_refcounting;
6099
0
              }
6100
0
              if (delayed_fetch_this) {
6101
0
                on_this = 1;
6102
0
              } else if (ssa_op->op1_use >= 0 && ssa->vars[ssa_op->op1_use].definition >= 0) {
6103
0
                on_this = ssa_opcodes[ssa->vars[ssa_op->op1_use].definition]->opcode == ZEND_FETCH_THIS;
6104
0
              } else if (op_array_ssa->ops
6105
0
                      && op_array_ssa->vars
6106
0
                  && op_array_ssa->ops[opline-op_array->opcodes].op1_use >= 0
6107
0
                  && op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition >= 0) {
6108
0
                on_this = op_array->opcodes[op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition].opcode == ZEND_FETCH_THIS;
6109
0
              }
6110
0
            }
6111
0
            if (!zend_jit_fetch_obj(&ctx, opline, op_array, ssa, ssa_op,
6112
0
                op1_info, op1_addr, op1_indirect, ce, ce_is_instanceof,
6113
0
                on_this, delayed_fetch_this, avoid_refcounting, op1_ce,
6114
0
                RES_REG_ADDR(), val_type,
6115
0
                zend_may_throw_ex(opline, ssa_op, op_array, ssa, op1_info, MAY_BE_STRING))) {
6116
0
              goto jit_failure;
6117
0
            }
6118
0
            goto done;
6119
0
          case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
6120
0
            if (!JIT_G(current_frame)
6121
0
             || !JIT_G(current_frame)->call
6122
0
             || !TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
6123
0
              break;
6124
0
            }
6125
0
            ZEND_FALLTHROUGH;
6126
0
          case ZEND_FETCH_STATIC_PROP_R:
6127
0
          case ZEND_FETCH_STATIC_PROP_IS:
6128
0
          case ZEND_FETCH_STATIC_PROP_W:
6129
0
          case ZEND_FETCH_STATIC_PROP_RW:
6130
0
          case ZEND_FETCH_STATIC_PROP_UNSET:
6131
0
            if (!(opline->op1_type == IS_CONST
6132
0
             && (opline->op2_type == IS_CONST
6133
0
              || (opline->op2_type == IS_UNUSED
6134
0
               && ((opline->op2.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
6135
0
                || (opline->op2.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT))))) {
6136
0
              break;
6137
0
            }
6138
0
            if (!zend_jit_fetch_static_prop(&ctx, opline, op_array)) {
6139
0
              goto jit_failure;
6140
0
            }
6141
0
            goto done;
6142
0
          case ZEND_BIND_GLOBAL:
6143
0
            orig_opline = opline;
6144
0
            orig_ssa_op = ssa_op;
6145
0
            while (1) {
6146
0
              if (!ssa->ops || !ssa->var_info) {
6147
0
                op1_info = MAY_BE_ANY|MAY_BE_REF;
6148
0
              } else {
6149
0
                op1_info = OP1_INFO();
6150
0
              }
6151
0
              if (ssa->vars[ssa_op->op1_def].alias == NO_ALIAS) {
6152
0
                ssa->var_info[ssa_op->op1_def].guarded_reference = 1;
6153
0
              }
6154
0
              if (!zend_jit_bind_global(&ctx, opline, op1_info)) {
6155
0
                goto jit_failure;
6156
0
              }
6157
0
              if ((opline+1)->opcode == ZEND_BIND_GLOBAL) {
6158
0
                opline++;
6159
0
                ssa_op++;
6160
0
              } else {
6161
0
                break;
6162
0
              }
6163
0
            }
6164
0
            opline = orig_opline;
6165
0
            ssa_op = orig_ssa_op;
6166
0
            goto done;
6167
0
          case ZEND_RECV:
6168
0
            if (!zend_jit_recv(&ctx, opline, op_array)) {
6169
0
              goto jit_failure;
6170
0
            }
6171
0
            goto done;
6172
0
          case ZEND_RECV_INIT:
6173
0
            orig_opline = opline;
6174
0
            orig_ssa_op = ssa_op;
6175
0
            while (1) {
6176
0
              if (!zend_jit_recv_init(&ctx, opline, op_array,
6177
0
                  (opline + 1)->opcode != ZEND_RECV_INIT,
6178
0
                  zend_may_throw(opline, ssa_op, op_array, ssa))) {
6179
0
                goto jit_failure;
6180
0
              }
6181
0
              if ((opline+1)->opcode == ZEND_RECV_INIT) {
6182
0
                opline++;
6183
0
                ssa_op++;
6184
0
              } else {
6185
0
                break;
6186
0
              }
6187
0
            }
6188
0
            opline = orig_opline;
6189
0
            ssa_op = orig_ssa_op;
6190
0
            goto done;
6191
0
          case ZEND_FREE:
6192
0
          case ZEND_FE_FREE:
6193
0
            op1_info = OP1_INFO();
6194
0
            if (!zend_jit_free(&ctx, opline, op1_info,
6195
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
6196
0
              goto jit_failure;
6197
0
            }
6198
0
            goto done;
6199
0
          case ZEND_ECHO:
6200
0
            op1_info = OP1_INFO();
6201
0
            CHECK_OP1_TRACE_TYPE();
6202
0
            if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_STRING) {
6203
0
              break;
6204
0
            }
6205
0
            if (!zend_jit_echo(&ctx, opline, op1_info)) {
6206
0
              goto jit_failure;
6207
0
            }
6208
0
            goto done;
6209
0
          case ZEND_STRLEN:
6210
0
            op1_info = OP1_INFO();
6211
0
            op1_addr = OP1_REG_ADDR();
6212
0
            if (orig_op1_type == (IS_TRACE_REFERENCE|IS_STRING)) {
6213
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
6214
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
6215
0
                goto jit_failure;
6216
0
              }
6217
0
              if (opline->op1_type == IS_CV
6218
0
               && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
6219
0
                ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
6220
0
              }
6221
0
            } else {
6222
0
              CHECK_OP1_TRACE_TYPE();
6223
0
              if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_STRING) {
6224
0
                break;
6225
0
              }
6226
0
            }
6227
0
            if (!zend_jit_strlen(&ctx, opline, op1_info, op1_addr, RES_REG_ADDR())) {
6228
0
              goto jit_failure;
6229
0
            }
6230
0
            goto done;
6231
0
          case ZEND_COUNT:
6232
0
            op1_info = OP1_INFO();
6233
0
            op1_addr = OP1_REG_ADDR();
6234
0
            if (orig_op1_type == (IS_TRACE_REFERENCE|IS_ARRAY)) {
6235
0
              if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
6236
0
                  !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
6237
0
                goto jit_failure;
6238
0
              }
6239
0
              if (opline->op1_type == IS_CV
6240
0
               && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
6241
0
                ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
6242
0
              }
6243
0
            } else {
6244
0
              CHECK_OP1_TRACE_TYPE();
6245
0
              if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_ARRAY) {
6246
0
                break;
6247
0
              }
6248
0
            }
6249
0
            if (!zend_jit_count(&ctx, opline, op1_info, op1_addr, RES_REG_ADDR(), zend_may_throw(opline, ssa_op, op_array, ssa))) {
6250
0
              goto jit_failure;
6251
0
            }
6252
0
            goto done;
6253
0
          case ZEND_FETCH_THIS:
6254
0
            delayed_fetch_this = 0;
6255
0
            if (ssa_op->result_def >= 0 && opline->result_type != IS_CV) {
6256
0
              if (zend_jit_may_delay_fetch_this(op_array, ssa, ssa_opcodes, ssa_op)) {
6257
0
                ssa->var_info[ssa_op->result_def].delayed_fetch_this = 1;
6258
0
                delayed_fetch_this = 1;
6259
0
              }
6260
0
            }
6261
0
            if (!zend_jit_fetch_this(&ctx, opline, op_array, delayed_fetch_this)) {
6262
0
              goto jit_failure;
6263
0
            }
6264
0
            goto done;
6265
0
          case ZEND_SWITCH_LONG:
6266
0
          case ZEND_SWITCH_STRING:
6267
0
          case ZEND_MATCH:
6268
0
            if (!zend_jit_switch(&ctx, opline, op_array, op_array_ssa, p+1, &zend_jit_traces[ZEND_JIT_TRACE_NUM])) {
6269
0
              goto jit_failure;
6270
0
            }
6271
0
            goto done;
6272
0
          case ZEND_VERIFY_RETURN_TYPE:
6273
0
            if (opline->op1_type == IS_UNUSED) {
6274
              /* Always throws */
6275
0
              break;
6276
0
            }
6277
0
            if (opline->op1_type == IS_CONST) {
6278
              /* TODO Different instruction format, has return value */
6279
0
              break;
6280
0
            }
6281
0
            if (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
6282
              /* Not worth bothering with */
6283
0
              break;
6284
0
            }
6285
0
            op1_info = OP1_INFO();
6286
0
            CHECK_OP1_TRACE_TYPE();
6287
0
            if (op1_info & MAY_BE_REF) {
6288
              /* TODO May need reference unwrapping. */
6289
0
              break;
6290
0
            }
6291
0
            if (!zend_jit_verify_return_type(&ctx, opline, op_array, op1_info)) {
6292
0
              goto jit_failure;
6293
0
            }
6294
0
            goto done;
6295
0
          case ZEND_FE_RESET_R:
6296
0
            op1_info = OP1_INFO();
6297
0
            CHECK_OP1_TRACE_TYPE();
6298
0
            if ((op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) != MAY_BE_ARRAY) {
6299
0
              break;
6300
0
            }
6301
0
            if (!zend_jit_fe_reset(&ctx, opline, op1_info)) {
6302
0
              goto jit_failure;
6303
0
            }
6304
0
            goto done;
6305
0
          case ZEND_FE_FETCH_R:
6306
0
            op1_info = OP1_INFO();
6307
0
            CHECK_OP1_TRACE_TYPE();
6308
0
            if ((op1_info & MAY_BE_ANY) != MAY_BE_ARRAY) {
6309
0
              break;
6310
0
            }
6311
0
            if ((p+1)->op == ZEND_JIT_TRACE_VM || (p+1)->op == ZEND_JIT_TRACE_END) {
6312
0
              const zend_op *exit_opline = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
6313
0
              uint32_t exit_point;
6314
6315
0
              if ((p+1)->opline == exit_opline) {
6316
                /* taken branch (exit from loop) */
6317
0
                exit_opline = opline;
6318
0
                smart_branch_opcode = ZEND_NOP;
6319
0
              } else if ((p+1)->opline == opline + 1) {
6320
                /* not taken branch (loop) */
6321
0
                smart_branch_opcode = ZEND_JMP;
6322
0
              } else {
6323
0
                ZEND_UNREACHABLE();
6324
0
              }
6325
0
              exit_point = zend_jit_trace_get_exit_point(exit_opline, 0);
6326
0
              exit_addr = zend_jit_trace_get_exit_addr(exit_point);
6327
0
              if (!exit_addr) {
6328
0
                goto jit_failure;
6329
0
              }
6330
0
            } else  {
6331
0
              ZEND_UNREACHABLE();
6332
0
            }
6333
0
            if (!zend_jit_fe_fetch(&ctx, opline, op1_info, OP2_INFO(),
6334
0
                -1, smart_branch_opcode, exit_addr)) {
6335
0
              goto jit_failure;
6336
0
            }
6337
0
            goto done;
6338
0
          case ZEND_FETCH_CONSTANT:
6339
0
            if (!zend_jit_fetch_constant(&ctx, opline, op_array, ssa, ssa_op, RES_REG_ADDR())) {
6340
0
              goto jit_failure;
6341
0
            }
6342
0
            goto done;
6343
0
          case ZEND_INIT_METHOD_CALL:
6344
0
            if (opline->op2_type != IS_CONST
6345
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING) {
6346
0
              break;
6347
0
            }
6348
0
            on_this = delayed_fetch_this = 0;
6349
0
            ce = NULL;
6350
0
            ce_is_instanceof = 0;
6351
0
            if (opline->op1_type == IS_UNUSED) {
6352
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
6353
0
              ce = op_array->scope;
6354
              /* scope is NULL for closures. */
6355
0
              if (ce) {
6356
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
6357
0
              }
6358
0
              op1_addr = 0;
6359
0
              on_this = 1;
6360
0
            } else {
6361
0
              op1_info = OP1_INFO();
6362
0
              op1_addr = OP1_REG_ADDR();
6363
0
              if (polymorphic_side_trace) {
6364
0
                op1_info = MAY_BE_OBJECT;
6365
0
              } else if (orig_op1_type != IS_UNKNOWN
6366
0
               && (orig_op1_type & IS_TRACE_REFERENCE)) {
6367
0
                if (!zend_jit_fetch_reference(&ctx, opline, orig_op1_type, &op1_info, &op1_addr,
6368
0
                    !ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
6369
0
                  goto jit_failure;
6370
0
                }
6371
0
                if (opline->op1_type == IS_CV
6372
0
                 && ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
6373
0
                  ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
6374
0
                }
6375
0
              } else {
6376
0
                CHECK_OP1_TRACE_TYPE();
6377
0
              }
6378
0
              if (ssa->var_info && ssa->ops) {
6379
0
                if (ssa_op->op1_use >= 0) {
6380
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
6381
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
6382
0
                    ce = op1_ssa->ce;
6383
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
6384
0
                  }
6385
0
                }
6386
0
              }
6387
0
              if (ssa_op->op1_use >= 0) {
6388
0
                delayed_fetch_this = ssa->var_info[ssa_op->op1_use].delayed_fetch_this;
6389
0
              }
6390
0
              if (delayed_fetch_this) {
6391
0
                on_this = 1;
6392
0
              } else if (ssa_op->op1_use >= 0 && ssa->vars[ssa_op->op1_use].definition >= 0) {
6393
0
                on_this = ssa_opcodes[ssa->vars[ssa_op->op1_use].definition]->opcode == ZEND_FETCH_THIS;
6394
0
              } else if (op_array_ssa->ops
6395
0
                      && op_array_ssa->vars
6396
0
                  && op_array_ssa->ops[opline-op_array->opcodes].op1_use >= 0
6397
0
                  && op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition >= 0) {
6398
0
                on_this = op_array->opcodes[op_array_ssa->vars[op_array_ssa->ops[opline-op_array->opcodes].op1_use].definition].opcode == ZEND_FETCH_THIS;
6399
0
              }
6400
0
            }
6401
0
            frame_flags = TRACE_FRAME_MASK_NESTED;
6402
0
            if (!zend_jit_init_method_call(&ctx, opline,
6403
0
                op_array_ssa->cfg.map ? op_array_ssa->cfg.map[opline - op_array->opcodes] : -1,
6404
0
                op_array, ssa, ssa_op, frame->call_level,
6405
0
                op1_info, op1_addr, ce, ce_is_instanceof, on_this, delayed_fetch_this, op1_ce,
6406
0
                p + 1, peek_checked_stack - checked_stack,
6407
0
                polymorphic_side_trace ? jit->poly_func_ref : -1,
6408
0
                polymorphic_side_trace ? jit->poly_this_ref : -1,
6409
0
                polymorphic_side_trace)) {
6410
0
              goto jit_failure;
6411
0
            }
6412
0
            goto done;
6413
0
          case ZEND_INIT_STATIC_METHOD_CALL:
6414
0
            if (!(opline->op2_type == IS_CONST
6415
0
             && (opline->op1_type == IS_CONST
6416
0
              || (opline->op1_type == IS_UNUSED
6417
0
               && ((opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
6418
0
                || (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT))))) {
6419
0
              break;
6420
0
            }
6421
0
            if (!zend_jit_init_static_method_call(&ctx, opline,
6422
0
                op_array_ssa->cfg.map ? op_array_ssa->cfg.map[opline - op_array->opcodes] : -1,
6423
0
                op_array, ssa, ssa_op, frame->call_level,
6424
0
                p + 1, peek_checked_stack - checked_stack)) {
6425
0
              goto jit_failure;
6426
0
            }
6427
0
            goto done;
6428
0
          case ZEND_INIT_DYNAMIC_CALL:
6429
0
            if (orig_op2_type != IS_OBJECT || op2_ce != zend_ce_closure) {
6430
0
              break;
6431
0
            }
6432
0
            op2_info = OP2_INFO();
6433
0
            CHECK_OP2_TRACE_TYPE();
6434
0
            frame_flags = TRACE_FRAME_MASK_NESTED;
6435
0
            if (!zend_jit_init_closure_call(&ctx, opline, op_array_ssa->cfg.map ? op_array_ssa->cfg.map[opline - op_array->opcodes] : -1, op_array, ssa, ssa_op, frame->call_level, p + 1, peek_checked_stack - checked_stack)) {
6436
0
              goto jit_failure;
6437
0
            }
6438
0
            goto done;
6439
0
          case ZEND_SEND_ARRAY:
6440
0
          case ZEND_SEND_UNPACK:
6441
0
            if (JIT_G(current_frame)
6442
0
             && JIT_G(current_frame)->call) {
6443
0
              TRACE_FRAME_SET_UNKNOWN_NUM_ARGS(JIT_G(current_frame)->call);
6444
0
            }
6445
0
            break;
6446
0
          case ZEND_ROPE_INIT:
6447
0
          case ZEND_ROPE_ADD:
6448
0
          case ZEND_ROPE_END:
6449
0
            op2_info = OP2_INFO();
6450
0
            CHECK_OP2_TRACE_TYPE();
6451
0
            if ((op2_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_STRING) {
6452
0
              break;
6453
0
            }
6454
0
            if (!zend_jit_rope(&ctx, opline, op2_info)) {
6455
0
              goto jit_failure;
6456
0
            }
6457
0
            goto done;
6458
0
          case ZEND_FRAMELESS_ICALL_0:
6459
0
            jit_frameless_icall0(jit, opline);
6460
0
            goto done;
6461
0
          case ZEND_FRAMELESS_ICALL_1:
6462
0
            op1_info = OP1_INFO();
6463
0
            jit_frameless_icall1(jit, opline, op1_info);
6464
0
            goto done;
6465
0
          case ZEND_FRAMELESS_ICALL_2:
6466
0
            op1_info = OP1_INFO();
6467
0
            op2_info = OP2_INFO();
6468
0
            jit_frameless_icall2(jit, opline, op1_info, op2_info);
6469
0
            goto done;
6470
0
          case ZEND_FRAMELESS_ICALL_3:
6471
0
            op1_info = OP1_INFO();
6472
0
            op2_info = OP2_INFO();
6473
0
            jit_frameless_icall3(jit, opline, op1_info, op2_info, OP1_DATA_INFO());
6474
0
            goto done;
6475
0
          default:
6476
0
            break;
6477
0
        }
6478
0
      }
6479
6480
0
      if (opline->opcode != ZEND_NOP && opline->opcode != ZEND_JMP) {
6481
0
        gen_handler = 1;
6482
0
        op1_info = OP1_INFO();
6483
0
        op2_info = OP2_INFO();
6484
0
        if (op1_info & MAY_BE_GUARD) {
6485
0
          op1_info = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
6486
0
        }
6487
0
        if (op2_info & MAY_BE_GUARD) {
6488
0
          op2_info = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
6489
0
        }
6490
0
        if (!zend_jit_trace_handler(&ctx, op_array, opline,
6491
0
            zend_may_throw_ex(opline, ssa_op, op_array, ssa, op1_info, op2_info), p + 1)) {
6492
0
          goto jit_failure;
6493
0
        }
6494
0
        if ((p+1)->op == ZEND_JIT_TRACE_INIT_CALL && (p+1)->func) {
6495
0
          if (opline->opcode == ZEND_NEW && opline->result_type != IS_UNUSED) {
6496
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_OBJECT, 1);
6497
0
          }
6498
0
          if (zend_jit_may_be_polymorphic_call(opline) ||
6499
0
              zend_jit_may_be_modified((p+1)->func, op_array)) {
6500
0
            if (!zend_jit_init_fcall_guard(&ctx, 0, (p+1)->func, opline+1)) {
6501
0
              goto jit_failure;
6502
0
            }
6503
0
          }
6504
0
        }
6505
0
      }
6506
6507
0
done:
6508
0
      polymorphic_side_trace = 0;
6509
0
      if (zend_jit_dec_call_level(opline->opcode)) {
6510
0
        frame->call_level--;
6511
0
      }
6512
6513
0
      if (ra) {
6514
0
        zend_jit_trace_cleanup_stack(&ctx, stack, opline, ssa_op, ssa, ssa_opcodes);
6515
0
      }
6516
6517
0
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
6518
0
       && STACK_FLAGS(stack, EX_VAR_TO_NUM(opline->op1.var)) & (ZREG_ZVAL_ADDREF|ZREG_THIS)) {
6519
0
        SET_STACK_REG(stack, EX_VAR_TO_NUM(opline->op1.var), ZREG_NONE);
6520
0
      }
6521
6522
0
      if (opline->opcode == ZEND_ROPE_INIT) {
6523
        /* clear stack slots used by rope */
6524
0
        uint32_t var = EX_VAR_TO_NUM(opline->result.var);
6525
0
        uint32_t count =
6526
0
          ((opline->extended_value * sizeof(void*)) + (sizeof(zval)-1)) / sizeof(zval);
6527
6528
0
        do {
6529
0
          SET_STACK_TYPE(stack, var, IS_UNKNOWN, 1);
6530
0
          var++;
6531
0
          count--;
6532
0
        } while (count);
6533
0
      }
6534
6535
0
      if (ssa_op) {
6536
0
        zend_ssa_range tmp;
6537
6538
        /* Keep information about known types on abstract stack */
6539
0
        if (ssa_op->result_def >= 0) {
6540
0
          uint8_t type = IS_UNKNOWN;
6541
6542
0
          if ((opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0
6543
0
           || send_result) {
6544
            /* we didn't set result variable */
6545
0
            type = IS_UNKNOWN;
6546
0
          } else if (!(ssa->var_info[ssa_op->result_def].type & MAY_BE_GUARD)
6547
0
           && has_concrete_type(ssa->var_info[ssa_op->result_def].type)) {
6548
0
            type = concrete_type(ssa->var_info[ssa_op->result_def].type);
6549
0
          } else if (opline->opcode == ZEND_QM_ASSIGN) {
6550
0
            if (opline->op1_type != IS_CONST) {
6551
              /* copy */
6552
0
              type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var));
6553
0
            }
6554
0
          } else if (opline->opcode == ZEND_ASSIGN) {
6555
0
            if (opline->op2_type != IS_CONST
6556
0
             && ssa_op->op1_use >= 0
6557
             /* assignment to typed reference may cause conversion */
6558
0
             && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF) == 0) {
6559
              /* copy */
6560
0
              type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var));
6561
0
            }
6562
0
          } else if (opline->opcode == ZEND_POST_INC
6563
0
               || opline->opcode == ZEND_POST_DEC) {
6564
            /* copy */
6565
0
            type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var));
6566
0
          }
6567
0
          if (opline->opcode == ZEND_JMP_SET
6568
0
            || opline->opcode == ZEND_COALESCE
6569
0
            || opline->opcode == ZEND_JMP_NULL) {
6570
0
            if ((p+1)->op != ZEND_JIT_TRACE_VM) {
6571
0
              SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), IS_UNKNOWN, 1);
6572
0
            } else if ((p+1)->opline != (opline + 1)) {
6573
0
              SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), type, 1);
6574
0
            }
6575
0
          } else {
6576
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), type,
6577
0
              (gen_handler || type == IS_UNKNOWN || !ra || !RA_HAS_REG(ssa_op->result_def)));
6578
6579
0
            if (op_array->last_live_range
6580
0
             && opline->result.var > op_array->last_var
6581
0
             && STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var)) != type) {
6582
0
              if (!gen_handler && type != IS_UNKNOWN && ra && RA_HAS_REG(ssa_op->result_def)) {
6583
0
                uint32_t var_num = opline->result.var;
6584
0
                uint32_t op_num = opline - op_array->opcodes;
6585
0
                const zend_live_range *range = op_array->live_range;
6586
0
                int j;
6587
6588
0
                op_num += zend_jit_trace_op_len(opline);
6589
0
                for (j = 0; j < op_array->last_live_range; range++, j++) {
6590
0
                  if (range->start > op_num) {
6591
                    /* further blocks will not be relevant... */
6592
0
                    break;
6593
0
                  } else if (op_num < range->end && var_num == (range->var & ~ZEND_LIVE_MASK)) {
6594
                    /* check if opcodes in range may throw */
6595
0
                    bool store_type = 0;
6596
0
                    const zend_ssa_op *next_ssa_op = ssa_op + zend_jit_trace_op_len(opline);
6597
0
                    const zend_jit_trace_rec *q = p + 1;
6598
6599
0
                    while (1) {
6600
0
                      if (q->op != ZEND_JIT_TRACE_VM) {
6601
0
                        store_type = 1;
6602
0
                        break;
6603
0
                      }
6604
0
                      op_num = q->opline - op_array->opcodes;
6605
0
                      if (op_num >= range->end || op_num < range->start) {
6606
0
                        break;
6607
0
                      }
6608
0
                      if (zend_may_throw(q->opline, next_ssa_op, op_array, ssa)) {
6609
0
                        store_type = 1;
6610
0
                        break;
6611
0
                      }
6612
0
                      next_ssa_op += zend_jit_trace_op_len(q->opline);
6613
0
                      q++;
6614
0
                    }
6615
0
                    if (store_type) {
6616
0
                      var_num = EX_VAR_TO_NUM(var_num);
6617
6618
0
                      if (!zend_jit_store_type(&ctx, var_num, type)) {
6619
0
                        return 0;
6620
0
                      }
6621
0
                      SET_STACK_TYPE(stack, var_num, type, 1);
6622
0
                    }
6623
0
                    break;
6624
0
                  }
6625
0
                }
6626
0
              }
6627
0
            }
6628
0
            if (ssa->var_info[ssa_op->result_def].type & MAY_BE_INDIRECT) {
6629
0
              RESET_STACK_MEM_TYPE(stack, EX_VAR_TO_NUM(opline->result.var));
6630
0
            }
6631
0
            if (type != IS_UNKNOWN) {
6632
0
              ssa->var_info[ssa_op->result_def].type &= ~MAY_BE_GUARD;
6633
0
              if (opline->opcode == ZEND_FETCH_THIS
6634
0
               && delayed_fetch_this) {
6635
0
                SET_STACK_REG_EX(stack, EX_VAR_TO_NUM(opline->result.var), ZREG_NONE, ZREG_THIS);
6636
0
              } else if (ssa->var_info[ssa_op->result_def].avoid_refcounting) {
6637
0
                SET_STACK_REG_EX(stack, EX_VAR_TO_NUM(opline->result.var), ZREG_NONE, ZREG_ZVAL_ADDREF);
6638
0
              } else if (ra && RA_HAS_REG(ssa_op->result_def)) {
6639
0
                SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ra[ssa_op->result_def].ref,
6640
0
                  RA_REG_FLAGS(ssa_op->result_def) & ZREG_STORE);
6641
0
              }
6642
0
            }
6643
0
          }
6644
6645
0
          if (type == IS_LONG
6646
0
           && zend_inference_propagate_range(op_array, ssa, opline, ssa_op, ssa_op->result_def, &tmp)) {
6647
0
            ssa->var_info[ssa_op->result_def].range.min = tmp.min;
6648
0
            ssa->var_info[ssa_op->result_def].range.max = tmp.max;
6649
0
            ssa->var_info[ssa_op->result_def].range.underflow = 0;
6650
0
            ssa->var_info[ssa_op->result_def].range.overflow = 0;
6651
0
            ssa->var_info[ssa_op->result_def].has_range = 1;
6652
0
          }
6653
0
        }
6654
0
        if (ssa_op->op1_def >= 0
6655
0
         && ((opline->opcode != ZEND_QM_ASSIGN && opline->opcode != ZEND_CAST)
6656
0
          || opline->result_type != IS_CV
6657
0
          || opline->result.var != opline->op1.var)) {
6658
0
          uint8_t type = IS_UNKNOWN;
6659
6660
0
          if (!(ssa->var_info[ssa_op->op1_def].type & MAY_BE_GUARD)
6661
0
           && has_concrete_type(ssa->var_info[ssa_op->op1_def].type)) {
6662
0
            type = concrete_type(ssa->var_info[ssa_op->op1_def].type);
6663
0
          } else if (opline->opcode == ZEND_ASSIGN) {
6664
0
            if (!(OP1_INFO() & MAY_BE_REF)
6665
0
             || STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var)) != IS_UNKNOWN) {
6666
0
              if (opline->op2_type != IS_CONST) {
6667
                /* copy */
6668
0
                type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var));
6669
0
              }
6670
0
            }
6671
0
          } else if (opline->opcode == ZEND_SEND_VAR
6672
0
           || opline->opcode == ZEND_CAST
6673
0
           || opline->opcode == ZEND_QM_ASSIGN
6674
0
           || opline->opcode == ZEND_JMP_SET
6675
0
           || opline->opcode == ZEND_COALESCE
6676
0
           || opline->opcode == ZEND_JMP_NULL
6677
0
           || opline->opcode == ZEND_FE_RESET_R) {
6678
            /* keep old value */
6679
0
            type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var));
6680
0
          }
6681
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), type,
6682
0
            (gen_handler || type == IS_UNKNOWN || !ra ||
6683
0
              (!RA_HAS_REG(ssa_op->op1_def) &&
6684
0
                !(ssa->vars[ssa_op->op1_def].no_val &&
6685
0
                  Z_MODE(OP1_REG_ADDR()) == IS_REG &&
6686
0
                    (opline->opcode == ZEND_QM_ASSIGN ||
6687
0
                      opline->opcode == ZEND_SEND_VAR ||
6688
0
                      opline->opcode == ZEND_SEND_VAR_EX ||
6689
0
                      opline->opcode == ZEND_SEND_VAR_NO_REF ||
6690
0
                      opline->opcode == ZEND_SEND_VAR_NO_REF_EX ||
6691
0
                      opline->opcode == ZEND_SEND_FUNC_ARG)))));
6692
0
          if (type != IS_UNKNOWN) {
6693
0
            ssa->var_info[ssa_op->op1_def].type &= ~MAY_BE_GUARD;
6694
0
            if (ra && RA_HAS_REG(ssa_op->op1_def)) {
6695
0
              uint8_t flags = RA_REG_FLAGS(ssa_op->op1_def) & ZREG_STORE;
6696
6697
0
              if (ssa_op->op1_use >= 0) {
6698
0
                if (opline->opcode == ZEND_SEND_VAR
6699
0
                 || opline->opcode == ZEND_CAST
6700
0
                 || opline->opcode == ZEND_QM_ASSIGN
6701
0
                 || opline->opcode == ZEND_JMP_SET
6702
0
                 || opline->opcode == ZEND_COALESCE
6703
0
                 || opline->opcode == ZEND_JMP_NULL
6704
0
                 || opline->opcode == ZEND_FE_RESET_R) {
6705
0
                  if (!RA_HAS_REG(ssa_op->op1_use)) {
6706
0
                    flags |= ZREG_LOAD;
6707
0
                  }
6708
0
                }
6709
0
              }
6710
0
              SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->op1.var), ra[ssa_op->op1_def].ref, flags);
6711
0
            }
6712
0
          }
6713
0
          if (type == IS_LONG
6714
0
           && zend_inference_propagate_range(op_array, ssa, opline, ssa_op, ssa_op->op1_def, &tmp)) {
6715
0
            ssa->var_info[ssa_op->op1_def].range.min = tmp.min;
6716
0
            ssa->var_info[ssa_op->op1_def].range.max = tmp.max;
6717
0
            ssa->var_info[ssa_op->op1_def].range.underflow = 0;
6718
0
            ssa->var_info[ssa_op->op1_def].range.overflow = 0;
6719
0
            ssa->var_info[ssa_op->op1_def].has_range = 1;
6720
0
          }
6721
0
        }
6722
0
        if (ssa_op->op2_def >= 0
6723
0
         && (opline->opcode != ZEND_ASSIGN
6724
0
          || opline->op1_type != IS_CV
6725
0
          || opline->op1.var != opline->op2.var)) {
6726
0
          uint8_t type = IS_UNKNOWN;
6727
6728
0
          if (!(ssa->var_info[ssa_op->op2_def].type & MAY_BE_GUARD)
6729
0
           && has_concrete_type(ssa->var_info[ssa_op->op2_def].type)) {
6730
0
            type = concrete_type(ssa->var_info[ssa_op->op2_def].type);
6731
0
          } else if (opline->opcode == ZEND_ASSIGN) {
6732
            /* keep old value */
6733
0
            type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var));
6734
0
          }
6735
0
          SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op2.var), type,
6736
0
            (gen_handler || type == IS_UNKNOWN || !ra ||
6737
0
              (!RA_HAS_REG(ssa_op->op2_def) &&
6738
0
                !(ssa->vars[ssa_op->op2_def].no_val &&
6739
0
                  Z_MODE(OP2_REG_ADDR()) == IS_REG &&
6740
0
                  opline->opcode == ZEND_ASSIGN))));
6741
0
          if (type != IS_UNKNOWN) {
6742
0
            ssa->var_info[ssa_op->op2_def].type &= ~MAY_BE_GUARD;
6743
0
            if (ra && RA_HAS_REG(ssa_op->op2_def)) {
6744
0
              uint8_t flags = RA_REG_FLAGS(ssa_op->op2_def) & ZREG_STORE;
6745
6746
0
              if (ssa_op->op2_use >= 0) {
6747
0
                if (opline->opcode == ZEND_ASSIGN) {
6748
0
                  if (!RA_HAS_REG(ssa_op->op2_use)
6749
0
                  ) {
6750
0
                    flags |= ZREG_LOAD;
6751
0
                  }
6752
0
                }
6753
0
              }
6754
0
              SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->op2.var), ra[ssa_op->op2_def].ref, flags);
6755
0
            }
6756
0
          }
6757
0
          if (type == IS_LONG
6758
0
           && zend_inference_propagate_range(op_array, ssa, opline, ssa_op, ssa_op->op2_def, &tmp)) {
6759
0
            ssa->var_info[ssa_op->op2_def].range.min = tmp.min;
6760
0
            ssa->var_info[ssa_op->op2_def].range.max = tmp.max;
6761
0
            ssa->var_info[ssa_op->op2_def].range.underflow = 0;
6762
0
            ssa->var_info[ssa_op->op2_def].range.overflow = 0;
6763
0
            ssa->var_info[ssa_op->op2_def].has_range = 1;
6764
0
          }
6765
0
        }
6766
6767
0
        switch (opline->opcode) {
6768
0
          case ZEND_ASSIGN_DIM:
6769
0
          case ZEND_ASSIGN_OBJ:
6770
0
          case ZEND_ASSIGN_STATIC_PROP:
6771
0
          case ZEND_ASSIGN_DIM_OP:
6772
0
          case ZEND_ASSIGN_OBJ_OP:
6773
0
          case ZEND_ASSIGN_STATIC_PROP_OP:
6774
0
          case ZEND_ASSIGN_OBJ_REF:
6775
0
          case ZEND_ASSIGN_STATIC_PROP_REF:
6776
            /* OP_DATA */
6777
0
            ssa_op++;
6778
0
            opline++;
6779
0
            if (ssa_op->op1_def >= 0) {
6780
0
              uint8_t type = IS_UNKNOWN;
6781
6782
0
              if (!(ssa->var_info[ssa_op->op1_def].type & MAY_BE_GUARD)
6783
0
               && has_concrete_type(ssa->var_info[ssa_op->op1_def].type)) {
6784
0
                type = concrete_type(ssa->var_info[ssa_op->op1_def].type);
6785
0
              } else if ((opline-1)->opcode == ZEND_ASSIGN_DIM
6786
0
               || (opline-1)->opcode == ZEND_ASSIGN_OBJ
6787
0
               || (opline-1)->opcode == ZEND_ASSIGN_STATIC_PROP) {
6788
                /* keep old value */
6789
0
                type = STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var));
6790
0
              }
6791
0
              SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), type,
6792
0
                (gen_handler || type == IS_UNKNOWN || !ra || !RA_HAS_REG(ssa_op->op1_def)));
6793
0
              if (type != IS_UNKNOWN) {
6794
0
                ssa->var_info[ssa_op->op1_def].type &= ~MAY_BE_GUARD;
6795
0
                if (ra && RA_HAS_REG(ssa_op->op1_def)) {
6796
0
                  SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->op1.var), ra[ssa_op->op1_def].ref,
6797
0
                    RA_REG_FLAGS(ssa_op->op1_def) & ZREG_STORE);
6798
0
                }
6799
0
              }
6800
0
              if (type == IS_LONG
6801
0
               && zend_inference_propagate_range(op_array, ssa, opline, ssa_op, ssa_op->op1_def, &tmp)) {
6802
0
                ssa->var_info[ssa_op->op1_def].range.min = tmp.min;
6803
0
                ssa->var_info[ssa_op->op1_def].range.max = tmp.max;
6804
0
                ssa->var_info[ssa_op->op1_def].range.underflow = 0;
6805
0
                ssa->var_info[ssa_op->op1_def].range.overflow = 0;
6806
0
                ssa->var_info[ssa_op->op1_def].has_range = 1;
6807
0
              }
6808
0
            }
6809
0
            ssa_op++;
6810
0
            break;
6811
0
          case ZEND_RECV_INIT:
6812
0
              ssa_op++;
6813
0
            opline++;
6814
0
            while (opline->opcode == ZEND_RECV_INIT) {
6815
0
              if (ssa_op->result_def >= 0) {
6816
0
                uint8_t type = IS_UNKNOWN;
6817
6818
0
                if (!(ssa->var_info[ssa_op->result_def].type & MAY_BE_GUARD)
6819
0
                 && has_concrete_type(ssa->var_info[ssa_op->result_def].type)) {
6820
0
                  type = concrete_type(ssa->var_info[ssa_op->result_def].type);
6821
0
                }
6822
0
                SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), type,
6823
0
                  (gen_handler || !ra || !RA_HAS_REG(ssa_op->result_def)));
6824
0
                if (ra && RA_HAS_REG(ssa_op->result_def)) {
6825
0
                  SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->result.var), ra[ssa_op->result_def].ref,
6826
0
                    RA_REG_FLAGS(ssa_op->result_def) & ZREG_STORE);
6827
0
                }
6828
0
              }
6829
0
              ssa_op++;
6830
0
              opline++;
6831
0
            }
6832
0
            break;
6833
0
          case ZEND_BIND_GLOBAL:
6834
0
            ssa_op++;
6835
0
            opline++;
6836
0
            while (opline->opcode == ZEND_BIND_GLOBAL) {
6837
0
              if (ssa_op->op1_def >= 0) {
6838
0
                uint8_t type = IS_UNKNOWN;
6839
6840
0
                if (!(ssa->var_info[ssa_op->op1_def].type & MAY_BE_GUARD)
6841
0
                 && has_concrete_type(ssa->var_info[ssa_op->op1_def].type)) {
6842
0
                  type = concrete_type(ssa->var_info[ssa_op->op1_def].type);
6843
0
                }
6844
0
                SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->op1.var), type,
6845
0
                  (gen_handler || !ra || !RA_HAS_REG(ssa_op->op1_def)));
6846
0
                if (ra && RA_HAS_REG(ssa_op->op1_def)) {
6847
0
                  SET_STACK_REF_EX(stack, EX_VAR_TO_NUM(opline->op1.var), ra[ssa_op->op1_def].ref,
6848
0
                    RA_REG_FLAGS(ssa_op->op1_def) & ZREG_STORE);
6849
0
                }
6850
0
              }
6851
0
              ssa_op++;
6852
0
              opline++;
6853
0
            }
6854
0
            break;
6855
0
          default:
6856
0
            ssa_op += zend_jit_trace_op_len(opline);
6857
0
            break;
6858
0
        }
6859
6860
0
        if (send_result) {
6861
0
          ssa_op++;
6862
0
          p++;
6863
0
          if ((p+1)->op == ZEND_JIT_TRACE_OP1_TYPE) {
6864
0
            p++;
6865
0
          }
6866
0
          send_result = 0;
6867
0
        }
6868
0
      }
6869
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
6870
0
      call = frame->call;
6871
0
      assert(call && &call->func->op_array == p->op_array);
6872
6873
0
      if (opline->opcode == ZEND_DO_UCALL
6874
0
       || opline->opcode == ZEND_DO_FCALL_BY_NAME
6875
0
       || opline->opcode == ZEND_DO_FCALL) {
6876
6877
0
        frame->call_opline = opline;
6878
6879
        /* Check if SEND_UNPACK/SEND_ARRAY may cause enter at different opline */
6880
0
        if (opline > op_array->opcodes) {
6881
0
          const zend_op *prev_opline = opline - 1;
6882
6883
0
          while (prev_opline->opcode == ZEND_EXT_FCALL_BEGIN || prev_opline->opcode == ZEND_TICKS) {
6884
0
            prev_opline--;
6885
0
          }
6886
0
          JIT_G(current_frame) = call;
6887
0
          if ((prev_opline->opcode == ZEND_SEND_ARRAY
6888
0
            || prev_opline->opcode == ZEND_SEND_UNPACK
6889
0
            || prev_opline->opcode == ZEND_CHECK_UNDEF_ARGS)
6890
0
           && p->op_array->num_args
6891
0
           && (p->op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0
6892
0
           && ((p+1)->op == ZEND_JIT_TRACE_VM
6893
0
            || (p+1)->op == ZEND_JIT_TRACE_END)
6894
0
           && (TRACE_FRAME_NUM_ARGS(call) < 0
6895
0
            || TRACE_FRAME_NUM_ARGS(call) < p->op_array->num_args)
6896
0
           && !zend_jit_trace_opline_guard(&ctx, (p+1)->opline)) {
6897
0
            goto jit_failure;
6898
0
          }
6899
0
          JIT_G(current_frame) = frame;
6900
0
        }
6901
0
      }
6902
6903
0
      if ((p+1)->op == ZEND_JIT_TRACE_END) {
6904
0
        p++;
6905
0
        break;
6906
0
      }
6907
0
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
6908
0
        if (TRACE_FRAME_IS_THIS_CHECKED(frame)) {
6909
0
          TRACE_FRAME_SET_THIS_CHECKED(call);
6910
0
        }
6911
0
      } else if (op_array->scope && !(op_array->fn_flags & ZEND_ACC_STATIC)) {
6912
0
        TRACE_FRAME_SET_THIS_CHECKED(call);
6913
0
      }
6914
0
      op_array = (zend_op_array*)p->op_array;
6915
0
      ctx.current_op_array = op_array;
6916
0
      jit_extension =
6917
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
6918
0
      op_array_ssa = &jit_extension->func_info.ssa;
6919
0
      frame->call = call->prev;
6920
0
      call->prev = frame;
6921
0
      if (p->info & ZEND_JIT_TRACE_RETURN_VALUE_USED) {
6922
0
        TRACE_FRAME_SET_RETURN_VALUE_USED(call);
6923
0
      } else {
6924
0
        TRACE_FRAME_SET_RETURN_VALUE_UNUSED(call);
6925
0
      }
6926
0
      JIT_G(current_frame) = frame = call;
6927
0
      stack = frame->stack;
6928
0
      if (ra) {
6929
0
        int j = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
6930
6931
0
        for (i = 0; i < op_array->last_var; i++, j++) {
6932
0
          if (RA_HAS_REG(j) && (RA_REG_FLAGS(j) & ZREG_LOAD) != 0) {
6933
0
            if ((ssa->var_info[j].type & MAY_BE_GUARD) != 0) {
6934
0
              uint8_t op_type;
6935
6936
0
              ssa->var_info[j].type &= ~MAY_BE_GUARD;
6937
0
              op_type = concrete_type(ssa->var_info[j].type);
6938
0
              if (!zend_jit_type_guard(&ctx, NULL, EX_NUM_TO_VAR(i), op_type)) {
6939
0
                goto jit_failure;
6940
0
              }
6941
0
              SET_STACK_TYPE(stack, i, op_type, 1);
6942
0
            }
6943
0
            if (!zend_jit_load_var(&ctx, ssa->var_info[j].type, i, j)) {
6944
0
              goto jit_failure;
6945
0
            }
6946
0
            SET_STACK_REF_EX(stack, i, ra[j].ref, ZREG_LOAD);
6947
0
          }
6948
0
        }
6949
0
      }
6950
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
6951
0
      op_array = (zend_op_array*)p->op_array;
6952
0
      ctx.current_op_array = op_array;
6953
0
      jit_extension =
6954
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
6955
0
      op_array_ssa = &jit_extension->func_info.ssa;
6956
0
      top = frame;
6957
0
      if (frame->prev) {
6958
0
        checked_stack = frame->old_checked_stack;
6959
0
        peek_checked_stack = frame->old_peek_checked_stack;
6960
0
        frame = frame->prev;
6961
0
        stack = frame->stack;
6962
0
        ZEND_ASSERT(&frame->func->op_array == op_array);
6963
0
      } else {
6964
0
        frame = zend_jit_trace_ret_frame(frame, op_array);
6965
0
        TRACE_FRAME_INIT(frame, op_array, TRACE_FRAME_MASK_UNKNOWN_RETURN, -1);
6966
0
        frame->used_stack = checked_stack = peek_checked_stack = 0;
6967
0
        stack = frame->stack;
6968
0
        if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) {
6969
0
          uint32_t j = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
6970
6971
0
          for (i = 0; i < op_array->last_var + op_array->T; i++, j++) {
6972
            /* Initialize abstract stack using SSA */
6973
0
            if (!(ssa->var_info[j].type & MAY_BE_GUARD)
6974
0
             && has_concrete_type(ssa->var_info[j].type)) {
6975
0
              SET_STACK_TYPE(stack, i, concrete_type(ssa->var_info[j].type), 1);
6976
0
            } else {
6977
0
              SET_STACK_TYPE(stack, i, IS_UNKNOWN, 1);
6978
0
            }
6979
0
          }
6980
0
          if (ra) {
6981
0
            j = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
6982
0
            for (i = 0; i < op_array->last_var + op_array->T; i++, j++) {
6983
0
              if (RA_HAS_REG(j) && (RA_REG_FLAGS(j) & ZREG_LOAD) != 0) {
6984
0
                if (!zend_jit_load_var(&ctx, ssa->var_info[j].type, i, j)) {
6985
0
                  goto jit_failure;
6986
0
                }
6987
0
                SET_STACK_REF_EX(stack, i, ra[j].ref, ZREG_LOAD);
6988
0
              }
6989
0
            }
6990
0
          }
6991
0
        } else {
6992
0
          for (i = 0; i < op_array->last_var + op_array->T; i++) {
6993
0
            SET_STACK_TYPE(stack, i, IS_UNKNOWN, 1);
6994
0
          }
6995
0
        }
6996
0
        opline = NULL;
6997
0
      }
6998
0
      JIT_G(current_frame) = frame;
6999
0
      if (res_type != IS_UNKNOWN
7000
0
       && (p+1)->op == ZEND_JIT_TRACE_VM) {
7001
0
        const zend_op *opline = (p+1)->opline - 1;
7002
0
        if (opline->result_type != IS_UNUSED) {
7003
0
            SET_STACK_TYPE(stack, EX_VAR_TO_NUM(opline->result.var), res_type, 1);
7004
0
        }
7005
0
      }
7006
0
      res_type = IS_UNKNOWN;
7007
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
7008
0
      break;
7009
0
    } else if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
7010
0
      const zend_op *init_opline = zend_jit_trace_find_init_fcall_op(p, op_array);
7011
0
      int num_args = -1;
7012
7013
0
      if (init_opline
7014
0
       && init_opline->extended_value <= TRACE_FRAME_MAX_NUM_ARGS) {
7015
0
        num_args = init_opline->extended_value;
7016
0
      }
7017
7018
0
      call = top;
7019
0
      TRACE_FRAME_INIT(call, p->func, frame_flags, num_args);
7020
0
      call->prev = frame->call;
7021
0
      if (!(p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL)) {
7022
0
        TRACE_FRAME_SET_LAST_SEND_BY_VAL(call);
7023
0
        if (init_opline && init_opline->opcode == ZEND_INIT_DYNAMIC_CALL) {
7024
0
          TRACE_FRAME_SET_CLOSURE_CALL(call);
7025
0
        }
7026
0
      }
7027
0
      if (init_opline) {
7028
0
        if (init_opline->opcode != ZEND_NEW
7029
0
         && (init_opline->opcode != ZEND_INIT_METHOD_CALL
7030
0
          || init_opline->op1_type == IS_UNDEF
7031
0
          || (!(p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL)
7032
0
           && ssa_op
7033
0
           && (ssa_op-1)->op1_use >=0
7034
0
           && ssa->var_info[(ssa_op-1)->op1_use].delayed_fetch_this))
7035
0
         && (init_opline->opcode != ZEND_INIT_USER_CALL
7036
0
          || (p->func && (!p->func->common.scope || (p->func->common.fn_flags & ZEND_ACC_STATIC))))
7037
0
         && (init_opline->opcode != ZEND_INIT_DYNAMIC_CALL
7038
0
          || (p->func && (!p->func->common.scope || (p->func->common.fn_flags & ZEND_ACC_STATIC))))
7039
0
        ) {
7040
0
          TRACE_FRAME_SET_NO_NEED_RELEASE_THIS(call);
7041
0
        } else if (init_opline->opcode == ZEND_NEW
7042
0
         || (init_opline->opcode == ZEND_INIT_METHOD_CALL
7043
0
          && init_opline->op1_type != IS_UNDEF
7044
0
          && !(p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL)
7045
0
          && p->func && p->func->common.scope && !(p->func->common.fn_flags & ZEND_ACC_STATIC))) {
7046
0
          TRACE_FRAME_SET_ALWAYS_RELEASE_THIS(call);
7047
0
        }
7048
0
      }
7049
0
      frame->call = call;
7050
0
      top = zend_jit_trace_call_frame(top, p->op_array, ZEND_JIT_TRACE_NUM_ARGS(p->info));
7051
0
      if (p->func) {
7052
0
        if (p->func->type == ZEND_USER_FUNCTION) {
7053
0
          if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) {
7054
0
            zend_jit_op_array_trace_extension *jit_extension =
7055
0
              (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(p->op_array);
7056
7057
0
            i = 0;
7058
0
            while (i < p->op_array->num_args) {
7059
              /* Types of arguments are going to be stored in abstract stack when processing SEV instruction */
7060
0
              SET_STACK_TYPE(call->stack, i, IS_UNKNOWN, 1);
7061
0
              i++;
7062
0
            }
7063
0
            while (i < p->op_array->last_var) {
7064
0
              if (jit_extension
7065
0
               && zend_jit_var_may_alias(p->op_array, &jit_extension->func_info.ssa, i) != NO_ALIAS) {
7066
0
                SET_STACK_TYPE(call->stack, i, IS_UNKNOWN, 1);
7067
0
              } else {
7068
0
                SET_STACK_TYPE(call->stack, i, IS_UNDEF, 1);
7069
0
              }
7070
0
              i++;
7071
0
            }
7072
0
            while (i < p->op_array->last_var + p->op_array->T) {
7073
0
              SET_STACK_TYPE(call->stack, i, IS_UNKNOWN, 1);
7074
0
              i++;
7075
0
            }
7076
0
          } else {
7077
0
            for (i = 0; i < p->op_array->last_var + p->op_array->T; i++) {
7078
0
              SET_STACK_TYPE(call->stack, i, IS_UNKNOWN, 1);
7079
0
            }
7080
0
          }
7081
0
        } else {
7082
0
          ZEND_ASSERT(p->func->type == ZEND_INTERNAL_FUNCTION);
7083
0
          for (i = 0; i < p->op_array->num_args; i++) {
7084
0
            SET_STACK_TYPE(call->stack, i, IS_UNKNOWN, 1);
7085
0
          }
7086
0
        }
7087
0
        if (p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL) {
7088
0
          int skip_guard = 0;
7089
7090
0
          if (init_opline) {
7091
0
            zend_call_info *call_info = jit_extension->func_info.callee_info;
7092
7093
0
            while (call_info) {
7094
0
              if (call_info->caller_init_opline == init_opline
7095
0
                  && !call_info->is_prototype) {
7096
0
                if (op_array->fn_flags & ZEND_ACC_TRAIT_CLONE) {
7097
0
                  if (init_opline->opcode == ZEND_INIT_STATIC_METHOD_CALL
7098
0
                   && init_opline->op1_type != IS_CONST) {
7099
0
                    break;
7100
0
                  } else if (init_opline->opcode == ZEND_INIT_METHOD_CALL) {
7101
0
                    break;
7102
0
                  }
7103
0
                }
7104
0
                skip_guard = 1;
7105
0
                break;
7106
0
              }
7107
0
              call_info = call_info->next_callee;
7108
0
            }
7109
0
            if (!skip_guard
7110
0
             && !zend_jit_may_be_polymorphic_call(init_opline)
7111
0
             && !zend_jit_may_be_modified(p->func, op_array)) {
7112
0
              skip_guard = 1;
7113
0
            }
7114
0
          }
7115
7116
0
          if (!skip_guard) {
7117
0
            if (!opline) {
7118
0
              zend_jit_trace_rec *q = p + 1;
7119
0
              while (q->op != ZEND_JIT_TRACE_VM && q->op != ZEND_JIT_TRACE_END) {
7120
0
                q++;
7121
0
              }
7122
0
              opline = q->opline;
7123
0
              ZEND_ASSERT(opline != NULL);
7124
0
            }
7125
0
            if (!zend_jit_init_fcall_guard(&ctx,
7126
0
                ZEND_JIT_TRACE_FAKE_LEVEL(p->info), p->func, opline)) {
7127
0
              goto jit_failure;
7128
0
            }
7129
0
          }
7130
0
        }
7131
0
      }
7132
0
      call->old_checked_stack = checked_stack;
7133
0
      call->old_peek_checked_stack = peek_checked_stack;
7134
0
      if (p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL) {
7135
0
        frame->call_level++;
7136
0
        call->used_stack = checked_stack = peek_checked_stack = 0;
7137
0
      } else {
7138
0
        if (p->func) {
7139
0
          call->used_stack = zend_vm_calc_used_stack(init_opline->extended_value, (zend_function*)p->func);
7140
0
        } else {
7141
0
          call->used_stack = (ZEND_CALL_FRAME_SLOT + init_opline->extended_value) * sizeof(zval);
7142
0
        }
7143
0
        switch (init_opline->opcode) {
7144
0
          case ZEND_INIT_FCALL:
7145
0
          case ZEND_INIT_FCALL_BY_NAME:
7146
0
          case ZEND_INIT_NS_FCALL_BY_NAME:
7147
0
          case ZEND_INIT_METHOD_CALL:
7148
0
          case ZEND_INIT_DYNAMIC_CALL:
7149
          //case ZEND_INIT_STATIC_METHOD_CALL:
7150
          //case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
7151
          //case ZEND_INIT_USER_CALL:
7152
          //case ZEND_NEW:
7153
0
            checked_stack += call->used_stack;
7154
0
            if (checked_stack > peek_checked_stack) {
7155
0
              peek_checked_stack = checked_stack;
7156
0
            }
7157
0
            break;
7158
0
          default:
7159
0
            checked_stack = peek_checked_stack = 0;
7160
0
        }
7161
0
      }
7162
0
    } else if (p->op == ZEND_JIT_TRACE_DO_ICALL) {
7163
0
      call = frame->call;
7164
0
      if (call) {
7165
0
        checked_stack = call->old_checked_stack;
7166
0
        peek_checked_stack = call->old_peek_checked_stack;
7167
0
        top = call;
7168
0
        frame->call = call->prev;
7169
0
      }
7170
0
    } else {
7171
0
      ZEND_UNREACHABLE();
7172
0
    }
7173
0
  }
7174
7175
0
  ZEND_ASSERT(p->op == ZEND_JIT_TRACE_END);
7176
7177
0
  t = &zend_jit_traces[ZEND_JIT_TRACE_NUM];
7178
7179
0
  if (!parent_trace && zend_jit_trace_uses_initial_ip(&ctx)) {
7180
0
    t->flags |= ZEND_JIT_TRACE_USES_INITIAL_IP;
7181
0
  }
7182
7183
0
  if (p->stop == ZEND_JIT_TRACE_STOP_LOOP
7184
0
   || p->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
7185
0
   || p->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
7186
0
    if (ra) {
7187
0
      zend_ssa_phi *phi = ssa->blocks[1].phis;
7188
7189
0
      while (phi) {
7190
0
        if (RA_HAS_REG(phi->sources[1])
7191
0
         && STACK_MEM_TYPE(stack, phi->var) != STACK_TYPE(stack, phi->var)
7192
0
         && (RA_REG_FLAGS(phi->sources[1]) & (ZREG_LOAD|ZREG_STORE)) == 0) {
7193
7194
0
          if (!RA_HAS_REG(phi->ssa_var)
7195
0
           || (RA_REG_FLAGS(phi->ssa_var) & (ZREG_LOAD|ZREG_STORE)) == 0) {
7196
            /* Store actual type to memory to avoid deoptimization mistakes */
7197
0
            zend_jit_store_var_type(&ctx, phi->var, STACK_TYPE(stack, phi->var));
7198
0
          }
7199
0
        }
7200
0
        phi = phi->next;
7201
0
      }
7202
0
    }
7203
0
    if (p->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
7204
0
      if ((t->flags & ZEND_JIT_TRACE_USES_INITIAL_IP)
7205
0
       && !zend_jit_set_ip(&ctx, p->opline)) {
7206
0
        goto jit_failure;
7207
0
      }
7208
0
    }
7209
0
    t->link = ZEND_JIT_TRACE_NUM;
7210
0
    if (p->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
7211
0
      t->flags |= ZEND_JIT_TRACE_CHECK_INTERRUPT;
7212
0
    }
7213
0
    if (!(t->flags & ZEND_JIT_TRACE_LOOP)) {
7214
0
      const void *timeout_exit_addr = NULL;
7215
7216
0
      t->flags |= ZEND_JIT_TRACE_LOOP;
7217
7218
0
      if (trace_buffer->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
7219
0
        if (!(t->flags & ZEND_JIT_TRACE_USES_INITIAL_IP)
7220
0
         || (ra
7221
0
          && zend_jit_trace_stack_needs_deoptimization(stack, op_array->last_var + op_array->T))) {
7222
          /* Deoptimize to the first instruction of the loop */
7223
0
          uint32_t exit_point = zend_jit_trace_get_exit_point(trace_buffer[1].opline, ZEND_JIT_EXIT_TO_VM);
7224
7225
0
          timeout_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
7226
0
          if (!timeout_exit_addr) {
7227
0
            goto jit_failure;
7228
0
          }
7229
0
        } else {
7230
0
          timeout_exit_addr = zend_jit_stub_handlers[jit_stub_interrupt_handler];
7231
0
        }
7232
0
      }
7233
7234
0
      zend_jit_trace_end_loop(&ctx, jit->trace_loop_ref, timeout_exit_addr); /* jump back to start of the trace loop */
7235
0
    }
7236
0
  } else if (p->stop >= ZEND_JIT_TRACE_STOP_LINK) {
7237
0
    if (ra
7238
0
     && (p-1)->op != ZEND_JIT_TRACE_ENTER
7239
0
     && (p-1)->op != ZEND_JIT_TRACE_BACK
7240
0
     && opline->opcode != ZEND_DO_UCALL
7241
0
     && opline->opcode != ZEND_DO_FCALL
7242
0
     && opline->opcode != ZEND_DO_FCALL_BY_NAME
7243
0
     && opline->opcode != ZEND_INCLUDE_OR_EVAL) {
7244
0
      for (i = 0; i < op_array->last_var + op_array->T; i++) {
7245
0
        int32_t ref = STACK_REF(stack, i);
7246
0
        uint8_t type = STACK_TYPE(stack, i);
7247
7248
0
        if (ref && !(STACK_FLAGS(stack, i) & (ZREG_LOAD|ZREG_STORE))) {
7249
0
          if (!zend_jit_store_ref(jit, 1 << type, i, ref, STACK_MEM_TYPE(stack, i) != type)) {
7250
0
            goto jit_failure;
7251
0
          }
7252
0
          SET_STACK_TYPE(stack, i, type, 1);
7253
0
        } else if (i < op_array->last_var
7254
0
         && type != IS_UNKNOWN
7255
0
         && type != STACK_MEM_TYPE(stack, i)
7256
0
         && zend_jit_trace_must_store_type(op_array, op_array_ssa, opline - op_array->opcodes, i, type)) {
7257
0
          if (!zend_jit_store_type(jit, i, type)) {
7258
0
            return 0;
7259
0
          }
7260
0
          SET_STACK_TYPE(stack, i, type, 1);
7261
0
        }
7262
0
        CLEAR_STACK_REF(stack, i);
7263
0
      }
7264
0
    }
7265
0
    if (p->stop == ZEND_JIT_TRACE_STOP_LINK) {
7266
0
      const void *timeout_exit_addr = NULL;
7267
7268
0
      t->link = zend_jit_find_trace(p->opline->handler);
7269
0
      if (t->link == 0) {
7270
        /* this can happen if ZEND_JIT_EXIT_INVALIDATE was handled
7271
         * by zend_jit_trace_exit() in another thread after this
7272
         * thread set ZEND_JIT_TRACE_STOP_LINK in zend_jit_trace_execute();
7273
         * ZEND_JIT_EXIT_INVALIDATE resets the opline handler to one of
7274
         * the "_counter_handler" functions, and these are not registered
7275
         * tracer functions */
7276
0
        goto jit_failure;
7277
0
      }
7278
0
      if ((zend_jit_traces[t->link].flags & ZEND_JIT_TRACE_USES_INITIAL_IP)
7279
0
       && !zend_jit_set_ip(&ctx, p->opline)) {
7280
0
        goto jit_failure;
7281
0
      }
7282
0
      if (!parent_trace && zend_jit_trace_uses_initial_ip(&ctx)) {
7283
0
        t->flags |= ZEND_JIT_TRACE_USES_INITIAL_IP;
7284
0
      }
7285
0
      if (parent_trace
7286
0
       && (zend_jit_traces[t->link].flags & ZEND_JIT_TRACE_CHECK_INTERRUPT)
7287
0
       && zend_jit_traces[parent_trace].root == t->link) {
7288
0
        if (!(zend_jit_traces[t->link].flags & ZEND_JIT_TRACE_USES_INITIAL_IP)) {
7289
0
          uint32_t exit_point;
7290
7291
0
          for (i = 0; i < op_array->last_var + op_array->T; i++) {
7292
0
            SET_STACK_TYPE(stack, i, IS_UNKNOWN, 1);
7293
0
          }
7294
0
          exit_point = zend_jit_trace_get_exit_point(zend_jit_traces[t->link].opline, ZEND_JIT_EXIT_TO_VM);
7295
0
          timeout_exit_addr = zend_jit_trace_get_exit_addr(exit_point);
7296
0
          if (!timeout_exit_addr) {
7297
0
            goto jit_failure;
7298
0
          }
7299
0
        } else {
7300
0
          timeout_exit_addr = zend_jit_stub_handlers[jit_stub_interrupt_handler];
7301
0
        }
7302
0
      }
7303
0
      zend_jit_trace_link_to_root(&ctx, &zend_jit_traces[t->link], timeout_exit_addr);
7304
0
    } else {
7305
0
      zend_jit_trace_return(&ctx, 0, NULL);
7306
0
    }
7307
0
  } else if (p->stop == ZEND_JIT_TRACE_STOP_RETURN) {
7308
0
    zend_jit_trace_return(&ctx, 0, NULL);
7309
0
  } else {
7310
    // TODO: not implemented ???
7311
0
    ZEND_ASSERT(0 && p->stop);
7312
0
  }
7313
7314
0
  if (ZEND_JIT_EXIT_COUNTERS + t->exit_count >= JIT_G(max_exit_counters)) {
7315
0
    goto jit_failure;
7316
0
  }
7317
7318
0
  handler = zend_jit_finish(&ctx);
7319
7320
0
  if (handler) {
7321
0
    if (p->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL) {
7322
0
      const zend_op_array *rec_op_array;
7323
7324
0
      rec_op_array = op_array = trace_buffer->op_array;
7325
0
      jit_extension =
7326
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
7327
0
      p = trace_buffer + ZEND_JIT_TRACE_START_REC_SIZE;
7328
0
      for (;;p++) {
7329
0
        if (p->op == ZEND_JIT_TRACE_VM) {
7330
0
          opline = p->opline;
7331
0
        } else if (p->op == ZEND_JIT_TRACE_ENTER) {
7332
0
          if (p->op_array == rec_op_array) {
7333
0
            zend_jit_trace_setup_ret_counter(opline, jit_extension->offset);
7334
0
          }
7335
0
          op_array = p->op_array;
7336
0
          jit_extension =
7337
0
            (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
7338
0
        } else if (p->op == ZEND_JIT_TRACE_BACK) {
7339
0
          op_array = p->op_array;
7340
0
          jit_extension =
7341
0
            (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
7342
0
        } else if (p->op == ZEND_JIT_TRACE_END) {
7343
0
          break;
7344
0
        }
7345
0
      }
7346
0
    } else if (p->stop >= ZEND_JIT_TRACE_STOP_LINK) {
7347
0
      if (opline
7348
0
       && (opline->opcode == ZEND_DO_UCALL
7349
0
        || opline->opcode == ZEND_DO_FCALL
7350
0
        || opline->opcode == ZEND_DO_FCALL_BY_NAME
7351
0
        || opline->opcode == ZEND_YIELD
7352
0
        || opline->opcode == ZEND_YIELD_FROM
7353
0
        || opline->opcode == ZEND_INCLUDE_OR_EVAL)) {
7354
0
        zend_jit_trace_setup_ret_counter(opline, jit_extension->offset);
7355
0
      }
7356
0
      if (JIT_G(current_frame)
7357
0
       && JIT_G(current_frame)->prev) {
7358
0
        frame = JIT_G(current_frame)->prev;
7359
0
        do {
7360
0
          if (frame->call_opline) {
7361
0
            op_array = &frame->func->op_array;
7362
0
            jit_extension =
7363
0
              (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
7364
0
            zend_jit_trace_setup_ret_counter(frame->call_opline, jit_extension->offset);
7365
0
          }
7366
0
          frame = frame->prev;
7367
0
        } while (frame);
7368
0
      }
7369
0
    }
7370
0
  }
7371
7372
0
jit_failure:
7373
0
  zend_jit_free_ctx(&ctx);
7374
7375
0
  if (name) {
7376
0
    zend_string_release(name);
7377
0
  }
7378
7379
0
  } zend_catch {
7380
0
    do_bailout = 1;
7381
0
  }  zend_end_try();
7382
7383
0
jit_cleanup:
7384
  /* Clean up used op_arrays */
7385
0
  while (num_op_arrays > 0) {
7386
0
    op_array = op_arrays[--num_op_arrays];
7387
0
    jit_extension =
7388
0
      (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
7389
7390
0
      jit_extension->func_info.num = 0;
7391
0
    jit_extension->func_info.flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC
7392
0
      | ZEND_FUNC_JIT_ON_PROF_REQUEST
7393
0
      | ZEND_FUNC_JIT_ON_HOT_COUNTERS
7394
0
      | ZEND_FUNC_JIT_ON_HOT_TRACE;
7395
0
    memset(&jit_extension->func_info.ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa));
7396
0
  }
7397
7398
0
  zend_arena_release(&CG(arena), checkpoint);
7399
7400
0
  JIT_G(current_frame) = NULL;
7401
0
  JIT_G(current_trace) = NULL;
7402
7403
0
  if (do_bailout) {
7404
0
    zend_bailout();
7405
0
  }
7406
7407
0
  return handler;
7408
0
}
7409
7410
static zend_string *zend_jit_trace_escape_name(uint32_t trace_num, uint32_t exit_num)
7411
0
{
7412
0
  smart_str buf = {0};
7413
7414
0
  smart_str_appends(&buf," ESCAPE-");
7415
0
  smart_str_append_long(&buf, (zend_long)trace_num);
7416
0
  smart_str_appendc(&buf, '-');
7417
0
  smart_str_append_long(&buf, (zend_long)exit_num);
7418
0
  smart_str_0(&buf);
7419
0
  return buf.s;
7420
0
}
7421
7422
static zend_vm_opcode_handler_t zend_jit_trace_exit_to_vm(uint32_t trace_num, uint32_t exit_num)
7423
0
{
7424
0
  zend_vm_opcode_handler_t handler = NULL;
7425
0
  zend_jit_ctx ctx;
7426
0
  zend_string *name;
7427
0
  void *checkpoint;
7428
0
  const zend_op *opline;
7429
0
  uint32_t stack_size;
7430
0
  zend_jit_trace_stack *stack;
7431
0
  bool original_handler = false;
7432
7433
0
  if (!zend_jit_trace_exit_needs_deoptimization(trace_num, exit_num)) {
7434
0
    return zend_jit_stub_handlers[jit_stub_trace_escape];
7435
0
  }
7436
7437
0
  name = zend_jit_trace_escape_name(trace_num, exit_num);
7438
7439
0
  if (!zend_jit_deoptimizer_start(&ctx, name, trace_num, exit_num)) {
7440
0
    zend_string_release(name);
7441
0
    return NULL;
7442
0
  }
7443
7444
0
  checkpoint = zend_arena_checkpoint(CG(arena));;
7445
7446
  /* Deoptimization */
7447
0
  stack_size = zend_jit_traces[trace_num].exit_info[exit_num].stack_size;
7448
0
  stack =  zend_jit_traces[trace_num].exit_info[exit_num].stack_size ?
7449
0
    zend_jit_traces[trace_num].stack_map + zend_jit_traces[trace_num].exit_info[exit_num].stack_offset :
7450
0
    NULL;
7451
7452
0
  if (!zend_jit_trace_deoptimization(&ctx,
7453
0
      &zend_jit_traces[trace_num].exit_info[exit_num],
7454
0
      stack, stack_size, NULL, NULL,
7455
0
      zend_jit_traces[trace_num].constants,
7456
0
      false)) {
7457
0
    goto jit_failure;
7458
0
  }
7459
7460
0
  opline = zend_jit_traces[trace_num].exit_info[exit_num].opline;
7461
0
  if (opline) {
7462
0
    if (opline == zend_jit_traces[zend_jit_traces[trace_num].root].opline) {
7463
0
      zend_jit_op_array_trace_extension *jit_extension =
7464
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(zend_jit_traces[zend_jit_traces[trace_num].root].op_array);
7465
7466
0
      if (ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->orig_handler != opline->handler) {
7467
        /* prevent endless loop */
7468
0
        original_handler = true;
7469
0
      }
7470
0
    }
7471
0
    zend_jit_set_ip(&ctx, opline);
7472
0
  }
7473
7474
0
  zend_jit_trace_return(&ctx, original_handler, opline);
7475
7476
0
  handler = zend_jit_finish(&ctx);
7477
7478
0
jit_failure:
7479
0
  zend_jit_free_ctx(&ctx);
7480
0
  zend_string_release(name);
7481
0
  zend_arena_release(&CG(arena), checkpoint);
7482
0
  return handler;
7483
0
}
7484
7485
static zend_jit_trace_stop zend_jit_compile_root_trace(zend_jit_trace_rec *trace_buffer, const zend_op *opline, size_t offset)
7486
0
{
7487
0
  zend_jit_trace_stop ret;
7488
0
  zend_vm_opcode_handler_t handler;
7489
0
  uint8_t orig_trigger;
7490
0
  zend_jit_trace_info *t = NULL;
7491
0
  zend_jit_trace_exit_info exit_info[ZEND_JIT_TRACE_MAX_EXITS];
7492
0
  bool do_bailout = 0;
7493
7494
0
  zend_shared_alloc_lock();
7495
7496
  /* Checks under lock */
7497
0
  if ((ZEND_OP_TRACE_INFO(opline, offset)->trace_flags & ZEND_JIT_TRACE_JITED)) {
7498
0
    ret = ZEND_JIT_TRACE_STOP_ALREADY_DONE;
7499
0
  } else if (ZEND_JIT_TRACE_NUM >= JIT_G(max_root_traces)) {
7500
0
    ret = ZEND_JIT_TRACE_STOP_TOO_MANY_TRACES;
7501
0
  } else {
7502
0
    zend_try {
7503
0
      SHM_UNPROTECT();
7504
0
      zend_jit_unprotect();
7505
7506
0
      t = &zend_jit_traces[ZEND_JIT_TRACE_NUM];
7507
7508
0
      t->id = ZEND_JIT_TRACE_NUM;
7509
0
      t->root = ZEND_JIT_TRACE_NUM;
7510
0
      t->parent = 0;
7511
0
      t->link = 0;
7512
0
      t->exit_count = 0;
7513
0
      t->child_count = 0;
7514
0
      t->stack_map_size = 0;
7515
0
      t->flags = 0;
7516
0
      t->polymorphism = 0;
7517
0
      t->jmp_table_size = 0;
7518
0
      t->op_array = trace_buffer[0].op_array;
7519
0
      if (!(t->op_array->fn_flags & ZEND_ACC_IMMUTABLE)) {
7520
0
        zend_jit_op_array_trace_extension *jit_extension =
7521
0
          (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(t->op_array);
7522
0
        t->op_array = jit_extension->op_array;
7523
0
      }
7524
0
      t->opline = trace_buffer[1].opline;
7525
0
      t->exit_info = exit_info;
7526
0
      t->stack_map = NULL;
7527
0
      t->consts_count = 0;
7528
0
      t->constants = NULL;
7529
7530
0
      orig_trigger = JIT_G(trigger);
7531
0
      JIT_G(trigger) = ZEND_JIT_ON_HOT_TRACE;
7532
7533
0
      handler = zend_jit_trace(trace_buffer, 0, 0);
7534
7535
0
      JIT_G(trigger) = orig_trigger;
7536
7537
0
      if (handler) {
7538
0
        zend_jit_trace_exit_info *shared_exit_info = NULL;
7539
7540
0
        t->exit_info = NULL;
7541
0
        if (t->exit_count) {
7542
          /* reallocate exit_info into shared memory */
7543
0
          shared_exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc(
7544
0
            sizeof(zend_jit_trace_exit_info) * t->exit_count);
7545
7546
0
          if (!shared_exit_info) {
7547
0
              if (t->stack_map) {
7548
0
              efree(t->stack_map);
7549
0
              t->stack_map = NULL;
7550
0
            }
7551
0
            if (t->constants) {
7552
0
              efree(t->constants);
7553
0
              t->constants = NULL;
7554
0
            }
7555
0
            ret = ZEND_JIT_TRACE_STOP_NO_SHM;
7556
0
            goto exit;
7557
0
          }
7558
0
          memcpy(shared_exit_info, exit_info,
7559
0
            sizeof(zend_jit_trace_exit_info) * t->exit_count);
7560
0
          t->exit_info = shared_exit_info;
7561
0
        }
7562
7563
0
          if (t->stack_map_size) {
7564
0
          zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc(t->stack_map_size * sizeof(zend_jit_trace_stack));
7565
0
          if (!shared_stack_map) {
7566
0
            efree(t->stack_map);
7567
0
            t->stack_map = NULL;
7568
0
            if (t->constants) {
7569
0
              efree(t->constants);
7570
0
              t->constants = NULL;
7571
0
            }
7572
0
            ret = ZEND_JIT_TRACE_STOP_NO_SHM;
7573
0
            goto exit;
7574
0
          }
7575
0
          memcpy(shared_stack_map, t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
7576
0
          efree(t->stack_map);
7577
0
          t->stack_map = shared_stack_map;
7578
0
          }
7579
7580
0
        if (t->consts_count) {
7581
0
          zend_jit_exit_const *constants = (zend_jit_exit_const*)zend_shared_alloc(t->consts_count * sizeof(zend_jit_exit_const));
7582
0
          if (!constants) {
7583
0
            efree(t->constants);
7584
0
            ret = ZEND_JIT_TRACE_STOP_NO_SHM;
7585
0
            goto exit;
7586
0
          }
7587
0
          memcpy(constants, t->constants, t->consts_count * sizeof(zend_jit_exit_const));
7588
0
          efree(t->constants);
7589
0
          t->constants = constants;
7590
0
        }
7591
7592
0
        t->exit_counters = ZEND_JIT_EXIT_COUNTERS;
7593
0
        ZEND_JIT_EXIT_COUNTERS += t->exit_count;
7594
7595
0
        ((zend_op*)opline)->handler = handler;
7596
7597
0
        ZEND_JIT_TRACE_NUM++;
7598
0
        ZEND_OP_TRACE_INFO(opline, offset)->trace_flags |= ZEND_JIT_TRACE_JITED;
7599
7600
0
        ret = ZEND_JIT_TRACE_STOP_COMPILED;
7601
0
      } else if (t->exit_count >= ZEND_JIT_TRACE_MAX_EXITS ||
7602
0
                 ZEND_JIT_EXIT_COUNTERS + t->exit_count >= JIT_G(max_exit_counters)) {
7603
0
          if (t->stack_map) {
7604
0
          efree(t->stack_map);
7605
0
          t->stack_map = NULL;
7606
0
        }
7607
0
        if (t->constants) {
7608
0
          efree(t->constants);
7609
0
          t->constants = NULL;
7610
0
        }
7611
0
        ret = ZEND_JIT_TRACE_STOP_TOO_MANY_EXITS;
7612
0
      } else {
7613
0
          if (t->stack_map) {
7614
0
          efree(t->stack_map);
7615
0
          t->stack_map = NULL;
7616
0
        }
7617
0
        if (t->constants) {
7618
0
          efree(t->constants);
7619
0
          t->constants = NULL;
7620
0
        }
7621
0
        ret = ZEND_JIT_TRACE_STOP_COMPILER_ERROR;
7622
0
      }
7623
7624
0
exit:;
7625
0
    } zend_catch {
7626
0
      do_bailout = 1;
7627
0
    } zend_end_try();
7628
7629
0
    zend_jit_protect();
7630
0
    SHM_PROTECT();
7631
0
  }
7632
7633
0
  zend_shared_alloc_unlock();
7634
7635
0
  if (do_bailout) {
7636
0
    zend_bailout();
7637
0
  }
7638
7639
0
  if ((JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_EXIT_INFO) != 0
7640
0
   && ret == ZEND_JIT_TRACE_STOP_COMPILED
7641
0
   && t->exit_count > 0) {
7642
0
    zend_jit_dump_exit_info(t);
7643
0
  }
7644
7645
0
  return ret;
7646
0
}
7647
7648
/* Set counting handler back to original VM handler. */
7649
static void zend_jit_stop_hot_trace_counters(zend_op_array *op_array)
7650
0
{
7651
0
  zend_jit_op_array_trace_extension *jit_extension;
7652
0
  uint32_t i;
7653
7654
0
  jit_extension = (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
7655
0
  for (i = 0; i < op_array->last; i++) {
7656
    /* Opline with Jit-ed code handler is skipped. */
7657
0
    if (jit_extension->trace_info[i].trace_flags &
7658
0
        (ZEND_JIT_TRACE_JITED|ZEND_JIT_TRACE_BLACKLISTED)) {
7659
0
      continue;
7660
0
    }
7661
0
    if (jit_extension->trace_info[i].trace_flags &
7662
0
        (ZEND_JIT_TRACE_START_LOOP | ZEND_JIT_TRACE_START_ENTER | ZEND_JIT_TRACE_START_RETURN)) {
7663
0
      op_array->opcodes[i].handler = jit_extension->trace_info[i].orig_handler;
7664
0
    }
7665
0
  }
7666
0
}
7667
7668
/* Get the tracing op_array. */
7669
static void zend_jit_stop_persistent_op_array(zend_op_array *op_array)
7670
0
{
7671
0
  zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
7672
0
  if (!func_info) {
7673
0
    return;
7674
0
  }
7675
0
  if (func_info->flags & ZEND_FUNC_JIT_ON_HOT_TRACE) {
7676
0
    zend_jit_stop_hot_trace_counters(op_array);
7677
0
  }
7678
0
}
7679
7680
/* Get all op_arrays with counter handler. */
7681
static void zend_jit_stop_persistent_script(zend_persistent_script *script)
7682
0
{
7683
0
  zend_class_entry *ce;
7684
0
  zend_op_array *op_array;
7685
7686
0
  zend_jit_stop_persistent_op_array(&script->script.main_op_array);
7687
7688
0
  ZEND_HASH_FOREACH_PTR(&script->script.function_table, op_array) {
7689
0
    zend_jit_stop_persistent_op_array(op_array);
7690
0
  } ZEND_HASH_FOREACH_END();
7691
7692
0
  ZEND_HASH_FOREACH_PTR(&script->script.class_table, ce) {
7693
0
    ZEND_HASH_FOREACH_PTR(&ce->function_table, op_array) {
7694
0
      if (op_array->type == ZEND_USER_FUNCTION) {
7695
0
        zend_jit_stop_persistent_op_array(op_array);
7696
0
      }
7697
0
    } ZEND_HASH_FOREACH_END();
7698
0
  } ZEND_HASH_FOREACH_END();
7699
0
}
7700
7701
/* Get all scripts which are accelerated by JIT */
7702
static void zend_jit_stop_counter_handlers(void)
7703
0
{
7704
0
  if (ZCSG(jit_counters_stopped)) {
7705
0
    return;
7706
0
  }
7707
7708
0
  zend_shared_alloc_lock();
7709
  /* mprotect has an extreme overhead, avoid calls to it for every function. */
7710
0
  SHM_UNPROTECT();
7711
0
  if (!ZCSG(jit_counters_stopped)) {
7712
0
    ZCSG(jit_counters_stopped) = true;
7713
0
    for (uint32_t i = 0; i < ZCSG(hash).max_num_entries; i++) {
7714
0
      zend_accel_hash_entry *cache_entry;
7715
0
      for (cache_entry = ZCSG(hash).hash_table[i]; cache_entry; cache_entry = cache_entry->next) {
7716
0
        zend_persistent_script *script;
7717
0
        if (cache_entry->indirect) continue;
7718
0
        script = (zend_persistent_script *)cache_entry->data;
7719
0
        zend_jit_stop_persistent_script(script);
7720
0
      }
7721
0
    }
7722
0
  }
7723
0
  SHM_PROTECT();
7724
0
  zend_shared_alloc_unlock();
7725
0
}
7726
7727
static void zend_jit_blacklist_root_trace(const zend_op *opline, size_t offset)
7728
0
{
7729
0
  zend_shared_alloc_lock();
7730
7731
0
  if (!(ZEND_OP_TRACE_INFO(opline, offset)->trace_flags & ZEND_JIT_TRACE_BLACKLISTED)) {
7732
0
    SHM_UNPROTECT();
7733
0
    zend_jit_unprotect();
7734
7735
0
    ((zend_op*)opline)->handler =
7736
0
      ZEND_OP_TRACE_INFO(opline, offset)->orig_handler;
7737
7738
0
    ZEND_OP_TRACE_INFO(opline, offset)->trace_flags |= ZEND_JIT_TRACE_BLACKLISTED;
7739
7740
0
    zend_jit_protect();
7741
0
    SHM_PROTECT();
7742
0
  }
7743
7744
0
  zend_shared_alloc_unlock();
7745
0
}
7746
7747
11
ZEND_EXT_API void zend_jit_blacklist_function(zend_op_array *op_array) {
7748
11
  zend_jit_op_array_trace_extension *jit_extension = (zend_jit_op_array_trace_extension *)ZEND_FUNC_INFO(op_array);
7749
11
  if (!jit_extension || !(jit_extension->func_info.flags & ZEND_FUNC_JIT_ON_HOT_TRACE)) {
7750
11
    return;
7751
11
  }
7752
7753
0
  zend_shared_alloc_lock();
7754
0
  SHM_UNPROTECT();
7755
0
  zend_jit_unprotect();
7756
7757
0
  zend_jit_stop_persistent_op_array(op_array);
7758
0
  jit_extension->func_info.flags &= ~ZEND_FUNC_JIT_ON_HOT_TRACE;
7759
7760
0
  zend_jit_protect();
7761
0
  SHM_PROTECT();
7762
0
  zend_shared_alloc_unlock();
7763
0
}
7764
7765
static bool zend_jit_trace_is_bad_root(const zend_op *opline, zend_jit_trace_stop stop, size_t offset)
7766
0
{
7767
0
  const zend_op **cache_opline = JIT_G(bad_root_cache_opline);
7768
0
  uint8_t *cache_count = JIT_G(bad_root_cache_count);
7769
0
  uint8_t *cache_stop = JIT_G(bad_root_cache_stop);
7770
0
  uint32_t cache_slot = JIT_G(bad_root_slot);
7771
0
  uint32_t i;
7772
7773
0
  for (i = 0; i < ZEND_JIT_TRACE_BAD_ROOT_SLOTS; i++) {
7774
0
    if (cache_opline[i] == opline) {
7775
0
      if (cache_count[i] >= JIT_G(blacklist_root_trace) - 1) {
7776
0
        cache_opline[i] = NULL;
7777
0
        return true;
7778
0
      } else {
7779
#if 0
7780
        if (ZEND_OP_TRACE_INFO(opline, offset)->counter) {
7781
          *ZEND_OP_TRACE_INFO(opline, offset)->counter =
7782
            random() % ZEND_JIT_TRACE_COUNTER_MAX;
7783
        }
7784
#endif
7785
0
        cache_count[i]++;
7786
0
        cache_stop[i] = stop;
7787
0
        return false;
7788
0
      }
7789
0
    }
7790
0
  }
7791
0
  i = cache_slot;
7792
0
  cache_opline[i] = opline;
7793
0
  cache_count[i] = 1;
7794
0
  cache_stop[i] = stop;
7795
0
  cache_slot = (i + 1) % ZEND_JIT_TRACE_BAD_ROOT_SLOTS;
7796
0
  JIT_G(bad_root_slot) = cache_slot;
7797
0
  return false;
7798
0
}
7799
7800
static void zend_jit_dump_trace(zend_jit_trace_rec *trace_buffer, zend_ssa *tssa)
7801
0
{
7802
0
  zend_jit_trace_rec *p = trace_buffer;
7803
0
  const zend_op_array *op_array;
7804
0
  const zend_op *opline;
7805
0
  uint32_t level = 1 + trace_buffer[0].level;
7806
0
  int idx, len, i, v, vars_count, call_level;
7807
7808
0
  ZEND_ASSERT(p->op == ZEND_JIT_TRACE_START);
7809
0
  op_array = p->op_array;
7810
0
  p += ZEND_JIT_TRACE_START_REC_SIZE;
7811
0
  idx = 0;
7812
0
  call_level = 0;
7813
7814
0
  if (tssa && tssa->var_info) {
7815
0
    if (trace_buffer->start == ZEND_JIT_TRACE_START_ENTER) {
7816
0
      vars_count = op_array->last_var;
7817
0
    } else {
7818
0
      vars_count = op_array->last_var + op_array->T;
7819
0
    }
7820
0
    for (i = 0; i < vars_count; i++) {
7821
0
      if (tssa->vars[i].use_chain >= 0 || tssa->vars[i].phi_use_chain) {
7822
0
        fprintf(stderr, "    %*c;", level, ' ');
7823
0
        zend_dump_ssa_var(op_array, tssa, i, 0, i, ZEND_DUMP_RC_INFERENCE);
7824
0
        fprintf(stderr, "\n");
7825
0
      }
7826
0
    }
7827
0
    if (trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
7828
0
     || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
7829
0
     || trace_buffer->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
7830
0
      zend_ssa_phi *p = tssa->blocks[1].phis;
7831
7832
0
      fprintf(stderr, "LOOP:\n");
7833
7834
0
      while (p) {
7835
0
        fprintf(stderr, "     ;");
7836
0
        zend_dump_ssa_var(op_array, tssa, p->ssa_var, 0, p->var, ZEND_DUMP_RC_INFERENCE);
7837
0
        fprintf(stderr, " = Phi(");
7838
0
        zend_dump_ssa_var(op_array, tssa, p->sources[0], 0, p->var, ZEND_DUMP_RC_INFERENCE);
7839
0
        fprintf(stderr, ", ");
7840
0
        zend_dump_ssa_var(op_array, tssa, p->sources[1], 0, p->var, ZEND_DUMP_RC_INFERENCE);
7841
0
        fprintf(stderr, ")\n");
7842
0
        p = p->next;
7843
0
      }
7844
0
    }
7845
0
  }
7846
7847
0
  while (1) {
7848
0
    if (p->op == ZEND_JIT_TRACE_VM) {
7849
0
      uint8_t op1_type, op2_type, op3_type;
7850
7851
0
      opline = p->opline;
7852
0
      fprintf(stderr, "%04d%*c",
7853
0
        (int)(opline - op_array->opcodes),
7854
0
        level, ' ');
7855
0
      zend_dump_op(op_array, NULL, opline, ZEND_DUMP_RC_INFERENCE, tssa, (tssa && tssa->ops) ? tssa->ops + idx : NULL);
7856
7857
0
      op1_type = p->op1_type;
7858
0
      op2_type = p->op2_type;
7859
0
      op3_type = p->op3_type;
7860
0
      if (op1_type != IS_UNKNOWN || op2_type != IS_UNKNOWN || op3_type != IS_UNKNOWN) {
7861
0
        fprintf(stderr, " ;");
7862
0
        if (op1_type != IS_UNKNOWN) {
7863
0
          const char *ref = (op1_type & IS_TRACE_INDIRECT) ?
7864
0
            ((op1_type & IS_TRACE_REFERENCE) ? "*&" : "*") :
7865
0
            ((op1_type & IS_TRACE_REFERENCE) ? "&" : "");
7866
0
          if ((p+1)->op == ZEND_JIT_TRACE_OP1_TYPE) {
7867
0
            p++;
7868
0
            fprintf(stderr, " op1(%sobject of class %s)", ref,
7869
0
              ZSTR_VAL(p->ce->name));
7870
0
          } else {
7871
0
            const char *type = ((op1_type & ~IS_TRACE_INDIRECT) == 0) ? "undef" : zend_get_type_by_const(op1_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT|IS_TRACE_PACKED));
7872
0
            fprintf(stderr, " op1(%s%s%s)", ref, (op1_type & IS_TRACE_PACKED) ? "packed " : "", type);
7873
0
          }
7874
0
        }
7875
0
        if (op2_type != IS_UNKNOWN) {
7876
0
          const char *ref = (op2_type & IS_TRACE_INDIRECT) ?
7877
0
            ((op2_type & IS_TRACE_REFERENCE) ? "*&" : "*") :
7878
0
            ((op2_type & IS_TRACE_REFERENCE) ? "&" : "");
7879
0
          if ((p+1)->op == ZEND_JIT_TRACE_OP2_TYPE) {
7880
0
            p++;
7881
0
            fprintf(stderr, " op2(%sobject of class %s)", ref,
7882
0
              ZSTR_VAL(p->ce->name));
7883
0
          } else {
7884
0
            const char *type = ((op2_type & ~IS_TRACE_INDIRECT) == 0) ? "undef" : zend_get_type_by_const(op2_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT));
7885
0
            fprintf(stderr, " op2(%s%s)", ref, type);
7886
0
          }
7887
0
        }
7888
0
        if (op3_type != IS_UNKNOWN) {
7889
0
          const char *ref = (op3_type & IS_TRACE_INDIRECT) ?
7890
0
            ((op3_type & IS_TRACE_REFERENCE) ? "*&" : "*") :
7891
0
            ((op3_type & IS_TRACE_REFERENCE) ? "&" : "");
7892
0
          const char *type = ((op3_type & ~IS_TRACE_INDIRECT) == 0) ? "undef" : zend_get_type_by_const(op3_type & ~(IS_TRACE_REFERENCE|IS_TRACE_INDIRECT));
7893
0
          fprintf(stderr, " op3(%s%s)", ref, type);
7894
0
        }
7895
0
      }
7896
0
      if ((p+1)->op == ZEND_JIT_TRACE_VAL_INFO) {
7897
0
        uint8_t val_type;
7898
0
        const char *type;
7899
7900
0
        if (op1_type == IS_UNKNOWN && op2_type == IS_UNKNOWN && op3_type == IS_UNKNOWN) {
7901
0
          fprintf(stderr, " ;");
7902
0
        }
7903
0
        p++;
7904
0
        val_type = p->op1_type;
7905
7906
0
        if (val_type == IS_UNDEF) {
7907
0
          type = "undef";
7908
0
        } else if (val_type == IS_REFERENCE) {
7909
0
          type = "ref";
7910
0
        } else {
7911
0
          type = zend_get_type_by_const(val_type);
7912
0
        }
7913
0
        fprintf(stderr, " val(%s)", type);
7914
0
      }
7915
0
      fprintf(stderr, "\n");
7916
0
      idx++;
7917
7918
0
      len = zend_jit_trace_op_len(opline);
7919
0
      while (len > 1) {
7920
0
        opline++;
7921
0
        fprintf(stderr, "%04d%*c;",
7922
0
          (int)(opline - op_array->opcodes),
7923
0
          level, ' ');
7924
0
        zend_dump_op(op_array, NULL, opline, ZEND_DUMP_RC_INFERENCE, tssa, (tssa && tssa->ops) ? tssa->ops + idx : NULL);
7925
0
        idx++;
7926
0
        len--;
7927
0
        fprintf(stderr, "\n");
7928
0
      }
7929
0
    } else if (p->op == ZEND_JIT_TRACE_ENTER) {
7930
0
      op_array = p->op_array;
7931
0
      fprintf(stderr, "    %*c>enter %s%s%s\n",
7932
0
        level, ' ',
7933
0
        op_array->scope ? ZSTR_VAL(op_array->scope->name) : "",
7934
0
        op_array->scope ? "::" : "",
7935
0
        op_array->function_name ?
7936
0
          ZSTR_VAL(op_array->function_name) :
7937
0
          ZSTR_VAL(op_array->filename));
7938
0
      level++;
7939
0
      if (tssa && tssa->var_info) {
7940
0
        call_level++;
7941
0
        v = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
7942
0
        vars_count = op_array->last_var;
7943
0
        for (i = 0; i < vars_count; i++, v++) {
7944
0
          if (tssa->vars[v].use_chain >= 0 || tssa->vars[v].phi_use_chain) {
7945
0
            fprintf(stderr, "    %*c;", level, ' ');
7946
0
            zend_dump_ssa_var(op_array, tssa, v, 0, i, ZEND_DUMP_RC_INFERENCE);
7947
0
            fprintf(stderr, "\n");
7948
0
          }
7949
0
        }
7950
0
      }
7951
0
    } else if (p->op == ZEND_JIT_TRACE_BACK) {
7952
0
      op_array = p->op_array;
7953
0
      level--;
7954
0
      fprintf(stderr, "    %*c<back %s%s%s\n",
7955
0
        level, ' ',
7956
0
        op_array->scope ? ZSTR_VAL(op_array->scope->name) : "",
7957
0
        op_array->scope ? "::" : "",
7958
0
        op_array->function_name ?
7959
0
          ZSTR_VAL(op_array->function_name) :
7960
0
          ZSTR_VAL(op_array->filename));
7961
0
      if (tssa && tssa->var_info) {
7962
0
        if (call_level == 0) {
7963
0
          v = ZEND_JIT_TRACE_GET_FIRST_SSA_VAR(p->info);
7964
0
          vars_count = op_array->last_var + op_array->T;
7965
0
          for (i = 0; i < vars_count; i++, v++) {
7966
0
            if (tssa->vars[v].use_chain >= 0 || tssa->vars[v].phi_use_chain) {
7967
0
              fprintf(stderr, "    %*c;", level, ' ');
7968
0
              zend_dump_ssa_var(op_array, tssa, v, 0, i, ZEND_DUMP_RC_INFERENCE);
7969
0
              fprintf(stderr, "\n");
7970
0
            }
7971
0
          }
7972
0
        } else {
7973
0
          call_level--;
7974
0
        }
7975
0
      }
7976
0
    } else if (p->op == ZEND_JIT_TRACE_INIT_CALL) {
7977
0
      if (p->func != (zend_function*)&zend_pass_function) {
7978
0
        fprintf(stderr, (p->info & ZEND_JIT_TRACE_FAKE_INIT_CALL) ? "    %*c>fake_init %s%s%s\n" : "    %*c>init %s%s%s\n",
7979
0
          level, ' ',
7980
0
          (p->func && p->func->common.scope) ? ZSTR_VAL(p->func->common.scope->name) : "",
7981
0
          (p->func && p->func->common.scope) ? "::" : "",
7982
0
          (p->func  && p->func->common.function_name) ? ZSTR_VAL(p->func->common.function_name) : "???");
7983
0
      } else {
7984
0
        fprintf(stderr, "    %*c>skip\n",
7985
0
          level, ' ');
7986
0
      }
7987
0
    } else if (p->op == ZEND_JIT_TRACE_DO_ICALL) {
7988
0
      if (p->func != (zend_function*)&zend_pass_function) {
7989
0
        fprintf(stderr, "    %*c>call %s%s%s\n",
7990
0
          level, ' ',
7991
0
          (p->func && p->func->common.scope) ? ZSTR_VAL(p->func->common.scope->name) : "",
7992
0
          (p->func && p->func->common.scope) ? "::" : "",
7993
0
          (p->func && p->func->common.function_name) ? ZSTR_VAL(p->func->common.function_name) : "???");
7994
0
      } else {
7995
0
        fprintf(stderr, "    %*c>skip\n",
7996
0
          level, ' ');
7997
0
      }
7998
0
    } else if (p->op == ZEND_JIT_TRACE_END) {
7999
0
      break;
8000
0
    }
8001
0
    p++;
8002
0
  }
8003
0
}
8004
8005
static void zend_jit_dump_ref_snapshot(zend_jit_ref_snapshot *rs)
8006
0
{
8007
0
  if (rs->reg == ZREG_NONE) {
8008
0
    fprintf(stderr, "?");
8009
0
  } else if (!IR_REG_SPILLED(rs->reg)) {
8010
0
    fprintf(stderr, "%s", zend_reg_name(rs->reg));
8011
0
  } else {
8012
0
    fprintf(stderr, "0x%x(%s)", rs->offset, zend_reg_name(IR_REG_NUM(rs->reg)));
8013
0
  }
8014
0
}
8015
8016
static void zend_jit_dump_exit_info(zend_jit_trace_info *t)
8017
0
{
8018
0
  int i, j;
8019
8020
0
  fprintf(stderr, "---- TRACE %d exit info\n", t->id);
8021
0
  for (i = 0; i < t->exit_count; i++) {
8022
0
    const zend_op_array *op_array = t->exit_info[i].op_array;
8023
0
    uint32_t stack_size = t->exit_info[i].stack_size;
8024
0
    zend_jit_trace_stack *stack = t->exit_info[i].stack_size ? t->stack_map + t->exit_info[i].stack_offset : NULL;
8025
8026
0
    fprintf(stderr, "     exit_%d:", i);
8027
0
    if (t->exit_info[i].opline) {
8028
0
      fprintf(stderr, " %04d/", (int)(t->exit_info[i].opline - op_array->opcodes));
8029
0
    } else {
8030
0
      fprintf(stderr, " ----/");
8031
0
    }
8032
0
    if (t->exit_info[i].stack_size) {
8033
0
      fprintf(stderr, "%04d/%d", t->exit_info[i].stack_offset, t->exit_info[i].stack_size);
8034
0
    } else {
8035
0
      fprintf(stderr, "----/0");
8036
0
    }
8037
0
    if (t->exit_info[i].flags & ZEND_JIT_EXIT_TO_VM) {
8038
0
      fprintf(stderr, "/VM");
8039
0
    }
8040
0
    if (t->exit_info[i].flags & ZEND_JIT_EXIT_RESTORE_CALL) {
8041
0
      fprintf(stderr, "/CALL");
8042
0
    }
8043
0
    if (t->exit_info[i].flags & (ZEND_JIT_EXIT_POLYMORPHISM|ZEND_JIT_EXIT_METHOD_CALL|ZEND_JIT_EXIT_CLOSURE_CALL)) {
8044
0
      fprintf(stderr, "/POLY");
8045
0
      if (t->exit_info[i].flags & ZEND_JIT_EXIT_METHOD_CALL) {
8046
0
        fprintf(stderr, "(");
8047
0
        zend_jit_dump_ref_snapshot(&t->exit_info[i].poly_func);
8048
0
        fprintf(stderr, ", ");
8049
0
        zend_jit_dump_ref_snapshot(&t->exit_info[i].poly_this);
8050
0
        fprintf(stderr, ")");
8051
0
      }
8052
0
    }
8053
0
    if (t->exit_info[i].flags & ZEND_JIT_EXIT_FREE_OP1) {
8054
0
      fprintf(stderr, "/FREE_OP1");
8055
0
    }
8056
0
    if (t->exit_info[i].flags & ZEND_JIT_EXIT_FREE_OP2) {
8057
0
      fprintf(stderr, "/FREE_OP2");
8058
0
    }
8059
0
    if (t->exit_info[i].flags & ZEND_JIT_EXIT_CHECK_EXCEPTION) {
8060
0
      fprintf(stderr, "/CHK_EXC");
8061
0
    }
8062
0
    for (j = 0; j < stack_size; j++) {
8063
0
      uint8_t type = STACK_TYPE(stack, j);
8064
0
      if (type != IS_UNKNOWN) {
8065
0
        fprintf(stderr, " ");
8066
0
        zend_dump_var(op_array, (j < op_array->last_var) ? IS_CV : 0, j);
8067
0
        fprintf(stderr, ":");
8068
0
        if (type == IS_UNDEF) {
8069
0
          fprintf(stderr, "undef");
8070
0
        } else {
8071
0
          fprintf(stderr, "%s", zend_get_type_by_const(type));
8072
0
        }
8073
0
        if (STACK_FLAGS(stack, j) == ZREG_CONST) {
8074
0
          if (type == IS_LONG) {
8075
0
            fprintf(stderr, "(" ZEND_LONG_FMT ")", (zend_long)t->constants[STACK_REF(stack, j)].i);
8076
0
          } else if (type == IS_DOUBLE) {
8077
0
            fprintf(stderr, "(%g)", t->constants[STACK_REF(stack, j)].d);
8078
0
          } else {
8079
0
            ZEND_UNREACHABLE();
8080
0
          }
8081
0
        } else if (STACK_FLAGS(stack, j) == ZREG_TYPE_ONLY) {
8082
0
          fprintf(stderr, "(type_only)");
8083
0
        } else if (STACK_FLAGS(stack, j) == ZREG_THIS) {
8084
0
          fprintf(stderr, "(this)");
8085
0
        } else if (STACK_FLAGS(stack, j) == ZREG_ZVAL_ADDREF) {
8086
0
          fprintf(stderr, "(zval_try_addref)");
8087
0
        } else if (STACK_FLAGS(stack, j) == ZREG_ZVAL_COPY) {
8088
0
          fprintf(stderr, "zval_copy(%s)", zend_reg_name(STACK_REG(stack, j)));
8089
0
        } else if (STACK_FLAGS(stack, j) & ZREG_SPILL_SLOT) {
8090
0
          if (STACK_REG(stack, j) == ZREG_NONE) {
8091
0
            fprintf(stderr, "(spill=0x%x", STACK_REF(stack, j));
8092
0
          } else {
8093
0
            fprintf(stderr, "(spill=0x%x(%s)", STACK_REF(stack, j), zend_reg_name(STACK_REG(stack, j)));
8094
0
          }
8095
0
          if (STACK_FLAGS(stack, j) != 0) {
8096
0
            fprintf(stderr, ":%x", STACK_FLAGS(stack, j));
8097
0
          }
8098
0
          fprintf(stderr, ")");
8099
0
        } else if (STACK_REG(stack, j) != ZREG_NONE) {
8100
0
          fprintf(stderr, "(%s", zend_reg_name(STACK_REG(stack, j)));
8101
0
          if (STACK_FLAGS(stack, j) != 0) {
8102
0
            fprintf(stderr, ":%x", STACK_FLAGS(stack, j));
8103
0
          }
8104
0
          fprintf(stderr, ")");
8105
0
        }
8106
0
      } else if (STACK_FLAGS(stack, j) == ZREG_ZVAL_ADDREF) {
8107
0
        fprintf(stderr, ":unknown(zval_try_addref)");
8108
0
      } else if (STACK_FLAGS(stack, j) == ZREG_ZVAL_COPY) {
8109
0
        fprintf(stderr, " ");
8110
0
        zend_dump_var(op_array, (j < op_array->last_var) ? IS_CV : 0, j);
8111
0
        fprintf(stderr, ":unknown(zval_copy(%s))", zend_reg_name(STACK_REG(stack, j)));
8112
0
      }
8113
0
    }
8114
0
#if ZEND_DEBUG
8115
0
    if ((JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_EXIT_INFO_SRC) != 0) {
8116
0
      fprintf(stderr, " %s:%d", t->exit_info[i].filename, t->exit_info[i].lineno);
8117
0
    }
8118
0
#endif
8119
0
    fprintf(stderr, "\n");
8120
0
  }
8121
0
}
8122
8123
int ZEND_FASTCALL zend_jit_trace_hot_root(zend_execute_data *execute_data, const zend_op *opline)
8124
0
{
8125
0
  const zend_op *orig_opline;
8126
0
  zend_jit_trace_stop stop;
8127
0
  int ret = 0;
8128
0
  zend_op_array *op_array;
8129
0
  zend_jit_op_array_trace_extension *jit_extension;
8130
0
  size_t offset;
8131
0
  uint32_t trace_num;
8132
0
  zend_jit_trace_rec trace_buffer[ZEND_JIT_TRACE_MAX_LENGTH];
8133
8134
0
  ZEND_ASSERT(EX(func)->type == ZEND_USER_FUNCTION);
8135
0
  ZEND_ASSERT(opline >= EX(func)->op_array.opcodes &&
8136
0
    opline < EX(func)->op_array.opcodes + EX(func)->op_array.last);
8137
8138
0
repeat:
8139
0
  trace_num = ZEND_JIT_TRACE_NUM;
8140
0
  orig_opline = opline;
8141
0
  op_array = &EX(func)->op_array;
8142
0
  jit_extension = (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8143
0
  offset = jit_extension->offset;
8144
8145
0
  EX(opline) = opline;
8146
8147
  /* Lock-free check if the root trace was already JIT-ed or blacklist-ed in another process */
8148
0
  if (ZEND_OP_TRACE_INFO(opline, offset)->trace_flags & (ZEND_JIT_TRACE_JITED|ZEND_JIT_TRACE_BLACKLISTED)) {
8149
0
    return 0;
8150
0
  }
8151
8152
0
  if (JIT_G(tracing)) {
8153
0
    ++(*ZEND_OP_TRACE_INFO(opline, offset)->counter);
8154
0
    return 0;
8155
0
  }
8156
8157
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_START) {
8158
0
    fprintf(stderr, "---- TRACE %d start (%s) %s%s%s() %s:%d\n",
8159
0
      trace_num,
8160
0
      zend_jit_trace_star_desc(ZEND_OP_TRACE_INFO(opline, offset)->trace_flags),
8161
0
      EX(func)->op_array.scope ? ZSTR_VAL(EX(func)->op_array.scope->name) : "",
8162
0
      EX(func)->op_array.scope ? "::" : "",
8163
0
      EX(func)->op_array.function_name ?
8164
0
        ZSTR_VAL(EX(func)->op_array.function_name) : "$main",
8165
0
      ZSTR_VAL(EX(func)->op_array.filename),
8166
0
      opline->lineno);
8167
0
  }
8168
8169
0
  if (ZEND_JIT_TRACE_NUM >= JIT_G(max_root_traces)) {
8170
0
    stop = ZEND_JIT_TRACE_STOP_TOO_MANY_TRACES;
8171
0
    zend_jit_stop_counter_handlers();
8172
0
    goto abort;
8173
0
  }
8174
8175
0
  JIT_G(tracing) = 1;
8176
0
  stop = zend_jit_trace_execute(execute_data, opline, trace_buffer,
8177
0
    ZEND_OP_TRACE_INFO(opline, offset)->trace_flags & ZEND_JIT_TRACE_START_MASK, 0, 0);
8178
0
  JIT_G(tracing) = 0;
8179
8180
0
  if (stop & ZEND_JIT_TRACE_HALT) {
8181
0
    ret = -1;
8182
0
  }
8183
0
  stop &= ~ZEND_JIT_TRACE_HALT;
8184
8185
0
  if (UNEXPECTED(trace_buffer[1].opline != orig_opline)) {
8186
0
    orig_opline = trace_buffer[1].opline;
8187
0
    op_array = (zend_op_array*)trace_buffer[0].op_array;
8188
0
    jit_extension = (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8189
0
    offset = jit_extension->offset;
8190
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_START) {
8191
0
      const zend_op_array *op_array = trace_buffer[0].op_array;
8192
0
      const zend_op *opline = trace_buffer[1].opline;
8193
0
      zend_jit_op_array_trace_extension *jit_extension =
8194
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8195
0
      size_t offset = jit_extension->offset;
8196
8197
0
      fprintf(stderr, "---- TRACE %d start (%s) %s%s%s() %s:%d\n",
8198
0
        trace_num,
8199
0
        zend_jit_trace_star_desc(ZEND_OP_TRACE_INFO(opline, offset)->trace_flags),
8200
0
        op_array->scope ? ZSTR_VAL(op_array->scope->name) : "",
8201
0
        op_array->scope ? "::" : "",
8202
0
        op_array->function_name ?
8203
0
          ZSTR_VAL(op_array->function_name) : "$main",
8204
0
        ZSTR_VAL(op_array->filename),
8205
0
        opline->lineno);
8206
0
    }
8207
0
  }
8208
8209
0
  if (UNEXPECTED(JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BYTECODE)) {
8210
0
    zend_jit_dump_trace(trace_buffer, NULL);
8211
0
  }
8212
8213
0
  if (ZEND_JIT_TRACE_STOP_OK(stop)) {
8214
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_STOP) {
8215
0
      if (stop == ZEND_JIT_TRACE_STOP_LINK) {
8216
0
        uint32_t idx = trace_buffer[1].last;
8217
0
        uint32_t link_to = zend_jit_find_trace(trace_buffer[idx].opline->handler);
8218
0
        fprintf(stderr, "---- TRACE %d stop (link to %d)\n",
8219
0
          trace_num,
8220
0
          link_to);
8221
0
      } else {
8222
0
        fprintf(stderr, "---- TRACE %d stop (%s)\n",
8223
0
          trace_num,
8224
0
          zend_jit_trace_stop_description[stop]);
8225
0
      }
8226
0
    }
8227
0
    stop = zend_jit_compile_root_trace(trace_buffer, orig_opline, offset);
8228
0
    if (EXPECTED(ZEND_JIT_TRACE_STOP_DONE(stop))) {
8229
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_COMPILED) {
8230
0
        fprintf(stderr, "---- TRACE %d %s\n",
8231
0
          trace_num,
8232
0
          zend_jit_trace_stop_description[stop]);
8233
0
      }
8234
0
    } else {
8235
0
      goto abort;
8236
0
    }
8237
0
  } else {
8238
0
abort:
8239
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_ABORT) {
8240
0
      fprintf(stderr, "---- TRACE %d abort (%s)\n",
8241
0
        trace_num,
8242
0
        zend_jit_trace_stop_description[stop]);
8243
0
    }
8244
0
    if (!ZEND_JIT_TRACE_STOP_MAY_RECOVER(stop)
8245
0
     || zend_jit_trace_is_bad_root(orig_opline, stop, offset)) {
8246
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BLACKLIST) {
8247
0
        fprintf(stderr, "---- TRACE %d blacklisted\n",
8248
0
          trace_num);
8249
0
      }
8250
0
      zend_jit_blacklist_root_trace(orig_opline, offset);
8251
0
    }
8252
0
    if (ZEND_JIT_TRACE_STOP_REPEAT(stop)) {
8253
0
      execute_data = EG(current_execute_data);
8254
0
      opline = EX(opline);
8255
0
      goto repeat;
8256
0
    }
8257
0
  }
8258
8259
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_TRACE_STOP|ZEND_JIT_DEBUG_TRACE_ABORT|ZEND_JIT_DEBUG_TRACE_COMPILED|ZEND_JIT_DEBUG_TRACE_BLACKLIST)) {
8260
0
    fprintf(stderr, "\n");
8261
0
  }
8262
8263
0
  return ret;
8264
0
}
8265
8266
static void zend_jit_blacklist_trace_exit(uint32_t trace_num, uint32_t exit_num)
8267
0
{
8268
0
  const void *handler;
8269
0
  bool do_bailout = 0;
8270
8271
0
  zend_shared_alloc_lock();
8272
8273
0
  if (!(zend_jit_traces[trace_num].exit_info[exit_num].flags & (ZEND_JIT_EXIT_JITED|ZEND_JIT_EXIT_BLACKLISTED))) {
8274
0
    SHM_UNPROTECT();
8275
0
    zend_jit_unprotect();
8276
8277
0
    zend_try {
8278
0
      handler = zend_jit_trace_exit_to_vm(trace_num, exit_num);
8279
8280
0
      if (handler) {
8281
0
        zend_jit_link_side_trace(
8282
0
          zend_jit_traces[trace_num].code_start,
8283
0
          zend_jit_traces[trace_num].code_size,
8284
0
          zend_jit_traces[trace_num].jmp_table_size,
8285
0
          exit_num,
8286
0
          handler);
8287
0
      }
8288
0
      zend_jit_traces[trace_num].exit_info[exit_num].flags |= ZEND_JIT_EXIT_BLACKLISTED;
8289
0
    } zend_catch {
8290
0
      do_bailout = 1;
8291
0
    } zend_end_try();
8292
8293
0
    zend_jit_protect();
8294
0
    SHM_PROTECT();
8295
0
  }
8296
8297
0
  zend_shared_alloc_unlock();
8298
8299
0
  if (do_bailout) {
8300
0
    zend_bailout();
8301
0
  }
8302
0
}
8303
8304
static bool zend_jit_trace_exit_is_bad(uint32_t trace_num, uint32_t exit_num)
8305
0
{
8306
0
  uint8_t *counter = JIT_G(exit_counters) +
8307
0
    zend_jit_traces[trace_num].exit_counters + exit_num;
8308
8309
0
  if (*counter + 1 >= JIT_G(hot_side_exit) + JIT_G(blacklist_side_trace)) {
8310
0
    return true;
8311
0
  }
8312
0
  (*counter)++;
8313
0
  return false;
8314
0
}
8315
8316
static bool zend_jit_trace_exit_is_hot(uint32_t trace_num, uint32_t exit_num)
8317
0
{
8318
0
  uint8_t *counter = JIT_G(exit_counters) +
8319
0
    zend_jit_traces[trace_num].exit_counters + exit_num;
8320
8321
0
  if (*counter + 1 >= JIT_G(hot_side_exit)) {
8322
0
    return true;
8323
0
  }
8324
0
  (*counter)++;
8325
0
  return false;
8326
0
}
8327
8328
static zend_jit_trace_stop zend_jit_compile_side_trace(zend_jit_trace_rec *trace_buffer, uint32_t parent_num, uint32_t exit_num, uint32_t polymorphism)
8329
0
{
8330
0
  zend_jit_trace_stop ret;
8331
0
  const void *handler;
8332
0
  uint8_t orig_trigger;
8333
0
  zend_jit_trace_info *t;
8334
0
  zend_jit_trace_exit_info exit_info[ZEND_JIT_TRACE_MAX_EXITS];
8335
0
  bool do_bailout = 0;
8336
8337
0
  zend_shared_alloc_lock();
8338
8339
  /* Checks under lock */
8340
0
  if (zend_jit_traces[parent_num].exit_info[exit_num].flags & (ZEND_JIT_EXIT_JITED|ZEND_JIT_EXIT_BLACKLISTED)) {
8341
0
    ret = ZEND_JIT_TRACE_STOP_ALREADY_DONE;
8342
0
  } else if (ZEND_JIT_TRACE_NUM >= JIT_G(max_root_traces)) {
8343
0
    ret = ZEND_JIT_TRACE_STOP_TOO_MANY_TRACES;
8344
0
  } else if (zend_jit_traces[zend_jit_traces[parent_num].root].child_count >= JIT_G(max_side_traces)) {
8345
0
    ret = ZEND_JIT_TRACE_STOP_TOO_MANY_CHILDREN;
8346
0
  } else {
8347
0
    SHM_UNPROTECT();
8348
0
    zend_jit_unprotect();
8349
8350
0
    zend_try {
8351
0
      t = &zend_jit_traces[ZEND_JIT_TRACE_NUM];
8352
8353
0
      t->id = ZEND_JIT_TRACE_NUM;
8354
0
      t->root = zend_jit_traces[parent_num].root;
8355
0
      t->parent = parent_num;
8356
0
      t->link = 0;
8357
0
      t->exit_count = 0;
8358
0
      t->child_count = 0;
8359
0
      t->stack_map_size = 0;
8360
0
      t->flags = 0;
8361
0
      t->polymorphism = polymorphism;
8362
0
      t->jmp_table_size = 0;
8363
0
      t->opline = NULL;
8364
0
      t->exit_info = exit_info;
8365
0
      t->stack_map = NULL;
8366
0
      t->consts_count = 0;
8367
0
      t->constants = NULL;
8368
8369
0
      orig_trigger = JIT_G(trigger);
8370
0
      JIT_G(trigger) = ZEND_JIT_ON_HOT_TRACE;
8371
8372
0
      handler = zend_jit_trace(trace_buffer, parent_num, exit_num);
8373
8374
0
      JIT_G(trigger) = orig_trigger;
8375
8376
0
      if (handler) {
8377
0
        zend_jit_trace_exit_info *shared_exit_info = NULL;
8378
8379
0
        t->exit_info = NULL;
8380
0
        if (t->exit_count) {
8381
          /* reallocate exit_info into shared memory */
8382
0
          shared_exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc(
8383
0
            sizeof(zend_jit_trace_exit_info) * t->exit_count);
8384
8385
0
          if (!shared_exit_info) {
8386
0
            if (t->stack_map) {
8387
0
              efree(t->stack_map);
8388
0
              t->stack_map = NULL;
8389
0
            }
8390
0
            if (t->constants) {
8391
0
              efree(t->constants);
8392
0
              t->constants = NULL;
8393
0
            }
8394
0
            ret = ZEND_JIT_TRACE_STOP_NO_SHM;
8395
0
            goto exit;
8396
0
          }
8397
0
          memcpy(shared_exit_info, exit_info,
8398
0
            sizeof(zend_jit_trace_exit_info) * t->exit_count);
8399
0
          t->exit_info = shared_exit_info;
8400
0
        }
8401
8402
0
        if (t->stack_map_size) {
8403
0
          zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc(t->stack_map_size * sizeof(zend_jit_trace_stack));
8404
0
          if (!shared_stack_map) {
8405
0
            efree(t->stack_map);
8406
0
            t->stack_map = NULL;
8407
0
            if (t->constants) {
8408
0
              efree(t->constants);
8409
0
              t->constants = NULL;
8410
0
            }
8411
0
            ret = ZEND_JIT_TRACE_STOP_NO_SHM;
8412
0
            goto exit;
8413
0
          }
8414
0
          memcpy(shared_stack_map, t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
8415
0
          efree(t->stack_map);
8416
0
          t->stack_map = shared_stack_map;
8417
0
          }
8418
8419
0
        if (t->consts_count) {
8420
0
          zend_jit_exit_const *constants = (zend_jit_exit_const*)zend_shared_alloc(t->consts_count * sizeof(zend_jit_exit_const));
8421
0
          if (!constants) {
8422
0
            efree(t->constants);
8423
0
            ret = ZEND_JIT_TRACE_STOP_NO_SHM;
8424
0
            goto exit;
8425
0
          }
8426
0
          memcpy(constants, t->constants, t->consts_count * sizeof(zend_jit_exit_const));
8427
0
          efree(t->constants);
8428
0
          t->constants = constants;
8429
0
        }
8430
8431
0
        zend_jit_link_side_trace(
8432
0
          zend_jit_traces[parent_num].code_start,
8433
0
          zend_jit_traces[parent_num].code_size,
8434
0
          zend_jit_traces[parent_num].jmp_table_size,
8435
0
          exit_num,
8436
0
          handler);
8437
8438
0
        t->exit_counters = ZEND_JIT_EXIT_COUNTERS;
8439
0
        ZEND_JIT_EXIT_COUNTERS += t->exit_count;
8440
8441
0
        zend_jit_traces[zend_jit_traces[parent_num].root].child_count++;
8442
0
        ZEND_JIT_TRACE_NUM++;
8443
0
        zend_jit_traces[parent_num].exit_info[exit_num].flags |= ZEND_JIT_EXIT_JITED;
8444
8445
0
        ret = ZEND_JIT_TRACE_STOP_COMPILED;
8446
0
      } else if (t->exit_count >= ZEND_JIT_TRACE_MAX_EXITS ||
8447
0
                 ZEND_JIT_EXIT_COUNTERS + t->exit_count >= JIT_G(max_exit_counters)) {
8448
0
          if (t->stack_map) {
8449
0
          efree(t->stack_map);
8450
0
          t->stack_map = NULL;
8451
0
        }
8452
0
        if (t->constants) {
8453
0
          efree(t->constants);
8454
0
          t->constants = NULL;
8455
0
        }
8456
0
        ret = ZEND_JIT_TRACE_STOP_TOO_MANY_EXITS;
8457
0
      } else {
8458
0
        if (t->stack_map) {
8459
0
          efree(t->stack_map);
8460
0
          t->stack_map = NULL;
8461
0
        }
8462
0
        if (t->constants) {
8463
0
          efree(t->constants);
8464
0
          t->constants = NULL;
8465
0
        }
8466
0
        ret = ZEND_JIT_TRACE_STOP_COMPILER_ERROR;
8467
0
      }
8468
8469
0
exit:;
8470
0
    } zend_catch {
8471
0
      do_bailout = 1;
8472
0
    }  zend_end_try();
8473
8474
0
    zend_jit_protect();
8475
0
    SHM_PROTECT();
8476
0
  }
8477
8478
0
  zend_shared_alloc_unlock();
8479
8480
0
  if (do_bailout) {
8481
0
    zend_bailout();
8482
0
  }
8483
8484
0
  if ((JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_EXIT_INFO) != 0
8485
0
   && ret == ZEND_JIT_TRACE_STOP_COMPILED
8486
0
   && t->exit_count > 0) {
8487
0
    zend_jit_dump_exit_info(t);
8488
0
  }
8489
8490
0
  return ret;
8491
0
}
8492
8493
int ZEND_FASTCALL zend_jit_trace_hot_side(zend_execute_data *execute_data, uint32_t parent_num, uint32_t exit_num)
8494
0
{
8495
0
  zend_jit_trace_stop stop;
8496
0
  int ret = 0;
8497
0
  uint32_t trace_num;
8498
0
  zend_jit_trace_rec trace_buffer[ZEND_JIT_TRACE_MAX_LENGTH];
8499
0
  uint32_t is_megamorphic = 0;
8500
0
  uint32_t polymorphism = 0;
8501
0
  uint32_t root;
8502
0
  int ret_depth = 0;
8503
8504
0
  trace_num = ZEND_JIT_TRACE_NUM;
8505
8506
  /* Lock-free check if the side trace was already JIT-ed or blacklist-ed in another process */
8507
0
  if (zend_jit_traces[parent_num].exit_info[exit_num].flags & (ZEND_JIT_EXIT_JITED|ZEND_JIT_EXIT_BLACKLISTED)) {
8508
0
    return 0;
8509
0
  }
8510
8511
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_START) {
8512
0
    fprintf(stderr, "---- TRACE %d start (side trace %d/%d) %s%s%s() %s:%d\n",
8513
0
      trace_num, parent_num, exit_num,
8514
0
      EX(func)->op_array.scope ? ZSTR_VAL(EX(func)->op_array.scope->name) : "",
8515
0
      EX(func)->op_array.scope ? "::" : "",
8516
0
      EX(func)->op_array.function_name ?
8517
0
        ZSTR_VAL(EX(func)->op_array.function_name) : "$main",
8518
0
      ZSTR_VAL(EX(func)->op_array.filename),
8519
0
      EX(opline)->lineno);
8520
0
  }
8521
8522
0
  if (ZEND_JIT_TRACE_NUM >= JIT_G(max_root_traces)) {
8523
0
    stop = ZEND_JIT_TRACE_STOP_TOO_MANY_TRACES;
8524
0
    goto abort;
8525
0
  }
8526
8527
0
  root = zend_jit_traces[parent_num].root;
8528
0
  if (zend_jit_traces[root].child_count >= JIT_G(max_side_traces)) {
8529
0
    stop = ZEND_JIT_TRACE_STOP_TOO_MANY_CHILDREN;
8530
0
    goto abort;
8531
0
  }
8532
8533
0
  if (JIT_G(max_polymorphic_calls) > 0) {
8534
0
    if ((zend_jit_traces[parent_num].exit_info[exit_num].flags & (ZEND_JIT_EXIT_METHOD_CALL|ZEND_JIT_EXIT_CLOSURE_CALL))
8535
0
     || ((zend_jit_traces[parent_num].exit_info[exit_num].flags & ZEND_JIT_EXIT_POLYMORPHISM)
8536
0
      && EX(call))) {
8537
0
      if (zend_jit_traces[parent_num].polymorphism >= JIT_G(max_polymorphic_calls) - 1) {
8538
0
        is_megamorphic = zend_jit_traces[parent_num].exit_info[exit_num].flags &
8539
0
          (ZEND_JIT_EXIT_METHOD_CALL | ZEND_JIT_EXIT_CLOSURE_CALL | ZEND_JIT_EXIT_POLYMORPHISM);
8540
0
      } else if (!zend_jit_traces[parent_num].polymorphism) {
8541
0
        polymorphism = 1;
8542
0
      } else if (exit_num == 0) {
8543
0
        polymorphism = zend_jit_traces[parent_num].polymorphism + 1;
8544
0
      }
8545
0
    }
8546
0
  }
8547
8548
  /* Check if this is a side trace of a root LOOP trace */
8549
0
  if ((zend_jit_traces[root].flags & ZEND_JIT_TRACE_LOOP)
8550
0
   && zend_jit_traces[root].op_array != &EX(func)->op_array) {
8551
0
    const zend_op_array *op_array = zend_jit_traces[root].op_array;
8552
0
    const zend_op *opline = zend_jit_traces[root].opline;
8553
0
    zend_jit_op_array_trace_extension *jit_extension =
8554
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8555
8556
0
    if (jit_extension->trace_info[opline - op_array->opcodes].trace_flags & ZEND_JIT_TRACE_START_LOOP) {
8557
0
      zend_execute_data *ex = execute_data;
8558
0
      int n = 0;
8559
0
      do {
8560
0
        ex = ex->prev_execute_data;
8561
0
        n++;
8562
0
      } while (ex && zend_jit_traces[root].op_array != &ex->func->op_array);
8563
0
      if (ex && n <= ZEND_JIT_TRACE_MAX_RET_DEPTH) {
8564
0
        ret_depth = n;
8565
0
      }
8566
0
    }
8567
0
  }
8568
8569
0
  JIT_G(tracing) = 1;
8570
0
  stop = zend_jit_trace_execute(execute_data, EX(opline), trace_buffer, ZEND_JIT_TRACE_START_SIDE, is_megamorphic, ret_depth);
8571
0
  JIT_G(tracing) = 0;
8572
8573
0
  if (stop & ZEND_JIT_TRACE_HALT) {
8574
0
    ret = -1;
8575
0
  }
8576
0
  stop &= ~ZEND_JIT_TRACE_HALT;
8577
8578
0
  if (UNEXPECTED(trace_buffer->start != ZEND_JIT_TRACE_START_SIDE)) {
8579
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_START) {
8580
0
      const zend_op_array *op_array = trace_buffer[0].op_array;
8581
0
      const zend_op *opline = trace_buffer[1].opline;
8582
0
      zend_jit_op_array_trace_extension *jit_extension =
8583
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8584
0
      size_t offset = jit_extension->offset;
8585
8586
0
      fprintf(stderr, "---- TRACE %d start (%s) %s%s%s() %s:%d\n",
8587
0
        trace_num,
8588
0
        zend_jit_trace_star_desc(ZEND_OP_TRACE_INFO(opline, offset)->trace_flags),
8589
0
        op_array->scope ? ZSTR_VAL(op_array->scope->name) : "",
8590
0
        op_array->scope ? "::" : "",
8591
0
        op_array->function_name ?
8592
0
          ZSTR_VAL(op_array->function_name) : "$main",
8593
0
        ZSTR_VAL(op_array->filename),
8594
0
        opline->lineno);
8595
0
    }
8596
0
  }
8597
8598
0
  if (UNEXPECTED(JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BYTECODE)) {
8599
0
    zend_jit_dump_trace(trace_buffer, NULL);
8600
0
  }
8601
8602
0
  if (ZEND_JIT_TRACE_STOP_OK(stop)) {
8603
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_STOP) {
8604
0
      if (stop == ZEND_JIT_TRACE_STOP_LINK) {
8605
0
        uint32_t idx = trace_buffer[1].last;
8606
0
        uint32_t link_to = zend_jit_find_trace(trace_buffer[idx].opline->handler);;
8607
0
        fprintf(stderr, "---- TRACE %d stop (link to %d)\n",
8608
0
          trace_num,
8609
0
          link_to);
8610
0
      } else {
8611
0
        fprintf(stderr, "---- TRACE %d stop (%s)\n",
8612
0
          trace_num,
8613
0
          zend_jit_trace_stop_description[stop]);
8614
0
      }
8615
0
    }
8616
0
    if (EXPECTED(trace_buffer->start == ZEND_JIT_TRACE_START_SIDE)) {
8617
0
      stop = zend_jit_compile_side_trace(trace_buffer, parent_num, exit_num, polymorphism);
8618
0
    } else {
8619
0
      const zend_op_array *op_array = trace_buffer[0].op_array;
8620
0
      zend_jit_op_array_trace_extension *jit_extension =
8621
0
        (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8622
0
      const zend_op *opline = trace_buffer[1].opline;
8623
8624
0
      stop = zend_jit_compile_root_trace(trace_buffer, opline, jit_extension->offset);
8625
0
    }
8626
0
    if (EXPECTED(ZEND_JIT_TRACE_STOP_DONE(stop))) {
8627
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_COMPILED) {
8628
0
        fprintf(stderr, "---- TRACE %d %s\n",
8629
0
          trace_num,
8630
0
          zend_jit_trace_stop_description[stop]);
8631
0
      }
8632
0
    } else {
8633
0
      goto abort;
8634
0
    }
8635
0
  } else {
8636
0
abort:
8637
0
    if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_ABORT) {
8638
0
      fprintf(stderr, "---- TRACE %d abort (%s)\n",
8639
0
        trace_num,
8640
0
        zend_jit_trace_stop_description[stop]);
8641
0
    }
8642
0
    if (!ZEND_JIT_TRACE_STOP_MAY_RECOVER(stop)
8643
0
     || zend_jit_trace_exit_is_bad(parent_num, exit_num)) {
8644
0
      zend_jit_blacklist_trace_exit(parent_num, exit_num);
8645
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BLACKLIST) {
8646
0
        fprintf(stderr, "---- EXIT %d/%d blacklisted\n",
8647
0
          parent_num, exit_num);
8648
0
      }
8649
0
    }
8650
0
    if (ZEND_JIT_TRACE_STOP_REPEAT(stop)) {
8651
0
      execute_data = EG(current_execute_data);
8652
0
      return zend_jit_trace_hot_root(execute_data, EX(opline));
8653
0
    }
8654
0
  }
8655
8656
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_TRACE_STOP|ZEND_JIT_DEBUG_TRACE_ABORT|ZEND_JIT_DEBUG_TRACE_COMPILED|ZEND_JIT_DEBUG_TRACE_BLACKLIST)) {
8657
0
    fprintf(stderr, "\n");
8658
0
  }
8659
8660
0
  return ret;
8661
0
}
8662
8663
int ZEND_FASTCALL zend_jit_trace_exit(uint32_t exit_num, zend_jit_registers_buf *regs)
8664
0
{
8665
0
  uint32_t trace_num = EG(jit_trace_num);
8666
0
  zend_execute_data *execute_data = EG(current_execute_data);
8667
0
  const zend_op *orig_opline = EX(opline);
8668
0
  const zend_op *opline;
8669
0
  zend_jit_trace_info *t = &zend_jit_traces[trace_num];
8670
0
  int repeat_last_opline = 0;
8671
8672
  /* Deoptimization of VM stack state */
8673
0
  uint32_t i;
8674
0
  uint32_t stack_size = t->exit_info[exit_num].stack_size;
8675
0
  zend_jit_trace_stack *stack = stack_size ? t->stack_map + t->exit_info[exit_num].stack_offset : NULL;
8676
8677
0
  if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_RESTORE_CALL) {
8678
0
    zend_execute_data *call = (zend_execute_data *)regs->gpr[ZREG_RX];
8679
0
    call->prev_execute_data = EX(call);
8680
0
    EX(call) = call;
8681
0
  }
8682
8683
0
  for (i = 0; i < stack_size; i++) {
8684
0
    if (STACK_FLAGS(stack, i) == ZREG_CONST) {
8685
0
      if (STACK_TYPE(stack, i) == IS_LONG) {
8686
0
        ZVAL_LONG(EX_VAR_NUM(i), (zend_long)t->constants[STACK_REF(stack, i)].i);
8687
0
      } else if (STACK_TYPE(stack, i) == IS_DOUBLE) {
8688
0
        ZVAL_DOUBLE(EX_VAR_NUM(i), t->constants[STACK_REF(stack, i)].d);
8689
0
      } else {
8690
0
        ZEND_UNREACHABLE();
8691
0
      }
8692
0
    } else if (STACK_FLAGS(stack, i) == ZREG_TYPE_ONLY) {
8693
0
      uint32_t type = STACK_TYPE(stack, i);
8694
0
      if (type <= IS_DOUBLE) {
8695
0
        Z_TYPE_INFO_P(EX_VAR_NUM(i)) = type;
8696
0
      } else {
8697
0
        ZEND_UNREACHABLE();
8698
0
      }
8699
0
    } else if (STACK_FLAGS(stack, i) == ZREG_THIS) {
8700
0
      zend_object *obj = Z_OBJ(EX(This));
8701
8702
0
      GC_ADDREF(obj);
8703
0
      ZVAL_OBJ(EX_VAR_NUM(i), obj);
8704
0
    } else if (STACK_FLAGS(stack, i) == ZREG_ZVAL_ADDREF) {
8705
0
      Z_TRY_ADDREF_P(EX_VAR_NUM(i));
8706
0
    } else if (STACK_FLAGS(stack, i) == ZREG_ZVAL_COPY) {
8707
0
      zval *val = (zval*)regs->gpr[STACK_REG(stack, i)];
8708
8709
0
      if (UNEXPECTED(Z_TYPE_P(val) == IS_UNDEF)) {
8710
        /* Undefined array index or property */
8711
0
        const zend_op *op = t->exit_info[exit_num].opline;
8712
0
        ZEND_ASSERT(op);
8713
0
        op--;
8714
0
        if (op->opcode == ZEND_FETCH_DIM_IS || op->opcode == ZEND_FETCH_OBJ_IS) {
8715
0
          ZVAL_NULL(EX_VAR_NUM(i));
8716
0
        } else {
8717
0
          ZEND_ASSERT(op->opcode == ZEND_FETCH_DIM_R || op->opcode == ZEND_FETCH_LIST_R || op->opcode == ZEND_FETCH_OBJ_R || op->opcode == ZEND_FETCH_DIM_FUNC_ARG || op->opcode == ZEND_FETCH_OBJ_FUNC_ARG);
8718
0
          repeat_last_opline = 1;
8719
0
        }
8720
0
      } else {
8721
0
        ZVAL_COPY(EX_VAR_NUM(i), val);
8722
0
      }
8723
0
    } else if (STACK_FLAGS(stack, i) & ZREG_SPILL_SLOT) {
8724
0
      ZEND_ASSERT(STACK_REG(stack, i) != ZREG_NONE);
8725
0
      uintptr_t ptr = (uintptr_t)regs->gpr[STACK_REG(stack, i)] + STACK_REF(stack, i);
8726
8727
0
      if (STACK_TYPE(stack, i) == IS_LONG) {
8728
0
        ZVAL_LONG(EX_VAR_NUM(i), *(zend_long*)ptr);
8729
0
      } else if (STACK_TYPE(stack, i) == IS_DOUBLE) {
8730
0
        ZVAL_DOUBLE(EX_VAR_NUM(i), *(double*)ptr);
8731
0
      } else {
8732
0
        ZEND_UNREACHABLE();
8733
0
      }
8734
0
    } else if (STACK_REG(stack, i) != ZREG_NONE) {
8735
0
      if (STACK_TYPE(stack, i) == IS_LONG) {
8736
0
        zend_long val = regs->gpr[STACK_REG(stack, i)];
8737
0
        ZVAL_LONG(EX_VAR_NUM(i), val);
8738
0
      } else if (STACK_TYPE(stack, i) == IS_DOUBLE) {
8739
0
        double val = regs->fpr[STACK_REG(stack, i) - ZREG_FIRST_FPR];
8740
0
        ZVAL_DOUBLE(EX_VAR_NUM(i), val);
8741
0
      } else {
8742
0
        ZEND_UNREACHABLE();
8743
0
      }
8744
0
    }
8745
0
  }
8746
8747
0
  if (repeat_last_opline) {
8748
0
    EX(opline) = t->exit_info[exit_num].opline - 1;
8749
0
    if ((EX(opline)->op1_type & (IS_VAR|IS_TMP_VAR))
8750
0
     && !(t->exit_info[exit_num].flags & ZEND_JIT_EXIT_FREE_OP1)
8751
0
     && EX(opline)->opcode != ZEND_FETCH_LIST_R) {
8752
0
      Z_TRY_ADDREF_P(EX_VAR(EX(opline)->op1.var));
8753
0
    }
8754
0
    return 1;
8755
0
  }
8756
8757
0
  opline = t->exit_info[exit_num].opline;
8758
8759
0
  if (opline) {
8760
0
    if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_FREE_OP2) {
8761
0
      ZEND_ASSERT((opline-1)->opcode == ZEND_FETCH_DIM_R
8762
0
          || (opline-1)->opcode == ZEND_FETCH_DIM_IS
8763
0
          || (opline-1)->opcode == ZEND_FETCH_LIST_R
8764
0
          || (opline-1)->opcode == ZEND_FETCH_DIM_FUNC_ARG);
8765
0
      EX(opline) = opline-1;
8766
0
      zval_ptr_dtor_nogc(EX_VAR((opline-1)->op2.var));
8767
0
    }
8768
0
    if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_FREE_OP1) {
8769
0
      ZEND_ASSERT((opline-1)->opcode == ZEND_FETCH_DIM_R
8770
0
          || (opline-1)->opcode == ZEND_FETCH_DIM_IS
8771
0
          || (opline-1)->opcode == ZEND_FETCH_DIM_FUNC_ARG
8772
0
          || (opline-1)->opcode == ZEND_FETCH_OBJ_R
8773
0
          || (opline-1)->opcode == ZEND_FETCH_OBJ_IS
8774
0
          || (opline-1)->opcode == ZEND_FETCH_OBJ_FUNC_ARG);
8775
0
      EX(opline) = opline-1;
8776
0
      zval_ptr_dtor_nogc(EX_VAR((opline-1)->op1.var));
8777
0
    }
8778
0
    if (t->exit_info[exit_num].flags & (ZEND_JIT_EXIT_FREE_OP1|ZEND_JIT_EXIT_FREE_OP2|ZEND_JIT_EXIT_CHECK_EXCEPTION)) {
8779
0
      if (EG(exception)) {
8780
        /* EX(opline) was overridden in zend_jit_trace_exit_stub(),
8781
         * and may be wrong when IP is reused. */
8782
0
        EX(opline) = EG(exception_op);
8783
0
        return 0;
8784
0
      }
8785
0
    }
8786
0
    if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL) {
8787
0
      zend_jit_ref_snapshot *func_snapshot = &t->exit_info[exit_num].poly_func;
8788
0
      ZEND_ASSERT(func_snapshot->reg >= 0);
8789
8790
0
      zend_function *func;
8791
0
      if (IR_REG_SPILLED(func_snapshot->reg)) {
8792
0
        func = *(zend_function**)(regs->gpr[IR_REG_NUM(func_snapshot->reg)] + func_snapshot->offset);
8793
0
      } else {
8794
0
        func = (zend_function*)regs->gpr[func_snapshot->reg];
8795
0
      }
8796
0
      if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
8797
0
        zend_string_release_ex(func->common.function_name, 0);
8798
0
        zend_free_trampoline(func);
8799
0
        EX(opline) = opline;
8800
0
        return 1;
8801
0
      }
8802
0
    }
8803
8804
    /* Set VM opline to continue interpretation */
8805
0
    EX(opline) = opline;
8806
0
  }
8807
8808
0
  if (zend_atomic_bool_load_ex(&EG(vm_interrupt)) || JIT_G(tracing)) {
8809
0
    return 1;
8810
  /* Lock-free check if the side trace was already JIT-ed or blacklist-ed in another process */
8811
0
  } else if (t->exit_info[exit_num].flags & (ZEND_JIT_EXIT_JITED|ZEND_JIT_EXIT_BLACKLISTED)) {
8812
0
    return 0;
8813
0
  }
8814
8815
0
  ZEND_ASSERT(EX(func)->type == ZEND_USER_FUNCTION);
8816
0
  ZEND_ASSERT(EX(opline) >= EX(func)->op_array.opcodes &&
8817
0
    EX(opline) < EX(func)->op_array.opcodes + EX(func)->op_array.last);
8818
8819
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_EXIT) {
8820
0
    fprintf(stderr, "     TRACE %d exit %d %s%s%s() %s:%d\n",
8821
0
      trace_num,
8822
0
      exit_num,
8823
0
      EX(func)->op_array.scope ? ZSTR_VAL(EX(func)->op_array.scope->name) : "",
8824
0
      EX(func)->op_array.scope ? "::" : "",
8825
0
      EX(func)->op_array.function_name ?
8826
0
        ZSTR_VAL(EX(func)->op_array.function_name) : "$main",
8827
0
      ZSTR_VAL(EX(func)->op_array.filename),
8828
0
      EX(opline)->lineno);
8829
0
  }
8830
8831
0
  if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_INVALIDATE) {
8832
0
    zend_jit_op_array_trace_extension *jit_extension;
8833
0
    uint32_t num = trace_num;
8834
8835
0
    while (t->root != num) {
8836
0
      num = t->root;
8837
0
      t = &zend_jit_traces[num];
8838
0
    }
8839
8840
0
    zend_shared_alloc_lock();
8841
8842
0
    jit_extension = (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(t->op_array);
8843
8844
    /* Checks under lock, just in case something has changed while we were waiting for the lock */
8845
0
    if (!(ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags & (ZEND_JIT_TRACE_JITED|ZEND_JIT_TRACE_BLACKLISTED))) {
8846
      /* skip: not JIT-ed nor blacklisted */
8847
0
    } else if (ZEND_JIT_TRACE_NUM >= JIT_G(max_root_traces)) {
8848
      /* too many root traces, blacklist the root trace */
8849
0
      if (!(ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags & ZEND_JIT_TRACE_BLACKLISTED)) {
8850
0
        SHM_UNPROTECT();
8851
0
        zend_jit_unprotect();
8852
8853
0
        ((zend_op*)opline)->handler =
8854
0
          ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->orig_handler;
8855
8856
0
        ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags &= ~ZEND_JIT_TRACE_JITED;
8857
0
        ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags |= ZEND_JIT_TRACE_BLACKLISTED;
8858
8859
0
        zend_jit_protect();
8860
0
        SHM_PROTECT();
8861
0
      }
8862
0
    } else {
8863
0
      SHM_UNPROTECT();
8864
0
      zend_jit_unprotect();
8865
8866
0
      if (ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags & ZEND_JIT_TRACE_START_LOOP) {
8867
0
        ((zend_op*)(t->opline))->handler = zend_jit_loop_trace_counter_handler;
8868
0
      } else if (ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags & ZEND_JIT_TRACE_START_ENTER) {
8869
0
        ((zend_op*)(t->opline))->handler = zend_jit_func_trace_counter_handler;
8870
0
      } else if (ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags & ZEND_JIT_TRACE_START_RETURN) {
8871
0
        ((zend_op*)(t->opline))->handler = zend_jit_ret_trace_counter_handler;
8872
0
      }
8873
0
      ZEND_OP_TRACE_INFO(t->opline, jit_extension->offset)->trace_flags &=
8874
0
        ZEND_JIT_TRACE_START_LOOP|ZEND_JIT_TRACE_START_ENTER|ZEND_JIT_TRACE_START_RETURN;
8875
8876
0
      zend_jit_protect();
8877
0
      SHM_PROTECT();
8878
0
    }
8879
8880
0
    zend_shared_alloc_unlock();
8881
8882
0
    return 0;
8883
0
  }
8884
8885
0
  if (t->exit_info[exit_num].flags & ZEND_JIT_EXIT_TO_VM) {
8886
0
    if (zend_jit_trace_exit_is_bad(trace_num, exit_num)) {
8887
0
      zend_jit_blacklist_trace_exit(trace_num, exit_num);
8888
0
      if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BLACKLIST) {
8889
0
        fprintf(stderr, "---- EXIT %d/%d blacklisted\n",
8890
0
          trace_num, exit_num);
8891
0
      }
8892
0
      return 0;
8893
0
    }
8894
0
  } else if (JIT_G(hot_side_exit) && zend_jit_trace_exit_is_hot(trace_num, exit_num)) {
8895
0
    return zend_jit_trace_hot_side(execute_data, trace_num, exit_num);
8896
0
  }
8897
8898
  /* Return 1 to call original handler instead of the same JIT-ed trace */
8899
0
  return (orig_opline == t->opline && EX(opline) == orig_opline);
8900
0
}
8901
8902
static zend_always_inline uint8_t zend_jit_trace_supported(const zend_op *opline)
8903
0
{
8904
0
  switch (opline->opcode) {
8905
0
    case ZEND_CATCH:
8906
0
    case ZEND_FAST_CALL:
8907
0
    case ZEND_FAST_RET:
8908
0
      return ZEND_JIT_TRACE_UNSUPPORTED;
8909
0
    default:
8910
0
      return ZEND_JIT_TRACE_SUPPORTED;
8911
0
  }
8912
0
}
8913
8914
static int zend_jit_restart_hot_trace_counters(zend_op_array *op_array)
8915
0
{
8916
0
  zend_jit_op_array_trace_extension *jit_extension;
8917
0
  uint32_t i;
8918
8919
0
  jit_extension = (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array);
8920
0
  for (i = 0; i < op_array->last; i++) {
8921
0
    jit_extension->trace_info[i].trace_flags &=
8922
0
      ZEND_JIT_TRACE_START_LOOP | ZEND_JIT_TRACE_START_ENTER | ZEND_JIT_TRACE_UNSUPPORTED;
8923
0
    if (jit_extension->trace_info[i].trace_flags == ZEND_JIT_TRACE_START_LOOP) {
8924
0
      op_array->opcodes[i].handler = zend_jit_loop_trace_counter_handler;
8925
0
    } else if (jit_extension->trace_info[i].trace_flags == ZEND_JIT_TRACE_START_ENTER) {
8926
0
      op_array->opcodes[i].handler = zend_jit_func_trace_counter_handler;
8927
0
    } else {
8928
0
      op_array->opcodes[i].handler = jit_extension->trace_info[i].orig_handler;
8929
0
    }
8930
0
  }
8931
0
  return SUCCESS;
8932
0
}
8933
8934
static int zend_jit_setup_hot_trace_counters(zend_op_array *op_array)
8935
0
{
8936
0
  zend_op *opline;
8937
0
  zend_jit_op_array_trace_extension *jit_extension;
8938
0
  uint32_t i;
8939
8940
0
  ZEND_ASSERT(sizeof(zend_op_trace_info) == sizeof(zend_op));
8941
8942
0
  jit_extension = (zend_jit_op_array_trace_extension*)zend_shared_alloc(sizeof(zend_jit_op_array_trace_extension) + (op_array->last - 1) * sizeof(zend_op_trace_info));
8943
0
  if (!jit_extension) {
8944
0
    return FAILURE;
8945
0
  }
8946
0
  memset(&jit_extension->func_info, 0, sizeof(zend_func_info));
8947
0
  jit_extension->func_info.flags = ZEND_FUNC_JIT_ON_HOT_TRACE;
8948
0
  jit_extension->op_array = op_array;
8949
0
  jit_extension->offset = (char*)jit_extension->trace_info - (char*)op_array->opcodes;
8950
0
  for (i = 0; i < op_array->last; i++) {
8951
0
    jit_extension->trace_info[i].orig_handler = op_array->opcodes[i].handler;
8952
0
    jit_extension->trace_info[i].call_handler = (zend_vm_opcode_handler_func_t)zend_get_opcode_handler_func(&op_array->opcodes[i]);
8953
0
    jit_extension->trace_info[i].counter = NULL;
8954
0
    jit_extension->trace_info[i].trace_flags =
8955
0
      zend_jit_trace_supported(&op_array->opcodes[i]);
8956
0
  }
8957
0
  ZEND_SET_FUNC_INFO(op_array, (void*)jit_extension);
8958
8959
0
  if (JIT_G(hot_loop)) {
8960
0
    zend_cfg cfg;
8961
8962
0
    ZEND_ASSERT(zend_jit_loop_trace_counter_handler != NULL);
8963
8964
0
    if (zend_jit_build_cfg(op_array, &cfg) != SUCCESS) {
8965
0
      return FAILURE;
8966
0
    }
8967
8968
0
    for (i = 0; i < cfg.blocks_count; i++) {
8969
0
      if (cfg.blocks[i].flags & ZEND_BB_REACHABLE) {
8970
0
        if (cfg.blocks[i].flags & ZEND_BB_LOOP_HEADER) {
8971
          /* loop header */
8972
0
          opline = op_array->opcodes + cfg.blocks[i].start;
8973
0
          if (!(ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->trace_flags & ZEND_JIT_TRACE_UNSUPPORTED)) {
8974
0
            opline->handler = zend_jit_loop_trace_counter_handler;
8975
0
            if (!ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->counter) {
8976
0
              ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->counter =
8977
0
                &zend_jit_hot_counters[ZEND_JIT_COUNTER_NUM];
8978
0
              ZEND_JIT_COUNTER_NUM = (ZEND_JIT_COUNTER_NUM + 1) % ZEND_HOT_COUNTERS_COUNT;
8979
0
            }
8980
0
            ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->trace_flags |=
8981
0
              ZEND_JIT_TRACE_START_LOOP;
8982
0
          }
8983
0
        }
8984
0
      }
8985
0
    }
8986
0
  }
8987
8988
0
  if (JIT_G(hot_func)) {
8989
0
    ZEND_ASSERT(zend_jit_func_trace_counter_handler != NULL);
8990
0
    opline = op_array->opcodes;
8991
0
    if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
8992
0
      while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
8993
0
        opline++;
8994
0
      }
8995
0
    }
8996
8997
0
    if (!ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->trace_flags) {
8998
      /* function entry */
8999
0
      opline->handler = zend_jit_func_trace_counter_handler;
9000
0
      ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->counter =
9001
0
        &zend_jit_hot_counters[ZEND_JIT_COUNTER_NUM];
9002
0
      ZEND_JIT_COUNTER_NUM = (ZEND_JIT_COUNTER_NUM + 1) % ZEND_HOT_COUNTERS_COUNT;
9003
0
      ZEND_OP_TRACE_INFO(opline, jit_extension->offset)->trace_flags |=
9004
0
        ZEND_JIT_TRACE_START_ENTER;
9005
0
    }
9006
0
  }
9007
9008
0
  zend_shared_alloc_register_xlat_entry(op_array->opcodes, jit_extension);
9009
9010
0
  return SUCCESS;
9011
0
}
9012
9013
static void zend_jit_trace_init_caches(void)
9014
16
{
9015
16
  memset(ZEND_VOIDP(JIT_G(bad_root_cache_opline)), 0, sizeof(JIT_G(bad_root_cache_opline)));
9016
16
  memset(JIT_G(bad_root_cache_count), 0, sizeof(JIT_G(bad_root_cache_count)));
9017
16
  memset(JIT_G(bad_root_cache_stop), 0, sizeof(JIT_G(bad_root_cache_count)));
9018
16
  JIT_G(bad_root_slot) = 0;
9019
9020
16
  if (JIT_G(exit_counters)) {
9021
0
    memset(JIT_G(exit_counters), 0, JIT_G(max_exit_counters));
9022
0
  }
9023
16
}
9024
9025
static void zend_jit_trace_reset_caches(void)
9026
0
{
9027
0
  JIT_G(tracing) = 0;
9028
#ifdef ZTS
9029
  if (!JIT_G(exit_counters)) {
9030
    JIT_G(exit_counters) = calloc(JIT_G(max_exit_counters), 1);
9031
  }
9032
#endif
9033
0
}
9034
9035
static void zend_jit_trace_free_caches(zend_jit_globals *jit_globals)
9036
0
{
9037
0
  if (jit_globals->exit_counters) {
9038
0
    free(jit_globals->exit_counters);
9039
0
  }
9040
0
}
9041
9042
static void zend_jit_trace_restart(void)
9043
0
{
9044
0
  ZEND_JIT_TRACE_NUM = 1;
9045
0
  ZEND_JIT_COUNTER_NUM = 0;
9046
0
  ZEND_JIT_EXIT_NUM = 0;
9047
0
  ZEND_JIT_EXIT_COUNTERS = 0;
9048
0
  ZCSG(jit_counters_stopped) = false;
9049
9050
0
  zend_jit_trace_init_caches();
9051
0
}