| [338] | 1 | #if 0 |
|---|
| [543] | 2 | # define XCACHE_DEBUG |
|---|
| [308] | 3 | #endif |
|---|
| 4 | |
|---|
| 5 | #include "utils.h" |
|---|
| [1] | 6 | #include "optimizer.h" |
|---|
| [308] | 7 | /* the "vector" stack */ |
|---|
| 8 | #include "stack.h" |
|---|
| [477] | 9 | #include "xcache_globals.h" |
|---|
| [1] | 10 | |
|---|
| [543] | 11 | #ifdef XCACHE_DEBUG |
|---|
| [308] | 12 | # include "processor.h" |
|---|
| [326] | 13 | # include "const_string.h" |
|---|
| [331] | 14 | # include "ext/standard/php_var.h" |
|---|
| [308] | 15 | #endif |
|---|
| 16 | |
|---|
| [331] | 17 | #ifdef IS_CV |
|---|
| 18 | # define XCACHE_IS_CV IS_CV |
|---|
| 19 | #else |
|---|
| 20 | # define XCACHE_IS_CV 16 |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| [822] | 23 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 24 | # undef Z_OP_CONSTANT |
|---|
| 25 | /* Z_OP_CONSTANT is used before pass_two is applied */ |
|---|
| 26 | # define Z_OP_CONSTANT(op) op_array->literals[op.constant].constant |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| [308] | 29 | typedef int bbid_t; |
|---|
| 30 | enum { |
|---|
| 31 | BBID_INVALID = -1, |
|---|
| 32 | }; |
|---|
| 33 | /* {{{ basic block */ |
|---|
| 34 | typedef struct _bb_t { |
|---|
| 35 | bbid_t id; |
|---|
| 36 | zend_bool used; |
|---|
| 37 | |
|---|
| 38 | zend_bool alloc; |
|---|
| 39 | zend_op *opcodes; |
|---|
| 40 | int count; |
|---|
| 41 | int size; |
|---|
| 42 | |
|---|
| [312] | 43 | bbid_t fall; |
|---|
| [657] | 44 | #ifdef ZEND_ENGINE_2 |
|---|
| [326] | 45 | bbid_t catch; |
|---|
| [657] | 46 | #endif |
|---|
| [326] | 47 | |
|---|
| 48 | int opnum; /* opnum after joining basic block */ |
|---|
| [308] | 49 | } bb_t; |
|---|
| 50 | /* }}} */ |
|---|
| 51 | |
|---|
| 52 | /* basic blocks */ |
|---|
| 53 | typedef xc_stack_t bbs_t; |
|---|
| 54 | |
|---|
| [326] | 55 | /* op array helper functions */ |
|---|
| [308] | 56 | static int op_array_convert_switch(zend_op_array *op_array) /* {{{ */ |
|---|
| 57 | { |
|---|
| 58 | int i; |
|---|
| 59 | |
|---|
| 60 | if (op_array->brk_cont_array == NULL) { |
|---|
| 61 | return SUCCESS; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | for (i = 0; i < op_array->last; i ++) { |
|---|
| 65 | zend_op *opline = &op_array->opcodes[i]; |
|---|
| 66 | zend_brk_cont_element *jmp_to; |
|---|
| [834] | 67 | zend_bool can_convert = 1; |
|---|
| [308] | 68 | int array_offset, nest_levels, original_nest_levels; |
|---|
| 69 | |
|---|
| [834] | 70 | switch (opline->opcode) { |
|---|
| 71 | case ZEND_BRK: |
|---|
| 72 | case ZEND_CONT: |
|---|
| 73 | break; |
|---|
| 74 | |
|---|
| 75 | #ifdef ZEND_GOTO |
|---|
| 76 | case ZEND_GOTO: |
|---|
| 77 | #endif |
|---|
| [845] | 78 | continue; |
|---|
| [834] | 79 | |
|---|
| 80 | default: |
|---|
| 81 | continue; |
|---|
| [308] | 82 | } |
|---|
| [834] | 83 | |
|---|
| [716] | 84 | if (Z_OP_TYPE(opline->op2) != IS_CONST |
|---|
| 85 | || Z_OP_CONSTANT(opline->op2).type != IS_LONG) { |
|---|
| [308] | 86 | return FAILURE; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| [716] | 89 | nest_levels = Z_OP_CONSTANT(opline->op2).value.lval; |
|---|
| [308] | 90 | original_nest_levels = nest_levels; |
|---|
| 91 | |
|---|
| [716] | 92 | array_offset = Z_OP(opline->op1).opline_num; |
|---|
| [308] | 93 | do { |
|---|
| 94 | if (array_offset == -1) { |
|---|
| 95 | /* this is a runtime error in ZE |
|---|
| 96 | zend_error(E_ERROR, "Cannot break/continue %d level%s", original_nest_levels, (original_nest_levels == 1) ? "" : "s"); |
|---|
| 97 | */ |
|---|
| 98 | return FAILURE; |
|---|
| 99 | } |
|---|
| 100 | jmp_to = &op_array->brk_cont_array[array_offset]; |
|---|
| 101 | if (nest_levels > 1) { |
|---|
| 102 | zend_op *brk_opline = &op_array->opcodes[jmp_to->brk]; |
|---|
| 103 | |
|---|
| 104 | switch (brk_opline->opcode) { |
|---|
| 105 | case ZEND_SWITCH_FREE: |
|---|
| 106 | case ZEND_FREE: |
|---|
| [834] | 107 | if (!(brk_opline->extended_value & EXT_TYPE_FREE_ON_RETURN)) { |
|---|
| 108 | can_convert = 0; |
|---|
| 109 | } |
|---|
| [308] | 110 | break; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | array_offset = jmp_to->parent; |
|---|
| 114 | } while (--nest_levels > 0); |
|---|
| 115 | |
|---|
| [834] | 116 | if (can_convert) { |
|---|
| 117 | /* rewrite to jmp */ |
|---|
| 118 | switch (opline->opcode) { |
|---|
| 119 | case ZEND_BRK: |
|---|
| 120 | Z_OP(opline->op1).opline_num = jmp_to->brk; |
|---|
| 121 | break; |
|---|
| 122 | |
|---|
| 123 | case ZEND_CONT: |
|---|
| 124 | Z_OP(opline->op1).opline_num = jmp_to->cont; |
|---|
| 125 | break; |
|---|
| 126 | } |
|---|
| 127 | Z_OP_TYPE(opline->op2) = IS_UNUSED; |
|---|
| 128 | opline->opcode = ZEND_JMP; |
|---|
| [308] | 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | return SUCCESS; |
|---|
| 133 | } |
|---|
| 134 | /* }}} */ |
|---|
| [326] | 135 | /* {{{ op_flowinfo helper func */ |
|---|
| 136 | enum { |
|---|
| 137 | XC_OPNUM_INVALID = -1, |
|---|
| 138 | }; |
|---|
| 139 | typedef struct { |
|---|
| 140 | int jmpout_op1; |
|---|
| 141 | int jmpout_op2; |
|---|
| 142 | int jmpout_ext; |
|---|
| 143 | zend_bool fall; |
|---|
| 144 | } op_flowinfo_t; |
|---|
| 145 | static void op_flowinfo_init(op_flowinfo_t *fi) |
|---|
| [308] | 146 | { |
|---|
| [326] | 147 | fi->jmpout_op1 = fi->jmpout_op2 = fi->jmpout_ext = XC_OPNUM_INVALID; |
|---|
| 148 | fi->fall = 0; |
|---|
| 149 | } |
|---|
| 150 | /* }}} */ |
|---|
| 151 | static int op_get_flowinfo(op_flowinfo_t *fi, zend_op *opline) /* {{{ */ |
|---|
| 152 | { |
|---|
| 153 | op_flowinfo_init(fi); |
|---|
| 154 | |
|---|
| [312] | 155 | /* break=will fall */ |
|---|
| [308] | 156 | switch (opline->opcode) { |
|---|
| [657] | 157 | #ifdef ZEND_HANDLE_EXCEPTION |
|---|
| [326] | 158 | case ZEND_HANDLE_EXCEPTION: |
|---|
| [657] | 159 | #endif |
|---|
| [308] | 160 | case ZEND_RETURN: |
|---|
| 161 | case ZEND_EXIT: |
|---|
| [312] | 162 | return SUCCESS; /* no fall */ |
|---|
| [308] | 163 | |
|---|
| 164 | case ZEND_JMP: |
|---|
| [716] | 165 | fi->jmpout_op1 = Z_OP(opline->op1).opline_num; |
|---|
| [312] | 166 | return SUCCESS; /* no fall */ |
|---|
| [308] | 167 | |
|---|
| 168 | case ZEND_JMPZNZ: |
|---|
| [716] | 169 | fi->jmpout_op2 = Z_OP(opline->op2).opline_num; |
|---|
| [326] | 170 | fi->jmpout_ext = (int) opline->extended_value; |
|---|
| 171 | return SUCCESS; /* no fall */ |
|---|
| [308] | 172 | |
|---|
| 173 | case ZEND_JMPZ: |
|---|
| 174 | case ZEND_JMPNZ: |
|---|
| 175 | case ZEND_JMPZ_EX: |
|---|
| 176 | case ZEND_JMPNZ_EX: |
|---|
| [485] | 177 | #ifdef ZEND_JMP_SET |
|---|
| 178 | case ZEND_JMP_SET: |
|---|
| 179 | #endif |
|---|
| [326] | 180 | #ifdef ZEND_JMP_NO_CTOR |
|---|
| [308] | 181 | case ZEND_JMP_NO_CTOR: |
|---|
| [326] | 182 | #endif |
|---|
| 183 | #ifdef ZEND_NEW |
|---|
| [308] | 184 | case ZEND_NEW: |
|---|
| [326] | 185 | #endif |
|---|
| 186 | #ifdef ZEND_FE_RESET |
|---|
| [308] | 187 | case ZEND_FE_RESET: |
|---|
| 188 | #endif |
|---|
| 189 | case ZEND_FE_FETCH: |
|---|
| [716] | 190 | fi->jmpout_op2 = Z_OP(opline->op2).opline_num; |
|---|
| [308] | 191 | break; |
|---|
| 192 | |
|---|
| [326] | 193 | #ifdef ZEND_CATCH |
|---|
| 194 | case ZEND_CATCH: |
|---|
| 195 | fi->jmpout_ext = (int) opline->extended_value; |
|---|
| 196 | break; |
|---|
| 197 | #endif |
|---|
| 198 | |
|---|
| [308] | 199 | default: |
|---|
| 200 | return FAILURE; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| [326] | 203 | fi->fall = 1; |
|---|
| [308] | 204 | return SUCCESS; |
|---|
| 205 | } |
|---|
| 206 | /* }}} */ |
|---|
| [543] | 207 | #ifdef XCACHE_DEBUG |
|---|
| [716] | 208 | static void op_snprint(char *buf, int size, zend_uchar op_type, znode_op *op) /* {{{ */ |
|---|
| [331] | 209 | { |
|---|
| [716] | 210 | switch (op_type) { |
|---|
| [331] | 211 | case IS_CONST: |
|---|
| 212 | { |
|---|
| 213 | zval result; |
|---|
| [716] | 214 | zval *zv = &Z_OP_CONSTANT(*op); |
|---|
| [331] | 215 | TSRMLS_FETCH(); |
|---|
| [308] | 216 | |
|---|
| [716] | 217 | /* TODO: update for PHP 6 */ |
|---|
| [331] | 218 | php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC); |
|---|
| 219 | php_var_export(&zv, 1 TSRMLS_CC); |
|---|
| 220 | |
|---|
| 221 | php_ob_get_buffer(&result TSRMLS_CC); |
|---|
| 222 | php_end_ob_buffer(0, 0 TSRMLS_CC); |
|---|
| 223 | snprintf(buf, size, Z_STRVAL(result)); |
|---|
| 224 | zval_dtor(&result); |
|---|
| 225 | } |
|---|
| 226 | break; |
|---|
| 227 | |
|---|
| 228 | case IS_TMP_VAR: |
|---|
| [716] | 229 | snprintf(buf, size, "t@%d", Z_OP(*op).var); |
|---|
| [331] | 230 | break; |
|---|
| 231 | |
|---|
| 232 | case XCACHE_IS_CV: |
|---|
| 233 | case IS_VAR: |
|---|
| [716] | 234 | snprintf(buf, size, "v@%d", Z_OP(*op).var); |
|---|
| [331] | 235 | break; |
|---|
| 236 | |
|---|
| 237 | case IS_UNUSED: |
|---|
| [716] | 238 | if (Z_OP(*op).opline_num) { |
|---|
| 239 | snprintf(buf, size, "u#%d", Z_OP(op).opline_num); |
|---|
| [331] | 240 | } |
|---|
| 241 | else { |
|---|
| 242 | snprintf(buf, size, "-"); |
|---|
| 243 | } |
|---|
| 244 | break; |
|---|
| 245 | |
|---|
| 246 | default: |
|---|
| [716] | 247 | snprintf(buf, size, "%d %d", op->op_type, Z_OP(op).var); |
|---|
| [331] | 248 | } |
|---|
| 249 | } |
|---|
| 250 | /* }}} */ |
|---|
| 251 | static void op_print(int line, zend_op *first, zend_op *end) /* {{{ */ |
|---|
| 252 | { |
|---|
| 253 | zend_op *opline; |
|---|
| 254 | for (opline = first; opline < end; opline ++) { |
|---|
| 255 | char buf_r[20]; |
|---|
| 256 | char buf_1[20]; |
|---|
| 257 | char buf_2[20]; |
|---|
| [716] | 258 | op_snprint(buf_r, sizeof(buf_r), Z_OP_TYPE(opline->result), &opline->result); |
|---|
| 259 | op_snprint(buf_1, sizeof(buf_1), Z_OP_TYPE(opline->op1), &opline->op1); |
|---|
| 260 | op_snprint(buf_2, sizeof(buf_2), Z_OP_TYPE(opline->op2), &opline->op2); |
|---|
| [333] | 261 | fprintf(stderr, |
|---|
| 262 | "%3d %3d" |
|---|
| 263 | " %-25s%-5s%-20s%-20s%5lu\r\n" |
|---|
| 264 | , opline->lineno, opline - first + line |
|---|
| 265 | , xc_get_opcode(opline->opcode), buf_r, buf_1, buf_2, opline->extended_value); |
|---|
| [331] | 266 | } |
|---|
| 267 | } |
|---|
| 268 | /* }}} */ |
|---|
| 269 | #endif |
|---|
| 270 | |
|---|
| [308] | 271 | /* |
|---|
| 272 | * basic block functions |
|---|
| 273 | */ |
|---|
| 274 | |
|---|
| 275 | static bb_t *bb_new_ex(zend_op *opcodes, int count) /* {{{ */ |
|---|
| 276 | { |
|---|
| 277 | bb_t *bb = (bb_t *) ecalloc(sizeof(bb_t), 1); |
|---|
| 278 | |
|---|
| [312] | 279 | bb->fall = BBID_INVALID; |
|---|
| [657] | 280 | #ifdef ZEND_ENGINE_2 |
|---|
| [326] | 281 | bb->catch = BBID_INVALID; |
|---|
| [657] | 282 | #endif |
|---|
| [308] | 283 | |
|---|
| 284 | if (opcodes) { |
|---|
| 285 | bb->alloc = 0; |
|---|
| 286 | bb->size = bb->count = count; |
|---|
| 287 | bb->opcodes = opcodes; |
|---|
| 288 | } |
|---|
| 289 | else { |
|---|
| 290 | bb->alloc = 1; |
|---|
| 291 | bb->size = bb->count = 8; |
|---|
| 292 | bb->opcodes = ecalloc(sizeof(zend_op), bb->size); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | return bb; |
|---|
| 296 | } |
|---|
| 297 | /* }}} */ |
|---|
| 298 | #define bb_new() bb_new_ex(NULL, 0) |
|---|
| 299 | static void bb_destroy(bb_t *bb) /* {{{ */ |
|---|
| 300 | { |
|---|
| 301 | if (bb->alloc) { |
|---|
| 302 | efree(bb->opcodes); |
|---|
| 303 | } |
|---|
| 304 | efree(bb); |
|---|
| 305 | } |
|---|
| 306 | /* }}} */ |
|---|
| [543] | 307 | #ifdef XCACHE_DEBUG |
|---|
| [312] | 308 | static void bb_print(bb_t *bb, zend_op *opcodes) /* {{{ */ |
|---|
| 309 | { |
|---|
| [331] | 310 | int line = bb->opcodes - opcodes; |
|---|
| [326] | 311 | op_flowinfo_t fi; |
|---|
| 312 | zend_op *last = bb->opcodes + bb->count - 1; |
|---|
| [657] | 313 | bbid_t catchbbid; |
|---|
| 314 | #ifdef ZEND_ENGINE_2 |
|---|
| 315 | catchbbid = BBID_INVALID; |
|---|
| 316 | #else |
|---|
| 317 | catchbbid = bb->catch; |
|---|
| 318 | #endif |
|---|
| [326] | 319 | |
|---|
| 320 | op_get_flowinfo(&fi, last); |
|---|
| 321 | |
|---|
| [312] | 322 | fprintf(stderr, |
|---|
| [333] | 323 | "\r\n==== #%-3d cnt:%-3d lno:%-3d" |
|---|
| [312] | 324 | " %c%c" |
|---|
| [333] | 325 | " op1:%-3d op2:%-3d ext:%-3d fal:%-3d cat:%-3d %s ====\r\n" |
|---|
| [331] | 326 | , bb->id, bb->count, bb->alloc ? -1 : line |
|---|
| [312] | 327 | , bb->used ? 'U' : ' ', bb->alloc ? 'A' : ' ' |
|---|
| [657] | 328 | , fi.jmpout_op1, fi.jmpout_op2, fi.jmpout_ext, bb->fall, catchbbid, xc_get_opcode(last->opcode) |
|---|
| [312] | 329 | ); |
|---|
| [331] | 330 | op_print(line, bb->opcodes, last + 1); |
|---|
| [312] | 331 | } |
|---|
| 332 | /* }}} */ |
|---|
| 333 | #endif |
|---|
| 334 | |
|---|
| [330] | 335 | static bb_t *bbs_get(bbs_t *bbs, int n) /* {{{ */ |
|---|
| 336 | { |
|---|
| 337 | return (bb_t *) xc_stack_get(bbs, n); |
|---|
| 338 | } |
|---|
| 339 | /* }}} */ |
|---|
| 340 | static int bbs_count(bbs_t *bbs) /* {{{ */ |
|---|
| 341 | { |
|---|
| 342 | return xc_stack_count(bbs); |
|---|
| 343 | } |
|---|
| 344 | /* }}} */ |
|---|
| [308] | 345 | static void bbs_destroy(bbs_t *bbs) /* {{{ */ |
|---|
| 346 | { |
|---|
| 347 | bb_t *bb; |
|---|
| [326] | 348 | while (bbs_count(bbs)) { |
|---|
| [308] | 349 | bb = (bb_t *) xc_stack_pop(bbs); |
|---|
| 350 | bb_destroy(bb); |
|---|
| 351 | } |
|---|
| [313] | 352 | xc_stack_destroy(bbs); |
|---|
| [308] | 353 | } |
|---|
| 354 | /* }}} */ |
|---|
| [543] | 355 | #ifdef XCACHE_DEBUG |
|---|
| [312] | 356 | static void bbs_print(bbs_t *bbs, zend_op *opcodes) /* {{{ */ |
|---|
| 357 | { |
|---|
| 358 | int i; |
|---|
| 359 | for (i = 0; i < xc_stack_count(bbs); i ++) { |
|---|
| 360 | bb_print(bbs_get(bbs, i), opcodes); |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | /* }}} */ |
|---|
| 364 | #endif |
|---|
| [308] | 365 | #define bbs_init(bbs) xc_stack_init_ex(bbs, 16) |
|---|
| 366 | static bb_t *bbs_add_bb(bbs_t *bbs, bb_t *bb) /* {{{ */ |
|---|
| 367 | { |
|---|
| 368 | bb->id = (bbid_t) xc_stack_count(bbs); |
|---|
| 369 | xc_stack_push(bbs, (void *) bb); |
|---|
| 370 | return bb; |
|---|
| 371 | } |
|---|
| 372 | /* }}} */ |
|---|
| 373 | static bb_t *bbs_new_bb_ex(bbs_t *bbs, zend_op *opcodes, int count) /* {{{ */ |
|---|
| 374 | { |
|---|
| 375 | return bbs_add_bb(bbs, bb_new_ex(opcodes, count)); |
|---|
| 376 | } |
|---|
| 377 | /* }}} */ |
|---|
| [326] | 378 | static int bbs_build_from(bbs_t *bbs, zend_op_array *op_array, int count) /* {{{ */ |
|---|
| [308] | 379 | { |
|---|
| [326] | 380 | int i, start; |
|---|
| [312] | 381 | bb_t *pbb; |
|---|
| 382 | bbid_t id; |
|---|
| [326] | 383 | op_flowinfo_t fi; |
|---|
| 384 | zend_op *opline; |
|---|
| [485] | 385 | ALLOCA_FLAG(use_heap_bbids) |
|---|
| 386 | ALLOCA_FLAG(use_heap_catchbbids) |
|---|
| 387 | ALLOCA_FLAG(use_heap_markbbhead) |
|---|
| 388 | bbid_t *bbids = my_do_alloca(count * sizeof(bbid_t), use_heap_bbids); |
|---|
| [657] | 389 | #ifdef ZEND_ENGINE_2 |
|---|
| [485] | 390 | bbid_t *catchbbids = my_do_alloca(count * sizeof(bbid_t), use_heap_catchbbids); |
|---|
| [657] | 391 | #endif |
|---|
| [485] | 392 | zend_bool *markbbhead = my_do_alloca(count * sizeof(zend_bool), use_heap_markbbhead); |
|---|
| [308] | 393 | |
|---|
| [312] | 394 | /* {{{ mark jmpin/jumpout */ |
|---|
| [326] | 395 | memset(markbbhead, 0, count * sizeof(zend_bool)); |
|---|
| [308] | 396 | |
|---|
| [326] | 397 | markbbhead[0] = 1; |
|---|
| [308] | 398 | for (i = 0; i < count; i ++) { |
|---|
| [326] | 399 | if (op_get_flowinfo(&fi, &op_array->opcodes[i]) == SUCCESS) { |
|---|
| 400 | if (fi.jmpout_op1 != XC_OPNUM_INVALID) { |
|---|
| 401 | markbbhead[fi.jmpout_op1] = 1; |
|---|
| [308] | 402 | } |
|---|
| [326] | 403 | if (fi.jmpout_op2 != XC_OPNUM_INVALID) { |
|---|
| 404 | markbbhead[fi.jmpout_op2] = 1; |
|---|
| [308] | 405 | } |
|---|
| [326] | 406 | if (fi.jmpout_ext != XC_OPNUM_INVALID) { |
|---|
| 407 | markbbhead[fi.jmpout_ext] = 1; |
|---|
| [308] | 408 | } |
|---|
| [326] | 409 | if (i + 1 < count) { |
|---|
| 410 | markbbhead[i + 1] = 1; |
|---|
| 411 | } |
|---|
| [308] | 412 | } |
|---|
| 413 | } |
|---|
| [657] | 414 | #ifdef ZEND_ENGINE_2 |
|---|
| [326] | 415 | /* mark try start */ |
|---|
| 416 | for (i = 0; i < op_array->last_try_catch; i ++) { |
|---|
| 417 | markbbhead[op_array->try_catch_array[i].try_op] = 1; |
|---|
| 418 | } |
|---|
| [657] | 419 | #endif |
|---|
| [312] | 420 | /* }}} */ |
|---|
| [326] | 421 | /* {{{ fill op lines with newly allocated id */ |
|---|
| [312] | 422 | for (i = 0; i < count; i ++) { |
|---|
| 423 | bbids[i] = BBID_INVALID; |
|---|
| 424 | } |
|---|
| [308] | 425 | |
|---|
| [326] | 426 | id = -1; |
|---|
| 427 | for (i = 0; i < count; i ++) { |
|---|
| 428 | if (markbbhead[i]) { |
|---|
| 429 | id ++; |
|---|
| [312] | 430 | } |
|---|
| [326] | 431 | bbids[i] = id; |
|---|
| 432 | TRACE("bbids[%d] = %d", i, id); |
|---|
| [308] | 433 | } |
|---|
| [312] | 434 | /* }}} */ |
|---|
| [657] | 435 | #ifdef ZEND_ENGINE_2 |
|---|
| [326] | 436 | /* {{{ fill op lines with catch id */ |
|---|
| 437 | for (i = 0; i < count; i ++) { |
|---|
| 438 | catchbbids[i] = BBID_INVALID; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | for (i = 0; i < op_array->last_try_catch; i ++) { |
|---|
| 442 | int j; |
|---|
| 443 | zend_try_catch_element *e = &op_array->try_catch_array[i]; |
|---|
| 444 | for (j = e->try_op; j < e->catch_op; j ++) { |
|---|
| 445 | catchbbids[j] = bbids[e->catch_op]; |
|---|
| 446 | } |
|---|
| 447 | } |
|---|
| [543] | 448 | #ifdef XCACHE_DEBUG |
|---|
| [326] | 449 | for (i = 0; i < count; i ++) { |
|---|
| 450 | TRACE("catchbbids[%d] = %d", i, catchbbids[i]); |
|---|
| 451 | } |
|---|
| 452 | #endif |
|---|
| 453 | /* }}} */ |
|---|
| [657] | 454 | #endif |
|---|
| [312] | 455 | /* {{{ create basic blocks */ |
|---|
| [326] | 456 | start = 0; |
|---|
| [312] | 457 | id = 0; |
|---|
| [326] | 458 | /* loop over to deal with the last block */ |
|---|
| 459 | for (i = 1; i <= count; i ++) { |
|---|
| 460 | if (i < count && id == bbids[i]) { |
|---|
| [312] | 461 | continue; |
|---|
| 462 | } |
|---|
| [308] | 463 | |
|---|
| [326] | 464 | opline = op_array->opcodes + start; |
|---|
| 465 | pbb = bbs_new_bb_ex(bbs, opline, i - start); |
|---|
| [657] | 466 | #ifdef ZEND_ENGINE_2 |
|---|
| [326] | 467 | pbb->catch = catchbbids[start]; |
|---|
| [657] | 468 | #endif |
|---|
| [326] | 469 | |
|---|
| 470 | /* last */ |
|---|
| 471 | opline = pbb->opcodes + pbb->count - 1; |
|---|
| 472 | if (op_get_flowinfo(&fi, opline) == SUCCESS) { |
|---|
| 473 | if (fi.jmpout_op1 != XC_OPNUM_INVALID) { |
|---|
| [716] | 474 | Z_OP(opline->op1).opline_num = bbids[fi.jmpout_op1]; |
|---|
| 475 | assert(Z_OP(opline->op1).opline_num != BBID_INVALID); |
|---|
| [312] | 476 | } |
|---|
| [326] | 477 | if (fi.jmpout_op2 != XC_OPNUM_INVALID) { |
|---|
| [716] | 478 | Z_OP(opline->op2).opline_num = bbids[fi.jmpout_op2]; |
|---|
| 479 | assert(Z_OP(opline->op2).opline_num != BBID_INVALID); |
|---|
| [312] | 480 | } |
|---|
| [326] | 481 | if (fi.jmpout_ext != XC_OPNUM_INVALID) { |
|---|
| 482 | opline->extended_value = bbids[fi.jmpout_ext]; |
|---|
| 483 | assert(opline->extended_value != BBID_INVALID); |
|---|
| [312] | 484 | } |
|---|
| [326] | 485 | if (fi.fall && i + 1 < count) { |
|---|
| [312] | 486 | pbb->fall = bbids[i + 1]; |
|---|
| [326] | 487 | TRACE("fall %d", pbb->fall); |
|---|
| [312] | 488 | assert(pbb->fall != BBID_INVALID); |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| [326] | 491 | if (i >= count) { |
|---|
| 492 | break; |
|---|
| 493 | } |
|---|
| [332] | 494 | start = i; |
|---|
| [326] | 495 | id = bbids[i]; |
|---|
| [312] | 496 | } |
|---|
| 497 | /* }}} */ |
|---|
| 498 | |
|---|
| [485] | 499 | my_free_alloca(markbbhead, use_heap_markbbhead); |
|---|
| [657] | 500 | #ifdef ZEND_ENGINE_2 |
|---|
| [485] | 501 | my_free_alloca(catchbbids, use_heap_catchbbids); |
|---|
| [657] | 502 | #endif |
|---|
| [485] | 503 | my_free_alloca(bbids, use_heap_bbids); |
|---|
| [308] | 504 | return SUCCESS; |
|---|
| 505 | } |
|---|
| 506 | /* }}} */ |
|---|
| [330] | 507 | static void bbs_restore_opnum(bbs_t *bbs, zend_op_array *op_array) /* {{{ */ |
|---|
| [326] | 508 | { |
|---|
| 509 | int i; |
|---|
| [657] | 510 | #ifdef ZEND_ENGINE_2 |
|---|
| [330] | 511 | bbid_t lasttrybbid; |
|---|
| [369] | 512 | bbid_t lastcatchbbid; |
|---|
| [657] | 513 | #endif |
|---|
| [330] | 514 | |
|---|
| [326] | 515 | for (i = 0; i < bbs_count(bbs); i ++) { |
|---|
| 516 | op_flowinfo_t fi; |
|---|
| 517 | bb_t *bb = bbs_get(bbs, i); |
|---|
| 518 | zend_op *last = bb->opcodes + bb->count - 1; |
|---|
| [308] | 519 | |
|---|
| [326] | 520 | if (op_get_flowinfo(&fi, last) == SUCCESS) { |
|---|
| 521 | if (fi.jmpout_op1 != XC_OPNUM_INVALID) { |
|---|
| [716] | 522 | Z_OP(last->op1).opline_num = bbs_get(bbs, fi.jmpout_op1)->opnum; |
|---|
| 523 | assert(Z_OP(last->op1).opline_num != BBID_INVALID); |
|---|
| [326] | 524 | } |
|---|
| 525 | if (fi.jmpout_op2 != XC_OPNUM_INVALID) { |
|---|
| [716] | 526 | Z_OP(last->op2).opline_num = bbs_get(bbs, fi.jmpout_op2)->opnum; |
|---|
| 527 | assert(Z_OP(last->op2).opline_num != BBID_INVALID); |
|---|
| [326] | 528 | } |
|---|
| 529 | if (fi.jmpout_ext != XC_OPNUM_INVALID) { |
|---|
| 530 | last->extended_value = bbs_get(bbs, fi.jmpout_ext)->opnum; |
|---|
| 531 | assert(last->extended_value != BBID_INVALID); |
|---|
| 532 | } |
|---|
| 533 | } |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| [657] | 536 | #ifdef ZEND_ENGINE_2 |
|---|
| [369] | 537 | lasttrybbid = BBID_INVALID; |
|---|
| 538 | lastcatchbbid = BBID_INVALID; |
|---|
| [330] | 539 | op_array->last_try_catch = 0; |
|---|
| 540 | for (i = 0; i < bbs_count(bbs); i ++) { |
|---|
| 541 | bb_t *bb = bbs_get(bbs, i); |
|---|
| 542 | |
|---|
| [369] | 543 | if (lastcatchbbid != bb->catch) { |
|---|
| 544 | if (lasttrybbid != BBID_INVALID && lastcatchbbid != BBID_INVALID) { |
|---|
| [330] | 545 | int try_catch_offset = op_array->last_try_catch ++; |
|---|
| 546 | |
|---|
| 547 | op_array->try_catch_array = erealloc(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch); |
|---|
| 548 | op_array->try_catch_array[try_catch_offset].try_op = bbs_get(bbs, lasttrybbid)->opnum; |
|---|
| [369] | 549 | op_array->try_catch_array[try_catch_offset].catch_op = bbs_get(bbs, lastcatchbbid)->opnum; |
|---|
| [330] | 550 | } |
|---|
| [369] | 551 | lasttrybbid = i; |
|---|
| 552 | lastcatchbbid = bb->catch; |
|---|
| [330] | 553 | } |
|---|
| 554 | } |
|---|
| 555 | /* it is impossible to have last bb catched */ |
|---|
| [657] | 556 | #endif |
|---|
| [326] | 557 | } |
|---|
| 558 | /* }}} */ |
|---|
| 559 | |
|---|
| [308] | 560 | /* |
|---|
| 561 | * optimize |
|---|
| 562 | */ |
|---|
| [1] | 563 | static int xc_optimize_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 564 | { |
|---|
| [308] | 565 | bbs_t bbs; |
|---|
| 566 | |
|---|
| [1] | 567 | if (op_array->type != ZEND_USER_FUNCTION) { |
|---|
| 568 | return 0; |
|---|
| 569 | } |
|---|
| [335] | 570 | |
|---|
| [543] | 571 | #ifdef XCACHE_DEBUG |
|---|
| [312] | 572 | # if 0 |
|---|
| [308] | 573 | TRACE("optimize file: %s", op_array->filename); |
|---|
| 574 | xc_dprint_zend_op_array(op_array, 0 TSRMLS_CC); |
|---|
| [312] | 575 | # endif |
|---|
| [331] | 576 | op_print(0, op_array->opcodes, op_array->opcodes + op_array->last); |
|---|
| [308] | 577 | #endif |
|---|
| 578 | |
|---|
| [312] | 579 | if (op_array_convert_switch(op_array) == SUCCESS) { |
|---|
| [308] | 580 | bbs_init(&bbs); |
|---|
| [326] | 581 | if (bbs_build_from(&bbs, op_array, op_array->last) == SUCCESS) { |
|---|
| 582 | int i; |
|---|
| [543] | 583 | #ifdef XCACHE_DEBUG |
|---|
| [312] | 584 | bbs_print(&bbs, op_array->opcodes); |
|---|
| 585 | #endif |
|---|
| [326] | 586 | /* TODO: calc opnum after basic block move */ |
|---|
| 587 | for (i = 0; i < bbs_count(&bbs); i ++) { |
|---|
| 588 | bb_t *bb = bbs_get(&bbs, i); |
|---|
| 589 | bb->opnum = bb->opcodes - op_array->opcodes; |
|---|
| 590 | } |
|---|
| [330] | 591 | bbs_restore_opnum(&bbs, op_array); |
|---|
| [308] | 592 | } |
|---|
| 593 | bbs_destroy(&bbs); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| [543] | 596 | #ifdef XCACHE_DEBUG |
|---|
| [312] | 597 | # if 0 |
|---|
| [308] | 598 | TRACE("%s", "after compiles"); |
|---|
| 599 | xc_dprint_zend_op_array(op_array, 0 TSRMLS_CC); |
|---|
| [312] | 600 | # endif |
|---|
| [369] | 601 | op_print(0, op_array->opcodes, op_array->opcodes + op_array->last); |
|---|
| [308] | 602 | #endif |
|---|
| [1] | 603 | return 0; |
|---|
| 604 | } |
|---|
| 605 | /* }}} */ |
|---|
| [477] | 606 | void xc_optimizer_op_array_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| [335] | 607 | { |
|---|
| [477] | 608 | TSRMLS_FETCH(); |
|---|
| 609 | if (XG(optimizer)) { |
|---|
| 610 | xc_optimize_op_array(op_array TSRMLS_CC); |
|---|
| [316] | 611 | } |
|---|
| [1] | 612 | } |
|---|
| 613 | /* }}} */ |
|---|