comparison gcc/tree-ssa-copy.c @ 111:04ced10e8804

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