Mercurial > hg > CbC > CbC_gcc
comparison gcc/tree-ssa-copy.c @ 67:f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author | nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 22 Mar 2011 17:18:12 +0900 |
parents | b7f97abdc517 |
children | 04ced10e8804 |
comparison
equal
deleted
inserted
replaced
65:65488c3d617d | 67:f6334be47118 |
---|---|
25 #include "tree.h" | 25 #include "tree.h" |
26 #include "flags.h" | 26 #include "flags.h" |
27 #include "tm_p.h" | 27 #include "tm_p.h" |
28 #include "basic-block.h" | 28 #include "basic-block.h" |
29 #include "output.h" | 29 #include "output.h" |
30 #include "expr.h" | |
31 #include "function.h" | 30 #include "function.h" |
32 #include "diagnostic.h" | |
33 #include "tree-pretty-print.h" | 31 #include "tree-pretty-print.h" |
34 #include "gimple-pretty-print.h" | 32 #include "gimple-pretty-print.h" |
35 #include "timevar.h" | 33 #include "timevar.h" |
36 #include "tree-dump.h" | 34 #include "tree-dump.h" |
37 #include "tree-flow.h" | 35 #include "tree-flow.h" |
191 | 189 |
192 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME). | 190 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME). |
193 | 191 |
194 Use this version when not const/copy propagating values. For example, | 192 Use this version when not const/copy propagating values. For example, |
195 PRE uses this version when building expressions as they would appear | 193 PRE uses this version when building expressions as they would appear |
196 in specific blocks taking into account actions of PHI nodes. */ | 194 in specific blocks taking into account actions of PHI nodes. |
195 | |
196 The statement in which an expression has been replaced should be | |
197 folded using fold_stmt_inplace. */ | |
197 | 198 |
198 void | 199 void |
199 replace_exp (use_operand_p op_p, tree val) | 200 replace_exp (use_operand_p op_p, tree val) |
200 { | 201 { |
201 replace_exp_1 (op_p, val, false); | 202 replace_exp_1 (op_p, val, false); |
211 Be sure to mark the stmt as modified. */ | 212 Be sure to mark the stmt as modified. */ |
212 | 213 |
213 void | 214 void |
214 propagate_tree_value (tree *op_p, tree val) | 215 propagate_tree_value (tree *op_p, tree val) |
215 { | 216 { |
216 #if defined ENABLE_CHECKING | 217 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME |
217 gcc_assert (!(TREE_CODE (val) == SSA_NAME | 218 && *op_p |
218 && *op_p | 219 && TREE_CODE (*op_p) == SSA_NAME |
219 && TREE_CODE (*op_p) == SSA_NAME | 220 && !may_propagate_copy (*op_p, val))); |
220 && !may_propagate_copy (*op_p, val))); | |
221 #endif | |
222 | 221 |
223 if (TREE_CODE (val) == SSA_NAME) | 222 if (TREE_CODE (val) == SSA_NAME) |
224 *op_p = val; | 223 *op_p = val; |
225 else | 224 else |
226 *op_p = unsave_expr_now (val); | 225 *op_p = unsave_expr_now (val); |
248 stmt = gsi_stmt (*gsi); | 247 stmt = gsi_stmt (*gsi); |
249 } | 248 } |
250 else if (gimple_code (stmt) == GIMPLE_COND) | 249 else if (gimple_code (stmt) == GIMPLE_COND) |
251 { | 250 { |
252 tree lhs = NULL_TREE; | 251 tree lhs = NULL_TREE; |
253 tree rhs = fold_convert (TREE_TYPE (val), integer_zero_node); | 252 tree rhs = build_zero_cst (TREE_TYPE (val)); |
254 propagate_tree_value (&lhs, val); | 253 propagate_tree_value (&lhs, val); |
255 gimple_cond_set_code (stmt, NE_EXPR); | 254 gimple_cond_set_code (stmt, NE_EXPR); |
256 gimple_cond_set_lhs (stmt, lhs); | 255 gimple_cond_set_lhs (stmt, lhs); |
257 gimple_cond_set_rhs (stmt, rhs); | 256 gimple_cond_set_rhs (stmt, rhs); |
258 } | 257 } |
274 } | 273 } |
275 | 274 |
276 /*--------------------------------------------------------------------------- | 275 /*--------------------------------------------------------------------------- |
277 Copy propagation | 276 Copy propagation |
278 ---------------------------------------------------------------------------*/ | 277 ---------------------------------------------------------------------------*/ |
279 /* During propagation, we keep chains of variables that are copies of | 278 /* Lattice for copy-propagation. The lattice is initialized to |
280 one another. If variable X_i is a copy of X_j and X_j is a copy of | 279 UNDEFINED (value == NULL) for SSA names that can become a copy |
281 X_k, COPY_OF will contain: | 280 of something or VARYING (value == self) if not (see get_copy_of_val |
282 | 281 and stmt_may_generate_copy). Other values make the name a COPY |
283 COPY_OF[i].VALUE = X_j | 282 of that value. |
284 COPY_OF[j].VALUE = X_k | 283 |
285 COPY_OF[k].VALUE = X_k | 284 When visiting a statement or PHI node the lattice value for an |
286 | 285 SSA name can transition from UNDEFINED to COPY to VARYING. */ |
287 After propagation, the copy-of value for each variable X_i is | 286 |
288 converted into the final value by walking the copy-of chains and | 287 struct prop_value_d { |
289 updating COPY_OF[i].VALUE to be the last element of the chain. */ | 288 /* Copy-of value. */ |
289 tree value; | |
290 }; | |
291 typedef struct prop_value_d prop_value_t; | |
292 | |
290 static prop_value_t *copy_of; | 293 static prop_value_t *copy_of; |
291 | |
292 /* Used in set_copy_of_val to determine if the last link of a copy-of | |
293 chain has changed. */ | |
294 static tree *cached_last_copy_of; | |
295 | 294 |
296 | 295 |
297 /* Return true if this statement may generate a useful copy. */ | 296 /* Return true if this statement may generate a useful copy. */ |
298 | 297 |
299 static bool | 298 static bool |
338 } | 337 } |
339 | 338 |
340 return val; | 339 return val; |
341 } | 340 } |
342 | 341 |
343 | 342 /* Return the variable VAR is a copy of or VAR if VAR isn't the result |
344 /* Return last link in the copy-of chain for VAR. */ | 343 of a copy. */ |
345 | 344 |
346 static tree | 345 static inline tree |
347 get_last_copy_of (tree var) | 346 valueize_val (tree var) |
348 { | 347 { |
349 tree last; | 348 if (TREE_CODE (var) == SSA_NAME) |
350 int i; | 349 { |
351 | 350 tree val = get_copy_of_val (var)->value; |
352 /* Traverse COPY_OF starting at VAR until we get to the last | 351 if (val) |
353 link in the chain. Since it is possible to have cycles in PHI | 352 return val; |
354 nodes, the copy-of chain may also contain cycles. | 353 } |
355 | 354 return var; |
356 To avoid infinite loops and to avoid traversing lengthy copy-of | 355 } |
357 chains, we artificially limit the maximum number of chains we are | 356 |
358 willing to traverse. | 357 /* Set VAL to be the copy of VAR. If that changed return true. */ |
359 | |
360 The value 5 was taken from a compiler and runtime library | |
361 bootstrap and a mixture of C and C++ code from various sources. | |
362 More than 82% of all copy-of chains were shorter than 5 links. */ | |
363 #define LIMIT 5 | |
364 | |
365 last = var; | |
366 for (i = 0; i < LIMIT; i++) | |
367 { | |
368 tree copy = copy_of[SSA_NAME_VERSION (last)].value; | |
369 if (copy == NULL_TREE || copy == last) | |
370 break; | |
371 last = copy; | |
372 } | |
373 | |
374 /* If we have reached the limit, then we are either in a copy-of | |
375 cycle or the copy-of chain is too long. In this case, just | |
376 return VAR so that it is not considered a copy of anything. */ | |
377 return (i < LIMIT ? last : var); | |
378 } | |
379 | |
380 | |
381 /* Set FIRST to be the first variable in the copy-of chain for DEST. | |
382 If DEST's copy-of value or its copy-of chain has changed, return | |
383 true. | |
384 | |
385 MEM_REF is the memory reference where FIRST is stored. This is | |
386 used when DEST is a non-register and we are copy propagating loads | |
387 and stores. */ | |
388 | 358 |
389 static inline bool | 359 static inline bool |
390 set_copy_of_val (tree dest, tree first) | 360 set_copy_of_val (tree var, tree val) |
391 { | 361 { |
392 unsigned int dest_ver = SSA_NAME_VERSION (dest); | 362 unsigned int ver = SSA_NAME_VERSION (var); |
393 tree old_first, old_last, new_last; | 363 tree old; |
394 | 364 |
395 /* Set FIRST to be the first link in COPY_OF[DEST]. If that | 365 /* Set FIRST to be the first link in COPY_OF[DEST]. If that |
396 changed, return true. */ | 366 changed, return true. */ |
397 old_first = copy_of[dest_ver].value; | 367 old = copy_of[ver].value; |
398 copy_of[dest_ver].value = first; | 368 copy_of[ver].value = val; |
399 | 369 |
400 if (old_first != first) | 370 if (old != val |
371 || (val && !operand_equal_p (old, val, 0))) | |
401 return true; | 372 return true; |
402 | 373 |
403 /* If FIRST and OLD_FIRST are the same, we need to check whether the | 374 return false; |
404 copy-of chain starting at FIRST ends in a different variable. If | |
405 the copy-of chain starting at FIRST ends up in a different | |
406 variable than the last cached value we had for DEST, then return | |
407 true because DEST is now a copy of a different variable. | |
408 | |
409 This test is necessary because even though the first link in the | |
410 copy-of chain may not have changed, if any of the variables in | |
411 the copy-of chain changed its final value, DEST will now be the | |
412 copy of a different variable, so we have to do another round of | |
413 propagation for everything that depends on DEST. */ | |
414 old_last = cached_last_copy_of[dest_ver]; | |
415 new_last = get_last_copy_of (dest); | |
416 cached_last_copy_of[dest_ver] = new_last; | |
417 | |
418 return (old_last != new_last); | |
419 } | 375 } |
420 | 376 |
421 | 377 |
422 /* Dump the copy-of value for variable VAR to FILE. */ | 378 /* Dump the copy-of value for variable VAR to FILE. */ |
423 | 379 |
424 static void | 380 static void |
425 dump_copy_of (FILE *file, tree var) | 381 dump_copy_of (FILE *file, tree var) |
426 { | 382 { |
427 tree val; | 383 tree val; |
428 sbitmap visited; | |
429 | 384 |
430 print_generic_expr (file, var, dump_flags); | 385 print_generic_expr (file, var, dump_flags); |
431 | |
432 if (TREE_CODE (var) != SSA_NAME) | 386 if (TREE_CODE (var) != SSA_NAME) |
433 return; | 387 return; |
434 | 388 |
435 visited = sbitmap_alloc (num_ssa_names); | 389 val = copy_of[SSA_NAME_VERSION (var)].value; |
436 sbitmap_zero (visited); | |
437 SET_BIT (visited, SSA_NAME_VERSION (var)); | |
438 | |
439 fprintf (file, " copy-of chain: "); | 390 fprintf (file, " copy-of chain: "); |
440 | 391 print_generic_expr (file, var, 0); |
441 val = var; | |
442 print_generic_expr (file, val, 0); | |
443 fprintf (file, " "); | 392 fprintf (file, " "); |
444 while (copy_of[SSA_NAME_VERSION (val)].value) | 393 if (!val) |
394 fprintf (file, "[UNDEFINED]"); | |
395 else if (val == var) | |
396 fprintf (file, "[NOT A COPY]"); | |
397 else | |
445 { | 398 { |
446 fprintf (file, "-> "); | 399 fprintf (file, "-> "); |
447 val = copy_of[SSA_NAME_VERSION (val)].value; | |
448 print_generic_expr (file, val, 0); | 400 print_generic_expr (file, val, 0); |
449 fprintf (file, " "); | 401 fprintf (file, " "); |
450 if (TEST_BIT (visited, SSA_NAME_VERSION (val))) | 402 fprintf (file, "[COPY]"); |
451 break; | 403 } |
452 SET_BIT (visited, SSA_NAME_VERSION (val)); | |
453 } | |
454 | |
455 val = get_copy_of_val (var)->value; | |
456 if (val == NULL_TREE) | |
457 fprintf (file, "[UNDEFINED]"); | |
458 else if (val != var) | |
459 fprintf (file, "[COPY]"); | |
460 else | |
461 fprintf (file, "[NOT A COPY]"); | |
462 | |
463 sbitmap_free (visited); | |
464 } | 404 } |
465 | 405 |
466 | 406 |
467 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS | 407 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS |
468 value and store the LHS into *RESULT_P. If STMT generates more | 408 value and store the LHS into *RESULT_P. */ |
469 than one name (i.e., STMT is an aliased store), it is enough to | |
470 store the first name in the VDEF list into *RESULT_P. After | |
471 all, the names generated will be VUSEd in the same statements. */ | |
472 | 409 |
473 static enum ssa_prop_result | 410 static enum ssa_prop_result |
474 copy_prop_visit_assignment (gimple stmt, tree *result_p) | 411 copy_prop_visit_assignment (gimple stmt, tree *result_p) |
475 { | 412 { |
476 tree lhs, rhs; | 413 tree lhs, rhs; |
477 prop_value_t *rhs_val; | |
478 | 414 |
479 lhs = gimple_assign_lhs (stmt); | 415 lhs = gimple_assign_lhs (stmt); |
480 rhs = gimple_assign_rhs1 (stmt); | 416 rhs = valueize_val (gimple_assign_rhs1 (stmt)); |
481 | |
482 | |
483 gcc_assert (gimple_assign_rhs_code (stmt) == SSA_NAME); | |
484 | |
485 rhs_val = get_copy_of_val (rhs); | |
486 | 417 |
487 if (TREE_CODE (lhs) == SSA_NAME) | 418 if (TREE_CODE (lhs) == SSA_NAME) |
488 { | 419 { |
489 /* Straight copy between two SSA names. First, make sure that | 420 /* Straight copy between two SSA names. First, make sure that |
490 we can propagate the RHS into uses of LHS. */ | 421 we can propagate the RHS into uses of LHS. */ |
491 if (!may_propagate_copy (lhs, rhs)) | 422 if (!may_propagate_copy (lhs, rhs)) |
492 return SSA_PROP_VARYING; | 423 return SSA_PROP_VARYING; |
493 | 424 |
494 /* Notice that in the case of assignments, we make the LHS be a | |
495 copy of RHS's value, not of RHS itself. This avoids keeping | |
496 unnecessary copy-of chains (assignments cannot be in a cycle | |
497 like PHI nodes), speeding up the propagation process. | |
498 This is different from what we do in copy_prop_visit_phi_node. | |
499 In those cases, we are interested in the copy-of chains. */ | |
500 *result_p = lhs; | 425 *result_p = lhs; |
501 if (set_copy_of_val (*result_p, rhs_val->value)) | 426 if (set_copy_of_val (*result_p, rhs)) |
502 return SSA_PROP_INTERESTING; | 427 return SSA_PROP_INTERESTING; |
503 else | 428 else |
504 return SSA_PROP_NOT_INTERESTING; | 429 return SSA_PROP_NOT_INTERESTING; |
505 } | 430 } |
506 | 431 |
523 | 448 |
524 /* The only conditionals that we may be able to compute statically | 449 /* The only conditionals that we may be able to compute statically |
525 are predicates involving two SSA_NAMEs. */ | 450 are predicates involving two SSA_NAMEs. */ |
526 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME) | 451 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME) |
527 { | 452 { |
528 op0 = get_last_copy_of (op0); | 453 op0 = valueize_val (op0); |
529 op1 = get_last_copy_of (op1); | 454 op1 = valueize_val (op1); |
530 | 455 |
531 /* See if we can determine the predicate's value. */ | 456 /* See if we can determine the predicate's value. */ |
532 if (dump_file && (dump_flags & TDF_DETAILS)) | 457 if (dump_file && (dump_flags & TDF_DETAILS)) |
533 { | 458 { |
534 fprintf (dump_file, "Trying to determine truth value of "); | 459 fprintf (dump_file, "Trying to determine truth value of "); |
582 fprintf (dump_file, "\n"); | 507 fprintf (dump_file, "\n"); |
583 } | 508 } |
584 | 509 |
585 if (gimple_assign_single_p (stmt) | 510 if (gimple_assign_single_p (stmt) |
586 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME | 511 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME |
587 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME) | 512 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME |
513 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))) | |
588 { | 514 { |
589 /* If the statement is a copy assignment, evaluate its RHS to | 515 /* If the statement is a copy assignment, evaluate its RHS to |
590 see if the lattice value of its output has changed. */ | 516 see if the lattice value of its output has changed. */ |
591 retval = copy_prop_visit_assignment (stmt, result_p); | 517 retval = copy_prop_visit_assignment (stmt, result_p); |
592 } | 518 } |
626 static enum ssa_prop_result | 552 static enum ssa_prop_result |
627 copy_prop_visit_phi_node (gimple phi) | 553 copy_prop_visit_phi_node (gimple phi) |
628 { | 554 { |
629 enum ssa_prop_result retval; | 555 enum ssa_prop_result retval; |
630 unsigned i; | 556 unsigned i; |
631 prop_value_t phi_val = { 0, NULL_TREE }; | 557 prop_value_t phi_val = { NULL_TREE }; |
632 | 558 |
633 tree lhs = gimple_phi_result (phi); | 559 tree lhs = gimple_phi_result (phi); |
634 | 560 |
635 if (dump_file && (dump_flags & TDF_DETAILS)) | 561 if (dump_file && (dump_flags & TDF_DETAILS)) |
636 { | 562 { |
637 fprintf (dump_file, "\nVisiting PHI node: "); | 563 fprintf (dump_file, "\nVisiting PHI node: "); |
638 print_gimple_stmt (dump_file, phi, 0, dump_flags); | 564 print_gimple_stmt (dump_file, phi, 0, dump_flags); |
639 fprintf (dump_file, "\n\n"); | |
640 } | 565 } |
641 | 566 |
642 for (i = 0; i < gimple_phi_num_args (phi); i++) | 567 for (i = 0; i < gimple_phi_num_args (phi); i++) |
643 { | 568 { |
644 prop_value_t *arg_val; | 569 prop_value_t *arg_val; |
662 /* Avoid copy propagation from an inner into an outer loop. | 587 /* Avoid copy propagation from an inner into an outer loop. |
663 Otherwise, this may move loop variant variables outside of | 588 Otherwise, this may move loop variant variables outside of |
664 their loops and prevent coalescing opportunities. If the | 589 their loops and prevent coalescing opportunities. If the |
665 value was loop invariant, it will be hoisted by LICM and | 590 value was loop invariant, it will be hoisted by LICM and |
666 exposed for copy propagation. Not a problem for virtual | 591 exposed for copy propagation. Not a problem for virtual |
667 operands though. */ | 592 operands though. |
593 ??? The value will be always loop invariant. */ | |
668 if (is_gimple_reg (lhs) | 594 if (is_gimple_reg (lhs) |
669 && loop_depth_of_name (arg) > loop_depth_of_name (lhs)) | 595 && loop_depth_of_name (arg) > loop_depth_of_name (lhs)) |
670 { | 596 { |
671 phi_val.value = lhs; | 597 phi_val.value = lhs; |
672 break; | 598 break; |
673 } | 599 } |
674 | |
675 /* If the LHS appears in the argument list, ignore it. It is | |
676 irrelevant as a copy. */ | |
677 if (arg == lhs || get_last_copy_of (arg) == lhs) | |
678 continue; | |
679 | 600 |
680 if (dump_file && (dump_flags & TDF_DETAILS)) | 601 if (dump_file && (dump_flags & TDF_DETAILS)) |
681 { | 602 { |
682 fprintf (dump_file, "\tArgument #%d: ", i); | 603 fprintf (dump_file, "\tArgument #%d: ", i); |
683 dump_copy_of (dump_file, arg); | 604 dump_copy_of (dump_file, arg); |
684 fprintf (dump_file, "\n"); | 605 fprintf (dump_file, "\n"); |
685 } | 606 } |
686 | 607 |
687 arg_val = get_copy_of_val (arg); | 608 arg_val = get_copy_of_val (arg); |
688 | 609 |
610 /* If we didn't visit the definition of arg yet treat it as | |
611 UNDEFINED. This also handles PHI arguments that are the | |
612 same as lhs. We'll come here again. */ | |
613 if (!arg_val->value) | |
614 continue; | |
615 | |
689 /* If the LHS didn't have a value yet, make it a copy of the | 616 /* If the LHS didn't have a value yet, make it a copy of the |
690 first argument we find. Notice that while we make the LHS be | 617 first argument we find. */ |
691 a copy of the argument itself, we take the memory reference | |
692 from the argument's value so that we can compare it to the | |
693 memory reference of all the other arguments. */ | |
694 if (phi_val.value == NULL_TREE) | 618 if (phi_val.value == NULL_TREE) |
695 { | 619 { |
696 phi_val.value = arg_val->value ? arg_val->value : arg; | 620 phi_val.value = arg_val->value; |
697 continue; | 621 continue; |
698 } | 622 } |
699 | 623 |
700 /* If PHI_VAL and ARG don't have a common copy-of chain, then | 624 /* If PHI_VAL and ARG don't have a common copy-of chain, then |
701 this PHI node cannot be a copy operation. Also, if we are | 625 this PHI node cannot be a copy operation. */ |
702 copy propagating stores and these two arguments came from | 626 if (phi_val.value != arg_val->value |
703 different memory references, they cannot be considered | 627 && !operand_equal_p (phi_val.value, arg_val->value, 0)) |
704 copies. */ | |
705 if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg)) | |
706 { | 628 { |
707 phi_val.value = lhs; | 629 phi_val.value = lhs; |
708 break; | 630 break; |
709 } | 631 } |
710 } | 632 } |
711 | 633 |
712 if (phi_val.value && may_propagate_copy (lhs, phi_val.value) | 634 if (phi_val.value |
635 && may_propagate_copy (lhs, phi_val.value) | |
713 && set_copy_of_val (lhs, phi_val.value)) | 636 && set_copy_of_val (lhs, phi_val.value)) |
714 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; | 637 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; |
715 else | 638 else |
716 retval = SSA_PROP_NOT_INTERESTING; | 639 retval = SSA_PROP_NOT_INTERESTING; |
717 | 640 |
718 if (dump_file && (dump_flags & TDF_DETAILS)) | 641 if (dump_file && (dump_flags & TDF_DETAILS)) |
719 { | 642 { |
720 fprintf (dump_file, "\nPHI node "); | 643 fprintf (dump_file, "PHI node "); |
721 dump_copy_of (dump_file, lhs); | 644 dump_copy_of (dump_file, lhs); |
722 fprintf (dump_file, "\nTelling the propagator to "); | 645 fprintf (dump_file, "\nTelling the propagator to "); |
723 if (retval == SSA_PROP_INTERESTING) | 646 if (retval == SSA_PROP_INTERESTING) |
724 fprintf (dump_file, "add SSA edges out of this PHI and continue."); | 647 fprintf (dump_file, "add SSA edges out of this PHI and continue."); |
725 else if (retval == SSA_PROP_VARYING) | 648 else if (retval == SSA_PROP_VARYING) |
731 | 654 |
732 return retval; | 655 return retval; |
733 } | 656 } |
734 | 657 |
735 | 658 |
736 /* Initialize structures used for copy propagation. PHIS_ONLY is true | 659 /* Initialize structures used for copy propagation. */ |
737 if we should only consider PHI nodes as generating copy propagation | |
738 opportunities. */ | |
739 | 660 |
740 static void | 661 static void |
741 init_copy_prop (void) | 662 init_copy_prop (void) |
742 { | 663 { |
743 basic_block bb; | 664 basic_block bb; |
744 | 665 |
745 copy_of = XCNEWVEC (prop_value_t, num_ssa_names); | 666 copy_of = XCNEWVEC (prop_value_t, num_ssa_names); |
746 | |
747 cached_last_copy_of = XCNEWVEC (tree, num_ssa_names); | |
748 | 667 |
749 FOR_EACH_BB (bb) | 668 FOR_EACH_BB (bb) |
750 { | 669 { |
751 gimple_stmt_iterator si; | 670 gimple_stmt_iterator si; |
752 int depth = bb->loop_depth; | 671 int depth = bb->loop_depth; |
765 | 684 |
766 Avoid copy propagation from an inner into an outer loop. | 685 Avoid copy propagation from an inner into an outer loop. |
767 Otherwise, this may move loop variant variables outside of | 686 Otherwise, this may move loop variant variables outside of |
768 their loops and prevent coalescing opportunities. If the | 687 their loops and prevent coalescing opportunities. If the |
769 value was loop invariant, it will be hoisted by LICM and | 688 value was loop invariant, it will be hoisted by LICM and |
770 exposed for copy propagation. */ | 689 exposed for copy propagation. |
690 ??? This doesn't make sense. */ | |
771 if (stmt_ends_bb_p (stmt)) | 691 if (stmt_ends_bb_p (stmt)) |
772 prop_set_simulate_again (stmt, true); | 692 prop_set_simulate_again (stmt, true); |
773 else if (stmt_may_generate_copy (stmt) | 693 else if (stmt_may_generate_copy (stmt) |
774 /* Since we are iterating over the statements in | 694 /* Since we are iterating over the statements in |
775 BB, not the phi nodes, STMT will always be an | 695 BB, not the phi nodes, STMT will always be an |
782 /* Mark all the outputs of this statement as not being | 702 /* Mark all the outputs of this statement as not being |
783 the copy of anything. */ | 703 the copy of anything. */ |
784 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) | 704 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) |
785 if (!prop_simulate_again_p (stmt)) | 705 if (!prop_simulate_again_p (stmt)) |
786 set_copy_of_val (def, def); | 706 set_copy_of_val (def, def); |
787 else | |
788 cached_last_copy_of[SSA_NAME_VERSION (def)] = def; | |
789 } | 707 } |
790 | 708 |
791 /* In loop-closed SSA form do not copy-propagate through | 709 /* In loop-closed SSA form do not copy-propagate through |
792 PHI nodes in blocks with a loop exit edge predecessor. */ | 710 PHI nodes in blocks with a loop exit edge predecessor. */ |
793 if (current_loops | 711 if (current_loops |
812 else | 730 else |
813 prop_set_simulate_again (phi, true); | 731 prop_set_simulate_again (phi, true); |
814 | 732 |
815 if (!prop_simulate_again_p (phi)) | 733 if (!prop_simulate_again_p (phi)) |
816 set_copy_of_val (def, def); | 734 set_copy_of_val (def, def); |
817 else | 735 } |
818 cached_last_copy_of[SSA_NAME_VERSION (def)] = def; | 736 } |
819 } | 737 } |
820 } | 738 |
821 } | 739 /* Callback for substitute_and_fold to get at the final copy-of values. */ |
822 | 740 |
741 static tree | |
742 get_value (tree name) | |
743 { | |
744 tree val = copy_of[SSA_NAME_VERSION (name)].value; | |
745 if (val && val != name) | |
746 return val; | |
747 return NULL_TREE; | |
748 } | |
823 | 749 |
824 /* Deallocate memory used in copy propagation and do final | 750 /* Deallocate memory used in copy propagation and do final |
825 substitution. */ | 751 substitution. */ |
826 | 752 |
827 static void | 753 static void |
828 fini_copy_prop (void) | 754 fini_copy_prop (void) |
829 { | 755 { |
830 size_t i; | 756 unsigned i; |
831 prop_value_t *tmp; | |
832 | 757 |
833 /* Set the final copy-of value for each variable by traversing the | 758 /* Set the final copy-of value for each variable by traversing the |
834 copy-of chains. */ | 759 copy-of chains. */ |
835 tmp = XCNEWVEC (prop_value_t, num_ssa_names); | |
836 for (i = 1; i < num_ssa_names; i++) | 760 for (i = 1; i < num_ssa_names; i++) |
837 { | 761 { |
838 tree var = ssa_name (i); | 762 tree var = ssa_name (i); |
839 if (!var | 763 if (!var |
840 || !copy_of[i].value | 764 || !copy_of[i].value |
841 || copy_of[i].value == var) | 765 || copy_of[i].value == var) |
842 continue; | 766 continue; |
843 | |
844 tmp[i].value = get_last_copy_of (var); | |
845 | 767 |
846 /* In theory the points-to solution of all members of the | 768 /* In theory the points-to solution of all members of the |
847 copy chain is their intersection. For now we do not bother | 769 copy chain is their intersection. For now we do not bother |
848 to compute this but only make sure we do not lose points-to | 770 to compute this but only make sure we do not lose points-to |
849 information completely by setting the points-to solution | 771 information completely by setting the points-to solution |
850 of the representative to the first solution we find if | 772 of the representative to the first solution we find if |
851 it doesn't have one already. */ | 773 it doesn't have one already. */ |
852 if (tmp[i].value != var | 774 if (copy_of[i].value != var |
853 && POINTER_TYPE_P (TREE_TYPE (var)) | 775 && POINTER_TYPE_P (TREE_TYPE (var)) |
854 && SSA_NAME_PTR_INFO (var) | 776 && SSA_NAME_PTR_INFO (var) |
855 && !SSA_NAME_PTR_INFO (tmp[i].value)) | 777 && !SSA_NAME_PTR_INFO (copy_of[i].value)) |
856 duplicate_ssa_name_ptr_info (tmp[i].value, SSA_NAME_PTR_INFO (var)); | 778 duplicate_ssa_name_ptr_info (copy_of[i].value, SSA_NAME_PTR_INFO (var)); |
857 } | 779 } |
858 | 780 |
859 substitute_and_fold (tmp, NULL); | 781 /* Don't do DCE if we have loops. That's the simplest way to not |
860 | 782 destroy the scev cache. */ |
861 free (cached_last_copy_of); | 783 substitute_and_fold (get_value, NULL, !current_loops); |
784 | |
862 free (copy_of); | 785 free (copy_of); |
863 free (tmp); | |
864 } | 786 } |
865 | 787 |
866 | 788 |
867 /* Main entry point to the copy propagator. | 789 /* Main entry point to the copy propagator. |
868 | 790 |
893 | 815 |
894 When visiting PHI nodes, we only consider arguments that flow | 816 When visiting PHI nodes, we only consider arguments that flow |
895 through edges marked executable by the propagation engine. So, | 817 through edges marked executable by the propagation engine. So, |
896 when visiting statement #2 for the first time, we will only look at | 818 when visiting statement #2 for the first time, we will only look at |
897 the first argument (a_24) and optimistically assume that its value | 819 the first argument (a_24) and optimistically assume that its value |
898 is the copy of a_24 (x_1). | 820 is the copy of a_24 (x_1). */ |
899 | |
900 The problem with this approach is that it may fail to discover copy | |
901 relations in PHI cycles. Instead of propagating copy-of | |
902 values, we actually propagate copy-of chains. For instance: | |
903 | |
904 A_3 = B_1; | |
905 C_9 = A_3; | |
906 D_4 = C_9; | |
907 X_i = D_4; | |
908 | |
909 In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }. | |
910 Obviously, we are only really interested in the last value of the | |
911 chain, however the propagator needs to access the copy-of chain | |
912 when visiting PHI nodes. | |
913 | |
914 To represent the copy-of chain, we use the array COPY_CHAINS, which | |
915 holds the first link in the copy-of chain for every variable. | |
916 If variable X_i is a copy of X_j, which in turn is a copy of X_k, | |
917 the array will contain: | |
918 | |
919 COPY_CHAINS[i] = X_j | |
920 COPY_CHAINS[j] = X_k | |
921 COPY_CHAINS[k] = X_k | |
922 | |
923 Keeping copy-of chains instead of copy-of values directly becomes | |
924 important when visiting PHI nodes. Suppose that we had the | |
925 following PHI cycle, such that x_52 is already considered a copy of | |
926 x_53: | |
927 | |
928 1 x_54 = PHI <x_53, x_52> | |
929 2 x_53 = PHI <x_898, x_54> | |
930 | |
931 Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53) | |
932 Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53, | |
933 so it is considered irrelevant | |
934 as a copy). | |
935 Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and | |
936 x_52 is a copy of x_53, so | |
937 they don't match) | |
938 Visit #2: x_53 is copy-of nothing | |
939 | |
940 This problem is avoided by keeping a chain of copies, instead of | |
941 the final copy-of value. Propagation will now only keep the first | |
942 element of a variable's copy-of chain. When visiting PHI nodes, | |
943 arguments are considered equal if their copy-of chains end in the | |
944 same variable. So, as long as their copy-of chains overlap, we | |
945 know that they will be a copy of the same variable, regardless of | |
946 which variable that may be). | |
947 | |
948 Propagation would then proceed as follows (the notation a -> b | |
949 means that a is a copy-of b): | |
950 | |
951 Visit #1: x_54 = PHI <x_53, x_52> | |
952 x_53 -> x_53 | |
953 x_52 -> x_53 | |
954 Result: x_54 -> x_53. Value changed. Add SSA edges. | |
955 | |
956 Visit #1: x_53 = PHI <x_898, x_54> | |
957 x_898 -> x_898 | |
958 x_54 -> x_53 | |
959 Result: x_53 -> x_898. Value changed. Add SSA edges. | |
960 | |
961 Visit #2: x_54 = PHI <x_53, x_52> | |
962 x_53 -> x_898 | |
963 x_52 -> x_53 -> x_898 | |
964 Result: x_54 -> x_898. Value changed. Add SSA edges. | |
965 | |
966 Visit #2: x_53 = PHI <x_898, x_54> | |
967 x_898 -> x_898 | |
968 x_54 -> x_898 | |
969 Result: x_53 -> x_898. Value didn't change. Stable state | |
970 | |
971 Once the propagator stabilizes, we end up with the desired result | |
972 x_53 and x_54 are both copies of x_898. */ | |
973 | 821 |
974 static unsigned int | 822 static unsigned int |
975 execute_copy_prop (void) | 823 execute_copy_prop (void) |
976 { | 824 { |
977 init_copy_prop (); | 825 init_copy_prop (); |