]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-copy.c
* output.h (__gcc_host_wide_int__): Move to hwint.h.
[thirdparty/gcc.git] / gcc / tree-ssa-copy.c
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "tree-pretty-print.h"
31 #include "gimple-pretty-print.h"
32 #include "timevar.h"
33 #include "tree-dump.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "tree-ssa-propagate.h"
37 #include "langhooks.h"
38 #include "cfgloop.h"
39
40 /* This file implements the copy propagation pass and provides a
41 handful of interfaces for performing const/copy propagation and
42 simple expression replacement which keep variable annotations
43 up-to-date.
44
45 We require that for any copy operation where the RHS and LHS have
46 a non-null memory tag the memory tag be the same. It is OK
47 for one or both of the memory tags to be NULL.
48
49 We also require tracking if a variable is dereferenced in a load or
50 store operation.
51
52 We enforce these requirements by having all copy propagation and
53 replacements of one SSA_NAME with a different SSA_NAME to use the
54 APIs defined in this file. */
55
56 /* Return true if we may propagate ORIG into DEST, false otherwise. */
57
58 bool
59 may_propagate_copy (tree dest, tree orig)
60 {
61 tree type_d = TREE_TYPE (dest);
62 tree type_o = TREE_TYPE (orig);
63
64 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
65 if (TREE_CODE (orig) == SSA_NAME
66 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
67 return false;
68
69 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
70 cannot be replaced. */
71 if (TREE_CODE (dest) == SSA_NAME
72 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
73 return false;
74
75 /* Do not copy between types for which we *do* need a conversion. */
76 if (!useless_type_conversion_p (type_d, type_o))
77 return false;
78
79 /* Propagating virtual operands is always ok. */
80 if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
81 {
82 /* But only between virtual operands. */
83 gcc_assert (TREE_CODE (orig) == SSA_NAME && !is_gimple_reg (orig));
84
85 return true;
86 }
87
88 /* Anything else is OK. */
89 return true;
90 }
91
92 /* Like may_propagate_copy, but use as the destination expression
93 the principal expression (typically, the RHS) contained in
94 statement DEST. This is more efficient when working with the
95 gimple tuples representation. */
96
97 bool
98 may_propagate_copy_into_stmt (gimple dest, tree orig)
99 {
100 tree type_d;
101 tree type_o;
102
103 /* If the statement is a switch or a single-rhs assignment,
104 then the expression to be replaced by the propagation may
105 be an SSA_NAME. Fortunately, there is an explicit tree
106 for the expression, so we delegate to may_propagate_copy. */
107
108 if (gimple_assign_single_p (dest))
109 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
110 else if (gimple_code (dest) == GIMPLE_SWITCH)
111 return may_propagate_copy (gimple_switch_index (dest), orig);
112
113 /* In other cases, the expression is not materialized, so there
114 is no destination to pass to may_propagate_copy. On the other
115 hand, the expression cannot be an SSA_NAME, so the analysis
116 is much simpler. */
117
118 if (TREE_CODE (orig) == SSA_NAME
119 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
120 return false;
121
122 if (is_gimple_assign (dest))
123 type_d = TREE_TYPE (gimple_assign_lhs (dest));
124 else if (gimple_code (dest) == GIMPLE_COND)
125 type_d = boolean_type_node;
126 else if (is_gimple_call (dest)
127 && gimple_call_lhs (dest) != NULL_TREE)
128 type_d = TREE_TYPE (gimple_call_lhs (dest));
129 else
130 gcc_unreachable ();
131
132 type_o = TREE_TYPE (orig);
133
134 if (!useless_type_conversion_p (type_d, type_o))
135 return false;
136
137 return true;
138 }
139
140 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
141
142 bool
143 may_propagate_copy_into_asm (tree dest)
144 {
145 /* Hard register operands of asms are special. Do not bypass. */
146 return !(TREE_CODE (dest) == SSA_NAME
147 && TREE_CODE (SSA_NAME_VAR (dest)) == VAR_DECL
148 && DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
149 }
150
151
152 /* Common code for propagate_value and replace_exp.
153
154 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
155 replacement is done to propagate a value or not. */
156
157 static void
158 replace_exp_1 (use_operand_p op_p, tree val,
159 bool for_propagation ATTRIBUTE_UNUSED)
160 {
161 #if defined ENABLE_CHECKING
162 tree op = USE_FROM_PTR (op_p);
163
164 gcc_assert (!(for_propagation
165 && TREE_CODE (op) == SSA_NAME
166 && TREE_CODE (val) == SSA_NAME
167 && !may_propagate_copy (op, val)));
168 #endif
169
170 if (TREE_CODE (val) == SSA_NAME)
171 SET_USE (op_p, val);
172 else
173 SET_USE (op_p, unsave_expr_now (val));
174 }
175
176
177 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
178 into the operand pointed to by OP_P.
179
180 Use this version for const/copy propagation as it will perform additional
181 checks to ensure validity of the const/copy propagation. */
182
183 void
184 propagate_value (use_operand_p op_p, tree val)
185 {
186 replace_exp_1 (op_p, val, true);
187 }
188
189 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
190
191 Use this version when not const/copy propagating values. For example,
192 PRE uses this version when building expressions as they would appear
193 in specific blocks taking into account actions of PHI nodes.
194
195 The statement in which an expression has been replaced should be
196 folded using fold_stmt_inplace. */
197
198 void
199 replace_exp (use_operand_p op_p, tree val)
200 {
201 replace_exp_1 (op_p, val, false);
202 }
203
204
205 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
206 into the tree pointed to by OP_P.
207
208 Use this version for const/copy propagation when SSA operands are not
209 available. It will perform the additional checks to ensure validity of
210 the const/copy propagation, but will not update any operand information.
211 Be sure to mark the stmt as modified. */
212
213 void
214 propagate_tree_value (tree *op_p, tree val)
215 {
216 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
217 && *op_p
218 && TREE_CODE (*op_p) == SSA_NAME
219 && !may_propagate_copy (*op_p, val)));
220
221 if (TREE_CODE (val) == SSA_NAME)
222 *op_p = val;
223 else
224 *op_p = unsave_expr_now (val);
225 }
226
227
228 /* Like propagate_tree_value, but use as the operand to replace
229 the principal expression (typically, the RHS) contained in the
230 statement referenced by iterator GSI. Note that it is not
231 always possible to update the statement in-place, so a new
232 statement may be created to replace the original. */
233
234 void
235 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
236 {
237 gimple stmt = gsi_stmt (*gsi);
238
239 if (is_gimple_assign (stmt))
240 {
241 tree expr = NULL_TREE;
242 if (gimple_assign_single_p (stmt))
243 expr = gimple_assign_rhs1 (stmt);
244 propagate_tree_value (&expr, val);
245 gimple_assign_set_rhs_from_tree (gsi, expr);
246 }
247 else if (gimple_code (stmt) == GIMPLE_COND)
248 {
249 tree lhs = NULL_TREE;
250 tree rhs = build_zero_cst (TREE_TYPE (val));
251 propagate_tree_value (&lhs, val);
252 gimple_cond_set_code (stmt, NE_EXPR);
253 gimple_cond_set_lhs (stmt, lhs);
254 gimple_cond_set_rhs (stmt, rhs);
255 }
256 else if (is_gimple_call (stmt)
257 && gimple_call_lhs (stmt) != NULL_TREE)
258 {
259 tree expr = NULL_TREE;
260 bool res;
261 propagate_tree_value (&expr, val);
262 res = update_call_from_tree (gsi, expr);
263 gcc_assert (res);
264 }
265 else if (gimple_code (stmt) == GIMPLE_SWITCH)
266 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
267 else
268 gcc_unreachable ();
269 }
270
271 /*---------------------------------------------------------------------------
272 Copy propagation
273 ---------------------------------------------------------------------------*/
274 /* Lattice for copy-propagation. The lattice is initialized to
275 UNDEFINED (value == NULL) for SSA names that can become a copy
276 of something or VARYING (value == self) if not (see get_copy_of_val
277 and stmt_may_generate_copy). Other values make the name a COPY
278 of that value.
279
280 When visiting a statement or PHI node the lattice value for an
281 SSA name can transition from UNDEFINED to COPY to VARYING. */
282
283 struct prop_value_d {
284 /* Copy-of value. */
285 tree value;
286 };
287 typedef struct prop_value_d prop_value_t;
288
289 static prop_value_t *copy_of;
290
291
292 /* Return true if this statement may generate a useful copy. */
293
294 static bool
295 stmt_may_generate_copy (gimple stmt)
296 {
297 if (gimple_code (stmt) == GIMPLE_PHI)
298 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
299
300 if (gimple_code (stmt) != GIMPLE_ASSIGN)
301 return false;
302
303 /* If the statement has volatile operands, it won't generate a
304 useful copy. */
305 if (gimple_has_volatile_ops (stmt))
306 return false;
307
308 /* Statements with loads and/or stores will never generate a useful copy. */
309 if (gimple_vuse (stmt))
310 return false;
311
312 /* Otherwise, the only statements that generate useful copies are
313 assignments whose RHS is just an SSA name that doesn't flow
314 through abnormal edges. */
315 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
316 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
317 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
318 }
319
320
321 /* Return the copy-of value for VAR. */
322
323 static inline prop_value_t *
324 get_copy_of_val (tree var)
325 {
326 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
327
328 if (val->value == NULL_TREE
329 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
330 {
331 /* If the variable will never generate a useful copy relation,
332 make it its own copy. */
333 val->value = var;
334 }
335
336 return val;
337 }
338
339 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
340 of a copy. */
341
342 static inline tree
343 valueize_val (tree var)
344 {
345 if (TREE_CODE (var) == SSA_NAME)
346 {
347 tree val = get_copy_of_val (var)->value;
348 if (val)
349 return val;
350 }
351 return var;
352 }
353
354 /* Set VAL to be the copy of VAR. If that changed return true. */
355
356 static inline bool
357 set_copy_of_val (tree var, tree val)
358 {
359 unsigned int ver = SSA_NAME_VERSION (var);
360 tree old;
361
362 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
363 changed, return true. */
364 old = copy_of[ver].value;
365 copy_of[ver].value = val;
366
367 if (old != val
368 || (val && !operand_equal_p (old, val, 0)))
369 return true;
370
371 return false;
372 }
373
374
375 /* Dump the copy-of value for variable VAR to FILE. */
376
377 static void
378 dump_copy_of (FILE *file, tree var)
379 {
380 tree val;
381
382 print_generic_expr (file, var, dump_flags);
383 if (TREE_CODE (var) != SSA_NAME)
384 return;
385
386 val = copy_of[SSA_NAME_VERSION (var)].value;
387 fprintf (file, " copy-of chain: ");
388 print_generic_expr (file, var, 0);
389 fprintf (file, " ");
390 if (!val)
391 fprintf (file, "[UNDEFINED]");
392 else if (val == var)
393 fprintf (file, "[NOT A COPY]");
394 else
395 {
396 fprintf (file, "-> ");
397 print_generic_expr (file, val, 0);
398 fprintf (file, " ");
399 fprintf (file, "[COPY]");
400 }
401 }
402
403
404 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
405 value and store the LHS into *RESULT_P. */
406
407 static enum ssa_prop_result
408 copy_prop_visit_assignment (gimple stmt, tree *result_p)
409 {
410 tree lhs, rhs;
411
412 lhs = gimple_assign_lhs (stmt);
413 rhs = valueize_val (gimple_assign_rhs1 (stmt));
414
415 if (TREE_CODE (lhs) == SSA_NAME)
416 {
417 /* Straight copy between two SSA names. First, make sure that
418 we can propagate the RHS into uses of LHS. */
419 if (!may_propagate_copy (lhs, rhs))
420 return SSA_PROP_VARYING;
421
422 *result_p = lhs;
423 if (set_copy_of_val (*result_p, rhs))
424 return SSA_PROP_INTERESTING;
425 else
426 return SSA_PROP_NOT_INTERESTING;
427 }
428
429 return SSA_PROP_VARYING;
430 }
431
432
433 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
434 if it can determine which edge will be taken. Otherwise, return
435 SSA_PROP_VARYING. */
436
437 static enum ssa_prop_result
438 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
439 {
440 enum ssa_prop_result retval = SSA_PROP_VARYING;
441 location_t loc = gimple_location (stmt);
442
443 tree op0 = gimple_cond_lhs (stmt);
444 tree op1 = gimple_cond_rhs (stmt);
445
446 /* The only conditionals that we may be able to compute statically
447 are predicates involving two SSA_NAMEs. */
448 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
449 {
450 op0 = valueize_val (op0);
451 op1 = valueize_val (op1);
452
453 /* See if we can determine the predicate's value. */
454 if (dump_file && (dump_flags & TDF_DETAILS))
455 {
456 fprintf (dump_file, "Trying to determine truth value of ");
457 fprintf (dump_file, "predicate ");
458 print_gimple_stmt (dump_file, stmt, 0, 0);
459 }
460
461 /* We can fold COND and get a useful result only when we have
462 the same SSA_NAME on both sides of a comparison operator. */
463 if (op0 == op1)
464 {
465 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
466 boolean_type_node, op0, op1);
467 if (folded_cond)
468 {
469 basic_block bb = gimple_bb (stmt);
470 *taken_edge_p = find_taken_edge (bb, folded_cond);
471 if (*taken_edge_p)
472 retval = SSA_PROP_INTERESTING;
473 }
474 }
475 }
476
477 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
478 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
479 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
480
481 return retval;
482 }
483
484
485 /* Evaluate statement STMT. If the statement produces a new output
486 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
487 the new value in *RESULT_P.
488
489 If STMT is a conditional branch and we can determine its truth
490 value, set *TAKEN_EDGE_P accordingly.
491
492 If the new value produced by STMT is varying, return
493 SSA_PROP_VARYING. */
494
495 static enum ssa_prop_result
496 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
497 {
498 enum ssa_prop_result retval;
499
500 if (dump_file && (dump_flags & TDF_DETAILS))
501 {
502 fprintf (dump_file, "\nVisiting statement:\n");
503 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
504 fprintf (dump_file, "\n");
505 }
506
507 if (gimple_assign_single_p (stmt)
508 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
509 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
510 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
511 {
512 /* If the statement is a copy assignment, evaluate its RHS to
513 see if the lattice value of its output has changed. */
514 retval = copy_prop_visit_assignment (stmt, result_p);
515 }
516 else if (gimple_code (stmt) == GIMPLE_COND)
517 {
518 /* See if we can determine which edge goes out of a conditional
519 jump. */
520 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
521 }
522 else
523 retval = SSA_PROP_VARYING;
524
525 if (retval == SSA_PROP_VARYING)
526 {
527 tree def;
528 ssa_op_iter i;
529
530 /* Any other kind of statement is not interesting for constant
531 propagation and, therefore, not worth simulating. */
532 if (dump_file && (dump_flags & TDF_DETAILS))
533 fprintf (dump_file, "No interesting values produced.\n");
534
535 /* The assignment is not a copy operation. Don't visit this
536 statement again and mark all the definitions in the statement
537 to be copies of nothing. */
538 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
539 set_copy_of_val (def, def);
540 }
541
542 return retval;
543 }
544
545
546 /* Visit PHI node PHI. If all the arguments produce the same value,
547 set it to be the value of the LHS of PHI. */
548
549 static enum ssa_prop_result
550 copy_prop_visit_phi_node (gimple phi)
551 {
552 enum ssa_prop_result retval;
553 unsigned i;
554 prop_value_t phi_val = { NULL_TREE };
555
556 tree lhs = gimple_phi_result (phi);
557
558 if (dump_file && (dump_flags & TDF_DETAILS))
559 {
560 fprintf (dump_file, "\nVisiting PHI node: ");
561 print_gimple_stmt (dump_file, phi, 0, dump_flags);
562 }
563
564 for (i = 0; i < gimple_phi_num_args (phi); i++)
565 {
566 prop_value_t *arg_val;
567 tree arg_value;
568 tree arg = gimple_phi_arg_def (phi, i);
569 edge e = gimple_phi_arg_edge (phi, i);
570
571 /* We don't care about values flowing through non-executable
572 edges. */
573 if (!(e->flags & EDGE_EXECUTABLE))
574 continue;
575
576 /* Names that flow through abnormal edges cannot be used to
577 derive copies. */
578 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
579 {
580 phi_val.value = lhs;
581 break;
582 }
583
584 if (dump_file && (dump_flags & TDF_DETAILS))
585 {
586 fprintf (dump_file, "\tArgument #%d: ", i);
587 dump_copy_of (dump_file, arg);
588 fprintf (dump_file, "\n");
589 }
590
591 if (TREE_CODE (arg) == SSA_NAME)
592 {
593 arg_val = get_copy_of_val (arg);
594
595 /* If we didn't visit the definition of arg yet treat it as
596 UNDEFINED. This also handles PHI arguments that are the
597 same as lhs. We'll come here again. */
598 if (!arg_val->value)
599 continue;
600
601 arg_value = arg_val->value;
602 }
603 else
604 arg_value = valueize_val (arg);
605
606 /* Avoid copy propagation from an inner into an outer loop.
607 Otherwise, this may move loop variant variables outside of
608 their loops and prevent coalescing opportunities. If the
609 value was loop invariant, it will be hoisted by LICM and
610 exposed for copy propagation.
611 ??? The value will be always loop invariant.
612 In loop-closed SSA form do not copy-propagate through
613 PHI nodes in blocks with a loop exit edge predecessor. */
614 if (current_loops
615 && TREE_CODE (arg_value) == SSA_NAME
616 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
617 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
618 && loop_exit_edge_p (e->src->loop_father, e))))
619 {
620 phi_val.value = lhs;
621 break;
622 }
623
624 /* If the LHS didn't have a value yet, make it a copy of the
625 first argument we find. */
626 if (phi_val.value == NULL_TREE)
627 {
628 phi_val.value = arg_value;
629 continue;
630 }
631
632 /* If PHI_VAL and ARG don't have a common copy-of chain, then
633 this PHI node cannot be a copy operation. */
634 if (phi_val.value != arg_value
635 && !operand_equal_p (phi_val.value, arg_value, 0))
636 {
637 phi_val.value = lhs;
638 break;
639 }
640 }
641
642 if (phi_val.value
643 && may_propagate_copy (lhs, phi_val.value)
644 && set_copy_of_val (lhs, phi_val.value))
645 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
646 else
647 retval = SSA_PROP_NOT_INTERESTING;
648
649 if (dump_file && (dump_flags & TDF_DETAILS))
650 {
651 fprintf (dump_file, "PHI node ");
652 dump_copy_of (dump_file, lhs);
653 fprintf (dump_file, "\nTelling the propagator to ");
654 if (retval == SSA_PROP_INTERESTING)
655 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
656 else if (retval == SSA_PROP_VARYING)
657 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
658 else
659 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
660 fprintf (dump_file, "\n\n");
661 }
662
663 return retval;
664 }
665
666
667 /* Initialize structures used for copy propagation. */
668
669 static void
670 init_copy_prop (void)
671 {
672 basic_block bb;
673
674 copy_of = XCNEWVEC (prop_value_t, num_ssa_names);
675
676 FOR_EACH_BB (bb)
677 {
678 gimple_stmt_iterator si;
679 int depth = bb->loop_depth;
680
681 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
682 {
683 gimple stmt = gsi_stmt (si);
684 ssa_op_iter iter;
685 tree def;
686
687 /* The only statements that we care about are those that may
688 generate useful copies. We also need to mark conditional
689 jumps so that their outgoing edges are added to the work
690 lists of the propagator.
691
692 Avoid copy propagation from an inner into an outer loop.
693 Otherwise, this may move loop variant variables outside of
694 their loops and prevent coalescing opportunities. If the
695 value was loop invariant, it will be hoisted by LICM and
696 exposed for copy propagation.
697 ??? This doesn't make sense. */
698 if (stmt_ends_bb_p (stmt))
699 prop_set_simulate_again (stmt, true);
700 else if (stmt_may_generate_copy (stmt)
701 /* Since we are iterating over the statements in
702 BB, not the phi nodes, STMT will always be an
703 assignment. */
704 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
705 prop_set_simulate_again (stmt, true);
706 else
707 prop_set_simulate_again (stmt, false);
708
709 /* Mark all the outputs of this statement as not being
710 the copy of anything. */
711 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
712 if (!prop_simulate_again_p (stmt))
713 set_copy_of_val (def, def);
714 }
715
716 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
717 {
718 gimple phi = gsi_stmt (si);
719 tree def;
720
721 def = gimple_phi_result (phi);
722 if (!is_gimple_reg (def))
723 prop_set_simulate_again (phi, false);
724 else
725 prop_set_simulate_again (phi, true);
726
727 if (!prop_simulate_again_p (phi))
728 set_copy_of_val (def, def);
729 }
730 }
731 }
732
733 /* Callback for substitute_and_fold to get at the final copy-of values. */
734
735 static tree
736 get_value (tree name)
737 {
738 tree val = copy_of[SSA_NAME_VERSION (name)].value;
739 if (val && val != name)
740 return val;
741 return NULL_TREE;
742 }
743
744 /* Deallocate memory used in copy propagation and do final
745 substitution. */
746
747 static void
748 fini_copy_prop (void)
749 {
750 unsigned i;
751
752 /* Set the final copy-of value for each variable by traversing the
753 copy-of chains. */
754 for (i = 1; i < num_ssa_names; i++)
755 {
756 tree var = ssa_name (i);
757 if (!var
758 || !copy_of[i].value
759 || copy_of[i].value == var)
760 continue;
761
762 /* In theory the points-to solution of all members of the
763 copy chain is their intersection. For now we do not bother
764 to compute this but only make sure we do not lose points-to
765 information completely by setting the points-to solution
766 of the representative to the first solution we find if
767 it doesn't have one already. */
768 if (copy_of[i].value != var
769 && TREE_CODE (copy_of[i].value) == SSA_NAME
770 && POINTER_TYPE_P (TREE_TYPE (var))
771 && SSA_NAME_PTR_INFO (var)
772 && !SSA_NAME_PTR_INFO (copy_of[i].value))
773 duplicate_ssa_name_ptr_info (copy_of[i].value, SSA_NAME_PTR_INFO (var));
774 }
775
776 /* Don't do DCE if we have loops. That's the simplest way to not
777 destroy the scev cache. */
778 substitute_and_fold (get_value, NULL, !current_loops);
779
780 free (copy_of);
781 }
782
783
784 /* Main entry point to the copy propagator.
785
786 PHIS_ONLY is true if we should only consider PHI nodes as generating
787 copy propagation opportunities.
788
789 The algorithm propagates the value COPY-OF using ssa_propagate. For
790 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
791 from. The following example shows how the algorithm proceeds at a
792 high level:
793
794 1 a_24 = x_1
795 2 a_2 = PHI <a_24, x_1>
796 3 a_5 = PHI <a_2>
797 4 x_1 = PHI <x_298, a_5, a_2>
798
799 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
800 x_298. Propagation proceeds as follows.
801
802 Visit #1: a_24 is copy-of x_1. Value changed.
803 Visit #2: a_2 is copy-of x_1. Value changed.
804 Visit #3: a_5 is copy-of x_1. Value changed.
805 Visit #4: x_1 is copy-of x_298. Value changed.
806 Visit #1: a_24 is copy-of x_298. Value changed.
807 Visit #2: a_2 is copy-of x_298. Value changed.
808 Visit #3: a_5 is copy-of x_298. Value changed.
809 Visit #4: x_1 is copy-of x_298. Stable state reached.
810
811 When visiting PHI nodes, we only consider arguments that flow
812 through edges marked executable by the propagation engine. So,
813 when visiting statement #2 for the first time, we will only look at
814 the first argument (a_24) and optimistically assume that its value
815 is the copy of a_24 (x_1). */
816
817 static unsigned int
818 execute_copy_prop (void)
819 {
820 init_copy_prop ();
821 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
822 fini_copy_prop ();
823 return 0;
824 }
825
826 static bool
827 gate_copy_prop (void)
828 {
829 return flag_tree_copy_prop != 0;
830 }
831
832 struct gimple_opt_pass pass_copy_prop =
833 {
834 {
835 GIMPLE_PASS,
836 "copyprop", /* name */
837 gate_copy_prop, /* gate */
838 execute_copy_prop, /* execute */
839 NULL, /* sub */
840 NULL, /* next */
841 0, /* static_pass_number */
842 TV_TREE_COPY_PROP, /* tv_id */
843 PROP_ssa | PROP_cfg, /* properties_required */
844 0, /* properties_provided */
845 0, /* properties_destroyed */
846 0, /* todo_flags_start */
847 TODO_cleanup_cfg
848 | TODO_ggc_collect
849 | TODO_verify_ssa
850 | TODO_update_ssa /* todo_flags_finish */
851 }
852 };