1/*
2   +----------------------------------------------------------------------+
3   | Zend OPcache                                                         |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 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   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Andi Gutmans <andi@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   |          Stanislav Malyshev <stas@zend.com>                          |
18   |          Dmitry Stogov <dmitry@zend.com>                             |
19   +----------------------------------------------------------------------+
20*/
21
22#include "php.h"
23#include "Optimizer/zend_optimizer.h"
24#include "Optimizer/zend_optimizer_internal.h"
25#include "zend_API.h"
26#include "zend_constants.h"
27#include "zend_execute.h"
28#include "zend_vm.h"
29
30#define DEBUG_BLOCKPASS 0
31
32/* Checks if a constant (like "true") may be replaced by its value */
33int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
34{
35    zend_constant *c;
36    char *lookup_name;
37    int retval = 1;
38    ALLOCA_FLAG(use_heap);
39
40    if ((c = zend_hash_find_ptr(EG(zend_constants), name)) == NULL) {
41        lookup_name = DO_ALLOCA(name->len + 1);
42        memcpy(lookup_name, name->val, name->len + 1);
43        zend_str_tolower(lookup_name, name->len);
44
45        if ((c = zend_hash_str_find_ptr(EG(zend_constants), lookup_name, name->len)) != NULL) {
46            if (!(c->flags & CONST_CT_SUBST) || (c->flags & CONST_CS)) {
47                retval = 0;
48            }
49        } else {
50            retval = 0;
51        }
52        FREE_ALLOCA(lookup_name);
53    }
54
55    if (retval) {
56        if (c->flags & CONST_PERSISTENT) {
57            ZVAL_COPY_VALUE(result, &c->value);
58            if (copy) {
59                zval_copy_ctor(result);
60            }
61        } else {
62            retval = 0;
63        }
64    }
65
66    return retval;
67}
68
69#if DEBUG_BLOCKPASS
70# define BLOCK_REF(b) b?op_array->opcodes-b->start_opline:-1
71
72static inline void print_block(zend_code_block *block, zend_op *opcodes, char *txt)
73{
74    fprintf(stderr, "%sBlock: %d-%d (%d)", txt, block->start_opline - opcodes, block->start_opline - opcodes + block->len - 1, block->len);
75    if (!block->access) {
76        fprintf(stderr, " unused");
77    }
78    if (block->op1_to) {
79        fprintf(stderr, " 1: %d", block->op1_to->start_opline - opcodes);
80    }
81    if (block->op2_to) {
82        fprintf(stderr, " 2: %d", block->op2_to->start_opline - opcodes);
83    }
84    if (block->ext_to) {
85        fprintf(stderr, " e: %d", block->ext_to->start_opline - opcodes);
86    }
87    if (block->follow_to) {
88        fprintf(stderr, " f: %d", block->follow_to->start_opline - opcodes);
89    }
90
91    if (block->sources) {
92        zend_block_source *bs = block->sources;
93        fprintf(stderr, " s:");
94        while (bs) {
95            fprintf(stderr, " %d", bs->from->start_opline - opcodes);
96            bs = bs->next;
97        }
98    }
99
100    fprintf(stderr, "\n");
101    fflush(stderr);
102}
103#else
104#define print_block(a,b,c)
105#endif
106
107#define START_BLOCK_OP(opno) blocks[opno].start_opline = &op_array->opcodes[opno]; blocks[opno].start_opline_no = opno; blocks[opno].access = 1
108
109/* find code blocks in op_array
110   code block is a set of opcodes with single flow of control, i.e. without jmps,
111   branches, etc. */
112static int find_code_blocks(zend_op_array *op_array, zend_cfg *cfg, zend_optimizer_ctx *ctx)
113{
114    zend_op *opline;
115    zend_op *end = op_array->opcodes + op_array->last;
116    zend_code_block *blocks, *cur_block;
117    uint32_t opno = 0;
118
119    memset(cfg, 0, sizeof(zend_cfg));
120    blocks = cfg->blocks = zend_arena_calloc(&ctx->arena, op_array->last + 2, sizeof(zend_code_block));
121    opline = op_array->opcodes;
122    blocks[0].start_opline = opline;
123    blocks[0].start_opline_no = 0;
124    while (opline < end) {
125        switch((unsigned)opline->opcode) {
126            case ZEND_BRK:
127            case ZEND_CONT:
128            case ZEND_GOTO:
129                /* would not optimize non-optimized BRK/CONTs - we cannot
130                 really know where it jumps, so these optimizations are
131                too dangerous */
132                return 0;
133            case ZEND_FAST_CALL:
134                START_BLOCK_OP(ZEND_OP1(opline).opline_num);
135                if (opline->extended_value) {
136                    START_BLOCK_OP(ZEND_OP2(opline).opline_num);
137                }
138                START_BLOCK_OP(opno + 1);
139                break;
140            case ZEND_FAST_RET:
141                if (opline->extended_value) {
142                    START_BLOCK_OP(ZEND_OP2(opline).opline_num);
143                }
144                START_BLOCK_OP(opno + 1);
145                break;
146            case ZEND_JMP:
147                START_BLOCK_OP(ZEND_OP1(opline).opline_num);
148                /* break missing intentionally */
149            case ZEND_RETURN:
150            case ZEND_RETURN_BY_REF:
151            case ZEND_GENERATOR_RETURN:
152            case ZEND_EXIT:
153            case ZEND_THROW:
154                /* start new block from this+1 */
155                START_BLOCK_OP(opno + 1);
156                break;
157                /* TODO: if conditional jmp depends on constant,
158                         don't start block that won't be executed */
159            case ZEND_CATCH:
160                START_BLOCK_OP(opline->extended_value);
161                START_BLOCK_OP(opno + 1);
162                break;
163            case ZEND_JMPZNZ:
164                START_BLOCK_OP(opline->extended_value);
165            case ZEND_JMPZ:
166            case ZEND_JMPNZ:
167            case ZEND_JMPZ_EX:
168            case ZEND_JMPNZ_EX:
169            case ZEND_FE_RESET:
170            case ZEND_NEW:
171            case ZEND_JMP_SET:
172            case ZEND_COALESCE:
173                START_BLOCK_OP(ZEND_OP2(opline).opline_num);
174                START_BLOCK_OP(opno + 1);
175                break;
176            case ZEND_FE_FETCH:
177                START_BLOCK_OP(ZEND_OP2(opline).opline_num);
178                START_BLOCK_OP(opno + 2);
179                break;
180        }
181        opno++;
182        opline++;
183    }
184
185    /* first find block start points */
186    if (op_array->last_try_catch) {
187        int i;
188        cfg->try = zend_arena_calloc(&ctx->arena, op_array->last_try_catch, sizeof(zend_code_block *));
189        cfg->catch = zend_arena_calloc(&ctx->arena, op_array->last_try_catch, sizeof(zend_code_block *));
190        for (i = 0; i< op_array->last_try_catch; i++) {
191            cfg->try[i] = &blocks[op_array->try_catch_array[i].try_op];
192            cfg->catch[i] = &blocks[op_array->try_catch_array[i].catch_op];
193            START_BLOCK_OP(op_array->try_catch_array[i].try_op);
194            START_BLOCK_OP(op_array->try_catch_array[i].catch_op);
195            blocks[op_array->try_catch_array[i].try_op].protected = 1;
196        }
197    }
198    /* Currently, we don't optimize op_arrays with BRK/CONT/GOTO opcodes,
199     * but, we have to keep brk_cont_array to avoid memory leaks during
200     * exception handling */
201    if (op_array->last_brk_cont) {
202        int i, j;
203
204        j = 0;
205        for (i = 0; i< op_array->last_brk_cont; i++) {
206            if (op_array->brk_cont_array[i].start >= 0 &&
207                (op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_FREE ||
208                 op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_END_SILENCE)) {
209                int parent = op_array->brk_cont_array[i].parent;
210
211                while (parent >= 0 &&
212                       op_array->brk_cont_array[parent].start < 0 &&
213                       op_array->opcodes[op_array->brk_cont_array[parent].brk].opcode != ZEND_FREE) {
214                    parent = op_array->brk_cont_array[parent].parent;
215                }
216                op_array->brk_cont_array[i].parent = parent;
217                j++;
218            }
219        }
220        if (j) {
221            cfg->loop_start = zend_arena_calloc(&ctx->arena, op_array->last_brk_cont, sizeof(zend_code_block *));
222            cfg->loop_cont  = zend_arena_calloc(&ctx->arena, op_array->last_brk_cont, sizeof(zend_code_block *));
223            cfg->loop_brk   = zend_arena_calloc(&ctx->arena, op_array->last_brk_cont, sizeof(zend_code_block *));
224            j = 0;
225            for (i = 0; i< op_array->last_brk_cont; i++) {
226                if (op_array->brk_cont_array[i].start >= 0 &&
227                    (op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_FREE ||
228                     op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_END_SILENCE)) {
229                    if (i != j) {
230                        op_array->brk_cont_array[j] = op_array->brk_cont_array[i];
231                    }
232                    cfg->loop_start[j] = &blocks[op_array->brk_cont_array[j].start];
233                    cfg->loop_cont[j]  = &blocks[op_array->brk_cont_array[j].cont];
234                    cfg->loop_brk[j]   = &blocks[op_array->brk_cont_array[j].brk];
235                    START_BLOCK_OP(op_array->brk_cont_array[j].start);
236                    START_BLOCK_OP(op_array->brk_cont_array[j].cont);
237                    START_BLOCK_OP(op_array->brk_cont_array[j].brk);
238                    blocks[op_array->brk_cont_array[j].start].protected = 1;
239                    blocks[op_array->brk_cont_array[j].brk].protected = 1;
240                    j++;
241                }
242            }
243            op_array->last_brk_cont = j;
244        } else {
245            efree(op_array->brk_cont_array);
246            op_array->brk_cont_array = NULL;
247            op_array->last_brk_cont = 0;
248        }
249    }
250
251    /* Build CFG (Control Flow Graph) */
252    cur_block = blocks;
253    for (opno = 1; opno < op_array->last; opno++) {
254        if (blocks[opno].start_opline) {
255            /* found new block start */
256            cur_block->len = blocks[opno].start_opline - cur_block->start_opline;
257            cur_block->next = &blocks[opno];
258            /* what is the last OP of previous block? */
259            opline = blocks[opno].start_opline - 1;
260            if (opline->opcode == ZEND_OP_DATA) {
261                opline--;
262            }
263            switch((unsigned)opline->opcode) {
264                case ZEND_RETURN:
265                case ZEND_RETURN_BY_REF:
266                case ZEND_GENERATOR_RETURN:
267                case ZEND_EXIT:
268                case ZEND_THROW:
269                    break;
270                case ZEND_FAST_CALL:
271                    if (opline->extended_value) {
272                        cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
273                    }
274                    cur_block->op1_to = &blocks[ZEND_OP1(opline).opline_num];
275                    break;
276                case ZEND_FAST_RET:
277                    if (opline->extended_value) {
278                        cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
279                    }
280                    break;
281                case ZEND_JMP:
282                    cur_block->op1_to = &blocks[ZEND_OP1(opline).opline_num];
283                    break;
284                case ZEND_JMPZNZ:
285                    cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
286                    cur_block->ext_to = &blocks[opline->extended_value];
287                    break;
288                case ZEND_CATCH:
289                    cur_block->ext_to = &blocks[opline->extended_value];
290                    cur_block->follow_to = &blocks[opno];
291                    break;
292                case ZEND_JMPZ:
293                case ZEND_JMPNZ:
294                case ZEND_JMPZ_EX:
295                case ZEND_JMPNZ_EX:
296                case ZEND_FE_RESET:
297                case ZEND_NEW:
298                case ZEND_JMP_SET:
299                case ZEND_COALESCE:
300                case ZEND_FE_FETCH:
301                    cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
302                    /* break missing intentionally */
303                default:
304                    /* next block follows this */
305                    cur_block->follow_to = &blocks[opno];
306                    break;
307            }
308            print_block(cur_block, op_array->opcodes, "");
309            cur_block = cur_block->next;
310        }
311    }
312    cur_block->len = end - cur_block->start_opline;
313    cur_block->next = &blocks[op_array->last + 1];
314    print_block(cur_block, op_array->opcodes, "");
315
316    return 1;
317}
318
319/* CFG back references management */
320
321#define ADD_SOURCE(fromb, tob) { \
322    zend_block_source *__s = tob->sources; \
323    while (__s && __s->from != fromb) __s = __s->next; \
324    if (__s == NULL) { \
325        zend_block_source *__t = zend_arena_alloc(&ctx->arena, sizeof(zend_block_source)); \
326        __t->next = tob->sources; \
327        tob->sources = __t; \
328        __t->from = fromb; \
329    } \
330}
331
332#define DEL_SOURCE(cs) do { \
333        *(cs) = (*(cs))->next; \
334    } while (0)
335
336
337static inline void replace_source(zend_block_source *list, zend_code_block *old, zend_code_block *new)
338{
339    /* replace all references to 'old' in 'list' with 'new' */
340    zend_block_source **cs;
341    int found = 0;
342
343    for (cs = &list; *cs; cs = &((*cs)->next)) {
344        if ((*cs)->from == new) {
345            if (found) {
346                DEL_SOURCE(cs);
347            } else {
348                found = 1;
349            }
350        }
351
352        if ((*cs)->from == old) {
353            if (found) {
354                DEL_SOURCE(cs);
355            } else {
356                (*cs)->from = new;
357                found = 1;
358            }
359        }
360    }
361}
362
363static inline void del_source(zend_code_block *from, zend_code_block *to)
364{
365    /* delete source 'from' from 'to'-s sources list */
366    zend_block_source **cs = &to->sources;
367
368    if (to->sources == NULL) {
369        to->access = 0;
370        return;
371    }
372
373    while (*cs) {
374        if ((*cs)->from == from) {
375            DEL_SOURCE(cs);
376            break;
377        }
378        cs = &((*cs)->next);
379    }
380
381    if (to->sources == NULL) {
382        /* 'to' has no more sources - it's unused, will be stripped */
383        to->access = 0;
384        return;
385    }
386
387    if (to->sources->next == NULL) {
388        /* source to only one block */
389        zend_code_block *from_block = to->sources->from;
390
391        if (from_block->access && from_block->follow_to == to &&
392            from_block->op1_to == NULL &&
393            from_block->op2_to == NULL &&
394            from_block->ext_to == NULL) {
395            /* this block follows it's only predecessor - we can join them */
396            zend_op *new_to = from_block->start_opline + from_block->len;
397            if (new_to != to->start_opline) {
398                /* move block to new location */
399                memmove(new_to, to->start_opline, sizeof(zend_op)*to->len);
400            }
401            /* join blocks' lengths */
402            from_block->len += to->len;
403            /* move 'to'`s references to 'from' */
404            to->start_opline = NULL;
405            to->access = 0;
406            to->sources = NULL;
407            from_block->follow_to = to->follow_to;
408            if (to->op1_to) {
409                from_block->op1_to = to->op1_to;
410                replace_source(to->op1_to->sources, to, from_block);
411            }
412            if (to->op2_to) {
413                from_block->op2_to = to->op2_to;
414                replace_source(to->op2_to->sources, to, from_block);
415            }
416            if (to->ext_to) {
417                from_block->ext_to = to->ext_to;
418                replace_source(to->ext_to->sources, to, from_block);
419            }
420            if (to->follow_to) {
421                replace_source(to->follow_to->sources, to, from_block);
422            }
423            /* remove "to" from list */
424        }
425    }
426}
427
428static void delete_code_block(zend_code_block *block, zend_optimizer_ctx *ctx)
429{
430    if (block->protected) {
431        return;
432    }
433    if (block->follow_to) {
434        zend_block_source *bs = block->sources;
435        while (bs) {
436            zend_code_block *from_block = bs->from;
437            zend_code_block *to = block->follow_to;
438            if (from_block->op1_to == block) {
439                from_block->op1_to = to;
440                ADD_SOURCE(from_block, to);
441            }
442            if (from_block->op2_to == block) {
443                from_block->op2_to = to;
444                ADD_SOURCE(from_block, to);
445            }
446            if (from_block->ext_to == block) {
447                from_block->ext_to = to;
448                ADD_SOURCE(from_block, to);
449            }
450            if (from_block->follow_to == block) {
451                from_block->follow_to = to;
452                ADD_SOURCE(from_block, to);
453            }
454            bs = bs->next;
455        }
456    }
457    block->access = 0;
458}
459
460static void zend_access_path(zend_code_block *block, zend_optimizer_ctx *ctx)
461{
462    if (block->access) {
463        return;
464    }
465
466    block->access = 1;
467    if (block->op1_to) {
468        zend_access_path(block->op1_to, ctx);
469        ADD_SOURCE(block, block->op1_to);
470    }
471    if (block->op2_to) {
472        zend_access_path(block->op2_to, ctx);
473        ADD_SOURCE(block, block->op2_to);
474    }
475    if (block->ext_to) {
476        zend_access_path(block->ext_to, ctx);
477        ADD_SOURCE(block, block->ext_to);
478    }
479    if (block->follow_to) {
480        zend_access_path(block->follow_to, ctx);
481        ADD_SOURCE(block, block->follow_to);
482    }
483}
484
485/* Traverse CFG, mark reachable basic blocks and build back references */
486static void zend_rebuild_access_path(zend_cfg *cfg, zend_op_array *op_array, int find_start, zend_optimizer_ctx *ctx)
487{
488    zend_code_block *blocks = cfg->blocks;
489    zend_code_block *start = find_start? NULL : blocks;
490    zend_code_block *b;
491
492    /* Mark all blocks as unaccessible and destroy back references */
493    b = blocks;
494    while (b != NULL) {
495        if (!start && b->access) {
496            start = b;
497        }
498        b->access = 0;
499        b->sources = NULL;
500        b = b->next;
501    }
502
503    /* Walk thorough all paths */
504    zend_access_path(start, ctx);
505
506    /* Add brk/cont paths */
507    if (op_array->last_brk_cont) {
508        int i;
509        for (i=0; i< op_array->last_brk_cont; i++) {
510            zend_access_path(cfg->loop_start[i], ctx);
511            zend_access_path(cfg->loop_cont[i], ctx);
512            zend_access_path(cfg->loop_brk[i], ctx);
513        }
514    }
515
516    /* Add exception paths */
517    if (op_array->last_try_catch) {
518        int i;
519        for (i=0; i< op_array->last_try_catch; i++) {
520            if (!cfg->catch[i]->access) {
521                zend_access_path(cfg->catch[i], ctx);
522            }
523        }
524    }
525}
526
527/* Data dependencies macros */
528
529#define VAR_NUM_EX(op) VAR_NUM((op).var)
530
531#define VAR_SOURCE(op) Tsource[VAR_NUM(op.var)]
532#define SET_VAR_SOURCE(opline) Tsource[VAR_NUM(opline->result.var)] = opline
533
534#define VAR_UNSET(op) do { if (op ## _type & (IS_TMP_VAR|IS_VAR)) {VAR_SOURCE(op) = NULL;}} while (0)
535
536#define convert_to_string_safe(v) \
537    if (Z_TYPE_P((v)) == IS_NULL) { \
538        ZVAL_STRINGL((v), "", 0); \
539    } else { \
540        convert_to_string((v)); \
541    }
542
543static void strip_nop(zend_code_block *block, zend_optimizer_ctx *ctx)
544{
545    zend_op *opline = block->start_opline;
546    zend_op *end, *new_end;
547
548    /* remove leading NOPs */
549    while (block->len > 0 && block->start_opline->opcode == ZEND_NOP) {
550        if (block->len == 1) {
551            /* this block is all NOPs, join with following block */
552            if (block->follow_to) {
553                delete_code_block(block, ctx);
554            }
555            return;
556        }
557        block->start_opline++;
558        block->start_opline_no++;
559        block->len--;
560    }
561
562    /* strip the inside NOPs */
563    opline = new_end = block->start_opline;
564    end = opline + block->len;
565
566    while (opline < end) {
567        zend_op *src;
568        int len = 0;
569
570        while (opline < end && opline->opcode == ZEND_NOP) {
571            opline++;
572        }
573        src = opline;
574
575        while (opline < end && opline->opcode != ZEND_NOP) {
576            opline++;
577        }
578        len = opline - src;
579
580        /* move up non-NOP opcodes */
581        memmove(new_end, src, len*sizeof(zend_op));
582
583        new_end += len;
584    }
585    block->len = new_end - block->start_opline;
586}
587
588static void zend_optimize_block(zend_code_block *block, zend_op_array *op_array, char *used_ext, zend_cfg *cfg, zend_optimizer_ctx *ctx)
589{
590    zend_op *opline = block->start_opline;
591    zend_op *end, *last_op = NULL;
592    zend_op **Tsource = cfg->Tsource;
593
594    print_block(block, op_array->opcodes, "Opt ");
595
596    /* remove leading NOPs */
597    while (block->len > 0 && block->start_opline->opcode == ZEND_NOP) {
598        if (block->len == 1) {
599            /* this block is all NOPs, join with following block */
600            if (block->follow_to) {
601                delete_code_block(block, ctx);
602            }
603            return;
604        }
605        block->start_opline++;
606        block->start_opline_no++;
607        block->len--;
608    }
609
610    /* we track data dependencies only insight a single basic block */
611    memset(Tsource, 0, (op_array->last_var + op_array->T) * sizeof(zend_op *));
612    opline = block->start_opline;
613    end = opline + block->len;
614    while ((op_array->T) && (opline < end)) {
615        /* strip X = QM_ASSIGN(const) */
616        if ((ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
617            VAR_SOURCE(opline->op1) &&
618            VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN &&
619            ZEND_OP1_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
620            opline->opcode != ZEND_CASE &&         /* CASE _always_ expects variable */
621            opline->opcode != ZEND_FETCH_LIST &&
622            opline->opcode != ZEND_FE_RESET &&
623            opline->opcode != ZEND_FREE
624            ) {
625            zend_op *src = VAR_SOURCE(opline->op1);
626            zval c = ZEND_OP1_LITERAL(src);
627            VAR_UNSET(opline->op1);
628            zval_copy_ctor(&c);
629            zend_optimizer_update_op1_const(op_array, opline, &c);
630            literal_dtor(&ZEND_OP1_LITERAL(src));
631            MAKE_NOP(src);
632        }
633
634        /* T = QM_ASSIGN(C), F(T) => NOP, F(C) */
635        if ((ZEND_OP2_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
636            VAR_SOURCE(opline->op2) &&
637            VAR_SOURCE(opline->op2)->opcode == ZEND_QM_ASSIGN &&
638            ZEND_OP1_TYPE(VAR_SOURCE(opline->op2)) == IS_CONST) {
639            zend_op *src = VAR_SOURCE(opline->op2);
640            zval c = ZEND_OP1_LITERAL(src);
641            VAR_UNSET(opline->op2);
642            zval_copy_ctor(&c);
643            zend_optimizer_update_op2_const(op_array, opline, &c);
644            literal_dtor(&ZEND_OP1_LITERAL(src));
645            MAKE_NOP(src);
646        }
647
648        /* T = CAST(X, String), ECHO(T) => NOP, ECHO(X) */
649        if (opline->opcode == ZEND_ECHO &&
650            ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR) &&
651            VAR_SOURCE(opline->op1) &&
652            VAR_SOURCE(opline->op1)->opcode == ZEND_CAST &&
653            VAR_SOURCE(opline->op1)->extended_value == IS_STRING) {
654            zend_op *src = VAR_SOURCE(opline->op1);
655            COPY_NODE(opline->op1, src->op1);
656            MAKE_NOP(src);
657        }
658
659       /* T = BOOL(X), FREE(T) => NOP */
660        if (opline->opcode == ZEND_FREE &&
661            ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
662            VAR_SOURCE(opline->op1)) {
663            zend_op *src = VAR_SOURCE(opline->op1);
664            if (src->opcode == ZEND_BOOL) {
665                if (ZEND_OP1_TYPE(src) == IS_CONST) {
666                    literal_dtor(&ZEND_OP1_LITERAL(src));
667                }
668                MAKE_NOP(src);
669                MAKE_NOP(opline);
670            }
671        }
672
673#if 0
674        /* pre-evaluate functions:
675           constant(x)
676           defined(x)
677           function_exists(x)
678           extension_loaded(x)
679           BAD: interacts badly with Accelerator
680        */
681        if((ZEND_OP1_TYPE(opline) & IS_VAR) &&
682           VAR_SOURCE(opline->op1) && VAR_SOURCE(opline->op1)->opcode == ZEND_DO_CF_FCALL &&
683           VAR_SOURCE(opline->op1)->extended_value == 1) {
684            zend_op *fcall = VAR_SOURCE(opline->op1);
685            zend_op *sv = fcall-1;
686            if(sv >= block->start_opline && sv->opcode == ZEND_SEND_VAL &&
687               ZEND_OP1_TYPE(sv) == IS_CONST && Z_TYPE(OPLINE_OP1_LITERAL(sv)) == IS_STRING &&
688               Z_LVAL(OPLINE_OP2_LITERAL(sv)) == 1
689               ) {
690                zval *arg = &OPLINE_OP1_LITERAL(sv);
691                char *fname = FUNCTION_CACHE->funcs[Z_LVAL(ZEND_OP1_LITERAL(fcall))].function_name;
692                int flen = FUNCTION_CACHE->funcs[Z_LVAL(ZEND_OP1_LITERAL(fcall))].name_len;
693                if(flen == sizeof("defined")-1 && zend_binary_strcasecmp(fname, flen, "defined", sizeof("defined")-1) == 0) {
694                    zval c;
695                    if(zend_optimizer_get_persistent_constant(Z_STR_P(arg), &c, 0 ELS_CC) != 0) {
696                        literal_dtor(arg);
697                        MAKE_NOP(sv);
698                        MAKE_NOP(fcall);
699                        LITERAL_BOOL(opline->op1, 1);
700                        ZEND_OP1_TYPE(opline) = IS_CONST;
701                    }
702                } else if((flen == sizeof("function_exists")-1 && zend_binary_strcasecmp(fname, flen, "function_exists", sizeof("function_exists")-1) == 0) ||
703                          (flen == sizeof("is_callable")-1 && zend_binary_strcasecmp(fname, flen, "is_callable", sizeof("is_callable")-1) == 0)
704                          ) {
705                    zend_function *function;
706                    if((function = zend_hash_find_ptr(EG(function_table), Z_STR_P(arg))) != NULL) {
707                        literal_dtor(arg);
708                        MAKE_NOP(sv);
709                        MAKE_NOP(fcall);
710                        LITERAL_BOOL(opline->op1, 1);
711                        ZEND_OP1_TYPE(opline) = IS_CONST;
712                    }
713                } else if(flen == sizeof("constant")-1 && zend_binary_strcasecmp(fname, flen, "constant", sizeof("constant")-1) == 0) {
714                    zval c;
715                    if(zend_optimizer_get_persistent_constant(Z_STR_P(arg), &c, 1 ELS_CC) != 0) {
716                        literal_dtor(arg);
717                        MAKE_NOP(sv);
718                        MAKE_NOP(fcall);
719                        ZEND_OP1_LITERAL(opline) = zend_optimizer_add_literal(op_array, &c);
720                        /* no copy ctor - get already copied it */
721                        ZEND_OP1_TYPE(opline) = IS_CONST;
722                    }
723                } else if(flen == sizeof("extension_loaded")-1 && zend_binary_strcasecmp(fname, flen, "extension_loaded", sizeof("extension_loaded")-1) == 0) {
724                    if(zend_hash_exists(&module_registry, Z_STR_P(arg))) {
725                        literal_dtor(arg);
726                        MAKE_NOP(sv);
727                        MAKE_NOP(fcall);
728                        LITERAL_BOOL(opline->op1, 1);
729                        ZEND_OP1_TYPE(opline) = IS_CONST;
730                    }
731                }
732            }
733        }
734#endif
735
736        /* IS_EQ(TRUE, X)      => BOOL(X)
737         * IS_EQ(FALSE, X)     => BOOL_NOT(X)
738         * IS_NOT_EQ(TRUE, X)  => BOOL_NOT(X)
739         * IS_NOT_EQ(FALSE, X) => BOOL(X)
740         * CASE(TRUE, X)       => BOOL(X)
741         * CASE(FALSE, X)      => BOOL_NOT(X)
742         */
743        if (opline->opcode == ZEND_IS_EQUAL ||
744            opline->opcode == ZEND_IS_NOT_EQUAL ||
745            opline->opcode == ZEND_CASE) {
746            if (ZEND_OP1_TYPE(opline) == IS_CONST &&
747// TODO: Optimization of comparison with null may be not safe ???
748#if 1
749                (Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_FALSE ||
750                 Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_TRUE)) {
751#else
752                 Z_TYPE(ZEND_OP1_LITERAL(opline)) <= IS_TRUE) {
753#endif
754                opline->opcode =
755                    ((opline->opcode != ZEND_IS_NOT_EQUAL) == ((Z_TYPE(ZEND_OP1_LITERAL(opline))) == IS_TRUE)) ?
756                    ZEND_BOOL : ZEND_BOOL_NOT;
757                COPY_NODE(opline->op1, opline->op2);
758                SET_UNUSED(opline->op2);
759            } else if (ZEND_OP2_TYPE(opline) == IS_CONST &&
760// TODO: Optimization of comparison with null may be not safe ???
761#if 1
762                       (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_FALSE ||
763                        Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_TRUE)) {
764#else
765                        Z_TYPE(ZEND_OP2_LITERAL(opline)) <= IS_TRUE) {
766#endif
767                opline->opcode =
768                    ((opline->opcode != ZEND_IS_NOT_EQUAL) == ((Z_TYPE(ZEND_OP2_LITERAL(opline))) == IS_TRUE)) ?
769                    ZEND_BOOL : ZEND_BOOL_NOT;
770                SET_UNUSED(opline->op2);
771            }
772        }
773
774        if ((opline->opcode == ZEND_BOOL ||
775            opline->opcode == ZEND_BOOL_NOT ||
776            opline->opcode == ZEND_JMPZ ||
777            opline->opcode == ZEND_JMPNZ ||
778            opline->opcode == ZEND_JMPZNZ) &&
779            ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
780            VAR_SOURCE(opline->op1) != NULL &&
781            !used_ext[VAR_NUM(ZEND_OP1(opline).var)] &&
782            VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL_NOT) {
783            /* T = BOOL_NOT(X) + JMPZ(T) -> NOP, JMPNZ(X) */
784            zend_op *src = VAR_SOURCE(opline->op1);
785
786            COPY_NODE(opline->op1, src->op1);
787
788            switch (opline->opcode) {
789                case ZEND_BOOL:
790                    /* T = BOOL_NOT(X) + BOOL(T) -> NOP, BOOL_NOT(X) */
791                    opline->opcode = ZEND_BOOL_NOT;
792                    break;
793                case ZEND_BOOL_NOT:
794                    /* T = BOOL_NOT(X) + BOOL_BOOL(T) -> NOP, BOOL(X) */
795                    opline->opcode = ZEND_BOOL;
796                    break;
797                case ZEND_JMPZ:
798                    /* T = BOOL_NOT(X) + JMPZ(T,L) -> NOP, JMPNZ(X,L) */
799                    opline->opcode = ZEND_JMPNZ;
800                    break;
801                case ZEND_JMPNZ:
802                    /* T = BOOL_NOT(X) + JMPNZ(T,L) -> NOP, JMPZ(X,L) */
803                    opline->opcode = ZEND_JMPZ;
804                    break;
805                case ZEND_JMPZNZ:
806                {
807                    /* T = BOOL_NOT(X) + JMPZNZ(T,L1,L2) -> NOP, JMPZNZ(X,L2,L1) */
808                    int op_t;
809                    zend_code_block *op_b;
810
811                    op_t = opline->extended_value;
812                    opline->extended_value = ZEND_OP2(opline).opline_num;
813                    ZEND_OP2(opline).opline_num = op_t;
814
815                    op_b = block->ext_to;
816                    block->ext_to = block->op2_to;
817                    block->op2_to = op_b;
818                }
819                break;
820            }
821
822            VAR_UNSET(opline->op1);
823            MAKE_NOP(src);
824            continue;
825        } else
826#if 0
827        /* T = BOOL_NOT(X) + T = JMPZ_EX(T, X) -> T = BOOL_NOT(X), JMPNZ(X) */
828        if(0 && (opline->opcode == ZEND_JMPZ_EX ||
829            opline->opcode == ZEND_JMPNZ_EX) &&
830           ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
831           VAR_SOURCE(opline->op1) != NULL &&
832           VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL_NOT &&
833           ZEND_OP1(opline).var == ZEND_RESULT(opline).var
834           ) {
835            zend_op *src = VAR_SOURCE(opline->op1);
836            if(opline->opcode == ZEND_JMPZ_EX) {
837                opline->opcode = ZEND_JMPNZ;
838            } else {
839                opline->opcode = ZEND_JMPZ;
840            }
841            COPY_NODE(opline->op1, src->op1);
842            SET_UNUSED(opline->result);
843            continue;
844        } else
845#endif
846        /* T = BOOL(X) + JMPZ(T) -> NOP, JMPZ(X) */
847        if ((opline->opcode == ZEND_BOOL ||
848            opline->opcode == ZEND_BOOL_NOT ||
849            opline->opcode == ZEND_JMPZ ||
850            opline->opcode == ZEND_JMPZ_EX ||
851            opline->opcode == ZEND_JMPNZ_EX ||
852            opline->opcode == ZEND_JMPNZ ||
853            opline->opcode == ZEND_JMPZNZ) &&
854            (ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
855            VAR_SOURCE(opline->op1) != NULL &&
856            (!used_ext[VAR_NUM(ZEND_OP1(opline).var)] ||
857            ((ZEND_RESULT_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
858             ZEND_RESULT(opline).var == ZEND_OP1(opline).var)) &&
859            (VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL ||
860            VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN)) {
861            zend_op *src = VAR_SOURCE(opline->op1);
862            COPY_NODE(opline->op1, src->op1);
863
864            VAR_UNSET(opline->op1);
865            MAKE_NOP(src);
866            continue;
867        } else if (last_op && opline->opcode == ZEND_ECHO &&
868                  last_op->opcode == ZEND_ECHO &&
869                  ZEND_OP1_TYPE(opline) == IS_CONST &&
870                  Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_DOUBLE &&
871                  ZEND_OP1_TYPE(last_op) == IS_CONST &&
872                  Z_TYPE(ZEND_OP1_LITERAL(last_op)) != IS_DOUBLE) {
873            /* compress consecutive ECHO's.
874             * Float to string conversion may be affected by current
875             * locale setting.
876             */
877            int l, old_len;
878
879            if (Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_STRING) {
880                convert_to_string_safe(&ZEND_OP1_LITERAL(opline));
881            }
882            if (Z_TYPE(ZEND_OP1_LITERAL(last_op)) != IS_STRING) {
883                convert_to_string_safe(&ZEND_OP1_LITERAL(last_op));
884            }
885            old_len = Z_STRLEN(ZEND_OP1_LITERAL(last_op));
886            l = old_len + Z_STRLEN(ZEND_OP1_LITERAL(opline));
887            if (!Z_REFCOUNTED(ZEND_OP1_LITERAL(last_op))) {
888                zend_string *tmp = zend_string_alloc(l, 0);
889                memcpy(tmp->val, Z_STRVAL(ZEND_OP1_LITERAL(last_op)), old_len);
890                Z_STR(ZEND_OP1_LITERAL(last_op)) = tmp;
891            } else {
892                Z_STR(ZEND_OP1_LITERAL(last_op)) = zend_string_realloc(Z_STR(ZEND_OP1_LITERAL(last_op)), l, 0);
893            }
894            Z_TYPE_INFO(ZEND_OP1_LITERAL(last_op)) = IS_STRING_EX;
895            memcpy(Z_STRVAL(ZEND_OP1_LITERAL(last_op)) + old_len, Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)));
896            Z_STRVAL(ZEND_OP1_LITERAL(last_op))[l] = '\0';
897            zval_dtor(&ZEND_OP1_LITERAL(opline));
898            Z_STR(ZEND_OP1_LITERAL(opline)) = zend_new_interned_string(Z_STR(ZEND_OP1_LITERAL(last_op)));
899            if (!Z_REFCOUNTED(ZEND_OP1_LITERAL(opline))) {
900                Z_TYPE_FLAGS(ZEND_OP1_LITERAL(opline)) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
901            }
902            ZVAL_NULL(&ZEND_OP1_LITERAL(last_op));
903            MAKE_NOP(last_op);
904        } else if (opline->opcode == ZEND_CONCAT &&
905                  ZEND_OP2_TYPE(opline) == IS_CONST &&
906                  ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
907                  VAR_SOURCE(opline->op1) &&
908                  (VAR_SOURCE(opline->op1)->opcode == ZEND_CONCAT ||
909                   VAR_SOURCE(opline->op1)->opcode == ZEND_ADD_STRING) &&
910                  ZEND_OP2_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
911                  ZEND_RESULT(VAR_SOURCE(opline->op1)).var == ZEND_OP1(opline).var) {
912            /* compress consecutive CONCATs */
913            zend_op *src = VAR_SOURCE(opline->op1);
914            int l, old_len;
915
916            if (Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
917                convert_to_string_safe(&ZEND_OP2_LITERAL(opline));
918            }
919            if (Z_TYPE(ZEND_OP2_LITERAL(src)) != IS_STRING) {
920                convert_to_string_safe(&ZEND_OP2_LITERAL(src));
921            }
922
923            VAR_UNSET(opline->op1);
924            if (ZEND_OP1_TYPE(src) == IS_UNUSED) {
925                /* 5.3 may use IS_UNUSED as first argument to ZEND_ADD_... */
926                opline->opcode = ZEND_ADD_STRING;
927            }
928            COPY_NODE(opline->op1, src->op1);
929            old_len = Z_STRLEN(ZEND_OP2_LITERAL(src));
930            l = old_len + Z_STRLEN(ZEND_OP2_LITERAL(opline));
931            if (!Z_REFCOUNTED(ZEND_OP2_LITERAL(src))) {
932                zend_string *tmp = zend_string_alloc(l, 0);
933                memcpy(tmp->val, Z_STRVAL(ZEND_OP2_LITERAL(src)), old_len);
934                Z_STR(ZEND_OP2_LITERAL(last_op)) = tmp;
935            } else {
936                Z_STR(ZEND_OP2_LITERAL(src)) = zend_string_realloc(Z_STR(ZEND_OP2_LITERAL(src)), l, 0);
937            }
938            Z_TYPE_INFO(ZEND_OP2_LITERAL(last_op)) = IS_STRING_EX;
939            memcpy(Z_STRVAL(ZEND_OP2_LITERAL(src)) + old_len, Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)));
940            Z_STRVAL(ZEND_OP2_LITERAL(src))[l] = '\0';
941            zend_string_release(Z_STR(ZEND_OP2_LITERAL(opline)));
942            Z_STR(ZEND_OP2_LITERAL(opline)) = zend_new_interned_string(Z_STR(ZEND_OP2_LITERAL(src)));
943            if (!Z_REFCOUNTED(ZEND_OP2_LITERAL(opline))) {
944                Z_TYPE_FLAGS(ZEND_OP2_LITERAL(opline)) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
945            }
946            ZVAL_NULL(&ZEND_OP2_LITERAL(src));
947            MAKE_NOP(src);
948        } else if ((opline->opcode == ZEND_ADD_STRING || opline->opcode == ZEND_ADD_VAR) && ZEND_OP1_TYPE(opline) == IS_CONST) {
949            /* convert ADD_STRING(C1, C2) to CONCAT(C1, C2) */
950            opline->opcode = ZEND_CONCAT;
951            continue;
952        } else if (opline->opcode == ZEND_ADD_CHAR && ZEND_OP1_TYPE(opline) == IS_CONST && ZEND_OP2_TYPE(opline) == IS_CONST) {
953            /* convert ADD_CHAR(C1, C2) to CONCAT(C1, C2) */
954            char c = (char)Z_LVAL(ZEND_OP2_LITERAL(opline));
955            ZVAL_STRINGL(&ZEND_OP2_LITERAL(opline), &c, 1);
956            opline->opcode = ZEND_CONCAT;
957            continue;
958        } else if ((opline->opcode == ZEND_ADD ||
959                    opline->opcode == ZEND_SUB ||
960                    opline->opcode == ZEND_MUL ||
961                    opline->opcode == ZEND_DIV ||
962                    opline->opcode == ZEND_MOD ||
963                    opline->opcode == ZEND_SL ||
964                    opline->opcode == ZEND_SR ||
965                    opline->opcode == ZEND_CONCAT ||
966                    opline->opcode == ZEND_IS_EQUAL ||
967                    opline->opcode == ZEND_IS_NOT_EQUAL ||
968                    opline->opcode == ZEND_IS_SMALLER ||
969                    opline->opcode == ZEND_IS_SMALLER_OR_EQUAL ||
970                    opline->opcode == ZEND_IS_IDENTICAL ||
971                    opline->opcode == ZEND_IS_NOT_IDENTICAL ||
972                    opline->opcode == ZEND_BOOL_XOR ||
973                    opline->opcode == ZEND_BW_OR ||
974                    opline->opcode == ZEND_BW_AND ||
975                    opline->opcode == ZEND_BW_XOR) &&
976                    ZEND_OP1_TYPE(opline)==IS_CONST &&
977                    ZEND_OP2_TYPE(opline)==IS_CONST) {
978            /* evaluate constant expressions */
979            int (*binary_op)(zval *result, zval *op1, zval *op2) = get_binary_op(opline->opcode);
980            zval result;
981            int er;
982
983            if ((opline->opcode == ZEND_DIV || opline->opcode == ZEND_MOD) &&
984                ((Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_LONG &&
985                  Z_LVAL(ZEND_OP2_LITERAL(opline)) == 0) ||
986                 (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_DOUBLE &&
987                  Z_DVAL(ZEND_OP2_LITERAL(opline)) == 0.0))) {
988                if (RESULT_USED(opline)) {
989                    SET_VAR_SOURCE(opline);
990                }
991                opline++;
992                continue;
993            }
994            er = EG(error_reporting);
995            EG(error_reporting) = 0;
996            if (binary_op(&result, &ZEND_OP1_LITERAL(opline), &ZEND_OP2_LITERAL(opline)) == SUCCESS) {
997                literal_dtor(&ZEND_OP1_LITERAL(opline));
998                literal_dtor(&ZEND_OP2_LITERAL(opline));
999                opline->opcode = ZEND_QM_ASSIGN;
1000                SET_UNUSED(opline->op2);
1001                zend_optimizer_update_op1_const(op_array, opline, &result);
1002            }
1003            EG(error_reporting) = er;
1004        } else if ((opline->opcode == ZEND_BOOL ||
1005                    opline->opcode == ZEND_BOOL_NOT ||
1006                    opline->opcode == ZEND_BW_NOT) && ZEND_OP1_TYPE(opline) == IS_CONST) {
1007            /* evaluate constant unary ops */
1008            unary_op_type unary_op = get_unary_op(opline->opcode);
1009            zval result;
1010
1011            if (unary_op) {
1012                unary_op(&result, &ZEND_OP1_LITERAL(opline));
1013                literal_dtor(&ZEND_OP1_LITERAL(opline));
1014            } else {
1015                /* BOOL */
1016                result = ZEND_OP1_LITERAL(opline);
1017                convert_to_boolean(&result);
1018                ZVAL_NULL(&ZEND_OP1_LITERAL(opline));
1019            }
1020            opline->opcode = ZEND_QM_ASSIGN;
1021            zend_optimizer_update_op1_const(op_array, opline, &result);
1022        } else if ((opline->opcode == ZEND_RETURN || opline->opcode == ZEND_EXIT) &&
1023                    (ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
1024                    VAR_SOURCE(opline->op1) &&
1025                    VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN) {
1026            /* T = QM_ASSIGN(X), RETURN(T) to RETURN(X) */
1027            zend_op *src = VAR_SOURCE(opline->op1);
1028            VAR_UNSET(opline->op1);
1029            COPY_NODE(opline->op1, src->op1);
1030            MAKE_NOP(src);
1031        } else if ((opline->opcode == ZEND_ADD_STRING ||
1032                    opline->opcode == ZEND_ADD_CHAR ||
1033                    opline->opcode == ZEND_ADD_VAR ||
1034                    opline->opcode == ZEND_CONCAT) &&
1035                    ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
1036                    VAR_SOURCE(opline->op1) &&
1037                    VAR_SOURCE(opline->op1)->opcode == ZEND_CONCAT &&
1038                    ZEND_OP2_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
1039                    Z_TYPE(ZEND_OP2_LITERAL(VAR_SOURCE(opline->op1))) == IS_STRING &&
1040                    Z_STRLEN(ZEND_OP2_LITERAL(VAR_SOURCE(opline->op1))) == 0) {
1041            /* convert T = CONCAT(X,''), T = ADD_STRING(T, Y) to T = CONCAT(X,Y) */
1042            zend_op *src = VAR_SOURCE(opline->op1);
1043            VAR_UNSET(opline->op1);
1044            COPY_NODE(opline->op1, src->op1);
1045            if (opline->opcode == ZEND_ADD_CHAR) {
1046                char c = (char)Z_LVAL(ZEND_OP2_LITERAL(opline));
1047                ZVAL_STRINGL(&ZEND_OP2_LITERAL(opline), &c, 1);
1048            }
1049            opline->opcode = ZEND_CONCAT;
1050            literal_dtor(&ZEND_OP2_LITERAL(src)); /* will take care of empty_string too */
1051            MAKE_NOP(src);
1052        } else if ((opline->opcode == ZEND_ADD_STRING ||
1053                    opline->opcode == ZEND_ADD_CHAR ||
1054                    opline->opcode == ZEND_ADD_VAR ||
1055                    opline->opcode == ZEND_CONCAT) &&
1056                    (ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
1057                    VAR_SOURCE(opline->op1) &&
1058                    VAR_SOURCE(opline->op1)->opcode == ZEND_CAST &&
1059                    VAR_SOURCE(opline->op1)->extended_value == IS_STRING) {
1060            /* convert T1 = CAST(STRING, X), T2 = CONCAT(T1, Y) to T2 = CONCAT(X,Y) */
1061            zend_op *src = VAR_SOURCE(opline->op1);
1062            VAR_UNSET(opline->op1);
1063            COPY_NODE(opline->op1, src->op1);
1064            if (opline->opcode == ZEND_ADD_CHAR) {
1065                char c = (char)Z_LVAL(ZEND_OP2_LITERAL(opline));
1066                ZVAL_STRINGL(&ZEND_OP2_LITERAL(opline), &c, 1);
1067            }
1068            opline->opcode = ZEND_CONCAT;
1069            MAKE_NOP(src);
1070        } else if (opline->opcode == ZEND_QM_ASSIGN &&
1071                    ZEND_OP1_TYPE(opline) == ZEND_RESULT_TYPE(opline) &&
1072                    ZEND_OP1(opline).var == ZEND_RESULT(opline).var) {
1073            /* strip T = QM_ASSIGN(T) */
1074            MAKE_NOP(opline);
1075        } else if (opline->opcode == ZEND_BOOL &&
1076                    ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
1077                    VAR_SOURCE(opline->op1) &&
1078                    (VAR_SOURCE(opline->op1)->opcode == ZEND_IS_EQUAL ||
1079                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_NOT_EQUAL ||
1080                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_SMALLER ||
1081                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_SMALLER_OR_EQUAL ||
1082                    VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL ||
1083                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_IDENTICAL ||
1084                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_NOT_IDENTICAL ||
1085                    VAR_SOURCE(opline->op1)->opcode == ZEND_ISSET_ISEMPTY_VAR ||
1086                    VAR_SOURCE(opline->op1)->opcode == ZEND_ISSET_ISEMPTY_DIM_OBJ) &&
1087                    !used_ext[VAR_NUM(ZEND_OP1(opline).var)]) {
1088            /* T = IS_SMALLER(X, Y), T1 = BOOL(T) => T = IS_SMALLER(X, Y), T1 = QM_ASSIGN(T) */
1089            zend_op *src = VAR_SOURCE(opline->op1);
1090            COPY_NODE(src->result, opline->result);
1091            SET_VAR_SOURCE(src);
1092            MAKE_NOP(opline);
1093        }
1094        /* get variable source */
1095        if (RESULT_USED(opline)) {
1096            SET_VAR_SOURCE(opline);
1097        }
1098        if (opline->opcode != ZEND_NOP) {
1099            last_op = opline;
1100        }
1101        opline++;
1102    }
1103
1104    strip_nop(block, ctx);
1105}
1106
1107/* Rebuild plain (optimized) op_array from CFG */
1108static void assemble_code_blocks(zend_cfg *cfg, zend_op_array *op_array)
1109{
1110    zend_code_block *blocks = cfg->blocks;
1111    zend_op *new_opcodes = emalloc(op_array->last * sizeof(zend_op));
1112    zend_op *opline = new_opcodes;
1113    zend_code_block *cur_block = blocks;
1114
1115    /* Copy code of reachable blocks into a single buffer */
1116    while (cur_block) {
1117        if (cur_block->access) {
1118            memcpy(opline, cur_block->start_opline, cur_block->len * sizeof(zend_op));
1119            cur_block->start_opline = opline;
1120            opline += cur_block->len;
1121            if ((opline - 1)->opcode == ZEND_JMP) {
1122                zend_code_block *next;
1123                next = cur_block->next;
1124                while (next && !next->access) {
1125                    next = next->next;
1126                }
1127                if (next && next == cur_block->op1_to) {
1128                    /* JMP to the next block - strip it */
1129                    cur_block->follow_to = cur_block->op1_to;
1130                    cur_block->op1_to = NULL;
1131                    MAKE_NOP((opline - 1));
1132                    opline--;
1133                    cur_block->len--;
1134                }
1135            }
1136        } else {
1137            /* this block will not be used, delete all constants there */
1138            zend_op *_opl;
1139            zend_op *end = cur_block->start_opline + cur_block->len;
1140            for (_opl = cur_block->start_opline; _opl && _opl < end; _opl++) {
1141                if (ZEND_OP1_TYPE(_opl) == IS_CONST) {
1142                    literal_dtor(&ZEND_OP1_LITERAL(_opl));
1143                }
1144                if (ZEND_OP2_TYPE(_opl) == IS_CONST) {
1145                    literal_dtor(&ZEND_OP2_LITERAL(_opl));
1146                }
1147            }
1148        }
1149        cur_block = cur_block->next;
1150    }
1151
1152    if ((opline-1)->opcode == ZEND_THROW) {
1153        /* if we finished with THROW, we need to add space between THROW and HANDLE to not confuse
1154           zend_throw_internal */
1155        MAKE_NOP(opline);
1156        opline->lineno = opline[-1].lineno;
1157        opline++;
1158    }
1159
1160    op_array->last = opline-new_opcodes;
1161
1162    /* adjust exception jump targets */
1163    if (op_array->last_try_catch) {
1164        int i, j;
1165        for (i = 0, j = 0; i< op_array->last_try_catch; i++) {
1166            if (cfg->try[i]->access) {
1167                op_array->try_catch_array[j].try_op = cfg->try[i]->start_opline - new_opcodes;
1168                op_array->try_catch_array[j].catch_op = cfg->catch[i]->start_opline - new_opcodes;
1169                j++;
1170            }
1171        }
1172        op_array->last_try_catch = j;
1173    }
1174
1175    /* adjust loop jump targets */
1176    if (op_array->last_brk_cont) {
1177        int i;
1178        for (i = 0; i< op_array->last_brk_cont; i++) {
1179            op_array->brk_cont_array[i].start = cfg->loop_start[i]->start_opline - new_opcodes;
1180            op_array->brk_cont_array[i].cont = cfg->loop_cont[i]->start_opline - new_opcodes;
1181            op_array->brk_cont_array[i].brk = cfg->loop_brk[i]->start_opline - new_opcodes;
1182        }
1183    }
1184
1185    /* adjust jump targets */
1186    for (cur_block = blocks; cur_block; cur_block = cur_block->next) {
1187        if (!cur_block->access) {
1188            continue;
1189        }
1190        opline = cur_block->start_opline + cur_block->len - 1;
1191        if (opline->opcode == ZEND_OP_DATA) {
1192            opline--;
1193        }
1194        if (cur_block->op1_to) {
1195            ZEND_OP1(opline).opline_num = cur_block->op1_to->start_opline - new_opcodes;
1196        }
1197        if (cur_block->op2_to) {
1198            ZEND_OP2(opline).opline_num = cur_block->op2_to->start_opline - new_opcodes;
1199        }
1200        if (cur_block->ext_to) {
1201            opline->extended_value = cur_block->ext_to->start_opline - new_opcodes;
1202        }
1203        print_block(cur_block, new_opcodes, "Out ");
1204    }
1205    efree(op_array->opcodes);
1206    op_array->opcodes = erealloc(new_opcodes, op_array->last * sizeof(zend_op));
1207
1208    /* adjust early binding list */
1209    if (op_array->early_binding != (uint32_t)-1) {
1210        uint32_t *opline_num = &op_array->early_binding;
1211        zend_op *end;
1212
1213        opline = op_array->opcodes;
1214        end = opline + op_array->last;
1215        while (opline < end) {
1216            if (opline->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
1217                *opline_num = opline - op_array->opcodes;
1218                opline_num = &ZEND_RESULT(opline).opline_num;
1219            }
1220            ++opline;
1221        }
1222        *opline_num = -1;
1223    }
1224}
1225
1226static void zend_jmp_optimization(zend_code_block *block, zend_op_array *op_array, zend_code_block *blocks, zend_cfg *cfg, zend_optimizer_ctx *ctx)
1227{
1228    /* last_op is the last opcode of the current block */
1229    zend_op *last_op = (block->start_opline + block->len - 1);
1230
1231    if (!block->len) {
1232        return;
1233    }
1234    switch (last_op->opcode) {
1235        case ZEND_JMP:
1236            {
1237                zend_op *target = block->op1_to->start_opline;
1238                zend_code_block *next = block->next;
1239
1240                while (next && !next->access) {
1241                    /* find used one */
1242                    next = next->next;
1243                }
1244
1245                /* JMP(next) -> NOP */
1246                if (block->op1_to == next) {
1247                    block->follow_to = block->op1_to;
1248                    block->op1_to = NULL;
1249                    MAKE_NOP(last_op);
1250                    block->len--;
1251                    if (block->len == 0) {
1252                        /* this block is nothing but NOP now */
1253                        delete_code_block(block, ctx);
1254                    }
1255                    break;
1256                }
1257
1258                if (((target->opcode == ZEND_JMP &&
1259                    block->op1_to != block->op1_to->op1_to) ||
1260                    target->opcode == ZEND_JMPZNZ) &&
1261                    !block->op1_to->protected) {
1262                    /* JMP L, L: JMP L1 -> JMP L1 */
1263                    /* JMP L, L: JMPZNZ L1,L2 -> JMPZNZ L1,L2 */
1264                    *last_op = *target;
1265                    if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1266                        zval zv = ZEND_OP1_LITERAL(last_op);
1267                        zval_copy_ctor(&zv);
1268                        last_op->op1.constant = zend_optimizer_add_literal(op_array, &zv);
1269                    }
1270                    del_source(block, block->op1_to);
1271                    if (block->op1_to->op2_to) {
1272                        block->op2_to = block->op1_to->op2_to;
1273                        ADD_SOURCE(block, block->op2_to);
1274                    }
1275                    if (block->op1_to->ext_to) {
1276                        block->ext_to = block->op1_to->ext_to;
1277                        ADD_SOURCE(block, block->ext_to);
1278                    }
1279                    if (block->op1_to->op1_to) {
1280                        block->op1_to = block->op1_to->op1_to;
1281                        ADD_SOURCE(block, block->op1_to);
1282                    } else {
1283                        block->op1_to = NULL;
1284                    }
1285                } else if (target->opcode == ZEND_RETURN ||
1286                          target->opcode == ZEND_RETURN_BY_REF ||
1287                          target->opcode == ZEND_FAST_RET ||
1288                          target->opcode == ZEND_EXIT) {
1289                    /* JMP L, L: RETURN to immediate RETURN */
1290                    *last_op = *target;
1291                    if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1292                        zval zv = ZEND_OP1_LITERAL(last_op);
1293                        zval_copy_ctor(&zv);
1294                        last_op->op1.constant = zend_optimizer_add_literal(op_array, &zv);
1295                    }
1296                    del_source(block, block->op1_to);
1297                    block->op1_to = NULL;
1298#if 0
1299                /* Temporarily disabled - see bug #0025274 */
1300                } else if (0&& block->op1_to != block &&
1301                       block->op1_to != blocks &&
1302                           op_array->last_try_catch == 0 &&
1303                           target->opcode != ZEND_FREE) {
1304                    /* Block Reordering (saves one JMP on each "for" loop iteration)
1305                     * It is disabled for some cases (ZEND_FREE)
1306                     * which may break register allocation.
1307                     */
1308                    zend_bool can_reorder = 0;
1309                    zend_block_source *cs = block->op1_to->sources;
1310
1311                    /* the "target" block doesn't had any followed block */
1312                    while(cs) {
1313                        if (cs->from->follow_to == block->op1_to) {
1314                            can_reorder = 0;
1315                            break;
1316                        }
1317                        cs = cs->next;
1318                    }
1319                    if (can_reorder) {
1320                        next = block->op1_to;
1321                        /* the "target" block is not followed by current "block" */
1322                        while (next->follow_to != NULL) {
1323                            if (next->follow_to == block) {
1324                                can_reorder = 0;
1325                                break;
1326                            }
1327                            next = next->follow_to;
1328                        }
1329                        if (can_reorder) {
1330                            zend_code_block *prev = blocks;
1331
1332                            while (prev->next != block->op1_to) {
1333                                prev = prev->next;
1334                            }
1335                            prev->next = next->next;
1336                            next->next = block->next;
1337                            block->next = block->op1_to;
1338
1339                            block->follow_to = block->op1_to;
1340                            block->op1_to = NULL;
1341                            MAKE_NOP(last_op);
1342                            block->len--;
1343                            if(block->len == 0) {
1344                                /* this block is nothing but NOP now */
1345                                delete_code_block(block, ctx);
1346                            }
1347                            break;
1348                        }
1349                    }
1350#endif
1351                }
1352            }
1353            break;
1354
1355        case ZEND_JMPZ:
1356        case ZEND_JMPNZ:
1357            /* constant conditional JMPs */
1358            if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1359                int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(last_op));
1360
1361                if (last_op->opcode == ZEND_JMPZ) {
1362                    should_jmp = !should_jmp;
1363                }
1364                literal_dtor(&ZEND_OP1_LITERAL(last_op));
1365                ZEND_OP1_TYPE(last_op) = IS_UNUSED;
1366                if (should_jmp) {
1367                    /* JMPNZ(true) -> JMP */
1368                    last_op->opcode = ZEND_JMP;
1369                    COPY_NODE(last_op->op1, last_op->op2);
1370                    block->op1_to = block->op2_to;
1371                    del_source(block, block->follow_to);
1372                    block->op2_to = NULL;
1373                    block->follow_to = NULL;
1374                } else {
1375                    /* JMPNZ(false) -> NOP */
1376                    MAKE_NOP(last_op);
1377                    del_source(block, block->op2_to);
1378                    block->op2_to = NULL;
1379                }
1380                break;
1381            }
1382
1383            if (block->op2_to) {
1384                zend_uchar same_type = ZEND_OP1_TYPE(last_op);
1385                uint32_t same_var = VAR_NUM_EX(last_op->op1);
1386                zend_op *target;
1387                zend_op *target_end;
1388                zend_code_block *target_block = block->op2_to;;
1389
1390next_target:
1391                target = target_block->start_opline;
1392                target_end = target_block->start_opline + target_block->len;
1393                while (target < target_end && target->opcode == ZEND_NOP) {
1394                    target++;
1395                }
1396
1397                /* next block is only NOP's */
1398                if (target == target_end) {
1399                    target_block = target_block->follow_to;
1400                    goto next_target;
1401                } else if (target->opcode == INV_COND(last_op->opcode) &&
1402                    /* JMPZ(X, L), L: JMPNZ(X, L2) -> JMPZ(X, L+1) */
1403                   (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1404                   same_type == ZEND_OP1_TYPE(target) &&
1405                   same_var == VAR_NUM_EX(target->op1) &&
1406                   target_block->follow_to &&
1407                   !target_block->protected
1408                   ) {
1409                    del_source(block, block->op2_to);
1410                    block->op2_to = target_block->follow_to;
1411                    ADD_SOURCE(block, block->op2_to);
1412                } else if (target->opcode == INV_COND_EX(last_op->opcode) &&
1413                            (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1414                            same_type == ZEND_OP1_TYPE(target) &&
1415                            same_var == VAR_NUM_EX(target->op1) &&
1416                            target_block->follow_to &&
1417                            !target_block->protected) {
1418                    /* JMPZ(X, L), L: X = JMPNZ_EX(X, L2) -> JMPZ(X, L+1) */
1419                    last_op->opcode += 3;
1420                    last_op->result = target->result;
1421                    del_source(block, block->op2_to);
1422                    block->op2_to = target_block->follow_to;
1423                    ADD_SOURCE(block, block->op2_to);
1424                } else if (target_block->op2_to &&
1425                           target->opcode == last_op->opcode &&
1426                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1427                           same_type == ZEND_OP1_TYPE(target) &&
1428                           same_var == VAR_NUM_EX(target->op1) &&
1429                           !target_block->protected) {
1430                    /* JMPZ(X, L), L: JMPZ(X, L2) -> JMPZ(X, L2) */
1431                    del_source(block, block->op2_to);
1432                    block->op2_to = target_block->op2_to;
1433                    ADD_SOURCE(block, block->op2_to);
1434                } else if (target_block->op1_to &&
1435                            target->opcode == ZEND_JMP &&
1436                            !target_block->protected) {
1437                    /* JMPZ(X, L), L: JMP(L2) -> JMPZ(X, L2) */
1438                    del_source(block, block->op2_to);
1439                    block->op2_to = target_block->op1_to;
1440                    ADD_SOURCE(block, block->op2_to);
1441                } else if (target_block->op2_to &&
1442                            target_block->ext_to &&
1443                            target->opcode == ZEND_JMPZNZ &&
1444                            (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1445                            same_type == ZEND_OP1_TYPE(target) &&
1446                            same_var == VAR_NUM_EX(target->op1) &&
1447                            !target_block->protected) {
1448                    /* JMPZ(X, L), L: JMPZNZ(X, L2, L3) -> JMPZ(X, L2) */
1449                    del_source(block, block->op2_to);
1450                    if (last_op->opcode == ZEND_JMPZ) {
1451                        block->op2_to = target_block->op2_to;
1452                    } else {
1453                        block->op2_to = target_block->ext_to;
1454                    }
1455                    ADD_SOURCE(block, block->op2_to);
1456                }
1457            }
1458
1459            if (block->follow_to &&
1460                (last_op->opcode == ZEND_JMPZ || last_op->opcode == ZEND_JMPNZ)) {
1461                zend_op *target;
1462                zend_op *target_end;
1463
1464                while (1) {
1465                    target = block->follow_to->start_opline;
1466                    target_end = block->follow_to->start_opline + block->follow_to->len;
1467                    while (target < target_end && target->opcode == ZEND_NOP) {
1468                        target++;
1469                    }
1470
1471                    /* next block is only NOP's */
1472                    if (target == target_end) {
1473                        del_source(block, block->follow_to);
1474                        block->follow_to = block->follow_to->follow_to;
1475                        ADD_SOURCE(block, block->follow_to);
1476                    } else {
1477                        break;
1478                    }
1479                }
1480                /* JMPZ(X,L1), JMP(L2) -> JMPZNZ(X,L1,L2) */
1481                if (target->opcode == ZEND_JMP &&
1482                    block->follow_to->op1_to &&
1483                    !block->follow_to->protected) {
1484                    del_source(block, block->follow_to);
1485                    if (last_op->opcode == ZEND_JMPZ) {
1486                        block->ext_to = block->follow_to->op1_to;
1487                        ADD_SOURCE(block, block->ext_to);
1488                    } else {
1489                        block->ext_to = block->op2_to;
1490                        block->op2_to = block->follow_to->op1_to;
1491                        ADD_SOURCE(block, block->op2_to);
1492                    }
1493                    block->follow_to = NULL;
1494                    last_op->opcode = ZEND_JMPZNZ;
1495                }
1496            }
1497            break;
1498
1499        case ZEND_JMPNZ_EX:
1500        case ZEND_JMPZ_EX:
1501            /* constant conditional JMPs */
1502            if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1503                int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(last_op));
1504
1505                if (last_op->opcode == ZEND_JMPZ_EX) {
1506                    should_jmp = !should_jmp;
1507                }
1508                if (!should_jmp) {
1509                    /* T = JMPZ_EX(true,L)   -> T = QM_ASSIGN(true)
1510                     * T = JMPNZ_EX(false,L) -> T = QM_ASSIGN(false)
1511                     */
1512                    last_op->opcode = ZEND_QM_ASSIGN;
1513                    SET_UNUSED(last_op->op2);
1514                    del_source(block, block->op2_to);
1515                    block->op2_to = NULL;
1516                }
1517                break;
1518            }
1519
1520            if (block->op2_to) {
1521                zend_op *target, *target_end;
1522                char *same_t=NULL;
1523                zend_code_block *target_block;
1524                int var_num = op_array->last_var + op_array->T;
1525
1526                if (var_num <= 0) {
1527                    return;
1528                }
1529                same_t = cfg->same_t;
1530                memset(same_t, 0, var_num);
1531                same_t[VAR_NUM_EX(last_op->op1)] |= ZEND_OP1_TYPE(last_op);
1532                same_t[VAR_NUM_EX(last_op->result)] |= ZEND_RESULT_TYPE(last_op);
1533                target_block = block->op2_to;
1534next_target_ex:
1535                target = target_block->start_opline;
1536                target_end = target_block->start_opline + target_block->len;
1537                while (target < target_end && target->opcode == ZEND_NOP) {
1538                    target++;
1539                }
1540                /* next block is only NOP's */
1541                if (target == target_end) {
1542                    target_block = target_block->follow_to;
1543                    goto next_target_ex;
1544                } else if (target_block->op2_to &&
1545                           target->opcode == last_op->opcode-3 &&
1546                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1547                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1548                           !target_block->protected) {
1549                    /* T = JMPZ_EX(X, L1), L1: JMPZ({X|T}, L2) -> T = JMPZ_EX(X, L2) */
1550                    del_source(block, block->op2_to);
1551                    block->op2_to = target_block->op2_to;
1552                    ADD_SOURCE(block, block->op2_to);
1553                } else if (target_block->op2_to &&
1554                           target->opcode == INV_EX_COND(last_op->opcode) &&
1555                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1556                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1557                           !target_block->protected) {
1558                    /* T = JMPZ_EX(X, L1), L1: JMPNZ({X|T1}, L2) -> T = JMPZ_EX(X, L1+1) */
1559                    del_source(block, block->op2_to);
1560                    block->op2_to = target_block->follow_to;
1561                    ADD_SOURCE(block, block->op2_to);
1562                } else if (target_block->op2_to &&
1563                           target->opcode == INV_EX_COND_EX(last_op->opcode) &&
1564                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1565                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1566                           (same_t[VAR_NUM_EX(target->result)] & ZEND_RESULT_TYPE(target)) != 0 &&
1567                           !target_block->protected) {
1568                    /* T = JMPZ_EX(X, L1), L1: T = JMPNZ_EX(T, L2) -> T = JMPZ_EX(X, L1+1) */
1569                    del_source(block, block->op2_to);
1570                    block->op2_to = target_block->follow_to;
1571                    ADD_SOURCE(block, block->op2_to);
1572                } else if (target_block->op2_to &&
1573                           target->opcode == last_op->opcode &&
1574                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1575                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1576                           (same_t[VAR_NUM_EX(target->result)] & ZEND_RESULT_TYPE(target)) != 0 &&
1577                           !target_block->protected) {
1578                    /* T = JMPZ_EX(X, L1), L1: T = JMPZ({X|T}, L2) -> T = JMPZ_EX(X, L2) */
1579                    del_source(block, block->op2_to);
1580                    block->op2_to = target_block->op2_to;
1581                    ADD_SOURCE(block, block->op2_to);
1582                } else if (target_block->op1_to &&
1583                           target->opcode == ZEND_JMP &&
1584                           !target_block->protected) {
1585                    /* T = JMPZ_EX(X, L), L: JMP(L2) -> T = JMPZ(X, L2) */
1586                    del_source(block, block->op2_to);
1587                    block->op2_to = target_block->op1_to;
1588                    ADD_SOURCE(block, block->op2_to);
1589                } else if (target_block->op2_to &&
1590                           target_block->ext_to &&
1591                           target->opcode == ZEND_JMPZNZ &&
1592                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1593                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1594                           !target_block->protected) {
1595                    /* T = JMPZ_EX(X, L), L: JMPZNZ({X|T}, L2, L3) -> T = JMPZ_EX(X, L2) */
1596                    del_source(block, block->op2_to);
1597                    if (last_op->opcode == ZEND_JMPZ_EX) {
1598                        block->op2_to = target_block->op2_to;
1599                    } else {
1600                        block->op2_to = target_block->ext_to;
1601                    }
1602                    ADD_SOURCE(block, block->op2_to);
1603                }
1604            }
1605            break;
1606
1607        case ZEND_JMPZNZ: {
1608            zend_code_block *next = block->next;
1609
1610            while (next && !next->access) {
1611                /* find first accessed one */
1612                next = next->next;
1613            }
1614
1615            if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1616                if (!zend_is_true(&ZEND_OP1_LITERAL(last_op))) {
1617                    /* JMPZNZ(false,L1,L2) -> JMP(L1) */
1618                    zend_code_block *todel;
1619
1620                    literal_dtor(&ZEND_OP1_LITERAL(last_op));
1621                    last_op->opcode = ZEND_JMP;
1622                    SET_UNUSED(last_op->op1);
1623                    SET_UNUSED(last_op->op2);
1624                    block->op1_to = block->op2_to;
1625                    todel = block->ext_to;
1626                    block->op2_to = NULL;
1627                    block->ext_to = NULL;
1628                    del_source(block, todel);
1629                } else {
1630                    /* JMPZNZ(true,L1,L2) -> JMP(L2) */
1631                    zend_code_block *todel;
1632
1633                    literal_dtor(&ZEND_OP1_LITERAL(last_op));
1634                    last_op->opcode = ZEND_JMP;
1635                    SET_UNUSED(last_op->op1);
1636                    SET_UNUSED(last_op->op2);
1637                    block->op1_to = block->ext_to;
1638                    todel =  block->op2_to;
1639                    block->op2_to = NULL;
1640                    block->ext_to = NULL;
1641                    del_source(block, todel);
1642                }
1643            } else if (block->op2_to == block->ext_to) {
1644                /* both goto the same one - it's JMP */
1645                /* JMPZNZ(?,L,L) -> JMP(L) */
1646                last_op->opcode = ZEND_JMP;
1647                SET_UNUSED(last_op->op1);
1648                SET_UNUSED(last_op->op2);
1649                block->op1_to = block->op2_to;
1650                block->op2_to = NULL;
1651                block->ext_to = NULL;
1652            } else if (block->op2_to == next) {
1653                /* jumping to next on Z - can follow to it and jump only on NZ */
1654                /* JMPZNZ(X,L1,L2) L1: -> JMPNZ(X,L2) */
1655                last_op->opcode = ZEND_JMPNZ;
1656                block->op2_to = block->ext_to;
1657                block->follow_to = next;
1658                block->ext_to = NULL;
1659                /* no need to add source - it's block->op2_to */
1660            } else if (block->ext_to == next) {
1661                /* jumping to next on NZ - can follow to it and jump only on Z */
1662                /* JMPZNZ(X,L1,L2) L2: -> JMPZ(X,L1) */
1663                last_op->opcode = ZEND_JMPZ;
1664                block->follow_to = next;
1665                block->ext_to = NULL;
1666                /* no need to add source - it's block->ext_to */
1667            }
1668
1669            if (last_op->opcode == ZEND_JMPZNZ && block->op2_to) {
1670                zend_uchar same_type = ZEND_OP1_TYPE(last_op);
1671                zend_uchar same_var = VAR_NUM_EX(last_op->op1);
1672                zend_op *target;
1673                zend_op *target_end;
1674                zend_code_block *target_block = block->op2_to;
1675
1676next_target_znz:
1677                target = target_block->start_opline;
1678                target_end = target_block->start_opline + target_block->len;
1679                while (target < target_end && target->opcode == ZEND_NOP) {
1680                    target++;
1681                }
1682                /* next block is only NOP's */
1683                if (target == target_end) {
1684                    target_block = target_block->follow_to;
1685                    goto next_target_znz;
1686                } else if (target_block->op2_to &&
1687                           (target->opcode == ZEND_JMPZ || target->opcode == ZEND_JMPZNZ) &&
1688                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1689                           same_type == ZEND_OP1_TYPE(target) &&
1690                           same_var == VAR_NUM_EX(target->op1) &&
1691                           !target_block->protected) {
1692                    /* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
1693                    del_source(block, block->op2_to);
1694                    block->op2_to = target_block->op2_to;
1695                    ADD_SOURCE(block, block->op2_to);
1696                } else if (target->opcode == ZEND_JMPNZ &&
1697                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1698                           same_type == ZEND_OP1_TYPE(target) &&
1699                           same_var == VAR_NUM_EX(target->op1) &&
1700                           target_block->follow_to &&
1701                           !target_block->protected) {
1702                    /* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
1703                    del_source(block, block->op2_to);
1704                    block->op2_to = target_block->follow_to;
1705                    ADD_SOURCE(block, block->op2_to);
1706                } else if (target_block->op1_to &&
1707                           target->opcode == ZEND_JMP &&
1708                           !target_block->protected) {
1709                    /* JMPZNZ(X, L1, L2), L1: JMP(L3) -> JMPZNZ(X, L3, L2) */
1710                    del_source(block, block->op2_to);
1711                    block->op2_to = target_block->op1_to;
1712                    ADD_SOURCE(block, block->op2_to);
1713                }
1714            }
1715            break;
1716        }
1717    }
1718}
1719
1720/* Global data dependencies */
1721
1722#define T_USAGE(op) do { \
1723        if ((op ## _type & (IS_VAR | IS_TMP_VAR)) && \
1724           !defined_here[VAR_NUM(op.var)] && !used_ext[VAR_NUM(op.var)]) {  \
1725            used_ext[VAR_NUM(op.var)] = 1;                                  \
1726        } \
1727    } while (0)
1728
1729#define NEVER_USED(op) ((op ## _type & (IS_VAR | IS_TMP_VAR)) && !usage[VAR_NUM(op.var)]) /* !used_ext[op.var] && */
1730#define RES_NEVER_USED(opline) (opline->result_type == IS_UNUSED || NEVER_USED(opline->result))
1731
1732/* Find a set of variables which are used outside of the block where they are
1733 * defined. We won't apply some optimization patterns for such variables. */
1734static void zend_t_usage(zend_code_block *block, zend_op_array *op_array, char *used_ext, zend_optimizer_ctx *ctx)
1735{
1736    zend_code_block *next_block = block->next;
1737    char *usage;
1738    char *defined_here;
1739    void *checkpoint;
1740
1741    if (op_array->T == 0) {
1742        /* shortcut - if no Ts, nothing to do */
1743        return;
1744    }
1745
1746    checkpoint = zend_arena_checkpoint(ctx->arena);
1747    usage = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1748    memset(usage, 0, op_array->last_var + op_array->T);
1749    defined_here = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1750
1751    while (next_block) {
1752        zend_op *opline = next_block->start_opline;
1753        zend_op *end = opline + next_block->len;
1754
1755        if (!next_block->access) {
1756            next_block = next_block->next;
1757            continue;
1758        }
1759        memset(defined_here, 0, op_array->last_var + op_array->T);
1760
1761        while (opline<end) {
1762            T_USAGE(opline->op1);
1763            T_USAGE(opline->op2);
1764
1765            if (RESULT_USED(opline)) {
1766                if (!defined_here[VAR_NUM(ZEND_RESULT(opline).var)] && !used_ext[VAR_NUM(ZEND_RESULT(opline).var)] &&
1767                    (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT ||
1768                     opline->opcode == ZEND_RECV_VARIADIC ||
1769                    (opline->opcode == ZEND_OP_DATA && ZEND_RESULT_TYPE(opline) == IS_TMP_VAR) ||
1770                    opline->opcode == ZEND_ADD_ARRAY_ELEMENT)) {
1771                    /* these opcodes use the result as argument */
1772                    used_ext[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
1773                }
1774                defined_here[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
1775            }
1776            opline++;
1777        }
1778        next_block = next_block->next;
1779    }
1780
1781#if DEBUG_BLOCKPASS
1782    {
1783        int i;
1784        for (i = op_array->last_var; i< op_array->T; i++) {
1785            fprintf(stderr, "T%d: %c\n", i, used_ext[i] + '0');
1786        }
1787    }
1788#endif
1789
1790    while (block) {
1791        zend_op *opline = block->start_opline + block->len - 1;
1792
1793        if (!block->access) {
1794            block = block->next;
1795            continue;
1796        }
1797
1798        memcpy(usage, used_ext, op_array->last_var + op_array->T);
1799
1800        while (opline >= block->start_opline) {
1801            /* usage checks */
1802            if (RES_NEVER_USED(opline)) {
1803                switch (opline->opcode) {
1804                    case ZEND_ASSIGN_ADD:
1805                    case ZEND_ASSIGN_SUB:
1806                    case ZEND_ASSIGN_MUL:
1807                    case ZEND_ASSIGN_DIV:
1808                    case ZEND_ASSIGN_MOD:
1809                    case ZEND_ASSIGN_SL:
1810                    case ZEND_ASSIGN_SR:
1811                    case ZEND_ASSIGN_CONCAT:
1812                    case ZEND_ASSIGN_BW_OR:
1813                    case ZEND_ASSIGN_BW_AND:
1814                    case ZEND_ASSIGN_BW_XOR:
1815                    case ZEND_PRE_INC:
1816                    case ZEND_PRE_DEC:
1817                    case ZEND_POST_INC:
1818                    case ZEND_POST_DEC:
1819                    case ZEND_ASSIGN:
1820                    case ZEND_ASSIGN_REF:
1821                    case ZEND_DO_FCALL:
1822                        if (ZEND_RESULT_TYPE(opline) == IS_VAR) {
1823                            ZEND_RESULT_TYPE(opline) |= EXT_TYPE_UNUSED;
1824                        }
1825                        break;
1826                    case ZEND_QM_ASSIGN:
1827                    case ZEND_BOOL:
1828                    case ZEND_BOOL_NOT:
1829                        if (ZEND_OP1_TYPE(opline) == IS_CONST) {
1830                            literal_dtor(&ZEND_OP1_LITERAL(opline));
1831                        }
1832                        MAKE_NOP(opline);
1833                        break;
1834                    case ZEND_JMPZ_EX:
1835                    case ZEND_JMPNZ_EX:
1836                        opline->opcode -= 3;
1837                        SET_UNUSED(opline->result);
1838                        break;
1839                }
1840            }
1841
1842            if (opline->opcode == ZEND_RECV ||
1843                opline->opcode == ZEND_RECV_INIT ||
1844                opline->opcode == ZEND_RECV_VARIADIC ||
1845                opline->opcode == ZEND_ADD_ARRAY_ELEMENT) {
1846                if (ZEND_OP1_TYPE(opline) == IS_VAR || ZEND_OP1_TYPE(opline) == IS_TMP_VAR) {
1847                    usage[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
1848                }
1849            } else {
1850                if (RESULT_USED(opline)) {
1851                    usage[VAR_NUM(ZEND_RESULT(opline).var)] = 0;
1852                }
1853            }
1854
1855            if (ZEND_OP1_TYPE(opline) == IS_VAR || ZEND_OP1_TYPE(opline) == IS_TMP_VAR) {
1856                usage[VAR_NUM(ZEND_OP1(opline).var)] = 1;
1857            }
1858
1859            if (ZEND_OP2_TYPE(opline) == IS_VAR || ZEND_OP2_TYPE(opline) == IS_TMP_VAR) {
1860                usage[VAR_NUM(ZEND_OP2(opline).var)] = 1;
1861            }
1862
1863            if ((ZEND_RESULT_TYPE(opline) & IS_VAR) &&
1864                (ZEND_RESULT_TYPE(opline) & EXT_TYPE_UNUSED) &&
1865                usage[VAR_NUM(ZEND_RESULT(opline).var)]) {
1866                ZEND_RESULT_TYPE(opline) &= ~EXT_TYPE_UNUSED;
1867            }
1868
1869            opline--;
1870        }
1871        block = block->next;
1872    } /* end blocks */
1873
1874    zend_arena_release(&ctx->arena, checkpoint);
1875}
1876
1877#define PASSES 3
1878
1879void optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx)
1880{
1881    zend_cfg cfg;
1882    zend_code_block *cur_block;
1883    int pass;
1884    char *usage;
1885    void *checkpoint;
1886
1887#if DEBUG_BLOCKPASS
1888    fprintf(stderr, "File %s func %s\n", op_array->filename, op_array->function_name? op_array->function_name : "main");
1889    fflush(stderr);
1890#endif
1891
1892    if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
1893        return;
1894    }
1895
1896    /* Build CFG */
1897    checkpoint = zend_arena_checkpoint(ctx->arena);
1898    if (!find_code_blocks(op_array, &cfg, ctx)) {
1899        zend_arena_release(&ctx->arena, checkpoint);
1900        return;
1901    }
1902
1903    zend_rebuild_access_path(&cfg, op_array, 0, ctx);
1904    /* full rebuild here to produce correct sources! */
1905    if (op_array->last_var || op_array->T) {
1906        cfg.Tsource = zend_arena_calloc(&ctx->arena, op_array->last_var + op_array->T, sizeof(zend_op *));
1907        cfg.same_t = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1908        usage = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1909    } else {
1910        cfg.Tsource = NULL;
1911        cfg.same_t = NULL;
1912        usage = NULL;
1913    }
1914    for (pass = 0; pass < PASSES; pass++) {
1915        /* Compute data dependencies */
1916        memset(usage, 0, op_array->last_var + op_array->T);
1917        zend_t_usage(cfg.blocks, op_array, usage, ctx);
1918
1919        /* optimize each basic block separately */
1920        for (cur_block = cfg.blocks; cur_block; cur_block = cur_block->next) {
1921            if (!cur_block->access) {
1922                continue;
1923            }
1924            zend_optimize_block(cur_block, op_array, usage, &cfg, ctx);
1925        }
1926
1927        /* Jump optimization for each block */
1928        for (cur_block = cfg.blocks; cur_block; cur_block = cur_block->next) {
1929            if (!cur_block->access) {
1930                continue;
1931            }
1932            zend_jmp_optimization(cur_block, op_array, cfg.blocks, &cfg, ctx);
1933        }
1934
1935        /* Eliminate unreachable basic blocks */
1936        zend_rebuild_access_path(&cfg, op_array, 1, ctx);
1937    }
1938
1939    memset(usage, 0, op_array->last_var + op_array->T);
1940    zend_t_usage(cfg.blocks, op_array, usage, ctx);
1941    assemble_code_blocks(&cfg, op_array);
1942
1943    /* Destroy CFG */
1944    zend_arena_release(&ctx->arena, checkpoint);
1945}
1946