26
26
#include "zend_bitset.h"
27
27
#include "zend_cfg.h"
28
28
#include "zend_ssa.h"
29
+ #include "zend_func_info.h"
30
+ #include "zend_inference.h"
29
31
#include "zend_dump.h"
30
32
31
33
#ifndef HAVE_DFA_PASS
35
37
void optimize_dfa (zend_op_array * op_array , zend_optimizer_ctx * ctx )
36
38
{
37
39
void * checkpoint ;
38
- uint32_t flags ;
39
- zend_cfg cfg ;
40
+ uint32_t build_flags ;
41
+ uint32_t flags = 0 ;
40
42
zend_ssa ssa ;
41
43
42
44
#if !HAVE_DFA_PASS
43
45
return ;
44
46
#endif
45
47
46
48
/* Build SSA */
49
+ memset (& ssa , 0 , sizeof (ssa ));
47
50
checkpoint = zend_arena_checkpoint (ctx -> arena );
48
51
49
- if (zend_build_cfg (& ctx -> arena , op_array , 0 , 0 , & cfg , & flags ) != SUCCESS ) {
52
+ if (zend_build_cfg (& ctx -> arena , op_array , 0 , & ssa . cfg , & flags ) != SUCCESS ) {
50
53
zend_arena_release (& ctx -> arena , checkpoint );
51
54
return ;
52
55
}
@@ -56,52 +59,79 @@ void optimize_dfa(zend_op_array *op_array, zend_optimizer_ctx *ctx)
56
59
return ;
57
60
}
58
61
59
- if (zend_cfg_build_predecessors (& ctx -> arena , & cfg ) != SUCCESS ) {
62
+ if (zend_cfg_build_predecessors (& ctx -> arena , & ssa . cfg ) != SUCCESS ) {
60
63
zend_arena_release (& ctx -> arena , checkpoint );
61
64
return ;
62
65
}
63
66
64
67
if (ctx -> debug_level & ZEND_DUMP_DFA_CFG ) {
65
- zend_dump_op_array (op_array , & cfg , 0 , "dfa cfg" );
68
+ zend_dump_op_array (op_array , ZEND_DUMP_CFG | ZEND_DUMP_HIDE_UNUSED_VARS , "dfa cfg" , & ssa . cfg );
66
69
}
67
70
68
71
/* Compute Dominators Tree */
69
- if (zend_cfg_compute_dominators_tree (op_array , & cfg ) != SUCCESS ) {
72
+ if (zend_cfg_compute_dominators_tree (op_array , & ssa . cfg ) != SUCCESS ) {
70
73
zend_arena_release (& ctx -> arena , checkpoint );
71
74
return ;
72
75
}
73
76
74
77
/* Identify reducible and irreducible loops */
75
- if (zend_cfg_identify_loops (op_array , & cfg , & flags ) != SUCCESS ) {
78
+ if (zend_cfg_identify_loops (op_array , & ssa . cfg , & flags ) != SUCCESS ) {
76
79
zend_arena_release (& ctx -> arena , checkpoint );
77
80
return ;
78
81
}
79
82
80
83
if (ctx -> debug_level & ZEND_DUMP_DFA_DOMINATORS ) {
81
- int j ;
84
+ zend_dump_dominators (op_array , & ssa .cfg );
85
+ }
82
86
83
- fprintf (stderr , "DOMINATORS-TREE:\n" );
84
- for (j = 0 ; j < cfg .blocks_count ; j ++ ) {
85
- zend_basic_block * b = cfg .blocks + j ;
86
- if (b -> flags & ZEND_BB_REACHABLE ) {
87
- zend_dump_block_info (& cfg , j , 0 );
88
- }
89
- }
87
+ build_flags = 0 ;
88
+ if (ctx -> debug_level & ZEND_DUMP_DFA_LIVENESS ) {
89
+ build_flags |= ZEND_SSA_DEBUG_LIVENESS ;
90
+ }
91
+ if (ctx -> debug_level & ZEND_DUMP_DFA_PHI ) {
92
+ build_flags |= ZEND_SSA_DEBUG_PHI_PLACEMENT ;
93
+ }
94
+ if (zend_build_ssa (& ctx -> arena , op_array , build_flags , & ssa , & flags ) != SUCCESS ) {
95
+ zend_arena_release (& ctx -> arena , checkpoint );
96
+ return ;
90
97
}
91
98
92
- if (zend_build_ssa (& ctx -> arena , op_array , & cfg , 0 , & ssa , & flags ) != SUCCESS ) {
99
+ if (ctx -> debug_level & ZEND_DUMP_DFA_SSA ) {
100
+ zend_dump_op_array (op_array , ZEND_DUMP_SSA | ZEND_DUMP_HIDE_UNUSED_VARS , "before dfa pass" , & ssa );
101
+ }
102
+
103
+
104
+ if (zend_ssa_compute_use_def_chains (& ctx -> arena , op_array , & ssa ) != SUCCESS ){
93
105
zend_arena_release (& ctx -> arena , checkpoint );
94
106
return ;
95
107
}
96
108
109
+ if (zend_ssa_find_false_dependencies (op_array , & ssa ) != SUCCESS ) {
110
+ zend_arena_release (& ctx -> arena , checkpoint );
111
+ return ;
112
+ }
113
+
114
+ if (zend_ssa_find_sccs (op_array , & ssa ) != SUCCESS ){
115
+ zend_arena_release (& ctx -> arena , checkpoint );
116
+ return ;
117
+ }
118
+
119
+ if (zend_ssa_inference (& ctx -> arena , op_array , ctx -> script , & ssa ) != SUCCESS ) {
120
+ return ;
121
+ }
122
+
123
+ if (ctx -> debug_level & ZEND_DUMP_DFA_SSA_VARS ) {
124
+ zend_dump_ssa_variables (op_array , & ssa );
125
+ }
126
+
97
127
if (ctx -> debug_level & ZEND_DUMP_BEFORE_DFA_PASS ) {
98
- zend_dump_op_array (op_array , & cfg , ZEND_DUMP_UNREACHABLE , "before dfa pass" );
128
+ zend_dump_op_array (op_array , ZEND_DUMP_SSA | ZEND_DUMP_HIDE_UNUSED_VARS , "before dfa pass" , & ssa );
99
129
}
100
130
101
- //TODO: ???
131
+ //TODO: Add optimization ???
102
132
103
133
if (ctx -> debug_level & ZEND_DUMP_AFTER_DFA_PASS ) {
104
- zend_dump_op_array (op_array , & cfg , 0 , "after dfa pass" );
134
+ zend_dump_op_array (op_array , ZEND_DUMP_SSA | ZEND_DUMP_HIDE_UNUSED_VARS , "after dfa pass" , & ssa );
105
135
}
106
136
107
137
/* Destroy SSA */
0 commit comments