1/*
2   +----------------------------------------------------------------------+
3   | Zend OPcache                                                         |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 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->protected && 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                    opline->opcode == ZEND_ADD_STRING ||
906                    opline->opcode == ZEND_ADD_CHAR) &&
907                  ZEND_OP2_TYPE(opline) == IS_CONST &&
908                  ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
909                  VAR_SOURCE(opline->op1) &&
910                  (VAR_SOURCE(opline->op1)->opcode == ZEND_CONCAT ||
911                   VAR_SOURCE(opline->op1)->opcode == ZEND_ADD_STRING ||
912                   VAR_SOURCE(opline->op1)->opcode == ZEND_ADD_CHAR) &&
913                  ZEND_OP2_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
914                  ZEND_RESULT(VAR_SOURCE(opline->op1)).var == ZEND_OP1(opline).var) {
915            /* compress consecutive CONCAT/ADD_STRING/ADD_CHARs */
916            zend_op *src = VAR_SOURCE(opline->op1);
917            int l, old_len;
918
919            if (opline->opcode == ZEND_ADD_CHAR) {
920                char c = (char)Z_LVAL(ZEND_OP2_LITERAL(opline));
921                ZVAL_STRINGL(&ZEND_OP2_LITERAL(opline), &c, 1);
922            } else if (Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
923                convert_to_string_safe(&ZEND_OP2_LITERAL(opline));
924            }
925            if (src->opcode == ZEND_ADD_CHAR) {
926                char c = (char)Z_LVAL(ZEND_OP2_LITERAL(src));
927                ZVAL_STRINGL(&ZEND_OP2_LITERAL(src), &c, 1);
928            } else if (Z_TYPE(ZEND_OP2_LITERAL(src)) != IS_STRING) {
929                convert_to_string_safe(&ZEND_OP2_LITERAL(src));
930            }
931
932            VAR_UNSET(opline->op1);
933            if (ZEND_OP1_TYPE(src) == IS_UNUSED) {
934                /* 5.3 may use IS_UNUSED as first argument to ZEND_ADD_... */
935                opline->opcode = ZEND_ADD_STRING;
936            } else {
937                opline->opcode = ZEND_CONCAT;
938            }
939            COPY_NODE(opline->op1, src->op1);
940            old_len = Z_STRLEN(ZEND_OP2_LITERAL(src));
941            l = old_len + Z_STRLEN(ZEND_OP2_LITERAL(opline));
942            if (!Z_REFCOUNTED(ZEND_OP2_LITERAL(src))) {
943                zend_string *tmp = zend_string_alloc(l, 0);
944                memcpy(tmp->val, Z_STRVAL(ZEND_OP2_LITERAL(src)), old_len);
945                Z_STR(ZEND_OP2_LITERAL(last_op)) = tmp;
946            } else {
947                Z_STR(ZEND_OP2_LITERAL(src)) = zend_string_realloc(Z_STR(ZEND_OP2_LITERAL(src)), l, 0);
948            }
949            Z_TYPE_INFO(ZEND_OP2_LITERAL(last_op)) = IS_STRING_EX;
950            memcpy(Z_STRVAL(ZEND_OP2_LITERAL(src)) + old_len, Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)));
951            Z_STRVAL(ZEND_OP2_LITERAL(src))[l] = '\0';
952            zend_string_release(Z_STR(ZEND_OP2_LITERAL(opline)));
953            Z_STR(ZEND_OP2_LITERAL(opline)) = zend_new_interned_string(Z_STR(ZEND_OP2_LITERAL(src)));
954            if (!Z_REFCOUNTED(ZEND_OP2_LITERAL(opline))) {
955                Z_TYPE_FLAGS(ZEND_OP2_LITERAL(opline)) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
956            }
957            ZVAL_NULL(&ZEND_OP2_LITERAL(src));
958            MAKE_NOP(src);
959        } else if ((opline->opcode == ZEND_ADD ||
960                    opline->opcode == ZEND_SUB ||
961                    opline->opcode == ZEND_MUL ||
962                    opline->opcode == ZEND_DIV ||
963                    opline->opcode == ZEND_MOD ||
964                    opline->opcode == ZEND_SL ||
965                    opline->opcode == ZEND_SR ||
966                    opline->opcode == ZEND_CONCAT ||
967                    opline->opcode == ZEND_IS_EQUAL ||
968                    opline->opcode == ZEND_IS_NOT_EQUAL ||
969                    opline->opcode == ZEND_IS_SMALLER ||
970                    opline->opcode == ZEND_IS_SMALLER_OR_EQUAL ||
971                    opline->opcode == ZEND_IS_IDENTICAL ||
972                    opline->opcode == ZEND_IS_NOT_IDENTICAL ||
973                    opline->opcode == ZEND_BOOL_XOR ||
974                    opline->opcode == ZEND_BW_OR ||
975                    opline->opcode == ZEND_BW_AND ||
976                    opline->opcode == ZEND_BW_XOR) &&
977                    ZEND_OP1_TYPE(opline)==IS_CONST &&
978                    ZEND_OP2_TYPE(opline)==IS_CONST) {
979            /* evaluate constant expressions */
980            int (*binary_op)(zval *result, zval *op1, zval *op2) = get_binary_op(opline->opcode);
981            zval result;
982            int er;
983
984            if ((opline->opcode == ZEND_DIV || opline->opcode == ZEND_MOD) &&
985                ((Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_LONG &&
986                  Z_LVAL(ZEND_OP2_LITERAL(opline)) == 0) ||
987                 (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_DOUBLE &&
988                  Z_DVAL(ZEND_OP2_LITERAL(opline)) == 0.0))) {
989                if (RESULT_USED(opline)) {
990                    SET_VAR_SOURCE(opline);
991                }
992                opline++;
993                continue;
994            }
995            er = EG(error_reporting);
996            EG(error_reporting) = 0;
997            if (binary_op(&result, &ZEND_OP1_LITERAL(opline), &ZEND_OP2_LITERAL(opline)) == SUCCESS) {
998                literal_dtor(&ZEND_OP1_LITERAL(opline));
999                literal_dtor(&ZEND_OP2_LITERAL(opline));
1000                opline->opcode = ZEND_QM_ASSIGN;
1001                SET_UNUSED(opline->op2);
1002                zend_optimizer_update_op1_const(op_array, opline, &result);
1003            }
1004            EG(error_reporting) = er;
1005        } else if ((opline->opcode == ZEND_BOOL ||
1006                    opline->opcode == ZEND_BOOL_NOT ||
1007                    opline->opcode == ZEND_BW_NOT) && ZEND_OP1_TYPE(opline) == IS_CONST) {
1008            /* evaluate constant unary ops */
1009            unary_op_type unary_op = get_unary_op(opline->opcode);
1010            zval result;
1011
1012            if (unary_op) {
1013                unary_op(&result, &ZEND_OP1_LITERAL(opline));
1014                literal_dtor(&ZEND_OP1_LITERAL(opline));
1015            } else {
1016                /* BOOL */
1017                result = ZEND_OP1_LITERAL(opline);
1018                convert_to_boolean(&result);
1019                ZVAL_NULL(&ZEND_OP1_LITERAL(opline));
1020            }
1021            opline->opcode = ZEND_QM_ASSIGN;
1022            zend_optimizer_update_op1_const(op_array, opline, &result);
1023        } else if ((opline->opcode == ZEND_RETURN || opline->opcode == ZEND_EXIT) &&
1024                    (ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
1025                    VAR_SOURCE(opline->op1) &&
1026                    VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN) {
1027            /* T = QM_ASSIGN(X), RETURN(T) to RETURN(X) */
1028            zend_op *src = VAR_SOURCE(opline->op1);
1029            VAR_UNSET(opline->op1);
1030            COPY_NODE(opline->op1, src->op1);
1031            MAKE_NOP(src);
1032        } else if ((opline->opcode == ZEND_ADD_STRING ||
1033                    opline->opcode == ZEND_ADD_CHAR ||
1034                    opline->opcode == ZEND_ADD_VAR ||
1035                    opline->opcode == ZEND_CONCAT) &&
1036                    ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
1037                    VAR_SOURCE(opline->op1) &&
1038                    VAR_SOURCE(opline->op1)->opcode == ZEND_CONCAT &&
1039                    ZEND_OP2_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
1040                    Z_TYPE(ZEND_OP2_LITERAL(VAR_SOURCE(opline->op1))) == IS_STRING &&
1041                    Z_STRLEN(ZEND_OP2_LITERAL(VAR_SOURCE(opline->op1))) == 0) {
1042            /* convert T = CONCAT(X,''), T = ADD_STRING(T, Y) to T = CONCAT(X,Y) */
1043            zend_op *src = VAR_SOURCE(opline->op1);
1044            VAR_UNSET(opline->op1);
1045            COPY_NODE(opline->op1, src->op1);
1046            if (opline->opcode == ZEND_ADD_CHAR) {
1047                char c = (char)Z_LVAL(ZEND_OP2_LITERAL(opline));
1048                ZVAL_STRINGL(&ZEND_OP2_LITERAL(opline), &c, 1);
1049            }
1050            opline->opcode = ZEND_CONCAT;
1051            literal_dtor(&ZEND_OP2_LITERAL(src)); /* will take care of empty_string too */
1052            MAKE_NOP(src);
1053        } else if ((opline->opcode == ZEND_ADD_STRING ||
1054                    opline->opcode == ZEND_ADD_CHAR ||
1055                    opline->opcode == ZEND_ADD_VAR ||
1056                    opline->opcode == ZEND_CONCAT) &&
1057                    (ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
1058                    VAR_SOURCE(opline->op1) &&
1059                    VAR_SOURCE(opline->op1)->opcode == ZEND_CAST &&
1060                    VAR_SOURCE(opline->op1)->extended_value == IS_STRING) {
1061            /* convert T1 = CAST(STRING, X), T2 = CONCAT(T1, Y) to T2 = CONCAT(X,Y) */
1062            zend_op *src = VAR_SOURCE(opline->op1);
1063            VAR_UNSET(opline->op1);
1064            COPY_NODE(opline->op1, src->op1);
1065            if (opline->opcode == ZEND_ADD_CHAR) {
1066                char c = (char)Z_LVAL(ZEND_OP2_LITERAL(opline));
1067                ZVAL_STRINGL(&ZEND_OP2_LITERAL(opline), &c, 1);
1068            }
1069            opline->opcode = ZEND_CONCAT;
1070            MAKE_NOP(src);
1071        } else if (opline->opcode == ZEND_QM_ASSIGN &&
1072                    ZEND_OP1_TYPE(opline) == ZEND_RESULT_TYPE(opline) &&
1073                    ZEND_OP1(opline).var == ZEND_RESULT(opline).var) {
1074            /* strip T = QM_ASSIGN(T) */
1075            MAKE_NOP(opline);
1076        } else if (opline->opcode == ZEND_BOOL &&
1077                    ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
1078                    VAR_SOURCE(opline->op1) &&
1079                    (VAR_SOURCE(opline->op1)->opcode == ZEND_IS_EQUAL ||
1080                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_NOT_EQUAL ||
1081                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_SMALLER ||
1082                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_SMALLER_OR_EQUAL ||
1083                    VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL ||
1084                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_IDENTICAL ||
1085                    VAR_SOURCE(opline->op1)->opcode == ZEND_IS_NOT_IDENTICAL ||
1086                    VAR_SOURCE(opline->op1)->opcode == ZEND_ISSET_ISEMPTY_VAR ||
1087                    VAR_SOURCE(opline->op1)->opcode == ZEND_ISSET_ISEMPTY_DIM_OBJ) &&
1088                    !used_ext[VAR_NUM(ZEND_OP1(opline).var)]) {
1089            /* T = IS_SMALLER(X, Y), T1 = BOOL(T) => T = IS_SMALLER(X, Y), T1 = QM_ASSIGN(T) */
1090            zend_op *src = VAR_SOURCE(opline->op1);
1091            COPY_NODE(src->result, opline->result);
1092            SET_VAR_SOURCE(src);
1093            MAKE_NOP(opline);
1094        }
1095        /* get variable source */
1096        if (RESULT_USED(opline)) {
1097            SET_VAR_SOURCE(opline);
1098        }
1099        if (opline->opcode != ZEND_NOP) {
1100            last_op = opline;
1101        }
1102        opline++;
1103    }
1104
1105    strip_nop(block, ctx);
1106}
1107
1108/* Rebuild plain (optimized) op_array from CFG */
1109static void assemble_code_blocks(zend_cfg *cfg, zend_op_array *op_array)
1110{
1111    zend_code_block *blocks = cfg->blocks;
1112    zend_op *new_opcodes = emalloc(op_array->last * sizeof(zend_op));
1113    zend_op *opline = new_opcodes;
1114    zend_code_block *cur_block = blocks;
1115
1116    /* Copy code of reachable blocks into a single buffer */
1117    while (cur_block) {
1118        if (cur_block->access) {
1119            memcpy(opline, cur_block->start_opline, cur_block->len * sizeof(zend_op));
1120            cur_block->start_opline = opline;
1121            opline += cur_block->len;
1122            if ((opline - 1)->opcode == ZEND_JMP) {
1123                zend_code_block *next;
1124                next = cur_block->next;
1125                while (next && !next->access) {
1126                    next = next->next;
1127                }
1128                if (next && next == cur_block->op1_to) {
1129                    /* JMP to the next block - strip it */
1130                    cur_block->follow_to = cur_block->op1_to;
1131                    cur_block->op1_to = NULL;
1132                    MAKE_NOP((opline - 1));
1133                    opline--;
1134                    cur_block->len--;
1135                }
1136            }
1137        } else {
1138            /* this block will not be used, delete all constants there */
1139            zend_op *_opl;
1140            zend_op *end = cur_block->start_opline + cur_block->len;
1141            for (_opl = cur_block->start_opline; _opl && _opl < end; _opl++) {
1142                if (ZEND_OP1_TYPE(_opl) == IS_CONST) {
1143                    literal_dtor(&ZEND_OP1_LITERAL(_opl));
1144                }
1145                if (ZEND_OP2_TYPE(_opl) == IS_CONST) {
1146                    literal_dtor(&ZEND_OP2_LITERAL(_opl));
1147                }
1148            }
1149        }
1150        cur_block = cur_block->next;
1151    }
1152
1153    if ((opline-1)->opcode == ZEND_THROW) {
1154        /* if we finished with THROW, we need to add space between THROW and HANDLE to not confuse
1155           zend_throw_internal */
1156        MAKE_NOP(opline);
1157        opline->lineno = opline[-1].lineno;
1158        opline++;
1159    }
1160
1161    op_array->last = opline-new_opcodes;
1162
1163    /* adjust exception jump targets */
1164    if (op_array->last_try_catch) {
1165        int i, j;
1166        for (i = 0, j = 0; i< op_array->last_try_catch; i++) {
1167            if (cfg->try[i]->access) {
1168                op_array->try_catch_array[j].try_op = cfg->try[i]->start_opline - new_opcodes;
1169                op_array->try_catch_array[j].catch_op = cfg->catch[i]->start_opline - new_opcodes;
1170                j++;
1171            }
1172        }
1173        op_array->last_try_catch = j;
1174    }
1175
1176    /* adjust loop jump targets */
1177    if (op_array->last_brk_cont) {
1178        int i;
1179        for (i = 0; i< op_array->last_brk_cont; i++) {
1180            op_array->brk_cont_array[i].start = cfg->loop_start[i]->start_opline - new_opcodes;
1181            op_array->brk_cont_array[i].cont = cfg->loop_cont[i]->start_opline - new_opcodes;
1182            op_array->brk_cont_array[i].brk = cfg->loop_brk[i]->start_opline - new_opcodes;
1183        }
1184    }
1185
1186    /* adjust jump targets */
1187    for (cur_block = blocks; cur_block; cur_block = cur_block->next) {
1188        if (!cur_block->access) {
1189            continue;
1190        }
1191        opline = cur_block->start_opline + cur_block->len - 1;
1192        if (opline->opcode == ZEND_OP_DATA) {
1193            opline--;
1194        }
1195        if (cur_block->op1_to) {
1196            ZEND_OP1(opline).opline_num = cur_block->op1_to->start_opline - new_opcodes;
1197        }
1198        if (cur_block->op2_to) {
1199            ZEND_OP2(opline).opline_num = cur_block->op2_to->start_opline - new_opcodes;
1200        }
1201        if (cur_block->ext_to) {
1202            opline->extended_value = cur_block->ext_to->start_opline - new_opcodes;
1203        }
1204        print_block(cur_block, new_opcodes, "Out ");
1205    }
1206    efree(op_array->opcodes);
1207    op_array->opcodes = erealloc(new_opcodes, op_array->last * sizeof(zend_op));
1208
1209    /* adjust early binding list */
1210    if (op_array->early_binding != (uint32_t)-1) {
1211        uint32_t *opline_num = &op_array->early_binding;
1212        zend_op *end;
1213
1214        opline = op_array->opcodes;
1215        end = opline + op_array->last;
1216        while (opline < end) {
1217            if (opline->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
1218                *opline_num = opline - op_array->opcodes;
1219                opline_num = &ZEND_RESULT(opline).opline_num;
1220            }
1221            ++opline;
1222        }
1223        *opline_num = -1;
1224    }
1225}
1226
1227static void zend_jmp_optimization(zend_code_block *block, zend_op_array *op_array, zend_code_block *blocks, zend_cfg *cfg, zend_optimizer_ctx *ctx)
1228{
1229    /* last_op is the last opcode of the current block */
1230    zend_op *last_op = (block->start_opline + block->len - 1);
1231
1232    if (!block->len) {
1233        return;
1234    }
1235    switch (last_op->opcode) {
1236        case ZEND_JMP:
1237            {
1238                zend_op *target = block->op1_to->start_opline;
1239                zend_code_block *next = block->next;
1240
1241                while (next && !next->access) {
1242                    /* find used one */
1243                    next = next->next;
1244                }
1245
1246                /* JMP(next) -> NOP */
1247                if (block->op1_to == next) {
1248                    block->follow_to = block->op1_to;
1249                    block->op1_to = NULL;
1250                    MAKE_NOP(last_op);
1251                    block->len--;
1252                    if (block->len == 0) {
1253                        /* this block is nothing but NOP now */
1254                        delete_code_block(block, ctx);
1255                    }
1256                    break;
1257                }
1258
1259                if (((target->opcode == ZEND_JMP &&
1260                    block->op1_to != block->op1_to->op1_to) ||
1261                    target->opcode == ZEND_JMPZNZ) &&
1262                    !block->op1_to->protected) {
1263                    /* JMP L, L: JMP L1 -> JMP L1 */
1264                    /* JMP L, L: JMPZNZ L1,L2 -> JMPZNZ L1,L2 */
1265                    *last_op = *target;
1266                    if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1267                        zval zv = ZEND_OP1_LITERAL(last_op);
1268                        zval_copy_ctor(&zv);
1269                        last_op->op1.constant = zend_optimizer_add_literal(op_array, &zv);
1270                    }
1271                    del_source(block, block->op1_to);
1272                    if (block->op1_to->op2_to) {
1273                        block->op2_to = block->op1_to->op2_to;
1274                        ADD_SOURCE(block, block->op2_to);
1275                    }
1276                    if (block->op1_to->ext_to) {
1277                        block->ext_to = block->op1_to->ext_to;
1278                        ADD_SOURCE(block, block->ext_to);
1279                    }
1280                    if (block->op1_to->op1_to) {
1281                        block->op1_to = block->op1_to->op1_to;
1282                        ADD_SOURCE(block, block->op1_to);
1283                    } else {
1284                        block->op1_to = NULL;
1285                    }
1286                } else if (target->opcode == ZEND_RETURN ||
1287                          target->opcode == ZEND_RETURN_BY_REF ||
1288                          target->opcode == ZEND_FAST_RET ||
1289                          target->opcode == ZEND_EXIT) {
1290                    /* JMP L, L: RETURN to immediate RETURN */
1291                    *last_op = *target;
1292                    if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1293                        zval zv = ZEND_OP1_LITERAL(last_op);
1294                        zval_copy_ctor(&zv);
1295                        last_op->op1.constant = zend_optimizer_add_literal(op_array, &zv);
1296                    }
1297                    del_source(block, block->op1_to);
1298                    block->op1_to = NULL;
1299#if 0
1300                /* Temporarily disabled - see bug #0025274 */
1301                } else if (0&& block->op1_to != block &&
1302                       block->op1_to != blocks &&
1303                           op_array->last_try_catch == 0 &&
1304                           target->opcode != ZEND_FREE) {
1305                    /* Block Reordering (saves one JMP on each "for" loop iteration)
1306                     * It is disabled for some cases (ZEND_FREE)
1307                     * which may break register allocation.
1308                     */
1309                    zend_bool can_reorder = 0;
1310                    zend_block_source *cs = block->op1_to->sources;
1311
1312                    /* the "target" block doesn't had any followed block */
1313                    while(cs) {
1314                        if (cs->from->follow_to == block->op1_to) {
1315                            can_reorder = 0;
1316                            break;
1317                        }
1318                        cs = cs->next;
1319                    }
1320                    if (can_reorder) {
1321                        next = block->op1_to;
1322                        /* the "target" block is not followed by current "block" */
1323                        while (next->follow_to != NULL) {
1324                            if (next->follow_to == block) {
1325                                can_reorder = 0;
1326                                break;
1327                            }
1328                            next = next->follow_to;
1329                        }
1330                        if (can_reorder) {
1331                            zend_code_block *prev = blocks;
1332
1333                            while (prev->next != block->op1_to) {
1334                                prev = prev->next;
1335                            }
1336                            prev->next = next->next;
1337                            next->next = block->next;
1338                            block->next = block->op1_to;
1339
1340                            block->follow_to = block->op1_to;
1341                            block->op1_to = NULL;
1342                            MAKE_NOP(last_op);
1343                            block->len--;
1344                            if(block->len == 0) {
1345                                /* this block is nothing but NOP now */
1346                                delete_code_block(block, ctx);
1347                            }
1348                            break;
1349                        }
1350                    }
1351#endif
1352                }
1353            }
1354            break;
1355
1356        case ZEND_JMPZ:
1357        case ZEND_JMPNZ:
1358            /* constant conditional JMPs */
1359            if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1360                int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(last_op));
1361
1362                if (last_op->opcode == ZEND_JMPZ) {
1363                    should_jmp = !should_jmp;
1364                }
1365                literal_dtor(&ZEND_OP1_LITERAL(last_op));
1366                ZEND_OP1_TYPE(last_op) = IS_UNUSED;
1367                if (should_jmp) {
1368                    /* JMPNZ(true) -> JMP */
1369                    last_op->opcode = ZEND_JMP;
1370                    COPY_NODE(last_op->op1, last_op->op2);
1371                    block->op1_to = block->op2_to;
1372                    del_source(block, block->follow_to);
1373                    block->op2_to = NULL;
1374                    block->follow_to = NULL;
1375                } else {
1376                    /* JMPNZ(false) -> NOP */
1377                    MAKE_NOP(last_op);
1378                    del_source(block, block->op2_to);
1379                    block->op2_to = NULL;
1380                }
1381                break;
1382            }
1383
1384            if (block->op2_to) {
1385                zend_uchar same_type = ZEND_OP1_TYPE(last_op);
1386                uint32_t same_var = VAR_NUM_EX(last_op->op1);
1387                zend_op *target;
1388                zend_op *target_end;
1389                zend_code_block *target_block = block->op2_to;;
1390
1391next_target:
1392                target = target_block->start_opline;
1393                target_end = target_block->start_opline + target_block->len;
1394                while (target < target_end && target->opcode == ZEND_NOP) {
1395                    target++;
1396                }
1397
1398                /* next block is only NOP's */
1399                if (target == target_end) {
1400                    target_block = target_block->follow_to;
1401                    goto next_target;
1402                } else if (target->opcode == INV_COND(last_op->opcode) &&
1403                    /* JMPZ(X, L), L: JMPNZ(X, L2) -> JMPZ(X, L+1) */
1404                   (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1405                   same_type == ZEND_OP1_TYPE(target) &&
1406                   same_var == VAR_NUM_EX(target->op1) &&
1407                   target_block->follow_to &&
1408                   !target_block->protected
1409                   ) {
1410                    del_source(block, block->op2_to);
1411                    block->op2_to = target_block->follow_to;
1412                    ADD_SOURCE(block, block->op2_to);
1413                } else if (target->opcode == INV_COND_EX(last_op->opcode) &&
1414                            (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1415                            same_type == ZEND_OP1_TYPE(target) &&
1416                            same_var == VAR_NUM_EX(target->op1) &&
1417                            target_block->follow_to &&
1418                            !target_block->protected) {
1419                    /* JMPZ(X, L), L: X = JMPNZ_EX(X, L2) -> JMPZ(X, L+1) */
1420                    last_op->opcode += 3;
1421                    last_op->result = target->result;
1422                    del_source(block, block->op2_to);
1423                    block->op2_to = target_block->follow_to;
1424                    ADD_SOURCE(block, block->op2_to);
1425                } else if (target_block->op2_to &&
1426                           target->opcode == last_op->opcode &&
1427                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1428                           same_type == ZEND_OP1_TYPE(target) &&
1429                           same_var == VAR_NUM_EX(target->op1) &&
1430                           !target_block->protected) {
1431                    /* JMPZ(X, L), L: JMPZ(X, L2) -> JMPZ(X, L2) */
1432                    del_source(block, block->op2_to);
1433                    block->op2_to = target_block->op2_to;
1434                    ADD_SOURCE(block, block->op2_to);
1435                } else if (target_block->op1_to &&
1436                            target->opcode == ZEND_JMP &&
1437                            !target_block->protected) {
1438                    /* JMPZ(X, L), L: JMP(L2) -> JMPZ(X, L2) */
1439                    del_source(block, block->op2_to);
1440                    block->op2_to = target_block->op1_to;
1441                    ADD_SOURCE(block, block->op2_to);
1442                } else if (target_block->op2_to &&
1443                            target_block->ext_to &&
1444                            target->opcode == ZEND_JMPZNZ &&
1445                            (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1446                            same_type == ZEND_OP1_TYPE(target) &&
1447                            same_var == VAR_NUM_EX(target->op1) &&
1448                            !target_block->protected) {
1449                    /* JMPZ(X, L), L: JMPZNZ(X, L2, L3) -> JMPZ(X, L2) */
1450                    del_source(block, block->op2_to);
1451                    if (last_op->opcode == ZEND_JMPZ) {
1452                        block->op2_to = target_block->op2_to;
1453                    } else {
1454                        block->op2_to = target_block->ext_to;
1455                    }
1456                    ADD_SOURCE(block, block->op2_to);
1457                }
1458            }
1459
1460            if (block->follow_to &&
1461                (last_op->opcode == ZEND_JMPZ || last_op->opcode == ZEND_JMPNZ)) {
1462                zend_op *target;
1463                zend_op *target_end;
1464
1465                while (1) {
1466                    target = block->follow_to->start_opline;
1467                    target_end = block->follow_to->start_opline + block->follow_to->len;
1468                    while (target < target_end && target->opcode == ZEND_NOP) {
1469                        target++;
1470                    }
1471
1472                    /* next block is only NOP's */
1473                    if (target == target_end && ! block->follow_to->protected) {
1474                        del_source(block, block->follow_to);
1475                        block->follow_to = block->follow_to->follow_to;
1476                        ADD_SOURCE(block, block->follow_to);
1477                    } else {
1478                        break;
1479                    }
1480                }
1481                /* JMPZ(X,L1), JMP(L2) -> JMPZNZ(X,L1,L2) */
1482                if (target->opcode == ZEND_JMP &&
1483                    block->follow_to->op1_to &&
1484                    !block->follow_to->protected) {
1485                    del_source(block, block->follow_to);
1486                    if (last_op->opcode == ZEND_JMPZ) {
1487                        block->ext_to = block->follow_to->op1_to;
1488                        ADD_SOURCE(block, block->ext_to);
1489                    } else {
1490                        block->ext_to = block->op2_to;
1491                        block->op2_to = block->follow_to->op1_to;
1492                        ADD_SOURCE(block, block->op2_to);
1493                    }
1494                    block->follow_to = NULL;
1495                    last_op->opcode = ZEND_JMPZNZ;
1496                }
1497            }
1498            break;
1499
1500        case ZEND_JMPNZ_EX:
1501        case ZEND_JMPZ_EX:
1502            /* constant conditional JMPs */
1503            if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1504                int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(last_op));
1505
1506                if (last_op->opcode == ZEND_JMPZ_EX) {
1507                    should_jmp = !should_jmp;
1508                }
1509                if (!should_jmp) {
1510                    /* T = JMPZ_EX(true,L)   -> T = QM_ASSIGN(true)
1511                     * T = JMPNZ_EX(false,L) -> T = QM_ASSIGN(false)
1512                     */
1513                    last_op->opcode = ZEND_QM_ASSIGN;
1514                    SET_UNUSED(last_op->op2);
1515                    del_source(block, block->op2_to);
1516                    block->op2_to = NULL;
1517                }
1518                break;
1519            }
1520
1521            if (block->op2_to) {
1522                zend_op *target, *target_end;
1523                char *same_t=NULL;
1524                zend_code_block *target_block;
1525                int var_num = op_array->last_var + op_array->T;
1526
1527                if (var_num <= 0) {
1528                    return;
1529                }
1530                same_t = cfg->same_t;
1531                memset(same_t, 0, var_num);
1532                same_t[VAR_NUM_EX(last_op->op1)] |= ZEND_OP1_TYPE(last_op);
1533                same_t[VAR_NUM_EX(last_op->result)] |= ZEND_RESULT_TYPE(last_op);
1534                target_block = block->op2_to;
1535next_target_ex:
1536                target = target_block->start_opline;
1537                target_end = target_block->start_opline + target_block->len;
1538                while (target < target_end && target->opcode == ZEND_NOP) {
1539                    target++;
1540                }
1541                /* next block is only NOP's */
1542                if (target == target_end) {
1543                    target_block = target_block->follow_to;
1544                    goto next_target_ex;
1545                } else if (target_block->op2_to &&
1546                           target->opcode == last_op->opcode-3 &&
1547                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1548                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1549                           !target_block->protected) {
1550                    /* T = JMPZ_EX(X, L1), L1: JMPZ({X|T}, L2) -> T = JMPZ_EX(X, L2) */
1551                    del_source(block, block->op2_to);
1552                    block->op2_to = target_block->op2_to;
1553                    ADD_SOURCE(block, block->op2_to);
1554                } else if (target_block->op2_to &&
1555                           target->opcode == INV_EX_COND(last_op->opcode) &&
1556                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1557                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1558                           !target_block->protected) {
1559                    /* T = JMPZ_EX(X, L1), L1: JMPNZ({X|T1}, L2) -> T = JMPZ_EX(X, L1+1) */
1560                    del_source(block, block->op2_to);
1561                    block->op2_to = target_block->follow_to;
1562                    ADD_SOURCE(block, block->op2_to);
1563                } else if (target_block->op2_to &&
1564                           target->opcode == INV_EX_COND_EX(last_op->opcode) &&
1565                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1566                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1567                           (same_t[VAR_NUM_EX(target->result)] & ZEND_RESULT_TYPE(target)) != 0 &&
1568                           !target_block->protected) {
1569                    /* T = JMPZ_EX(X, L1), L1: T = JMPNZ_EX(T, L2) -> T = JMPZ_EX(X, L1+1) */
1570                    del_source(block, block->op2_to);
1571                    block->op2_to = target_block->follow_to;
1572                    ADD_SOURCE(block, block->op2_to);
1573                } else if (target_block->op2_to &&
1574                           target->opcode == last_op->opcode &&
1575                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1576                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1577                           (same_t[VAR_NUM_EX(target->result)] & ZEND_RESULT_TYPE(target)) != 0 &&
1578                           !target_block->protected) {
1579                    /* T = JMPZ_EX(X, L1), L1: T = JMPZ({X|T}, L2) -> T = JMPZ_EX(X, L2) */
1580                    del_source(block, block->op2_to);
1581                    block->op2_to = target_block->op2_to;
1582                    ADD_SOURCE(block, block->op2_to);
1583                } else if (target_block->op1_to &&
1584                           target->opcode == ZEND_JMP &&
1585                           !target_block->protected) {
1586                    /* T = JMPZ_EX(X, L), L: JMP(L2) -> T = JMPZ(X, L2) */
1587                    del_source(block, block->op2_to);
1588                    block->op2_to = target_block->op1_to;
1589                    ADD_SOURCE(block, block->op2_to);
1590                } else if (target_block->op2_to &&
1591                           target_block->ext_to &&
1592                           target->opcode == ZEND_JMPZNZ &&
1593                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1594                           (same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
1595                           !target_block->protected) {
1596                    /* T = JMPZ_EX(X, L), L: JMPZNZ({X|T}, L2, L3) -> T = JMPZ_EX(X, L2) */
1597                    del_source(block, block->op2_to);
1598                    if (last_op->opcode == ZEND_JMPZ_EX) {
1599                        block->op2_to = target_block->op2_to;
1600                    } else {
1601                        block->op2_to = target_block->ext_to;
1602                    }
1603                    ADD_SOURCE(block, block->op2_to);
1604                }
1605            }
1606            break;
1607
1608        case ZEND_JMPZNZ: {
1609            zend_code_block *next = block->next;
1610
1611            while (next && !next->access) {
1612                /* find first accessed one */
1613                next = next->next;
1614            }
1615
1616            if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
1617                if (!zend_is_true(&ZEND_OP1_LITERAL(last_op))) {
1618                    /* JMPZNZ(false,L1,L2) -> JMP(L1) */
1619                    zend_code_block *todel;
1620
1621                    literal_dtor(&ZEND_OP1_LITERAL(last_op));
1622                    last_op->opcode = ZEND_JMP;
1623                    SET_UNUSED(last_op->op1);
1624                    SET_UNUSED(last_op->op2);
1625                    block->op1_to = block->op2_to;
1626                    todel = block->ext_to;
1627                    block->op2_to = NULL;
1628                    block->ext_to = NULL;
1629                    del_source(block, todel);
1630                } else {
1631                    /* JMPZNZ(true,L1,L2) -> JMP(L2) */
1632                    zend_code_block *todel;
1633
1634                    literal_dtor(&ZEND_OP1_LITERAL(last_op));
1635                    last_op->opcode = ZEND_JMP;
1636                    SET_UNUSED(last_op->op1);
1637                    SET_UNUSED(last_op->op2);
1638                    block->op1_to = block->ext_to;
1639                    todel =  block->op2_to;
1640                    block->op2_to = NULL;
1641                    block->ext_to = NULL;
1642                    del_source(block, todel);
1643                }
1644            } else if (block->op2_to == block->ext_to) {
1645                /* both goto the same one - it's JMP */
1646                /* JMPZNZ(?,L,L) -> JMP(L) */
1647                last_op->opcode = ZEND_JMP;
1648                SET_UNUSED(last_op->op1);
1649                SET_UNUSED(last_op->op2);
1650                block->op1_to = block->op2_to;
1651                block->op2_to = NULL;
1652                block->ext_to = NULL;
1653            } else if (block->op2_to == next) {
1654                /* jumping to next on Z - can follow to it and jump only on NZ */
1655                /* JMPZNZ(X,L1,L2) L1: -> JMPNZ(X,L2) */
1656                last_op->opcode = ZEND_JMPNZ;
1657                block->op2_to = block->ext_to;
1658                block->follow_to = next;
1659                block->ext_to = NULL;
1660                /* no need to add source - it's block->op2_to */
1661            } else if (block->ext_to == next) {
1662                /* jumping to next on NZ - can follow to it and jump only on Z */
1663                /* JMPZNZ(X,L1,L2) L2: -> JMPZ(X,L1) */
1664                last_op->opcode = ZEND_JMPZ;
1665                block->follow_to = next;
1666                block->ext_to = NULL;
1667                /* no need to add source - it's block->ext_to */
1668            }
1669
1670            if (last_op->opcode == ZEND_JMPZNZ && block->op2_to) {
1671                zend_uchar same_type = ZEND_OP1_TYPE(last_op);
1672                zend_uchar same_var = VAR_NUM_EX(last_op->op1);
1673                zend_op *target;
1674                zend_op *target_end;
1675                zend_code_block *target_block = block->op2_to;
1676
1677next_target_znz:
1678                target = target_block->start_opline;
1679                target_end = target_block->start_opline + target_block->len;
1680                while (target < target_end && target->opcode == ZEND_NOP) {
1681                    target++;
1682                }
1683                /* next block is only NOP's */
1684                if (target == target_end) {
1685                    target_block = target_block->follow_to;
1686                    goto next_target_znz;
1687                } else if (target_block->op2_to &&
1688                           (target->opcode == ZEND_JMPZ || target->opcode == ZEND_JMPZNZ) &&
1689                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1690                           same_type == ZEND_OP1_TYPE(target) &&
1691                           same_var == VAR_NUM_EX(target->op1) &&
1692                           !target_block->protected) {
1693                    /* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
1694                    del_source(block, block->op2_to);
1695                    block->op2_to = target_block->op2_to;
1696                    ADD_SOURCE(block, block->op2_to);
1697                } else if (target->opcode == ZEND_JMPNZ &&
1698                           (ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
1699                           same_type == ZEND_OP1_TYPE(target) &&
1700                           same_var == VAR_NUM_EX(target->op1) &&
1701                           target_block->follow_to &&
1702                           !target_block->protected) {
1703                    /* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
1704                    del_source(block, block->op2_to);
1705                    block->op2_to = target_block->follow_to;
1706                    ADD_SOURCE(block, block->op2_to);
1707                } else if (target_block->op1_to &&
1708                           target->opcode == ZEND_JMP &&
1709                           !target_block->protected) {
1710                    /* JMPZNZ(X, L1, L2), L1: JMP(L3) -> JMPZNZ(X, L3, L2) */
1711                    del_source(block, block->op2_to);
1712                    block->op2_to = target_block->op1_to;
1713                    ADD_SOURCE(block, block->op2_to);
1714                }
1715            }
1716            break;
1717        }
1718    }
1719}
1720
1721/* Global data dependencies */
1722
1723#define T_USAGE(op) do { \
1724        if ((op ## _type & (IS_VAR | IS_TMP_VAR)) && \
1725           !defined_here[VAR_NUM(op.var)] && !used_ext[VAR_NUM(op.var)]) {  \
1726            used_ext[VAR_NUM(op.var)] = 1;                                  \
1727        } \
1728    } while (0)
1729
1730#define NEVER_USED(op) ((op ## _type & (IS_VAR | IS_TMP_VAR)) && !usage[VAR_NUM(op.var)]) /* !used_ext[op.var] && */
1731#define RES_NEVER_USED(opline) (opline->result_type == IS_UNUSED || NEVER_USED(opline->result))
1732
1733/* Find a set of variables which are used outside of the block where they are
1734 * defined. We won't apply some optimization patterns for such variables. */
1735static void zend_t_usage(zend_code_block *block, zend_op_array *op_array, char *used_ext, zend_optimizer_ctx *ctx)
1736{
1737    zend_code_block *next_block = block->next;
1738    char *usage;
1739    char *defined_here;
1740    void *checkpoint;
1741
1742    if (op_array->T == 0) {
1743        /* shortcut - if no Ts, nothing to do */
1744        return;
1745    }
1746
1747    checkpoint = zend_arena_checkpoint(ctx->arena);
1748    usage = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1749    memset(usage, 0, op_array->last_var + op_array->T);
1750    defined_here = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1751
1752    while (next_block) {
1753        zend_op *opline = next_block->start_opline;
1754        zend_op *end = opline + next_block->len;
1755
1756        if (!next_block->access) {
1757            next_block = next_block->next;
1758            continue;
1759        }
1760        memset(defined_here, 0, op_array->last_var + op_array->T);
1761
1762        while (opline<end) {
1763            T_USAGE(opline->op1);
1764            T_USAGE(opline->op2);
1765
1766            if (RESULT_USED(opline)) {
1767                if (!defined_here[VAR_NUM(ZEND_RESULT(opline).var)] && !used_ext[VAR_NUM(ZEND_RESULT(opline).var)] &&
1768                    (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT ||
1769                     opline->opcode == ZEND_RECV_VARIADIC ||
1770                    (opline->opcode == ZEND_OP_DATA && ZEND_RESULT_TYPE(opline) == IS_TMP_VAR) ||
1771                    opline->opcode == ZEND_ADD_ARRAY_ELEMENT)) {
1772                    /* these opcodes use the result as argument */
1773                    used_ext[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
1774                }
1775                defined_here[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
1776            }
1777            opline++;
1778        }
1779        next_block = next_block->next;
1780    }
1781
1782#if DEBUG_BLOCKPASS
1783    {
1784        int i;
1785        for (i = op_array->last_var; i< op_array->T; i++) {
1786            fprintf(stderr, "T%d: %c\n", i, used_ext[i] + '0');
1787        }
1788    }
1789#endif
1790
1791    while (block) {
1792        zend_op *opline = block->start_opline + block->len - 1;
1793
1794        if (!block->access) {
1795            block = block->next;
1796            continue;
1797        }
1798
1799        memcpy(usage, used_ext, op_array->last_var + op_array->T);
1800
1801        while (opline >= block->start_opline) {
1802            /* usage checks */
1803            if (RES_NEVER_USED(opline)) {
1804                switch (opline->opcode) {
1805                    case ZEND_ASSIGN_ADD:
1806                    case ZEND_ASSIGN_SUB:
1807                    case ZEND_ASSIGN_MUL:
1808                    case ZEND_ASSIGN_DIV:
1809                    case ZEND_ASSIGN_MOD:
1810                    case ZEND_ASSIGN_SL:
1811                    case ZEND_ASSIGN_SR:
1812                    case ZEND_ASSIGN_CONCAT:
1813                    case ZEND_ASSIGN_BW_OR:
1814                    case ZEND_ASSIGN_BW_AND:
1815                    case ZEND_ASSIGN_BW_XOR:
1816                    case ZEND_PRE_INC:
1817                    case ZEND_PRE_DEC:
1818                    case ZEND_POST_INC:
1819                    case ZEND_POST_DEC:
1820                    case ZEND_ASSIGN:
1821                    case ZEND_ASSIGN_REF:
1822                    case ZEND_DO_FCALL:
1823                        if (ZEND_RESULT_TYPE(opline) == IS_VAR) {
1824                            ZEND_RESULT_TYPE(opline) |= EXT_TYPE_UNUSED;
1825                        }
1826                        break;
1827                    case ZEND_QM_ASSIGN:
1828                    case ZEND_BOOL:
1829                    case ZEND_BOOL_NOT:
1830                        if (ZEND_OP1_TYPE(opline) == IS_CONST) {
1831                            literal_dtor(&ZEND_OP1_LITERAL(opline));
1832                        }
1833                        MAKE_NOP(opline);
1834                        break;
1835                    case ZEND_JMPZ_EX:
1836                    case ZEND_JMPNZ_EX:
1837                        opline->opcode -= 3;
1838                        SET_UNUSED(opline->result);
1839                        break;
1840                }
1841            }
1842
1843            if (opline->opcode == ZEND_RECV ||
1844                opline->opcode == ZEND_RECV_INIT ||
1845                opline->opcode == ZEND_RECV_VARIADIC ||
1846                opline->opcode == ZEND_ADD_ARRAY_ELEMENT) {
1847                if (ZEND_OP1_TYPE(opline) == IS_VAR || ZEND_OP1_TYPE(opline) == IS_TMP_VAR) {
1848                    usage[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
1849                }
1850            } else {
1851                if (RESULT_USED(opline)) {
1852                    usage[VAR_NUM(ZEND_RESULT(opline).var)] = 0;
1853                }
1854            }
1855
1856            if (ZEND_OP1_TYPE(opline) == IS_VAR || ZEND_OP1_TYPE(opline) == IS_TMP_VAR) {
1857                usage[VAR_NUM(ZEND_OP1(opline).var)] = 1;
1858            }
1859
1860            if (ZEND_OP2_TYPE(opline) == IS_VAR || ZEND_OP2_TYPE(opline) == IS_TMP_VAR) {
1861                usage[VAR_NUM(ZEND_OP2(opline).var)] = 1;
1862            }
1863
1864            if ((ZEND_RESULT_TYPE(opline) & IS_VAR) &&
1865                (ZEND_RESULT_TYPE(opline) & EXT_TYPE_UNUSED) &&
1866                usage[VAR_NUM(ZEND_RESULT(opline).var)]) {
1867                ZEND_RESULT_TYPE(opline) &= ~EXT_TYPE_UNUSED;
1868            }
1869
1870            opline--;
1871        }
1872        block = block->next;
1873    } /* end blocks */
1874
1875    zend_arena_release(&ctx->arena, checkpoint);
1876}
1877
1878#define PASSES 3
1879
1880void optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx)
1881{
1882    zend_cfg cfg;
1883    zend_code_block *cur_block;
1884    int pass;
1885    char *usage;
1886    void *checkpoint;
1887
1888#if DEBUG_BLOCKPASS
1889    fprintf(stderr, "File %s func %s\n", op_array->filename, op_array->function_name? op_array->function_name : "main");
1890    fflush(stderr);
1891#endif
1892
1893    if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
1894        return;
1895    }
1896
1897    /* Build CFG */
1898    checkpoint = zend_arena_checkpoint(ctx->arena);
1899    if (!find_code_blocks(op_array, &cfg, ctx)) {
1900        zend_arena_release(&ctx->arena, checkpoint);
1901        return;
1902    }
1903
1904    zend_rebuild_access_path(&cfg, op_array, 0, ctx);
1905    /* full rebuild here to produce correct sources! */
1906    if (op_array->last_var || op_array->T) {
1907        cfg.Tsource = zend_arena_calloc(&ctx->arena, op_array->last_var + op_array->T, sizeof(zend_op *));
1908        cfg.same_t = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1909        usage = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
1910    } else {
1911        cfg.Tsource = NULL;
1912        cfg.same_t = NULL;
1913        usage = NULL;
1914    }
1915    for (pass = 0; pass < PASSES; pass++) {
1916        /* Compute data dependencies */
1917        memset(usage, 0, op_array->last_var + op_array->T);
1918        zend_t_usage(cfg.blocks, op_array, usage, ctx);
1919
1920        /* optimize each basic block separately */
1921        for (cur_block = cfg.blocks; cur_block; cur_block = cur_block->next) {
1922            if (!cur_block->access) {
1923                continue;
1924            }
1925            zend_optimize_block(cur_block, op_array, usage, &cfg, ctx);
1926        }
1927
1928        /* Jump optimization for each block */
1929        for (cur_block = cfg.blocks; cur_block; cur_block = cur_block->next) {
1930            if (!cur_block->access) {
1931                continue;
1932            }
1933            zend_jmp_optimization(cur_block, op_array, cfg.blocks, &cfg, ctx);
1934        }
1935
1936        /* Eliminate unreachable basic blocks */
1937        zend_rebuild_access_path(&cfg, op_array, 1, ctx);
1938    }
1939
1940    memset(usage, 0, op_array->last_var + op_array->T);
1941    zend_t_usage(cfg.blocks, op_array, usage, ctx);
1942    assemble_code_blocks(&cfg, op_array);
1943
1944    /* Destroy CFG */
1945    zend_arena_release(&ctx->arena, checkpoint);
1946}
1947