forked from eaccelerator/eaccelerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.c
3719 lines (3596 loc) · 155 KB
/
optimize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| eAccelerator project |
+----------------------------------------------------------------------+
| Copyright (c) 2004 - 2012 eAccelerator |
| http://eaccelerator.net |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| MA 02111-1307, USA. |
| |
| A copy is availble at http://www.gnu.org/copyleft/gpl.txt |
+----------------------------------------------------------------------+
$Id: $
*/
#include "eaccelerator.h"
#ifdef HAVE_EACCELERATOR
#ifdef WITH_EACCELERATOR_OPTIMIZER
#include "zend.h"
#include "zend_API.h"
#include "zend_constants.h"
#ifdef ZEND_ENGINE_2_4
#include "zend_string.h"
#endif
#include "opcodes.h"
#include "debug.h"
#define EMBED_BREAKPOINT asm volatile ("int3;")
typedef unsigned int* set;
struct _BBlink;
typedef struct _BB {
zend_op* start;
int len;
int used;
/*
* HOESH: To protect merging. Primary
* it abblies to try & catch blocks.
* ZEND_ENGINE_2 specific, but can take place
*/
int protect_merge;
struct _BB* jmp_1;
struct _BB* jmp_2;
struct _BB* jmp_ext;
struct _BB* follow;
struct _BBlink* pred; // Gonna be a chain of BBs
struct _BB* next;
} BB;
typedef struct _BBlink {
struct _BB* bb;
struct _BBlink* next;
} BBlink;
#ifdef DEBUG
static void dump_bb(BB* bb, zend_op_array *op_array)
{
BB* p = bb;
BBlink *q;
DBG(ea_debug_printf, (EA_DEBUG, "=== CFG FOR %s:%s ===\n", op_array->filename, op_array->function_name));
while (p != NULL) {
DBG(ea_debug_printf, (EA_DEBUG, " bb%u start=%u len=%d used=%d\n",
(unsigned int)(p-bb),
(unsigned int)(p->start-op_array->opcodes),
p->len,
p->used));
if (p->jmp_1) {
DBG(ea_debug_printf, (EA_DEBUG, " jmp_1 bb%u start=%u len=%d used=%d\n",
(unsigned int)(p->jmp_1-bb),
(unsigned int)(p->jmp_1->start-op_array->opcodes),
p->jmp_1->len,
p->jmp_1->used));
}
if (p->jmp_2) {
DBG(ea_debug_printf, (EA_DEBUG, " jmp_2 bb%u start=%u len=%d used=%d\n",
(unsigned int)(p->jmp_2-bb),
(unsigned int)(p->jmp_2->start-op_array->opcodes),
p->jmp_2->len,
p->jmp_2->used));
}
if (p->jmp_ext) {
DBG(ea_debug_printf, (EA_DEBUG, " jmp_ext bb%u start=%u len=%d used=%d\n",
(unsigned int)(p->jmp_ext-bb),
(unsigned int)(p->jmp_ext->start-op_array->opcodes),
p->jmp_ext->len,
p->jmp_ext->used));
}
if (p->follow) {
DBG(ea_debug_printf, (EA_DEBUG, " follow bb%u start=%u len=%d used=%d\n",
(unsigned int)(p->follow-bb),
(unsigned int)(p->follow->start-op_array->opcodes),
p->follow->len,
p->follow->used));
}
q = p->pred;
while (q != NULL) {
DBG(ea_debug_printf, (EA_DEBUG, " pred bb%u start=%u len=%d used=%d (",
(unsigned int)(q->bb-bb),
(unsigned int)(q->bb->start-op_array->opcodes),
q->bb->len,
q->bb->used));
if (q->bb->jmp_1 == p) {
DBG(ea_debug_printf, (EA_DEBUG, "jmp_1 "));
}
if (q->bb->jmp_2 == p) {
DBG(ea_debug_printf, (EA_DEBUG, "jmp_2 "));
}
if (q->bb->jmp_ext == p) {
DBG(ea_debug_printf, (EA_DEBUG, "jmp_ext "));
}
if (q->bb->follow == p) {
DBG(ea_debug_printf, (EA_DEBUG, "follow "));
}
DBG(ea_debug_printf, (EA_DEBUG, ")\n"));
q = q->next;
}
p = p->next;
}
DBG(ea_debug_printf, (EA_DEBUG, "=== END OF CFG ===========================\n"));
}
static void dump_array(int nb,void *pos,char type)
{
int j;
switch(type) {
case 'i': {
int *ptr=pos;
for (j=0; j<nb; j++) {
zend_printf("%d:%6d ",j,*ptr);
ptr++;
}
}
break;
case 'x': {
int *ptr=pos;
for (j=0; j<nb; j++) {
zend_printf("%d:%x ",j,*ptr);
ptr++;
}
}
break;
case 'c': {
unsigned char *ptr=pos;
for (j=0; j<nb; j++) {
/* if (*ptr>=32 && *ptr<128) zend_printf("%d:%c",j,*ptr);
else if (*ptr>=128) zend_printf("%d:%2x",j,*ptr);
else if (*ptr<16) zend_printf("%d:&%1x",j,*ptr);
else zend_printf("%d:$%1x",j,(*ptr)-16); */
zend_printf("%d:%1x ",j,*ptr);
ptr++;
}
}
break;
default:
for (j=0; j<nb; j++) {
zend_printf("# ");
}
}
zend_printf("<br>\n");
}
#endif
#ifdef ZEND_ENGINE_2_4
#define SET_TO_NOP(op) \
(op)->opcode = ZEND_NOP; \
(op)->op1_type = IS_UNUSED; \
(op)->op2_type = IS_UNUSED; \
(op)->result_type = IS_UNUSED;
#else
#define SET_TO_NOP(op) \
(op)->opcode = ZEND_NOP; \
(op)->op1.op_type = IS_UNUSED; \
(op)->op2.op_type = IS_UNUSED; \
(op)->result.op_type = IS_UNUSED;
#endif
static void compute_live_var(BB* bb, zend_op_array* op_array, char* global)
{
BB* p = bb;
char* def;
char* used;
#ifdef ZEND_ENGINE_2_3
ALLOCA_FLAG(use_heap)
#endif
memset(global, 0, op_array->T * sizeof(char));
if (p != NULL && p->next != NULL) {
int bb_count = 0;
#ifdef ZEND_ENGINE_2_3
def = do_alloca(op_array->T * sizeof(char), use_heap);
#else
def = do_alloca(op_array->T * sizeof(char));
#endif
while (p != NULL) {
zend_op* op = p->start;
zend_op* end = op + p->len;
memset(def, 0, op_array->T * sizeof(char));
while (op < end) {
if ((OP1_TYPE(op) == IS_VAR || OP1_TYPE(op) == IS_TMP_VAR) &&
!def[VAR_NUM(OP1_VARR(op))] && !global[VAR_NUM(OP1_VARR(op))]) {
global[VAR_NUM(OP1_VARR(op))] = 1;
}
if ((OP2_TYPE(op) == IS_VAR || OP2_TYPE(op) == IS_TMP_VAR) &&
!def[VAR_NUM(OP2_VARR(op))] && !global[VAR_NUM(OP2_VARR(op))]) {
if (op->opcode != ZEND_OP_DATA) {
global[VAR_NUM(OP2_VARR(op))] = 1;
}
}
#ifdef ZEND_ENGINE_2_3
if ((op->opcode == ZEND_DECLARE_INHERITED_CLASS || op->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) &&
#else
if (op->opcode == ZEND_DECLARE_INHERITED_CLASS &&
#endif
!def[VAR_NUM(op->extended_value)] &&
!global[VAR_NUM(op->extended_value)]) {
global[VAR_NUM(op->extended_value)] = 1;
}
if ((RES_TYPE(op) & IS_VAR &&
(op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT ||
(RES_USED(op) & EXT_TYPE_UNUSED) == 0)) ||
(RES_TYPE(op) & IS_TMP_VAR)) {
if (!def[VAR_NUM(RES_VARR(op))] && !global[VAR_NUM(RES_VARR(op))]) {
switch (op->opcode) {
case ZEND_RECV:
case ZEND_RECV_INIT:
case ZEND_ADD_ARRAY_ELEMENT:
global[VAR_NUM(RES_VARR(op))] = 1;
break;
}
}
def[VAR_NUM(RES_VARR(op))] = 1;
}
op++;
}
p = p->next;
bb_count++;
}
#ifdef ZEND_ENGINE_2_3
free_alloca(def, use_heap);
#else
free_alloca(def);
#endif
}
#ifdef ZEND_ENGINE_2_3
used = do_alloca(op_array->T * sizeof(char), use_heap);
#else
used = do_alloca(op_array->T * sizeof(char));
#endif
p = bb;
while (p != NULL) {
zend_op* op = p->start;
zend_op* end = op + p->len;
memset(used, 0, op_array->T * sizeof(char));
while (op < end) {
end--;
if (((RES_TYPE(end) & IS_VAR &&
(end->opcode == ZEND_RECV || end->opcode == ZEND_RECV_INIT ||
(RES_USED(end) & EXT_TYPE_UNUSED) == 0)) ||
(RES_TYPE(end) & IS_TMP_VAR)) &&
!global[VAR_NUM(RES_VARR(end))] && !used[VAR_NUM(RES_VARR(end))]) {
switch(end->opcode) {
case ZEND_JMPZ_EX:
end->opcode = ZEND_JMPZ;
RES_TYPE(end) = IS_UNUSED;
break;
case ZEND_JMPNZ_EX:
end->opcode = ZEND_JMPNZ;
RES_TYPE(end) = IS_UNUSED;
break;
case ZEND_ASSIGN_ADD:
case ZEND_ASSIGN_SUB:
case ZEND_ASSIGN_MUL:
case ZEND_ASSIGN_DIV:
case ZEND_ASSIGN_MOD:
case ZEND_ASSIGN_SL:
case ZEND_ASSIGN_SR:
case ZEND_ASSIGN_CONCAT:
case ZEND_ASSIGN_BW_OR:
case ZEND_ASSIGN_BW_AND:
case ZEND_ASSIGN_BW_XOR:
case ZEND_PRE_INC:
case ZEND_PRE_DEC:
case ZEND_POST_INC:
case ZEND_POST_DEC:
case ZEND_ASSIGN:
case ZEND_ASSIGN_REF:
case ZEND_DO_FCALL:
case ZEND_DO_FCALL_BY_NAME:
if (RES_TYPE(end) & IS_VAR) {
RES_USED(end) |= EXT_TYPE_UNUSED;
}
break;
case ZEND_UNSET_VAR:
case ZEND_UNSET_DIM:
case ZEND_UNSET_OBJ:
RES_TYPE(end) = IS_UNUSED;
break;
case ZEND_RECV:
case ZEND_RECV_INIT:
case ZEND_INCLUDE_OR_EVAL:
case ZEND_NEW:
case ZEND_FE_FETCH:
case ZEND_PRINT:
case ZEND_INIT_METHOD_CALL:
case ZEND_INIT_STATIC_METHOD_CALL:
case ZEND_ASSIGN_DIM:
case ZEND_ASSIGN_OBJ:
case ZEND_DECLARE_CLASS:
case ZEND_DECLARE_INHERITED_CLASS:
#ifdef ZEND_DECLARE_INHERITED_CLASS_DELAYED
case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
#endif
break;
default:
#ifndef ZEND_ENGINE_2_4
/* TODO: check this */
if (OP1_TYPE(end) == IS_CONST) {
zval_dtor(&end->op1.u.constant);
}
if (OP2_TYPE(end) == IS_CONST) {
zval_dtor(&end->op2.u.constant);
}
#endif
SET_TO_NOP(end);
}
} else if (RES_TYPE(end) & IS_VAR &&
(RES_USED(end) & EXT_TYPE_UNUSED) != 0 &&
end->opcode != ZEND_RECV && end->opcode != ZEND_RECV_INIT &&
used[VAR_NUM(RES_VARR(end))]) {
RES_USED(end) &= ~EXT_TYPE_UNUSED;
}
if ((RES_TYPE(end) & IS_VAR &&
(end->opcode == ZEND_RECV || end->opcode == ZEND_RECV_INIT ||
(RES_USED(end) & EXT_TYPE_UNUSED) == 0)) ||
(RES_TYPE(end) & IS_TMP_VAR)) {
switch (end->opcode) {
case ZEND_RECV:
case ZEND_RECV_INIT:
case ZEND_ADD_ARRAY_ELEMENT:
used[VAR_NUM(RES_VARR(end))] = 1;
break;
default:
used[VAR_NUM(RES_VARR(end))] = 0;
}
}
if (OP1_TYPE(end) == IS_VAR || OP1_TYPE(end) == IS_TMP_VAR) {
used[VAR_NUM(OP1_VARR(end))] = 1;
}
if (OP2_TYPE(end) == IS_VAR || OP2_TYPE(end) == IS_TMP_VAR) {
used[VAR_NUM(OP2_VARR(end))] = 1;
}
#ifdef ZEND_ENGINE_2_3
if (end->opcode == ZEND_DECLARE_INHERITED_CLASS || end->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
#else
if (end->opcode == ZEND_DECLARE_INHERITED_CLASS) {
#endif
used[VAR_NUM(end->extended_value)] = 1;
}
}
p = p->next;
}
#ifdef ZEND_ENGINE_2_3
free_alloca(used, use_heap);
#else
free_alloca(used);
#endif
}
/* Adds FROM as predecessor of TO */
#define BB_ADD_PRED(TO,FROM) { \
BBlink *q = (TO)->pred; \
while (q != NULL) { \
if (q->bb == (FROM)) break; \
q = q->next; \
} \
if (q == NULL) { \
q = emalloc(sizeof(*q)); \
q->bb = (FROM); \
q->next = (TO)->pred; \
(TO)->pred = q; \
} \
}
/* Removes FROM from predecessors of TO */
#define BB_DEL_PRED(TO,FROM) { \
BBlink *q = (TO)->pred; \
if (q != NULL) { \
if (q->bb == (FROM)) { \
(TO)->pred = q->next; \
efree(q); \
} else { \
while (q->next != NULL) { \
if (q->next->bb == (FROM)) { \
BBlink *r = q->next; \
q->next = q->next->next; \
efree(r); \
break; \
} \
q = q->next; \
} \
} \
} \
}
#define RM_BB(p) do {if (p->pred == NULL && p != bb) rm_bb(p);} while (0)
static void mark_used_bb(BB* bb)
{
if (bb->used) {
return;
}
bb->used = 1;
if (bb->jmp_1 != NULL) {
mark_used_bb(bb->jmp_1);
BB_ADD_PRED(bb->jmp_1, bb);
}
if (bb->jmp_2 != NULL) {
mark_used_bb(bb->jmp_2);
BB_ADD_PRED(bb->jmp_2, bb);
}
if (bb->jmp_ext != NULL) {
mark_used_bb(bb->jmp_ext);
BB_ADD_PRED(bb->jmp_ext, bb);
}
if (bb->follow != NULL) {
mark_used_bb(bb->follow);
BB_ADD_PRED(bb->follow, bb);
}
}
static void mark_used_bb2(BB* bb)
{
if (bb->used) {
return;
}
bb->used = 1;
if (bb->jmp_1 != NULL) {
mark_used_bb2(bb->jmp_1);
}
if (bb->jmp_2 != NULL) {
mark_used_bb2(bb->jmp_2);
}
if (bb->jmp_ext != NULL) {
mark_used_bb2(bb->jmp_ext);
}
if (bb->follow != NULL) {
mark_used_bb2(bb->follow);
}
}
static void rm_bb(BB* bb)
{
if (bb->used == 0) {
return;
}
bb->used = 0;
if (bb->jmp_1 != NULL) {
BB_DEL_PRED(bb->jmp_1, bb);
}
if (bb->jmp_2 != NULL) {
BB_DEL_PRED(bb->jmp_2, bb);
}
if (bb->jmp_ext != NULL) {
BB_DEL_PRED(bb->jmp_ext, bb);
}
if (bb->follow != NULL) {
BB_DEL_PRED(bb->follow, bb);
}
}
static void del_bb(BB* bb)
{
zend_op* op = bb->start;
zend_op* end = op + bb->len;
rm_bb(bb);
while (op < end) {
--end;
#ifndef ZEND_ENGINE_2_4
if (OP1_TYPE(end) == IS_CONST) {
zval_dtor(&end->op1.u.constant);
}
if (OP2_TYPE(end) == IS_CONST) {
zval_dtor(&end->op2.u.constant);
}
#endif
SET_TO_NOP(end);
}
bb->len = 0;
bb->used = 0;
}
/*
static void replace_bb(BB* src, BB* dst)
{
BBlink* p = src->pred;
while (p != NULL) {
BBlink* q = p->next;
if (p->bb->jmp_1 == src) {
p->bb->jmp_1 = dst;
BB_ADD_PRED(dst,p->bb);
}
if (p->bb->jmp_2 == src) {
p->bb->jmp_2 = dst;
BB_ADD_PRED(dst,p->bb);
}
if (p->bb->jmp_ext == src) {
p->bb->jmp_ext = dst;
BB_ADD_PRED(dst,p->bb);
}
if (p->bb->follow == src) {
p->bb->follow = dst;
BB_ADD_PRED(dst,p->bb);
}
efree(p);
p = q;
}
src->pred = NULL;
}
*/
static void optimize_jmp(BB* bb, zend_op_array* op_array)
{
BB* p;
while(1) {
int ok = 1;
/* Remove Unused Basic Blocks */
p = bb;
while (p->next != NULL) {
if (p->next->used && p->next->pred) {
p = p->next;
} else {
del_bb(p->next);
p->next = p->next->next;
ok = 0;
}
}
/* JMP optimization */
p = bb;
while (p != NULL) {
while (p->next != NULL && (!p->next->used || p->next->pred == NULL)) {
del_bb(p->next);
p->next = p->next->next;
ok = 0;
}
if (p->used && p->len > 0) {
zend_op* op = &p->start[p->len-1];
switch (op->opcode) {
case ZEND_JMP:
jmp:
/* L1: JMP L1+1 => NOP
*/
if (p->jmp_1 == p->next) {
if (p->follow) {
BB_DEL_PRED(p->follow, p);
}
p->follow = p->jmp_1;
p->jmp_1 = NULL;
SET_TO_NOP(op);
--(p->len);
ok = 0;
break;
}
/* JMP L1 => JMP L2
... ...
L1: JMP L2 JMP L2
*/
while (p->jmp_1->len == 1 &&
p->jmp_1->start->opcode == ZEND_JMP &&
p->jmp_1 != p) {
BB* x_p = p->jmp_1;
BB_DEL_PRED(p->jmp_1, p);
RM_BB(x_p);
p->jmp_1 = x_p->jmp_1;
BB_ADD_PRED(p->jmp_1, p);
ok = 0;
}
break;
case ZEND_JMPZNZ:
jmp_znz:
/* JMPZNZ ?,L1,L1 => JMP L1
*/
if (p->jmp_ext == p->jmp_2) {
op->opcode = ZEND_JMP;
op->extended_value = 0;
OP1_TYPE(op) = IS_UNUSED;
OP2_TYPE(op) = IS_UNUSED;
p->jmp_1 = p->jmp_2;
p->jmp_2 = NULL;
p->jmp_ext = NULL;
ok = 0;
goto jmp;
} else if (OP1_TYPE(op) == IS_CONST) {
/* JMPZNZ 0,L1,L2 => JMP L1
*/
#ifdef ZEND_ENGINE_2_4
if (!zend_is_true(&CONSTANT(op->op1.constant))) {
#else
if (!zend_is_true(&op->op1.u.constant)) {
#endif
op->opcode = ZEND_JMP;
op->extended_value = 0;
OP1_TYPE(op) = IS_UNUSED;
OP2_TYPE(op) = IS_UNUSED;
if (p->jmp_ext != p->jmp_2) {
BB_DEL_PRED(p->jmp_ext, p);
RM_BB(p->jmp_ext);
}
p->jmp_1 = p->jmp_2;
p->jmp_2 = NULL;
p->jmp_ext = NULL;
p->follow = NULL;
ok = 0;
goto jmp;
/* JMPZNZ 1,L1,L2 => JMP L2
*/
} else {
op->opcode = ZEND_JMP;
op->extended_value = 0;
OP1_TYPE(op) = IS_UNUSED;
OP2_TYPE(op) = IS_UNUSED;
if (p->jmp_ext != p->jmp_2) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_1 = p->jmp_ext;
p->jmp_2 = NULL;
p->jmp_ext = NULL;
p->follow = NULL;
ok = 0;
goto jmp;
}
/* L1: JMPZNZ ?,L2,L1+1 => JMPZ ?,L2
*/
} else if (p->jmp_ext == p->next) {
op->opcode = ZEND_JMPZ;
op->extended_value = 0;
p->follow = p->jmp_ext;
p->jmp_ext = NULL;
ok = 0;
goto jmp_z;
/* L1: JMPZNZ ?,L1+1,L2 => JMPNZ ?,L2
*/
} else if (p->jmp_2 == p->next) {
op->opcode = ZEND_JMPNZ;
op->extended_value = 0;
p->follow = p->jmp_2;
p->jmp_2 = p->jmp_ext;
p->jmp_ext = NULL;
ok = 0;
goto jmp_nz;
} else if (p->jmp_2->len == 1 &&
OP1_TYPE(op) == IS_TMP_VAR) {
/* JMPZNZ $x,L1,L2 => JMPZNZ $x,L3,L2
... ...
L1: JMPZ $x,L3 JMPZ $x,L3
*/
/* JMPZNZ $x,L1,L2 => JMPZNZ $x,L3,L2
... ...
L1: JMPZNZ $x,L3,L4 JMPZNZ $x,L3,L4
*/
if ((p->jmp_2->start->opcode == ZEND_JMPZ ||
p->jmp_2->start->opcode == ZEND_JMPZNZ) &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->jmp_2;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_znz;
/* JMPZNZ $x,L1,L2 => JMPZNZ $x,L1+1,L2
... ...
L1: JMPNZ $x,L3 JMPNZ $x,L3
*/
} else if (p->jmp_2->start->opcode == ZEND_JMPNZ &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->follow;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_znz;
/* JMPZNZ $x,L1,L2 => JMPZNZ $x,L1,L3
... ...
L2: JMPNZ $x,L3 JMPNZ $x,L3
*/
} else if (p->jmp_ext->start->opcode == ZEND_JMPNZ &&
OP1_TYPE(p->jmp_ext->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_ext->start)) {
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_ext, p);
RM_BB(p->jmp_ext);
}
p->jmp_ext = p->jmp_ext->jmp_2;
BB_ADD_PRED(p->jmp_ext, p);
ok = 0;
goto jmp_znz;
/* JMPZNZ $x,L1,L2 => JMPZNZ $x,L1,L4
... ...
L2: JMPZNZ $x,L3,L4 JMPZNZ $x,L3,L4
*/
} else if (p->jmp_ext->start->opcode == ZEND_JMPZNZ &&
OP1_TYPE(p->jmp_ext->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_ext->start)) {
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_ext, p);
RM_BB(p->jmp_ext);
}
p->jmp_ext = p->jmp_ext->jmp_ext;
BB_ADD_PRED(p->jmp_ext, p);
ok = 0;
goto jmp_znz;
/* JMPZNZ $x,L1,L2 => JMPZNZ $x,L1,L2+1
... ...
L2: JMPZ $x,L3 JMPZ $x,L3
*/
} else if (p->jmp_ext->start->opcode == ZEND_JMPZ &&
OP1_TYPE(p->jmp_ext->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_ext->start)) {
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_ext, p);
RM_BB(p->jmp_ext);
}
p->jmp_ext = p->jmp_ext->follow;
BB_ADD_PRED(p->jmp_ext, p);
ok = 0;
goto jmp_znz;
}
}
while (p->jmp_2->len == 1 && p->jmp_2->start->opcode == ZEND_JMP) {
BB* x_p = p->jmp_2;
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(x_p);
}
p->jmp_2 = x_p->jmp_1;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
}
while (p->jmp_ext->len == 1 && p->jmp_ext->start->opcode == ZEND_JMP) {
BB* x_p = p->jmp_ext;
if (p->jmp_2 != p->jmp_ext) {
BB_DEL_PRED(p->jmp_ext, p);
RM_BB(x_p);
}
p->jmp_ext = x_p->jmp_1;
BB_ADD_PRED(p->jmp_ext, p);
ok = 0;
}
break;
case ZEND_JMPZ:
jmp_z:
/* L1: JMPZ ?,L1+1 => NOP
*/
if (p->follow == p->jmp_2) {
p->jmp_2 = NULL;
SET_TO_NOP(op);
--(p->len);
ok = 0;
break;
} else if (OP1_TYPE(op) == IS_CONST) {
/* JMPZ 0,L1 => JMP L1
*/
#ifdef ZEND_ENGINE_2_4
if (!zend_is_true(&CONSTANT(op->op1.constant))) {
#else
if (!zend_is_true(&op->op1.u.constant)) {
#endif
op->opcode = ZEND_JMP;
OP1_TYPE(op) = IS_UNUSED;
OP2_TYPE(op) = IS_UNUSED;
if (p->follow != p->jmp_2) {
BB_DEL_PRED(p->follow, p);
RM_BB(p->follow);
}
p->jmp_1 = p->jmp_2;
p->jmp_2 = NULL;
p->follow = NULL;
ok = 0;
goto jmp;
/* JMPZ 1,L1 => NOP
*/
} else {
if (p->follow != p->jmp_2) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = NULL;
SET_TO_NOP(op);
--(p->len);
ok = 0;
break;
}
/* JMPZ ?,L1 => JMPZNZ ?,L1,L2
JMP L2 JMP L2
*/
} else if (p->follow->len == 1 && p->follow->start->opcode == ZEND_JMP) {
BB* x_p = p->follow;
op->opcode = ZEND_JMPZNZ;
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->follow, p);
RM_BB(x_p);
}
p->follow = NULL;
p->jmp_ext = x_p->jmp_1;
BB_ADD_PRED(p->jmp_ext, p);
ok = 0;
goto jmp_znz;
} else if (p->jmp_2->len == 1 &&
OP1_TYPE(op) == IS_TMP_VAR) {
/* JMPZ $x,L1 => JMPZ $x,L2
... ...
L1: JMPZ $x,L2 JMPZ $x,L2
----------------------------------------
JMPZ $x,L1 => JMPZ $x,L2
... ...
L1: JMPZNZ $x,L2,L3 JMPZNZ $x,L2,L3
*/
if ((p->jmp_2->start->opcode == ZEND_JMPZ ||
p->jmp_2->start->opcode == ZEND_JMPZNZ) &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->jmp_2;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_z;
/* JMPZ $x,L1 => JMPZ $x,L1+1
... ...
L1: JMPNZ $x,L2 JMPNZ $x,L2
*/
} else if (p->jmp_2->start->opcode == ZEND_JMPNZ &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->follow;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_z;
}
}
goto jmp_2;
case ZEND_JMPNZ:
jmp_nz:
/* L1: JMPNZ ?,L1+1 => NOP
*/
if (p->follow == p->jmp_2) {
p->jmp_2 = NULL;
SET_TO_NOP(op);
--(p->len);
ok = 0;
break;
} else if (OP1_TYPE(op) == IS_CONST) {
/* JMPNZ 1,L1 => JMP L1
*/
#ifdef ZEND_ENGINE_2_4
if (zend_is_true(&CONSTANT(op->op1.constant))) {
#else
if (zend_is_true(&op->op1.u.constant)) {
#endif
op->opcode = ZEND_JMP;
OP1_TYPE(op) = IS_UNUSED;
OP2_TYPE(op) = IS_UNUSED;
if (p->follow != p->jmp_2) {
BB_DEL_PRED(p->follow, p);
RM_BB(p->follow);
}
p->jmp_1 = p->jmp_2;
p->jmp_2 = NULL;
p->follow = NULL;
ok = 0;
goto jmp;
/* JMPNZ 0,L1 => NOP
*/
} else {
if (p->follow != p->jmp_2) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = NULL;
SET_TO_NOP(op);
--(p->len);
ok = 0;
break;
}
/* JMPNZ ?,L1 => JMPZNZ ?,L2,L1
JMP L2 JMP L2
*/
} else if (p->follow->len == 1 && p->follow->start->opcode == ZEND_JMP) {
BB* x_p = p->follow;
op->opcode = ZEND_JMPZNZ;
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->follow, p);
RM_BB(p->follow);
}
p->follow = NULL;
p->jmp_ext = p->jmp_2;
p->jmp_2 = x_p->jmp_1;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_znz;
/* JMPNZ $x,L1 => JMPNZ $x,L2
... ...
L1: JMPNZ $x,L2 JMPNZ $x,L2
*/
} else if (p->jmp_2->len == 1 &&
OP1_TYPE(op) == IS_TMP_VAR) {
if (p->jmp_2->start->opcode == ZEND_JMPNZ &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->jmp_2;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_nz;
/* JMPNZ $x,L1 => JMPNZ $x,L1+1
... ...
L1: JMPZ $x,L2 JMPZ $x,L2
*/
} else if (p->jmp_2->start->opcode == ZEND_JMPZ &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->follow;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_nz;
/* JMPNZ $x,L1 => JMPNZ $x,L3
... ...
L1: JMPZNZ $x,L2,L3 JMPZNZ $x,L2,L3
*/
} else if (p->jmp_2->start->opcode == ZEND_JMPZNZ &&
OP1_TYPE(p->jmp_2->start) == IS_TMP_VAR &&
OP1_VARR(op) == OP1_VARR(p->jmp_2->start)) {
if (p->jmp_2 != p->follow) {
BB_DEL_PRED(p->jmp_2, p);
RM_BB(p->jmp_2);
}
p->jmp_2 = p->jmp_2->jmp_ext;
BB_ADD_PRED(p->jmp_2, p);
ok = 0;
goto jmp_nz;
}
}
goto jmp_2;
case ZEND_JMPZ_EX:
jmp_z_ex:
/* L1: JMPZ_EX $x,L1+1,$x => NOP
*/
if (p->follow == p->jmp_2 &&
OP1_TYPE(op) == IS_TMP_VAR &&
RES_TYPE(op) == IS_TMP_VAR &&
OP1_VARR(op) == RES_VARR(op)) {
p->jmp_2 = NULL;
SET_TO_NOP(op);
--(p->len);
ok = 0;
break;