1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "tree-pass.h"
28 #include "gimple-pretty-print.h"
29 #include "fold-const.h"
30 #include "gimple-iterator.h"
32 #include "tree-ssa-propagate.h"
34 #include "tree-scalar-evolution.h"
35 #include "tree-ssa-loop-niter.h"
36 #include "gimple-fold.h"
39 /* This file implements the copy propagation pass and provides a
40 handful of interfaces for performing const/copy propagation and
41 simple expression replacement which keep variable annotations
44 We require that for any copy operation where the RHS and LHS have
45 a non-null memory tag the memory tag be the same. It is OK
46 for one or both of the memory tags to be NULL.
48 We also require tracking if a variable is dereferenced in a load or
51 We enforce these requirements by having all copy propagation and
52 replacements of one SSA_NAME with a different SSA_NAME to use the
53 APIs defined in this file. */
55 /*---------------------------------------------------------------------------
57 ---------------------------------------------------------------------------*/
58 /* Lattice for copy-propagation. The lattice is initialized to
59 UNDEFINED (value == NULL) for SSA names that can become a copy
60 of something or VARYING (value == self) if not (see get_copy_of_val
61 and stmt_may_generate_copy). Other values make the name a COPY
64 When visiting a statement or PHI node the lattice value for an
65 SSA name can transition from UNDEFINED to COPY to VARYING. */
72 class copy_prop
: public ssa_propagation_engine
75 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) final override
;
76 enum ssa_prop_result
visit_phi (gphi
*) final override
;
79 static prop_value_t
*copy_of
;
80 static unsigned n_copy_of
;
83 /* Return true if this statement may generate a useful copy. */
86 stmt_may_generate_copy (gimple
*stmt
)
88 if (gimple_code (stmt
) == GIMPLE_PHI
)
89 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt
));
91 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
94 /* If the statement has volatile operands, it won't generate a
96 if (gimple_has_volatile_ops (stmt
))
99 /* Statements with loads and/or stores will never generate a useful copy. */
100 if (gimple_vuse (stmt
))
103 /* If the assignment is from a constant it generates a useful copy. */
104 if (gimple_assign_single_p (stmt
)
105 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
108 /* Otherwise, the only statements that generate useful copies are
109 assignments whose single SSA use doesn't flow through abnormal
111 tree rhs
= single_ssa_tree_operand (stmt
, SSA_OP_USE
);
112 return (rhs
&& !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
));
116 /* Return the copy-of value for VAR. */
118 static inline prop_value_t
*
119 get_copy_of_val (tree var
)
121 prop_value_t
*val
= ©_of
[SSA_NAME_VERSION (var
)];
123 if (val
->value
== NULL_TREE
124 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var
)))
126 /* If the variable will never generate a useful copy relation,
127 make it its own copy. */
134 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
138 valueize_val (tree var
)
140 if (TREE_CODE (var
) == SSA_NAME
)
142 tree val
= get_copy_of_val (var
)->value
;
149 /* Set VAL to be the copy of VAR. If that changed return true. */
152 set_copy_of_val (tree var
, tree val
)
154 unsigned int ver
= SSA_NAME_VERSION (var
);
157 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
158 changed, return true. */
159 old
= copy_of
[ver
].value
;
160 copy_of
[ver
].value
= val
;
163 && (!old
|| !operand_equal_p (old
, val
, 0)))
170 /* Dump the copy-of value for variable VAR to FILE. */
173 dump_copy_of (FILE *file
, tree var
)
177 print_generic_expr (file
, var
, dump_flags
);
178 if (TREE_CODE (var
) != SSA_NAME
)
181 val
= copy_of
[SSA_NAME_VERSION (var
)].value
;
182 fprintf (file
, " copy-of chain: ");
183 print_generic_expr (file
, var
);
186 fprintf (file
, "[UNDEFINED]");
188 fprintf (file
, "[NOT A COPY]");
191 fprintf (file
, "-> ");
192 print_generic_expr (file
, val
);
194 fprintf (file
, "[COPY]");
199 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
200 value and store the LHS into *RESULT_P. */
202 static enum ssa_prop_result
203 copy_prop_visit_assignment (gimple
*stmt
, tree
*result_p
)
205 tree lhs
= gimple_assign_lhs (stmt
);
206 tree rhs
= gimple_fold_stmt_to_constant_1 (stmt
, valueize_val
);
208 && (TREE_CODE (rhs
) == SSA_NAME
209 || is_gimple_min_invariant (rhs
)))
211 /* Straight copy between two SSA names or a constant. Make sure that
212 we can propagate the RHS into uses of LHS. */
213 if (!may_propagate_copy (lhs
, rhs
))
220 if (set_copy_of_val (*result_p
, rhs
))
221 return SSA_PROP_INTERESTING
;
222 return rhs
!= lhs
? SSA_PROP_NOT_INTERESTING
: SSA_PROP_VARYING
;
226 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
227 if it can determine which edge will be taken. Otherwise, return
230 static enum ssa_prop_result
231 copy_prop_visit_cond_stmt (gimple
*stmt
, edge
*taken_edge_p
)
233 enum ssa_prop_result retval
= SSA_PROP_VARYING
;
234 location_t loc
= gimple_location (stmt
);
236 tree op0
= valueize_val (gimple_cond_lhs (stmt
));
237 tree op1
= valueize_val (gimple_cond_rhs (stmt
));
239 /* See if we can determine the predicate's value. */
240 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
242 fprintf (dump_file
, "Trying to determine truth value of ");
243 fprintf (dump_file
, "predicate ");
244 print_gimple_stmt (dump_file
, stmt
, 0);
247 /* Fold COND and see whether we get a useful result. */
248 tree folded_cond
= fold_binary_loc (loc
, gimple_cond_code (stmt
),
249 boolean_type_node
, op0
, op1
);
252 basic_block bb
= gimple_bb (stmt
);
253 *taken_edge_p
= find_taken_edge (bb
, folded_cond
);
255 retval
= SSA_PROP_INTERESTING
;
258 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && *taken_edge_p
)
259 fprintf (dump_file
, "\nConditional will always take edge %d->%d\n",
260 (*taken_edge_p
)->src
->index
, (*taken_edge_p
)->dest
->index
);
266 /* Evaluate statement STMT. If the statement produces a new output
267 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
268 the new value in *RESULT_P.
270 If STMT is a conditional branch and we can determine its truth
271 value, set *TAKEN_EDGE_P accordingly.
273 If the new value produced by STMT is varying, return
277 copy_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*result_p
)
279 enum ssa_prop_result retval
;
281 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
283 fprintf (dump_file
, "\nVisiting statement:\n");
284 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
285 fprintf (dump_file
, "\n");
288 if (is_gimple_assign (stmt
)
289 && TREE_CODE (gimple_assign_lhs (stmt
)) == SSA_NAME
)
291 /* If the statement is a copy assignment, evaluate its RHS to
292 see if the lattice value of its output has changed. */
293 retval
= copy_prop_visit_assignment (stmt
, result_p
);
295 else if (gimple_code (stmt
) == GIMPLE_COND
)
297 /* See if we can determine which edge goes out of a conditional
299 retval
= copy_prop_visit_cond_stmt (stmt
, taken_edge_p
);
302 retval
= SSA_PROP_VARYING
;
304 if (retval
== SSA_PROP_VARYING
)
309 /* Any other kind of statement is not interesting for constant
310 propagation and, therefore, not worth simulating. */
311 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
312 fprintf (dump_file
, "No interesting values produced.\n");
314 /* The assignment is not a copy operation. Don't visit this
315 statement again and mark all the definitions in the statement
316 to be copies of nothing. */
317 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_ALL_DEFS
)
318 set_copy_of_val (def
, def
);
325 /* Visit PHI node PHI. If all the arguments produce the same value,
326 set it to be the value of the LHS of PHI. */
329 copy_prop::visit_phi (gphi
*phi
)
331 enum ssa_prop_result retval
;
333 prop_value_t phi_val
= { NULL_TREE
};
335 tree lhs
= gimple_phi_result (phi
);
337 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
339 fprintf (dump_file
, "\nVisiting PHI node: ");
340 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
343 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
345 prop_value_t
*arg_val
;
347 tree arg
= gimple_phi_arg_def (phi
, i
);
348 edge e
= gimple_phi_arg_edge (phi
, i
);
350 /* We don't care about values flowing through non-executable
352 if (!(e
->flags
& EDGE_EXECUTABLE
))
355 /* Names that flow through abnormal edges cannot be used to
357 if (TREE_CODE (arg
) == SSA_NAME
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg
))
363 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
365 fprintf (dump_file
, "\tArgument #%d: ", i
);
366 dump_copy_of (dump_file
, arg
);
367 fprintf (dump_file
, "\n");
370 if (TREE_CODE (arg
) == SSA_NAME
)
372 arg_val
= get_copy_of_val (arg
);
374 /* If we didn't visit the definition of arg yet treat it as
375 UNDEFINED. This also handles PHI arguments that are the
376 same as lhs. We'll come here again. */
380 arg_value
= arg_val
->value
;
383 arg_value
= valueize_val (arg
);
385 /* In loop-closed SSA form do not copy-propagate SSA-names across
387 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
)
388 && TREE_CODE (arg_value
) == SSA_NAME
389 && loop_exit_edge_p (e
->src
->loop_father
, e
))
395 /* If the LHS didn't have a value yet, make it a copy of the
396 first argument we find. */
397 if (phi_val
.value
== NULL_TREE
)
399 phi_val
.value
= arg_value
;
403 /* If PHI_VAL and ARG don't have a common copy-of chain, then
404 this PHI node cannot be a copy operation. */
405 if (phi_val
.value
!= arg_value
406 && !operand_equal_p (phi_val
.value
, arg_value
, 0))
414 && may_propagate_copy (lhs
, phi_val
.value
)
415 && set_copy_of_val (lhs
, phi_val
.value
))
416 retval
= (phi_val
.value
!= lhs
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
418 retval
= SSA_PROP_NOT_INTERESTING
;
420 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
422 fprintf (dump_file
, "PHI node ");
423 dump_copy_of (dump_file
, lhs
);
424 fprintf (dump_file
, "\nTelling the propagator to ");
425 if (retval
== SSA_PROP_INTERESTING
)
426 fprintf (dump_file
, "add SSA edges out of this PHI and continue.");
427 else if (retval
== SSA_PROP_VARYING
)
428 fprintf (dump_file
, "add SSA edges out of this PHI and never visit again.");
430 fprintf (dump_file
, "do nothing with SSA edges and keep iterating.");
431 fprintf (dump_file
, "\n\n");
438 /* Initialize structures used for copy propagation. */
441 init_copy_prop (void)
445 n_copy_of
= num_ssa_names
;
446 copy_of
= XCNEWVEC (prop_value_t
, n_copy_of
);
448 FOR_EACH_BB_FN (bb
, cfun
)
450 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
453 gimple
*stmt
= gsi_stmt (si
);
457 /* The only statements that we care about are those that may
458 generate useful copies. We also need to mark conditional
459 jumps so that their outgoing edges are added to the work
460 lists of the propagator. */
461 if (stmt_ends_bb_p (stmt
))
462 prop_set_simulate_again (stmt
, true);
463 else if (stmt_may_generate_copy (stmt
))
464 prop_set_simulate_again (stmt
, true);
466 prop_set_simulate_again (stmt
, false);
468 /* Mark all the outputs of this statement as not being
469 the copy of anything. */
470 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
471 if (!prop_simulate_again_p (stmt
))
472 set_copy_of_val (def
, def
);
475 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
478 gphi
*phi
= si
.phi ();
481 def
= gimple_phi_result (phi
);
482 if (virtual_operand_p (def
))
483 prop_set_simulate_again (phi
, false);
485 prop_set_simulate_again (phi
, true);
487 if (!prop_simulate_again_p (phi
))
488 set_copy_of_val (def
, def
);
493 class copy_folder
: public substitute_and_fold_engine
496 tree
value_of_expr (tree name
, gimple
*) final override
;
499 /* Callback for substitute_and_fold to get at the final copy-of values. */
502 copy_folder::value_of_expr (tree name
, gimple
*)
505 if (SSA_NAME_VERSION (name
) >= n_copy_of
)
507 val
= copy_of
[SSA_NAME_VERSION (name
)].value
;
508 if (val
&& val
!= name
)
513 /* Deallocate memory used in copy propagation and do final
517 fini_copy_prop (void)
522 /* Set the final copy-of value for each variable by traversing the
524 FOR_EACH_SSA_NAME (i
, var
, cfun
)
526 if (!copy_of
[i
].value
527 || copy_of
[i
].value
== var
)
530 /* Duplicate points-to and range info appropriately. */
531 if (copy_of
[i
].value
!= var
532 && TREE_CODE (copy_of
[i
].value
) == SSA_NAME
)
533 maybe_duplicate_ssa_info_at_copy (var
, copy_of
[i
].value
);
536 class copy_folder copy_folder
;
537 bool changed
= copy_folder
.substitute_and_fold ();
540 free_numbers_of_iterations_estimates (cfun
);
541 if (scev_initialized_p ())
551 /* Main entry point to the copy propagator.
553 PHIS_ONLY is true if we should only consider PHI nodes as generating
554 copy propagation opportunities.
556 The algorithm propagates the value COPY-OF using ssa_propagate. For
557 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
558 from. The following example shows how the algorithm proceeds at a
562 2 a_2 = PHI <a_24, x_1>
564 4 x_1 = PHI <x_298, a_5, a_2>
566 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
567 x_298. Propagation proceeds as follows.
569 Visit #1: a_24 is copy-of x_1. Value changed.
570 Visit #2: a_2 is copy-of x_1. Value changed.
571 Visit #3: a_5 is copy-of x_1. Value changed.
572 Visit #4: x_1 is copy-of x_298. Value changed.
573 Visit #1: a_24 is copy-of x_298. Value changed.
574 Visit #2: a_2 is copy-of x_298. Value changed.
575 Visit #3: a_5 is copy-of x_298. Value changed.
576 Visit #4: x_1 is copy-of x_298. Stable state reached.
578 When visiting PHI nodes, we only consider arguments that flow
579 through edges marked executable by the propagation engine. So,
580 when visiting statement #2 for the first time, we will only look at
581 the first argument (a_24) and optimistically assume that its value
582 is the copy of a_24 (x_1). */
585 execute_copy_prop (void)
588 class copy_prop copy_prop
;
589 copy_prop
.ssa_propagate ();
590 if (fini_copy_prop ())
591 return TODO_cleanup_cfg
;
597 const pass_data pass_data_copy_prop
=
599 GIMPLE_PASS
, /* type */
600 "copyprop", /* name */
601 OPTGROUP_NONE
, /* optinfo_flags */
602 TV_TREE_COPY_PROP
, /* tv_id */
603 ( PROP_ssa
| PROP_cfg
), /* properties_required */
604 0, /* properties_provided */
605 0, /* properties_destroyed */
606 0, /* todo_flags_start */
607 0, /* todo_flags_finish */
610 class pass_copy_prop
: public gimple_opt_pass
613 pass_copy_prop (gcc::context
*ctxt
)
614 : gimple_opt_pass (pass_data_copy_prop
, ctxt
)
617 /* opt_pass methods: */
618 opt_pass
* clone () final override
{ return new pass_copy_prop (m_ctxt
); }
619 bool gate (function
*) final override
{ return flag_tree_copy_prop
!= 0; }
620 unsigned int execute (function
*) final override
622 return execute_copy_prop ();
625 }; // class pass_copy_prop
630 make_pass_copy_prop (gcc::context
*ctxt
)
632 return new pass_copy_prop (ctxt
);