Changeset 312
- Timestamp:
- 12/09/2006 05:28:25 PM (6 years ago)
- Files:
-
- 1 modified
-
trunk/optimizer.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/optimizer.c
r308 r312 1 #if 11 #if 0 2 2 #define DEBUG 3 3 #endif … … 29 29 bbid_t jmpout_op2; 30 30 bbid_t jmpout_ext; 31 bbid_t f ollow;31 bbid_t fall; 32 32 } bb_t; 33 33 /* }}} */ … … 102 102 } 103 103 /* }}} */ 104 static int op_get_jmpout( bb_t *bb, zend_op *opcodes, zend_op *opline) /* {{{ */105 { 106 /* break= have follow*/104 static int op_get_jmpout(int *jmpout_op1, int *jmpout_op2, int *jmpout_ext, zend_bool *fall, zend_op *opline) /* {{{ */ 105 { 106 /* break=will fall */ 107 107 switch (opline->opcode) { 108 108 case ZEND_RETURN: 109 109 case ZEND_EXIT: 110 break;110 return SUCCESS; /* no fall */ 111 111 112 112 case ZEND_JMP: 113 bb->jmpout_op1 = opline->op1.u.opline_num;114 return SUCCESS; /* no f ollow*/113 *jmpout_op1 = opline->op1.u.opline_num; 114 return SUCCESS; /* no fall */ 115 115 116 116 case ZEND_JMPZNZ: 117 bb->jmpout_ext = opline->extended_value;118 bb->jmpout_op2 = opline->op2.u.opline_num;117 *jmpout_ext = opline->extended_value; 118 *jmpout_op2 = opline->op2.u.opline_num; 119 119 break; 120 120 … … 131 131 #endif 132 132 case ZEND_FE_FETCH: 133 bb->jmpout_op2 = opline->op2.u.opline_num;133 *jmpout_op2 = opline->op2.u.opline_num; 134 134 break; 135 135 … … 138 138 } 139 139 140 *fall = 1; 140 141 return SUCCESS; 141 142 } … … 153 154 bb->jmpout_op2 = BBID_INVALID; 154 155 bb->jmpout_ext = BBID_INVALID; 155 bb->f ollow= BBID_INVALID;156 bb->fall = BBID_INVALID; 156 157 157 158 if (opcodes) { … … 178 179 } 179 180 /* }}} */ 181 #ifdef DEBUG 182 static void bb_print(bb_t *bb, zend_op *opcodes) /* {{{ */ 183 { 184 fprintf(stderr, 185 "%3d %3d %3d" 186 " %c%c" 187 " %3d %3d %3d %3d\r\n" 188 , bb->id, bb->count, bb->opcodes - opcodes 189 , bb->used ? 'U' : ' ', bb->alloc ? 'A' : ' ' 190 , bb->jmpout_op1, bb->jmpout_op2, bb->jmpout_ext, bb->fall 191 ); 192 } 193 /* }}} */ 194 #endif 195 180 196 #define bbs_get(bbs, n) xc_stack_get(bbs, n) 181 197 static void bbs_destroy(bbs_t *bbs) /* {{{ */ … … 188 204 } 189 205 /* }}} */ 206 #ifdef DEBUG 207 static void bbs_print(bbs_t *bbs, zend_op *opcodes) /* {{{ */ 208 { 209 int i; 210 for (i = 0; i < xc_stack_count(bbs); i ++) { 211 bb_print(bbs_get(bbs, i), opcodes); 212 } 213 } 214 /* }}} */ 215 #endif 190 216 #define bbs_init(bbs) xc_stack_init_ex(bbs, 16) 191 217 static bb_t *bbs_add_bb(bbs_t *bbs, bb_t *bb) /* {{{ */ … … 204 230 { 205 231 int i, prev; 206 bb_t bb, *pbb; 207 zend_bool *markjmpins = do_alloca(count); 208 zend_bool *markjmpouts = do_alloca(count); 209 210 memset(markjmpins, 0, sizeof(zend_bool)); 211 memset(markjmpouts, 0, sizeof(zend_bool)); 232 bb_t *pbb; 233 bbid_t id; 234 int jmpout_op1, jmpout_op2, jmpout_ext; 235 zend_bool fall; 236 bbid_t *bbids = do_alloca(count * sizeof(bbid_t)); 237 zend_bool *markjmpins = do_alloca(count * sizeof(zend_bool)); 238 zend_bool *markjmpouts = do_alloca(count * sizeof(zend_bool)); 239 240 /* {{{ mark jmpin/jumpout */ 241 memset(markjmpins, 0, count * sizeof(zend_bool)); 242 memset(markjmpouts, 0, count * sizeof(zend_bool)); 212 243 213 244 for (i = 0; i < count; i ++) { 214 /* BBID_INVALID=invalidate line 215 * bb.jmpout_op1 bb.jmpout_op2 bb.jmpout_ext bb.follow means opline number here, not basicblock id 216 */ 217 bb.jmpout_op1 = bb.jmpout_op2 = BBID_INVALID; 218 bb.jmpout_ext = bb.follow = BBID_INVALID; 219 if (op_get_jmpout(&bb, opcodes, &opcodes[i]) == SUCCESS) { 245 jmpout_op1 = jmpout_op2 = jmpout_ext = -1; 246 fall = 0; 247 if (op_get_jmpout(&jmpout_op1, &jmpout_op2, &jmpout_ext, &fall, &opcodes[i]) == SUCCESS) { 220 248 markjmpouts[i] = 1; 221 249 222 if (bb.jmpout_op1 != BBID_INVALID) { 223 markjmpins[bb.jmpout_op1] = 1; 224 } 225 if (bb.jmpout_op2 != BBID_INVALID) { 226 markjmpins[bb.jmpout_op2] = 1; 227 } 228 if (bb.jmpout_ext != BBID_INVALID) { 229 markjmpins[bb.jmpout_ext] = 1; 230 } 231 232 if (i < count && bb.follow != BBID_INVALID) { 233 markjmpins[i + 1] = 1; 234 } 235 } 250 if (jmpout_op1 != -1) { 251 markjmpins[jmpout_op1] = 1; 252 } 253 if (jmpout_op2 != -1) { 254 markjmpins[jmpout_op2] = 1; 255 } 256 if (jmpout_ext != -1) { 257 markjmpins[jmpout_ext] = 1; 258 } 259 } 260 } 261 /* }}} */ 262 /* {{{ fill opcodes with newly allocated id */ 263 for (i = 0; i < count; i ++) { 264 bbids[i] = BBID_INVALID; 236 265 } 237 266 238 267 prev = 0; 268 id = 0; 239 269 for (i = 1; i < count; i ++) { 240 if (markjmpins[i]) { 241 pbb = bbs_new_bb_ex(bbs, opcodes + prev, i - prev); 242 op_get_jmpout(pbb, opcodes, &opcodes[prev]); 270 if (markjmpins[i] || markjmpouts[i - 1]) { 271 for (; prev < i; prev ++) { 272 bbids[prev] = id; 273 } 274 id ++; 243 275 prev = i; 244 276 } 245 277 } 246 278 247 if (prev != count - 1) { 248 pbb = bbs_new_bb_ex(bbs, opcodes + prev, count - prev); 249 } 250 279 if (prev < count) { 280 for (; prev < i; prev ++) { 281 bbids[prev] = id; 282 } 283 } 284 /* }}} */ 285 /* {{{ create basic blocks */ 286 prev = 0; 287 id = 0; 288 for (i = 1; i < count; i ++) { 289 if (id == bbids[i]) { 290 continue; 291 } 292 id = bbids[i]; 293 294 pbb = bbs_new_bb_ex(bbs, opcodes + prev, i - prev); 295 jmpout_op1 = jmpout_op2 = jmpout_ext = -1; 296 fall = 0; 297 if (op_get_jmpout(&jmpout_op1, &jmpout_op2, &jmpout_ext, &fall, &opcodes[prev]) == SUCCESS) { 298 if (jmpout_op1 != -1) { 299 pbb->jmpout_op1 = bbids[jmpout_op1]; 300 assert(pbb->jmpout_op1 != BBID_INVALID); 301 } 302 if (jmpout_op2 != -1) { 303 pbb->jmpout_op2 = bbids[jmpout_op2]; 304 assert(pbb->jmpout_op2 != BBID_INVALID); 305 } 306 if (jmpout_ext != -1) { 307 pbb->jmpout_ext = bbids[jmpout_ext]; 308 assert(pbb->jmpout_ext != BBID_INVALID); 309 } 310 if (fall && i + 1 < count) { 311 pbb->fall = bbids[i + 1]; 312 assert(pbb->fall != BBID_INVALID); 313 } 314 } 315 prev = i + 1; 316 } 317 assert(prev == count); 318 /* }}} */ 319 320 free_alloca(bbids); 321 free_alloca(markjmpins); 322 free_alloca(markjmpouts); 251 323 return SUCCESS; 252 324 } … … 265 337 xc_undo_pass_two(op_array TSRMLS_CC); 266 338 #ifdef DEBUG 339 # if 0 267 340 TRACE("optimize file: %s", op_array->filename); 268 341 xc_dprint_zend_op_array(op_array, 0 TSRMLS_CC); 269 #endif 270 271 if (op_array_convert_switch(op_array)) { 342 # endif 343 #endif 344 345 if (op_array_convert_switch(op_array) == SUCCESS) { 272 346 bbs_init(&bbs); 273 if (bbs_build_from(&bbs, op_array->opcodes, op_array->last)) { 347 if (bbs_build_from(&bbs, op_array->opcodes, op_array->last) == SUCCESS) { 348 #ifdef DEBUG 349 bbs_print(&bbs, op_array->opcodes); 350 #endif 274 351 } 275 352 bbs_destroy(&bbs); … … 277 354 278 355 #ifdef DEBUG 356 # if 0 279 357 TRACE("%s", "after compiles"); 280 358 xc_dprint_zend_op_array(op_array, 0 TSRMLS_CC); 359 # endif 281 360 #endif 282 361 xc_redo_pass_two(op_array TSRMLS_CC);

