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