]> gcc.gnu.org Git - gcc.git/blob - gcc/tree-ssa-copy.c
Factor unrelated declarations out of tree.h.
[gcc.git] / gcc / tree-ssa-copy.c
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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)
9 any later version.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "gimple-pretty-print.h"
30 #include "gimple.h"
31 #include "gimple-iterator.h"
32 #include "gimple-ssa.h"
33 #include "tree-cfg.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
36 #include "stringpool.h"
37 #include "tree-ssanames.h"
38 #include "tree-pass.h"
39 #include "tree-ssa-propagate.h"
40 #include "langhooks.h"
41 #include "cfgloop.h"
42 #include "tree-scalar-evolution.h"
43 #include "tree-ssa-dom.h"
44
45 /* This file implements the copy propagation pass and provides a
46 handful of interfaces for performing const/copy propagation and
47 simple expression replacement which keep variable annotations
48 up-to-date.
49
50 We require that for any copy operation where the RHS and LHS have
51 a non-null memory tag the memory tag be the same. It is OK
52 for one or both of the memory tags to be NULL.
53
54 We also require tracking if a variable is dereferenced in a load or
55 store operation.
56
57 We enforce these requirements by having all copy propagation and
58 replacements of one SSA_NAME with a different SSA_NAME to use the
59 APIs defined in this file. */
60
61 /*---------------------------------------------------------------------------
62 Copy propagation
63 ---------------------------------------------------------------------------*/
64 /* Lattice for copy-propagation. The lattice is initialized to
65 UNDEFINED (value == NULL) for SSA names that can become a copy
66 of something or VARYING (value == self) if not (see get_copy_of_val
67 and stmt_may_generate_copy). Other values make the name a COPY
68 of that value.
69
70 When visiting a statement or PHI node the lattice value for an
71 SSA name can transition from UNDEFINED to COPY to VARYING. */
72
73 struct prop_value_d {
74 /* Copy-of value. */
75 tree value;
76 };
77 typedef struct prop_value_d prop_value_t;
78
79 static prop_value_t *copy_of;
80 static unsigned n_copy_of;
81
82
83 /* Return true if this statement may generate a useful copy. */
84
85 static bool
86 stmt_may_generate_copy (gimple stmt)
87 {
88 if (gimple_code (stmt) == GIMPLE_PHI)
89 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
90
91 if (gimple_code (stmt) != GIMPLE_ASSIGN)
92 return false;
93
94 /* If the statement has volatile operands, it won't generate a
95 useful copy. */
96 if (gimple_has_volatile_ops (stmt))
97 return false;
98
99 /* Statements with loads and/or stores will never generate a useful copy. */
100 if (gimple_vuse (stmt))
101 return false;
102
103 /* Otherwise, the only statements that generate useful copies are
104 assignments whose RHS is just an SSA name that doesn't flow
105 through abnormal edges. */
106 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
107 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
108 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
109 }
110
111
112 /* Return the copy-of value for VAR. */
113
114 static inline prop_value_t *
115 get_copy_of_val (tree var)
116 {
117 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
118
119 if (val->value == NULL_TREE
120 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
121 {
122 /* If the variable will never generate a useful copy relation,
123 make it its own copy. */
124 val->value = var;
125 }
126
127 return val;
128 }
129
130 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
131 of a copy. */
132
133 static inline tree
134 valueize_val (tree var)
135 {
136 if (TREE_CODE (var) == SSA_NAME)
137 {
138 tree val = get_copy_of_val (var)->value;
139 if (val)
140 return val;
141 }
142 return var;
143 }
144
145 /* Set VAL to be the copy of VAR. If that changed return true. */
146
147 static inline bool
148 set_copy_of_val (tree var, tree val)
149 {
150 unsigned int ver = SSA_NAME_VERSION (var);
151 tree old;
152
153 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
154 changed, return true. */
155 old = copy_of[ver].value;
156 copy_of[ver].value = val;
157
158 if (old != val
159 || (val && !operand_equal_p (old, val, 0)))
160 return true;
161
162 return false;
163 }
164
165
166 /* Dump the copy-of value for variable VAR to FILE. */
167
168 static void
169 dump_copy_of (FILE *file, tree var)
170 {
171 tree val;
172
173 print_generic_expr (file, var, dump_flags);
174 if (TREE_CODE (var) != SSA_NAME)
175 return;
176
177 val = copy_of[SSA_NAME_VERSION (var)].value;
178 fprintf (file, " copy-of chain: ");
179 print_generic_expr (file, var, 0);
180 fprintf (file, " ");
181 if (!val)
182 fprintf (file, "[UNDEFINED]");
183 else if (val == var)
184 fprintf (file, "[NOT A COPY]");
185 else
186 {
187 fprintf (file, "-> ");
188 print_generic_expr (file, val, 0);
189 fprintf (file, " ");
190 fprintf (file, "[COPY]");
191 }
192 }
193
194
195 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
196 value and store the LHS into *RESULT_P. */
197
198 static enum ssa_prop_result
199 copy_prop_visit_assignment (gimple stmt, tree *result_p)
200 {
201 tree lhs, rhs;
202
203 lhs = gimple_assign_lhs (stmt);
204 rhs = valueize_val (gimple_assign_rhs1 (stmt));
205
206 if (TREE_CODE (lhs) == SSA_NAME)
207 {
208 /* Straight copy between two SSA names. First, make sure that
209 we can propagate the RHS into uses of LHS. */
210 if (!may_propagate_copy (lhs, rhs))
211 return SSA_PROP_VARYING;
212
213 *result_p = lhs;
214 if (set_copy_of_val (*result_p, rhs))
215 return SSA_PROP_INTERESTING;
216 else
217 return SSA_PROP_NOT_INTERESTING;
218 }
219
220 return SSA_PROP_VARYING;
221 }
222
223
224 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
225 if it can determine which edge will be taken. Otherwise, return
226 SSA_PROP_VARYING. */
227
228 static enum ssa_prop_result
229 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
230 {
231 enum ssa_prop_result retval = SSA_PROP_VARYING;
232 location_t loc = gimple_location (stmt);
233
234 tree op0 = gimple_cond_lhs (stmt);
235 tree op1 = gimple_cond_rhs (stmt);
236
237 /* The only conditionals that we may be able to compute statically
238 are predicates involving two SSA_NAMEs. */
239 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
240 {
241 op0 = valueize_val (op0);
242 op1 = valueize_val (op1);
243
244 /* See if we can determine the predicate's value. */
245 if (dump_file && (dump_flags & TDF_DETAILS))
246 {
247 fprintf (dump_file, "Trying to determine truth value of ");
248 fprintf (dump_file, "predicate ");
249 print_gimple_stmt (dump_file, stmt, 0, 0);
250 }
251
252 /* We can fold COND and get a useful result only when we have
253 the same SSA_NAME on both sides of a comparison operator. */
254 if (op0 == op1)
255 {
256 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
257 boolean_type_node, op0, op1);
258 if (folded_cond)
259 {
260 basic_block bb = gimple_bb (stmt);
261 *taken_edge_p = find_taken_edge (bb, folded_cond);
262 if (*taken_edge_p)
263 retval = SSA_PROP_INTERESTING;
264 }
265 }
266 }
267
268 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
269 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
270 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
271
272 return retval;
273 }
274
275
276 /* Evaluate statement STMT. If the statement produces a new output
277 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
278 the new value in *RESULT_P.
279
280 If STMT is a conditional branch and we can determine its truth
281 value, set *TAKEN_EDGE_P accordingly.
282
283 If the new value produced by STMT is varying, return
284 SSA_PROP_VARYING. */
285
286 static enum ssa_prop_result
287 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
288 {
289 enum ssa_prop_result retval;
290
291 if (dump_file && (dump_flags & TDF_DETAILS))
292 {
293 fprintf (dump_file, "\nVisiting statement:\n");
294 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
295 fprintf (dump_file, "\n");
296 }
297
298 if (gimple_assign_single_p (stmt)
299 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
300 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
301 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
302 {
303 /* If the statement is a copy assignment, evaluate its RHS to
304 see if the lattice value of its output has changed. */
305 retval = copy_prop_visit_assignment (stmt, result_p);
306 }
307 else if (gimple_code (stmt) == GIMPLE_COND)
308 {
309 /* See if we can determine which edge goes out of a conditional
310 jump. */
311 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
312 }
313 else
314 retval = SSA_PROP_VARYING;
315
316 if (retval == SSA_PROP_VARYING)
317 {
318 tree def;
319 ssa_op_iter i;
320
321 /* Any other kind of statement is not interesting for constant
322 propagation and, therefore, not worth simulating. */
323 if (dump_file && (dump_flags & TDF_DETAILS))
324 fprintf (dump_file, "No interesting values produced.\n");
325
326 /* The assignment is not a copy operation. Don't visit this
327 statement again and mark all the definitions in the statement
328 to be copies of nothing. */
329 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
330 set_copy_of_val (def, def);
331 }
332
333 return retval;
334 }
335
336
337 /* Visit PHI node PHI. If all the arguments produce the same value,
338 set it to be the value of the LHS of PHI. */
339
340 static enum ssa_prop_result
341 copy_prop_visit_phi_node (gimple phi)
342 {
343 enum ssa_prop_result retval;
344 unsigned i;
345 prop_value_t phi_val = { NULL_TREE };
346
347 tree lhs = gimple_phi_result (phi);
348
349 if (dump_file && (dump_flags & TDF_DETAILS))
350 {
351 fprintf (dump_file, "\nVisiting PHI node: ");
352 print_gimple_stmt (dump_file, phi, 0, dump_flags);
353 }
354
355 for (i = 0; i < gimple_phi_num_args (phi); i++)
356 {
357 prop_value_t *arg_val;
358 tree arg_value;
359 tree arg = gimple_phi_arg_def (phi, i);
360 edge e = gimple_phi_arg_edge (phi, i);
361
362 /* We don't care about values flowing through non-executable
363 edges. */
364 if (!(e->flags & EDGE_EXECUTABLE))
365 continue;
366
367 /* Names that flow through abnormal edges cannot be used to
368 derive copies. */
369 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
370 {
371 phi_val.value = lhs;
372 break;
373 }
374
375 if (dump_file && (dump_flags & TDF_DETAILS))
376 {
377 fprintf (dump_file, "\tArgument #%d: ", i);
378 dump_copy_of (dump_file, arg);
379 fprintf (dump_file, "\n");
380 }
381
382 if (TREE_CODE (arg) == SSA_NAME)
383 {
384 arg_val = get_copy_of_val (arg);
385
386 /* If we didn't visit the definition of arg yet treat it as
387 UNDEFINED. This also handles PHI arguments that are the
388 same as lhs. We'll come here again. */
389 if (!arg_val->value)
390 continue;
391
392 arg_value = arg_val->value;
393 }
394 else
395 arg_value = valueize_val (arg);
396
397 /* Avoid copy propagation from an inner into an outer loop.
398 Otherwise, this may move loop variant variables outside of
399 their loops and prevent coalescing opportunities. If the
400 value was loop invariant, it will be hoisted by LICM and
401 exposed for copy propagation.
402 ??? The value will be always loop invariant.
403 In loop-closed SSA form do not copy-propagate through
404 PHI nodes in blocks with a loop exit edge predecessor. */
405 if (current_loops
406 && TREE_CODE (arg_value) == SSA_NAME
407 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
408 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
409 && loop_exit_edge_p (e->src->loop_father, e))))
410 {
411 phi_val.value = lhs;
412 break;
413 }
414
415 /* If the LHS didn't have a value yet, make it a copy of the
416 first argument we find. */
417 if (phi_val.value == NULL_TREE)
418 {
419 phi_val.value = arg_value;
420 continue;
421 }
422
423 /* If PHI_VAL and ARG don't have a common copy-of chain, then
424 this PHI node cannot be a copy operation. */
425 if (phi_val.value != arg_value
426 && !operand_equal_p (phi_val.value, arg_value, 0))
427 {
428 phi_val.value = lhs;
429 break;
430 }
431 }
432
433 if (phi_val.value
434 && may_propagate_copy (lhs, phi_val.value)
435 && set_copy_of_val (lhs, phi_val.value))
436 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
437 else
438 retval = SSA_PROP_NOT_INTERESTING;
439
440 if (dump_file && (dump_flags & TDF_DETAILS))
441 {
442 fprintf (dump_file, "PHI node ");
443 dump_copy_of (dump_file, lhs);
444 fprintf (dump_file, "\nTelling the propagator to ");
445 if (retval == SSA_PROP_INTERESTING)
446 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
447 else if (retval == SSA_PROP_VARYING)
448 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
449 else
450 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
451 fprintf (dump_file, "\n\n");
452 }
453
454 return retval;
455 }
456
457
458 /* Initialize structures used for copy propagation. */
459
460 static void
461 init_copy_prop (void)
462 {
463 basic_block bb;
464
465 n_copy_of = num_ssa_names;
466 copy_of = XCNEWVEC (prop_value_t, n_copy_of);
467
468 FOR_EACH_BB (bb)
469 {
470 gimple_stmt_iterator si;
471 int depth = bb_loop_depth (bb);
472
473 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
474 {
475 gimple stmt = gsi_stmt (si);
476 ssa_op_iter iter;
477 tree def;
478
479 /* The only statements that we care about are those that may
480 generate useful copies. We also need to mark conditional
481 jumps so that their outgoing edges are added to the work
482 lists of the propagator.
483
484 Avoid copy propagation from an inner into an outer loop.
485 Otherwise, this may move loop variant variables outside of
486 their loops and prevent coalescing opportunities. If the
487 value was loop invariant, it will be hoisted by LICM and
488 exposed for copy propagation.
489 ??? This doesn't make sense. */
490 if (stmt_ends_bb_p (stmt))
491 prop_set_simulate_again (stmt, true);
492 else if (stmt_may_generate_copy (stmt)
493 /* Since we are iterating over the statements in
494 BB, not the phi nodes, STMT will always be an
495 assignment. */
496 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
497 prop_set_simulate_again (stmt, true);
498 else
499 prop_set_simulate_again (stmt, false);
500
501 /* Mark all the outputs of this statement as not being
502 the copy of anything. */
503 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
504 if (!prop_simulate_again_p (stmt))
505 set_copy_of_val (def, def);
506 }
507
508 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
509 {
510 gimple phi = gsi_stmt (si);
511 tree def;
512
513 def = gimple_phi_result (phi);
514 if (virtual_operand_p (def))
515 prop_set_simulate_again (phi, false);
516 else
517 prop_set_simulate_again (phi, true);
518
519 if (!prop_simulate_again_p (phi))
520 set_copy_of_val (def, def);
521 }
522 }
523 }
524
525 /* Callback for substitute_and_fold to get at the final copy-of values. */
526
527 static tree
528 get_value (tree name)
529 {
530 tree val;
531 if (SSA_NAME_VERSION (name) >= n_copy_of)
532 return NULL_TREE;
533 val = copy_of[SSA_NAME_VERSION (name)].value;
534 if (val && val != name)
535 return val;
536 return NULL_TREE;
537 }
538
539 /* Deallocate memory used in copy propagation and do final
540 substitution. */
541
542 static void
543 fini_copy_prop (void)
544 {
545 unsigned i;
546
547 /* Set the final copy-of value for each variable by traversing the
548 copy-of chains. */
549 for (i = 1; i < num_ssa_names; i++)
550 {
551 tree var = ssa_name (i);
552 if (!var
553 || !copy_of[i].value
554 || copy_of[i].value == var)
555 continue;
556
557 /* In theory the points-to solution of all members of the
558 copy chain is their intersection. For now we do not bother
559 to compute this but only make sure we do not lose points-to
560 information completely by setting the points-to solution
561 of the representative to the first solution we find if
562 it doesn't have one already. */
563 if (copy_of[i].value != var
564 && TREE_CODE (copy_of[i].value) == SSA_NAME)
565 {
566 if (POINTER_TYPE_P (TREE_TYPE (var))
567 && SSA_NAME_PTR_INFO (var)
568 && !SSA_NAME_PTR_INFO (copy_of[i].value))
569 duplicate_ssa_name_ptr_info (copy_of[i].value,
570 SSA_NAME_PTR_INFO (var));
571 else if (!POINTER_TYPE_P (TREE_TYPE (var))
572 && SSA_NAME_RANGE_INFO (var)
573 && !SSA_NAME_RANGE_INFO (copy_of[i].value))
574 duplicate_ssa_name_range_info (copy_of[i].value,
575 SSA_NAME_RANGE_INFO (var));
576 }
577 }
578
579 /* Don't do DCE if SCEV is initialized. It would destroy the scev cache. */
580 substitute_and_fold (get_value, NULL, !scev_initialized_p ());
581
582 free (copy_of);
583 }
584
585
586 /* Main entry point to the copy propagator.
587
588 PHIS_ONLY is true if we should only consider PHI nodes as generating
589 copy propagation opportunities.
590
591 The algorithm propagates the value COPY-OF using ssa_propagate. For
592 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
593 from. The following example shows how the algorithm proceeds at a
594 high level:
595
596 1 a_24 = x_1
597 2 a_2 = PHI <a_24, x_1>
598 3 a_5 = PHI <a_2>
599 4 x_1 = PHI <x_298, a_5, a_2>
600
601 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
602 x_298. Propagation proceeds as follows.
603
604 Visit #1: a_24 is copy-of x_1. Value changed.
605 Visit #2: a_2 is copy-of x_1. Value changed.
606 Visit #3: a_5 is copy-of x_1. Value changed.
607 Visit #4: x_1 is copy-of x_298. Value changed.
608 Visit #1: a_24 is copy-of x_298. Value changed.
609 Visit #2: a_2 is copy-of x_298. Value changed.
610 Visit #3: a_5 is copy-of x_298. Value changed.
611 Visit #4: x_1 is copy-of x_298. Stable state reached.
612
613 When visiting PHI nodes, we only consider arguments that flow
614 through edges marked executable by the propagation engine. So,
615 when visiting statement #2 for the first time, we will only look at
616 the first argument (a_24) and optimistically assume that its value
617 is the copy of a_24 (x_1). */
618
619 static unsigned int
620 execute_copy_prop (void)
621 {
622 init_copy_prop ();
623 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
624 fini_copy_prop ();
625 return 0;
626 }
627
628 static bool
629 gate_copy_prop (void)
630 {
631 return flag_tree_copy_prop != 0;
632 }
633
634 namespace {
635
636 const pass_data pass_data_copy_prop =
637 {
638 GIMPLE_PASS, /* type */
639 "copyprop", /* name */
640 OPTGROUP_NONE, /* optinfo_flags */
641 true, /* has_gate */
642 true, /* has_execute */
643 TV_TREE_COPY_PROP, /* tv_id */
644 ( PROP_ssa | PROP_cfg ), /* properties_required */
645 0, /* properties_provided */
646 0, /* properties_destroyed */
647 0, /* todo_flags_start */
648 ( TODO_cleanup_cfg | TODO_verify_ssa
649 | TODO_update_ssa ), /* todo_flags_finish */
650 };
651
652 class pass_copy_prop : public gimple_opt_pass
653 {
654 public:
655 pass_copy_prop (gcc::context *ctxt)
656 : gimple_opt_pass (pass_data_copy_prop, ctxt)
657 {}
658
659 /* opt_pass methods: */
660 opt_pass * clone () { return new pass_copy_prop (m_ctxt); }
661 bool gate () { return gate_copy_prop (); }
662 unsigned int execute () { return execute_copy_prop (); }
663
664 }; // class pass_copy_prop
665
666 } // anon namespace
667
668 gimple_opt_pass *
669 make_pass_copy_prop (gcc::context *ctxt)
670 {
671 return new pass_copy_prop (ctxt);
672 }
This page took 0.07015 seconds and 6 git commands to generate.