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